Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/lexical-clipboard/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
$getTextPointCaret,
$isElementNode,
$isRangeSelection,
$isRootNode,
$isTextNode,
$isTextPointCaret,
$parseSerializedNode,
Expand Down Expand Up @@ -480,6 +481,40 @@ function exportNodeToJSON<T extends LexicalNode>(node: T): BaseSerializedNode {
return serializedNode;
}

function $shouldIncludeElementWithSelectedChildren(
node: LexicalNode,
selection: BaseSelection | null,
): boolean {
if (
!$isRangeSelection(selection) ||
!$isElementNode(node) ||
$isRootNode(node) ||
node.isInline()
) {
return false;
}
const selectedNodes = selection.getNodes();
if (selectedNodes.length === 0) {
return false;
}
const selectedTextContent = selectedNodes
.filter(selectedNode => node.isParentOf(selectedNode))
.map(selectedNode => {
if ($isTextNode(selectedNode)) {
return $sliceSelectedTextNodeContent(
selection,
selectedNode,
'clone',
).getTextContent();
}
return selectedNode.getTextContent();
})
.join('');
return (
selectedTextContent !== '' && selectedTextContent === node.getTextContent()
);
}

function $appendNodesToJSON(
editor: LexicalEditor,
selection: BaseSelection | null,
Expand Down Expand Up @@ -526,6 +561,11 @@ function $appendNodesToJSON(

if (shouldInclude && !shouldExclude) {
targetArray.push(serializedNode);
} else if (
!shouldExclude &&
$shouldIncludeElementWithSelectedChildren(currentNode, selection)
) {
targetArray.push(serializedNode);
} else if (Array.isArray(serializedNode.children)) {
for (let i = 0; i < serializedNode.children.length; i++) {
const serializedChildNode = serializedNode.children[i];
Expand Down
44 changes: 43 additions & 1 deletion packages/lexical/src/__tests__/unit/HTMLCopyAndPaste.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
*
*/

import {$insertDataTransferForRichText} from '@lexical/clipboard';
import {
$getClipboardDataFromSelection,
$insertDataTransferForRichText,
setLexicalClipboardDataTransfer,
} from '@lexical/clipboard';
import {$patchStyleText} from '@lexical/selection';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isRangeSelection,
Expand Down Expand Up @@ -143,6 +148,43 @@ describe('HTMLCopyAndPaste tests', () => {
});
});

test('preserves paragraph alignment when pasting into an unformatted paragraph', async () => {
const {editor} = testEnv;
const dataTransfer = new DataTransfer();

await editor.update(() => {
const root = $getRoot();
root.clear();
const text = $createTextNode('Centered');
const source = $createParagraphNode()
.setFormat('center')
.append(text);
const target = $createParagraphNode().append(
$createTextNode('Target'),
);
root.append(source, target);
text.select(0, text.getTextContentSize());

setLexicalClipboardDataTransfer(
dataTransfer,
$getClipboardDataFromSelection(),
);

target.select(0, target.getChildrenSize());

const selection = $getSelection();
invariant(
$isRangeSelection(selection),
'isRangeSelection(selection)',
);
$insertDataTransferForRichText(dataTransfer, selection, editor);
});

expect(testEnv.innerHTML).toBe(
'<p dir="auto" style="text-align: center;"><span data-lexical-text="true">Centered</span></p><p dir="auto" style="text-align: center;"><span data-lexical-text="true">Centered</span></p>',
);
});

test('iOS fix: Word predictions should be handled as plain text to maintain selection formatting', async () => {
const {editor} = testEnv;

Expand Down
Loading