@likec4/diagram 1.0.1 → 1.0.2
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/dist/index.mjs +19 -19
- package/package.json +8 -8
- package/src/xyflow/SyncWithDiagram.tsx +26 -23
package/dist/index.mjs
CHANGED
|
@@ -7764,7 +7764,9 @@ function toDomPrecision(v) {
|
|
|
7764
7764
|
return v === null ? 0.01 : Math.round(v * 100) / 100;
|
|
7765
7765
|
}
|
|
7766
7766
|
const isSameNode = (a) => (b) => a.id === b.id && a.type === b.type && Object.is(a.parentId, b.parentId), SyncWithDiagram = memo(() => {
|
|
7767
|
-
const
|
|
7767
|
+
const _xyflow = useXYFlow(), xyflowRef = useRef(_xyflow);
|
|
7768
|
+
xyflowRef.current = _xyflow;
|
|
7769
|
+
const xyflowApi = useXYStoreApi(), diagramStoreApi = useDiagramStoreApi();
|
|
7768
7770
|
return useEffect(() => diagramStoreApi.subscribe(
|
|
7769
7771
|
// selector
|
|
7770
7772
|
(state) => ({
|
|
@@ -7777,7 +7779,7 @@ const isSameNode = (a) => (b) => a.id === b.id && a.type === b.type && Object.is
|
|
|
7777
7779
|
// listener
|
|
7778
7780
|
({ viewId, nodes, edges, ...opts }) => {
|
|
7779
7781
|
const { lastOnNavigate } = diagramStoreApi.getState(), updates = diagramViewToXYFlowData({ nodes, edges }, opts);
|
|
7780
|
-
if (
|
|
7782
|
+
if (xyflowRef.current.setNodes(
|
|
7781
7783
|
(prev) => updates.nodes.map((update) => {
|
|
7782
7784
|
const existing = prev.find(isSameNode(update));
|
|
7783
7785
|
return existing ? deepEqual(existing.data.element, update.data.element) ? existing : {
|
|
@@ -7785,7 +7787,7 @@ const isSameNode = (a) => (b) => a.id === b.id && a.type === b.type && Object.is
|
|
|
7785
7787
|
...update
|
|
7786
7788
|
} : update;
|
|
7787
7789
|
})
|
|
7788
|
-
),
|
|
7790
|
+
), xyflowRef.current.setEdges(
|
|
7789
7791
|
(prev) => updates.edges.map((update) => {
|
|
7790
7792
|
const existing = prev.find((e) => e.id === update.id);
|
|
7791
7793
|
return existing ? deepEqual(existing.data, update.data) ? existing : {
|
|
@@ -7798,22 +7800,20 @@ const isSameNode = (a) => (b) => a.id === b.id && a.type === b.type && Object.is
|
|
|
7798
7800
|
if (elTo) {
|
|
7799
7801
|
const centerFrom = lastOnNavigate.elementCenterScreenPosition, nextZoom = Math.min(elFrom.width / elTo.width, elFrom.height / elTo.height);
|
|
7800
7802
|
requestAnimationFrame(() => {
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
|
|
7804
|
-
|
|
7805
|
-
|
|
7806
|
-
zoom: nextZoom
|
|
7807
|
-
});
|
|
7808
|
-
const centerTo = xyflow.flowToScreenPosition({
|
|
7809
|
-
x: elTo.position[0] + elTo.width / 2,
|
|
7810
|
-
y: elTo.position[1] + elTo.height / 2
|
|
7811
|
-
}), diff = {
|
|
7812
|
-
x: toDomPrecision(centerFrom.x - centerTo.x),
|
|
7813
|
-
y: toDomPrecision(centerFrom.y - centerTo.y)
|
|
7814
|
-
};
|
|
7815
|
-
xyflowApi.getState().panBy(diff);
|
|
7803
|
+
const v = xyflowRef.current.getViewport();
|
|
7804
|
+
nextZoom < v.zoom && xyflowRef.current.setViewport({
|
|
7805
|
+
x: v.x,
|
|
7806
|
+
y: v.y,
|
|
7807
|
+
zoom: nextZoom
|
|
7816
7808
|
});
|
|
7809
|
+
const centerTo = xyflowRef.current.flowToScreenPosition({
|
|
7810
|
+
x: elTo.position[0] + elTo.width / 2,
|
|
7811
|
+
y: elTo.position[1] + elTo.height / 2
|
|
7812
|
+
}), diff = {
|
|
7813
|
+
x: toDomPrecision(centerFrom.x - centerTo.x),
|
|
7814
|
+
y: toDomPrecision(centerFrom.y - centerTo.y)
|
|
7815
|
+
};
|
|
7816
|
+
xyflowApi.getState().panBy(diff);
|
|
7817
7817
|
});
|
|
7818
7818
|
}
|
|
7819
7819
|
diagramStoreApi.setState(
|
|
@@ -7831,7 +7831,7 @@ const isSameNode = (a) => (b) => a.id === b.id && a.type === b.type && Object.is
|
|
|
7831
7831
|
{
|
|
7832
7832
|
equalityFn: shallowEqual
|
|
7833
7833
|
}
|
|
7834
|
-
), [
|
|
7834
|
+
), [xyflowApi, diagramStoreApi]), null;
|
|
7835
7835
|
});
|
|
7836
7836
|
function getVarName(variable) {
|
|
7837
7837
|
var matches = variable.match(/^var\((.*)\)$/);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@likec4/diagram",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"bugs": "https://github.com/likec4/likec4/issues",
|
|
6
6
|
"homepage": "https://likec4.dev",
|
|
@@ -50,25 +50,25 @@
|
|
|
50
50
|
"clean": "run -T rimraf -v -g 'dist/*' .tsbuildinfo"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@likec4/core": "1.0.
|
|
53
|
+
"@likec4/core": "1.0.2",
|
|
54
54
|
"@react-hookz/web": "^24.0.4",
|
|
55
55
|
"esm-env": "^1.0.0",
|
|
56
56
|
"fast-equals": "^5.0.1",
|
|
57
|
-
"framer-motion": "^11.
|
|
57
|
+
"framer-motion": "^11.2.2",
|
|
58
58
|
"remeda": "^1.61.0",
|
|
59
59
|
"zustand": "^4.5.2"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
|
-
"@mantine/core": "^7.9.
|
|
63
|
-
"@mantine/hooks": "^7.9.
|
|
62
|
+
"@mantine/core": "^7.9.2",
|
|
63
|
+
"@mantine/hooks": "^7.9.2",
|
|
64
64
|
"react": "^18.3.1",
|
|
65
65
|
"react-dom": "^18.3.1"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@lume/kiwi": "^0.4.3",
|
|
69
|
-
"@mantine/core": "^7.9.
|
|
70
|
-
"@mantine/hooks": "^7.9.
|
|
71
|
-
"@mantine/vanilla-extract": "^7.9.
|
|
69
|
+
"@mantine/core": "^7.9.2",
|
|
70
|
+
"@mantine/hooks": "^7.9.2",
|
|
71
|
+
"@mantine/vanilla-extract": "^7.9.2",
|
|
72
72
|
"@total-typescript/ts-reset": "^0.5.1",
|
|
73
73
|
"@types/react": "18.2.0",
|
|
74
74
|
"@types/react-dom": "18.2.4",
|
|
@@ -2,7 +2,7 @@ import { deepEqual as eq, shallowEqual } from 'fast-equals'
|
|
|
2
2
|
import { useXYFlow, useXYStoreApi } from './hooks'
|
|
3
3
|
import type { XYFlowNode } from './types'
|
|
4
4
|
|
|
5
|
-
import { memo, useEffect } from 'react'
|
|
5
|
+
import { memo, useEffect, useRef } from 'react'
|
|
6
6
|
import { useDiagramStoreApi } from '../state'
|
|
7
7
|
import { diagramViewToXYFlowData } from './diagram-to-xyflow'
|
|
8
8
|
import { toDomPrecision } from './utils'
|
|
@@ -14,7 +14,10 @@ const isSameNode = (a: XYFlowNode) => (b: XYFlowNode) =>
|
|
|
14
14
|
* Syncs the diagram state with the XYFlow instance
|
|
15
15
|
*/
|
|
16
16
|
export const SyncWithDiagram = memo(() => {
|
|
17
|
-
const
|
|
17
|
+
const _xyflow = useXYFlow()
|
|
18
|
+
const xyflowRef = useRef(_xyflow)
|
|
19
|
+
xyflowRef.current = _xyflow
|
|
20
|
+
|
|
18
21
|
const xyflowApi = useXYStoreApi()
|
|
19
22
|
const diagramStoreApi = useDiagramStoreApi()
|
|
20
23
|
|
|
@@ -33,7 +36,7 @@ export const SyncWithDiagram = memo(() => {
|
|
|
33
36
|
const { lastOnNavigate } = diagramStoreApi.getState()
|
|
34
37
|
const updates = diagramViewToXYFlowData({ nodes, edges }, opts)
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
xyflowRef.current.setNodes(prev =>
|
|
37
40
|
updates.nodes.map(update => {
|
|
38
41
|
const existing = prev.find(isSameNode(update))
|
|
39
42
|
if (existing) {
|
|
@@ -49,7 +52,7 @@ export const SyncWithDiagram = memo(() => {
|
|
|
49
52
|
})
|
|
50
53
|
)
|
|
51
54
|
|
|
52
|
-
|
|
55
|
+
xyflowRef.current.setEdges(prev =>
|
|
53
56
|
updates.edges.map(update => {
|
|
54
57
|
const existing = prev.find(e => e.id === update.id)
|
|
55
58
|
if (existing) {
|
|
@@ -70,25 +73,25 @@ export const SyncWithDiagram = memo(() => {
|
|
|
70
73
|
const nextZoom = Math.min(elFrom.width / elTo.width, elFrom.height / elTo.height)
|
|
71
74
|
|
|
72
75
|
requestAnimationFrame(() => {
|
|
73
|
-
requestAnimationFrame(() => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
// requestAnimationFrame(() => {
|
|
77
|
+
const v = xyflowRef.current.getViewport()
|
|
78
|
+
if (nextZoom < v.zoom) {
|
|
79
|
+
xyflowRef.current.setViewport({
|
|
80
|
+
x: v.x,
|
|
81
|
+
y: v.y,
|
|
82
|
+
zoom: nextZoom
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
const centerTo = xyflowRef.current.flowToScreenPosition({
|
|
86
|
+
x: elTo.position[0] + elTo.width / 2,
|
|
87
|
+
y: elTo.position[1] + elTo.height / 2
|
|
88
|
+
}),
|
|
89
|
+
diff = {
|
|
90
|
+
x: toDomPrecision(centerFrom.x - centerTo.x),
|
|
91
|
+
y: toDomPrecision(centerFrom.y - centerTo.y)
|
|
81
92
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
y: elTo.position[1] + elTo.height / 2
|
|
85
|
-
}),
|
|
86
|
-
diff = {
|
|
87
|
-
x: toDomPrecision(centerFrom.x - centerTo.x),
|
|
88
|
-
y: toDomPrecision(centerFrom.y - centerTo.y)
|
|
89
|
-
}
|
|
90
|
-
xyflowApi.getState().panBy(diff)
|
|
91
|
-
})
|
|
93
|
+
xyflowApi.getState().panBy(diff)
|
|
94
|
+
// })
|
|
92
95
|
})
|
|
93
96
|
}
|
|
94
97
|
diagramStoreApi.setState(
|
|
@@ -107,7 +110,7 @@ export const SyncWithDiagram = memo(() => {
|
|
|
107
110
|
equalityFn: shallowEqual
|
|
108
111
|
}
|
|
109
112
|
)
|
|
110
|
-
}, [
|
|
113
|
+
}, [xyflowApi, diagramStoreApi])
|
|
111
114
|
|
|
112
115
|
return null
|
|
113
116
|
})
|