diff options
author | Sunil Shetye | 2025-06-13 11:33:28 +0530 |
---|---|---|
committer | Sunil Shetye | 2025-06-13 12:09:38 +0530 |
commit | 088a6d54e5047f8cd4b8bd197ae12d7ab6dbcd00 (patch) | |
tree | 9a9704de0f6b9f421bd6a7499dd8578ca8d685a7 | |
parent | c98afc7def98a7d5be5a7bc024c928838d80fbf9 (diff) | |
download | Common-Interface-Project-088a6d54e5047f8cd4b8bd197ae12d7ab6dbcd00.tar.gz Common-Interface-Project-088a6d54e5047f8cd4b8bd197ae12d7ab6dbcd00.tar.bz2 Common-Interface-Project-088a6d54e5047f8cd4b8bd197ae12d7ab6dbcd00.zip |
updateMxGraphXML to update ids of elements
3 files changed, 54 insertions, 3 deletions
diff --git a/blocks/eda-frontend/src/components/SchematicEditor/Helper/SvgParser.js b/blocks/eda-frontend/src/components/SchematicEditor/Helper/SvgParser.js index 44734d6c..3fde9af5 100644 --- a/blocks/eda-frontend/src/components/SchematicEditor/Helper/SvgParser.js +++ b/blocks/eda-frontend/src/components/SchematicEditor/Helper/SvgParser.js @@ -4,6 +4,7 @@ import 'mxgraph/javascript/src/css/common.css' import mxGraphFactory from 'mxgraph' import { getRotationParameters, getPins, getPointXY, getXYPos, getSuperBlockDiagram } from './ToolbarTools' +import { updateMxGraphXML } from '../../../utils/GalleryUtils' const { mxPoint @@ -56,7 +57,7 @@ export function getSvgMetadata (graph, parent, evt, target, x, y, component) { v1.controlPorts = 0 v1.commandPorts = 0 v1.simulationFunction = component.simulation_function - v1.SuperBlockDiagram = getSuperBlockDiagram(component.super_block) + v1.SuperBlockDiagram = getSuperBlockDiagram(updateMxGraphXML(component.super_block)) v1.pins = { explicitInputPorts: [], implicitInputPorts: [], diff --git a/blocks/eda-frontend/src/components/SchematicEditor/Helper/ToolbarTools.js b/blocks/eda-frontend/src/components/SchematicEditor/Helper/ToolbarTools.js index 3378a7e2..7ea0e5bf 100644 --- a/blocks/eda-frontend/src/components/SchematicEditor/Helper/ToolbarTools.js +++ b/blocks/eda-frontend/src/components/SchematicEditor/Helper/ToolbarTools.js @@ -359,8 +359,8 @@ export function getXYPos (rotationParameters, xPos, yPos) { export function getSuperBlockDiagram (xml) { xml = '<SuperBlockDiagram as="child" background="-1" title="">' + xml + '</SuperBlockDiagram>' - const updatedDOM = mxUtils.parseXml(xml) - return updatedDOM.getElementsByTagName('SuperBlockDiagram')[0] + const superBlockDiagram = mxUtils.parseXml(xml) + return superBlockDiagram.getElementsByTagName('SuperBlockDiagram')[0] } function parseXmlToGraph (xmlDoc, graph) { diff --git a/blocks/eda-frontend/src/utils/GalleryUtils.js b/blocks/eda-frontend/src/utils/GalleryUtils.js index 411e3448..de7f24a3 100644 --- a/blocks/eda-frontend/src/utils/GalleryUtils.js +++ b/blocks/eda-frontend/src/utils/GalleryUtils.js @@ -207,3 +207,53 @@ export const removeBySaveIdInPlace = (schematics, saveId) => { export const sanitizeTitle = (title, replacement = '_') => { return title.replace(/[<>:"/\\|?* ]/g, replacement).trim() } + +export const generate_ids = (() => { + let prefixCounter = 0 + + const generate = (count) => { + const prefix = prefixCounter.toString().padStart(9, '0') + const ids = Array.from({ length: count }, (_, i) => { + const hex = i.toString(16).padStart(4, '0') + return `${prefix}:${hex}` + }) + prefixCounter++ + return ids + } + + generate.reset = () => { + prefixCounter = 0 + } + + return generate +})() + +export const updateMxGraphXML = (xmlString) => { + const xmlDoc = new DOMParser().parseFromString(xmlString, 'application/xml') + + const idMap = new Map() + + // Step 1: Generate new IDs for all mxCell elements + const cells = xmlDoc.querySelectorAll('mxCell[id]') + const ids = generate_ids(cells.length) + let count = 0 + cells.forEach(cell => { + const oldId = cell.getAttribute('id') + const newId = ids[count++] + idMap.set(oldId, newId) + cell.setAttribute('id', newId) + }) + + // Step 2: update ParentComponent and other references as needed + cells.forEach(cell => { + ['ParentComponent', 'sourceVertex', 'targetVertex'].forEach(attr => { + const val = cell.getAttribute(attr) + if (val && idMap.has(val)) { + cell.setAttribute(attr, idMap.get(val)) + } + }) + }) + + // Step 3: Serialize back to XML + return new XMLSerializer().serializeToString(xmlDoc) +} |