fix: enable autosave to sync user modifications in draw.io

Previously, user modifications made directly in draw.io (moving nodes,
editing text, etc.) were not captured by React state until an explicit
export was triggered.

This adds the autosave feature from react-drawio:
- Enable autosave={true} on DrawIoEmbed component
- Add onAutoSave callback to update chartXML on every change
- Keep React state in sync with draw.io editor state
This commit is contained in:
dayuan.jiang
2026-01-13 22:54:02 +09:00
parent 6bd26c8bbd
commit a0e3130ac2
2 changed files with 18 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ interface DiagramContextType {
resolverRef: React.Ref<((value: string) => void) | null>
drawioRef: React.Ref<DrawIoEmbedRef | null>
handleDiagramExport: (data: any) => void
handleAutoSave: (data: any) => void
clearDiagram: () => void
saveDiagramToFile: (
filename: string,
@@ -226,6 +227,13 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
}
}
// Handle autosave events from draw.io - keeps chartXML in sync with user modifications
const handleAutoSave = (data: any) => {
if (data.xml) {
setChartXML(data.xml)
}
}
const clearDiagram = () => {
const emptyDiagram = `<mxfile><diagram name="Page-1" id="page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`
// Skip validation for trusted internal template (loadDiagram also sets chartXML)
@@ -350,6 +358,7 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
resolverRef,
drawioRef,
handleDiagramExport,
handleAutoSave,
clearDiagram,
saveDiagramToFile,
getThumbnailSvg,