UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) • 1.41 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{$isDecoratorBlockNode as r}from"@lexical/react/LexicalDecoratorBlockNode";import{useLexicalNodeSelection as t}from"@lexical/react/useLexicalNodeSelection";import{$getNearestBlockElementAncestorOrThrow as o}from"@lexical/utils";import{mergeRegister as i,FORMAT_ELEMENT_COMMAND as l,$getSelection as c,$isNodeSelection as a,$getNodeByKey as m,$isRangeSelection as n,COMMAND_PRIORITY_LOW as s,CLICK_COMMAND as f,getComposedEventTarget as u}from"lexical";import{useRef as x,useEffect as d}from"react";import{jsx as p}from"react/jsx-runtime";function N({children:N,format:g,nodeKey:C,className:h}){const[v]=e(),[y,F,L]=t(C),j=x(null);return d(()=>i(v.registerCommand(l,e=>{if(y){const t=c();if(a(t)){const t=m(C);r(t)&&t.setFormat(e)}else if(n(t)){const i=t.getNodes();for(const t of i)if(r(t))t.setFormat(e);else{o(t).setFormat(e)}}return!0}return!1},s),v.registerCommand(f,e=>u(e)===j.current&&(e.preventDefault(),e.shiftKey||L(),F(!y),!0),s)),[L,v,y,C,F]),/*#__PURE__*/p("div",{className:[h.base,y?h.focus:null].filter(Boolean).join(" "),ref:j,style:{textAlign:g||void 0},children:N})}export{N as BlockWithAlignableContents};