y-mxgraph 0.8.1 → 0.8.3

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.js CHANGED
@@ -1,6 +1,54 @@
1
- import { k as key, a as key$1, d as diagramOrderKey, b as key$2, m as mxCellOrderKey, c as key$3, p as parse, e as parse$1, f as serializer, s as serialize, x as xml2ydoc, y as ydoc2xml } from "./index-BtfQrQYT.js";
1
+ import { g as getMap, b as backgroundKey, k as key, a as key$1, c as key$2, d as diagramOrderKey, m as mxCellOrderKey, e as key$3, p as parse, f as parse$1, h as serializer, s as serialize, x as xml2ydoc, y as ydoc2xml } from "./index-BhW4J2Zt.js";
2
2
  import * as Y from "yjs";
3
3
  import { colord } from "colord";
4
+ function patchValueToXmlAttr(value) {
5
+ try {
6
+ const parsed = JSON.parse(value);
7
+ if (typeof parsed === "string") return parsed;
8
+ } catch (e) {
9
+ }
10
+ return value;
11
+ }
12
+ function xmlAttrToPatchValue(value) {
13
+ return JSON.stringify(value);
14
+ }
15
+ function getBackground(diagram) {
16
+ const gm = getMap(diagram, key);
17
+ if (!gm) return void 0;
18
+ const bg = gm.get(backgroundKey);
19
+ return bg || void 0;
20
+ }
21
+ function setBackground(diagram, background) {
22
+ const gm = getMap(diagram, key);
23
+ if (!gm) return;
24
+ if (background != null && background !== "") {
25
+ gm.set(backgroundKey, background);
26
+ } else {
27
+ gm.delete(backgroundKey);
28
+ }
29
+ }
30
+ function diffBackgroundViewPatch(prev, curr) {
31
+ const pv = prev != null ? prev : "";
32
+ const cv = curr != null ? curr : "";
33
+ if (pv === cv) return void 0;
34
+ return {
35
+ background: cv === "" ? '""' : xmlAttrToPatchValue(cv)
36
+ };
37
+ }
38
+ function applyViewPatch(diagram, viewPatch) {
39
+ if (!("background" in viewPatch)) return;
40
+ const raw = viewPatch.background;
41
+ if (raw === null || raw === void 0) {
42
+ setBackground(diagram, void 0);
43
+ return;
44
+ }
45
+ const str = String(raw);
46
+ if (str === "" || str === '""' || str === "null") {
47
+ setBackground(diagram, void 0);
48
+ } else {
49
+ setBackground(diagram, patchValueToXmlAttr(str));
50
+ }
51
+ }
4
52
  var __defProp$1 = Object.defineProperty;
5
53
  var __defProps = Object.defineProperties;
6
54
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -53,11 +101,39 @@ function ensureUniqueOrder(orderArr) {
53
101
  dupIdx.sort((a, b) => b - a).forEach((idx) => orderArr.delete(idx, 1));
54
102
  }
55
103
  }
