chore(react/type_widgets): add dragigng logic for jsplumb

This commit is contained in:
Elian Doran
2025-10-03 11:58:00 +03:00
parent 58aea03114
commit c5c304f85b
2 changed files with 28 additions and 6 deletions

View File

@@ -358,9 +358,17 @@ function NoteBox({ noteId, x, y, mapApiRef }: MapDataNoteEntry & { mapApiRef: Re
return note && (
<JsPlumbItem
id={idToNoteId(note.noteId)}
className={`note-box ${note?.getCssClass()}`}
onContextMenu={contextMenuHandler}
x={x} y={y}
draggable={{
start() {},
drag() {},
stop(params) {
},
}}
>
<NoteLink className="title" title={title} notePath={noteId} noTnLink noContextMenu />
<div className="endpoint" title={t("relation_map.start_dragging_relations")} />

View File

@@ -1,7 +1,9 @@
import { jsPlumb, Defaults, jsPlumbInstance } from "jsplumb";
import { ComponentChildren, RefObject } from "preact";
import { jsPlumb, Defaults, jsPlumbInstance, DragOptions } from "jsplumb";
import { ComponentChildren, createContext, RefObject } from "preact";
import { HTMLProps } from "preact/compat";
import { useEffect, useRef } from "preact/hooks";
import { useContext, useEffect, useRef } from "preact/hooks";
const JsPlumbInstance = createContext<RefObject<jsPlumbInstance> | undefined>(undefined);
export function JsPlumb({ className, props, children, containerRef: externalContainerRef, apiRef, onInstanceCreated }: {
className?: string;
@@ -36,18 +38,30 @@ export function JsPlumb({ className, props, children, containerRef: externalCont
return (
<div ref={containerRef} className={className}>
{children}
<JsPlumbInstance.Provider value={apiRef}>
{children}
</JsPlumbInstance.Provider>
</div>
)
}
export function JsPlumbItem({ x, y, children, ...restProps }: {
export function JsPlumbItem({ x, y, children, draggable, ...restProps }: {
x: number;
y: number;
children: ComponentChildren;
} & Pick<HTMLProps<HTMLDivElement>, "className" | "onContextMenu">) {
draggable?: DragOptions;
} & Pick<HTMLProps<HTMLDivElement>, "id" | "className" | "onContextMenu">) {
const containerRef = useRef<HTMLDivElement>(null);
const apiRef = useContext(JsPlumbInstance);
useEffect(() => {
if (!draggable || !apiRef?.current || !containerRef.current) return;
apiRef.current.draggable(containerRef.current, draggable);
}, [ draggable ]);
return (
<div
ref={containerRef}
{...restProps}
style={{ left: x, top: y }}
>