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/README.md +14 -8
- package/binding/index.d.ts +21 -0
- package/binding/index.d.ts.map +1 -1
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/package.json +6 -1
- package/types/drawio.d.ts +4 -0
- package/types/drawio.d.ts.map +1 -1
- package/y-mxgraph.cjs.js +132 -17
- package/y-mxgraph.cjs.js.map +1 -1
- package/y-mxgraph.es.js +132 -17
- package/y-mxgraph.es.js.map +1 -1
- package/y-mxgraph.iife.js +132 -17
- package/y-mxgraph.iife.js.map +1 -1
- package/y-mxgraph.umd.js +132 -17
- package/y-mxgraph.umd.js.map +1 -1
package/y-mxgraph.es.js
CHANGED
|
@@ -1436,32 +1436,147 @@ 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 {
|
|
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;
|
|
1445
1562
|
const ui = file.getUi();
|
|
1446
1563
|
const graph = ui.editor.graph;
|
|
1447
1564
|
this.mxGraphModel = graph.model;
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
file.patch([fullPatch]);
|
|
1459
|
-
} finally {
|
|
1460
|
-
this.suppressLocalApply = false;
|
|
1461
|
-
}
|
|
1565
|
+
this.suppressLocalApply = true;
|
|
1566
|
+
try {
|
|
1567
|
+
this.docInitialized = reconcileInitialContent(
|
|
1568
|
+
doc,
|
|
1569
|
+
file,
|
|
1570
|
+
initialContent,
|
|
1571
|
+
applyFileData
|
|
1572
|
+
);
|
|
1573
|
+
if (this.docInitialized) {
|
|
1574
|
+
initDocSnapshot(doc, false);
|
|
1462
1575
|
}
|
|
1463
|
-
|
|
1576
|
+
} finally {
|
|
1577
|
+
this.suppressLocalApply = false;
|
|
1464
1578
|
}
|
|
1579
|
+
file.setShadowPages(file.ui.clonePages(file.ui.pages));
|
|
1465
1580
|
this.mxListener = () => {
|
|
1466
1581
|
if (this.suppressLocalApply)
|
|
1467
1582
|
return;
|
|
@@ -1502,7 +1617,7 @@ class Binding {
|
|
|
1502
1617
|
this.suppressLocalApply = false;
|
|
1503
1618
|
}
|
|
1504
1619
|
};
|
|
1505
|
-
|
|
1620
|
+
doc.getMap(key).observeDeep(this.docObserver);
|
|
1506
1621
|
if (awareness) {
|
|
1507
1622
|
this.cleanupCollaborator = bindCollaborator(file, {
|
|
1508
1623
|
awareness,
|