UNPKG

@lexical/text

Version:

This package contains utilities and helpers for handling Lexical text.

33 lines (32 loc) 1.54 kB
/** * 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 { Klass, LexicalEditor, TextNode } from 'lexical'; export type EntityMatch = { end: number; start: number; }; /** * Returns a tuple that can be rested (...) into mergeRegister to clean up * node transforms listeners that transforms text into another node, eg. a HashtagNode. * @example * ```ts * useEffect(() => { return mergeRegister( ...registerLexicalTextEntity(editor, getMatch, targetNode, createNode), ); }, [createNode, editor, getMatch, targetNode]); * ``` * Where targetNode is the type of node containing the text you want to transform (like a text input), * then getMatch uses a regex to find a matching text and creates the proper node to include the matching text. * @param editor - The lexical editor. * @param getMatch - Finds a matching string that satisfies a regex expression. * @param targetNode - The node type that contains text to match with. eg. HashtagNode * @param createNode - A function that creates a new node to contain the matched text. eg createHashtagNode * @returns An array containing the plain text and reverse node transform listeners. */ export declare function registerLexicalTextEntity<T extends TextNode>(editor: LexicalEditor, getMatch: (text: string) => null | EntityMatch, targetNode: Klass<T>, createNode: (textNode: TextNode) => T): Array<() => void>;