UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 934 B
/** * 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 e=require("@lexical/react/LexicalComposerContext"),t=require("lexical"),c=require("react");function o(e,c){return e.getEditorState().read(()=>{const e=t.$getNodeByKey(c);return null!==e&&e.isSelected()})}exports.useLexicalNodeSelection=function(r){const[i]=e.useLexicalComposerContext(),[l,n]=c.useState(()=>o(i,r));return c.useEffect(()=>{let e=!0;const t=i.registerUpdateListener(()=>{e&&n(o(i,r))});return()=>{e=!1,t()}},[i,r]),[l,c.useCallback(e=>{i.update(()=>{let c=t.$getSelection();t.$isNodeSelection(c)||(c=t.$createNodeSelection(),t.$setSelection(c)),t.$isNodeSelection(c)&&(e?c.add(r):c.delete(r))})},[i,r]),c.useCallback(()=>{i.update(()=>{const e=t.$getSelection();t.$isNodeSelection(e)&&e.clear()})},[i])]};