UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

96 lines (89 loc) • 3.28 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 { HorizontalRuleNode as HorizontalRuleNode$1 } from '@lexical/extension/HorizontalRuleExtension'; export { $isHorizontalRuleNode, INSERT_HORIZONTAL_RULE_COMMAND } from '@lexical/extension/HorizontalRuleExtension'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; import { useLexicalNodeSelection } from '@lexical/react/useLexicalNodeSelection'; import { $applyNodeReplacement, mergeRegister, CLICK_COMMAND, getComposedEventTarget, COMMAND_PRIORITY_LOW, addClassNamesToElement, removeClassNamesFromElement } from 'lexical'; import { useEffect } from 'react'; import { jsx } from 'react/jsx-runtime'; /** * 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 HorizontalRuleComponent({ nodeKey }) { const [editor] = useLexicalComposerContext(); const [isSelected, setSelected, clearSelection] = useLexicalNodeSelection(nodeKey); useEffect(() => { return mergeRegister(editor.registerCommand(CLICK_COMMAND, event => { const hrElem = editor.getElementByKey(nodeKey); if (getComposedEventTarget(event) === hrElem) { if (!event.shiftKey) { clearSelection(); } setSelected(!isSelected); return true; } return false; }, COMMAND_PRIORITY_LOW)); }, [clearSelection, editor, isSelected, nodeKey, setSelected]); useEffect(() => { const hrElem = editor.getElementByKey(nodeKey); const isSelectedClassName = editor._config.theme.hrSelected ?? 'selected'; if (hrElem !== null) { if (isSelected) { addClassNamesToElement(hrElem, isSelectedClassName); } else { removeClassNamesFromElement(hrElem, isSelectedClassName); } } }, [editor, isSelected, nodeKey]); return null; } /** * @deprecated A pure Lexical implementation is available in `@lexical/extension` as HorizontalRuleExtension */ class HorizontalRuleNode extends HorizontalRuleNode$1 { $config() { // Named rather than left to the runtime default, for the reason the base // node names its own: it is what carries this config on the record the // composed serialization types walk. The shape still matches the base // node's, whose 'horizontalrule' type this deprecated subclass reuses. return this.config('horizontalrule', { extends: HorizontalRuleNode$1, importDOM: { hr: () => ({ conversion: $convertHorizontalRuleElement, priority: 0 }) } }); } decorate() { return /*#__PURE__*/jsx(HorizontalRuleComponent, { nodeKey: this.__key }); } } function $convertHorizontalRuleElement() { return { node: $createHorizontalRuleNode() }; } /** * @deprecated A pure Lexical implementation is available in `@lexical/extension` as HorizontalRuleExtension */ function $createHorizontalRuleNode() { return $applyNodeReplacement(new HorizontalRuleNode()); } export { $createHorizontalRuleNode, HorizontalRuleNode };