Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical] Bug Fix: Merge pasted paragraph into empty quote #6367

Merged
merged 15 commits into from
Aug 2, 2024
Merged
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
4 changes: 4 additions & 0 deletions packages/lexical-list/src/LexicalListItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ export class ListItemNode extends ElementNode {
createParentElementNode(): ElementNode {
return $createListNode('bullet');
}

canMergeWhenEmpty(): true {
return true;
}
}

function $setListItemThemeClassNames(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,4 +914,37 @@ test.describe('CopyAndPaste', () => {
`,
);
});

test('Copy and paste paragraph into quote', async ({page, isPlainText}) => {
test.skip(isPlainText);
await focusEditor(page);

await page.keyboard.type('Hello world');
await page.keyboard.press('Enter');
await page.keyboard.type('Some text');

await selectAll(page);

const clipboard = await copyToClipboard(page);

await page.keyboard.type('> ');

await pasteFromClipboard(page, clipboard);

await assertHTML(
page,
html`
<blockquote
class="PlaygroundEditorTheme__quote PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">Hello world</span>
</blockquote>
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span data-lexical-text="true">Some text</span>
</p>
`,
);
});
});
4 changes: 4 additions & 0 deletions packages/lexical-rich-text/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export class QuoteNode extends ElementNode {
this.replace(paragraph);
return true;
}

canMergeWhenEmpty(): true {
return true;
}
}

export function $createQuoteNode(): QuoteNode {
Expand Down
6 changes: 2 additions & 4 deletions packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,14 +1255,12 @@ export class RangeSelection implements BaseSelection {
const blocksParent = $wrapInlineNodes(nodes);
const nodeToSelect = blocksParent.getLastDescendant()!;
const blocks = blocksParent.getChildren();
const isLI = (node: LexicalNode) =>
'__value' in node && '__checked' in node;
const isMergeable = (node: LexicalNode): node is ElementNode =>
$isElementNode(node) &&
INTERNAL_$isBlock(node) &&
!node.isEmpty() &&
$isElementNode(firstBlock) &&
(!firstBlock.isEmpty() || isLI(firstBlock));
(!firstBlock.isEmpty() || firstBlock.canMergeWhenEmpty());

const shouldInsert = !$isElementNode(firstBlock) || !firstBlock.isEmpty();
const insertedParagraph = shouldInsert ? this.insertParagraph() : null;
Expand All @@ -1284,7 +1282,7 @@ export class RangeSelection implements BaseSelection {
if (
insertedParagraph &&
$isElementNode(lastInsertedBlock) &&
(isLI(insertedParagraph) || INTERNAL_$isBlock(lastToInsert))
(insertedParagraph.canMergeWhenEmpty() || INTERNAL_$isBlock(lastToInsert))
) {
lastInsertedBlock.append(...insertedParagraph.getChildren());
insertedParagraph.remove();
Expand Down
17 changes: 17 additions & 0 deletions packages/lexical/src/nodes/LexicalElementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,23 @@ export class ElementNode extends LexicalNode {
): boolean {
return false;
}

/**
* Determines whether this node, when empty, can merge with a first block
* of nodes being inserted.
*
* This method is specifically called in {@link RangeSelection.insertNodes}
* to determine merging behavior during nodes insertion.
*
* @example
* // In a ListItemNode or QuoteNode implementation:
* canMergeWhenEmpty(): true {
* return true;
* }
*/
canMergeWhenEmpty(): boolean {
return false;
}
}

export function $isElementNode(
Expand Down
Loading