@haklex/rich-plugin-block-handle 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext
6
6
  import { INSERT_HORIZONTAL_RULE_COMMAND } from "@lexical/react/LexicalHorizontalRuleNode";
7
7
  import { $createHeadingNode, $createQuoteNode } from "@lexical/rich-text";
8
8
  import { $setBlocksType } from "@lexical/selection";
9
- import { $createNodeSelection, $createParagraphNode, $createTabNode, $getNearestNodeFromDOMNode, $getNodeByKey, $getRoot, $getSelection, $isElementNode, $isNodeSelection, $isRangeSelection, $parseSerializedNode, $setSelection, COMMAND_PRIORITY_CRITICAL, COMMAND_PRIORITY_HIGH, COPY_COMMAND, CUT_COMMAND, DELETE_CHARACTER_COMMAND, DRAGOVER_COMMAND, DRAGSTART_COMMAND, DROP_COMMAND, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND, KEY_DOWN_COMMAND, KEY_ESCAPE_COMMAND, PASTE_COMMAND, PASTE_TAG, REMOVE_TEXT_COMMAND, SELECT_ALL_COMMAND } from "lexical";
9
+ import { $createNodeSelection, $createParagraphNode, $createTabNode, $getNearestNodeFromDOMNode, $getNodeByKey, $getRoot, $getSelection, $isElementNode, $isNodeSelection, $isRangeSelection, $isTextNode, $parseSerializedNode, $setSelection, COMMAND_PRIORITY_CRITICAL, COMMAND_PRIORITY_HIGH, COPY_COMMAND, CUT_COMMAND, DELETE_CHARACTER_COMMAND, DRAGOVER_COMMAND, DRAGSTART_COMMAND, DROP_COMMAND, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND, KEY_DOWN_COMMAND, KEY_ESCAPE_COMMAND, PASTE_COMMAND, PASTE_TAG, REMOVE_TEXT_COMMAND, SELECT_ALL_COMMAND } from "lexical";
10
10
  import { ArrowDown, ArrowUp, Code2, Copy, GripVertical, Heading1, Heading2, Heading3, List, ListChecks, ListOrdered, Minus, Plus, TextQuote, Trash2, Type } from "lucide-react";
11
11
  import { useCallback, useEffect, useRef, useState } from "react";
12
12
  import { createPortal } from "react-dom";
@@ -222,6 +222,15 @@ function getTopLevelKey(node) {
222
222
  while (current && current.getParent() && current.getParent() !== $getRoot()) current = current.getParent();
223
223
  return current?.getParent() === $getRoot() ? current.getKey() : null;
224
224
  }
225
+ function $rangeCoversEntireContent(sel, target) {
226
+ if (target.getChildrenSize() === 0) return false;
227
+ const [startPoint, endPoint] = sel.isBackward() ? [sel.focus, sel.anchor] : [sel.anchor, sel.focus];
228
+ const targetKey = target.getKey();
229
+ const firstDesc = target.getFirstDescendant();
230
+ if (!(startPoint.type === "element" && startPoint.key === targetKey && startPoint.offset === 0 || startPoint.type === "text" && firstDesc !== null && firstDesc.getKey() === startPoint.key && startPoint.offset === 0)) return false;
231
+ const lastDesc = target.getLastDescendant();
232
+ return endPoint.type === "element" && endPoint.key === targetKey && endPoint.offset === target.getChildrenSize() || endPoint.type === "text" && $isTextNode(lastDesc) && lastDesc.getKey() === endPoint.key && endPoint.offset === lastDesc.getTextContentSize();
233
+ }
225
234
  function $selectBlockRange(anchorKey, focusKey) {
226
235
  const children = $getRoot().getChildren();
227
236
  const anchorIdx = children.findIndex((c) => c.getKey() === anchorKey);
@@ -458,16 +467,31 @@ function useBlockSelection(editor) {
458
467
  }
459
468
  const sel = $getSelection();
460
469
  let topLevelKey = null;
470
+ let topLevelNode = null;
461
471
  if ($isRangeSelection(sel)) {
462
472
  let node = sel.anchor.getNode();
463
- while (node.getParent() && node.getParent() !== $getRoot()) node = node.getParent();
464
- if (node.getParent() === $getRoot()) topLevelKey = node.getKey();
473
+ while (node && node.getParent() && node.getParent() !== $getRoot()) node = node.getParent();
474
+ if (node && node.getParent() === $getRoot()) {
475
+ topLevelKey = node.getKey();
476
+ topLevelNode = node;
477
+ }
465
478
  } else if ($isNodeSelection(sel)) {
466
479
  const nodes = sel.getNodes();
467
480
  if (nodes.length > 0) {
468
481
  let node = nodes[0];
469
- while (node.getParent() && node.getParent() !== $getRoot()) node = node.getParent();
470
- if (node.getParent() === $getRoot()) topLevelKey = node.getKey();
482
+ while (node && node.getParent() && node.getParent() !== $getRoot()) node = node.getParent();
483
+ if (node && node.getParent() === $getRoot()) {
484
+ topLevelKey = node.getKey();
485
+ topLevelNode = node;
486
+ }
487
+ }
488
+ }
489
+ if (topLevelNode && $isElementNode(topLevelNode)) {
490
+ const childrenSize = topLevelNode.getChildrenSize();
491
+ const isContentRangeAlready = $isRangeSelection(sel) && $rangeCoversEntireContent(sel, topLevelNode);
492
+ if (childrenSize > 0 && !isContentRangeAlready) {
493
+ topLevelNode.select(0, childrenSize);
494
+ return true;
471
495
  }
472
496
  }
473
497
  if (topLevelKey) {
@@ -1 +1 @@
1
- {"version":3,"file":"useBlockSelection.d.ts","sourceRoot":"","sources":["../src/useBlockSelection.ts"],"names":[],"mappings":"AAAA,OAAO,EAkBL,KAAK,aAAa,EAMnB,MAAM,SAAS,CAAC;AAsDjB,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa;2BAsnBzC,MAAM,YAAY,OAAO;2BA6BG,MAAM,EAAE;kCAwB1C,OAAO;6CAZQ,MAAM,GAAG,IAAI;EAiBnC"}
1
+ {"version":3,"file":"useBlockSelection.d.ts","sourceRoot":"","sources":["../src/useBlockSelection.ts"],"names":[],"mappings":"AAAA,OAAO,EAqBL,KAAK,aAAa,EAOnB,MAAM,SAAS,CAAC;AAkFjB,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa;2BAuoBzC,MAAM,YAAY,OAAO;2BA6BG,MAAM,EAAE;kCAwB1C,OAAO;6CAZQ,MAAM,GAAG,IAAI;EAiBnC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-plugin-block-handle",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Block handle plugin with add button and context menu",
5
5
  "repository": {
6
6
  "type": "git",
@@ -51,8 +51,8 @@
51
51
  "lucide-react": "^1.0.0",
52
52
  "react": ">=19",
53
53
  "react-dom": ">=19",
54
- "@haklex/rich-editor-ui": "0.9.0",
55
- "@haklex/rich-style-token": "0.9.0"
54
+ "@haklex/rich-style-token": "0.9.2",
55
+ "@haklex/rich-editor-ui": "0.9.2"
56
56
  },
57
57
  "publishConfig": {
58
58
  "access": "public"