UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

13 lines (11 loc) 1.65 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. * */ 'use strict';var a=require("@lexical/react/LexicalComposerContext"),g=require("@lexical/react/LexicalDecoratorBlockNode"),l=require("@lexical/react/useLexicalNodeSelection"),m=require("@lexical/utils"),u=require("lexical"),v=require("react"),w=require("react/jsx-runtime"); exports.BlockWithAlignableContents=function({children:x,format:n,nodeKey:h,className:p}){let [e]=a.useLexicalComposerContext(),[f,q,r]=l.useLexicalNodeSelection(h),t=v.useRef(null),k=v.useCallback(b=>{const d=u.$getSelection();f&&u.$isNodeSelection(d)&&(b.preventDefault(),e.update(()=>{d.getNodes().forEach(c=>{u.$isDecoratorNode(c)&&c.remove()})}));return!1},[e,f]);v.useEffect(()=>m.mergeRegister(e.registerCommand(u.FORMAT_ELEMENT_COMMAND,b=>{if(f){var d=u.$getSelection();if(u.$isNodeSelection(d)){var c= u.$getNodeByKey(h);g.$isDecoratorBlockNode(c)&&c.setFormat(b)}else if(u.$isRangeSelection(d)){d=d.getNodes();for(c of d)g.$isDecoratorBlockNode(c)?c.setFormat(b):m.$getNearestBlockElementAncestorOrThrow(c).setFormat(b)}return!0}return!1},u.COMMAND_PRIORITY_LOW),e.registerCommand(u.CLICK_COMMAND,b=>b.target===t.current?(b.preventDefault(),b.shiftKey||r(),q(!f),!0):!1,u.COMMAND_PRIORITY_LOW),e.registerCommand(u.KEY_DELETE_COMMAND,k,u.COMMAND_PRIORITY_LOW),e.registerCommand(u.KEY_BACKSPACE_COMMAND,k, u.COMMAND_PRIORITY_LOW)),[r,e,f,h,k,q]);return w.jsx("div",{className:[p.base,f?p.focus:null].filter(Boolean).join(" "),ref:t,style:{textAlign:n?n:void 0},children:x})}