y-mxgraph 0.1.3 → 0.1.5

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.es.js CHANGED
@@ -66,12 +66,24 @@ function parse$3(object) {
66
66
  return xmlElement;
67
67
  }
68
68
  function serialize$2(xmlElement) {
69
- const attributes = {
69
+ const rawAttributes = {
70
70
  ...xmlElement.getAttributes()
71
71
  };
72
72
  let mxGeometry = null;
73
- if (mxGeometryAttributeKey in attributes) {
74
- const mxGeometryString = attributes[mxGeometryAttributeKey];
73
+ let mxGeometryString;
74
+ if (mxGeometryAttributeKey in rawAttributes) {
75
+ mxGeometryString = rawAttributes[mxGeometryAttributeKey];
76
+ delete rawAttributes[mxGeometryAttributeKey];
77
+ }
78
+ const attributes = {};
79
+ for (const [key2, value] of Object.entries(rawAttributes)) {
80
+ if (typeof value === "string") {
81
+ attributes[key2] = value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
82
+ } else if (value != null) {
83
+ attributes[key2] = String(value);
84
+ }
85
+ }
86
+ if (mxGeometryString) {
75
87
  try {
76
88
  const parsed = xml2js(mxGeometryString, { compact: true });
77
89
  mxGeometry = parsed[mxGeometryKey] ?? null;
@@ -81,7 +93,6 @@ function serialize$2(xmlElement) {
81
93
  } catch (e) {
82
94
  console.warn("[y-mxgraph] Failed to parse mxGeometry:", e);
83
95
  }
84
- delete attributes[mxGeometryAttributeKey];
85
96
  }
86
97
  const obj = {
87
98
  _attributes: attributes
@@ -170,11 +181,13 @@ function serializer(yMxFile) {
170
181
  const diagramOrder = yMxFile.get(
171
182
  diagramOrderKey
172
183
  );
184
+ const orderIds = diagramOrder ? diagramOrder.toArray() : [];
185
+ const ids = orderIds.length > 0 ? orderIds : diagrams ? Array.from(diagrams.keys()) : [];
173
186
  const obj = {
174
187
  _attributes: {
175
188
  pages: yMxFile.get("pages") || "1"
176
189
  },
177
- [key$1]: diagramOrder.map((id) => diagrams.get(id)).map((diagramElement) => serialize(diagramElement))
190
+ [key$1]: ids.map((id) => diagrams.get(id)).filter((d) => !!d).map((diagramElement) => serialize(diagramElement))
178
191
  };
179
192
  return obj;
180
193
  }
@@ -240,6 +253,12 @@ function applyFilePatch(doc, patch, options) {
240
253
  diagramOrderKey
241
254
  );
242
255
  ensureUniqueOrder(orderArr);
256
+ const currentOrder = orderArr.toArray();
257
+ if (currentOrder.length === 0 && diagramsMap && diagramsMap.size > 0) {
258
+ const allIds = Array.from(diagramsMap.keys());
259
+ orderArr.push(allIds);
260
+ }
261
+ ensureUniqueOrder(orderArr);
243
262
  const existingIds = orderArr.toArray();
244
263
  const existingIndex = /* @__PURE__ */ new Map();
245
264
  existingIds.forEach((id, idx) => existingIndex.set(id, idx));
@@ -459,7 +478,9 @@ function initDocSnapshot(doc, resetSnapshot = false) {
459
478
  const mxfile = doc.getMap(key);
460
479
  const diagramsMap = mxfile.get(key$1);
461
480
  const orderArr = mxfile.get(diagramOrderKey);
462
- const diagramOrder = resetSnapshot ? [] : orderArr ? orderArr.toArray().slice() : [];
481
+ const orderIds = orderArr ? orderArr.toArray() : [];
482
+ const allDiagramIds = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
483
+ const diagramOrder = resetSnapshot ? [] : allDiagramIds.slice();
463
484
  const snap = {
464
485
  diagramOrder,
465
486
  cellsOrder: /* @__PURE__ */ new Map(),
@@ -532,7 +553,8 @@ function generatePatch(events, explicitDoc) {
532
553
  u.cells = u.cells || {};
533
554
  return u.cells;
534
555
  };
535
- const currDiagramOrder = orderArr.toArray();
556
+ const orderIds = orderArr.toArray();
557
+ const currDiagramOrder = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
536
558
  const diagramsList = currDiagramOrder.map((id) => diagramsMap.get(id)).filter((d) => !!d);
537
559
  const currCellsOrder = /* @__PURE__ */ new Map();
538
560
  const cellAttrMap = /* @__PURE__ */ new Map();
@@ -1417,16 +1439,29 @@ function bindCollaborator(file, options) {
1417
1439
  class Binding {
1418
1440
  constructor(file, options) {
1419
1441
  this.suppressLocalApply = false;
1442
+ this.docInitialized = false;
1420
1443
  const { doc, awareness, undoManager, mouseMoveThrottle, cursor } = options;
1421
1444
  this.doc = doc;
1422
1445
  const ui = file.getUi();
1423
1446
  const graph = ui.editor.graph;
1424
1447
  this.mxGraphModel = graph.model;
1425
- const docHasData = doc.share.has(key);
1426
- if (!docHasData) {
1427
- xml2doc(file.data, doc);
1448
+ const mxfileMap = doc.getMap(key);
1449
+ const docHasData = mxfileMap.size > 0;
1450
+ this.docInitialized = docHasData;
1451
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1452
+ if (docHasData) {
1453
+ initDocSnapshot(doc, false);
1454
+ const fullPatch = generatePatch([], doc);
1455
+ if (Object.keys(fullPatch).length > 0) {
1456
+ this.suppressLocalApply = true;
1457
+ try {
1458
+ file.patch([fullPatch]);
1459
+ } finally {
1460
+ this.suppressLocalApply = false;
1461
+ }
1462
+ }
1463
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1428
1464
  }
1429
- initDocSnapshot(doc, docHasData);
1430
1465
  this.mxListener = () => {
1431
1466
  if (this.suppressLocalApply)
1432
1467
  return;
@@ -1434,16 +1469,31 @@ class Binding {
1434
1469
  file.shadowPages,
1435
1470
  file.ui.pages
1436
1471
  );
1472
+ const patchKeys = Object.keys(patch);
1473
+ if (patchKeys.length === 0)
1474
+ return;
1475
+ if (!this.docInitialized) {
1476
+ doc.transact(() => {
1477
+ xml2doc(file.data, doc);
1478
+ initDocSnapshot(doc, false);
1479
+ });
1480
+ this.docInitialized = true;
1481
+ }
1437
1482
  file.setShadowPages(file.ui.clonePages(file.ui.pages));
1438
1483
  applyFilePatch(doc, patch, { origin: LOCAL_ORIGIN });
1439
1484
  };
1440
1485
  this.mxGraphModel.addListener("change", this.mxListener);
1441
1486
  this.docObserver = (events, transaction) => {
1487
+ if (!this.docInitialized) {
1488
+ this.docInitialized = true;
1489
+ }
1442
1490
  if (transaction.local && transaction.origin === LOCAL_ORIGIN) {
1443
1491
  generatePatch(events);
1444
1492
  return;
1445
1493
  }
1446
1494
  const patch = generatePatch(events);
1495
+ if (Object.keys(patch).length === 0)
1496
+ return;
1447
1497
  this.suppressLocalApply = true;
1448
1498
  try {
1449
1499
  file.patch([patch]);
@@ -1452,19 +1502,7 @@ class Binding {
1452
1502
  this.suppressLocalApply = false;
1453
1503
  }
1454
1504
  };
1455
- doc.getMap(key).observeDeep(this.docObserver);
1456
- if (docHasData) {
1457
- const fullPatch = generatePatch([], doc);
1458
- if (Object.keys(fullPatch).length > 0) {
1459
- this.suppressLocalApply = true;
1460
- try {
1461
- file.patch([fullPatch]);
1462
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1463
- } finally {
1464
- this.suppressLocalApply = false;
1465
- }
1466
- }
1467
- }
1505
+ mxfileMap.observeDeep(this.docObserver);
1468
1506
  if (awareness) {
1469
1507
  this.cleanupCollaborator = bindCollaborator(file, {
1470
1508
  awareness,