@sessile/presets 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/LICENSE +373 -0
- package/dist/dragMerge.d.ts +108 -0
- package/dist/dragMerge.js +578 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/popover.d.ts +252 -0
- package/dist/popover.js +1144 -0
- package/dist/segmented.d.ts +68 -0
- package/dist/segmented.js +377 -0
- package/package.json +33 -3
- package/README.md +0 -3
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag-Merge N-primitive preset for Sessile.
|
|
3
|
+
*
|
|
4
|
+
* Manages draggable rounded-box glass primitives with exact rounded-box hit testing,
|
|
5
|
+
* spring-coupled inertia, soft magnetic attraction, elastic neighbor tugging,
|
|
6
|
+
* release snap/return behaviors, and pairwise smin proximity bridge reporting.
|
|
7
|
+
*
|
|
8
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
9
|
+
*/
|
|
10
|
+
import { clamp, computeRoundedBoxDistance, createSpringChannel, } from '@sessile/core';
|
|
11
|
+
/** Default smin blend, magnetic range, and spring constants for drag-merge fields. */
|
|
12
|
+
export const DRAG_MERGE_DEFAULTS = {
|
|
13
|
+
blendK: 32,
|
|
14
|
+
magneticRadiusPx: 48,
|
|
15
|
+
magneticStrength: 0.18,
|
|
16
|
+
neighborTugRatio: 0.14,
|
|
17
|
+
spring: { mass: 1, stiffness: 280, dampingRatio: 0.72 },
|
|
18
|
+
};
|
|
19
|
+
function clampNodeSize(size) {
|
|
20
|
+
return {
|
|
21
|
+
width: Math.max(0, size.width),
|
|
22
|
+
height: Math.max(0, size.height),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function clampNodeRadius(radius, size) {
|
|
26
|
+
return Math.max(0, Math.min(radius, size.width * 0.5, size.height * 0.5));
|
|
27
|
+
}
|
|
28
|
+
function clampCenterToBounds(center, size, bounds) {
|
|
29
|
+
if (!bounds) {
|
|
30
|
+
return { x: center.x, y: center.y };
|
|
31
|
+
}
|
|
32
|
+
const halfW = size.width * 0.5;
|
|
33
|
+
const halfH = size.height * 0.5;
|
|
34
|
+
const minX = bounds.x + halfW;
|
|
35
|
+
const maxX = bounds.x + bounds.width - halfW;
|
|
36
|
+
const minY = bounds.y + halfH;
|
|
37
|
+
const maxY = bounds.y + bounds.height - halfH;
|
|
38
|
+
return {
|
|
39
|
+
x: maxX < minX ? bounds.x + bounds.width * 0.5 : clamp(center.x, minX, maxX),
|
|
40
|
+
y: maxY < minY ? bounds.y + bounds.height * 0.5 : clamp(center.y, minY, maxY),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Computes exact signed surface-to-surface Euclidean gap between two axis-aligned
|
|
45
|
+
* rounded rectangles using Minkowski core decomposition (`Box = Core (+) Disk(r)`).
|
|
46
|
+
* Negative values indicate surface penetration depth; `0` means kissing contact.
|
|
47
|
+
*/
|
|
48
|
+
function computeRoundedBoxesGap(a, b) {
|
|
49
|
+
const haw = Math.max(0, a.size.width * 0.5);
|
|
50
|
+
const hah = Math.max(0, a.size.height * 0.5);
|
|
51
|
+
const ra = clampNodeRadius(a.radius, a.size);
|
|
52
|
+
const iaw = haw - ra;
|
|
53
|
+
const iah = hah - ra;
|
|
54
|
+
const hbw = Math.max(0, b.size.width * 0.5);
|
|
55
|
+
const hbh = Math.max(0, b.size.height * 0.5);
|
|
56
|
+
const rb = clampNodeRadius(b.radius, b.size);
|
|
57
|
+
const ibw = hbw - rb;
|
|
58
|
+
const ibh = hbh - rb;
|
|
59
|
+
const dx = Math.abs(b.center.x - a.center.x);
|
|
60
|
+
const dy = Math.abs(b.center.y - a.center.y);
|
|
61
|
+
const gx = Math.max(0, dx - iaw - ibw);
|
|
62
|
+
const gy = Math.max(0, dy - iah - ibh);
|
|
63
|
+
if (gx > 0 || gy > 0) {
|
|
64
|
+
return Math.hypot(gx, gy) - ra - rb;
|
|
65
|
+
}
|
|
66
|
+
const overlapX = iaw + ibw - dx;
|
|
67
|
+
const overlapY = iah + ibh - dy;
|
|
68
|
+
return -Math.min(overlapX, overlapY) - ra - rb;
|
|
69
|
+
}
|
|
70
|
+
function makePrimitiveFromCoords(node, center) {
|
|
71
|
+
return {
|
|
72
|
+
id: node.id,
|
|
73
|
+
center: { x: center.x, y: center.y },
|
|
74
|
+
size: { width: node.size.width, height: node.size.height },
|
|
75
|
+
radius: node.radius,
|
|
76
|
+
weight: node.weight,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates an interactive N-primitive drag-and-merge glass field controller.
|
|
81
|
+
*/
|
|
82
|
+
export function createDragMergeController(options = {}) {
|
|
83
|
+
const blendK = Math.max(0, options.blendK ?? DRAG_MERGE_DEFAULTS.blendK);
|
|
84
|
+
const magneticRadiusPx = Math.max(0, options.magneticRadiusPx ?? DRAG_MERGE_DEFAULTS.magneticRadiusPx);
|
|
85
|
+
const magneticStrength = clamp(options.magneticStrength ?? DRAG_MERGE_DEFAULTS.magneticStrength, 0, 1);
|
|
86
|
+
const neighborTugRatio = clamp(options.neighborTugRatio ?? DRAG_MERGE_DEFAULTS.neighborTugRatio, 0, 1);
|
|
87
|
+
const springParams = options.spring ?? DRAG_MERGE_DEFAULTS.spring;
|
|
88
|
+
const returnToRestOnRelease = options.returnToRestOnRelease ?? false;
|
|
89
|
+
const snapToNearestOnRelease = options.snapToNearestOnRelease ?? false;
|
|
90
|
+
let bounds = options.bounds ? { ...options.bounds } : undefined;
|
|
91
|
+
let reducedMotion = options.reducedMotion ?? false;
|
|
92
|
+
let disposed = false;
|
|
93
|
+
let activeDragId = null;
|
|
94
|
+
let dragGrabOffset = { x: 0, y: 0 };
|
|
95
|
+
let dragOriginCenter = null;
|
|
96
|
+
let nearestCandidateId = null;
|
|
97
|
+
const nodes = [];
|
|
98
|
+
const listeners = new Set();
|
|
99
|
+
function findNodeIndex(id) {
|
|
100
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
101
|
+
if (nodes[i].id === id)
|
|
102
|
+
return i;
|
|
103
|
+
}
|
|
104
|
+
return -1;
|
|
105
|
+
}
|
|
106
|
+
function findNode(id) {
|
|
107
|
+
if (!id)
|
|
108
|
+
return null;
|
|
109
|
+
const index = findNodeIndex(id);
|
|
110
|
+
return index >= 0 ? nodes[index] : null;
|
|
111
|
+
}
|
|
112
|
+
function createChannel(initialValue) {
|
|
113
|
+
return createSpringChannel({
|
|
114
|
+
params: springParams,
|
|
115
|
+
initialValue,
|
|
116
|
+
initialTarget: initialValue,
|
|
117
|
+
settlePosEps: 0.05,
|
|
118
|
+
settleVelEps: 0.25,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
function createInternalNode(input) {
|
|
122
|
+
const size = clampNodeSize(input.size);
|
|
123
|
+
const radius = clampNodeRadius(input.radius, size);
|
|
124
|
+
const clampedCenter = clampCenterToBounds(input.center, size, bounds);
|
|
125
|
+
const clampedRest = clampCenterToBounds(input.restCenter ?? clampedCenter, size, bounds);
|
|
126
|
+
return {
|
|
127
|
+
id: input.id,
|
|
128
|
+
x: createChannel(clampedCenter.x),
|
|
129
|
+
y: createChannel(clampedCenter.y),
|
|
130
|
+
baseCenter: { ...clampedCenter },
|
|
131
|
+
restCenter: { ...clampedRest },
|
|
132
|
+
size,
|
|
133
|
+
radius,
|
|
134
|
+
pinned: input.pinned ?? false,
|
|
135
|
+
weight: input.weight ?? 1,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (options.nodes) {
|
|
139
|
+
for (const input of options.nodes) {
|
|
140
|
+
if (findNodeIndex(input.id) === -1) {
|
|
141
|
+
nodes.push(createInternalNode(input));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function setNodeTarget(node, target, immediate) {
|
|
146
|
+
const clamped = clampCenterToBounds(target, node.size, bounds);
|
|
147
|
+
node.x.setTarget(clamped.x);
|
|
148
|
+
node.y.setTarget(clamped.y);
|
|
149
|
+
if (immediate) {
|
|
150
|
+
node.x.snap(clamped.x, 0);
|
|
151
|
+
node.y.snap(clamped.y, 0);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function clampAxisToBounds(channel, boundStart, boundSpan, halfSize) {
|
|
155
|
+
const minPos = boundStart + halfSize;
|
|
156
|
+
const maxPos = boundStart + boundSpan - halfSize;
|
|
157
|
+
const state = channel.getState();
|
|
158
|
+
if (maxPos < minPos) {
|
|
159
|
+
const mid = boundStart + boundSpan * 0.5;
|
|
160
|
+
if (state.value !== mid || state.velocity !== 0 || state.target !== mid) {
|
|
161
|
+
channel.setTarget(mid);
|
|
162
|
+
channel.snap(mid, 0);
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
if (state.value < minPos) {
|
|
168
|
+
channel.snap(minPos, state.velocity < 0 ? 0 : state.velocity);
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
if (state.value > maxPos) {
|
|
172
|
+
channel.snap(maxPos, state.velocity > 0 ? 0 : state.velocity);
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
function clampNodePositionToBounds(node) {
|
|
178
|
+
if (!bounds)
|
|
179
|
+
return false;
|
|
180
|
+
const cx = clampAxisToBounds(node.x, bounds.x, bounds.width, node.size.width * 0.5);
|
|
181
|
+
const cy = clampAxisToBounds(node.y, bounds.y, bounds.height, node.size.height * 0.5);
|
|
182
|
+
return cx || cy;
|
|
183
|
+
}
|
|
184
|
+
function restoreOtherNodeBases(excludeId, immediate) {
|
|
185
|
+
for (const node of nodes) {
|
|
186
|
+
if (node.id === excludeId)
|
|
187
|
+
continue;
|
|
188
|
+
setNodeTarget(node, node.baseCenter, immediate);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function evaluateMagneticTargets(dragNode, rawTarget) {
|
|
192
|
+
if (reducedMotion || magneticRadiusPx <= 0) {
|
|
193
|
+
let bestId = null;
|
|
194
|
+
let bestGap = Infinity;
|
|
195
|
+
if (magneticRadiusPx > 0) {
|
|
196
|
+
const dragPrim = makePrimitiveFromCoords(dragNode, rawTarget);
|
|
197
|
+
for (const candidate of nodes) {
|
|
198
|
+
if (candidate.id === dragNode.id)
|
|
199
|
+
continue;
|
|
200
|
+
const candPrim = makePrimitiveFromCoords(candidate, candidate.baseCenter);
|
|
201
|
+
const gap = computeRoundedBoxesGap(dragPrim, candPrim);
|
|
202
|
+
if (gap <= magneticRadiusPx && gap < bestGap) {
|
|
203
|
+
bestGap = gap;
|
|
204
|
+
bestId = candidate.id;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
nearestCandidateId = bestId;
|
|
209
|
+
setNodeTarget(dragNode, rawTarget, reducedMotion);
|
|
210
|
+
restoreOtherNodeBases(dragNode.id, reducedMotion);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const dragPrimAtRaw = makePrimitiveFromCoords(dragNode, rawTarget);
|
|
214
|
+
let bestCandidate = null;
|
|
215
|
+
let bestGap = Infinity;
|
|
216
|
+
for (const candidate of nodes) {
|
|
217
|
+
if (candidate.id === dragNode.id)
|
|
218
|
+
continue;
|
|
219
|
+
const candPrim = makePrimitiveFromCoords(candidate, candidate.baseCenter);
|
|
220
|
+
const gap = computeRoundedBoxesGap(dragPrimAtRaw, candPrim);
|
|
221
|
+
if (gap <= magneticRadiusPx && gap < bestGap) {
|
|
222
|
+
bestGap = gap;
|
|
223
|
+
bestCandidate = candidate;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
nearestCandidateId = bestCandidate?.id ?? null;
|
|
227
|
+
let draggedTarget = rawTarget;
|
|
228
|
+
if (bestCandidate && magneticStrength > 0) {
|
|
229
|
+
const prox = clamp(1 - Math.max(0, bestGap) / magneticRadiusPx, 0, 1);
|
|
230
|
+
const pullWeight = magneticStrength * prox;
|
|
231
|
+
draggedTarget = clampCenterToBounds({
|
|
232
|
+
x: rawTarget.x + (bestCandidate.baseCenter.x - rawTarget.x) * pullWeight,
|
|
233
|
+
y: rawTarget.y + (bestCandidate.baseCenter.y - rawTarget.y) * pullWeight,
|
|
234
|
+
}, dragNode.size, bounds);
|
|
235
|
+
}
|
|
236
|
+
setNodeTarget(dragNode, draggedTarget, false);
|
|
237
|
+
const dragPrimAtPulled = makePrimitiveFromCoords(dragNode, draggedTarget);
|
|
238
|
+
for (const neighbor of nodes) {
|
|
239
|
+
if (neighbor.id === dragNode.id)
|
|
240
|
+
continue;
|
|
241
|
+
if (neighbor.pinned || neighborTugRatio <= 0) {
|
|
242
|
+
setNodeTarget(neighbor, neighbor.baseCenter, false);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const neighborPrim = makePrimitiveFromCoords(neighbor, neighbor.baseCenter);
|
|
246
|
+
const gap = computeRoundedBoxesGap(dragPrimAtPulled, neighborPrim);
|
|
247
|
+
if (gap <= magneticRadiusPx) {
|
|
248
|
+
const prox = clamp(1 - Math.max(0, gap) / magneticRadiusPx, 0, 1);
|
|
249
|
+
const tug = neighborTugRatio * prox;
|
|
250
|
+
setNodeTarget(neighbor, {
|
|
251
|
+
x: neighbor.baseCenter.x + (draggedTarget.x - neighbor.baseCenter.x) * tug,
|
|
252
|
+
y: neighbor.baseCenter.y + (draggedTarget.y - neighbor.baseCenter.y) * tug,
|
|
253
|
+
}, false);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
setNodeTarget(neighbor, neighbor.baseCenter, false);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function buildState() {
|
|
261
|
+
const nodeStates = [];
|
|
262
|
+
const primitives = [];
|
|
263
|
+
let animating = false;
|
|
264
|
+
for (const node of nodes) {
|
|
265
|
+
const xState = node.x.getState();
|
|
266
|
+
const yState = node.y.getState();
|
|
267
|
+
const settled = xState.settled && yState.settled;
|
|
268
|
+
if (!settled) {
|
|
269
|
+
animating = true;
|
|
270
|
+
}
|
|
271
|
+
const center = { x: xState.value, y: yState.value };
|
|
272
|
+
const primitive = makePrimitiveFromCoords(node, center);
|
|
273
|
+
primitives.push(primitive);
|
|
274
|
+
nodeStates.push({
|
|
275
|
+
id: node.id,
|
|
276
|
+
primitive,
|
|
277
|
+
center,
|
|
278
|
+
targetCenter: { x: xState.target, y: yState.target },
|
|
279
|
+
restCenter: { ...node.restCenter },
|
|
280
|
+
size: { ...node.size },
|
|
281
|
+
radius: node.radius,
|
|
282
|
+
pinned: node.pinned,
|
|
283
|
+
weight: node.weight,
|
|
284
|
+
dragging: activeDragId === node.id,
|
|
285
|
+
settled,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
const links = [];
|
|
289
|
+
const sminBridgeThreshold = blendK * 0.5;
|
|
290
|
+
for (let i = 0; i < primitives.length; i += 1) {
|
|
291
|
+
const a = primitives[i];
|
|
292
|
+
for (let j = i + 1; j < primitives.length; j += 1) {
|
|
293
|
+
const b = primitives[j];
|
|
294
|
+
const gapPx = computeRoundedBoxesGap(a, b);
|
|
295
|
+
const mergeDepth = sminBridgeThreshold - gapPx;
|
|
296
|
+
const merged = mergeDepth >= 0;
|
|
297
|
+
let bridgeStrength = 0;
|
|
298
|
+
if (merged) {
|
|
299
|
+
if (gapPx <= 0 || sminBridgeThreshold <= 1e-6) {
|
|
300
|
+
bridgeStrength = 1;
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
bridgeStrength = clamp(1 - gapPx / sminBridgeThreshold, 0, 1);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
links.push({
|
|
307
|
+
aId: nodes[i].id,
|
|
308
|
+
bId: nodes[j].id,
|
|
309
|
+
gapPx,
|
|
310
|
+
bridgeStrength,
|
|
311
|
+
merged,
|
|
312
|
+
mergeDepth,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
nodes: nodeStates,
|
|
318
|
+
links,
|
|
319
|
+
activeDragId,
|
|
320
|
+
nearestCandidateId,
|
|
321
|
+
field: {
|
|
322
|
+
primitives,
|
|
323
|
+
blendK,
|
|
324
|
+
},
|
|
325
|
+
isAnimating: animating || activeDragId !== null,
|
|
326
|
+
reducedMotion,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function notify() {
|
|
330
|
+
if (disposed || listeners.size === 0)
|
|
331
|
+
return;
|
|
332
|
+
const snapshot = buildState();
|
|
333
|
+
for (const listener of [...listeners]) {
|
|
334
|
+
listener(snapshot);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
hitTest(point) {
|
|
339
|
+
for (let i = nodes.length - 1; i >= 0; i -= 1) {
|
|
340
|
+
const node = nodes[i];
|
|
341
|
+
const prim = makePrimitiveFromCoords(node, {
|
|
342
|
+
x: node.x.getState().value,
|
|
343
|
+
y: node.y.getState().value,
|
|
344
|
+
});
|
|
345
|
+
if (computeRoundedBoxDistance(point.x, point.y, prim) <= 0) {
|
|
346
|
+
return node.id;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
},
|
|
351
|
+
startDrag(id, pointer) {
|
|
352
|
+
if (disposed)
|
|
353
|
+
return false;
|
|
354
|
+
const node = findNode(id);
|
|
355
|
+
if (!node || node.pinned) {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
if (activeDragId && activeDragId !== id) {
|
|
359
|
+
restoreOtherNodeBases(null, reducedMotion);
|
|
360
|
+
}
|
|
361
|
+
activeDragId = node.id;
|
|
362
|
+
const currentX = node.x.getState().value;
|
|
363
|
+
const currentY = node.y.getState().value;
|
|
364
|
+
dragOriginCenter = { ...node.baseCenter };
|
|
365
|
+
dragGrabOffset = {
|
|
366
|
+
x: pointer.x - currentX,
|
|
367
|
+
y: pointer.y - currentY,
|
|
368
|
+
};
|
|
369
|
+
evaluateMagneticTargets(node, clampCenterToBounds({ x: currentX, y: currentY }, node.size, bounds));
|
|
370
|
+
notify();
|
|
371
|
+
return true;
|
|
372
|
+
},
|
|
373
|
+
moveDrag(pointer) {
|
|
374
|
+
if (disposed || !activeDragId)
|
|
375
|
+
return;
|
|
376
|
+
const dragNode = findNode(activeDragId);
|
|
377
|
+
if (!dragNode) {
|
|
378
|
+
activeDragId = null;
|
|
379
|
+
nearestCandidateId = null;
|
|
380
|
+
dragOriginCenter = null;
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const rawTarget = clampCenterToBounds({
|
|
384
|
+
x: pointer.x - dragGrabOffset.x,
|
|
385
|
+
y: pointer.y - dragGrabOffset.y,
|
|
386
|
+
}, dragNode.size, bounds);
|
|
387
|
+
evaluateMagneticTargets(dragNode, rawTarget);
|
|
388
|
+
notify();
|
|
389
|
+
},
|
|
390
|
+
endDrag() {
|
|
391
|
+
if (disposed || !activeDragId)
|
|
392
|
+
return;
|
|
393
|
+
const dragNode = findNode(activeDragId);
|
|
394
|
+
const candidate = findNode(nearestCandidateId);
|
|
395
|
+
activeDragId = null;
|
|
396
|
+
nearestCandidateId = null;
|
|
397
|
+
dragOriginCenter = null;
|
|
398
|
+
if (dragNode) {
|
|
399
|
+
if (returnToRestOnRelease) {
|
|
400
|
+
dragNode.baseCenter = { ...dragNode.restCenter };
|
|
401
|
+
setNodeTarget(dragNode, dragNode.restCenter, reducedMotion);
|
|
402
|
+
}
|
|
403
|
+
else if (snapToNearestOnRelease && candidate) {
|
|
404
|
+
const snapped = clampCenterToBounds(candidate.baseCenter, dragNode.size, bounds);
|
|
405
|
+
dragNode.baseCenter = { ...snapped };
|
|
406
|
+
setNodeTarget(dragNode, snapped, reducedMotion);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
const finalTarget = clampCenterToBounds({
|
|
410
|
+
x: dragNode.x.getState().target,
|
|
411
|
+
y: dragNode.y.getState().target,
|
|
412
|
+
}, dragNode.size, bounds);
|
|
413
|
+
dragNode.baseCenter = { ...finalTarget };
|
|
414
|
+
setNodeTarget(dragNode, finalTarget, reducedMotion);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
restoreOtherNodeBases(null, reducedMotion);
|
|
418
|
+
notify();
|
|
419
|
+
},
|
|
420
|
+
cancelDrag() {
|
|
421
|
+
if (disposed || !activeDragId)
|
|
422
|
+
return;
|
|
423
|
+
const dragNode = findNode(activeDragId);
|
|
424
|
+
const origin = dragOriginCenter;
|
|
425
|
+
activeDragId = null;
|
|
426
|
+
nearestCandidateId = null;
|
|
427
|
+
dragOriginCenter = null;
|
|
428
|
+
if (dragNode && origin) {
|
|
429
|
+
const restored = clampCenterToBounds(origin, dragNode.size, bounds);
|
|
430
|
+
dragNode.baseCenter = { ...restored };
|
|
431
|
+
setNodeTarget(dragNode, restored, reducedMotion);
|
|
432
|
+
}
|
|
433
|
+
restoreOtherNodeBases(null, reducedMotion);
|
|
434
|
+
notify();
|
|
435
|
+
},
|
|
436
|
+
addNode(nodeInput) {
|
|
437
|
+
if (disposed || findNodeIndex(nodeInput.id) !== -1)
|
|
438
|
+
return;
|
|
439
|
+
nodes.push(createInternalNode(nodeInput));
|
|
440
|
+
notify();
|
|
441
|
+
},
|
|
442
|
+
removeNode(id) {
|
|
443
|
+
if (disposed)
|
|
444
|
+
return;
|
|
445
|
+
const index = findNodeIndex(id);
|
|
446
|
+
if (index === -1)
|
|
447
|
+
return;
|
|
448
|
+
nodes.splice(index, 1);
|
|
449
|
+
if (activeDragId === id) {
|
|
450
|
+
activeDragId = null;
|
|
451
|
+
dragOriginCenter = null;
|
|
452
|
+
nearestCandidateId = null;
|
|
453
|
+
restoreOtherNodeBases(null, reducedMotion);
|
|
454
|
+
}
|
|
455
|
+
else if (nearestCandidateId === id) {
|
|
456
|
+
nearestCandidateId = null;
|
|
457
|
+
}
|
|
458
|
+
notify();
|
|
459
|
+
},
|
|
460
|
+
updateNode(id, patch) {
|
|
461
|
+
if (disposed)
|
|
462
|
+
return;
|
|
463
|
+
const node = findNode(id);
|
|
464
|
+
if (!node)
|
|
465
|
+
return;
|
|
466
|
+
if (patch.size !== undefined) {
|
|
467
|
+
node.size = clampNodeSize(patch.size);
|
|
468
|
+
}
|
|
469
|
+
if (patch.radius !== undefined) {
|
|
470
|
+
node.radius = clampNodeRadius(patch.radius, node.size);
|
|
471
|
+
}
|
|
472
|
+
else if (patch.size !== undefined) {
|
|
473
|
+
node.radius = clampNodeRadius(node.radius, node.size);
|
|
474
|
+
}
|
|
475
|
+
if (patch.pinned !== undefined) {
|
|
476
|
+
node.pinned = patch.pinned;
|
|
477
|
+
if (node.pinned && activeDragId === node.id) {
|
|
478
|
+
activeDragId = null;
|
|
479
|
+
nearestCandidateId = null;
|
|
480
|
+
dragOriginCenter = null;
|
|
481
|
+
restoreOtherNodeBases(node.id, reducedMotion);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (patch.weight !== undefined) {
|
|
485
|
+
node.weight = patch.weight;
|
|
486
|
+
}
|
|
487
|
+
if (patch.restCenter !== undefined) {
|
|
488
|
+
node.restCenter = clampCenterToBounds(patch.restCenter, node.size, bounds);
|
|
489
|
+
}
|
|
490
|
+
if (patch.center !== undefined) {
|
|
491
|
+
const clamped = clampCenterToBounds(patch.center, node.size, bounds);
|
|
492
|
+
node.baseCenter = { ...clamped };
|
|
493
|
+
setNodeTarget(node, clamped, reducedMotion);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
const clampedCurrent = clampCenterToBounds(node.baseCenter, node.size, bounds);
|
|
497
|
+
node.baseCenter = { ...clampedCurrent };
|
|
498
|
+
if (activeDragId !== node.id) {
|
|
499
|
+
setNodeTarget(node, clampedCurrent, reducedMotion);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
clampNodePositionToBounds(node);
|
|
503
|
+
notify();
|
|
504
|
+
},
|
|
505
|
+
setBounds(nextBounds) {
|
|
506
|
+
if (disposed)
|
|
507
|
+
return;
|
|
508
|
+
bounds = nextBounds ? { ...nextBounds } : undefined;
|
|
509
|
+
for (const node of nodes) {
|
|
510
|
+
node.restCenter = clampCenterToBounds(node.restCenter, node.size, bounds);
|
|
511
|
+
node.baseCenter = clampCenterToBounds(node.baseCenter, node.size, bounds);
|
|
512
|
+
setNodeTarget(node, { x: node.x.getState().target, y: node.y.getState().target }, reducedMotion);
|
|
513
|
+
clampNodePositionToBounds(node);
|
|
514
|
+
}
|
|
515
|
+
notify();
|
|
516
|
+
},
|
|
517
|
+
setReducedMotion(reduced) {
|
|
518
|
+
if (disposed || reducedMotion === reduced)
|
|
519
|
+
return;
|
|
520
|
+
reducedMotion = reduced;
|
|
521
|
+
if (reducedMotion) {
|
|
522
|
+
nearestCandidateId = activeDragId ? nearestCandidateId : null;
|
|
523
|
+
for (const node of nodes) {
|
|
524
|
+
const target = clampCenterToBounds({
|
|
525
|
+
x: node.x.getState().target,
|
|
526
|
+
y: node.y.getState().target,
|
|
527
|
+
}, node.size, bounds);
|
|
528
|
+
node.x.setTarget(target.x);
|
|
529
|
+
node.y.setTarget(target.y);
|
|
530
|
+
node.x.snap(target.x, 0);
|
|
531
|
+
node.y.snap(target.y, 0);
|
|
532
|
+
}
|
|
533
|
+
restoreOtherNodeBases(activeDragId, true);
|
|
534
|
+
}
|
|
535
|
+
notify();
|
|
536
|
+
},
|
|
537
|
+
step(dtSec) {
|
|
538
|
+
if (disposed || !Number.isFinite(dtSec) || dtSec <= 0) {
|
|
539
|
+
return buildState();
|
|
540
|
+
}
|
|
541
|
+
if (reducedMotion) {
|
|
542
|
+
return buildState();
|
|
543
|
+
}
|
|
544
|
+
let changed = false;
|
|
545
|
+
for (const node of nodes) {
|
|
546
|
+
const prevX = node.x.getState();
|
|
547
|
+
const prevY = node.y.getState();
|
|
548
|
+
if (!prevX.settled || !prevY.settled) {
|
|
549
|
+
node.x.step(dtSec);
|
|
550
|
+
node.y.step(dtSec);
|
|
551
|
+
clampNodePositionToBounds(node);
|
|
552
|
+
changed = true;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const next = buildState();
|
|
556
|
+
if (changed) {
|
|
557
|
+
notify();
|
|
558
|
+
}
|
|
559
|
+
return next;
|
|
560
|
+
},
|
|
561
|
+
getState() {
|
|
562
|
+
return buildState();
|
|
563
|
+
},
|
|
564
|
+
subscribe(listener) {
|
|
565
|
+
if (disposed) {
|
|
566
|
+
return () => { };
|
|
567
|
+
}
|
|
568
|
+
listeners.add(listener);
|
|
569
|
+
return () => {
|
|
570
|
+
listeners.delete(listener);
|
|
571
|
+
};
|
|
572
|
+
},
|
|
573
|
+
dispose() {
|
|
574
|
+
disposed = true;
|
|
575
|
+
listeners.clear();
|
|
576
|
+
},
|
|
577
|
+
};
|
|
578
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for `@sessile/presets`.
|
|
3
|
+
* Framework-agnostic interaction presets built on `@sessile/core`.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
export { POPOVER_MORPH_DEFAULTS, computePopoverMorphGeometry, computePopoverTargetRect, createPopoverMorphController, createPopoverMorphSnapshot, evaluatePopoverMotion, getPopoverPhaseDurationSec, type EvaluatePopoverMotionOptions, type PopoverAnchorConfig, type PopoverContentTransform, type PopoverFrameGeometry, type PopoverGlyphTransform, type PopoverInterruptStart, type PopoverMorphController, type PopoverMorphControllerOptions, type PopoverMorphPhase, type PopoverMorphSnapshot, type PopoverMotionState, type PopoverPanelConfig, type PopoverPlacement, type PopoverReopenOrigin, } from './popover.ts';
|
|
8
|
+
export { SEGMENTED_PILL_DEFAULTS, createSegmentedPillController, type SegmentItem, type SegmentedPillController, type SegmentedPillOptions, type SegmentedPillState, } from './segmented.ts';
|
|
9
|
+
export { DRAG_MERGE_DEFAULTS, createDragMergeController, type DragMergeController, type DragMergeLink, type DragMergeNodeInput, type DragMergeNodePatch, type DragMergeNodeState, type DragMergeOptions, type DragMergeState, } from './dragMerge.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry point for `@sessile/presets`.
|
|
3
|
+
* Framework-agnostic interaction presets built on `@sessile/core`.
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: MPL-2.0
|
|
6
|
+
*/
|
|
7
|
+
export { POPOVER_MORPH_DEFAULTS, computePopoverMorphGeometry, computePopoverTargetRect, createPopoverMorphController, createPopoverMorphSnapshot, evaluatePopoverMotion, getPopoverPhaseDurationSec, } from "./popover.js";
|
|
8
|
+
export { SEGMENTED_PILL_DEFAULTS, createSegmentedPillController, } from "./segmented.js";
|
|
9
|
+
export { DRAG_MERGE_DEFAULTS, createDragMergeController, } from "./dragMerge.js";
|