mirror of
https://github.com/TriliumNext/Trilium.git
synced 2025-12-10 03:53:37 -06:00
refactor(type_widgets): use editorspaced update for data
This commit is contained in:
parent
d8b9d14712
commit
39bd236799
@ -37,9 +37,40 @@ const uniDirectionalOverlays: OverlaySpec[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export default function RelationMap({ note }: TypeWidgetProps) {
|
export default function RelationMap({ note }: TypeWidgetProps) {
|
||||||
const data = useData(note);
|
const [ data, setData ] = useState<MapData>();
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
const apiRef = useRef<jsPlumbInstance>(null);
|
const apiRef = useRef<jsPlumbInstance>(null);
|
||||||
|
const spacedUpdate = useEditorSpacedUpdate({
|
||||||
|
note,
|
||||||
|
getData() {
|
||||||
|
},
|
||||||
|
onContentChange(content) {
|
||||||
|
if (content) {
|
||||||
|
try {
|
||||||
|
setData(JSON.parse(content));
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Could not parse content: ", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setData({
|
||||||
|
notes: [],
|
||||||
|
// it is important to have this exact value here so that initial transform is the same as this
|
||||||
|
// which will guarantee note won't be saved on first conversion to the relation map note type
|
||||||
|
// this keeps the principle that note type change doesn't destroy note content unless user
|
||||||
|
// does some actual change
|
||||||
|
transform: {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
scale: 1
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
dataSaved() {
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const onTransform = useCallback(() => {
|
const onTransform = useCallback(() => {
|
||||||
if (!containerRef.current || !apiRef.current) return;
|
if (!containerRef.current || !apiRef.current) return;
|
||||||
@ -60,7 +91,7 @@ export default function RelationMap({ note }: TypeWidgetProps) {
|
|||||||
return e.altKey;
|
return e.altKey;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
transformData: data.transform,
|
transformData: data?.transform,
|
||||||
onTransform
|
onTransform
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -78,7 +109,7 @@ export default function RelationMap({ note }: TypeWidgetProps) {
|
|||||||
HoverPaintStyle: { stroke: "#777", strokeWidth: 1 },
|
HoverPaintStyle: { stroke: "#777", strokeWidth: 1 },
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{data.notes.map(note => (
|
{data?.notes.map(note => (
|
||||||
<NoteBox {...note} />
|
<NoteBox {...note} />
|
||||||
))}
|
))}
|
||||||
</JsPlumb>
|
</JsPlumb>
|
||||||
@ -87,36 +118,6 @@ export default function RelationMap({ note }: TypeWidgetProps) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function useData(note: FNote) {
|
|
||||||
const blob = useNoteBlob(note);
|
|
||||||
let content: MapData | null = null;
|
|
||||||
|
|
||||||
if (blob?.content) {
|
|
||||||
try {
|
|
||||||
content = JSON.parse(blob.content);
|
|
||||||
} catch (e) {
|
|
||||||
console.log("Could not parse content: ", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!content) {
|
|
||||||
content = {
|
|
||||||
notes: [],
|
|
||||||
// it is important to have this exact value here so that initial transform is the same as this
|
|
||||||
// which will guarantee note won't be saved on first conversion to the relation map note type
|
|
||||||
// this keeps the principle that note type change doesn't destroy note content unless user
|
|
||||||
// does some actual change
|
|
||||||
transform: {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
scale: 1
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
function usePanZoom({ containerRef, options, transformData, onTransform }: {
|
function usePanZoom({ containerRef, options, transformData, onTransform }: {
|
||||||
containerRef: RefObject<HTMLElement>;
|
containerRef: RefObject<HTMLElement>;
|
||||||
options: PanZoomOptions;
|
options: PanZoomOptions;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user