@haklex/rich-plugin-floating-toolbar 0.0.103 → 0.0.105

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":"AAkCA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAgMrD,MAAM,WAAW,0BAA0B;IACzC,OAAO,CAAC,EAAE,SAAS,CAAC;CACrB;AAED,wBAAgB,qBAAqB,CAAC,EACpC,OAAO,GACR,GAAE,0BAA+B,GAAG,YAAY,GAAG,IAAI,CA2gBvD"}
1
+ {"version":3,"file":"FloatingToolbarPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingToolbarPlugin.tsx"],"names":[],"mappings":"AAmCA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AA8LrD,MAAM,WAAW,0BAA0B;IACzC,OAAO,CAAC,EAAE,SAAS,CAAC;CACrB;AAED,wBAAgB,qBAAqB,CAAC,EACpC,OAAO,GACR,GAAE,0BAA+B,GAAG,YAAY,GAAG,IAAI,CAwhBvD"}
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import { jsxs, Fragment, jsx } from "react/jsx-runtime";
2
+ import { useTextSelectionSnapshot, getDOMRectFromTextSelection } from "@haklex/rich-editor";
2
3
  import { $toggleSpoilerSelection, $selectionTouchesSpoiler } from "@haklex/rich-editor/commands";
3
4
  import { $createRubyNode, $isRubyNode } from "@haklex/rich-editor/nodes";
4
5
  import { ColorPicker } from "@haklex/rich-editor-ui";
@@ -99,9 +100,7 @@ function getSelectionState(selection) {
99
100
  fontColor: $getSelectionStyleValueForProperty(selection, "color", "")
100
101
  };
101
102
  }
102
- function computePosition(nativeSelection, toolbar2, container) {
103
- const range = nativeSelection.getRangeAt(0);
104
- const rect = range.getBoundingClientRect();
103
+ function computePosition(rect, toolbar2, container) {
105
104
  if (rect.width === 0 && rect.height === 0) return null;
106
105
  const toolbarWidth = toolbar2.offsetWidth;
107
106
  const toolbarHeight = toolbar2.offsetHeight;
@@ -162,6 +161,7 @@ function FloatingToolbarPlugin({
162
161
  const [editor] = useLexicalComposerContext();
163
162
  const { className: portalClassName } = usePortalTheme();
164
163
  const portalContainer = usePortalContainer();
164
+ const selectionSnapshot = useTextSelectionSnapshot();
165
165
  const toolbarRef = useRef(null);
166
166
  const [visible, setVisible] = useState(false);
167
167
  const [state, setState] = useState(INITIAL_STATE);
@@ -213,17 +213,27 @@ function FloatingToolbarPlugin({
213
213
  };
214
214
  }, [editor, updateToolbar]);
215
215
  useEffect(() => {
216
- if (!visible || !toolbarRef.current) return;
216
+ if (!selectionSnapshot) {
217
+ setVisible(false);
218
+ }
219
+ }, [selectionSnapshot]);
220
+ useEffect(() => {
221
+ if (!visible || !toolbarRef.current || !selectionSnapshot) return;
217
222
  const positionToolbar = () => {
218
223
  const toolbar2 = toolbarRef.current;
219
224
  if (!toolbar2) return;
220
225
  applyThemeVars(toolbar2);
221
- const nativeSelection = window.getSelection();
222
- if (!nativeSelection || nativeSelection.rangeCount === 0) {
226
+ const rootElement2 = editor.getRootElement();
227
+ if (!rootElement2) {
228
+ setVisible(false);
229
+ return;
230
+ }
231
+ const rect = getDOMRectFromTextSelection(rootElement2, selectionSnapshot);
232
+ if (!rect) {
223
233
  setVisible(false);
224
234
  return;
225
235
  }
226
- const pos = computePosition(nativeSelection, toolbar2, portalContainer);
236
+ const pos = computePosition(rect, toolbar2, portalContainer);
227
237
  if (!pos) {
228
238
  setVisible(false);
229
239
  return;
@@ -247,7 +257,7 @@ function FloatingToolbarPlugin({
247
257
  const onScroll = () => requestAnimationFrame(positionToolbar);
248
258
  scrollParent.addEventListener("scroll", onScroll, { passive: true });
249
259
  return () => scrollParent.removeEventListener("scroll", onScroll);
250
- }, [applyThemeVars, editor, visible, state]);
260
+ }, [applyThemeVars, editor, portalContainer, selectionSnapshot, state, visible]);
251
261
  const handleFormat = useCallback(
252
262
  (format) => {
253
263
  editor.dispatchCommand(FORMAT_TEXT_COMMAND, format);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-plugin-floating-toolbar",
3
- "version": "0.0.103",
3
+ "version": "0.0.105",
4
4
  "description": "Floating toolbar plugin",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,9 +21,9 @@
21
21
  "dist"
22
22
  ],
23
23
  "dependencies": {
24
- "@haklex/rich-editor": "0.0.103",
25
- "@haklex/rich-editor-ui": "0.0.103",
26
- "@haklex/rich-style-token": "0.0.103"
24
+ "@haklex/rich-editor": "0.0.105",
25
+ "@haklex/rich-editor-ui": "0.0.105",
26
+ "@haklex/rich-style-token": "0.0.105"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@lexical/link": "^0.42.0",