Skip to content

Commit f06e146

Browse files
authored
[lexical-table] Fix table selection paste as plain text (#6548)
1 parent 149806b commit f06e146

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

packages/lexical-table/src/LexicalTableSelection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,13 @@ export class TableSelection implements BaseSelection {
331331
}
332332

333333
getTextContent(): string {
334-
const nodes = this.getNodes();
334+
const nodes = this.getNodes().filter((node) => $isTableCellNode(node));
335335
let textContent = '';
336336
for (let i = 0; i < nodes.length; i++) {
337-
textContent += nodes[i].getTextContent();
337+
const node = nodes[i];
338+
const row = node.__parent;
339+
const nextRow = (nodes[i + 1] || {}).__parent;
340+
textContent += node.getTextContent() + (nextRow !== row ? '\n' : '\t');
338341
}
339342
return textContent;
340343
}

packages/lexical-table/src/__tests__/unit/LexicalTableNode.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,56 @@ describe('LexicalTableNode tests', () => {
294294
`<p><br></p><table><tr><th><p><br></p></th><th><p><br></p></th><th><p><br></p></th><th><p><br></p></th></tr><tr><th><p><br></p></th><td><p><br></p></td><td><p><br></p></td><td><p><br></p></td></tr><tr><th><p><br></p></th><td><p><br></p></td><td><p><br></p></td><td><p><br></p></td></tr><tr><th><p><br></p></th><td><p><br></p></td><td><p><br></p></td><td><p><br></p></td></tr></table>`,
295295
);
296296
});
297+
298+
test('Table plain text output validation', async () => {
299+
const {editor} = testEnv;
300+
301+
await editor.update(() => {
302+
const root = $getRoot();
303+
const table = $createTableNodeWithDimensions(4, 4, true);
304+
root.append(table);
305+
});
306+
await editor.update(() => {
307+
const root = $getRoot();
308+
const table = root.getLastChild<TableNode>();
309+
if (table) {
310+
const DOMTable = $getElementForTableNode(editor, table);
311+
if (DOMTable) {
312+
table
313+
?.getCellNodeFromCords(0, 0, DOMTable)
314+
?.getLastChild<ParagraphNode>()
315+
?.append($createTextNode('1'));
316+
table
317+
?.getCellNodeFromCords(1, 0, DOMTable)
318+
?.getLastChild<ParagraphNode>()
319+
?.append($createTextNode(''));
320+
table
321+
?.getCellNodeFromCords(2, 0, DOMTable)
322+
?.getLastChild<ParagraphNode>()
323+
?.append($createTextNode('2'));
324+
table
325+
?.getCellNodeFromCords(0, 1, DOMTable)
326+
?.getLastChild<ParagraphNode>()
327+
?.append($createTextNode('3'));
328+
table
329+
?.getCellNodeFromCords(1, 1, DOMTable)
330+
?.getLastChild<ParagraphNode>()
331+
?.append($createTextNode('4'));
332+
table
333+
?.getCellNodeFromCords(2, 1, DOMTable)
334+
?.getLastChild<ParagraphNode>()
335+
?.append($createTextNode(''));
336+
const selection = $createTableSelection();
337+
selection.set(
338+
table.__key,
339+
table?.getCellNodeFromCords(0, 0, DOMTable)?.__key || '',
340+
table?.getCellNodeFromCords(2, 1, DOMTable)?.__key || '',
341+
);
342+
expect(selection.getTextContent()).toBe(`1\t\t2\n3\t4\t\n`);
343+
}
344+
}
345+
});
346+
});
297347
},
298348
undefined,
299349
<TablePlugin />,

0 commit comments

Comments
 (0)