@g9tm/lynxcontrols-graph 0.0.0-stage → 2.10.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 +93 -2
- package/catalog/cards/G9GraphCanvas.md +17 -0
- package/catalog/catalog.json +981 -0
- package/catalog/index.md +5 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/canvas.d.ts +122 -0
- package/dist/canvas.d.ts.map +1 -0
- package/dist/canvas.jsx +889 -0
- package/dist/core.d.ts +151 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +325 -0
- package/dist/core.js.map +1 -0
- package/dist/core.jsx +323 -0
- package/dist/graph.css +142 -0
- package/dist/graph.css.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.jsx +2 -0
- package/dist/mt.d.ts +26 -0
- package/dist/mt.d.ts.map +1 -0
- package/dist/mt.jsx +91 -0
- package/package.json +36 -4
package/dist/canvas.jsx
ADDED
|
@@ -0,0 +1,889 @@
|
|
|
1
|
+
import { measureNode } from "@g9tm/lynxcontrols/anchor";
|
|
2
|
+
import { g9SafeId, startTimer, stopTimer } from "@g9tm/lynxcontrols/base";
|
|
3
|
+
import { g9HeldModifiers, useG9Keys } from "@g9tm/lynxcontrols/commands";
|
|
4
|
+
import { registerG9DropTarget, useG9DragState } from "@g9tm/lynxcontrols/pointer";
|
|
5
|
+
import { useG9Direction, useG9Theme } from "@g9tm/lynxcontrols/provider";
|
|
6
|
+
import { g9MotionScale, isContextMenuKey } from "@g9tm/lynxcontrols-foundation";
|
|
7
|
+
import { memo, runOnBackground, runOnMainThread, useEffect, useMainThreadRef, useMemo, useRef, useState } from "@lynx-js/react";
|
|
8
|
+
import { contentBounds, cullGraph, edgeGeometry, fitView, nextSelection, nodeAt, nodeInDirection, portPoint, snapTo, toScreen, toWorld, visibleWorld, zoomAt } from "./core.jsx";
|
|
9
|
+
import { edgeOfTargetMT, liveEdgesSvgMT, nodeIndexAtMT, snapMT, zoomAtMT } from "./mt.jsx";
|
|
10
|
+
import "./graph.css";
|
|
11
|
+
const IDLE_DRAG = {
|
|
12
|
+
mode: 0,
|
|
13
|
+
node: -1,
|
|
14
|
+
sx: 0,
|
|
15
|
+
sy: 0,
|
|
16
|
+
lx: 0,
|
|
17
|
+
ly: 0,
|
|
18
|
+
z: 1,
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
ids: [],
|
|
22
|
+
starts: [],
|
|
23
|
+
els: [],
|
|
24
|
+
moved: {},
|
|
25
|
+
dist: 0,
|
|
26
|
+
mx: 0,
|
|
27
|
+
my: 0
|
|
28
|
+
};
|
|
29
|
+
let seq = 0;
|
|
30
|
+
const sameView = (a, b)=>Math.abs(a.zoom - b.zoom) < 1e-6 && Math.abs(a.x - b.x) < 0.01 && Math.abs(a.y - b.y) < 0.01;
|
|
31
|
+
const sameState = (a, b)=>a.selected === b.selected && a.focused === b.focused && a.dragging === b.dragging && a.dropCandidate === b.dropCandidate && a.dropOver === b.dropOver && a.zoom === b.zoom;
|
|
32
|
+
const Card = memo(function(props) {
|
|
33
|
+
const { node, state } = props;
|
|
34
|
+
const cls = `g9-graph__node${state.selected ? ' g9-graph__node--selected' : ''}${state.focused ? ' g9-graph__node--focused' : ''}${state.selected && state.focused ? ' g9-graph__node--selfocus' : ''}${state.dragging ? ' g9-graph__node--dragging' : ''}${state.dropCandidate ? ' g9-graph__node--candidate' : ''}${state.dropOver ? ' g9-graph__node--over' : ''}`;
|
|
35
|
+
return <view id={props.elementId} className={cls} style={{
|
|
36
|
+
left: `${node.x}px`,
|
|
37
|
+
top: `${node.y}px`,
|
|
38
|
+
width: `${node.width}px`,
|
|
39
|
+
height: `${node.height}px`
|
|
40
|
+
}} accessibility-element={false}>
|
|
41
|
+
{props.render(node, state)}
|
|
42
|
+
</view>;
|
|
43
|
+
}, (a, b)=>a.node === b.node && a.render === b.render && a.elementId === b.elementId && sameState(a.state, b.state));
|
|
44
|
+
function svgOf(paths, box) {
|
|
45
|
+
const r = (v)=>Math.round(10 * v) / 10;
|
|
46
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${r(box.x)} ${r(box.y)} ${r(box.width)} ${r(box.height)}" width="${r(box.width)}" height="${r(box.height)}">${paths}</svg>`;
|
|
47
|
+
}
|
|
48
|
+
function pathsOf(list, color, offset) {
|
|
49
|
+
let out = '';
|
|
50
|
+
for (const { g, edge } of list){
|
|
51
|
+
var _edge_width;
|
|
52
|
+
const dashed = 'dashed' === edge.style || 'active' === edge.style;
|
|
53
|
+
out += `<path d="${g.d}" fill="none" stroke="${color(edge)}" stroke-width="${null != (_edge_width = edge.width) ? _edge_width : 1.5}"${dashed ? ` stroke-dasharray="6 4"${offset ? ` stroke-dashoffset="${offset}"` : ''}` : ''}/>`;
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
function boxOf(list) {
|
|
58
|
+
let x0 = 1 / 0;
|
|
59
|
+
let y0 = 1 / 0;
|
|
60
|
+
let x1 = -1 / 0;
|
|
61
|
+
let y1 = -1 / 0;
|
|
62
|
+
for (const { g } of list){
|
|
63
|
+
x0 = Math.min(x0, g.box.x - 4);
|
|
64
|
+
y0 = Math.min(y0, g.box.y - 4);
|
|
65
|
+
x1 = Math.max(x1, g.box.x + g.box.width + 4);
|
|
66
|
+
y1 = Math.max(y1, g.box.y + g.box.height + 4);
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
x: x0,
|
|
70
|
+
y: y0,
|
|
71
|
+
width: x1 - x0,
|
|
72
|
+
height: y1 - y0
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function ActiveEdges(props) {
|
|
76
|
+
const [phase, setPhase] = useState(0);
|
|
77
|
+
useEffect(()=>{
|
|
78
|
+
if (!props.animate || 0 === props.list.length) return;
|
|
79
|
+
let handle = null;
|
|
80
|
+
const tick = ()=>{
|
|
81
|
+
setPhase((p)=>(p + 1) % 5);
|
|
82
|
+
handle = startTimer(tick, 90);
|
|
83
|
+
};
|
|
84
|
+
handle = startTimer(tick, 90);
|
|
85
|
+
return ()=>stopTimer(handle);
|
|
86
|
+
}, [
|
|
87
|
+
props.animate,
|
|
88
|
+
props.list.length
|
|
89
|
+
]);
|
|
90
|
+
if (0 === props.list.length) return null;
|
|
91
|
+
const box = boxOf(props.list);
|
|
92
|
+
return <svg className="g9-graph__edges g9-graph__edges--active" content={svgOf(pathsOf(props.list, props.color, 2 * -phase), box)} style={{
|
|
93
|
+
left: `${box.x}px`,
|
|
94
|
+
top: `${box.y}px`,
|
|
95
|
+
width: `${box.width}px`,
|
|
96
|
+
height: `${box.height}px`
|
|
97
|
+
}}/>;
|
|
98
|
+
}
|
|
99
|
+
const ICONS = {
|
|
100
|
+
plus: 'M12 5v14M5 12h14',
|
|
101
|
+
minus: 'M5 12h14',
|
|
102
|
+
fit: 'M4 9V4h5M15 4h5v5M20 15v5h-5M9 20H4v-5'
|
|
103
|
+
};
|
|
104
|
+
function ChromeButton(props) {
|
|
105
|
+
return <view id={props.id} className="g9-graph__button" bindtap={props.onPress} accessibility-element={true} accessibility-traits="button" accessibility-label={props.label}>
|
|
106
|
+
<svg className="g9-graph__icon" content={`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18"><path d="${ICONS[props.icon]}" fill="none" stroke="${props.color}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`}/>
|
|
107
|
+
</view>;
|
|
108
|
+
}
|
|
109
|
+
const pagePoint = (e)=>{
|
|
110
|
+
if ('number' == typeof e.pageX && 'number' == typeof e.pageY) return {
|
|
111
|
+
x: e.pageX,
|
|
112
|
+
y: e.pageY
|
|
113
|
+
};
|
|
114
|
+
if ('number' == typeof e.x && 'number' == typeof e.y) return {
|
|
115
|
+
x: e.x,
|
|
116
|
+
y: e.y
|
|
117
|
+
};
|
|
118
|
+
if (e.detail && 'number' == typeof e.detail.x && 'number' == typeof e.detail.y) return {
|
|
119
|
+
x: e.detail.x,
|
|
120
|
+
y: e.detail.y
|
|
121
|
+
};
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
function G9GraphCanvas(props) {
|
|
125
|
+
var _props_minZoom, _props_maxZoom, _props_curvature, _props_edges, _props_groups, _props_defaultView, _ref, _props_view, _props_defaultSelection, _props_selection, _props_keyboard, _props_cullMargin, _props_labels, _labels_zoomIn, _labels_zoomOut, _labels_fit;
|
|
126
|
+
var _props_view1, _props_view2, _props_view3;
|
|
127
|
+
const theme = useG9Theme();
|
|
128
|
+
const rtl = 'rtl' === useG9Direction();
|
|
129
|
+
const uid = useMemo(()=>{
|
|
130
|
+
var _props_id;
|
|
131
|
+
return g9SafeId(null != (_props_id = props.id) ? _props_id : `g9graph${++seq}`);
|
|
132
|
+
}, [
|
|
133
|
+
props.id
|
|
134
|
+
]);
|
|
135
|
+
const nodeElementId = (id)=>`${uid}__n_${g9SafeId(id)}`;
|
|
136
|
+
const min = null != (_props_minZoom = props.minZoom) ? _props_minZoom : 0.1;
|
|
137
|
+
const max = null != (_props_maxZoom = props.maxZoom) ? _props_maxZoom : 2.5;
|
|
138
|
+
const curvature = null != (_props_curvature = props.curvature) ? _props_curvature : 0.5;
|
|
139
|
+
const edges = null != (_props_edges = props.edges) ? _props_edges : [];
|
|
140
|
+
const groups = null != (_props_groups = props.groups) ? _props_groups : [];
|
|
141
|
+
const latest = useRef(props);
|
|
142
|
+
latest.current = props;
|
|
143
|
+
const [size, setSize] = useState(null);
|
|
144
|
+
const [ownView, setOwnView] = useState(null != (_props_defaultView = props.defaultView) ? _props_defaultView : null);
|
|
145
|
+
const view1 = null != (_ref = null != (_props_view = props.view) ? _props_view : ownView) ? _ref : {
|
|
146
|
+
zoom: 1,
|
|
147
|
+
x: 0,
|
|
148
|
+
y: 0
|
|
149
|
+
};
|
|
150
|
+
const viewRef = useRef(view1);
|
|
151
|
+
viewRef.current = view1;
|
|
152
|
+
const reported = useRef([]);
|
|
153
|
+
const root = useRef(null);
|
|
154
|
+
const origin = useRef({
|
|
155
|
+
x: 0,
|
|
156
|
+
y: 0
|
|
157
|
+
});
|
|
158
|
+
const world = useMainThreadRef(null);
|
|
159
|
+
const live = useMainThreadRef(null);
|
|
160
|
+
const geo = useMainThreadRef(null);
|
|
161
|
+
const drag = useMainThreadRef({
|
|
162
|
+
...IDLE_DRAG
|
|
163
|
+
});
|
|
164
|
+
const viewMT = useMainThreadRef([
|
|
165
|
+
1,
|
|
166
|
+
0,
|
|
167
|
+
0
|
|
168
|
+
]);
|
|
169
|
+
function applyViewMT(z, x, y) {
|
|
170
|
+
'main thread';
|
|
171
|
+
var _world_current;
|
|
172
|
+
viewMT.current = [
|
|
173
|
+
z,
|
|
174
|
+
x,
|
|
175
|
+
y
|
|
176
|
+
];
|
|
177
|
+
null == (_world_current = world.current) || _world_current.setStyleProperty('transform', `translate(${x}px, ${y}px) scale(${z})`);
|
|
178
|
+
}
|
|
179
|
+
const fromMain = (z, x, y)=>{
|
|
180
|
+
var _latest_current_onViewChange, _latest_current;
|
|
181
|
+
const next = {
|
|
182
|
+
zoom: z,
|
|
183
|
+
x,
|
|
184
|
+
y
|
|
185
|
+
};
|
|
186
|
+
reported.current = reported.current.slice(-15).concat(next);
|
|
187
|
+
if (void 0 === latest.current.view) setOwnView(next);
|
|
188
|
+
else viewRef.current = next;
|
|
189
|
+
null == (_latest_current_onViewChange = (_latest_current = latest.current).onViewChange) || _latest_current_onViewChange.call(_latest_current, next);
|
|
190
|
+
};
|
|
191
|
+
const setView = (next)=>{
|
|
192
|
+
var _latest_current_onViewChange, _latest_current;
|
|
193
|
+
const v = {
|
|
194
|
+
zoom: Math.min(max, Math.max(min, next.zoom)),
|
|
195
|
+
x: next.x,
|
|
196
|
+
y: next.y
|
|
197
|
+
};
|
|
198
|
+
viewRef.current = v;
|
|
199
|
+
runOnMainThread(applyViewMT)(v.zoom, v.x, v.y);
|
|
200
|
+
if (void 0 === latest.current.view) setOwnView(v);
|
|
201
|
+
null == (_latest_current_onViewChange = (_latest_current = latest.current).onViewChange) || _latest_current_onViewChange.call(_latest_current, v);
|
|
202
|
+
};
|
|
203
|
+
useEffect(()=>{
|
|
204
|
+
if (!props.view) return;
|
|
205
|
+
if (reported.current.some((r)=>sameView(r, props.view))) return;
|
|
206
|
+
runOnMainThread(applyViewMT)(props.view.zoom, props.view.x, props.view.y);
|
|
207
|
+
}, [
|
|
208
|
+
null == (_props_view1 = props.view) ? void 0 : _props_view1.zoom,
|
|
209
|
+
null == (_props_view2 = props.view) ? void 0 : _props_view2.x,
|
|
210
|
+
null == (_props_view3 = props.view) ? void 0 : _props_view3.y
|
|
211
|
+
]);
|
|
212
|
+
const bounds = useMemo(()=>contentBounds(props.nodes, groups), [
|
|
213
|
+
props.nodes,
|
|
214
|
+
groups
|
|
215
|
+
]);
|
|
216
|
+
const fit = ()=>{
|
|
217
|
+
if (!size) return;
|
|
218
|
+
setView(fitView(bounds, size.width, size.height, {
|
|
219
|
+
minZoom: min,
|
|
220
|
+
maxZoom: Math.min(1, max)
|
|
221
|
+
}));
|
|
222
|
+
};
|
|
223
|
+
const sized = useRef(false);
|
|
224
|
+
useEffect(()=>{
|
|
225
|
+
if (!size || sized.current) return;
|
|
226
|
+
sized.current = true;
|
|
227
|
+
if (void 0 === props.view && void 0 === props.defaultView) fit();
|
|
228
|
+
else runOnMainThread(applyViewMT)(view1.zoom, view1.x, view1.y);
|
|
229
|
+
}, [
|
|
230
|
+
size
|
|
231
|
+
]);
|
|
232
|
+
const zoomBy = (factor)=>{
|
|
233
|
+
var _ref, _ref1;
|
|
234
|
+
const w = null != (_ref = null == size ? void 0 : size.width) ? _ref : 0;
|
|
235
|
+
const h = null != (_ref1 = null == size ? void 0 : size.height) ? _ref1 : 0;
|
|
236
|
+
setView(zoomAt(viewRef.current, factor, w / 2, h / 2, min, max));
|
|
237
|
+
};
|
|
238
|
+
const centerOn = (id)=>{
|
|
239
|
+
const n = latest.current.nodes.find((x)=>x.id === id);
|
|
240
|
+
if (!n || !size) return;
|
|
241
|
+
const v = viewRef.current;
|
|
242
|
+
setView({
|
|
243
|
+
zoom: v.zoom,
|
|
244
|
+
x: size.width / 2 - (n.x + n.width / 2) * v.zoom,
|
|
245
|
+
y: size.height / 2 - (n.y + n.height / 2) * v.zoom
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
if (props.handleRef) props.handleRef.current = {
|
|
249
|
+
fit,
|
|
250
|
+
zoomIn: ()=>zoomBy(1.25),
|
|
251
|
+
zoomOut: ()=>zoomBy(0.8),
|
|
252
|
+
setView,
|
|
253
|
+
centerOn,
|
|
254
|
+
getView: ()=>viewRef.current
|
|
255
|
+
};
|
|
256
|
+
const onLayout = (e)=>{
|
|
257
|
+
const next = {
|
|
258
|
+
width: e.detail.width,
|
|
259
|
+
height: e.detail.height
|
|
260
|
+
};
|
|
261
|
+
if (!size || Math.abs(size.width - next.width) > 0.5 || Math.abs(size.height - next.height) > 0.5) setSize(next);
|
|
262
|
+
measureNode(root.current).then((r)=>{
|
|
263
|
+
if (!r) return;
|
|
264
|
+
origin.current = {
|
|
265
|
+
x: r.x,
|
|
266
|
+
y: r.y
|
|
267
|
+
};
|
|
268
|
+
runOnMainThread(setOriginMT)(r.x, r.y);
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
function setOriginMT(x, y) {
|
|
272
|
+
'main thread';
|
|
273
|
+
if (geo.current) {
|
|
274
|
+
geo.current.ox = x;
|
|
275
|
+
geo.current.oy = y;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const [ownSelection, setOwnSelection] = useState(null != (_props_defaultSelection = props.defaultSelection) ? _props_defaultSelection : []);
|
|
279
|
+
const selection = null != (_props_selection = props.selection) ? _props_selection : ownSelection;
|
|
280
|
+
const selectionRef = useRef(selection);
|
|
281
|
+
selectionRef.current = selection;
|
|
282
|
+
const select = (next)=>{
|
|
283
|
+
var _latest_current_onSelectionChange, _latest_current;
|
|
284
|
+
if (void 0 === props.selection) setOwnSelection(next);
|
|
285
|
+
selectionRef.current = next;
|
|
286
|
+
null == (_latest_current_onSelectionChange = (_latest_current = latest.current).onSelectionChange) || _latest_current_onSelectionChange.call(_latest_current, next);
|
|
287
|
+
};
|
|
288
|
+
const [focus, setFocus] = useState(null);
|
|
289
|
+
const [active, setActive] = useState(false);
|
|
290
|
+
const keyboard = null != (_props_keyboard = props.keyboard) ? _props_keyboard : 'auto';
|
|
291
|
+
const byId = useMemo(()=>new Map(props.nodes.map((n)=>[
|
|
292
|
+
n.id,
|
|
293
|
+
n
|
|
294
|
+
])), [
|
|
295
|
+
props.nodes
|
|
296
|
+
]);
|
|
297
|
+
const geometries = useMemo(()=>{
|
|
298
|
+
const out = [];
|
|
299
|
+
for (const edge of edges){
|
|
300
|
+
const g = edgeGeometry(edge, byId, curvature);
|
|
301
|
+
if (g) out.push({
|
|
302
|
+
g,
|
|
303
|
+
edge
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
}, [
|
|
308
|
+
edges,
|
|
309
|
+
byId,
|
|
310
|
+
curvature
|
|
311
|
+
]);
|
|
312
|
+
const outline = theme.palette.Outline;
|
|
313
|
+
const primary = theme.palette.Primary;
|
|
314
|
+
const strokeOf = (e)=>{
|
|
315
|
+
var _e_color;
|
|
316
|
+
return null != (_e_color = e.color) ? _e_color : 'active' === e.style ? primary : outline;
|
|
317
|
+
};
|
|
318
|
+
useEffect(()=>{
|
|
319
|
+
var _props_snap;
|
|
320
|
+
const nodes = props.nodes.map((n)=>[
|
|
321
|
+
n.id,
|
|
322
|
+
n.x,
|
|
323
|
+
n.y,
|
|
324
|
+
n.width,
|
|
325
|
+
n.height,
|
|
326
|
+
false === n.draggable ? 0 : 1
|
|
327
|
+
]);
|
|
328
|
+
const mtEdges = [];
|
|
329
|
+
for (const e of edges){
|
|
330
|
+
var _ref, _ref1, _e_width;
|
|
331
|
+
var _a_ports_find, _a_ports, _b_ports_find, _b_ports;
|
|
332
|
+
const a = byId.get(e.from.node);
|
|
333
|
+
const b = byId.get(e.to.node);
|
|
334
|
+
if (!a || !b) continue;
|
|
335
|
+
const pa = portPoint(a, e.from, 'from');
|
|
336
|
+
const pb = portPoint(b, e.to, 'to');
|
|
337
|
+
const sa = null != (_ref = null == (_a_ports = a.ports) ? void 0 : null == (_a_ports_find = _a_ports.find((p)=>p.id === e.from.port)) ? void 0 : _a_ports_find.side) ? _ref : 'right';
|
|
338
|
+
const sb = null != (_ref1 = null == (_b_ports = b.ports) ? void 0 : null == (_b_ports_find = _b_ports.find((p)=>p.id === e.to.port)) ? void 0 : _b_ports_find.side) ? _ref1 : 'left';
|
|
339
|
+
mtEdges.push([
|
|
340
|
+
e.id,
|
|
341
|
+
a.id,
|
|
342
|
+
pa.x - a.x,
|
|
343
|
+
pa.y - a.y,
|
|
344
|
+
'right' === sa ? 1 : -1,
|
|
345
|
+
b.id,
|
|
346
|
+
pb.x - b.x,
|
|
347
|
+
pb.y - b.y,
|
|
348
|
+
'right' === sb ? 1 : -1,
|
|
349
|
+
strokeOf(e),
|
|
350
|
+
null != (_e_width = e.width) ? _e_width : 1.5,
|
|
351
|
+
'dashed' === e.style ? 1 : 'active' === e.style ? 2 : 0
|
|
352
|
+
]);
|
|
353
|
+
}
|
|
354
|
+
const next = {
|
|
355
|
+
nodes,
|
|
356
|
+
edges: mtEdges,
|
|
357
|
+
selection: selection.slice(),
|
|
358
|
+
snap: null != (_props_snap = props.snap) ? _props_snap : 0,
|
|
359
|
+
curvature,
|
|
360
|
+
min,
|
|
361
|
+
max,
|
|
362
|
+
wheel: 'zoom' === props.wheel ? 1 : 'pan' === props.wheel ? 2 : 0,
|
|
363
|
+
ox: origin.current.x,
|
|
364
|
+
oy: origin.current.y,
|
|
365
|
+
uid,
|
|
366
|
+
onMove: Boolean(props.onMove)
|
|
367
|
+
};
|
|
368
|
+
runOnMainThread(setGeoMT)(next);
|
|
369
|
+
}, [
|
|
370
|
+
props.nodes,
|
|
371
|
+
edges,
|
|
372
|
+
selection,
|
|
373
|
+
props.snap,
|
|
374
|
+
curvature,
|
|
375
|
+
min,
|
|
376
|
+
max,
|
|
377
|
+
props.wheel,
|
|
378
|
+
uid,
|
|
379
|
+
Boolean(props.onMove),
|
|
380
|
+
outline,
|
|
381
|
+
primary
|
|
382
|
+
]);
|
|
383
|
+
function setGeoMT(next) {
|
|
384
|
+
'main thread';
|
|
385
|
+
geo.current = next;
|
|
386
|
+
}
|
|
387
|
+
const [dragging, setDragging] = useState([]);
|
|
388
|
+
const clearAfter = useRef(null);
|
|
389
|
+
const onDragStart = (ids)=>setDragging(ids);
|
|
390
|
+
const onMoveLive = (id, x, y)=>{
|
|
391
|
+
var _latest_current_onMove, _latest_current;
|
|
392
|
+
return null == (_latest_current_onMove = (_latest_current = latest.current).onMove) ? void 0 : _latest_current_onMove.call(_latest_current, id, x, y);
|
|
393
|
+
};
|
|
394
|
+
const onMoveEnd = (ids, xs, ys)=>{
|
|
395
|
+
var _latest_current_onMoveEnd, _latest_current;
|
|
396
|
+
clearAfter.current = ids;
|
|
397
|
+
setDragging([]);
|
|
398
|
+
const moves = ids.map((id, i)=>({
|
|
399
|
+
id,
|
|
400
|
+
x: xs[i],
|
|
401
|
+
y: ys[i]
|
|
402
|
+
}));
|
|
403
|
+
null == (_latest_current_onMoveEnd = (_latest_current = latest.current).onMoveEnd) || _latest_current_onMoveEnd.call(_latest_current, moves);
|
|
404
|
+
};
|
|
405
|
+
useEffect(()=>{
|
|
406
|
+
if (!clearAfter.current) return;
|
|
407
|
+
const ids = clearAfter.current;
|
|
408
|
+
clearAfter.current = null;
|
|
409
|
+
runOnMainThread(clearMovedMT)(ids);
|
|
410
|
+
});
|
|
411
|
+
function clearMovedMT(ids) {
|
|
412
|
+
'main thread';
|
|
413
|
+
var _live_current, _live_current1;
|
|
414
|
+
const g = geo.current;
|
|
415
|
+
if (!g || !world.current) return;
|
|
416
|
+
for (const id of ids){
|
|
417
|
+
const el = world.current.querySelector(`#${g.uid}__n_${id.replace(/[^A-Za-z0-9_-]/g, '_')}`);
|
|
418
|
+
null == el || el.setStyleProperty('transform', 'translate(0px, 0px)');
|
|
419
|
+
}
|
|
420
|
+
null == (_live_current = live.current) || _live_current.setAttribute('content', '');
|
|
421
|
+
null == (_live_current1 = live.current) || _live_current1.setStyleProperties({
|
|
422
|
+
width: '0px',
|
|
423
|
+
height: '0px'
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
const onClick = (id, double)=>{
|
|
427
|
+
var _latest_current_onActivate, _latest_current;
|
|
428
|
+
if ('auto' === keyboard) setActive(true);
|
|
429
|
+
const m = g9HeldModifiers();
|
|
430
|
+
const additive = m.shift || m.ctrl || m.meta;
|
|
431
|
+
select(nextSelection(selectionRef.current, id, additive));
|
|
432
|
+
setFocus(id);
|
|
433
|
+
if (double && id) null == (_latest_current_onActivate = (_latest_current = latest.current).onActivate) || _latest_current_onActivate.call(_latest_current, id);
|
|
434
|
+
};
|
|
435
|
+
const menuAt = (px, py, source, edge)=>{
|
|
436
|
+
var _latest_current_onContextMenu, _latest_current;
|
|
437
|
+
const sx = px - origin.current.x;
|
|
438
|
+
const sy = py - origin.current.y;
|
|
439
|
+
const w = toWorld(viewRef.current, sx, sy);
|
|
440
|
+
const n = edge ? null : nodeAt(latest.current.nodes, w.x, w.y);
|
|
441
|
+
const target = edge ? {
|
|
442
|
+
kind: 'edge',
|
|
443
|
+
id: edge
|
|
444
|
+
} : n ? {
|
|
445
|
+
kind: 'node',
|
|
446
|
+
id: n.id
|
|
447
|
+
} : {
|
|
448
|
+
kind: 'canvas'
|
|
449
|
+
};
|
|
450
|
+
if ('auto' === keyboard) setActive(true);
|
|
451
|
+
if (n && !selectionRef.current.includes(n.id)) select([
|
|
452
|
+
n.id
|
|
453
|
+
]);
|
|
454
|
+
if (n) setFocus(n.id);
|
|
455
|
+
null == (_latest_current_onContextMenu = (_latest_current = latest.current).onContextMenu) || _latest_current_onContextMenu.call(_latest_current, {
|
|
456
|
+
target,
|
|
457
|
+
x: px,
|
|
458
|
+
y: py,
|
|
459
|
+
world: w,
|
|
460
|
+
source
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
const edgeOf = (e)=>{
|
|
464
|
+
var _e_target_dataset, _e_target;
|
|
465
|
+
const v = null == (_e_target = e.target) ? void 0 : null == (_e_target_dataset = _e_target.dataset) ? void 0 : _e_target_dataset.g9edge;
|
|
466
|
+
return 'string' == typeof v ? v : null;
|
|
467
|
+
};
|
|
468
|
+
const lastTap = useMainThreadRef([
|
|
469
|
+
0,
|
|
470
|
+
-2
|
|
471
|
+
]);
|
|
472
|
+
function viewNowMT() {
|
|
473
|
+
'main thread';
|
|
474
|
+
return viewMT.current;
|
|
475
|
+
}
|
|
476
|
+
function downMT(e) {
|
|
477
|
+
'main thread';
|
|
478
|
+
var _raw_button, _ref, _raw_pageX, _ref1, _raw_pageY;
|
|
479
|
+
const g = geo.current;
|
|
480
|
+
if (!g) return;
|
|
481
|
+
const raw = e;
|
|
482
|
+
const touches = raw.touches;
|
|
483
|
+
if (touches && touches.length >= 2) {
|
|
484
|
+
const a = touches[0];
|
|
485
|
+
const b = touches[1];
|
|
486
|
+
const v = viewNowMT();
|
|
487
|
+
const d = drag.current;
|
|
488
|
+
d.mode = 4;
|
|
489
|
+
d.dist = Math.max(1, Math.hypot(b.pageX - a.pageX, b.pageY - a.pageY));
|
|
490
|
+
d.mx = (a.pageX + b.pageX) / 2 - g.ox;
|
|
491
|
+
d.my = (a.pageY + b.pageY) / 2 - g.oy;
|
|
492
|
+
d.z = v[0];
|
|
493
|
+
d.x = v[1];
|
|
494
|
+
d.y = v[2];
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const button = null != (_raw_button = raw.button) ? _raw_button : 0;
|
|
498
|
+
if (2 === button) {
|
|
499
|
+
var _ref2, _raw_pageX1, _ref3, _raw_pageY1;
|
|
500
|
+
const target = e.target;
|
|
501
|
+
const edge = edgeOfTargetMT(target);
|
|
502
|
+
runOnBackground(menuAt)(null != (_ref2 = null != (_raw_pageX1 = raw.pageX) ? _raw_pageX1 : raw.x) ? _ref2 : 0, null != (_ref3 = null != (_raw_pageY1 = raw.pageY) ? _raw_pageY1 : raw.y) ? _ref3 : 0, 'mouse', edge);
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
const t = null == touches ? void 0 : touches[0];
|
|
506
|
+
const px = t ? t.pageX : null != (_ref = null != (_raw_pageX = raw.pageX) ? _raw_pageX : raw.x) ? _ref : 0;
|
|
507
|
+
const py = t ? t.pageY : null != (_ref1 = null != (_raw_pageY = raw.pageY) ? _raw_pageY : raw.y) ? _ref1 : 0;
|
|
508
|
+
const v = viewNowMT();
|
|
509
|
+
const wx = (px - g.ox - v[1]) / v[0];
|
|
510
|
+
const wy = (py - g.oy - v[2]) / v[0];
|
|
511
|
+
const d = drag.current;
|
|
512
|
+
d.mode = 1 === button ? 2 : 1;
|
|
513
|
+
d.node = 1 === button ? -1 : nodeIndexAtMT(g.nodes, wx, wy);
|
|
514
|
+
d.sx = px;
|
|
515
|
+
d.sy = py;
|
|
516
|
+
d.lx = px;
|
|
517
|
+
d.ly = py;
|
|
518
|
+
d.z = v[0];
|
|
519
|
+
d.x = v[1];
|
|
520
|
+
d.y = v[2];
|
|
521
|
+
}
|
|
522
|
+
function startNodeDragMT() {
|
|
523
|
+
'main thread';
|
|
524
|
+
const g = geo.current;
|
|
525
|
+
const d = drag.current;
|
|
526
|
+
const n = g.nodes[d.node];
|
|
527
|
+
const ids = g.selection.includes(n[0]) ? g.selection : [
|
|
528
|
+
n[0]
|
|
529
|
+
];
|
|
530
|
+
d.ids = [];
|
|
531
|
+
d.starts = [];
|
|
532
|
+
d.els = [];
|
|
533
|
+
d.moved = {};
|
|
534
|
+
for (const id of ids){
|
|
535
|
+
var _world_current;
|
|
536
|
+
const node = g.nodes.find((x)=>x[0] === id);
|
|
537
|
+
if (!node || 0 === node[5]) continue;
|
|
538
|
+
const el = null == (_world_current = world.current) ? void 0 : _world_current.querySelector(`#${g.uid}__n_${id.replace(/[^A-Za-z0-9_-]/g, '_')}`);
|
|
539
|
+
if (el) {
|
|
540
|
+
d.ids.push(id);
|
|
541
|
+
d.starts.push(node[1], node[2]);
|
|
542
|
+
d.els.push(el);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
d.mode = 3;
|
|
546
|
+
runOnBackground(onDragStart)(d.ids.slice());
|
|
547
|
+
}
|
|
548
|
+
function moveMT(e) {
|
|
549
|
+
'main thread';
|
|
550
|
+
var _ref, _raw_pageX, _ref1, _raw_pageY;
|
|
551
|
+
var _live_current, _live_current1;
|
|
552
|
+
const d = drag.current;
|
|
553
|
+
const g = geo.current;
|
|
554
|
+
if (0 === d.mode || !g) return;
|
|
555
|
+
const raw = e;
|
|
556
|
+
const touches = raw.touches;
|
|
557
|
+
if (4 === d.mode) {
|
|
558
|
+
if (!touches || touches.length < 2) return;
|
|
559
|
+
const a = touches[0];
|
|
560
|
+
const b = touches[1];
|
|
561
|
+
const dist = Math.max(1, Math.hypot(b.pageX - a.pageX, b.pageY - a.pageY));
|
|
562
|
+
const mx = (a.pageX + b.pageX) / 2 - g.ox;
|
|
563
|
+
const my = (a.pageY + b.pageY) / 2 - g.oy;
|
|
564
|
+
const z = zoomAtMT(d.z, d.x + (mx - d.mx), d.y + (my - d.my), dist / d.dist, mx, my, g.min, g.max);
|
|
565
|
+
applyViewMT(z[0], z[1], z[2]);
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const t = null == touches ? void 0 : touches[0];
|
|
569
|
+
const px = t ? t.pageX : null != (_ref = null != (_raw_pageX = raw.pageX) ? _raw_pageX : raw.x) ? _ref : 0;
|
|
570
|
+
const py = t ? t.pageY : null != (_ref1 = null != (_raw_pageY = raw.pageY) ? _raw_pageY : raw.y) ? _ref1 : 0;
|
|
571
|
+
d.lx = px;
|
|
572
|
+
d.ly = py;
|
|
573
|
+
const dx = px - d.sx;
|
|
574
|
+
const dy = py - d.sy;
|
|
575
|
+
if (1 === d.mode) {
|
|
576
|
+
if (Math.abs(dx) + Math.abs(dy) < 4) return;
|
|
577
|
+
const n = d.node >= 0 ? g.nodes[d.node] : void 0;
|
|
578
|
+
if (n && 1 === n[5]) startNodeDragMT();
|
|
579
|
+
else d.mode = 2;
|
|
580
|
+
}
|
|
581
|
+
if (2 === d.mode) return void applyViewMT(d.z, d.x + dx, d.y + dy);
|
|
582
|
+
const wdx = dx / d.z;
|
|
583
|
+
const wdy = dy / d.z;
|
|
584
|
+
for(let i = 0; i < d.ids.length; i++){
|
|
585
|
+
var _d_els_i;
|
|
586
|
+
const sx = d.starts[2 * i];
|
|
587
|
+
const sy = d.starts[2 * i + 1];
|
|
588
|
+
const nx = snapMT(sx + wdx, g.snap);
|
|
589
|
+
const ny = snapMT(sy + wdy, g.snap);
|
|
590
|
+
d.moved[d.ids[i]] = [
|
|
591
|
+
nx,
|
|
592
|
+
ny
|
|
593
|
+
];
|
|
594
|
+
null == (_d_els_i = d.els[i]) || _d_els_i.setStyleProperty('transform', `translate(${nx - sx}px, ${ny - sy}px)`);
|
|
595
|
+
}
|
|
596
|
+
const svg1 = liveEdgesSvgMT(g.edges, g.nodes, d.moved, g.curvature);
|
|
597
|
+
null == (_live_current = live.current) || _live_current.setAttribute('content', svg1[0]);
|
|
598
|
+
null == (_live_current1 = live.current) || _live_current1.setStyleProperties({
|
|
599
|
+
left: `${svg1[1]}px`,
|
|
600
|
+
top: `${svg1[2]}px`,
|
|
601
|
+
width: `${svg1[3]}px`,
|
|
602
|
+
height: `${svg1[4]}px`
|
|
603
|
+
});
|
|
604
|
+
const first = d.ids[0];
|
|
605
|
+
if (g.onMove && first) {
|
|
606
|
+
const p = d.moved[first];
|
|
607
|
+
runOnBackground(onMoveLive)(first, p[0], p[1]);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function upMT() {
|
|
611
|
+
'main thread';
|
|
612
|
+
const d = drag.current;
|
|
613
|
+
const g = geo.current;
|
|
614
|
+
const mode = d.mode;
|
|
615
|
+
d.mode = 0;
|
|
616
|
+
if (!g || 0 === mode) return;
|
|
617
|
+
if (1 === mode) {
|
|
618
|
+
const id = d.node >= 0 ? g.nodes[d.node][0] : null;
|
|
619
|
+
const now = Date.now();
|
|
620
|
+
const double = d.node >= 0 && lastTap.current[1] === d.node && now - lastTap.current[0] < 400;
|
|
621
|
+
lastTap.current = [
|
|
622
|
+
now,
|
|
623
|
+
d.node
|
|
624
|
+
];
|
|
625
|
+
runOnBackground(onClick)(id, double);
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
if (3 === mode) {
|
|
629
|
+
const xs = [];
|
|
630
|
+
const ys = [];
|
|
631
|
+
for(let i = 0; i < d.ids.length; i++){
|
|
632
|
+
const p = d.moved[d.ids[i]];
|
|
633
|
+
xs.push(p ? p[0] : d.starts[2 * i]);
|
|
634
|
+
ys.push(p ? p[1] : d.starts[2 * i + 1]);
|
|
635
|
+
}
|
|
636
|
+
runOnBackground(onMoveEnd)(d.ids.slice(), xs, ys);
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
const v = viewNowMT();
|
|
640
|
+
runOnBackground(fromMain)(v[0], v[1], v[2]);
|
|
641
|
+
}
|
|
642
|
+
function touchEndMT(e) {
|
|
643
|
+
'main thread';
|
|
644
|
+
var _ref;
|
|
645
|
+
var _e_touches;
|
|
646
|
+
const left = null != (_ref = null == (_e_touches = e.touches) ? void 0 : _e_touches.length) ? _ref : 0;
|
|
647
|
+
if (4 === drag.current.mode) {
|
|
648
|
+
if (left > 0) return;
|
|
649
|
+
drag.current.mode = 0;
|
|
650
|
+
const v = viewNowMT();
|
|
651
|
+
runOnBackground(fromMain)(v[0], v[1], v[2]);
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
upMT();
|
|
655
|
+
}
|
|
656
|
+
function wheelMT(e) {
|
|
657
|
+
'main thread';
|
|
658
|
+
var _raw_deltaX, _raw_deltaY;
|
|
659
|
+
const g = geo.current;
|
|
660
|
+
if (!g) return;
|
|
661
|
+
const raw = e;
|
|
662
|
+
const dx = null != (_raw_deltaX = raw.deltaX) ? _raw_deltaX : 0;
|
|
663
|
+
const dy = null != (_raw_deltaY = raw.deltaY) ? _raw_deltaY : 0;
|
|
664
|
+
const v = viewNowMT();
|
|
665
|
+
const trackpad = 0 !== dx || Math.abs(dy) < 40 && Math.round(dy) !== dy;
|
|
666
|
+
const zoom = 1 === g.wheel || 0 === g.wheel && (true === raw.ctrlKey || !trackpad);
|
|
667
|
+
if (zoom) {
|
|
668
|
+
var _ref, _raw_pageX, _ref1, _raw_pageY;
|
|
669
|
+
const sx = (null != (_ref = null != (_raw_pageX = raw.pageX) ? _raw_pageX : raw.x) ? _ref : g.ox) - g.ox;
|
|
670
|
+
const sy = (null != (_ref1 = null != (_raw_pageY = raw.pageY) ? _raw_pageY : raw.y) ? _ref1 : g.oy) - g.oy;
|
|
671
|
+
const factor = Math.exp(0.0015 * -Math.max(-300, Math.min(300, dy)));
|
|
672
|
+
const z = zoomAtMT(v[0], v[1], v[2], factor, sx, sy, g.min, g.max);
|
|
673
|
+
applyViewMT(z[0], z[1], z[2]);
|
|
674
|
+
} else applyViewMT(v[0], v[1] - dx, v[2] - dy);
|
|
675
|
+
const n = viewNowMT();
|
|
676
|
+
runOnBackground(fromMain)(n[0], n[1], n[2]);
|
|
677
|
+
}
|
|
678
|
+
const keysOn = 'always' === keyboard || 'auto' === keyboard && active;
|
|
679
|
+
useG9Keys((e)=>{
|
|
680
|
+
const nodes = latest.current.nodes;
|
|
681
|
+
const mod = e.ctrl || e.meta;
|
|
682
|
+
const dirs = {
|
|
683
|
+
ArrowLeft: 'left',
|
|
684
|
+
ArrowRight: 'right',
|
|
685
|
+
ArrowUp: 'up',
|
|
686
|
+
ArrowDown: 'down'
|
|
687
|
+
};
|
|
688
|
+
const dir = dirs[e.key];
|
|
689
|
+
if (dir && mod) {
|
|
690
|
+
var _latest_current_onMoveEnd, _latest_current;
|
|
691
|
+
const step = latest.current.snap || 10;
|
|
692
|
+
const dx = 'left' === dir ? -step : 'right' === dir ? step : 0;
|
|
693
|
+
const dy = 'up' === dir ? -step : 'down' === dir ? step : 0;
|
|
694
|
+
const moves = nodes.filter((n)=>selectionRef.current.includes(n.id) && false !== n.draggable).map((n)=>({
|
|
695
|
+
id: n.id,
|
|
696
|
+
x: snapTo(n.x + dx, latest.current.snap),
|
|
697
|
+
y: snapTo(n.y + dy, latest.current.snap)
|
|
698
|
+
}));
|
|
699
|
+
if (moves.length) null == (_latest_current_onMoveEnd = (_latest_current = latest.current).onMoveEnd) || _latest_current_onMoveEnd.call(_latest_current, moves);
|
|
700
|
+
return moves.length > 0;
|
|
701
|
+
}
|
|
702
|
+
if (dir) {
|
|
703
|
+
const next = nodeInDirection(nodes, focus, dir);
|
|
704
|
+
if (!next) return true;
|
|
705
|
+
setFocus(next);
|
|
706
|
+
select(e.shift ? nextSelection(selectionRef.current.filter((s)=>s !== next), next, true) : [
|
|
707
|
+
next
|
|
708
|
+
]);
|
|
709
|
+
if (size) {
|
|
710
|
+
const n = nodes.find((x)=>x.id === next);
|
|
711
|
+
const v = viewRef.current;
|
|
712
|
+
const a = toScreen(v, n.x, n.y);
|
|
713
|
+
const b = toScreen(v, n.x + n.width, n.y + n.height);
|
|
714
|
+
if (a.x < 0 || a.y < 0 || b.x > size.width || b.y > size.height) centerOn(next);
|
|
715
|
+
}
|
|
716
|
+
return true;
|
|
717
|
+
}
|
|
718
|
+
if (('Delete' === e.key || 'Backspace' === e.key) && !mod) {
|
|
719
|
+
var _latest_current_onDelete, _latest_current1;
|
|
720
|
+
if (0 === selectionRef.current.length) return false;
|
|
721
|
+
null == (_latest_current_onDelete = (_latest_current1 = latest.current).onDelete) || _latest_current_onDelete.call(_latest_current1, selectionRef.current.slice());
|
|
722
|
+
return true;
|
|
723
|
+
}
|
|
724
|
+
if ('Enter' === e.key && focus) {
|
|
725
|
+
var _latest_current_onActivate, _latest_current2;
|
|
726
|
+
null == (_latest_current_onActivate = (_latest_current2 = latest.current).onActivate) || _latest_current_onActivate.call(_latest_current2, focus);
|
|
727
|
+
return true;
|
|
728
|
+
}
|
|
729
|
+
if (mod && 'a' === e.key.toLowerCase()) {
|
|
730
|
+
select(nodes.map((n)=>n.id));
|
|
731
|
+
return true;
|
|
732
|
+
}
|
|
733
|
+
if ('Escape' === e.key) {
|
|
734
|
+
if (0 === selectionRef.current.length && null === focus) {
|
|
735
|
+
if ('auto' === keyboard) setActive(false);
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
select([]);
|
|
739
|
+
setFocus(null);
|
|
740
|
+
return true;
|
|
741
|
+
}
|
|
742
|
+
if (!mod && ('+' === e.key || '=' === e.key)) return zoomBy(1.25), true;
|
|
743
|
+
if (!mod && '-' === e.key) return zoomBy(0.8), true;
|
|
744
|
+
if (!mod && '0' === e.key) return fit(), true;
|
|
745
|
+
if (isContextMenuKey(e) && focus) {
|
|
746
|
+
var _latest_current_onContextMenu, _latest_current3;
|
|
747
|
+
const n = nodes.find((x)=>x.id === focus);
|
|
748
|
+
if (!n) return false;
|
|
749
|
+
const c = toScreen(viewRef.current, n.x + n.width / 2, n.y + n.height / 2);
|
|
750
|
+
null == (_latest_current_onContextMenu = (_latest_current3 = latest.current).onContextMenu) || _latest_current_onContextMenu.call(_latest_current3, {
|
|
751
|
+
target: {
|
|
752
|
+
kind: 'node',
|
|
753
|
+
id: n.id
|
|
754
|
+
},
|
|
755
|
+
x: c.x + origin.current.x,
|
|
756
|
+
y: c.y + origin.current.y,
|
|
757
|
+
world: {
|
|
758
|
+
x: n.x + n.width / 2,
|
|
759
|
+
y: n.y + n.height / 2
|
|
760
|
+
},
|
|
761
|
+
source: 'keyboard'
|
|
762
|
+
});
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
return false;
|
|
766
|
+
}, {
|
|
767
|
+
enabled: keysOn
|
|
768
|
+
});
|
|
769
|
+
const dragState = useG9DragState();
|
|
770
|
+
useEffect(()=>{
|
|
771
|
+
if (!props.onDrop) return;
|
|
772
|
+
return registerG9DropTarget({
|
|
773
|
+
id: uid,
|
|
774
|
+
accepts: (p)=>latest.current.nodes.some((n)=>{
|
|
775
|
+
var _n_acceptsDrop;
|
|
776
|
+
return (null == (_n_acceptsDrop = n.acceptsDrop) ? void 0 : _n_acceptsDrop.call(n, p)) === true;
|
|
777
|
+
}),
|
|
778
|
+
measure: ()=>measureNode(root.current),
|
|
779
|
+
hit: (x, y, rect, payload)=>{
|
|
780
|
+
var _n_acceptsDrop;
|
|
781
|
+
const w = toWorld(viewRef.current, x - rect.x, y - rect.y);
|
|
782
|
+
const n = nodeAt(latest.current.nodes, w.x, w.y);
|
|
783
|
+
return n && (null == (_n_acceptsDrop = n.acceptsDrop) ? void 0 : _n_acceptsDrop.call(n, payload)) === true ? n.id : null;
|
|
784
|
+
},
|
|
785
|
+
onDrop: (e)=>{
|
|
786
|
+
var _latest_current_onDrop, _latest_current;
|
|
787
|
+
if (!e.part) return;
|
|
788
|
+
const w = toWorld(viewRef.current, e.x - origin.current.x, e.y - origin.current.y);
|
|
789
|
+
null == (_latest_current_onDrop = (_latest_current = latest.current).onDrop) || _latest_current_onDrop.call(_latest_current, {
|
|
790
|
+
nodeId: e.part,
|
|
791
|
+
payload: e.payload,
|
|
792
|
+
world: w
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
}, [
|
|
797
|
+
uid,
|
|
798
|
+
Boolean(props.onDrop)
|
|
799
|
+
]);
|
|
800
|
+
const dropping = dragState && dragState.candidates.includes(uid) ? dragState : null;
|
|
801
|
+
const draggingSet = useMemo(()=>new Set(dragging), [
|
|
802
|
+
dragging
|
|
803
|
+
]);
|
|
804
|
+
const visible = size ? visibleWorld(view1, size.width, size.height, null != (_props_cullMargin = props.cullMargin) ? _props_cullMargin : 0.5) : null;
|
|
805
|
+
const culled = useMemo(()=>visible ? cullGraph(props.nodes, geometries.map((s)=>s.g), visible) : null, [
|
|
806
|
+
props.nodes,
|
|
807
|
+
geometries,
|
|
808
|
+
null == visible ? void 0 : visible.x,
|
|
809
|
+
null == visible ? void 0 : visible.y,
|
|
810
|
+
null == visible ? void 0 : visible.width,
|
|
811
|
+
null == visible ? void 0 : visible.height
|
|
812
|
+
]);
|
|
813
|
+
const shownEdges = geometries.filter((s)=>(!culled || culled.edges.has(s.g.id)) && !draggingSet.has(s.edge.from.node) && !draggingSet.has(s.edge.to.node));
|
|
814
|
+
const staticEdges = shownEdges.filter((s)=>'active' !== s.edge.style);
|
|
815
|
+
const activeEdges = shownEdges.filter((s)=>'active' === s.edge.style);
|
|
816
|
+
const staticBox = staticEdges.length ? boxOf(staticEdges) : null;
|
|
817
|
+
const selectedSet = useMemo(()=>new Set(selection), [
|
|
818
|
+
selection
|
|
819
|
+
]);
|
|
820
|
+
const zoomStep = Math.round(20 * view1.zoom) / 20;
|
|
821
|
+
const motion = false !== props.animateActive && g9MotionScale() > 0;
|
|
822
|
+
const labels = null != (_props_labels = props.labels) ? _props_labels : {};
|
|
823
|
+
return <view className={`g9-graph${props.className ? ` ${props.className}` : ''}`} id={uid} ref={(r)=>{
|
|
824
|
+
root.current = r;
|
|
825
|
+
}} style={{
|
|
826
|
+
direction: 'ltr'
|
|
827
|
+
}} bindlayoutchange={onLayout} main-thread:bindmousedown={downMT} main-thread:bindmousemove={moveMT} main-thread:bindmouseup={upMT} main-thread:bindmouseleave={upMT} main-thread:bindtouchstart={downMT} main-thread:bindtouchmove={moveMT} main-thread:bindtouchend={touchEndMT} main-thread:bindtouchcancel={touchEndMT} main-thread:bindwheel={wheelMT} bindlongpress={(e)=>{
|
|
828
|
+
const p = pagePoint(e);
|
|
829
|
+
if (p) menuAt(p.x, p.y, 'touch', edgeOf(e));
|
|
830
|
+
}}>
|
|
831
|
+
<view className="g9-graph__world" main-thread:ref={world}>
|
|
832
|
+
{groups.map((g)=><view key={g.id} className="g9-graph__group" style={{
|
|
833
|
+
left: `${g.x}px`,
|
|
834
|
+
top: `${g.y}px`,
|
|
835
|
+
width: `${g.width}px`,
|
|
836
|
+
height: `${g.height}px`,
|
|
837
|
+
...g.color ? {
|
|
838
|
+
backgroundColor: g.color
|
|
839
|
+
} : {}
|
|
840
|
+
}}>
|
|
841
|
+
{g.title ? <text className="g9-graph__group-title" text-maxline="1">
|
|
842
|
+
{g.title}
|
|
843
|
+
</text> : null}
|
|
844
|
+
</view>)}
|
|
845
|
+
{staticBox ? <svg className="g9-graph__edges" content={svgOf(pathsOf(staticEdges, strokeOf, 0), staticBox)} style={{
|
|
846
|
+
left: `${staticBox.x}px`,
|
|
847
|
+
top: `${staticBox.y}px`,
|
|
848
|
+
width: `${staticBox.width}px`,
|
|
849
|
+
height: `${staticBox.height}px`
|
|
850
|
+
}}/> : null}
|
|
851
|
+
<ActiveEdges list={activeEdges} color={strokeOf} animate={motion}/>
|
|
852
|
+
<svg className="g9-graph__edges g9-graph__edges--live" main-thread:ref={live} content=""/>
|
|
853
|
+
{shownEdges.filter((s)=>s.edge.label).map((s)=><view key={`l:${s.edge.id}`} className="g9-graph__edge-label" data-g9edge={s.edge.id} style={{
|
|
854
|
+
left: `${s.g.mid.x}px`,
|
|
855
|
+
top: `${s.g.mid.y}px`
|
|
856
|
+
}} {...props.onEdgePress ? {
|
|
857
|
+
bindtap: ()=>{
|
|
858
|
+
var _latest_current_onEdgePress, _latest_current;
|
|
859
|
+
return null == (_latest_current_onEdgePress = (_latest_current = latest.current).onEdgePress) ? void 0 : _latest_current_onEdgePress.call(_latest_current, s.edge.id);
|
|
860
|
+
}
|
|
861
|
+
} : {}}>
|
|
862
|
+
<text className="g9-graph__edge-text" data-g9edge={s.edge.id} text-maxline="1">
|
|
863
|
+
{s.edge.label}
|
|
864
|
+
</text>
|
|
865
|
+
</view>)}
|
|
866
|
+
{props.nodes.map((n)=>{
|
|
867
|
+
var _n_acceptsDrop, _dropping_over;
|
|
868
|
+
const isDragging = draggingSet.has(n.id);
|
|
869
|
+
if (culled && !culled.nodes.has(n.id) && !isDragging && !selectedSet.has(n.id)) return null;
|
|
870
|
+
const candidate = null !== dropping && (null == (_n_acceptsDrop = n.acceptsDrop) ? void 0 : _n_acceptsDrop.call(n, dropping.payload)) === true;
|
|
871
|
+
const state = {
|
|
872
|
+
selected: selectedSet.has(n.id),
|
|
873
|
+
focused: focus === n.id,
|
|
874
|
+
dragging: isDragging,
|
|
875
|
+
dropCandidate: candidate,
|
|
876
|
+
dropOver: candidate && (null == dropping ? void 0 : null == (_dropping_over = dropping.over) ? void 0 : _dropping_over.part) === n.id,
|
|
877
|
+
zoom: zoomStep
|
|
878
|
+
};
|
|
879
|
+
return <Card key={n.id} node={n} state={state} render={props.renderNode} elementId={nodeElementId(n.id)}/>;
|
|
880
|
+
})}
|
|
881
|
+
</view>
|
|
882
|
+
{false === props.controls ? null : <view className={`g9-graph__controls g9-graph__controls--${rtl ? 'rtl' : 'ltr'}`}>
|
|
883
|
+
<ChromeButton id={`${uid}_ZoomIn`} icon="plus" label={null != (_labels_zoomIn = labels.zoomIn) ? _labels_zoomIn : 'Zoom in'} color={theme.palette.OnSurface} onPress={()=>zoomBy(1.25)}/>
|
|
884
|
+
<ChromeButton id={`${uid}_ZoomOut`} icon="minus" label={null != (_labels_zoomOut = labels.zoomOut) ? _labels_zoomOut : 'Zoom out'} color={theme.palette.OnSurface} onPress={()=>zoomBy(0.8)}/>
|
|
885
|
+
<ChromeButton id={`${uid}_Fit`} icon="fit" label={null != (_labels_fit = labels.fit) ? _labels_fit : 'Fit'} color={theme.palette.OnSurface} onPress={fit}/>
|
|
886
|
+
</view>}
|
|
887
|
+
</view>;
|
|
888
|
+
}
|
|
889
|
+
export { G9GraphCanvas };
|