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

Mutations to respect canInsertBeforeAfter #4553

Merged
merged 5 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions packages/lexical-playground/__tests__/e2e/Composition.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -719,5 +719,41 @@ test.describe('Composition', () => {

expect(isFloatingToolbarDisplayed).toEqual(true);
});

test('Typing after mention with IME should not break it', async ({
page,
browserName,
isPlainText,
}) => {
await focusEditor(page);

await page.keyboard.type('Luke');
await waitForSelector(page, '#typeahead-menu ul li');
await page.keyboard.press('Enter');

await page.pause();
await page.keyboard.imeSetComposition('s', 0, 1);
await page.keyboard.imeSetComposition('す', 0, 1);
await page.keyboard.imeSetComposition('すs', 0, 2);
await page.keyboard.imeSetComposition('すsh', 0, 3);
await page.keyboard.imeSetComposition('すsh', 0, 4);

await assertHTML(
page,
html`
<p
class="PlaygroundEditorTheme__paragraph PlaygroundEditorTheme__ltr"
dir="ltr">
<span
class="mention"
style="background-color: rgba(24, 119, 232, 0.2)"
data-lexical-text="true">
Luke Skywalker
</span>
<span data-lexical-text="true">すsh​</span>
</p>
`,
);
});
});
});
8 changes: 8 additions & 0 deletions packages/lexical-playground/src/nodes/MentionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export class MentionNode extends TextNode {
isTextEntity(): true {
return true;
}

canInsertTextBefore(): boolean {
return false;
}

canInsertTextAfter(): boolean {
return false;
}
}

export function $createMentionNode(mentionName: string): MentionNode {
Expand Down
15 changes: 11 additions & 4 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ export function $updateTextNodeFromDOMContent(
}
const parent = node.getParent();
const prevSelection = $getPreviousSelection();
const prevTextContentSize = node.getTextContentSize();
const compositionKey = $getCompositionKey();
const nodeKey = node.getKey();

Expand All @@ -646,10 +647,16 @@ export function $updateTextNodeFromDOMContent(
// Check if character was added at the start, and we need
// to clear this input from occurring as that action wasn't
// permitted.
(parent !== null &&
$isRangeSelection(prevSelection) &&
!parent.canInsertTextBefore() &&
prevSelection.anchor.offset === 0)
($isRangeSelection(prevSelection) &&
((parent !== null &&
!parent.canInsertTextBefore() &&
prevSelection.anchor.offset === 0) ||
(prevSelection.anchor.key === textNode.__key &&
prevSelection.anchor.offset === 0 &&
!node.canInsertTextBefore()) ||
(prevSelection.focus.key === textNode.__key &&
prevSelection.focus.offset === prevTextContentSize &&
!node.canInsertTextAfter())))
) {
node.markDirty();
return;
Expand Down