@haklex/rich-plugin-floating-toolbar 0.0.62 → 0.0.63

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.
@@ -1 +1 @@
1
- {"version":3,"file":"FloatingToolbarPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingToolbarPlugin.tsx"],"names":[],"mappings":"AAuCA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAkMzC,wBAAgB,qBAAqB,IAAI,YAAY,GAAG,IAAI,CAghB3D"}
1
+ {"version":3,"file":"FloatingToolbarPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingToolbarPlugin.tsx"],"names":[],"mappings":"AA2CA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AA0MzC,wBAAgB,qBAAqB,IAAI,YAAY,GAAG,IAAI,CA0hB3D"}
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
2
  import { $createRubyNode, $isRubyNode } from "@haklex/rich-editor";
3
3
  import { ColorPicker } from "@haklex/rich-editor-ui";
4
- import { usePortalTheme, vars } from "@haklex/rich-style-token";
4
+ import { usePortalTheme, usePortalContainer, vars } from "@haklex/rich-style-token";
5
5
  import { TOGGLE_LINK_COMMAND, $isLinkNode, $isAutoLinkNode } from "@lexical/link";
6
6
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
7
7
  import { $patchStyleText, $getSelectionStyleValueForProperty } from "@lexical/selection";
@@ -96,19 +96,24 @@ function getSelectionState(selection) {
96
96
  fontColor: $getSelectionStyleValueForProperty(selection, "color", "")
97
97
  };
98
98
  }
99
- function computePosition(nativeSelection, toolbar2) {
99
+ function computePosition(nativeSelection, toolbar2, container) {
100
100
  const range = nativeSelection.getRangeAt(0);
101
101
  const rect = range.getBoundingClientRect();
102
102
  if (rect.width === 0 && rect.height === 0) return null;
103
103
  const toolbarWidth = toolbar2.offsetWidth;
104
104
  const toolbarHeight = toolbar2.offsetHeight;
105
- const rawLeft = rect.left + rect.width / 2 - toolbarWidth / 2;
105
+ const isBody = container === document.body;
106
+ const containerRect = isBody ? void 0 : container.getBoundingClientRect();
107
+ const offsetX = containerRect?.left ?? 0;
108
+ const offsetY = containerRect?.top ?? 0;
109
+ const availableWidth = containerRect?.width ?? window.innerWidth;
110
+ const rawLeft = rect.left - offsetX + rect.width / 2 - toolbarWidth / 2;
106
111
  const clampedLeft = Math.max(
107
112
  8,
108
- Math.min(rawLeft, window.innerWidth - toolbarWidth - 8)
113
+ Math.min(rawLeft, availableWidth - toolbarWidth - 8)
109
114
  );
110
115
  return {
111
- top: rect.top - toolbarHeight - 10,
116
+ top: rect.top - offsetY - toolbarHeight - 10,
112
117
  left: clampedLeft
113
118
  };
114
119
  }
@@ -159,6 +164,7 @@ const THEME_VAR_NAMES = Array.from(collectThemeVarNames(vars, /* @__PURE__ */ ne
159
164
  function FloatingToolbarPlugin() {
160
165
  const [editor] = useLexicalComposerContext();
161
166
  const { className: portalClassName } = usePortalTheme();
167
+ const portalContainer = usePortalContainer();
162
168
  const toolbarRef = useRef(null);
163
169
  const [visible, setVisible] = useState(false);
164
170
  const [state, setState] = useState(INITIAL_STATE);
@@ -222,7 +228,7 @@ function FloatingToolbarPlugin() {
222
228
  setVisible(false);
223
229
  return;
224
230
  }
225
- const pos = computePosition(nativeSelection, toolbar2);
231
+ const pos = computePosition(nativeSelection, toolbar2, portalContainer);
226
232
  if (!pos) {
227
233
  setVisible(false);
228
234
  return;
@@ -417,13 +423,18 @@ function FloatingToolbarPlugin() {
417
423
  const rubyDom = editor.getElementByKey(rubyEdit.nodeKey);
418
424
  if (!rubyDom) return;
419
425
  const rect = rubyDom.getBoundingClientRect();
426
+ const isBody = portalContainer === document.body;
427
+ const containerRect = isBody ? void 0 : portalContainer.getBoundingClientRect();
428
+ const oX = containerRect?.left ?? 0;
429
+ const oY = containerRect?.top ?? 0;
430
+ const availW = containerRect?.width ?? window.innerWidth;
420
431
  const editorWidth = editorEl.offsetWidth;
421
- const rawLeft = rect.left + rect.width / 2 - editorWidth / 2;
432
+ const rawLeft = rect.left - oX + rect.width / 2 - editorWidth / 2;
422
433
  const clampedLeft = Math.max(
423
434
  8,
424
- Math.min(rawLeft, window.innerWidth - editorWidth - 8)
435
+ Math.min(rawLeft, availW - editorWidth - 8)
425
436
  );
426
- editorEl.style.top = `${rect.bottom + 8}px`;
437
+ editorEl.style.top = `${rect.bottom - oY + 8}px`;
427
438
  editorEl.style.left = `${clampedLeft}px`;
428
439
  };
429
440
  requestAnimationFrame(positionEditor);
@@ -547,7 +558,7 @@ function FloatingToolbarPlugin() {
547
558
  ]
548
559
  }
549
560
  ),
550
- document.body
561
+ portalContainer
551
562
  ),
552
563
  rubyEdit && createPortal(
553
564
  /* @__PURE__ */ jsxs(
@@ -631,7 +642,7 @@ function FloatingToolbarPlugin() {
631
642
  ]
632
643
  }
633
644
  ),
634
- document.body
645
+ portalContainer
635
646
  )
636
647
  ] });
637
648
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@haklex/rich-plugin-floating-toolbar",
3
3
  "type": "module",
4
- "version": "0.0.62",
4
+ "version": "0.0.63",
5
5
  "description": "Floating toolbar plugin",
6
6
  "license": "MIT",
7
7
  "exports": {
@@ -25,9 +25,9 @@
25
25
  "react-dom": ">=19"
26
26
  },
27
27
  "dependencies": {
28
- "@haklex/rich-editor": "0.0.62",
29
- "@haklex/rich-editor-ui": "0.0.62",
30
- "@haklex/rich-style-token": "0.0.62"
28
+ "@haklex/rich-editor": "0.0.63",
29
+ "@haklex/rich-editor-ui": "0.0.63",
30
+ "@haklex/rich-style-token": "0.0.63"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@lexical/link": "^0.41.0",