y-mxgraph 0.2.0 → 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
@@ -1559,6 +1559,7 @@ class Binding {
1559
1559
  applyFileData = defaultApplyFileData
1560
1560
  } = options;
1561
1561
  this.doc = doc;
1562
+ this.initialContentStrategy = initialContent;
1562
1563
  const ui = file.getUi();
1563
1564
  const graph = ui.editor.graph;
1564
1565
  this.mxGraphModel = graph.model;
@@ -1599,13 +1600,36 @@ class Binding {
1599
1600
  };
1600
1601
  this.mxGraphModel.addListener("change", this.mxListener);
1601
1602
  this.docObserver = (events, transaction) => {
1602
- if (!this.docInitialized) {
1603
- this.docInitialized = true;
1604
- }
1605
1603
  if (transaction.local && transaction.origin === LOCAL_ORIGIN) {
1606
1604
  generatePatch(events);
1607
1605
  return;
1608
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
+ }
1609
1633
  const patch = generatePatch(events);
1610
1634
  if (Object.keys(patch).length === 0)
1611
1635
  return;
@@ -1630,6 +1654,10 @@ class Binding {
1630
1654
  this.cleanupUndoManager = bindUndoManager(doc, file, undoManager);
1631
1655
  }
1632
1656
  }
1657
+ /** replace 策略下,构造时 doc 为空,现在 doc 有数据时需要强制替换 */
1658
+ get shouldReplaceWhenDocHasData() {
1659
+ return this.initialContentStrategy === "replace" && !this.docInitialized;
1660
+ }
1633
1661
  /**
1634
1662
  * 销毁绑定,解除所有监听器
1635
1663
  * @param deep - 是否深度清理(包括 awareness/undoManager),默认 false