@haklex/rich-plugin-block-handle 0.0.1
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/LICENSE +28 -0
- package/dist/BlockHandlePlugin.d.ts +3 -0
- package/dist/BlockHandlePlugin.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +533 -0
- package/dist/rich-plugin-block-handle.css +64 -0
- package/dist/styles.css.d.ts +8 -0
- package/dist/styles.css.d.ts.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Innei
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Additional Terms and Conditions
|
|
25
|
+
|
|
26
|
+
----------------
|
|
27
|
+
|
|
28
|
+
Use of this software is governed by the terms of MIT and, in addition, by the terms and conditions described in the additional file (ADDITIONAL_TERMS.md). By using this software, you agree to abide by these additional terms and conditions.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BlockHandlePlugin.d.ts","sourceRoot":"","sources":["../src/BlockHandlePlugin.tsx"],"names":[],"mappings":"AAkDA,OAAO,KAAK,EAGV,YAAY,EACb,MAAM,OAAO,CAAA;AAgmBd,wBAAgB,iBAAiB,IAAI,YAAY,CAGhD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuItem, DropdownMenuSeparator } from "@haklex/rich-editor-ui";
|
|
3
|
+
import { $createCodeNode } from "@lexical/code";
|
|
4
|
+
import { INSERT_CHECK_LIST_COMMAND, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND } from "@lexical/list";
|
|
5
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
6
|
+
import { INSERT_HORIZONTAL_RULE_COMMAND } from "@lexical/react/LexicalHorizontalRuleNode";
|
|
7
|
+
import { $createQuoteNode, $createHeadingNode } from "@lexical/rich-text";
|
|
8
|
+
import { $setBlocksType } from "@lexical/selection";
|
|
9
|
+
import { $getNearestNodeFromDOMNode, $getNodeByKey, $createParagraphNode, $isElementNode, $getSelection, $isRangeSelection, DRAGOVER_COMMAND, COMMAND_PRIORITY_HIGH, DROP_COMMAND, DRAGSTART_COMMAND } from "lexical";
|
|
10
|
+
import { Plus, GripVertical, Type, Heading1, Heading2, Heading3, List, ListOrdered, ListChecks, TextQuote, Minus, Code2, Copy, ArrowUp, ArrowDown, Trash2 } from "lucide-react";
|
|
11
|
+
import { useState, useRef, useCallback, useEffect } from "react";
|
|
12
|
+
import { createPortal } from "react-dom";
|
|
13
|
+
var handleContainer = "iihqkc0";
|
|
14
|
+
var handleContainerVisible = "iihqkc1";
|
|
15
|
+
var handleBtn = "iihqkc2";
|
|
16
|
+
var draggingBlock = "iihqkc3";
|
|
17
|
+
var dragPreview = "iihqkc4";
|
|
18
|
+
var dropIndicator = "iihqkc5";
|
|
19
|
+
var menuItemDestructive = "iihqkc6";
|
|
20
|
+
const DRAG_DATA_KEY = "application/x-rich-editor-drag";
|
|
21
|
+
const HIDE_DELAY = 300;
|
|
22
|
+
const HANDLE_OFFSET = 52;
|
|
23
|
+
const TURN_INTO_ITEMS = [
|
|
24
|
+
{ key: "paragraph", label: "Text", icon: Type },
|
|
25
|
+
{ key: "h1", label: "Heading 1", icon: Heading1 },
|
|
26
|
+
{ key: "h2", label: "Heading 2", icon: Heading2 },
|
|
27
|
+
{ key: "h3", label: "Heading 3", icon: Heading3 },
|
|
28
|
+
{ key: "bullet", label: "Bullet List", icon: List },
|
|
29
|
+
{ key: "numbered", label: "Numbered List", icon: ListOrdered },
|
|
30
|
+
{ key: "todo", label: "To-do", icon: ListChecks },
|
|
31
|
+
{ key: "quote", label: "Quote", icon: TextQuote },
|
|
32
|
+
{ key: "divider", label: "Divider", icon: Minus },
|
|
33
|
+
{ key: "code", label: "Code", icon: Code2 }
|
|
34
|
+
];
|
|
35
|
+
function getBlockElement(editor, target) {
|
|
36
|
+
const rootElement = editor.getRootElement();
|
|
37
|
+
if (!rootElement) return null;
|
|
38
|
+
let current = target;
|
|
39
|
+
while (current && current !== rootElement) {
|
|
40
|
+
if (current.parentElement === rootElement) return current;
|
|
41
|
+
current = current.parentElement;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
function toPagePosition(rect) {
|
|
46
|
+
return {
|
|
47
|
+
top: rect.top + window.scrollY,
|
|
48
|
+
left: rect.left + window.scrollX
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function $cloneNode(node) {
|
|
52
|
+
const Klass = node.constructor;
|
|
53
|
+
const serialized = node.exportJSON();
|
|
54
|
+
const clone = Klass.importJSON(serialized);
|
|
55
|
+
if ($isElementNode(node) && $isElementNode(clone)) {
|
|
56
|
+
for (const child of node.getChildren()) {
|
|
57
|
+
clone.append($cloneNode(child));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return clone;
|
|
61
|
+
}
|
|
62
|
+
function BlockHandleInner({
|
|
63
|
+
editor
|
|
64
|
+
}) {
|
|
65
|
+
const [handle, setHandle] = useState({
|
|
66
|
+
visible: false,
|
|
67
|
+
top: 0,
|
|
68
|
+
left: 0,
|
|
69
|
+
nodeKey: null
|
|
70
|
+
});
|
|
71
|
+
const [dropLine, setDropLine] = useState({
|
|
72
|
+
visible: false,
|
|
73
|
+
top: 0,
|
|
74
|
+
left: 0,
|
|
75
|
+
width: 0
|
|
76
|
+
});
|
|
77
|
+
const activeBlockRef = useRef(null);
|
|
78
|
+
const hideTimerRef = useRef(null);
|
|
79
|
+
const hoveringHandleRef = useRef(false);
|
|
80
|
+
const menuOpenCountRef = useRef(0);
|
|
81
|
+
const dragPreviewRef = useRef(null);
|
|
82
|
+
const draggingBlockRef = useRef(null);
|
|
83
|
+
const clearHideTimer = useCallback(() => {
|
|
84
|
+
if (hideTimerRef.current) {
|
|
85
|
+
clearTimeout(hideTimerRef.current);
|
|
86
|
+
hideTimerRef.current = null;
|
|
87
|
+
}
|
|
88
|
+
}, []);
|
|
89
|
+
const scheduleHide = useCallback(() => {
|
|
90
|
+
clearHideTimer();
|
|
91
|
+
hideTimerRef.current = setTimeout(() => {
|
|
92
|
+
if (!hoveringHandleRef.current && menuOpenCountRef.current === 0) {
|
|
93
|
+
activeBlockRef.current = null;
|
|
94
|
+
setHandle((s) => ({ ...s, visible: false, nodeKey: null }));
|
|
95
|
+
}
|
|
96
|
+
}, HIDE_DELAY);
|
|
97
|
+
}, [clearHideTimer]);
|
|
98
|
+
const onHandleEnter = useCallback(() => {
|
|
99
|
+
hoveringHandleRef.current = true;
|
|
100
|
+
clearHideTimer();
|
|
101
|
+
}, [clearHideTimer]);
|
|
102
|
+
const onHandleLeave = useCallback(() => {
|
|
103
|
+
hoveringHandleRef.current = false;
|
|
104
|
+
scheduleHide();
|
|
105
|
+
}, [scheduleHide]);
|
|
106
|
+
const onMenuOpenChange = useCallback(
|
|
107
|
+
(open) => {
|
|
108
|
+
menuOpenCountRef.current += open ? 1 : -1;
|
|
109
|
+
if (!open) scheduleHide();
|
|
110
|
+
else clearHideTimer();
|
|
111
|
+
},
|
|
112
|
+
[scheduleHide, clearHideTimer]
|
|
113
|
+
);
|
|
114
|
+
const updatePositionFromBlock = useCallback(
|
|
115
|
+
(block) => {
|
|
116
|
+
if (block !== void 0) activeBlockRef.current = block;
|
|
117
|
+
const el = activeBlockRef.current;
|
|
118
|
+
if (!el || !el.isConnected) {
|
|
119
|
+
activeBlockRef.current = null;
|
|
120
|
+
setHandle(
|
|
121
|
+
(s) => s.visible ? { ...s, visible: false, nodeKey: null } : s
|
|
122
|
+
);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const rootElement = editor.getRootElement();
|
|
126
|
+
if (!rootElement) return;
|
|
127
|
+
const blockRect = el.getBoundingClientRect();
|
|
128
|
+
const rootRect = rootElement.getBoundingClientRect();
|
|
129
|
+
const page = toPagePosition(blockRect);
|
|
130
|
+
let nodeKey = null;
|
|
131
|
+
editor.read(() => {
|
|
132
|
+
const node = $getNearestNodeFromDOMNode(el);
|
|
133
|
+
if (node) nodeKey = node.getKey();
|
|
134
|
+
});
|
|
135
|
+
setHandle({
|
|
136
|
+
visible: true,
|
|
137
|
+
top: page.top,
|
|
138
|
+
left: toPagePosition(rootRect).left - HANDLE_OFFSET,
|
|
139
|
+
nodeKey
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
[editor]
|
|
143
|
+
);
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
const rootElement = editor.getRootElement();
|
|
146
|
+
if (!rootElement) return;
|
|
147
|
+
let rafId = null;
|
|
148
|
+
const onMouseMove = (e) => {
|
|
149
|
+
if (rafId !== null) return;
|
|
150
|
+
rafId = requestAnimationFrame(() => {
|
|
151
|
+
rafId = null;
|
|
152
|
+
const target = e.target;
|
|
153
|
+
const block = getBlockElement(editor, target);
|
|
154
|
+
if (block) {
|
|
155
|
+
clearHideTimer();
|
|
156
|
+
updatePositionFromBlock(block);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
const onMouseLeave = () => {
|
|
161
|
+
if (!hoveringHandleRef.current && menuOpenCountRef.current === 0) {
|
|
162
|
+
scheduleHide();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
rootElement.addEventListener("mousemove", onMouseMove);
|
|
166
|
+
rootElement.addEventListener("mouseleave", onMouseLeave);
|
|
167
|
+
return () => {
|
|
168
|
+
if (rafId !== null) cancelAnimationFrame(rafId);
|
|
169
|
+
rootElement.removeEventListener("mousemove", onMouseMove);
|
|
170
|
+
rootElement.removeEventListener("mouseleave", onMouseLeave);
|
|
171
|
+
clearHideTimer();
|
|
172
|
+
};
|
|
173
|
+
}, [editor, clearHideTimer, scheduleHide, updatePositionFromBlock]);
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
const update = () => updatePositionFromBlock();
|
|
176
|
+
window.addEventListener("scroll", update, true);
|
|
177
|
+
window.addEventListener("resize", update);
|
|
178
|
+
return () => {
|
|
179
|
+
window.removeEventListener("scroll", update, true);
|
|
180
|
+
window.removeEventListener("resize", update);
|
|
181
|
+
};
|
|
182
|
+
}, [updatePositionFromBlock]);
|
|
183
|
+
useEffect(
|
|
184
|
+
() => editor.registerUpdateListener(() => updatePositionFromBlock()),
|
|
185
|
+
[editor, updatePositionFromBlock]
|
|
186
|
+
);
|
|
187
|
+
const handleAddBlock = useCallback(() => {
|
|
188
|
+
if (!handle.nodeKey) return;
|
|
189
|
+
editor.update(() => {
|
|
190
|
+
const node = $getNodeByKey(handle.nodeKey);
|
|
191
|
+
if (!node) return;
|
|
192
|
+
const p = $createParagraphNode();
|
|
193
|
+
node.insertAfter(p);
|
|
194
|
+
p.selectStart();
|
|
195
|
+
});
|
|
196
|
+
}, [editor, handle.nodeKey]);
|
|
197
|
+
const handleTurnInto = useCallback(
|
|
198
|
+
(type) => {
|
|
199
|
+
const { nodeKey } = handle;
|
|
200
|
+
if (!nodeKey) return;
|
|
201
|
+
if (["bullet", "numbered", "todo", "divider"].includes(type)) {
|
|
202
|
+
editor.update(() => {
|
|
203
|
+
const node = $getNodeByKey(nodeKey);
|
|
204
|
+
if (!node) return;
|
|
205
|
+
if ($isElementNode(node)) node.selectStart();
|
|
206
|
+
});
|
|
207
|
+
const commands = {
|
|
208
|
+
bullet: INSERT_UNORDERED_LIST_COMMAND,
|
|
209
|
+
numbered: INSERT_ORDERED_LIST_COMMAND,
|
|
210
|
+
todo: INSERT_CHECK_LIST_COMMAND,
|
|
211
|
+
divider: INSERT_HORIZONTAL_RULE_COMMAND
|
|
212
|
+
};
|
|
213
|
+
editor.dispatchCommand(commands[type], void 0);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
editor.update(() => {
|
|
217
|
+
const node = $getNodeByKey(nodeKey);
|
|
218
|
+
if (!node || !$isElementNode(node)) return;
|
|
219
|
+
node.selectStart();
|
|
220
|
+
const sel = $getSelection();
|
|
221
|
+
if (!$isRangeSelection(sel)) return;
|
|
222
|
+
const creators = {
|
|
223
|
+
paragraph: () => $createParagraphNode(),
|
|
224
|
+
h1: () => $createHeadingNode("h1"),
|
|
225
|
+
h2: () => $createHeadingNode("h2"),
|
|
226
|
+
h3: () => $createHeadingNode("h3"),
|
|
227
|
+
quote: () => $createQuoteNode(),
|
|
228
|
+
code: () => $createCodeNode()
|
|
229
|
+
};
|
|
230
|
+
const create = creators[type];
|
|
231
|
+
if (create) $setBlocksType(sel, create);
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
[editor, handle.nodeKey]
|
|
235
|
+
);
|
|
236
|
+
const handleDelete = useCallback(() => {
|
|
237
|
+
if (!handle.nodeKey) return;
|
|
238
|
+
editor.update(() => {
|
|
239
|
+
const node = $getNodeByKey(handle.nodeKey);
|
|
240
|
+
node?.remove();
|
|
241
|
+
});
|
|
242
|
+
setHandle((s) => ({ ...s, visible: false, nodeKey: null }));
|
|
243
|
+
}, [editor, handle.nodeKey]);
|
|
244
|
+
const handleDuplicate = useCallback(() => {
|
|
245
|
+
if (!handle.nodeKey) return;
|
|
246
|
+
editor.update(() => {
|
|
247
|
+
const node = $getNodeByKey(handle.nodeKey);
|
|
248
|
+
if (!node) return;
|
|
249
|
+
const clone = $cloneNode(node);
|
|
250
|
+
node.insertAfter(clone);
|
|
251
|
+
});
|
|
252
|
+
}, [editor, handle.nodeKey]);
|
|
253
|
+
const handleMoveUp = useCallback(() => {
|
|
254
|
+
if (!handle.nodeKey) return;
|
|
255
|
+
editor.update(() => {
|
|
256
|
+
const node = $getNodeByKey(handle.nodeKey);
|
|
257
|
+
if (!node) return;
|
|
258
|
+
const prev = node.getPreviousSibling();
|
|
259
|
+
if (prev) {
|
|
260
|
+
node.remove();
|
|
261
|
+
prev.insertBefore(node);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}, [editor, handle.nodeKey]);
|
|
265
|
+
const handleMoveDown = useCallback(() => {
|
|
266
|
+
if (!handle.nodeKey) return;
|
|
267
|
+
editor.update(() => {
|
|
268
|
+
const node = $getNodeByKey(handle.nodeKey);
|
|
269
|
+
if (!node) return;
|
|
270
|
+
const next = node.getNextSibling();
|
|
271
|
+
if (next) {
|
|
272
|
+
node.remove();
|
|
273
|
+
next.insertAfter(node);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}, [editor, handle.nodeKey]);
|
|
277
|
+
const [gripMenuOpen, setGripMenuOpen] = useState(false);
|
|
278
|
+
const dragStartedRef = useRef(false);
|
|
279
|
+
const clearDragVisualState = useCallback(() => {
|
|
280
|
+
const preview = dragPreviewRef.current;
|
|
281
|
+
if (preview) {
|
|
282
|
+
preview.remove();
|
|
283
|
+
dragPreviewRef.current = null;
|
|
284
|
+
}
|
|
285
|
+
const draggingBlock$1 = draggingBlockRef.current;
|
|
286
|
+
if (draggingBlock$1) {
|
|
287
|
+
draggingBlock$1.classList.remove(draggingBlock);
|
|
288
|
+
draggingBlockRef.current = null;
|
|
289
|
+
}
|
|
290
|
+
}, []);
|
|
291
|
+
const onGripDragStart = useCallback(
|
|
292
|
+
(e) => {
|
|
293
|
+
dragStartedRef.current = true;
|
|
294
|
+
if (!e.dataTransfer || !handle.nodeKey) return;
|
|
295
|
+
e.dataTransfer.setData(DRAG_DATA_KEY, handle.nodeKey);
|
|
296
|
+
e.dataTransfer.effectAllowed = "move";
|
|
297
|
+
const block = activeBlockRef.current;
|
|
298
|
+
if (!block) return;
|
|
299
|
+
clearDragVisualState();
|
|
300
|
+
const rect = block.getBoundingClientRect();
|
|
301
|
+
const preview = block.cloneNode(true);
|
|
302
|
+
preview.classList.add(dragPreview);
|
|
303
|
+
preview.style.width = `${rect.width}px`;
|
|
304
|
+
document.body.append(preview);
|
|
305
|
+
draggingBlockRef.current = block;
|
|
306
|
+
dragPreviewRef.current = preview;
|
|
307
|
+
block.classList.add(draggingBlock);
|
|
308
|
+
const offsetX = Math.max(
|
|
309
|
+
12,
|
|
310
|
+
Math.min(rect.width - 12, e.clientX - rect.left)
|
|
311
|
+
);
|
|
312
|
+
const offsetY = Math.max(
|
|
313
|
+
8,
|
|
314
|
+
Math.min(rect.height - 8, e.clientY - rect.top)
|
|
315
|
+
);
|
|
316
|
+
e.dataTransfer.setDragImage(preview, offsetX, offsetY);
|
|
317
|
+
},
|
|
318
|
+
[clearDragVisualState, handle.nodeKey]
|
|
319
|
+
);
|
|
320
|
+
const onGripOpenChange = useCallback(
|
|
321
|
+
(open) => {
|
|
322
|
+
setGripMenuOpen((prev) => {
|
|
323
|
+
if (prev === open) return prev;
|
|
324
|
+
onMenuOpenChange(open);
|
|
325
|
+
return open;
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
[onMenuOpenChange]
|
|
329
|
+
);
|
|
330
|
+
const onGripMouseDownCapture = useCallback((e) => {
|
|
331
|
+
dragStartedRef.current = false;
|
|
332
|
+
if (e.button === 0) e.stopPropagation();
|
|
333
|
+
}, []);
|
|
334
|
+
const onGripClick = useCallback(
|
|
335
|
+
(e) => {
|
|
336
|
+
if (e.detail === 0) return;
|
|
337
|
+
e.preventDefault();
|
|
338
|
+
e.stopPropagation();
|
|
339
|
+
if (dragStartedRef.current) {
|
|
340
|
+
dragStartedRef.current = false;
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
onGripOpenChange(!gripMenuOpen);
|
|
344
|
+
},
|
|
345
|
+
[gripMenuOpen, onGripOpenChange]
|
|
346
|
+
);
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
const rootElement = editor.getRootElement();
|
|
349
|
+
if (!rootElement) return;
|
|
350
|
+
const unregDragOver = editor.registerCommand(
|
|
351
|
+
DRAGOVER_COMMAND,
|
|
352
|
+
(event) => {
|
|
353
|
+
if (!event.dataTransfer?.types.includes(DRAG_DATA_KEY)) return false;
|
|
354
|
+
event.preventDefault();
|
|
355
|
+
event.dataTransfer.dropEffect = "move";
|
|
356
|
+
const target = event.target;
|
|
357
|
+
const block = getBlockElement(editor, target);
|
|
358
|
+
if (block) {
|
|
359
|
+
const rect = block.getBoundingClientRect();
|
|
360
|
+
const rootRect = rootElement.getBoundingClientRect();
|
|
361
|
+
const midY = rect.top + rect.height / 2;
|
|
362
|
+
const y = event.clientY < midY ? rect.top : rect.bottom;
|
|
363
|
+
setDropLine({
|
|
364
|
+
visible: true,
|
|
365
|
+
top: y + window.scrollY,
|
|
366
|
+
left: rootRect.left + window.scrollX,
|
|
367
|
+
width: rootRect.width
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
},
|
|
372
|
+
COMMAND_PRIORITY_HIGH
|
|
373
|
+
);
|
|
374
|
+
const unregDrop = editor.registerCommand(
|
|
375
|
+
DROP_COMMAND,
|
|
376
|
+
(event) => {
|
|
377
|
+
const draggedKey = event.dataTransfer?.getData(DRAG_DATA_KEY);
|
|
378
|
+
if (!draggedKey) return false;
|
|
379
|
+
event.preventDefault();
|
|
380
|
+
setDropLine((s) => ({ ...s, visible: false }));
|
|
381
|
+
clearDragVisualState();
|
|
382
|
+
const target = event.target;
|
|
383
|
+
const block = getBlockElement(editor, target);
|
|
384
|
+
if (!block) return false;
|
|
385
|
+
editor.update(() => {
|
|
386
|
+
const draggedNode = $getNodeByKey(draggedKey);
|
|
387
|
+
const targetNode = $getNearestNodeFromDOMNode(block);
|
|
388
|
+
if (!draggedNode || !targetNode || draggedNode === targetNode) return;
|
|
389
|
+
const rect = block.getBoundingClientRect();
|
|
390
|
+
const midY = rect.top + rect.height / 2;
|
|
391
|
+
draggedNode.remove();
|
|
392
|
+
if (event.clientY < midY) {
|
|
393
|
+
targetNode.insertBefore(draggedNode);
|
|
394
|
+
} else {
|
|
395
|
+
targetNode.insertAfter(draggedNode);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
return true;
|
|
399
|
+
},
|
|
400
|
+
COMMAND_PRIORITY_HIGH
|
|
401
|
+
);
|
|
402
|
+
const unregDragStart = editor.registerCommand(
|
|
403
|
+
DRAGSTART_COMMAND,
|
|
404
|
+
(event) => {
|
|
405
|
+
if (!event.dataTransfer?.types.includes(DRAG_DATA_KEY)) return false;
|
|
406
|
+
return true;
|
|
407
|
+
},
|
|
408
|
+
COMMAND_PRIORITY_HIGH
|
|
409
|
+
);
|
|
410
|
+
const clearDropLine = () => {
|
|
411
|
+
setDropLine((s) => s.visible ? { ...s, visible: false } : s);
|
|
412
|
+
};
|
|
413
|
+
const clearDragState = () => {
|
|
414
|
+
clearDropLine();
|
|
415
|
+
clearDragVisualState();
|
|
416
|
+
};
|
|
417
|
+
const onDragLeave = (e) => {
|
|
418
|
+
if (e.relatedTarget === null || !rootElement.contains(e.relatedTarget)) {
|
|
419
|
+
clearDropLine();
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
window.addEventListener("dragend", clearDragState);
|
|
423
|
+
window.addEventListener("drop", clearDragState);
|
|
424
|
+
rootElement.addEventListener("dragleave", onDragLeave);
|
|
425
|
+
return () => {
|
|
426
|
+
unregDragOver();
|
|
427
|
+
unregDrop();
|
|
428
|
+
unregDragStart();
|
|
429
|
+
window.removeEventListener("dragend", clearDragState);
|
|
430
|
+
window.removeEventListener("drop", clearDragState);
|
|
431
|
+
rootElement.removeEventListener("dragleave", onDragLeave);
|
|
432
|
+
clearDragState();
|
|
433
|
+
};
|
|
434
|
+
}, [clearDragVisualState, editor]);
|
|
435
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
436
|
+
/* @__PURE__ */ jsxs(
|
|
437
|
+
"div",
|
|
438
|
+
{
|
|
439
|
+
className: `${handleContainer} ${handle.visible ? handleContainerVisible : ""}`,
|
|
440
|
+
style: { top: handle.top, left: handle.left },
|
|
441
|
+
onMouseEnter: onHandleEnter,
|
|
442
|
+
onMouseLeave: onHandleLeave,
|
|
443
|
+
children: [
|
|
444
|
+
/* @__PURE__ */ jsx(
|
|
445
|
+
"button",
|
|
446
|
+
{
|
|
447
|
+
className: handleBtn,
|
|
448
|
+
"aria-label": "Add block",
|
|
449
|
+
onClick: handleAddBlock,
|
|
450
|
+
children: /* @__PURE__ */ jsx(Plus, { size: 14 })
|
|
451
|
+
}
|
|
452
|
+
),
|
|
453
|
+
/* @__PURE__ */ jsxs(DropdownMenu, { open: gripMenuOpen, onOpenChange: onGripOpenChange, children: [
|
|
454
|
+
/* @__PURE__ */ jsx(
|
|
455
|
+
DropdownMenuTrigger,
|
|
456
|
+
{
|
|
457
|
+
className: handleBtn,
|
|
458
|
+
"aria-label": "Block actions",
|
|
459
|
+
draggable: true,
|
|
460
|
+
onDragStart: onGripDragStart,
|
|
461
|
+
onMouseDownCapture: onGripMouseDownCapture,
|
|
462
|
+
onClick: onGripClick,
|
|
463
|
+
children: /* @__PURE__ */ jsx(GripVertical, { size: 14 })
|
|
464
|
+
}
|
|
465
|
+
),
|
|
466
|
+
/* @__PURE__ */ jsxs(DropdownMenuContent, { side: "bottom", align: "start", sideOffset: 4, children: [
|
|
467
|
+
/* @__PURE__ */ jsxs(DropdownMenuGroup, { children: [
|
|
468
|
+
/* @__PURE__ */ jsx(DropdownMenuLabel, { children: "TURN INTO" }),
|
|
469
|
+
TURN_INTO_ITEMS.map((item) => /* @__PURE__ */ jsxs(
|
|
470
|
+
DropdownMenuItem,
|
|
471
|
+
{
|
|
472
|
+
onClick: () => handleTurnInto(item.key),
|
|
473
|
+
children: [
|
|
474
|
+
/* @__PURE__ */ jsx(item.icon, { size: 14 }),
|
|
475
|
+
item.label
|
|
476
|
+
]
|
|
477
|
+
},
|
|
478
|
+
item.key
|
|
479
|
+
))
|
|
480
|
+
] }),
|
|
481
|
+
/* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
|
|
482
|
+
/* @__PURE__ */ jsxs(DropdownMenuGroup, { children: [
|
|
483
|
+
/* @__PURE__ */ jsx(DropdownMenuLabel, { children: "ACTIONS" }),
|
|
484
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: handleDuplicate, children: [
|
|
485
|
+
/* @__PURE__ */ jsx(Copy, { size: 14 }),
|
|
486
|
+
"Duplicate"
|
|
487
|
+
] }),
|
|
488
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: handleMoveUp, children: [
|
|
489
|
+
/* @__PURE__ */ jsx(ArrowUp, { size: 14 }),
|
|
490
|
+
"Move Up"
|
|
491
|
+
] }),
|
|
492
|
+
/* @__PURE__ */ jsxs(DropdownMenuItem, { onClick: handleMoveDown, children: [
|
|
493
|
+
/* @__PURE__ */ jsx(ArrowDown, { size: 14 }),
|
|
494
|
+
"Move Down"
|
|
495
|
+
] })
|
|
496
|
+
] }),
|
|
497
|
+
/* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
|
|
498
|
+
/* @__PURE__ */ jsxs(
|
|
499
|
+
DropdownMenuItem,
|
|
500
|
+
{
|
|
501
|
+
className: menuItemDestructive,
|
|
502
|
+
onClick: handleDelete,
|
|
503
|
+
children: [
|
|
504
|
+
/* @__PURE__ */ jsx(Trash2, { size: 14 }),
|
|
505
|
+
"Delete"
|
|
506
|
+
]
|
|
507
|
+
}
|
|
508
|
+
)
|
|
509
|
+
] })
|
|
510
|
+
] })
|
|
511
|
+
]
|
|
512
|
+
}
|
|
513
|
+
),
|
|
514
|
+
dropLine.visible && /* @__PURE__ */ jsx(
|
|
515
|
+
"div",
|
|
516
|
+
{
|
|
517
|
+
className: dropIndicator,
|
|
518
|
+
style: {
|
|
519
|
+
top: dropLine.top,
|
|
520
|
+
left: dropLine.left,
|
|
521
|
+
width: dropLine.width
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
)
|
|
525
|
+
] });
|
|
526
|
+
}
|
|
527
|
+
function BlockHandlePlugin() {
|
|
528
|
+
const [editor] = useLexicalComposerContext();
|
|
529
|
+
return createPortal(/* @__PURE__ */ jsx(BlockHandleInner, { editor }), document.body);
|
|
530
|
+
}
|
|
531
|
+
export {
|
|
532
|
+
BlockHandlePlugin
|
|
533
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
.iihqkc0 {
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: center;
|
|
4
|
+
gap: 1px;
|
|
5
|
+
position: absolute;
|
|
6
|
+
z-index: 30;
|
|
7
|
+
opacity: 0;
|
|
8
|
+
pointer-events: none;
|
|
9
|
+
transition: opacity 0.15s;
|
|
10
|
+
}
|
|
11
|
+
.iihqkc1 {
|
|
12
|
+
opacity: 1;
|
|
13
|
+
pointer-events: auto;
|
|
14
|
+
}
|
|
15
|
+
.iihqkc2 {
|
|
16
|
+
display: flex;
|
|
17
|
+
align-items: center;
|
|
18
|
+
justify-content: center;
|
|
19
|
+
width: 24px;
|
|
20
|
+
height: 24px;
|
|
21
|
+
border-radius: 4px;
|
|
22
|
+
border: none;
|
|
23
|
+
background: transparent;
|
|
24
|
+
color: color-mix(in srgb, var(--rc-text) 35%, transparent);
|
|
25
|
+
cursor: pointer;
|
|
26
|
+
padding: 0;
|
|
27
|
+
transition: background-color 0.15s, color 0.15s;
|
|
28
|
+
}
|
|
29
|
+
.iihqkc2:hover {
|
|
30
|
+
background: color-mix(in srgb, var(--rc-text) 8%, transparent);
|
|
31
|
+
color: color-mix(in srgb, var(--rc-text) 70%, transparent);
|
|
32
|
+
}
|
|
33
|
+
.iihqkc3 {
|
|
34
|
+
opacity: 0.35;
|
|
35
|
+
}
|
|
36
|
+
.iihqkc4 {
|
|
37
|
+
position: fixed;
|
|
38
|
+
top: -10000px;
|
|
39
|
+
left: -10000px;
|
|
40
|
+
z-index: 40;
|
|
41
|
+
pointer-events: none;
|
|
42
|
+
margin: 0;
|
|
43
|
+
box-sizing: border-box;
|
|
44
|
+
opacity: 0.9;
|
|
45
|
+
background-color: var(--rc-bg);
|
|
46
|
+
border: 1px solid var(--rc-border);
|
|
47
|
+
border-radius: var(--rc-radius-sm);
|
|
48
|
+
box-shadow: 0 10px 28px color-mix(in srgb, var(--rc-text) 18%, transparent);
|
|
49
|
+
}
|
|
50
|
+
.iihqkc5 {
|
|
51
|
+
position: absolute;
|
|
52
|
+
height: 2px;
|
|
53
|
+
background: var(--rc-accent);
|
|
54
|
+
border-radius: 1px;
|
|
55
|
+
z-index: 30;
|
|
56
|
+
pointer-events: none;
|
|
57
|
+
}
|
|
58
|
+
.iihqkc6 {
|
|
59
|
+
color: var(--rc-alert-caution);
|
|
60
|
+
}
|
|
61
|
+
.iihqkc6[data-highlighted] {
|
|
62
|
+
color: var(--rc-alert-caution);
|
|
63
|
+
background-color: color-mix(in srgb, var(--rc-alert-caution) 8%, transparent);
|
|
64
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const handleContainer: string;
|
|
2
|
+
export declare const handleContainerVisible: string;
|
|
3
|
+
export declare const handleBtn: string;
|
|
4
|
+
export declare const draggingBlock: string;
|
|
5
|
+
export declare const dragPreview: string;
|
|
6
|
+
export declare const dropIndicator: string;
|
|
7
|
+
export declare const menuItemDestructive: string;
|
|
8
|
+
//# sourceMappingURL=styles.css.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.css.d.ts","sourceRoot":"","sources":["../src/styles.css.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,eAAe,QAS1B,CAAA;AAEF,eAAO,MAAM,sBAAsB,QAGjC,CAAA;AAGF,eAAO,MAAM,SAAS,QAiBpB,CAAA;AAGF,eAAO,MAAM,aAAa,QAExB,CAAA;AAEF,eAAO,MAAM,WAAW,QAatB,CAAA;AAGF,eAAO,MAAM,aAAa,QAOxB,CAAA;AAGF,eAAO,MAAM,mBAAmB,QAQ9B,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haklex/rich-plugin-block-handle",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Block handle plugin with add button and context menu",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./style.css": "./dist/rich-plugin-block-handle.css"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.mjs",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@haklex/rich-editor-ui": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@lexical/code": "^0.39.0",
|
|
23
|
+
"@lexical/list": "^0.39.0",
|
|
24
|
+
"@lexical/react": "^0.39.0",
|
|
25
|
+
"@lexical/rich-text": "^0.39.0",
|
|
26
|
+
"@lexical/selection": "^0.39.0",
|
|
27
|
+
"@types/react": "^19.0.0",
|
|
28
|
+
"@types/react-dom": "^19.0.0",
|
|
29
|
+
"@vanilla-extract/css": "^1.17.1",
|
|
30
|
+
"@vanilla-extract/vite-plugin": "^4.0.20",
|
|
31
|
+
"lexical": "^0.39.0",
|
|
32
|
+
"lucide-react": "^0.574.0",
|
|
33
|
+
"react": "19.2.4",
|
|
34
|
+
"react-dom": "19.2.4",
|
|
35
|
+
"typescript": "^5.9.0",
|
|
36
|
+
"vite": "^7.3.1",
|
|
37
|
+
"vite-plugin-dts": "^4.5.0",
|
|
38
|
+
"@haklex/rich-style-token": "0.0.1"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"@lexical/code": "^0.39.0",
|
|
42
|
+
"@lexical/list": "^0.39.0",
|
|
43
|
+
"@lexical/react": "^0.39.0",
|
|
44
|
+
"@lexical/rich-text": "^0.39.0",
|
|
45
|
+
"@lexical/selection": "^0.39.0",
|
|
46
|
+
"lexical": "^0.39.0",
|
|
47
|
+
"lucide-react": "^0.574.0",
|
|
48
|
+
"react": ">=19",
|
|
49
|
+
"react-dom": ">=19",
|
|
50
|
+
"@haklex/rich-style-token": "0.0.1"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "vite build",
|
|
57
|
+
"dev:build": "vite build --watch"
|
|
58
|
+
},
|
|
59
|
+
"types": "./dist/index.d.ts"
|
|
60
|
+
}
|