UNPKG

@terrible-lexical/react

Version:

This package provides Lexical components and hooks for React applications.

30 lines (25 loc) 984 B
/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import type {EntityMatch} from '@terrible-lexical/text/src'; import type {Klass, TextNode} from 'terrible-lexical'; import {useLexicalComposerContext} from '@terrible-lexical/react/src/LexicalComposerContext'; import {registerLexicalTextEntity} from '@terrible-lexical/text/src'; import {mergeRegister} from '@terrible-lexical/utils/src'; import {useEffect} from 'react'; export function useLexicalTextEntity<T extends TextNode>( getMatch: (text: string) => null | EntityMatch, targetNode: Klass<T>, createNode: (textNode: TextNode) => T, ): void { const [editor] = useLexicalComposerContext(); useEffect(() => { return mergeRegister( ...registerLexicalTextEntity(editor, getMatch, targetNode, createNode), ); }, [createNode, editor, getMatch, targetNode]); }