Skip to content

Commit

Permalink
feat(lexical-react): insert space after HashtagNode when next sibling…
Browse files Browse the repository at this point in the history
… starts with #
  • Loading branch information
strdr4605 committed Dec 1, 2023
1 parent 163fd17 commit 2fe70f5
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions packages/lexical-react/src/LexicalHashtagPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
*
*/

import type {TextNode} from 'lexical';

import {$createHashtagNode, HashtagNode} from '@lexical/hashtag';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useLexicalTextEntity} from '@lexical/react/useLexicalTextEntity';
import {type TextNode, $getNodeByKey, $isTextNode} from 'lexical';
import {useCallback, useEffect} from 'react';

function getHashtagRegexStringChars(): Readonly<{
Expand Down Expand Up @@ -257,6 +256,30 @@ export function HashtagPlugin(): JSX.Element | null {
}
}, [editor]);

useEffect(() => {
return editor.registerMutationListener(HashtagNode, (mutations) => {
mutations.forEach((mutation, key) => {
if (mutation === 'updated') {
editor.update(() => {
const node = $getNodeByKey(key);
if (!node) {
return;
}

const nextSibling = node.getNextSibling();
if (
$isTextNode(nextSibling) &&
nextSibling.getTextContent().startsWith('#')
) {
nextSibling.setTextContent(` ${nextSibling.getTextContent()}`);
nextSibling.select(2, 2);
}
});
}
});
});
}, [editor]);

const createHashtagNode = useCallback((textNode: TextNode): HashtagNode => {
return $createHashtagNode(textNode.getTextContent());
}, []);
Expand Down

0 comments on commit 2fe70f5

Please sign in to comment.