@fragiola/flow 0.0.0-stage → 0.1.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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +25 -2
- package/dist/connect-session-nyEp02_P.js +375 -0
- package/dist/connect-session-nyEp02_P.js.map +1 -0
- package/dist/connection.d.ts +31 -0
- package/dist/connection.js +60 -0
- package/dist/connection.js.map +1 -0
- package/dist/core-queries-BPNbcM6k.js +452 -0
- package/dist/core-queries-BPNbcM6k.js.map +1 -0
- package/dist/document-schema-CPK5xkyO.js +369 -0
- package/dist/document-schema-CPK5xkyO.js.map +1 -0
- package/dist/drag.d.ts +19 -0
- package/dist/drag.js +175 -0
- package/dist/drag.js.map +1 -0
- package/dist/index.d.ts +876 -0
- package/dist/index.js +5711 -0
- package/dist/index.js.map +1 -0
- package/dist/keyboard-links-CVaTtlZg.js +8 -0
- package/dist/keyboard-links-CVaTtlZg.js.map +1 -0
- package/dist/keyboard.d.ts +40 -0
- package/dist/keyboard.js +342 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/minimap.d.ts +26 -0
- package/dist/minimap.js +206 -0
- package/dist/minimap.js.map +1 -0
- package/dist/node-moves-DyWe-4dj.js +36 -0
- package/dist/node-moves-DyWe-4dj.js.map +1 -0
- package/dist/part-attributes-DuBltA__.js +71 -0
- package/dist/part-attributes-DuBltA__.js.map +1 -0
- package/dist/path-format-2OU7Eiyy.js +43 -0
- package/dist/path-format-2OU7Eiyy.js.map +1 -0
- package/dist/reconnection.d.ts +15 -0
- package/dist/reconnection.js +138 -0
- package/dist/reconnection.js.map +1 -0
- package/dist/rect-CmoREaR6.js +92 -0
- package/dist/rect-CmoREaR6.js.map +1 -0
- package/dist/resize.d.ts +33 -0
- package/dist/resize.js +316 -0
- package/dist/resize.js.map +1 -0
- package/dist/rules-DLOMrPBG.js +102 -0
- package/dist/rules-DLOMrPBG.js.map +1 -0
- package/dist/selection.d.ts +38 -0
- package/dist/selection.js +392 -0
- package/dist/selection.js.map +1 -0
- package/dist/subflows.d.ts +9 -0
- package/dist/subflows.js +237 -0
- package/dist/subflows.js.map +1 -0
- package/dist/types-CLHcZVCb.d.ts +2660 -0
- package/dist/viewport-math-B-9Pn2kN.js +69 -0
- package/dist/viewport-math-B-9Pn2kN.js.map +1 -0
- package/package.json +94 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @fragiola/flow
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#72](https://github.com/fragiola/flow/pull/72) [`e3abda6`](https://github.com/fragiola/flow/commit/e3abda6bbb1422de6c06f9fad041d8c3cc6613c3) Thanks [@maurodesouza](https://github.com/maurodesouza)! - The framework-free core of Fragiola Flow, headless node-based editors and diagrams: the version 1
|
|
8
|
+
document (nodes, edges, handles) and its JSON Schema with `validateFlow` and
|
|
9
|
+
`FlowValidationError`; `createFlowModel`, a typed model whose every change is a command (`node.*`,
|
|
10
|
+
`edge.*`, `batch`) behind the five verbs `run`, `can`, `check`, `get` and `is`, with middleware
|
|
11
|
+
for connection rules; the engine that binds a flow to the DOM (the viewport with pan, zoom,
|
|
12
|
+
`fit-view` and `center-on`, node and handle measurement, edge paths for `bezier`,
|
|
13
|
+
`simplebezier`, `smoothstep`, `step` and `straight`, Pointer Events gestures for mouse, touch and
|
|
14
|
+
pen, z-order, the part attributes); and the opt-in features, each on its own entry:
|
|
15
|
+
`selection()`, `drag()`, `connection()`, `reconnection()`, `resize()`, `subflows()`,
|
|
16
|
+
`keyboard()` and `minimap()`. Zero runtime dependencies, no CSS.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mauro de Souza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fragiola/flow
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The framework-free core of **Flow**, headless node-based editors and diagrams: the flow document
|
|
4
|
+
with its JSON Schema, a typed model whose every change is a command (`run`, `can`, `check`, `get`,
|
|
5
|
+
`is`), opt-in features and the engine that binds a flow to the DOM (viewport, measurement, handles,
|
|
6
|
+
edge paths, gestures). Zero runtime dependencies, no CSS.
|
|
7
|
+
|
|
8
|
+
The opt-in features are on their own entries: `@fragiola/flow/selection`, `/subflows`, `/drag`,
|
|
9
|
+
`/resize`, `/connection`, `/reconnection`, `/keyboard`, `/minimap`.
|
|
10
|
+
|
|
11
|
+
An app installs its framework's package instead, which re-exports all of this:
|
|
12
|
+
[`@fragiola/flow-react`](https://www.npmjs.com/package/@fragiola/flow-react),
|
|
13
|
+
[`@fragiola/flow-vue`](https://www.npmjs.com/package/@fragiola/flow-vue),
|
|
14
|
+
[`@fragiola/flow-svelte`](https://www.npmjs.com/package/@fragiola/flow-svelte) or
|
|
15
|
+
[`@fragiola/flow-angular`](https://www.npmjs.com/package/@fragiola/flow-angular).
|
|
16
|
+
Install the core alone for Node, tests or another framework:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
pnpm add @fragiola/flow
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Documentation: **<https://fragiola.com/flow>**. ESM only.
|
|
23
|
+
|
|
24
|
+
## Licence
|
|
25
|
+
|
|
26
|
+
MIT
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import { f as flag } from "./part-attributes-DuBltA__.js";
|
|
2
|
+
import { r as oppositeSide } from "./path-format-2OU7Eiyy.js";
|
|
3
|
+
// src/connect-session/connect-session.ts
|
|
4
|
+
/** A handle as a connection's events name it. */
|
|
5
|
+
function endOf(handle) {
|
|
6
|
+
return {
|
|
7
|
+
nodeId: handle.nodeId,
|
|
8
|
+
handleId: handle.handleId,
|
|
9
|
+
type: handle.type
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** The shared connection state of one engine. */
|
|
13
|
+
var ConnectSession = class {
|
|
14
|
+
host;
|
|
15
|
+
/** how near (in pixels) a handle snaps; set by `connection()` */
|
|
16
|
+
radius = 20;
|
|
17
|
+
mode = "strict";
|
|
18
|
+
/** the connection line's path (`connection({ lineType })`) */
|
|
19
|
+
lineType = "bezier";
|
|
20
|
+
state = null;
|
|
21
|
+
constructor(host) {
|
|
22
|
+
this.host = host;
|
|
23
|
+
}
|
|
24
|
+
/** The connection being drawn, or null. */
|
|
25
|
+
current() {
|
|
26
|
+
return this.state;
|
|
27
|
+
}
|
|
28
|
+
/** The edge new ends would make. */
|
|
29
|
+
ends(from, to) {
|
|
30
|
+
const [source, target] = from.type === "source" ? [from, to] : [to, from];
|
|
31
|
+
return {
|
|
32
|
+
source: source.nodeId,
|
|
33
|
+
target: target.nodeId,
|
|
34
|
+
...source.handleId === void 0 ? {} : { sourceHandle: source.handleId },
|
|
35
|
+
...target.handleId === void 0 ? {} : { targetHandle: target.handleId }
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
valid(start, to) {
|
|
39
|
+
const ends = this.ends(start.from, to);
|
|
40
|
+
return start.reconnecting ? this.host.model.can("edge.reconnect", {
|
|
41
|
+
edgeId: start.reconnecting.edgeId,
|
|
42
|
+
...ends
|
|
43
|
+
}) : this.host.model.can("edge.add", ends);
|
|
44
|
+
}
|
|
45
|
+
/** Whether a connection from `from` may end at `handle` (the model not asked yet). */
|
|
46
|
+
eligible(from, handle) {
|
|
47
|
+
if (!handle.center || !handle.connectableEnd) return false;
|
|
48
|
+
if (handle.nodeId === from.nodeId && handle.type === from.type && handle.handleId === from.handleId) return false;
|
|
49
|
+
if (this.mode === "strict" && handle.type === from.type) return false;
|
|
50
|
+
return this.host.model.is("node-connectable", { nodeId: handle.nodeId });
|
|
51
|
+
}
|
|
52
|
+
/** The eligible handle nearest to a flow point, within the radius. */
|
|
53
|
+
nearest(start, point) {
|
|
54
|
+
const { from } = start;
|
|
55
|
+
const reach = this.radius / this.host.viewport().zoom;
|
|
56
|
+
let best;
|
|
57
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
58
|
+
for (const handle of this.host.handles()) {
|
|
59
|
+
if (!handle.center || !this.eligible(from, handle)) continue;
|
|
60
|
+
const d = Math.hypot(handle.center.x - point.x, handle.center.y - point.y);
|
|
61
|
+
if (d <= reach && d < bestDistance) {
|
|
62
|
+
best = handle;
|
|
63
|
+
bestDistance = d;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return best;
|
|
67
|
+
}
|
|
68
|
+
/** The connection state of a keyboard connection from `from` pointing at `to` (or nowhere). */
|
|
69
|
+
aimAt(from, to) {
|
|
70
|
+
const toPoint = to?.center ?? from.center;
|
|
71
|
+
const toSide = to?.side ?? oppositeSide(from.side);
|
|
72
|
+
const path = this.linePath(from.center, from.side, toPoint, toSide);
|
|
73
|
+
this.state = Object.freeze({
|
|
74
|
+
from,
|
|
75
|
+
fromPoint: from.center,
|
|
76
|
+
toPoint,
|
|
77
|
+
toSide,
|
|
78
|
+
to,
|
|
79
|
+
status: to ? "valid" : null,
|
|
80
|
+
path,
|
|
81
|
+
reconnecting: void 0
|
|
82
|
+
});
|
|
83
|
+
this.host.changed();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A connection drawn with the keyboard from a measured handle (`keyboard()` with
|
|
87
|
+
* `connection()`): its targets are the eligible handles `model.can("edge.add", …)` accepts
|
|
88
|
+
* (a vetoed one is skipped), nearest to it first, read again on every step (a handle rendered
|
|
89
|
+
* meanwhile is offered, a rule that changed applies); it points at the first, `next` walks them
|
|
90
|
+
* round from the one pointed at (found again by node, handle and type; gone, the one that took its
|
|
91
|
+
* place), `connect` adds the edge to the one pointed at. Nothing when a connection is under way.
|
|
92
|
+
*/
|
|
93
|
+
keyboard(from) {
|
|
94
|
+
const center = from.center;
|
|
95
|
+
if (this.state !== null || !center) return void 0;
|
|
96
|
+
const start = { from };
|
|
97
|
+
const find = () => this.host.handles().filter((handle) => this.eligible(from, handle) && this.valid(start, handle)).map((handle) => ({
|
|
98
|
+
handle,
|
|
99
|
+
distance: Math.hypot((handle.center?.x ?? 0) - center.x, (handle.center?.y ?? 0) - center.y)
|
|
100
|
+
})).sort((a, b) => a.distance - b.distance).map(({ handle }) => handle);
|
|
101
|
+
let targets = find();
|
|
102
|
+
const source = {
|
|
103
|
+
...from,
|
|
104
|
+
center
|
|
105
|
+
};
|
|
106
|
+
let index = targets.length > 0 ? 0 : -1;
|
|
107
|
+
let open = true;
|
|
108
|
+
const aim = () => {
|
|
109
|
+
const to = targets[index];
|
|
110
|
+
this.aimAt(source, to);
|
|
111
|
+
this.host.emit({
|
|
112
|
+
type: "connect-target",
|
|
113
|
+
nodeId: to?.nodeId,
|
|
114
|
+
handleId: to?.handleId,
|
|
115
|
+
handleType: to?.type,
|
|
116
|
+
index,
|
|
117
|
+
count: targets.length
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
this.started(start, "keyboard");
|
|
121
|
+
aim();
|
|
122
|
+
const finish = () => {
|
|
123
|
+
open = false;
|
|
124
|
+
this.state = null;
|
|
125
|
+
this.host.changed();
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
next: (by) => {
|
|
129
|
+
if (!open) return;
|
|
130
|
+
const pointed = targets[index];
|
|
131
|
+
targets = find();
|
|
132
|
+
const count = targets.length;
|
|
133
|
+
const at = targets.findIndex((handle) => handle.nodeId === pointed?.nodeId && handle.handleId === pointed.handleId && handle.type === pointed.type);
|
|
134
|
+
index = count === 0 ? -1 : ((at < 0 ? Math.max(index, 0) + Math.min(by, 0) : at + by) + count) % count;
|
|
135
|
+
aim();
|
|
136
|
+
},
|
|
137
|
+
target: () => open ? targets[index] : void 0,
|
|
138
|
+
connect: () => {
|
|
139
|
+
if (!open) return;
|
|
140
|
+
const to = targets[index];
|
|
141
|
+
finish();
|
|
142
|
+
if (to) {
|
|
143
|
+
const ends = this.ends(from, to);
|
|
144
|
+
const result = this.host.model.run("edge.add", ends);
|
|
145
|
+
if (result.ok) {
|
|
146
|
+
const { edgeId } = result.value;
|
|
147
|
+
this.host.emit({
|
|
148
|
+
type: "connect",
|
|
149
|
+
edgeId,
|
|
150
|
+
source: ends.source,
|
|
151
|
+
target: ends.target,
|
|
152
|
+
input: "keyboard"
|
|
153
|
+
});
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
this.cancelled(start, to !== void 0, "keyboard", this.keyboardDrop(source, to), true);
|
|
158
|
+
},
|
|
159
|
+
cancel: () => {
|
|
160
|
+
if (!open) return;
|
|
161
|
+
const to = targets[index];
|
|
162
|
+
finish();
|
|
163
|
+
this.cancelled(start, false, "keyboard", this.keyboardDrop(source, to), false);
|
|
164
|
+
},
|
|
165
|
+
open: () => open
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** The connection line's path (`lineType`), from the engine's path helpers. */
|
|
169
|
+
linePath(source, sourceSide, target, targetSide) {
|
|
170
|
+
return this.host.path(this.lineType, {
|
|
171
|
+
source,
|
|
172
|
+
sourceSide,
|
|
173
|
+
target,
|
|
174
|
+
targetSide
|
|
175
|
+
}).path;
|
|
176
|
+
}
|
|
177
|
+
update(start, client) {
|
|
178
|
+
const { from } = start;
|
|
179
|
+
const fromPoint = from.center ?? this.host.toFlow(client);
|
|
180
|
+
const pointer = this.host.toFlow(client);
|
|
181
|
+
const to = this.nearest(start, pointer);
|
|
182
|
+
const toPoint = to?.center ?? pointer;
|
|
183
|
+
const toSide = to?.side ?? oppositeSide(from.side);
|
|
184
|
+
const status = to ? this.valid(start, to) ? "valid" : "invalid" : null;
|
|
185
|
+
const path = this.linePath(fromPoint, from.side, toPoint, toSide);
|
|
186
|
+
this.state = Object.freeze({
|
|
187
|
+
from,
|
|
188
|
+
fromPoint,
|
|
189
|
+
toPoint,
|
|
190
|
+
toSide,
|
|
191
|
+
to,
|
|
192
|
+
status,
|
|
193
|
+
path,
|
|
194
|
+
reconnecting: start.reconnecting
|
|
195
|
+
});
|
|
196
|
+
this.host.changed();
|
|
197
|
+
}
|
|
198
|
+
started(start, input = "pointer") {
|
|
199
|
+
if (start.reconnecting) this.host.emit({
|
|
200
|
+
type: "reconnect-start",
|
|
201
|
+
edgeId: start.reconnecting.edgeId,
|
|
202
|
+
end: start.reconnecting.end
|
|
203
|
+
});
|
|
204
|
+
else this.host.emit({
|
|
205
|
+
type: "connect-start",
|
|
206
|
+
nodeId: start.from.nodeId,
|
|
207
|
+
handleId: start.from.handleId,
|
|
208
|
+
handleType: start.from.type,
|
|
209
|
+
input
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* A gesture drawing a connection from `start`, pressed at `client`: it starts once the pointer
|
|
214
|
+
* moves (1 pixel, or past `tolerance` pixels: a press that moves less stays a tap); `following`,
|
|
215
|
+
* it has started (a tap began it: it follows the pointer until the next press's release, its
|
|
216
|
+
* drop).
|
|
217
|
+
*/
|
|
218
|
+
gesture(start, client, { following = false, tolerance, threshold = 1 } = {}) {
|
|
219
|
+
let active = following;
|
|
220
|
+
let last = client;
|
|
221
|
+
const finish = () => {
|
|
222
|
+
this.state = null;
|
|
223
|
+
this.host.changed();
|
|
224
|
+
};
|
|
225
|
+
if (following) {
|
|
226
|
+
this.started(start);
|
|
227
|
+
this.update(start, client);
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
autoPan: "connect",
|
|
231
|
+
kind: start.reconnecting ? "reconnect" : "connect",
|
|
232
|
+
move: (event) => {
|
|
233
|
+
const point = {
|
|
234
|
+
x: event.clientX,
|
|
235
|
+
y: event.clientY
|
|
236
|
+
};
|
|
237
|
+
last = point;
|
|
238
|
+
if (!active) {
|
|
239
|
+
const travel = Math.hypot(point.x - client.x, point.y - client.y);
|
|
240
|
+
if (tolerance === void 0 ? travel < threshold : travel <= tolerance) return;
|
|
241
|
+
active = true;
|
|
242
|
+
this.started(start);
|
|
243
|
+
}
|
|
244
|
+
this.update(start, point);
|
|
245
|
+
},
|
|
246
|
+
end: (event) => {
|
|
247
|
+
if (!active) return;
|
|
248
|
+
const drop = {
|
|
249
|
+
x: event.clientX,
|
|
250
|
+
y: event.clientY
|
|
251
|
+
};
|
|
252
|
+
this.update(start, drop);
|
|
253
|
+
const state = this.state;
|
|
254
|
+
finish();
|
|
255
|
+
const to = state?.to;
|
|
256
|
+
if (to && state?.status === "valid") {
|
|
257
|
+
const ends = this.ends(start.from, to);
|
|
258
|
+
if (start.reconnecting) {
|
|
259
|
+
const { edgeId } = start.reconnecting;
|
|
260
|
+
if (this.host.model.run("edge.reconnect", {
|
|
261
|
+
edgeId,
|
|
262
|
+
...ends
|
|
263
|
+
}).ok) {
|
|
264
|
+
this.host.emit({
|
|
265
|
+
type: "reconnect",
|
|
266
|
+
edgeId,
|
|
267
|
+
source: ends.source,
|
|
268
|
+
target: ends.target
|
|
269
|
+
});
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
const result = this.host.model.run("edge.add", ends);
|
|
274
|
+
if (result.ok) {
|
|
275
|
+
const { edgeId } = result.value;
|
|
276
|
+
this.host.emit({
|
|
277
|
+
type: "connect",
|
|
278
|
+
edgeId,
|
|
279
|
+
source: ends.source,
|
|
280
|
+
target: ends.target,
|
|
281
|
+
input: "pointer"
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
this.cancelled(start, to !== void 0, "pointer", {
|
|
288
|
+
to,
|
|
289
|
+
client: drop,
|
|
290
|
+
point: this.host.toFlow(drop)
|
|
291
|
+
}, true);
|
|
292
|
+
},
|
|
293
|
+
cancel: () => {
|
|
294
|
+
if (!active) return;
|
|
295
|
+
const to = this.state?.to;
|
|
296
|
+
finish();
|
|
297
|
+
this.cancelled(start, false, "pointer", {
|
|
298
|
+
to,
|
|
299
|
+
client: last,
|
|
300
|
+
point: this.host.toFlow(last)
|
|
301
|
+
}, false);
|
|
302
|
+
},
|
|
303
|
+
active: () => active
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Tells a connection ended without an edge, with where it ended: the handle it was over (if
|
|
308
|
+
* any) and the drop point, as a client point and a flow point; `dropped` when it was released
|
|
309
|
+
* (or confirmed), not cancelled (Escape, the lock, a `pointercancel`).
|
|
310
|
+
*/
|
|
311
|
+
cancelled(start, refused, input, at, dropped) {
|
|
312
|
+
const drop = {
|
|
313
|
+
dropped,
|
|
314
|
+
from: endOf(start.from),
|
|
315
|
+
to: at.to ? endOf(at.to) : void 0,
|
|
316
|
+
client: at.client,
|
|
317
|
+
point: at.point
|
|
318
|
+
};
|
|
319
|
+
if (start.reconnecting) this.host.emit({
|
|
320
|
+
type: "reconnect-cancel",
|
|
321
|
+
edgeId: start.reconnecting.edgeId,
|
|
322
|
+
end: start.reconnecting.end,
|
|
323
|
+
refused,
|
|
324
|
+
...drop
|
|
325
|
+
});
|
|
326
|
+
else this.host.emit({
|
|
327
|
+
type: "connect-cancel",
|
|
328
|
+
refused,
|
|
329
|
+
input,
|
|
330
|
+
...drop
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** Where a keyboard connection pointing at `to` ends: its target's centre, or its start's. */
|
|
334
|
+
keyboardDrop(from, to) {
|
|
335
|
+
const point = to?.center ?? from.center;
|
|
336
|
+
return {
|
|
337
|
+
to,
|
|
338
|
+
point,
|
|
339
|
+
client: this.host.toClient(point)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
/** The state attributes of the connection's parts. */
|
|
343
|
+
attributes(part, payload, into) {
|
|
344
|
+
const state = this.state;
|
|
345
|
+
if (part === "root") {
|
|
346
|
+
into["data-connecting"] = flag(state !== null);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (part === "connection-line") {
|
|
350
|
+
into["data-valid"] = flag(state?.status === "valid");
|
|
351
|
+
into["data-invalid"] = flag(state?.status === "invalid");
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (part === "handle") {
|
|
355
|
+
const is = (handle) => handle !== void 0 && handle.nodeId === payload.nodeId && handle.type === payload.type && handle.handleId === payload.handleId;
|
|
356
|
+
into["data-connecting"] = flag(state !== null && is(state.from));
|
|
357
|
+
const target = state !== null && is(state.to);
|
|
358
|
+
into["data-valid"] = flag(target && state?.status === "valid");
|
|
359
|
+
into["data-invalid"] = flag(target && state?.status === "invalid");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (part === "edge" || part === "edge-path" || part === "reconnect-anchor") into["data-reconnecting"] = flag(state?.reconnecting !== void 0 && state.reconnecting.edgeId === payload.edgeId);
|
|
363
|
+
}
|
|
364
|
+
/** Ends the connection with nothing committed (the root went away). */
|
|
365
|
+
reset() {
|
|
366
|
+
this.state = null;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
/** The session of an engine, made by the first feature that asks. */
|
|
370
|
+
function connectSessionOf(host) {
|
|
371
|
+
return host.shared("connect-session", () => new ConnectSession(host));
|
|
372
|
+
}
|
|
373
|
+
export { connectSessionOf as t };
|
|
374
|
+
|
|
375
|
+
//# sourceMappingURL=connect-session-nyEp02_P.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-session-nyEp02_P.js","names":[],"sources":["../src/connect-session/connect-session.ts"],"sourcesContent":["// The connection under way, shared by `connection()` and `reconnection()` of one engine (through\n// `host.shared`, so neither imports the other): it starts at a handle (a new edge) or at an edge's\n// fixed end (a reconnection), follows the pointer, snaps to the nearest eligible handle within the\n// radius, and asks the model whether the drop would be taken (D7: `model.can(\"edge.add\", …)` or\n// `model.can(\"edge.reconnect\", …)`, so a middleware's veto marks the handle invalid). Its drop runs\n// the command; nothing is committed before it. An internal folder with no entry: only the two\n// features' entries import it.\nimport {\n flag,\n type MutablePartAttributes,\n} from \"../attributes/part-attributes\";\nimport type { ConnectionPayload } from \"../commands/types\";\nimport type { BuiltInEdgeType, XY } from \"../document/types\";\nimport type { FlowHandle } from \"../engine/adapter\";\nimport type { ConnectionEnd, MoveSource } from \"../engine/events\";\nimport type { Connection } from \"../engine/verbs\";\nimport type { KeyboardConnection } from \"../features/keyboard-links\";\nimport type { FeatureEngineHost, FeatureGesture } from \"../features/types\";\nimport { oppositeSide } from \"../geometry/path-format\";\nimport type { HandleSide } from \"../geometry/types\";\n\n/** Which handles a connection may end at (`connection({ mode })`). */\nexport type ConnectMode = \"strict\" | \"loose\";\n\n/** What a connection starts from: a handle, or (a reconnection) an edge's fixed end. */\nexport interface ConnectStart {\n readonly from: FlowHandle;\n readonly reconnecting?:\n | { readonly edgeId: string; readonly end: \"source\" | \"target\" }\n | undefined;\n}\n\n/** How a connection gesture starts (`ConnectSession.gesture`). */\nexport interface GestureOptions {\n /** it has started already: a tap began it */\n readonly following?: boolean | undefined;\n /** it starts only once the pointer moved past this many pixels (a tap's distance) */\n readonly tolerance?: number | undefined;\n /** without `tolerance`, it starts once the pointer moved this many pixels; default 1 */\n readonly threshold?: number | undefined;\n}\n\n/** A handle as a connection's events name it. */\nfunction endOf(handle: FlowHandle): ConnectionEnd {\n return {\n nodeId: handle.nodeId,\n handleId: handle.handleId,\n type: handle.type,\n };\n}\n\n/** The shared connection state of one engine. */\nexport class ConnectSession {\n /** how near (in pixels) a handle snaps; set by `connection()` */\n radius = 20;\n mode: ConnectMode = \"strict\";\n /** the connection line's path (`connection({ lineType })`) */\n lineType: BuiltInEdgeType = \"bezier\";\n private state: Connection | null = null;\n\n constructor(private readonly host: FeatureEngineHost) {}\n\n /** The connection being drawn, or null. */\n current(): Connection | null {\n return this.state;\n }\n\n /** The edge new ends would make. */\n private ends(from: FlowHandle, to: FlowHandle): ConnectionPayload {\n const [source, target] =\n from.type === \"source\" ? [from, to] : [to, from];\n return {\n source: source.nodeId,\n target: target.nodeId,\n ...(source.handleId === undefined\n ? {}\n : { sourceHandle: source.handleId }),\n ...(target.handleId === undefined\n ? {}\n : { targetHandle: target.handleId }),\n };\n }\n\n private valid(start: ConnectStart, to: FlowHandle): boolean {\n const ends = this.ends(start.from, to);\n return start.reconnecting\n ? this.host.model.can(\"edge.reconnect\", {\n edgeId: start.reconnecting.edgeId,\n ...ends,\n })\n : this.host.model.can(\"edge.add\", ends);\n }\n\n /** Whether a connection from `from` may end at `handle` (the model not asked yet). */\n private eligible(from: FlowHandle, handle: FlowHandle): boolean {\n if (!handle.center || !handle.connectableEnd) return false;\n const same =\n handle.nodeId === from.nodeId &&\n handle.type === from.type &&\n handle.handleId === from.handleId;\n if (same) return false;\n if (this.mode === \"strict\" && handle.type === from.type) return false;\n return this.host.model.is(\"node-connectable\", {\n nodeId: handle.nodeId,\n });\n }\n\n /** The eligible handle nearest to a flow point, within the radius. */\n private nearest(start: ConnectStart, point: XY): FlowHandle | undefined {\n const { from } = start;\n const reach = this.radius / this.host.viewport().zoom;\n let best: FlowHandle | undefined;\n let bestDistance = Number.POSITIVE_INFINITY;\n for (const handle of this.host.handles()) {\n if (!handle.center || !this.eligible(from, handle)) continue;\n const d = Math.hypot(\n handle.center.x - point.x,\n handle.center.y - point.y,\n );\n if (d <= reach && d < bestDistance) {\n best = handle;\n bestDistance = d;\n }\n }\n return best;\n }\n\n /** The connection state of a keyboard connection from `from` pointing at `to` (or nowhere). */\n private aimAt(\n from: FlowHandle & { center: XY },\n to: FlowHandle | undefined,\n ): void {\n const toPoint = to?.center ?? from.center;\n const toSide = to?.side ?? oppositeSide(from.side);\n const path = this.linePath(from.center, from.side, toPoint, toSide);\n this.state = Object.freeze({\n from,\n fromPoint: from.center,\n toPoint,\n toSide,\n to,\n status: to ? \"valid\" : null,\n path,\n reconnecting: undefined,\n });\n this.host.changed();\n }\n\n /**\n * A connection drawn with the keyboard from a measured handle (`keyboard()` with\n * `connection()`): its targets are the eligible handles `model.can(\"edge.add\", …)` accepts\n * (a vetoed one is skipped), nearest to it first, read again on every step (a handle rendered\n * meanwhile is offered, a rule that changed applies); it points at the first, `next` walks them\n * round from the one pointed at (found again by node, handle and type; gone, the one that took its\n * place), `connect` adds the edge to the one pointed at. Nothing when a connection is under way.\n */\n keyboard(from: FlowHandle): KeyboardConnection | undefined {\n const center = from.center;\n if (this.state !== null || !center) return undefined;\n const start: ConnectStart = { from };\n const find = () =>\n this.host\n .handles()\n .filter(\n (handle) =>\n this.eligible(from, handle) &&\n this.valid(start, handle),\n )\n .map((handle) => ({\n handle,\n distance: Math.hypot(\n (handle.center?.x ?? 0) - center.x,\n (handle.center?.y ?? 0) - center.y,\n ),\n }))\n .sort((a, b) => a.distance - b.distance)\n .map(({ handle }) => handle);\n let targets = find();\n const source = { ...from, center };\n let index = targets.length > 0 ? 0 : -1;\n let open = true;\n const aim = () => {\n const to = targets[index];\n this.aimAt(source, to);\n this.host.emit({\n type: \"connect-target\",\n nodeId: to?.nodeId,\n handleId: to?.handleId,\n handleType: to?.type,\n index,\n count: targets.length,\n });\n };\n this.started(start, \"keyboard\");\n aim();\n const finish = () => {\n open = false;\n this.state = null;\n this.host.changed();\n };\n return {\n next: (by) => {\n if (!open) return;\n const pointed = targets[index];\n targets = find();\n const count = targets.length;\n const at = targets.findIndex(\n (handle) =>\n handle.nodeId === pointed?.nodeId &&\n handle.handleId === pointed.handleId &&\n handle.type === pointed.type,\n );\n // gone: the step lands on the one that took its place\n index =\n count === 0\n ? -1\n : ((at < 0\n ? Math.max(index, 0) + Math.min(by, 0)\n : at + by) +\n count) %\n count;\n aim();\n },\n target: () => (open ? targets[index] : undefined),\n connect: () => {\n if (!open) return;\n const to = targets[index];\n finish();\n if (to) {\n const ends = this.ends(from, to);\n const result = this.host.model.run(\"edge.add\", ends);\n if (result.ok) {\n const { edgeId } = result.value as { edgeId: string };\n this.host.emit({\n type: \"connect\",\n edgeId,\n source: ends.source,\n target: ends.target,\n input: \"keyboard\",\n });\n return;\n }\n }\n this.cancelled(\n start,\n to !== undefined,\n \"keyboard\",\n this.keyboardDrop(source, to),\n true,\n );\n },\n cancel: () => {\n if (!open) return;\n const to = targets[index];\n finish();\n this.cancelled(\n start,\n false,\n \"keyboard\",\n this.keyboardDrop(source, to),\n false,\n );\n },\n open: () => open,\n };\n }\n\n /** The connection line's path (`lineType`), from the engine's path helpers. */\n private linePath(\n source: XY,\n sourceSide: HandleSide,\n target: XY,\n targetSide: HandleSide,\n ): string {\n return this.host.path(this.lineType, {\n source,\n sourceSide,\n target,\n targetSide,\n }).path;\n }\n\n private update(start: ConnectStart, client: XY): void {\n const { from } = start;\n const fromPoint = from.center ?? this.host.toFlow(client);\n const pointer = this.host.toFlow(client);\n const to = this.nearest(start, pointer);\n const toPoint = to?.center ?? pointer;\n const toSide = to?.side ?? oppositeSide(from.side);\n const status = to\n ? this.valid(start, to)\n ? \"valid\"\n : \"invalid\"\n : null;\n const path = this.linePath(fromPoint, from.side, toPoint, toSide);\n this.state = Object.freeze({\n from,\n fromPoint,\n toPoint,\n toSide,\n to,\n status,\n path,\n reconnecting: start.reconnecting,\n });\n this.host.changed();\n }\n\n private started(start: ConnectStart, input: MoveSource = \"pointer\"): void {\n if (start.reconnecting) {\n this.host.emit({\n type: \"reconnect-start\",\n edgeId: start.reconnecting.edgeId,\n end: start.reconnecting.end,\n });\n } else {\n this.host.emit({\n type: \"connect-start\",\n nodeId: start.from.nodeId,\n handleId: start.from.handleId,\n handleType: start.from.type,\n input,\n });\n }\n }\n\n /**\n * A gesture drawing a connection from `start`, pressed at `client`: it starts once the pointer\n * moves (1 pixel, or past `tolerance` pixels: a press that moves less stays a tap); `following`,\n * it has started (a tap began it: it follows the pointer until the next press's release, its\n * drop).\n */\n gesture(\n start: ConnectStart,\n client: XY,\n { following = false, tolerance, threshold = 1 }: GestureOptions = {},\n ): FeatureGesture {\n let active = following;\n // where the pointer was last: a cancel without a release (Escape) ends there\n let last = client;\n const finish = () => {\n this.state = null;\n this.host.changed();\n };\n if (following) {\n this.started(start);\n this.update(start, client);\n }\n return {\n autoPan: \"connect\",\n kind: start.reconnecting ? \"reconnect\" : \"connect\",\n move: (event) => {\n const point = { x: event.clientX, y: event.clientY };\n last = point;\n if (!active) {\n const travel = Math.hypot(\n point.x - client.x,\n point.y - client.y,\n );\n if (\n tolerance === undefined\n ? travel < threshold\n : travel <= tolerance\n )\n return;\n active = true;\n this.started(start);\n }\n this.update(start, point);\n },\n end: (event) => {\n if (!active) return;\n const drop = { x: event.clientX, y: event.clientY };\n this.update(start, drop);\n const state = this.state;\n finish();\n const to = state?.to;\n if (to && state?.status === \"valid\") {\n const ends = this.ends(start.from, to);\n if (start.reconnecting) {\n const { edgeId } = start.reconnecting;\n const result = this.host.model.run(\"edge.reconnect\", {\n edgeId,\n ...ends,\n });\n if (result.ok) {\n this.host.emit({\n type: \"reconnect\",\n edgeId,\n source: ends.source,\n target: ends.target,\n });\n return;\n }\n } else {\n const result = this.host.model.run(\"edge.add\", ends);\n if (result.ok) {\n const { edgeId } = result.value as {\n edgeId: string;\n };\n this.host.emit({\n type: \"connect\",\n edgeId,\n source: ends.source,\n target: ends.target,\n input: \"pointer\",\n });\n return;\n }\n }\n }\n this.cancelled(\n start,\n to !== undefined,\n \"pointer\",\n { to, client: drop, point: this.host.toFlow(drop) },\n true,\n );\n },\n cancel: () => {\n if (!active) return;\n const to = this.state?.to;\n finish();\n this.cancelled(\n start,\n false,\n \"pointer\",\n { to, client: last, point: this.host.toFlow(last) },\n false,\n );\n },\n active: () => active,\n };\n }\n\n /**\n * Tells a connection ended without an edge, with where it ended: the handle it was over (if\n * any) and the drop point, as a client point and a flow point; `dropped` when it was released\n * (or confirmed), not cancelled (Escape, the lock, a `pointercancel`).\n */\n private cancelled(\n start: ConnectStart,\n refused: boolean,\n input: MoveSource,\n at: { to: FlowHandle | undefined; client: XY; point: XY },\n dropped: boolean,\n ): void {\n const drop = {\n dropped,\n from: endOf(start.from),\n to: at.to ? endOf(at.to) : undefined,\n client: at.client,\n point: at.point,\n };\n if (start.reconnecting) {\n this.host.emit({\n type: \"reconnect-cancel\",\n edgeId: start.reconnecting.edgeId,\n end: start.reconnecting.end,\n refused,\n ...drop,\n });\n } else {\n this.host.emit({ type: \"connect-cancel\", refused, input, ...drop });\n }\n }\n\n /** Where a keyboard connection pointing at `to` ends: its target's centre, or its start's. */\n private keyboardDrop(from: FlowHandle & { center: XY }, to?: FlowHandle) {\n const point = to?.center ?? from.center;\n return { to, point, client: this.host.toClient(point) };\n }\n\n /** The state attributes of the connection's parts. */\n attributes(\n part: string,\n payload: Readonly<Record<string, unknown>>,\n into: MutablePartAttributes,\n ): void {\n const state = this.state;\n if (part === \"root\") {\n into[\"data-connecting\"] = flag(state !== null);\n return;\n }\n if (part === \"connection-line\") {\n into[\"data-valid\"] = flag(state?.status === \"valid\");\n into[\"data-invalid\"] = flag(state?.status === \"invalid\");\n return;\n }\n if (part === \"handle\") {\n const is = (handle: FlowHandle | undefined) =>\n handle !== undefined &&\n handle.nodeId === payload.nodeId &&\n handle.type === payload.type &&\n handle.handleId === payload.handleId;\n into[\"data-connecting\"] = flag(state !== null && is(state.from));\n const target = state !== null && is(state.to);\n into[\"data-valid\"] = flag(target && state?.status === \"valid\");\n into[\"data-invalid\"] = flag(target && state?.status === \"invalid\");\n return;\n }\n if (\n part === \"edge\" ||\n part === \"edge-path\" ||\n part === \"reconnect-anchor\"\n ) {\n into[\"data-reconnecting\"] = flag(\n state?.reconnecting !== undefined &&\n state.reconnecting.edgeId === payload.edgeId,\n );\n }\n }\n\n /** Ends the connection with nothing committed (the root went away). */\n reset(): void {\n this.state = null;\n }\n}\n\n/** The session of an engine, made by the first feature that asks. */\nexport function connectSessionOf(host: FeatureEngineHost): ConnectSession {\n return host.shared(\"connect-session\", () => new ConnectSession(host));\n}\n"],"mappings":";;;;AA2CA,SAAS,MAAM,QAAmC;CAC9C,OAAO;EACH,QAAQ,OAAO;EACf,UAAU,OAAO;EACjB,MAAM,OAAO;CACjB;AACJ;;AAGA,IAAa,iBAAb,MAA4B;CAQK;;CAN7B,SAAS;CACT,OAAoB;;CAEpB,WAA4B;CAC5B,QAAmC;CAEnC,YAAY,MAA0C;EAAzB,KAAA,OAAA;CAA0B;;CAGvD,UAA6B;EACzB,OAAO,KAAK;CAChB;;CAGA,KAAa,MAAkB,IAAmC;EAC9D,MAAM,CAAC,QAAQ,UACX,KAAK,SAAS,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,IAAI;EACnD,OAAO;GACH,QAAQ,OAAO;GACf,QAAQ,OAAO;GACf,GAAI,OAAO,aAAa,KAAA,IAClB,CAAC,IACD,EAAE,cAAc,OAAO,SAAS;GACtC,GAAI,OAAO,aAAa,KAAA,IAClB,CAAC,IACD,EAAE,cAAc,OAAO,SAAS;EAC1C;CACJ;CAEA,MAAc,OAAqB,IAAyB;EACxD,MAAM,OA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|
+
* Which handles a connection may end at: only a target handle when it started at a source one
|
|
4
|
+
* (`"strict"`), or any handle of another node (`"loose"`).
|
|
5
|
+
*/
|
|
6
|
+
type ConnectionMode = "strict" | "loose";
|
|
7
|
+
/** Options of {@link connection}: read by its engine side. */
|
|
8
|
+
interface ConnectionOptions {
|
|
9
|
+
/** how near (in pixels) a handle must be for a drop to snap to it; default 20 */
|
|
10
|
+
readonly radius: number;
|
|
11
|
+
/** which handles a connection may end at; default `"strict"` */
|
|
12
|
+
readonly mode: ConnectionMode;
|
|
13
|
+
/**
|
|
14
|
+
* a tap on a handle starts a connection that follows the pointer, and a tap on another handle
|
|
15
|
+
* ends it (elsewhere, or Escape, cancels it); default false
|
|
16
|
+
*/
|
|
17
|
+
readonly clickToConnect: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* how far (in pixels) a press on a handle moves before it draws a connection; default 1
|
|
20
|
+
* (with `clickToConnect`, a press within the tap distance stays a tap whatever it is)
|
|
21
|
+
*/
|
|
22
|
+
readonly threshold: number;
|
|
23
|
+
/** the connection line's path, one of the built-in edge types; default `"bezier"` */
|
|
24
|
+
readonly lineType: BuiltInEdgeType;
|
|
25
|
+
}
|
|
26
|
+
/** The connection feature, as `connection()` makes it. */
|
|
27
|
+
type ConnectionFeature = FlowFeature<"connection", ConnectionOptions>;
|
|
28
|
+
/** Drawing new edges from handles: `features: [connection()]` (`@fragiola/flow/connection`). */
|
|
29
|
+
export declare function connection(options?: Partial<ConnectionOptions>): ConnectionFeature;
|
|
30
|
+
export type { ConnectionFeature, ConnectionMode, ConnectionOptions };
|
|
31
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { t as connectSessionOf } from "./connect-session-nyEp02_P.js";
|
|
2
|
+
import { t as keyboardLinks } from "./keyboard-links-CVaTtlZg.js";
|
|
3
|
+
// src/connection/connection-engine.ts
|
|
4
|
+
/** The engine side of `connection()`. */
|
|
5
|
+
function connectionEngine(host, options) {
|
|
6
|
+
const session = connectSessionOf(host);
|
|
7
|
+
session.radius = options.radius;
|
|
8
|
+
session.mode = options.mode;
|
|
9
|
+
session.lineType = options.lineType;
|
|
10
|
+
/** The handle a press or a tap on `target` starts a connection from, if it may. */
|
|
11
|
+
const startable = (target) => {
|
|
12
|
+
const handle = target.handle;
|
|
13
|
+
return target.kind === "handle" && handle?.connectableStart === true && host.allows("connect") && session.current() === null && host.model.is("node-connectable", { nodeId: handle.nodeId }) ? handle : void 0;
|
|
14
|
+
};
|
|
15
|
+
keyboardLinks(host).connect = { start: (from) => startable({
|
|
16
|
+
kind: "handle",
|
|
17
|
+
element: void 0,
|
|
18
|
+
handle: from
|
|
19
|
+
}) ? session.keyboard(from) : void 0 };
|
|
20
|
+
return {
|
|
21
|
+
get: { connection: () => session.current() },
|
|
22
|
+
is: { connecting: () => session.current() !== null },
|
|
23
|
+
pointerdown: (event, target) => {
|
|
24
|
+
const handle = startable(target);
|
|
25
|
+
return handle ? session.gesture({ from: handle }, {
|
|
26
|
+
x: event.clientX,
|
|
27
|
+
y: event.clientY
|
|
28
|
+
}, options.clickToConnect ? { tolerance: Math.max(host.tapDistance("node"), options.threshold) } : { threshold: options.threshold }) : void 0;
|
|
29
|
+
},
|
|
30
|
+
tap: (event, target) => {
|
|
31
|
+
if (!options.clickToConnect) return void 0;
|
|
32
|
+
const handle = startable(target);
|
|
33
|
+
return handle ? session.gesture({ from: handle }, {
|
|
34
|
+
x: event.clientX,
|
|
35
|
+
y: event.clientY
|
|
36
|
+
}, { following: true }) : void 0;
|
|
37
|
+
},
|
|
38
|
+
attributes: (part, payload, into) => session.attributes(part, payload, into),
|
|
39
|
+
detached: () => session.reset()
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// src/connection/connection.ts
|
|
43
|
+
/** Drawing new edges from handles: `features: [connection()]` (`@fragiola/flow/connection`). */
|
|
44
|
+
function connection(options = {}) {
|
|
45
|
+
const resolved = Object.freeze({
|
|
46
|
+
radius: options.radius ?? 20,
|
|
47
|
+
mode: options.mode ?? "strict",
|
|
48
|
+
clickToConnect: options.clickToConnect ?? false,
|
|
49
|
+
threshold: options.threshold ?? 1,
|
|
50
|
+
lineType: options.lineType ?? "bezier"
|
|
51
|
+
});
|
|
52
|
+
return Object.freeze({
|
|
53
|
+
name: "connection",
|
|
54
|
+
options: resolved,
|
|
55
|
+
engine: (host) => connectionEngine(host, resolved)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
export { connection };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","names":[],"sources":["../src/connection/connection-engine.ts","../src/connection/connection.ts"],"sourcesContent":["// The connection feature's engine side (D7, D10): a press on a connectable handle that moves\n// draws a connection line (`engine.get(\"connection\")`); it snaps to the nearest eligible handle\n// within `radius`, valid when `model.can(\"edge.add\", …)`, and a drop there adds the edge. With\n// `clickToConnect`, a tap on a connectable handle starts the same connection without a press: the\n// engine moves it with the pointer, and the next press's release is its drop (a tap on another\n// handle connects; elsewhere it is cancelled, as Escape does). With `keyboard()`, a focused handle\n// starts a keyboard connection (its keys are the keyboard's; the session's `keyboard`).\nimport { connectSessionOf } from \"../connect-session/connect-session\";\nimport type { FlowHandle, PressTarget } from \"../engine/adapter\";\nimport { keyboardLinks } from \"../features/keyboard-links\";\nimport type { FeatureEngine, FeatureEngineHost } from \"../features/types\";\nimport type { ConnectionOptions } from \"./connection\";\n\n/** The engine side of `connection()`. */\nexport function connectionEngine(\n host: FeatureEngineHost,\n options: ConnectionOptions,\n): FeatureEngine {\n const session = connectSessionOf(host);\n session.radius = options.radius;\n session.mode = options.mode;\n session.lineType = options.lineType;\n\n /** The handle a press or a tap on `target` starts a connection from, if it may. */\n const startable = (target: PressTarget): FlowHandle | undefined => {\n const handle = target.handle;\n return target.kind === \"handle\" &&\n handle?.connectableStart === true &&\n host.allows(\"connect\") &&\n session.current() === null &&\n host.model.is(\"node-connectable\", { nodeId: handle.nodeId })\n ? handle\n : undefined;\n };\n\n // with keyboard(): Enter on a focused handle draws a connection from it\n keyboardLinks(host).connect = {\n start: (from) =>\n startable({ kind: \"handle\", element: undefined, handle: from })\n ? session.keyboard(from)\n : undefined,\n };\n\n return {\n get: { connection: () => session.current() },\n is: { connecting: () => session.current() !== null },\n pointerdown: (event, target) => {\n const handle = startable(target);\n // with click-to-connect, a press that barely moves stays a tap (it starts one)\n return handle\n ? session.gesture(\n { from: handle },\n { x: event.clientX, y: event.clientY },\n options.clickToConnect\n ? {\n tolerance: Math.max(\n host.tapDistance(\"node\"),\n options.threshold,\n ),\n }\n : { threshold: options.threshold },\n )\n : undefined;\n },\n tap: (event, target) => {\n if (!options.clickToConnect) return undefined;\n const handle = startable(target);\n return handle\n ? session.gesture(\n { from: handle },\n { x: event.clientX, y: event.clientY },\n { following: true },\n )\n : undefined;\n },\n attributes: (part, payload, into) =>\n session.attributes(part, payload, into),\n detached: () => session.reset(),\n };\n}\n","// The connection feature (D2, `@fragiola/flow/connection`): dragging from a handle draws a\n// connection line, and dropping it on another handle adds an edge (with `clickToConnect`, a tap on\n// a handle and a tap on another do too). Its validity is the core's\n// `model.can(\"edge.add\", …)` (D7: a middleware's veto marks the target invalid and refuses the\n// drop). Its engine side (`connection-engine.ts`: the gesture, the line's points, the nearest\n// handle within `radius`, the `connection` engine key) reads `options`.\nimport type { BuiltInEdgeType } from \"../document/types\";\nimport type { FlowFeature } from \"../features/types\";\nimport { connectionEngine } from \"./connection-engine\";\n\n/**\n * Which handles a connection may end at: only a target handle when it started at a source one\n * (`\"strict\"`), or any handle of another node (`\"loose\"`).\n */\nexport type ConnectionMode = \"strict\" | \"loose\";\n\n/** Options of {@link connection}: read by its engine side. */\nexport interface ConnectionOptions {\n /** how near (in pixels) a handle must be for a drop to snap to it; default 20 */\n readonly radius: number;\n /** which handles a connection may end at; default `\"strict\"` */\n readonly mode: ConnectionMode;\n /**\n * a tap on a handle starts a connection that follows the pointer, and a tap on another handle\n * ends it (elsewhere, or Escape, cancels it); default false\n */\n readonly clickToConnect: boolean;\n /**\n * how far (in pixels) a press on a handle moves before it draws a connection; default 1\n * (with `clickToConnect`, a press within the tap distance stays a tap whatever it is)\n */\n readonly threshold: number;\n /** the connection line's path, one of the built-in edge types; default `\"bezier\"` */\n readonly lineType: BuiltInEdgeType;\n}\n\n/** The connection feature, as `connection()` makes it. */\nexport type ConnectionFeature = FlowFeature<\"connection\", ConnectionOptions>;\n\n/** Drawing new edges from handles: `features: [connection()]` (`@fragiola/flow/connection`). */\nexport function connection(\n options: Partial<ConnectionOptions> = {},\n): ConnectionFeature {\n const resolved: ConnectionOptions = Object.freeze({\n radius: options.radius ?? 20,\n mode: options.mode ?? \"strict\",\n clickToConnect: options.clickToConnect ?? false,\n threshold: options.threshold ?? 1,\n lineType: options.lineType ?? \"bezier\",\n });\n return Object.freeze({\n name: \"connection\",\n options: resolved,\n engine: (host) => connectionEngine(host, resolved),\n } satisfies ConnectionFeature);\n}\n"],"mappings":";;;;AAcA,SAAgB,iBACZ,MACA,SACa;CACb,MAAM,UAAU,iBAAiB,IAAI;CACrC,QAAQ,SAAS,QAAQ;CACzB,QAAQ,OAAO,QAAQ;CACvB,QAAQ,WAAW,QAAQ;;CAG3B,MAAM,aAAa,WAAgD;EAC/D,MAAM,SAAS,OAAO;EACtB,OAAO,OAAO,SAAS,YACnB,QAAQ,qBAAqB,QAC7B,KAAK,OAAO,SAAS,KACrB,QAAQ,QAAQ,MAAM,QACtB,KAAK,MAAM,GAAG,oBAAoB,EAAE,QAAQ,OAAO,OAAO,CAAC,IACzD,SACA,KAAA;CACV;CAGA,cAAc,IAAI,CAAC,CAAC,UAAU,EAC1B,QAAQ,SACJ,UAAU;EAAE,MAAM;EAAU,SAAS,KAAA;EAAW,QAAQ;CAAK,CAAC,IACxD,QAAQ,SAAS,IAAI,IACrB,KAAA,EACd;CAEA,OAAO;EACH,KAAK,EAAE,kBAAkB,QAAQ,QAAQ,EA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