Skip to content

Commit

Permalink
Fixed current format being lost on an inserted emoji
Browse files Browse the repository at this point in the history
no issue

- apply the current selection's format to the newly inserted emoji to avoid formatting ending and restarting around the emoji
  • Loading branch information
kevinansfield committed Nov 1, 2023
1 parent 6686422 commit bb09e60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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

0 comments on commit bb09e60

Please sign in to comment.