fix(react/type_widgets): unable to add new items if the map is empty

This commit is contained in:
Elian Doran 2025-10-03 10:11:21 +03:00
parent 838d761b50
commit 2d67aab288
No known key found for this signature in database

View File

@ -44,33 +44,39 @@ export default function RelationMap({ note, ntxId }: TypeWidgetProps) {
}; };
}, },
onContentChange(content) { onContentChange(content) {
let newData: MapData | null = null;
if (content) { if (content) {
try { try {
const data = JSON.parse(content); const data = JSON.parse(content);
setData(data); setData(data);
mapApiRef.current = new RelationMapApi(note, data, (newData, refreshUi) => {
if (refreshUi) {
setData(newData);
}
spacedUpdate.scheduleUpdate();
});
return; return;
} catch (e) { } catch (e) {
console.log("Could not parse content: ", e); console.log("Could not parse content: ", e);
} }
} }
setData({ if (!newData) {
notes: [], newData = {
// it is important to have this exact value here so that initial transform is the same as this notes: [],
// which will guarantee note won't be saved on first conversion to the relation map note type // it is important to have this exact value here so that initial transform is the same as this
// this keeps the principle that note type change doesn't destroy note content unless user // which will guarantee note won't be saved on first conversion to the relation map note type
// does some actual change // this keeps the principle that note type change doesn't destroy note content unless user
transform: { // does some actual change
x: 0, transform: {
y: 0, x: 0,
scale: 1 y: 0,
scale: 1
}
};
}
setData(newData);
mapApiRef.current = new RelationMapApi(note, newData, (newData, refreshUi) => {
if (refreshUi) {
setData(newData);
} }
spacedUpdate.scheduleUpdate();
}); });
}, },
dataSaved() { dataSaved() {
@ -285,7 +291,7 @@ function useNoteCreation({ ntxId, note, containerRef, mapApiRef }: {
}); });
const onClickHandler = useCallback((e: MouseEvent) => { const onClickHandler = useCallback((e: MouseEvent) => {
const clipboard = clipboardRef.current; const clipboard = clipboardRef.current;
if (clipboard && containerRef.current) { if (clipboard && containerRef.current && mapApiRef.current) {
const zoom = getZoom(containerRef.current); const zoom = getZoom(containerRef.current);
let { x, y } = getMousePosition(e, containerRef.current, zoom); let { x, y } = getMousePosition(e, containerRef.current, zoom);
@ -293,7 +299,7 @@ function useNoteCreation({ ntxId, note, containerRef, mapApiRef }: {
x -= 80; x -= 80;
y -= 15; y -= 15;
mapApiRef.current?.createItem({ noteId: clipboard.noteId, x, y }); mapApiRef.current.createItem({ noteId: clipboard.noteId, x, y });
clipboardRef.current = null; clipboardRef.current = null;
} }
}, []); }, []);