@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
55 lines (47 loc) • 1.67 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 { registerClearEditor } from '@lexical/extension/ClearEditorExtension';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { CAN_USE_DOM } from 'lexical';
import { useLayoutEffect, 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.
*
*/
// This workaround is no longer necessary in React 19,
// but we currently support React >=18.x
// https://github.com/facebook/react/pull/26395
const useLayoutEffectImpl = CAN_USE_DOM ? useLayoutEffect : useEffect;
/**
* 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.
*
*/
/**
* Registers a handler for `CLEAR_EDITOR_COMMAND` that empties the editor and
* resets the selection. Provide `onClear` to run your own logic in place of the
* default clearing behavior.
*
* This is a legacy plugin. When building an editor with the extension API,
* configure {@link ClearEditorExtension} instead.
*
* @returns `null`, this plugin renders no DOM of its own.
*/
function ClearEditorPlugin({
onClear
}) {
const [editor] = useLexicalComposerContext();
useLayoutEffectImpl(() => registerClearEditor(editor, onClear), [editor, onClear]);
return null;
}
export { ClearEditorPlugin };