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,23 +44,20 @@ export default function RelationMap({ note, ntxId }: TypeWidgetProps) {
};
},
onContentChange(content) {
let newData: MapData | null = null;
if (content) {
try {
const data = JSON.parse(content);
setData(data);
mapApiRef.current = new RelationMapApi(note, data, (newData, refreshUi) => {
if (refreshUi) {
setData(newData);
}
spacedUpdate.scheduleUpdate();
});
return;
} catch (e) {
console.log("Could not parse content: ", e);
}
}
setData({
if (!newData) {
newData = {
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
@ -71,6 +68,15 @@ export default function RelationMap({ note, ntxId }: TypeWidgetProps) {
y: 0,
scale: 1
}
};
}
setData(newData);
mapApiRef.current = new RelationMapApi(note, newData, (newData, refreshUi) => {
if (refreshUi) {
setData(newData);
}
spacedUpdate.scheduleUpdate();
});
},
dataSaved() {
@ -285,7 +291,7 @@ function useNoteCreation({ ntxId, note, containerRef, mapApiRef }: {
});
const onClickHandler = useCallback((e: MouseEvent) => {
const clipboard = clipboardRef.current;
if (clipboard && containerRef.current) {
if (clipboard && containerRef.current && mapApiRef.current) {
const zoom = getZoom(containerRef.current);
let { x, y } = getMousePosition(e, containerRef.current, zoom);
@ -293,7 +299,7 @@ function useNoteCreation({ ntxId, note, containerRef, mapApiRef }: {
x -= 80;
y -= 15;
mapApiRef.current?.createItem({ noteId: clipboard.noteId, x, y });
mapApiRef.current.createItem({ noteId: clipboard.noteId, x, y });
clipboardRef.current = null;
}
}, []);