@lobehub/editor 4.20.2 → 4.20.3
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.
|
@@ -6,21 +6,36 @@ import { HIDE_TOOLBAR_COMMAND, registerToolbarCommand } from "../command/index.j
|
|
|
6
6
|
import { getDOMRangeRect } from "../utils/getDOMRangeRect.js";
|
|
7
7
|
import { setFloatingElemPosition } from "../utils/setFloatingElemPosition.js";
|
|
8
8
|
import { styles } from "./style.js";
|
|
9
|
-
import { Block } from "@lobehub/ui";
|
|
10
|
-
import { useCallback, useRef } from "react";
|
|
9
|
+
import { Block, LOBE_THEME_APP_ID } from "@lobehub/ui";
|
|
10
|
+
import { useCallback, useRef, useState } from "react";
|
|
11
11
|
import { jsx } from "react/jsx-runtime";
|
|
12
12
|
import { cx, useThemeMode } from "antd-style";
|
|
13
13
|
import { $getSelection, COMMAND_PRIORITY_LOW, SELECTION_CHANGE_COMMAND, getDOMSelection } from "lexical";
|
|
14
14
|
import { mergeRegister } from "@lexical/utils";
|
|
15
|
+
import { createPortal } from "react-dom";
|
|
15
16
|
//#region src/plugins/toolbar/react/index.tsx
|
|
16
17
|
init_debug();
|
|
17
|
-
const
|
|
18
|
+
const resolveDefaultPortalContainer = () => {
|
|
19
|
+
if (typeof document === "undefined") return null;
|
|
20
|
+
return document.getElementById(LOBE_THEME_APP_ID) ?? document.body;
|
|
21
|
+
};
|
|
22
|
+
const ReactToolbarPlugin = ({ className, children, getPopupContainer, usePortal = true, zIndex }) => {
|
|
18
23
|
const popupCharStylesEditorRef = useRef(null);
|
|
19
24
|
const anchorElemRef = useRef(null);
|
|
25
|
+
const [portalContainer, setPortalContainer] = useState(null);
|
|
20
26
|
const [kernelEditor] = useLexicalComposerContext();
|
|
21
27
|
const { isDarkMode } = useThemeMode();
|
|
22
28
|
const isMouseDownRef = useRef(false);
|
|
23
29
|
const logger = createDebugLogger("plugin", "toolbar");
|
|
30
|
+
const resolvePortalContainer = useCallback(() => {
|
|
31
|
+
if (!usePortal) return null;
|
|
32
|
+
if (getPopupContainer) return getPopupContainer();
|
|
33
|
+
return resolveDefaultPortalContainer();
|
|
34
|
+
}, [getPopupContainer, usePortal]);
|
|
35
|
+
const syncPortalContainer = useCallback(() => {
|
|
36
|
+
const nextContainer = resolvePortalContainer();
|
|
37
|
+
setPortalContainer((current) => current === nextContainer ? current : nextContainer);
|
|
38
|
+
}, [resolvePortalContainer]);
|
|
24
39
|
const $updateTextFormatFloatingToolbar = useCallback((editor) => {
|
|
25
40
|
if (!anchorElemRef.current) return;
|
|
26
41
|
const selection = $getSelection();
|
|
@@ -65,6 +80,14 @@ const ReactToolbarPlugin = ({ className, children }) => {
|
|
|
65
80
|
};
|
|
66
81
|
}
|
|
67
82
|
}, []);
|
|
83
|
+
useLexicalEditor((editor) => {
|
|
84
|
+
if (!usePortal) {
|
|
85
|
+
setPortalContainer(null);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
syncPortalContainer();
|
|
89
|
+
return editor.registerRootListener(syncPortalContainer);
|
|
90
|
+
}, [syncPortalContainer, usePortal]);
|
|
68
91
|
useLexicalEditor((editor) => {
|
|
69
92
|
const handleMouseDown = handleMouseDownFactory(() => {
|
|
70
93
|
editor.dispatchCommand(HIDE_TOOLBAR_COMMAND, void 0);
|
|
@@ -75,10 +98,32 @@ const ReactToolbarPlugin = ({ className, children }) => {
|
|
|
75
98
|
});
|
|
76
99
|
});
|
|
77
100
|
const rootElement = editor.getRootElement();
|
|
101
|
+
const editorWindow = editor._window ?? rootElement?.ownerDocument.defaultView ?? globalThis.window ?? null;
|
|
102
|
+
let animationFrameId = null;
|
|
103
|
+
const updateToolbarPosition = () => {
|
|
104
|
+
if (!editorWindow || isMouseDownRef.current || animationFrameId !== null) return;
|
|
105
|
+
animationFrameId = editorWindow.requestAnimationFrame(() => {
|
|
106
|
+
animationFrameId = null;
|
|
107
|
+
editor.getEditorState().read(() => {
|
|
108
|
+
$updateTextFormatFloatingToolbar(editor);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const scrollListenerOptions = {
|
|
113
|
+
capture: true,
|
|
114
|
+
passive: true
|
|
115
|
+
};
|
|
116
|
+
const scrollCleanupOptions = { capture: true };
|
|
117
|
+
const resizeListenerOptions = { passive: true };
|
|
78
118
|
if (rootElement) {
|
|
79
119
|
rootElement.addEventListener("mousedown", handleMouseDown);
|
|
80
120
|
document.addEventListener("mouseup", handleMouseUp);
|
|
81
121
|
}
|
|
122
|
+
if (editorWindow) {
|
|
123
|
+
editorWindow.addEventListener("scroll", updateToolbarPosition, scrollListenerOptions);
|
|
124
|
+
editorWindow.document.addEventListener("scroll", updateToolbarPosition, scrollListenerOptions);
|
|
125
|
+
editorWindow.addEventListener("resize", updateToolbarPosition, resizeListenerOptions);
|
|
126
|
+
}
|
|
82
127
|
return mergeRegister(registerToolbarCommand(editor, { onHide: $hideFloatingToolbar }), editor.registerUpdateListener(({ editorState }) => {
|
|
83
128
|
if (!isMouseDownRef.current) editorState.read(() => {
|
|
84
129
|
$updateTextFormatFloatingToolbar(editor);
|
|
@@ -91,11 +136,18 @@ const ReactToolbarPlugin = ({ className, children }) => {
|
|
|
91
136
|
rootElement.removeEventListener("mousedown", handleMouseDown);
|
|
92
137
|
document.removeEventListener("mouseup", handleMouseUp);
|
|
93
138
|
}
|
|
139
|
+
if (editorWindow) {
|
|
140
|
+
editorWindow.removeEventListener("scroll", updateToolbarPosition, scrollCleanupOptions);
|
|
141
|
+
editorWindow.document.removeEventListener("scroll", updateToolbarPosition, scrollCleanupOptions);
|
|
142
|
+
editorWindow.removeEventListener("resize", updateToolbarPosition);
|
|
143
|
+
if (animationFrameId !== null) editorWindow.cancelAnimationFrame(animationFrameId);
|
|
144
|
+
}
|
|
94
145
|
});
|
|
95
146
|
});
|
|
96
|
-
|
|
97
|
-
className: styles.anchor,
|
|
147
|
+
const toolbarNode = /* @__PURE__ */ jsx("div", {
|
|
148
|
+
className: usePortal ? styles.portalAnchor : styles.anchor,
|
|
98
149
|
ref: anchorElemRef,
|
|
150
|
+
style: usePortal && zIndex !== void 0 ? { zIndex } : void 0,
|
|
99
151
|
children: /* @__PURE__ */ jsx(Block, {
|
|
100
152
|
className: cx(isDarkMode ? styles.toolbarDark : styles.toolbarLight, className),
|
|
101
153
|
padding: 4,
|
|
@@ -104,6 +156,8 @@ const ReactToolbarPlugin = ({ className, children }) => {
|
|
|
104
156
|
children
|
|
105
157
|
})
|
|
106
158
|
});
|
|
159
|
+
if (usePortal) return portalContainer ? createPortal(toolbarNode, portalContainer) : null;
|
|
160
|
+
return toolbarNode;
|
|
107
161
|
};
|
|
108
162
|
//#endregion
|
|
109
163
|
export { ReactToolbarPlugin };
|
|
@@ -3,8 +3,15 @@ import { createStaticStyles } from "antd-style";
|
|
|
3
3
|
const styles = createStaticStyles(({ css, cssVar }) => ({
|
|
4
4
|
anchor: css`
|
|
5
5
|
position: relative;
|
|
6
|
+
`,
|
|
7
|
+
portalAnchor: css`
|
|
8
|
+
pointer-events: none;
|
|
9
|
+
position: fixed;
|
|
10
|
+
z-index: 1100;
|
|
11
|
+
inset: 0;
|
|
6
12
|
`,
|
|
7
13
|
toolbarDark: css`
|
|
14
|
+
pointer-events: auto;
|
|
8
15
|
will-change: transform;
|
|
9
16
|
|
|
10
17
|
position: absolute;
|
|
@@ -28,6 +35,7 @@ const styles = createStaticStyles(({ css, cssVar }) => ({
|
|
|
28
35
|
transition: opacity 0.12s ${cssVar.motionEaseOut};
|
|
29
36
|
`,
|
|
30
37
|
toolbarLight: css`
|
|
38
|
+
pointer-events: auto;
|
|
31
39
|
will-change: transform;
|
|
32
40
|
|
|
33
41
|
position: absolute;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { ReactNode } from "react";
|
|
1
|
+
import { CSSProperties, ReactNode } from "react";
|
|
2
2
|
|
|
3
3
|
//#region src/plugins/toolbar/react/type.d.ts
|
|
4
4
|
interface ReactToolbarPluginProps {
|
|
5
5
|
children?: ReactNode;
|
|
6
6
|
className?: string;
|
|
7
|
+
getPopupContainer?: () => HTMLElement | null;
|
|
8
|
+
usePortal?: boolean;
|
|
9
|
+
zIndex?: CSSProperties['zIndex'];
|
|
7
10
|
}
|
|
8
11
|
//#endregion
|
|
9
12
|
export { ReactToolbarPluginProps };
|
package/package.json
CHANGED