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

Fixed current format being lost on an inserted emoji #1038

Merged
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
9 changes: 6 additions & 3 deletions packages/koenig-lexical/src/plugins/EmojiPickerPlugin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ export function EmojiPickerPlugin() {
}

const currentNode = selection.anchor.getNode();

// need to replace the last text matching the :test: pattern with a single emoji
const shortcodeLength = emoji.id.length + 1; // +1 for the end colon
currentNode.spliceText(selection.anchor.offset - shortcodeLength, shortcodeLength, emoji.skins[0].native, true);
const textNode = currentNode.spliceText(selection.anchor.offset - shortcodeLength, shortcodeLength, emoji.skins[0].native, true);
textNode.setFormat(selection.format);
});
}, [editor]);

Expand Down Expand Up @@ -118,7 +118,10 @@ export function EmojiPickerPlugin() {
nodeToRemove.remove();
}

selection.insertNodes([$createTextNode(selectedOption.skins[0].native)]);
const emojiNode = $createTextNode(selectedOption.skins[0].native);
emojiNode.setFormat(selection.format);

selection.insertNodes([emojiNode]);

closeMenu();
});
Expand Down
21 changes: 19 additions & 2 deletions packages/koenig-lexical/test/e2e/plugins/EmojiPickerPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test.describe('Emoji Picker Plugin', async function () {
await page.keyboard.press('Enter');
await assertHTML(page, '<p dir="ltr"><span data-lexical-text="true">🌮</span></p>');
});

test('can use the mouse to select an emoji', async function () {
await focusEditor(page);

Expand All @@ -101,7 +101,7 @@ test.describe('Emoji Picker Plugin', async function () {
await expect(page.getByTestId('emoji-menu')).not.toBeVisible();
await assertHTML(page, '<p dir="ltr"><span data-lexical-text="true">🌮</span></p>');
});

test('can use punctuation', async function () {
await focusEditor(page);

Expand All @@ -124,6 +124,23 @@ test.describe('Emoji Picker Plugin', async function () {
await assertHTML(page, '<p dir="ltr"><span data-lexical-text="true">🌮🌮s for all</span></p>');
});

test('emojis retain text formatting on menu insert', async function () {
await focusEditor(page);
await page.keyboard.press('Control+Alt+H');
await page.keyboard.type('Test :heart', {delay: 10});
await page.keyboard.press('Enter');

await assertHTML(page, '<p dir="ltr"><mark data-lexical-text="true"><span>Test ❤️</span></mark></p>');
});

test('emojis retain text formatting on : completion', async function () {
await focusEditor(page);
await page.keyboard.press('Control+Alt+H');
await page.keyboard.type('Test :heart:', {delay: 10});

await assertHTML(page, '<p dir="ltr"><mark data-lexical-text="true"><span>Test ❤️</span></mark></p>');
});

test(`can use emojis in nested editors`, async function () {
await focusEditor(page);

Expand Down
Loading