y-mxgraph 0.1.6 → 0.2.0

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
@@ -1453,32 +1453,147 @@
1453
1453
  cleanupAwareness == null ? void 0 : cleanupAwareness();
1454
1454
  };
1455
1455
  }
1456
+ const defaultApplyFileData = (file, xml) => {
1457
+ file.ui.setFileData(xml);
1458
+ };
1459
+ function mergeFileIntoDoc(doc, fileXml, strategy) {
1460
+ let parsed;
1461
+ try {
1462
+ parsed = parse$4(fileXml);
1463
+ } catch (err) {
1464
+ console.warn(
1465
+ "[y-mxgraph] 合并失败,file XML 解析异常,回退到 replace:",
1466
+ err
1467
+ );
1468
+ return false;
1469
+ }
1470
+ const mxfileObj = parsed == null ? void 0 : parsed.mxfile;
1471
+ if (!mxfileObj || !Array.isArray(mxfileObj.diagram)) {
1472
+ console.warn(
1473
+ "[y-mxgraph] 合并失败,file XML 不是合法 mxfile,回退到 replace"
1474
+ );
1475
+ return false;
1476
+ }
1477
+ const mxfileMap = doc.getMap(key);
1478
+ const diagramMap = mxfileMap.get(key$1);
1479
+ const diagramOrder = mxfileMap.get(diagramOrderKey);
1480
+ if (!diagramMap || !diagramOrder) {
1481
+ console.warn("[y-mxgraph] 合并失败,doc 结构不完整,回退到 replace");
1482
+ return false;
1483
+ }
1484
+ doc.transact(() => {
1485
+ var _a;
1486
+ for (const diagram of mxfileObj.diagram) {
1487
+ const id = ((_a = diagram._attributes) == null ? void 0 : _a.id) || "";
1488
+ if (!id)
1489
+ continue;
1490
+ const docHas = diagramMap.has(id);
1491
+ if (docHas && strategy === "merge-remote") {
1492
+ continue;
1493
+ }
1494
+ const yDiagram = parse$1(
1495
+ diagram
1496
+ );
1497
+ diagramMap.set(id, yDiagram);
1498
+ if (!docHas) {
1499
+ diagramOrder.push([id]);
1500
+ }
1501
+ }
1502
+ });
1503
+ return true;
1504
+ }
1505
+ function reconcileInitialContent(doc, file, strategy, applyFileData) {
1506
+ const mxfileMap = doc.getMap(key);
1507
+ const docHasData = mxfileMap.size > 0;
1508
+ const fileHasAnyData = !!file.data;
1509
+ const fileHasDiagrams = fileHasAnyData && file.data.includes("<diagram");
1510
+ if (strategy === "replace") {
1511
+ if (docHasData) {
1512
+ const xml2 = doc2xml(doc);
1513
+ if (xml2 && xml2.includes("<diagram")) {
1514
+ applyFileData(file, xml2);
1515
+ } else if (!fileHasAnyData) {
1516
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1517
+ }
1518
+ } else if (!fileHasAnyData) {
1519
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1520
+ }
1521
+ return mxfileMap.size > 0;
1522
+ }
1523
+ if (!docHasData && !fileHasDiagrams) {
1524
+ if (!fileHasAnyData) {
1525
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1526
+ }
1527
+ return false;
1528
+ }
1529
+ if (!docHasData && fileHasDiagrams) {
1530
+ try {
1531
+ doc.transact(() => {
1532
+ xml2doc(file.data, doc);
1533
+ });
1534
+ return true;
1535
+ } catch (err) {
1536
+ console.warn(
1537
+ "[y-mxgraph] merge 模式下 xml2doc 失败,回退 replace:",
1538
+ err
1539
+ );
1540
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1541
+ return false;
1542
+ }
1543
+ }
1544
+ if (docHasData && !fileHasDiagrams) {
1545
+ const xml2 = doc2xml(doc);
1546
+ if (xml2 && xml2.includes("<diagram")) {
1547
+ applyFileData(file, xml2);
1548
+ } else if (!fileHasAnyData) {
1549
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1550
+ }
1551
+ return mxfileMap.size > 0;
1552
+ }
1553
+ const ok = mergeFileIntoDoc(doc, file.data, strategy);
1554
+ if (!ok) {
1555
+ const xml2 = doc2xml(doc);
1556
+ if (xml2 && xml2.includes("<diagram"))
1557
+ applyFileData(file, xml2);
1558
+ return mxfileMap.size > 0;
1559
+ }
1560
+ const xml = doc2xml(doc);
1561
+ if (xml && xml.includes("<diagram"))
1562
+ applyFileData(file, xml);
1563
+ return true;
1564
+ }
1456
1565
  class Binding {
1457
1566
  constructor(file, options) {
1458
1567
  this.suppressLocalApply = false;
1459
1568
  this.docInitialized = false;
1460
- const { doc, awareness, undoManager, mouseMoveThrottle, cursor } = options;
1569
+ const {
1570
+ doc,
1571
+ awareness,
1572
+ undoManager,
1573
+ mouseMoveThrottle,
1574
+ cursor,
1575
+ initialContent = "replace",
1576
+ applyFileData = defaultApplyFileData
1577
+ } = options;
1461
1578
  this.doc = doc;
1462
1579
  const ui = file.getUi();
1463
1580
  const graph = ui.editor.graph;
1464
1581
  this.mxGraphModel = graph.model;
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
- }
1582
+ this.suppressLocalApply = true;
1583
+ try {
1584
+ this.docInitialized = reconcileInitialContent(
1585
+ doc,
1586
+ file,
1587
+ initialContent,
1588
+ applyFileData
1589
+ );
1590
+ if (this.docInitialized) {
1591
+ initDocSnapshot(doc, false);
1479
1592
  }
1480
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1593
+ } finally {
1594
+ this.suppressLocalApply = false;
1481
1595
  }
1596
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1482
1597
  this.mxListener = () => {
1483
1598
  if (this.suppressLocalApply)
1484
1599
  return;
@@ -1519,7 +1634,7 @@
1519
1634
  this.suppressLocalApply = false;
1520
1635
  }
1521
1636
  };
1522
- mxfileMap.observeDeep(this.docObserver);
1637
+ doc.getMap(key).observeDeep(this.docObserver);
1523
1638
  if (awareness) {
1524
1639
  this.cleanupCollaborator = bindCollaborator(file, {
1525
1640
  awareness,