mirror of
https://github.com/zadam/trilium.git
synced 2025-11-02 11:26:15 +01:00
chore(react/type_widgets): add dragigng logic for jsplumb
This commit is contained in:
@@ -358,9 +358,17 @@ function NoteBox({ noteId, x, y, mapApiRef }: MapDataNoteEntry & { mapApiRef: Re
|
|||||||
|
|
||||||
return note && (
|
return note && (
|
||||||
<JsPlumbItem
|
<JsPlumbItem
|
||||||
|
id={idToNoteId(note.noteId)}
|
||||||
className={`note-box ${note?.getCssClass()}`}
|
className={`note-box ${note?.getCssClass()}`}
|
||||||
onContextMenu={contextMenuHandler}
|
onContextMenu={contextMenuHandler}
|
||||||
x={x} y={y}
|
x={x} y={y}
|
||||||
|
draggable={{
|
||||||
|
start() {},
|
||||||
|
drag() {},
|
||||||
|
stop(params) {
|
||||||
|
|
||||||
|
},
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<NoteLink className="title" title={title} notePath={noteId} noTnLink noContextMenu />
|
<NoteLink className="title" title={title} notePath={noteId} noTnLink noContextMenu />
|
||||||
<div className="endpoint" title={t("relation_map.start_dragging_relations")} />
|
<div className="endpoint" title={t("relation_map.start_dragging_relations")} />
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { jsPlumb, Defaults, jsPlumbInstance } from "jsplumb";
|
import { jsPlumb, Defaults, jsPlumbInstance, DragOptions } from "jsplumb";
|
||||||
import { ComponentChildren, RefObject } from "preact";
|
import { ComponentChildren, createContext, RefObject } from "preact";
|
||||||
import { HTMLProps } from "preact/compat";
|
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 }: {
|
export function JsPlumb({ className, props, children, containerRef: externalContainerRef, apiRef, onInstanceCreated }: {
|
||||||
className?: string;
|
className?: string;
|
||||||
@@ -36,18 +38,30 @@ export function JsPlumb({ className, props, children, containerRef: externalCont
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref={containerRef} className={className}>
|
<div ref={containerRef} className={className}>
|
||||||
{children}
|
<JsPlumbInstance.Provider value={apiRef}>
|
||||||
|
{children}
|
||||||
|
</JsPlumbInstance.Provider>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function JsPlumbItem({ x, y, children, ...restProps }: {
|
export function JsPlumbItem({ x, y, children, draggable, ...restProps }: {
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
children: ComponentChildren;
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
|
ref={containerRef}
|
||||||
{...restProps}
|
{...restProps}
|
||||||
style={{ left: x, top: y }}
|
style={{ left: x, top: y }}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user