y-mxgraph 0.1.5 → 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.iife.js CHANGED
@@ -1451,32 +1451,147 @@ 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;
1460
1577
  const ui = file.getUi();
1461
1578
  const graph = ui.editor.graph;
1462
1579
  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
- }
1580
+ this.suppressLocalApply = true;
1581
+ try {
1582
+ this.docInitialized = reconcileInitialContent(
1583
+ doc,
1584
+ file,
1585
+ initialContent,
1586
+ applyFileData
1587
+ );
1588
+ if (this.docInitialized) {
1589
+ initDocSnapshot(doc, false);
1477
1590
  }
1478
- file.setShadowPages(file.ui.clonePages(file.ui.pages));
1591
+ } finally {
1592
+ this.suppressLocalApply = false;
1479
1593
  }
1594
+ file.setShadowPages(file.ui.clonePages(file.ui.pages));
1480
1595
  this.mxListener = () => {
1481
1596
  if (this.suppressLocalApply)
1482
1597
  return;
@@ -1517,7 +1632,7 @@ var YMXGraph = function(exports, xmlJs, Y2, lodashEs, colord2) {
1517
1632
  this.suppressLocalApply = false;
1518
1633
  }
1519
1634
  };
1520
- mxfileMap.observeDeep(this.docObserver);
1635
+ doc.getMap(key).observeDeep(this.docObserver);
1521
1636
  if (awareness) {
1522
1637
  this.cleanupCollaborator = bindCollaborator(file, {
1523
1638
  awareness,