@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
58 lines (51 loc) • 1.82 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 { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { createEmptyHistoryState, registerHistory } from '@lexical/history';
export { createEmptyHistoryState } from '@lexical/history';
import { useMemo, 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.
*
*/
function useHistory(editor, externalHistoryState, delay = 1000) {
const historyState = useMemo(() => externalHistoryState || createEmptyHistoryState(), [externalHistoryState]);
useEffect(() => {
return registerHistory(editor, historyState, delay);
}, [delay, editor, historyState]);
}
/**
* 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.
*
*/
/**
* Adds undo/redo support to the editor by tracking changes in a
* {@link HistoryState}. Pass `delay` to control how long (in milliseconds)
* consecutive changes are merged into a single history entry, or
* `externalHistoryState` to share a history stack across editors.
*
* This is a legacy plugin. When building an editor with the extension API,
* configure {@link HistoryExtension} instead.
*
* @returns `null`, this plugin renders no DOM of its own.
*/
function HistoryPlugin({
delay,
externalHistoryState
}) {
const [editor] = useLexicalComposerContext();
useHistory(editor, externalHistoryState, delay);
return null;
}
export { HistoryPlugin };