Fix pasting tables

This commit is contained in:
morethanwords 2022-01-15 06:28:29 +04:00
parent 443822884d
commit 6a913e9271

View File

@ -47,7 +47,7 @@ export const markdownTags: {[type in MarkdownType]: MarkdownTag} = {
} }
}; };
const tabulationMatch = '[style*="table-cell"]'; const tabulationMatch = '[style*="table-cell"], th, td';
/* export function getDepth(child: Node, container?: Node) { /* export function getDepth(child: Node, container?: Node) {
let depth = 0; let depth = 0;
@ -75,18 +75,19 @@ const BLOCK_TAG_NAMES = new Set([
'H3', 'H3',
'H2', 'H2',
'H1', 'H1',
'TR'
]); ]);
export default function getRichElementValue(node: HTMLElement, lines: string[], line: string[], selNode?: Node, selOffset?: number, entities?: MessageEntity[], offset = {offset: 0}) { export default function getRichElementValue(node: HTMLElement, lines: string[], line: string[], selNode?: Node, selOffset?: number, entities?: MessageEntity[], offset = {offset: 0}) {
if(node.nodeType === 3) { // TEXT if(node.nodeType === 3) { // TEXT
let nodeValue = node.nodeValue; let nodeValue = node.nodeValue;
const tabulation = node.parentElement?.closest(tabulationMatch + ', [contenteditable]'); /* const tabulation = node.parentElement?.closest(tabulationMatch + ', [contenteditable]');
if(tabulation?.getAttribute('contenteditable') === null) { if(tabulation?.getAttribute('contenteditable') === null) {
nodeValue += ' '; nodeValue += ' ';
// line.push('\t'); // line.push('\t');
// ++offset.offset; // ++offset.offset;
} } */
if(selNode === node) { if(selNode === node) {
line.push(nodeValue.substr(0, selOffset) + '\x01' + nodeValue.substr(selOffset)); line.push(nodeValue.substr(0, selOffset) + '\x01' + nodeValue.substr(selOffset));
@ -163,6 +164,9 @@ export default function getRichElementValue(node: HTMLElement, lines: string[],
line.push('\x01'); line.push('\x01');
} }
const isTableCell = node.matches(tabulationMatch);
const wasEntitiesLength = entities?.length;
let curChild = node.firstChild as HTMLElement; let curChild = node.firstChild as HTMLElement;
while(curChild) { while(curChild) {
getRichElementValue(curChild, lines, line, selNode, selOffset, entities, offset); getRichElementValue(curChild, lines, line, selNode, selOffset, entities, offset);
@ -173,6 +177,18 @@ export default function getRichElementValue(node: HTMLElement, lines: string[],
line.push('\x01'); line.push('\x01');
} }
if(isTableCell && node.nextSibling) {
line.push(' ');
++offset.offset;
// * combine entities such as url after adding space
if(wasEntitiesLength !== undefined) {
for(let i = wasEntitiesLength, length = entities.length; i < length; ++i) {
++entities[i].length;
}
}
}
const wasLength = line.length; const wasLength = line.length;
if(isBlock && wasLength) { if(isBlock && wasLength) {
lines.push(line.join('')); lines.push(line.join(''));