fix(diagram): wrap XML in mxfile structure when canvas is empty (#156)

When clicking an example diagram on a clean page (no existing diagram),
the code was passing raw <root>...</root> format XML directly to DrawIO.
However, DrawIO expects the full mxfile/diagram/mxGraphModel/root structure.

This fix provides a default mxfile template when chartXML is empty,
ensuring replaceNodes properly wraps the content before loading into DrawIO.

Co-authored-by: dayuan.jiang <jiangdy@amazon.co.jp>
This commit is contained in:
Dayuan Jiang
2025-12-08 10:10:52 +09:00
committed by GitHub
parent 03db4c8096
commit f6682fe3ac

View File

@@ -177,10 +177,12 @@ export function ChatMessageDisplay({
const currentXml = xml || "" const currentXml = xml || ""
const convertedXml = convertToLegalXml(currentXml) const convertedXml = convertToLegalXml(currentXml)
if (convertedXml !== previousXML.current) { if (convertedXml !== previousXML.current) {
// If chartXML is empty, use the converted XML directly // If chartXML is empty, create a default mxfile structure to use with replaceNodes
const replacedXML = chartXML // This ensures the XML is properly wrapped in mxfile/diagram/mxGraphModel format
? replaceNodes(chartXML, convertedXml) const baseXML =
: convertedXml chartXML ||
`<mxfile><diagram name="Page-1" id="page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`
const replacedXML = replaceNodes(baseXML, convertedXml)
const validationError = validateMxCellStructure(replacedXML) const validationError = validateMxCellStructure(replacedXML)
if (!validationError) { if (!validationError) {