@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
69 lines (61 loc) • 2.08 kB
JavaScript
/**
* 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 { AutoLinkNode, registerAutoLink } from '@lexical/link';
export { createLinkMatcherWithRegExp } from '@lexical/link';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { useEffect } from 'react';
/**
* 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.
*
*/
// Do not require this module directly! Use normal `invariant` calls.
function formatDevErrorMessage(message) {
throw new Error(message);
}
function useAutoLink(editor, matchers, onChange, excludeParents) {
useEffect(() => {
if (!editor.hasNodes([AutoLinkNode])) {
{
formatDevErrorMessage(`LexicalAutoLinkPlugin: AutoLinkNode not registered on editor`);
}
}
});
useEffect(() => {
return registerAutoLink(editor, {
changeHandlers: onChange ? [onChange] : [],
excludeParents: excludeParents ?? [],
matchers
});
}, [editor, matchers, onChange, excludeParents]);
}
/**
* Automatically converts text that matches one of the provided `matchers` into
* {@link AutoLinkNode}s as the user types, and reverts them back to plain text
* when they no longer match. Provide `onChange` to react to links being
* created, updated, or removed, and `excludeParents` to skip matching inside
* particular ancestor nodes. The editor must have the {@link AutoLinkNode}
* registered.
*
* This is a legacy plugin. When building an editor with the extension API,
* configure {@link AutoLinkExtension} instead.
*
* @returns `null`, this plugin renders no DOM of its own.
*/
function AutoLinkPlugin({
matchers,
onChange,
excludeParents
}) {
const [editor] = useLexicalComposerContext();
useAutoLink(editor, matchers, onChange, excludeParents);
return null;
}
export { AutoLinkPlugin };