@kidecms/core 0.27.1 → 0.27.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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { useEffect, useRef, useState, type RefObject } from "react";
|
|
4
4
|
import type { Editor } from "@tiptap/core";
|
|
5
5
|
import Image from "@tiptap/extension-image";
|
|
6
6
|
import { NodeViewWrapper, ReactNodeViewRenderer, type NodeViewProps } from "@tiptap/react";
|
|
@@ -10,19 +10,29 @@ import { Input } from "./ui/input";
|
|
|
10
10
|
import ImageBrowseDialog from "./ImageBrowseDialog";
|
|
11
11
|
import { cn, thumbnail } from "../lib/utils";
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Node views only re-render on node/selection changes, so track editor focus
|
|
15
|
+
* ourselves. A blur whose focus lands inside `within` (the node's own toolbar)
|
|
16
|
+
* is ignored: in Chrome a mousedown on a button moves focus there before the
|
|
17
|
+
* click fires, and dropping `focused` at that point would unmount the toolbar
|
|
18
|
+
* under the pointer, so the click never happens.
|
|
19
|
+
*/
|
|
20
|
+
function useEditorFocus(editor: Editor, within: RefObject<HTMLElement | null>) {
|
|
15
21
|
const [focused, setFocused] = useState(editor.isFocused);
|
|
16
22
|
useEffect(() => {
|
|
17
23
|
const on = () => setFocused(true);
|
|
18
|
-
const off = () =>
|
|
24
|
+
const off = ({ event }: { event: FocusEvent }) => {
|
|
25
|
+
const next = event.relatedTarget as Node | null;
|
|
26
|
+
if (next && within.current?.contains(next)) return;
|
|
27
|
+
setFocused(false);
|
|
28
|
+
};
|
|
19
29
|
editor.on("focus", on);
|
|
20
30
|
editor.on("blur", off);
|
|
21
31
|
return () => {
|
|
22
32
|
editor.off("focus", on);
|
|
23
33
|
editor.off("blur", off);
|
|
24
34
|
};
|
|
25
|
-
}, [editor]);
|
|
35
|
+
}, [editor, within]);
|
|
26
36
|
return focused;
|
|
27
37
|
}
|
|
28
38
|
|
|
@@ -34,7 +44,8 @@ function useEditorFocus(editor: Editor) {
|
|
|
34
44
|
function ImageNodeView({ node, selected, editor, getPos, updateAttributes, deleteNode }: NodeViewProps) {
|
|
35
45
|
const [browseOpen, setBrowseOpen] = useState(false);
|
|
36
46
|
const [focusWithin, setFocusWithin] = useState(false);
|
|
37
|
-
const
|
|
47
|
+
const wrapperRef = useRef<HTMLDivElement>(null);
|
|
48
|
+
const focused = useEditorFocus(editor, wrapperRef);
|
|
38
49
|
const src = String(node.attrs.src ?? "");
|
|
39
50
|
const alt = String(node.attrs.alt ?? "");
|
|
40
51
|
|
|
@@ -56,7 +67,6 @@ function ImageNodeView({ node, selected, editor, getPos, updateAttributes, delet
|
|
|
56
67
|
// draws a bare <img> at natural size, so the ghost is a sized div. A real drag
|
|
57
68
|
// starts on Tiptap's outer node element (the wrapper's parent), and Tiptap
|
|
58
69
|
// sets its own ghost from React's root listener — override from the document.
|
|
59
|
-
const wrapperRef = useRef<HTMLDivElement>(null);
|
|
60
70
|
const ghostRef = useRef<HTMLDivElement>(null);
|
|
61
71
|
useEffect(() => {
|
|
62
72
|
const onDragStart = (e: DragEvent) => {
|