104
+ function pruneEmptyPatch(patch) {
105
+ var _a, _b;
106
+ if (!patch[DIFF_UPDATE]) return patch;
107
+ for (const id of Object.keys(patch[DIFF_UPDATE])) {
108
+ const u = patch[DIFF_UPDATE][id];
109
+ const cells = u.cells;
110
+ if (cells == null ? void 0 : cells[DIFF_UPDATE]) {
111
+ for (const cid of Object.keys(cells[DIFF_UPDATE])) {
112
+ if (Object.keys(cells[DIFF_UPDATE][cid]).length === 0) {
113
+ delete cells[DIFF_UPDATE][cid];
114
+ }
115
+ }
116
+ if (Object.keys(cells[DIFF_UPDATE]).length === 0) {
117
+ delete cells[DIFF_UPDATE];
118
+ }
119
+ }
120
+ if (cells && !((_a = cells[DIFF_REMOVE]) == null ? void 0 : _a.length) && !((_b = cells[DIFF_INSERT]) == null ? void 0 : _b.length) && (!cells[DIFF_UPDATE] || Object.keys(cells[DIFF_UPDATE]).length === 0)) {
121
+ delete u.cells;
122
+ }
123
+ if (Object.keys(u).length === 0) {
124
+ delete patch[DIFF_UPDATE][id];
125
+ }
126
+ }
127
+ if (Object.keys(patch[DIFF_UPDATE]).length === 0) {
128
+ delete patch[DIFF_UPDATE];
129
+ }
130
+ return patch;
131
+ }
56
132
  function applyFilePatch(doc, patch, options) {
57
133
  doc.transact(() => {
58
- const mxfile = doc.getMap(key);
134
+ const mxfile = doc.getMap(key$1);
59
135
  if (patch[DIFF_REMOVE]) {
60
- const diagramsMap = mxfile.get(key$1);
136
+ const diagramsMap = mxfile.get(key$2);
61
137
  const orderArr = mxfile.get(
62
138
  diagramOrderKey
63
139
  );
@@ -71,7 +147,7 @@ function applyFilePatch(doc, patch, options) {
71
147
  }
72
148
  }
73
149
  if (patch[DIFF_INSERT]) {
74
- const diagramsMap = mxfile.get(key$1);
150
+ const diagramsMap = mxfile.get(key$2);
75
151
  const orderArr = mxfile.get(
76
152
  diagramOrderKey
77
153
  );
@@ -142,7 +218,7 @@ function applyFilePatch(doc, patch, options) {
142
218
  if (patch[DIFF_UPDATE]) {
143
219
  Object.keys(patch[DIFF_UPDATE]).forEach((id) => {
144
220
  const diagramsMap = mxfile.get(
145
- key$1
221
+ key$2
146
222
  );
147
223
  const diagram = diagramsMap.get(id);
148
224
  if (diagram) {
@@ -153,8 +229,11 @@ function applyFilePatch(doc, patch, options) {
153
229
  update.name || ""
154
230
  );
155
231
  }
232
+ if (update.view && typeof update.view === "object") {
233
+ applyViewPatch(diagram, update.view);
234
+ }
156
235
  if (update.cells) {
157
- const yMxGraphModel = diagram.get(key$2);
236
+ const yMxGraphModel = diagram.get(key);
158
237
  if (!yMxGraphModel) return;
159
238
  const cellsMap = yMxGraphModel.get(key$3);
160
239
  const orderArr = yMxGraphModel.get(mxCellOrderKey);
@@ -289,8 +368,8 @@ function applyFilePatch(doc, patch, options) {
289
368
  }
290
369
  function initDocSnapshot(doc, resetSnapshot = false) {
291
370
  try {
292
- const mxfile = doc.getMap(key);
293
- const diagramsMap = mxfile.get(key$1);
371
+ const mxfile = doc.getMap(key$1);
372
+ const diagramsMap = mxfile.get(key$2);
294
373
  const orderArr = mxfile.get(diagramOrderKey);
295
374
  const orderIds = orderArr ? orderArr.toArray() : [];
296
375
  const allDiagramIds = orderIds.length > 0 ? orderIds : diagramsMap ? Array.from(diagramsMap.keys()) : [];
@@ -298,13 +377,14 @@ function initDocSnapshot(doc, resetSnapshot = false) {
298
377
  const snap = {
299
378
  diagramOrder,
300
379
  cellsOrder: /* @__PURE__ */ new Map(),
301
- cellAttrs: /* @__PURE__ */ new Map()
380
+ cellAttrs: /* @__PURE__ */ new Map(),
381
+ diagramBackground: /* @__PURE__ */ new Map()
302
382
  };
303
383
  const diagrams = diagramOrder.map((id) => diagramsMap.get(id)).filter((d) => !!d);
304
384
  for (const d of diagrams) {
305
385
  const did = d.get("id") || "";
306
386
  if (!did) continue;
307
- const gm = d.get(key$2);
387
+ const gm = d.get(key);
308
388
  if (gm) {
309
389
  const order = gm.get(mxCellOrderKey);
310
390
  const ids = order ? order.toArray().slice() : [];
@@ -327,6 +407,7 @@ function initDocSnapshot(doc, resetSnapshot = false) {
327
407
  snap.cellsOrder.set(did, []);
328
408
  snap.cellAttrs.set(did, /* @__PURE__ */ new Map());
329
409
  }
410
+ snap.diagramBackground.set(did, getBackground(d));
330
411
  }
331
412
  docSnapshots.set(doc, snap);
332
413
  } catch (e) {
@@ -339,21 +420,23 @@ function generatePatch(events, explicitDoc) {
339
420
  const doc = (_b = (_a = events[0]) == null ? void 0 : _a.transaction) == null ? void 0 : _b.doc;
340
421
  if (!doc) return patch;
341
422
  if (!events || events.length === 0) return patch;
342
- const mxfile = doc.getMap(key);
343
- const diagramsMap = mxfile.get(key$1);
423
+ const mxfile = doc.getMap(key$1);
424
+ const diagramsMap = mxfile.get(key$2);
344
425
  const orderArr = mxfile.get(diagramOrderKey);
345
426
  let snap = docSnapshots.get(doc);
346
427
  if (!snap) {
347
428
  snap = {
348
429
  diagramOrder: null,
349
430
  cellsOrder: /* @__PURE__ */ new Map(),
350
- cellAttrs: /* @__PURE__ */ new Map()
431
+ cellAttrs: /* @__PURE__ */ new Map(),
432
+ diagramBackground: /* @__PURE__ */ new Map()
351
433
  };
352
434
  docSnapshots.set(doc, snap);
353
435
  }
354
436
  const prevDiagramOrder = snap.diagramOrder;
355
437
  const prevCellsOrder = snap.cellsOrder;
356
438
  const prevCellsAttrs = snap.cellAttrs;
439
+ const prevDiagramBackground = snap.diagramBackground;
357
440
  const ensureUpdate = (diagramId) => {
358
441
  patch[DIFF_UPDATE] = patch[DIFF_UPDATE] || {};
359
442
  patch[DIFF_UPDATE][diagramId] = patch[DIFF_UPDATE][diagramId] || {};
@@ -369,10 +452,12 @@ function generatePatch(events, explicitDoc) {
369
452
  const diagramsList = currDiagramOrder.map((id) => diagramsMap.get(id)).filter((d) => !!d);
370
453
  const currCellsOrder = /* @__PURE__ */ new Map();
371
454
  const cellAttrMap = /* @__PURE__ */ new Map();
455
+ const currDiagramBackground = /* @__PURE__ */ new Map();
372
456
  for (const d of diagramsList) {
373
457
  const did = d.get("id") || "";
458
+ currDiagramBackground.set(did, getBackground(d));
374
459
  const attrs = /* @__PURE__ */ new Map();
375
- const gm = d.get(key$2);
460
+ const gm = d.get(key);
376
461
  if (gm) {
377
462
  const cellsMap = gm.get(key$3);
378
463
  const orderArr2 = gm.get(mxCellOrderKey);
@@ -492,19 +577,35 @@ function generatePatch(events, explicitDoc) {
492
577
  if (!(target instanceof Y.Map)) continue;
493
578
  if (!diagramSet.has(target)) continue;
494
579
  const changed = ev.keysChanged || /* @__PURE__ */ new Set();
495
- if (!changed || !changed.has("name")) continue;
580
+ if (!(changed == null ? void 0 : changed.has("name"))) continue;
496
581
  const did = target.get("id") || "";
497
582
  if (!did || insertedDiagramIdGlobal.has(did)) continue;
498
- const u = ensureUpdate(did);
499
- u.name = target.get("name") || "";
583
+ ensureUpdate(did).name = target.get("name") || "";
500
584
  }
501
585
  }
502
586
  if (!prevDiagramOrder) {
503
587
  for (const d of diagramsList) {
504
588
  const did = d.get("id") || "";
505
589
  if (!did) continue;
506
- const u = ensureUpdate(did);
507
- u.name = d.get("name") || "";
590
+ ensureUpdate(did).name = d.get("name") || "";
591
+ }
592
+ }
593
+ for (const ev of events) {
594
+ const target = ev.target;
595
+ if (!(target instanceof Y.Map)) continue;
596
+ for (const d of diagramsList) {
597
+ const gm = d.get(key);
598
+ if (gm !== target) continue;
599
+ const changed = ev.keysChanged || /* @__PURE__ */ new Set();
600
+ if (!changed.has(backgroundKey)) continue;
601
+ const did = d.get("id") || "";
602
+ if (!did || insertedDiagramIdGlobal.has(did)) continue;
603
+ const viewDiff = diffBackgroundViewPatch(
604
+ prevDiagramBackground.get(did),
605
+ getBackground(d)
606
+ );
607
+ if (viewDiff) ensureUpdate(did).view = viewDiff;
608
+ break;
508
609
  }
509
610
  }
510
611
  for (const ev of events) {
@@ -533,6 +634,14 @@ function generatePatch(events, explicitDoc) {
533
634
  }
534
635
  }
535
636
  if (prevDiagramOrder) {
637
+ for (const [did, currBg] of currDiagramBackground.entries()) {
638
+ if (insertedDiagramIdGlobal.has(did)) continue;
639
+ const viewDiff = diffBackgroundViewPatch(
640
+ prevDiagramBackground.get(did),
641
+ currBg
642
+ );
643
+ if (viewDiff) ensureUpdate(did).view = viewDiff;
644
+ }
536
645
  for (const [did, currAttrsMap] of cellAttrMap.entries()) {
537
646
  const prevAttrsMap = prevCellsAttrs.get(did) || /* @__PURE__ */ new Map();
538
647
  const cellsPatch = ensureCellSection(did);
@@ -580,8 +689,12 @@ function generatePatch(events, explicitDoc) {
580
689
  }
581
690
  snap.cellsOrder = newCellsOrder;
582
691
  snap.cellAttrs = newCellsAttrs;
692
+ snap.diagramBackground = /* @__PURE__ */ new Map();
693
+ for (const [did, bg] of currDiagramBackground.entries()) {
694
+ snap.diagramBackground.set(did, bg);
695
+ }
583
696
  docSnapshots.set(doc, snap);
584
- return patch;
697
+ return pruneEmptyPatch(patch);
585
698
  }
586
699
  const LOCAL_ORIGIN = {};
587
700
  function createMxEventObject(name, props) {
@@ -1254,8 +1367,8 @@ function mergeFileIntoDoc(doc, fileXml, strategy) {
1254
1367
  );
1255
1368
  return false;
1256
1369
  }
1257
- const mxfileMap = doc.getMap(key);
1258
- const diagramMap = mxfileMap.get(key$1);
1370
+ const mxfileMap = doc.getMap(key$1);
1371
+ const diagramMap = mxfileMap.get(key$2);
1259
1372
  const diagramOrder = mxfileMap.get(diagramOrderKey);
1260
1373
  if (!diagramMap || !diagramOrder) {
1261
1374
  console.warn("[y-mxgraph] 合并失败,doc 结构不完整,回退到 replace");
@@ -1282,7 +1395,7 @@ function mergeFileIntoDoc(doc, fileXml, strategy) {
1282
1395
  return true;
1283
1396
  }
1284
1397
  function reconcileInitialContent(doc, file, strategy, applyFileData) {
1285
- const mxfileMap = doc.getMap(key);
1398
+ const mxfileMap = doc.getMap(key$1);
1286
1399
  const docHasData = mxfileMap.size > 0;
1287
1400
  const fileHasAnyData = !!file.data;
1288
1401
  const fileHasDiagrams = fileHasAnyData && file.data.includes("<diagram");
@@ -1413,8 +1526,8 @@ class Binding {
1413
1526
  return;
1414
1527
  }
1415
1528
  if (this.shouldReplaceWhenDocHasData && !transaction.local) {
1416
- const mxfileMap = doc.getMap(key);
1417
- const diagramMap = mxfileMap.get(key$1);
1529
+ const mxfileMap = doc.getMap(key$1);
1530
+ const diagramMap = mxfileMap.get(key$2);
1418
1531
  if (diagramMap && diagramMap.size > 0) {
1419
1532
  const xml = ydoc2xml(doc);
1420
1533
  if (xml && xml.includes("<diagram")) {
@@ -1446,7 +1559,7 @@ class Binding {
1446
1559
  this.suppressLocalApply = false;
1447
1560
  }
1448
1561
  };
1449
- doc.getMap(key).observeDeep(this.docObserver);
1562
+ doc.getMap(key$1).observeDeep(this.docObserver);
1450
1563
  if (awareness) {
1451
1564
  this.cleanupCollaborator = bindCollaborator(file, {
1452
1565
  awareness,
@@ -1481,7 +1594,7 @@ class Binding {
1481
1594
  destroy(deep = false) {
1482
1595
  var _a, _b;
1483
1596
  this.mxGraphModel.removeListener("change", this.mxListener);
1484
- this.doc.getMap(key).unobserveDeep(this.docObserver);
1597
+ this.doc.getMap(key$1).unobserveDeep(this.docObserver);
1485
1598
  if (deep) {
1486
1599
  (_a = this.cleanupCollaborator) == null ? void 0 : _a.call(this);
1487
1600
  (_b = this.cleanupUndoManager) == null ? void 0 : _b.call(this);