UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 1.99 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 i,DecoratorNode as c,$applyNodeReplacement as u,$isNodeSelection as m,$getSelection as l,$getNodeByKey as a,CLICK_COMMAND as s,COMMAND_PRIORITY_LOW as p,KEY_DELETE_COMMAND as f,KEY_BACKSPACE_COMMAND as d}from"lexical";import{useCallback as x,useEffect as y}from"react";import{jsx as h}from"react/jsx-runtime";const O=i("INSERT_HORIZONTAL_RULE_COMMAND");function g({nodeKey:i}){const[c]=e(),[u,h,O]=t(i),g=x((e=>{if(u&&m(l())){e.preventDefault();const t=a(i);if(E(t))return t.remove(),!0}return!1}),[u,i]);return y((()=>n(c.registerCommand(s,(e=>{const t=c.getElementByKey(i);return e.target===t&&(e.shiftKey||O(),h(!u),!0)}),p),c.registerCommand(f,g,p),c.registerCommand(d,g,p))),[O,c,u,i,g,h]),y((()=>{const e=c.getElementByKey(i),t="selected";null!==e&&(u?r(e,t):o(e,t))}),[c,u,i]),null}class C extends c{static getType(){return"horizontalrule"}static clone(e){return new C(e.__key)}static importJSON(e){return D()}static importDOM(){return{hr:()=>({conversion:_,priority:0})}}exportJSON(){return{type:"horizontalrule",version:1}}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:D()}}function D(){return u(new C)}function E(e){return e instanceof C}export{D as $createHorizontalRuleNode,E as $isHorizontalRuleNode,C as HorizontalRuleNode,O as INSERT_HORIZONTAL_RULE_COMMAND};