y-mxgraph 0.1.6 → 0.2.1

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
@@ -1451,32 +1451,148 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1451
1451
  cleanupAwareness == null ? void 0 : cleanupAwareness();
1452
1452
  };
1453
1453
  }
1454
+ const defaultApplyFileData = (file, xml) => {
1455
+ file.ui.setFileData(xml);
1456
+ };
1457
+ function mergeFileIntoDoc(doc, fileXml, strategy) {
1458
+ let parsed;
1459
+ try {
1460
+ parsed = parse$4(fileXml);
1461
+ } catch (err) {
1462
+ console.warn(
1463
+ "[y-mxgraph] 合并失败,file XML 解析异常,回退到 replace:",
1464
+ err
1465
+ );
1466
+ return false;
1467
+ }
1468
+ const mxfileObj = parsed == null ? void 0 : parsed.mxfile;
1469
+ if (!mxfileObj || !Array.isArray(mxfileObj.diagram)) {
1470
+ console.warn(
1471
+ "[y-mxgraph] 合并失败,file XML 不是合法 mxfile,回退到 replace"
1472
+ );
1473
+ return false;
1474
+ }
1475
+ const mxfileMap = doc.getMap(key);
1476
+ const diagramMap = mxfileMap.get(key$1);
1477
+ const diagramOrder = mxfileMap.get(diagramOrderKey);
1478
+ if (!diagramMap || !diagramOrder) {
1479
+ console.warn("[y-mxgraph] 合并失败,doc 结构不完整,回退到 replace");
1480
+ return false;
1481
+ }
1482
+ doc.transact(() => {
1483
+ var _a;
1484
+ for (const diagram of mxfileObj.diagram) {
1485
+ const id = ((_a = diagram._attributes) == null ? void 0 : _a.id) || "";
1486
+ if (!id)
1487
+ continue;
1488
+ const docHas = diagramMap.has(id);
1489
+ if (docHas && strategy === "merge-remote") {
1490
+ continue;
1491
+ }
1492
+ const yDiagram = parse$1(
1493
+ diagram
1494
+ );
1495
+ diagramMap.set(id, yDiagram);
1496
+ if (!docHas) {
1497
+ diagramOrder.push([id]);
1498
+ }
1499
+ }
1500
+ });
1501
+ return true;
1502
+ }
1503
+ function reconcileInitialContent(doc, file, strategy, applyFileData) {
1504
+ const mxfileMap = doc.getMap(key);
1505
+ const docHasData = mxfileMap.size > 0;
1506
+ const fileHasAnyData = !!file.data;
1507
+ const fileHasDiagrams = fileHasAnyData && file.data.includes("<diagram");
1508
+ if (strategy === "replace") {
1509
+ if (docHasData) {
1510
+ const xml2 = doc2xml(doc);
1511
+ if (xml2 && xml2.includes("<diagram")) {
1512
+ applyFileData(file, xml2);
1513
+ } else if (!fileHasAnyData) {
1514
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1515
+ }
1516
+ } else if (!fileHasAnyData) {
1517
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1518
+ }
1519
+ return mxfileMap.size > 0;
1520
+ }
1521
+ if (!docHasData && !fileHasDiagrams) {
1522
+ if (!fileHasAnyData) {
1523
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1524
+ }
1525
+ return false;
1526
+ }
1527
+ if (!docHasData && fileHasDiagrams) {
1528
+ try {
1529
+ doc.transact(() => {
1530
+ xml2doc(file.data, doc);
1531
+ });
1532
+ return true;
1533
+ } catch (err) {
1534
+ console.warn(
1535
+ "[y-mxgraph] merge 模式下 xml2doc 失败,回退 replace:",
1536
+ err
1537
+ );
1538
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1539
+ return false;
1540
+ }
1541
+ }
1542
+ if (docHasData && !fileHasDiagrams) {
1543
+ const xml2 = doc2xml(doc);
1544
+ if (xml2 && xml2.includes("<diagram")) {
1545
+ applyFileData(file, xml2);
1546
+ } else if (!fileHasAnyData) {
1547
+ applyFileData(file, Binding.generateFileTemplate("diagram-0"));
1548
+ }
1549
+ return mxfileMap.size > 0;
1550
+ }
1551
+ const ok = mergeFileIntoDoc(doc, file.data, strategy);
1552
+ if (!ok) {
1553
+ const xml2 = doc2xml(doc);
1554
+ if (xml2 && xml2.includes("<diagram"))
1555
+ applyFileData(file, xml2);
1556
+ return mxfileMap.size > 0;
1557
+ }
1558
+ const xml = doc2xml(doc);
1559
+ if (xml && xml.includes("<diagram"))
1560
+ applyFileData(file, xml);
1561
+ return true;
1562
+ }
1454
1563
  class Binding {
1455
1564
  constructor(file, options) {
1456
1565
  this.suppressLocalApply = false;
1457
1566
  this.docInitialized = false;
1458
- const { doc, awareness, undoManager, mouseMoveThrottle, cursor } = options;
1567
+ const {
1568
+ doc,
1569
+ awareness,
1570
+ undoManager,
1571
+ mouseMoveThrottle,
1572
+ cursor,
1573
+ initialContent = "replace",
1574
+ applyFileData = defaultApplyFileData
1575
+ } = options;
1459
1576
  this.doc = doc;
1577
+ this.initialContentStrategy = initialContent;
1460
1578
  const ui = file.getUi();
1461
1579
  const graph = ui.editor.graph;
1462
1580
  this.mxGraphModel = graph.model;
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
- }
1581
+ this.suppressLocalApply = true;
1582
+ try {
1583
+ this.docInitialized = reconcileInitialContent(
1584
+ doc,
1585
+ file,
1586
+ initialContent,
1587
+ applyFileData
1588
+ );
1589
+ if (this.docInitialized) {
1590
+ initDocSnapshot(doc, false);
1477
1591
  }
1478
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1592
+ } finally {
1593
+ this.suppressLocalApply = false;
1479
1594
  }
1595
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1480
1596
  this.mxListener = () => {
1481
1597
  if (this.suppressLocalApply)
1482
1598
  return;
@@ -1499,13 +1615,36 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1499
1615
  };
1500
1616
  this.mxGraphModel.addListener("change", this.mxListener);
1501
1617
  this.docObserver = (events, transaction) => {
1502
- if (!this.docInitialized) {
1503
- this.docInitialized = true;
1504
- }
1505
1618
  if (transaction.local && transaction.origin === LOCAL_ORIGIN) {
1506
1619
  generatePatch(events);
1507
1620
  return;
1508
1621
  }
1622
+ if (this.shouldReplaceWhenDocHasData) {
1623
+ const mxfileMap = doc.getMap(key);
1624
+ const diagramMap = mxfileMap.get(key$1);
1625
+ if (diagramMap && diagramMap.size > 0) {
1626
+ const xml = doc2xml(doc);
1627
+ if (xml && xml.includes("<diagram")) {
1628
+ this.suppressLocalApply = true;
1629
+ try {
1630
+ applyFileData(file, xml);
1631
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1632
+ initDocSnapshot(doc, false);
1633
+ const ui2 = file.getUi();
1634
+ const editor = ui2.editor;
1635
+ editor.setStatus("");
1636
+ editor.setModified(false);
1637
+ } finally {
1638
+ this.suppressLocalApply = false;
1639
+ }
1640
+ this.docInitialized = true;
1641
+ return;
1642
+ }
1643
+ }
1644
+ }
1645
+ if (!this.docInitialized) {
1646
+ this.docInitialized = true;
1647
+ }
1509
1648
  const patch = generatePatch(events);
1510
1649
  if (Object.keys(patch).length === 0)
1511
1650
  return;
@@ -1517,7 +1656,7 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1517
1656
  this.suppressLocalApply = false;
1518
1657
  }
1519
1658
  };
1520
- mxfileMap.observeDeep(this.docObserver);
1659
+ doc.getMap(key).observeDeep(this.docObserver);
1521
1660
  if (awareness) {
1522
1661
  this.cleanupCollaborator = bindCollaborator(file, {
1523
1662
  awareness,
@@ -1530,6 +1669,10 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1530
1669
  this.cleanupUndoManager = bindUndoManager(doc, file, undoManager);
1531
1670
  }
1532
1671
  }
1672
+ /** replace 策略下,构造时 doc 为空,现在 doc 有数据时需要强制替换 */
1673
+ get shouldReplaceWhenDocHasData() {
1674
+ return this.initialContentStrategy === "replace" && !this.docInitialized;
1675
+ }
1533
1676
  /**
1534
1677
  * 销毁绑定,解除所有监听器
1535
1678
  * @param deep - 是否深度清理(包括 awareness/undoManager),默认 false