Browse Source

Handle pasted HTML document

master
morethanwords 3 years ago
parent
commit
7c67d80dab
  1. 21
      src/components/inputField.ts
  2. 8
      src/helpers/dom/getRichElementValue.ts

21
src/components/inputField.ts

@ -24,11 +24,28 @@ let init = () => { @@ -24,11 +24,28 @@ let init = () => {
let text: string, entities: MessageEntity[];
// @ts-ignore
const html: string = (e.originalEvent || e).clipboardData.getData('text/html');
let html: string = (e.originalEvent || e).clipboardData.getData('text/html');
if(html.trim()) {
const span = document.createElement('span');
const match = html.match(/<body>([\s\S]*)<\/body>/);
if(match) {
html = match[1].trim();
}
let span: HTMLElement = document.createElement('span');
span.innerHTML = html;
let curChild = span.firstChild;
while(curChild) { // * fix whitespace between elements like <p>asd</p>\n<p>zxc</p>
let nextSibling = curChild.nextSibling;
if(curChild.nodeType === 3) {
if(!curChild.nodeValue.trim()) {
curChild.remove();
}
}
curChild = nextSibling;
}
const richValue = getRichValue(span, true);
text = richValue.value;
entities = richValue.entities;

8
src/helpers/dom/getRichElementValue.ts

@ -138,7 +138,6 @@ export default function getRichElementValue(node: HTMLElement, lines: string[], @@ -138,7 +138,6 @@ export default function getRichElementValue(node: HTMLElement, lines: string[],
}
offset.offset += nodeValue.length;
return;
}
@ -174,13 +173,14 @@ export default function getRichElementValue(node: HTMLElement, lines: string[], @@ -174,13 +173,14 @@ export default function getRichElementValue(node: HTMLElement, lines: string[],
line.push('\x01');
}
if(isBlock && line.length) {
const wasLength = line.length;
if(isBlock && wasLength) {
lines.push(line.join(''));
line.splice(0, line.length);
line.splice(0, wasLength);
++offset.offset;
}
if(node.tagName === 'P') {
if(wasLength && node.tagName === 'P' && node.nextSibling) {
lines.push('');
++offset.offset;
}

Loading…
Cancel
Save