Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/text note transclusion: fix an issue
Adorian Doranad751627ded1
diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsxindex a3e2b64c80..b1e8464b67 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx@@ -409,6 +409,38 @@ describe("ContentEmbed", () => {expect(document.activeElement).toBe(box);expect(isActive()).toBe(true);});++ it("lets a drag reach its content, with the backdrop back once the drag leaves or ends", () => {+ renderBox();+ const box = contentBox();+ const body = box.parentElement;+ const backdrop = () => container.querySelector<HTMLElement>(".include-note-backdrop");+ if (!body) throw new Error("Expected the body of the embed.");+ body.getBoundingClientRect = () => new DOMRect(0, 0, 100, 100);+ const drag = (target: EventTarget, type: string, clientX = 50) => act(() => {+ target.dispatchEvent(new MouseEvent(type, { bubbles: true, clientX, clientY: 50 }));+ });++ const firstBackdrop = backdrop();+ expect(firstBackdrop).not.toBeNull();+ if (!firstBackdrop) return;+ drag(firstBackdrop, "dragenter");+ expect(backdrop()).toBeNull();+ // Moving between the elements of the content keeps the drag inside.+ drag(box, "dragleave");+ expect(backdrop()).toBeNull();+ drag(box, "dragleave", 150);+ expect(backdrop()).not.toBeNull();++ for (const [ target, end ] of [ [ box, "drop" ], [ document, "dragend" ] ] as const) {+ const shown = backdrop();+ if (!shown) return;+ drag(shown, "dragenter");+ expect(backdrop()).toBeNull();+ drag(target, end);+ expect(backdrop()).not.toBeNull();+ }+ });});describe("ContentEmbed focused on mount", () => {diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsxindex 250154760b..e78b463ea8 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx@@ -52,7 +52,9 @@ export default function ContentEmbed({boxSize, title, content, contentType, notePath, viewScope, isFocusedOnMount}: ContentEmbedProps) {const contentRef = useRef<HTMLDivElement>(null);+ const bodyRef = useRef<HTMLDivElement>(null);const isContentActive = useFocusWithin(contentRef);+ const isDraggedOver = useIsDraggedOver(bodyRef);useFullscreenEvents(contentRef);const [ isExpanded, setIsExpanded ] = useState(false);const isExpandable = boxSize === "expandable";@@ -110,7 +112,10 @@ export default function ContentEmbed({)}<MoreActionsButton notePath={notePath} viewScope={viewScope} /></div>- <div className={clsx("include-note-body", isContentActive && "active")}>+ <div+ ref={bodyRef}+ className={clsx("include-note-body", isContentActive && "active")}+ ><divref={contentRef}className={`include-note-content type-${contentType}`}@@ -137,7 +142,7 @@ export default function ContentEmbed({</div>)}</div>- {!isContentActive && (+ {!isContentActive && !isDraggedOver && (<divclassName="include-note-backdrop"onClick={(e) => {@@ -250,6 +255,42 @@ function ContentEmbedActionButton({ className, action }: {* Opens the menu of the embed for a right click on its title row. The title link is left to the* handler of every link, which opens the same menu, or a quick edit with Ctrl.*/+/**+ * Whether something is dragged over the body of the embed, which then lets the drag reach the+ * content under the backdrop, such as the editor of an included note.+ */+function useIsDraggedOver(bodyRef: RefObject<HTMLElement | null>) {+ const [ isDraggedOver, setIsDraggedOver ] = useState(false);++ useEffect(() => {+ const body = bodyRef.current;+ if (!body) return;++ const enter = () => setIsDraggedOver(true);+ const end = () => setIsDraggedOver(false);+ // A `dragleave` also fires when the drag moves between the elements of the body.+ const leave = (event: DragEvent) => {+ const { left, top, right, bottom } = body.getBoundingClientRect();+ const { clientX: x, clientY: y } = event;+ if (x < left || x >= right || y < top || y >= bottom) {+ end();+ }+ };+ body.addEventListener("dragenter", enter);+ body.addEventListener("dragleave", leave);+ body.addEventListener("drop", end, true);+ document.addEventListener("dragend", end);+ return () => {+ body.removeEventListener("dragenter", enter);+ body.removeEventListener("dragleave", leave);+ body.removeEventListener("drop", end, true);+ document.removeEventListener("dragend", end);+ };+ }, [ bodyRef ]);++ return isDraggedOver;+}+/*** Dispatches `fullscreenChangeStart` on the content box as it enters or leaves fullscreen, then* `enterFullscreen` or `leaveFullscreen` once it has the size that fullscreen gives or takes back.