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