UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 1.98 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{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{useLexicalNodeSelection as t}from"@lexical/react/useLexicalNodeSelection";import{addClassNamesToElement as r,mergeRegister as n,removeClassNamesFromElement as o}from"@lexical/utils";import{createCommand as c,DecoratorNode as i,$applyNodeReplacement as m,$getSelection as u,$isNodeSelection as a,CLICK_COMMAND as l,COMMAND_PRIORITY_LOW as s,KEY_DELETE_COMMAND as d,KEY_BACKSPACE_COMMAND as p}from"lexical";import{useCallback as f,useEffect as x}from"react";import{jsx as h}from"react/jsx-runtime";const y=c("INSERT_HORIZONTAL_RULE_COMMAND");function g({nodeKey:c}){const[i]=e(),[m,h,y]=t(c),g=f((e=>{const t=u();return m&&a(t)&&(e.preventDefault(),t.getNodes().forEach((e=>{E(e)&&e.remove()}))),!1}),[m]);return x((()=>n(i.registerCommand(l,(e=>{const t=i.getElementByKey(c);return e.target===t&&(e.shiftKey||y(),h(!m),!0)}),s),i.registerCommand(d,g,s),i.registerCommand(p,g,s))),[y,i,m,c,g,h]),x((()=>{const e=i.getElementByKey(c),t=i._config.theme.hrSelected??"selected";null!==e&&(m?r(e,t):o(e,t))}),[i,m,c]),null}class O extends i{static getType(){return"horizontalrule"}static clone(e){return new O(e.__key)}static importJSON(e){return C().updateFromJSON(e)}static importDOM(){return{hr:()=>({conversion:_,priority:0})}}exportDOM(){return{element:document.createElement("hr")}}createDOM(e){const t=document.createElement("hr");return r(t,e.theme.hr),t}getTextContent(){return"\n"}isInline(){return!1}updateDOM(){return!1}decorate(){return h(g,{nodeKey:this.__key})}}function _(){return{node:C()}}function C(){return m(new O)}function E(e){return e instanceof O}export{C as $createHorizontalRuleNode,E as $isHorizontalRuleNode,O as HorizontalRuleNode,y as INSERT_HORIZONTAL_RULE_COMMAND};