y-mxgraph 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/y-mxgraph.umd.js CHANGED
@@ -83,12 +83,24 @@
83
83
  return xmlElement;
84
84
  }
85
85
  function serialize$2(xmlElement) {
86
- const attributes = {
86
+ const rawAttributes = {
87
87
  ...xmlElement.getAttributes()
88
88
  };
89
89
  let mxGeometry = null;
90
- if (mxGeometryAttributeKey in attributes) {
91
- const mxGeometryString = attributes[mxGeometryAttributeKey];
90
+ let mxGeometryString;
91
+ if (mxGeometryAttributeKey in rawAttributes) {
92
+ mxGeometryString = rawAttributes[mxGeometryAttributeKey];
93
+ delete rawAttributes[mxGeometryAttributeKey];
94
+ }
95
+ const attributes = {};
96
+ for (const [key2, value] of Object.entries(rawAttributes)) {
97
+ if (typeof value === "string") {
98
+ attributes[key2] = value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
99
+ } else if (value != null) {
100
+ attributes[key2] = String(value);
101
+ }
102
+ }
103
+ if (mxGeometryString) {
92
104
  try {
93
105
  const parsed = xmlJs.xml2js(mxGeometryString, { compact: true });
94
106
  mxGeometry = parsed[mxGeometryKey] ?? null;
@@ -98,7 +110,6 @@
98
110
  } catch (e) {
99
111
  console.warn("[y-mxgraph] Failed to parse mxGeometry:", e);
100
112
  }
101
- delete attributes[mxGeometryAttributeKey];
102
113
  }
103
114
  const obj = {
104
115
  _attributes: attributes
@@ -187,11 +198,13 @@
187
198
  const diagramOrder = yMxFile.get(
188
199
  diagramOrderKey
189
200
  );
201
+ const orderIds = diagramOrder ? diagramOrder.toArray() : [];
202
+ const ids = orderIds.length > 0 ? orderIds : diagrams ? Array.from(diagrams.keys()) : [];
190
203
  const obj = {
191
204
  _attributes: {
192
205
  pages: yMxFile.get("pages") || "1"
193
206
  },
194
- [key$1]: diagramOrder.map((id) => diagrams.get(id)).map((diagramElement) => serialize(diagramElement))
207
+ [key$1]: ids.map((id) => diagrams.get(id)).filter((d) => !!d).map((diagramElement) => serialize(diagramElement))
195
208
  };
196
209
  return obj;
197
210
  }
@@ -257,6 +270,12 @@
257
270
  diagramOrderKey
258
271
  );
259
272
  ensureUniqueOrder(orderArr);
273
+ const currentOrder = orderArr.toArray();
274
+ if (currentOrder.length === 0 && diagramsMap && diagramsMap.size > 0) {
275
+ const allIds = Array.from(diagramsMap.keys());
276
+ orderArr.push(allIds);
277
+ }
278
+ ensureUniqueOrder(orderArr);
260
279
  const existingIds = orderArr.toArray();
261
280
  const existingIndex = /* @__PURE__ */ new Map();
262
281
  existingIds.forEach((id, idx) => existingIndex.set(id, idx));
@@ -476,7 +495,9 @@
476
495
  const mxfile = doc.getMap(key);
477
496
  const diagramsMap = mxfile.get(key$1);
478
497
  const orderArr = mxfile.get(diagramOrderKey);
479
- const diagramOrder = resetSnapshot ? [] : orderArr ? orderArr.toArray().slice() : [];
498
+ const orderIds = orderArr ? orderArr.toArray() : [];
499
+ const allDiagramIds = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
500
+ const diagramOrder = resetSnapshot ? [] : allDiagramIds.slice();
480
501
  const snap = {
481
502
  diagramOrder,
482
503
  cellsOrder: /* @__PURE__ */ new Map(),
@@ -549,7 +570,8 @@
549
570
  u.cells = u.cells || {};
550
571
  return u.cells;
551
572
  };
552
- const currDiagramOrder = orderArr.toArray();
573
+ const orderIds = orderArr.toArray();
574
+ const currDiagramOrder = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
553
575
  const diagramsList = currDiagramOrder.map((id) => diagramsMap.get(id)).filter((d) => !!d);
554
576
  const currCellsOrder = /* @__PURE__ */ new Map();
555
577
  const cellAttrMap = /* @__PURE__ */ new Map();
@@ -1434,16 +1456,29 @@
1434
1456
  class Binding {
1435
1457
  constructor(file, options) {
1436
1458
  this.suppressLocalApply = false;
1459
+ this.docInitialized = false;
1437
1460
  const { doc, awareness, undoManager, mouseMoveThrottle, cursor } = options;
1438
1461
  this.doc = doc;
1439
1462
  const ui = file.getUi();
1440
1463
  const graph = ui.editor.graph;
1441
1464
  this.mxGraphModel = graph.model;
1442
- const docHasData = doc.share.has(key);
1443
- if (!docHasData) {
1444
- xml2doc(file.data, doc);
1465
+ const mxfileMap = doc.getMap(key);
1466
+ const docHasData = mxfileMap.size > 0;
1467
+ this.docInitialized = docHasData;
1468
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1469
+ if (docHasData) {
1470
+ initDocSnapshot(doc, false);
1471
+ const fullPatch = generatePatch([], doc);
1472
+ if (Object.keys(fullPatch).length > 0) {
1473
+ this.suppressLocalApply = true;
1474
+ try {
1475
+ file.patch([fullPatch]);
1476
+ } finally {
1477
+ this.suppressLocalApply = false;
1478
+ }
1479
+ }
1480
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1445
1481
  }
1446
- initDocSnapshot(doc, docHasData);
1447
1482
  this.mxListener = () => {
1448
1483
  if (this.suppressLocalApply)
1449
1484
  return;
@@ -1451,16 +1486,31 @@
1451
1486
  file.shadowPages,
1452
1487
  file.ui.pages
1453
1488
  );
1489
+ const patchKeys = Object.keys(patch);
1490
+ if (patchKeys.length === 0)
1491
+ return;
1492
+ if (!this.docInitialized) {
1493
+ doc.transact(() => {
1494
+ xml2doc(file.data, doc);
1495
+ initDocSnapshot(doc, false);
1496
+ });
1497
+ this.docInitialized = true;
1498
+ }
1454
1499
  file.setShadowPages(file.ui.clonePages(file.ui.pages));
1455
1500
  applyFilePatch(doc, patch, { origin: LOCAL_ORIGIN });
1456
1501
  };
1457
1502
  this.mxGraphModel.addListener("change", this.mxListener);
1458
1503
  this.docObserver = (events, transaction) => {
1504
+ if (!this.docInitialized) {
1505
+ this.docInitialized = true;
1506
+ }
1459
1507
  if (transaction.local && transaction.origin === LOCAL_ORIGIN) {
1460
1508
  generatePatch(events);
1461
1509
  return;
1462
1510
  }
1463
1511
  const patch = generatePatch(events);
1512
+ if (Object.keys(patch).length === 0)
1513
+ return;
1464
1514
  this.suppressLocalApply = true;
1465
1515
  try {
1466
1516
  file.patch([patch]);
@@ -1469,19 +1519,7 @@
1469
1519
  this.suppressLocalApply = false;
1470
1520
  }
1471
1521
  };
1472
- doc.getMap(key).observeDeep(this.docObserver);
1473
- if (docHasData) {
1474
- const fullPatch = generatePatch([], doc);
1475
- if (Object.keys(fullPatch).length > 0) {
1476
- this.suppressLocalApply = true;
1477
- try {
1478
- file.patch([fullPatch]);
1479
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1480
- } finally {
1481
- this.suppressLocalApply = false;
1482
- }
1483
- }
1484
- }
1522
+ mxfileMap.observeDeep(this.docObserver);
1485
1523
  if (awareness) {
1486
1524
  this.cleanupCollaborator = bindCollaborator(file, {
1487
1525
  awareness,