{"version":3,"file":"is-block-node-active.cjs","sources":["../src/is-block-node-active.ts"],"sourcesContent":["import { $findMatchingParent } from \"@lexical/utils\";\nimport type { EditorState, LexicalEditor, LexicalNode } from \"lexical\";\nimport { $getSelection, $isRangeSelection, $isRootOrShadowRoot } from \"lexical\";\n\nfunction isParentRootOrShadowRoot(node: LexicalNode) {\n  const parent = node.getParent();\n\n  return parent !== null && $isRootOrShadowRoot(parent);\n}\n\nconst activeNodesByEditor = new WeakMap<\n  LexicalEditor,\n  {\n    state: EditorState;\n    nodes: LexicalNode[];\n  }\n>();\n\nfunction getActiveBlockNodes(editor: LexicalEditor) {\n  const currentState = editor.getEditorState();\n\n  return currentState.read(() => {\n    const selection = $getSelection();\n\n    if (!$isRangeSelection(selection)) {\n      activeNodesByEditor.delete(editor);\n\n      return [];\n    }\n\n    const cache = activeNodesByEditor.get(editor);\n\n    if (cache?.state === currentState) {\n      return cache.nodes;\n    }\n\n    const anchor = selection.anchor.getNode();\n    const focus = selection.focus.getNode();\n    const commonAncestor = anchor.getCommonAncestor(focus);\n\n    let activeNodes: LexicalNode[] = [];\n\n    if (commonAncestor && !$isRootOrShadowRoot(commonAncestor)) {\n      const activeNode = isParentRootOrShadowRoot(commonAncestor)\n        ? commonAncestor\n        : $findMatchingParent(commonAncestor, isParentRootOrShadowRoot);\n\n      if (activeNode) {\n        activeNodes = [activeNode];\n      }\n    } else {\n      activeNodes = selection\n        .getNodes()\n        .filter((node) => $isRootOrShadowRoot(node.getParent()));\n    }\n\n    activeNodesByEditor.set(editor, {\n      state: currentState,\n      nodes: activeNodes,\n    });\n\n    return activeNodes;\n  });\n}\n\n/**\n * Checks if a block node is active in the current selection.\n * If the selection contains multiple block nodes, it will return\n * `true` only if all of them are of the same type.\n */\nexport function isBlockNodeActive(\n  editor: LexicalEditor,\n  isActive: (node: LexicalNode) => boolean\n): boolean {\n  const activeNodes = getActiveBlockNodes(editor);\n\n  if (activeNodes.length === 0) {\n    return false;\n  }\n\n  return activeNodes.every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