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.iife.js CHANGED
@@ -81,12 +81,24 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
81
81
  return xmlElement;
82
82
  }
83
83
  function serialize$2(xmlElement) {
84
- const attributes = {
84
+ const rawAttributes = {
85
85
  ...xmlElement.getAttributes()
86
86
  };
87
87
  let mxGeometry = null;
88
- if (mxGeometryAttributeKey in attributes) {
89
- const mxGeometryString = attributes[mxGeometryAttributeKey];
88
+ let mxGeometryString;
89
+ if (mxGeometryAttributeKey in rawAttributes) {
90
+ mxGeometryString = rawAttributes[mxGeometryAttributeKey];
91
+ delete rawAttributes[mxGeometryAttributeKey];
92
+ }
93
+ const attributes = {};
94
+ for (const [key2, value] of Object.entries(rawAttributes)) {
95
+ if (typeof value === "string") {
96
+ attributes[key2] = value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
97
+ } else if (value != null) {
98
+ attributes[key2] = String(value);
99
+ }
100
+ }
101
+ if (mxGeometryString) {
90
102
  try {
91
103
  const parsed = xmlJs.xml2js(mxGeometryString, { compact: true });
92
104
  mxGeometry = parsed[mxGeometryKey] ?? null;
@@ -96,7 +108,6 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
96
108
  } catch (e) {
97
109
  console.warn("[y-mxgraph] Failed to parse mxGeometry:", e);
98
110
  }
99
- delete attributes[mxGeometryAttributeKey];
100
111
  }
101
112
  const obj = {
102
113
  _attributes: attributes
@@ -185,11 +196,13 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
185
196
  const diagramOrder = yMxFile.get(
186
197
  diagramOrderKey
187
198
  );
199
+ const orderIds = diagramOrder ? diagramOrder.toArray() : [];
200
+ const ids = orderIds.length > 0 ? orderIds : diagrams ? Array.from(diagrams.keys()) : [];
188
201
  const obj = {
189
202
  _attributes: {
190
203
  pages: yMxFile.get("pages") || "1"
191
204
  },
192
- [key$1]: diagramOrder.map((id) => diagrams.get(id)).map((diagramElement) => serialize(diagramElement))
205
+ [key$1]: ids.map((id) => diagrams.get(id)).filter((d) => !!d).map((diagramElement) => serialize(diagramElement))
193
206
  };
194
207
  return obj;
195
208
  }
@@ -255,6 +268,12 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
255
268
  diagramOrderKey
256
269
  );
257
270
  ensureUniqueOrder(orderArr);
271
+ const currentOrder = orderArr.toArray();
272
+ if (currentOrder.length === 0 && diagramsMap && diagramsMap.size > 0) {
273
+ const allIds = Array.from(diagramsMap.keys());
274
+ orderArr.push(allIds);
275
+ }
276
+ ensureUniqueOrder(orderArr);
258
277
  const existingIds = orderArr.toArray();
259
278
  const existingIndex = /* @__PURE__ */ new Map();
260
279
  existingIds.forEach((id, idx) => existingIndex.set(id, idx));
@@ -474,7 +493,9 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
474
493
  const mxfile = doc.getMap(key);
475
494
  const diagramsMap = mxfile.get(key$1);
476
495
  const orderArr = mxfile.get(diagramOrderKey);
477
- const diagramOrder = resetSnapshot ? [] : orderArr ? orderArr.toArray().slice() : [];
496
+ const orderIds = orderArr ? orderArr.toArray() : [];
497
+ const allDiagramIds = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
498
+ const diagramOrder = resetSnapshot ? [] : allDiagramIds.slice();
478
499
  const snap = {
479
500
  diagramOrder,
480
501
  cellsOrder: /* @__PURE__ */ new Map(),
@@ -547,7 +568,8 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
547
568
  u.cells = u.cells || {};
548
569
  return u.cells;
549
570
  };
550
- const currDiagramOrder = orderArr.toArray();
571
+ const orderIds = orderArr.toArray();
572
+ const currDiagramOrder = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
551
573
  const diagramsList = currDiagramOrder.map((id) => diagramsMap.get(id)).filter((d) => !!d);
552
574
  const currCellsOrder = /* @__PURE__ */ new Map();
553
575
  const cellAttrMap = /* @__PURE__ */ new Map();
@@ -1432,16 +1454,29 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1432
1454
  class Binding {
1433
1455
  constructor(file, options) {
1434
1456
  this.suppressLocalApply = false;
1457
+ this.docInitialized = false;
1435
1458
  const { doc, awareness, undoManager, mouseMoveThrottle, cursor } = options;
1436
1459
  this.doc = doc;
1437
1460
  const ui = file.getUi();
1438
1461
  const graph = ui.editor.graph;
1439
1462
  this.mxGraphModel = graph.model;
1440
- const docHasData = doc.share.has(key);
1441
- if (!docHasData) {
1442
- xml2doc(file.data, doc);
1463
+ const mxfileMap = doc.getMap(key);
1464
+ const docHasData = mxfileMap.size > 0;
1465
+ this.docInitialized = docHasData;
1466
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1467
+ if (docHasData) {
1468
+ initDocSnapshot(doc, false);
1469
+ const fullPatch = generatePatch([], doc);
1470
+ if (Object.keys(fullPatch).length > 0) {
1471
+ this.suppressLocalApply = true;
1472
+ try {
1473
+ file.patch([fullPatch]);
1474
+ } finally {
1475
+ this.suppressLocalApply = false;
1476
+ }
1477
+ }
1478
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1443
1479
  }
1444
- initDocSnapshot(doc, docHasData);
1445
1480
  this.mxListener = () => {
1446
1481
  if (this.suppressLocalApply)
1447
1482
  return;
@@ -1449,16 +1484,31 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1449
1484
  file.shadowPages,
1450
1485
  file.ui.pages
1451
1486
  );
1487
+ const patchKeys = Object.keys(patch);
1488
+ if (patchKeys.length === 0)
1489
+ return;
1490
+ if (!this.docInitialized) {
1491
+ doc.transact(() => {
1492
+ xml2doc(file.data, doc);
1493
+ initDocSnapshot(doc, false);
1494
+ });
1495
+ this.docInitialized = true;
1496
+ }
1452
1497
  file.setShadowPages(file.ui.clonePages(file.ui.pages));
1453
1498
  applyFilePatch(doc, patch, { origin: LOCAL_ORIGIN });
1454
1499
  };
1455
1500
  this.mxGraphModel.addListener("change", this.mxListener);
1456
1501
  this.docObserver = (events, transaction) => {
1502
+ if (!this.docInitialized) {
1503
+ this.docInitialized = true;
1504
+ }
1457
1505
  if (transaction.local && transaction.origin === LOCAL_ORIGIN) {
1458
1506
  generatePatch(events);
1459
1507
  return;
1460
1508
  }
1461
1509
  const patch = generatePatch(events);
1510
+ if (Object.keys(patch).length === 0)
1511
+ return;
1462
1512
  this.suppressLocalApply = true;
1463
1513
  try {
1464
1514
  file.patch([patch]);
@@ -1467,19 +1517,7 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1467
1517
  this.suppressLocalApply = false;
1468
1518
  }
1469
1519
  };
1470
- doc.getMap(key).observeDeep(this.docObserver);
1471
- if (docHasData) {
1472
- const fullPatch = generatePatch([], doc);
1473
- if (Object.keys(fullPatch).length > 0) {
1474
- this.suppressLocalApply = true;
1475
- try {
1476
- file.patch([fullPatch]);
1477
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1478
- } finally {
1479
- this.suppressLocalApply = false;
1480
- }
1481
- }
1482
- }
1520
+ mxfileMap.observeDeep(this.docObserver);
1483
1521
  if (awareness) {
1484
1522
  this.cleanupCollaborator = bindCollaborator(file, {
1485
1523
  awareness,