Skip to content
Open
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
51 changes: 49 additions & 2 deletions packages/lexical-clipboard/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,62 @@ export function $insertGeneratedNodes(
nodes: Array<LexicalNode>,
selection: BaseSelection,
): void {
// If the selection is entirely within a link,
// we want to insert the text content of the nodes into the link
function isSameLinkParent(sel: BaseSelection): boolean {
const points = sel.getStartEndPoints();
if (points == null) {
return false;
}

const startPoint = points[0];
const endPoint = points[1];

if (startPoint == null || endPoint == null) {
return false;
}

const startNode = startPoint.getNode();
const endNode = endPoint.getNode();

if (startNode == null || endNode == null) {
return false;
}

const parentElementBefore = startNode.getParent();
const parentElementAfter = endNode.getParent();

if (parentElementBefore == null || parentElementAfter == null) {
return false;
}

return (
parentElementAfter.getType() === 'link' &&
parentElementAfter.getType() === parentElementBefore.getType()
);
Comment on lines +257 to +260
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an acceptable approach. @lexical/clipboard can't depend on @lexical/link, and you can't use getType to check the type of a node since nodes can be subclasses with a different type.

}

if (
!editor.dispatchCommand(SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, {
nodes,
selection,
})
) {
selection.insertNodes(nodes);
$updateSelectionOnInsert(selection);
if (isSameLinkParent(selection)) {
// Only insert the content of the first text node
const textNodes = nodes.filter((node) => $isTextNode(node));

if (textNodes.length > 0) {
const firstText = textNodes[0];
selection.insertText(firstText.getTextContent());
$updateSelectionOnInsert(selection);
}
} else {
selection.insertNodes(nodes);
$updateSelectionOnInsert(selection);
}
Comment on lines +271 to +281
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look correct since all other nodes will be discarded if one TextNode exists. It also ignores any formatting.

}

return;
}

Expand Down