dssoca 0.17.0 → 0.19.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.
@@ -0,0 +1,304 @@
1
+ /**
2
+ * TierList (DS-0159) — plain-HTML behaviour for the markup `TierList.svelte` renders: pointer
3
+ * drag between/within rows (Pointer Events, so touch works) and the keyboard path (Space picks
4
+ * a tile up, arrows move it, Space drops, Escape cancels) with a polite live region. The move
5
+ * math and the wording come from `../tierlist-core.js`, shared with the Svelte component.
6
+ * Emits `ss:change` on the root with `{ placements }` (tier id → item ids; the tray is
7
+ * implicit). `data-ss-readonly` on the root makes the list inert.
8
+ */
9
+ import { on, emit, all } from './delegate.js';
10
+ import { TRAY, announceText, insertionIndex, keyboardTarget, } from '../tierlist-core.js';
11
+ const ROOT = '.ss-tierlist';
12
+ const TILE = `${ROOT} .tile[data-item]`;
13
+ const DRAG_THRESHOLD = 4;
14
+ function rootOf(el) {
15
+ return el.closest(ROOT);
16
+ }
17
+ function zonesOf(root) {
18
+ return all(root, '[data-zone]').filter((z) => rootOf(z) === root);
19
+ }
20
+ function zoneKey(zone) {
21
+ return zone.hasAttribute('data-tray') ? TRAY : zone.dataset.zone;
22
+ }
23
+ function zoneByKey(root, key) {
24
+ return zonesOf(root).find((z) => zoneKey(z) === key);
25
+ }
26
+ function zoneLabel(zone) {
27
+ return zone.getAttribute('aria-label') ?? zone.dataset.zoneLabel ?? zoneKey(zone);
28
+ }
29
+ function cellsOf(zone) {
30
+ return all(zone, ':scope > .cell');
31
+ }
32
+ /** The zone map as the DOM currently has it. */
33
+ export function readZones(root) {
34
+ const zones = {};
35
+ for (const zone of zonesOf(root)) {
36
+ zones[zoneKey(zone)] = cellsOf(zone).map((c) => c.querySelector('.tile[data-item]')?.dataset.item ?? '');
37
+ }
38
+ return zones;
39
+ }
40
+ /** Public placements read from the DOM: every non-tray zone, in DOM order. */
41
+ export function readPlacements(root) {
42
+ const out = {};
43
+ for (const zone of zonesOf(root)) {
44
+ const key = zoneKey(zone);
45
+ if (key !== TRAY)
46
+ out[key] = readZones(root)[key];
47
+ }
48
+ return out;
49
+ }
50
+ function isReadonly(root) {
51
+ return root.hasAttribute('data-ss-readonly');
52
+ }
53
+ function announce(root, text) {
54
+ const live = root.querySelector('.live');
55
+ if (live)
56
+ live.textContent = text;
57
+ }
58
+ // Re-inserting a focused node blurs it (browsers run the focus fixup on removal); while a
59
+ // keyboard move is in flight that blur must not read as "the user left the tile".
60
+ let moving = false;
61
+ /** Physically move a tile's cell to `zone` at `index` (clamped; the cell may already be there). */
62
+ function placeCell(root, cell, zone, index) {
63
+ const target = zoneByKey(root, zone);
64
+ if (!target)
65
+ return;
66
+ const siblings = cellsOf(target).filter((c) => c !== cell);
67
+ const before = siblings[Math.max(0, Math.min(index, siblings.length))] ?? null;
68
+ const was = moving;
69
+ moving = true;
70
+ try {
71
+ target.insertBefore(cell, before);
72
+ }
73
+ finally {
74
+ moving = was;
75
+ }
76
+ }
77
+ function describe(root, tile) {
78
+ const cell = tile.closest('.cell');
79
+ const zone = cell.parentElement;
80
+ const cells = cellsOf(zone);
81
+ return {
82
+ label: tile.getAttribute('aria-label') ?? tile.textContent?.trim() ?? '',
83
+ zone: zoneLabel(zone),
84
+ index: cells.indexOf(cell),
85
+ count: cells.length,
86
+ };
87
+ }
88
+ function change(root) {
89
+ emit(root, 'ss:change', { placements: readPlacements(root) });
90
+ }
91
+ const grabs = new WeakMap();
92
+ const KEY_MOVES = {
93
+ ArrowLeft: 'left',
94
+ ArrowRight: 'right',
95
+ ArrowUp: 'up',
96
+ ArrowDown: 'down',
97
+ Home: 'home',
98
+ End: 'end',
99
+ };
100
+ function setGrabbed(root, tile) {
101
+ for (const t of all(root, '.tile[aria-pressed="true"]')) {
102
+ t.setAttribute('aria-pressed', 'false');
103
+ t.closest('.cell')?.classList.remove('grabbed');
104
+ }
105
+ root.classList.toggle('sorting', Boolean(tile));
106
+ if (tile) {
107
+ tile.setAttribute('aria-pressed', 'true');
108
+ tile.closest('.cell')?.classList.add('grabbed');
109
+ }
110
+ }
111
+ /** Pick a tile up (or drop it when it is already grabbed). */
112
+ export function toggleGrab(tile) {
113
+ const root = rootOf(tile);
114
+ if (!root || isReadonly(root))
115
+ return;
116
+ const grab = grabs.get(root);
117
+ if (grab?.tile === tile) {
118
+ grabs.delete(root);
119
+ setGrabbed(root, null);
120
+ announce(root, announceText('drop', describe(root, tile)));
121
+ change(root);
122
+ return;
123
+ }
124
+ const d = describe(root, tile);
125
+ const cell = tile.closest('.cell');
126
+ grabs.set(root, { tile, zone: zoneKey(cell.parentElement), index: d.index });
127
+ setGrabbed(root, tile);
128
+ announce(root, announceText('grab', d));
129
+ }
130
+ /** `focusBack`: Escape keeps the user on the tile; a cancel caused by focus leaving must not pull it back. */
131
+ function cancelGrab(root, focusBack) {
132
+ const grab = grabs.get(root);
133
+ if (!grab)
134
+ return;
135
+ grabs.delete(root);
136
+ const cell = grab.tile.closest('.cell');
137
+ moving = true;
138
+ try {
139
+ placeCell(root, cell, grab.zone, grab.index);
140
+ setGrabbed(root, null);
141
+ if (focusBack)
142
+ grab.tile.focus();
143
+ }
144
+ finally {
145
+ moving = false;
146
+ }
147
+ announce(root, announceText('cancel', describe(root, grab.tile)));
148
+ }
149
+ function keyMove(root, tile, move) {
150
+ const zones = readZones(root);
151
+ const order = zonesOf(root).map(zoneKey);
152
+ const target = keyboardTarget(zones, order, tile.dataset.item, move);
153
+ if (!target)
154
+ return;
155
+ const cell = tile.closest('.cell');
156
+ moving = true;
157
+ try {
158
+ placeCell(root, cell, target.zone, target.index);
159
+ tile.focus();
160
+ }
161
+ finally {
162
+ moving = false;
163
+ }
164
+ announce(root, announceText('move', describe(root, tile)));
165
+ }
166
+ let drag = null;
167
+ function zoneAt(root, x, y) {
168
+ for (const el of document.elementsFromPoint(x, y)) {
169
+ const zone = el.closest('[data-zone]');
170
+ if (zone && rootOf(zone) === root)
171
+ return zone;
172
+ }
173
+ return null;
174
+ }
175
+ function startDrag(d) {
176
+ d.dragging = true;
177
+ const rect = d.tile.getBoundingClientRect();
178
+ d.dx = d.startX - rect.left;
179
+ d.dy = d.startY - rect.top;
180
+ const ghost = d.tile.cloneNode(true);
181
+ ghost.classList.add('ghost');
182
+ ghost.removeAttribute('id');
183
+ ghost.setAttribute('aria-hidden', 'true');
184
+ ghost.style.width = `${rect.width}px`;
185
+ ghost.style.height = `${rect.height}px`;
186
+ d.root.appendChild(ghost);
187
+ d.ghost = ghost;
188
+ d.cell.classList.add('shadow');
189
+ d.root.classList.add('dragging');
190
+ }
191
+ function moveDrag(d, x, y) {
192
+ if (d.ghost)
193
+ d.ghost.style.transform = `translate(${x - d.dx}px, ${y - d.dy}px)`;
194
+ const zone = zoneAt(d.root, x, y);
195
+ for (const z of zonesOf(d.root))
196
+ z.classList.toggle('target', z === zone);
197
+ if (!zone)
198
+ return;
199
+ const boxes = cellsOf(zone)
200
+ .filter((c) => c !== d.cell)
201
+ .map((c) => c.getBoundingClientRect());
202
+ placeCell(d.root, d.cell, zoneKey(zone), insertionIndex(boxes, x, y));
203
+ }
204
+ function endDrag(d, cancelled) {
205
+ d.ghost?.remove();
206
+ d.cell.classList.remove('shadow');
207
+ d.root.classList.remove('dragging');
208
+ for (const z of zonesOf(d.root))
209
+ z.classList.remove('target');
210
+ if (cancelled) {
211
+ placeCell(d.root, d.cell, d.zone, d.index);
212
+ return;
213
+ }
214
+ const now = describe(d.root, d.tile);
215
+ if (zoneKey(d.cell.parentElement) !== d.zone || now.index !== d.index) {
216
+ announce(d.root, announceText('drop', now));
217
+ change(d.root);
218
+ }
219
+ }
220
+ export function installTierlist() {
221
+ on('keydown', TILE, (tile, e) => {
222
+ const root = rootOf(tile);
223
+ if (!root || isReadonly(root))
224
+ return;
225
+ const grab = grabs.get(root);
226
+ if (e.key === ' ' || (e.key === 'Enter' && !tile.hasAttribute('data-ss-activate'))) {
227
+ e.preventDefault();
228
+ toggleGrab(tile);
229
+ return;
230
+ }
231
+ if (e.key === 'Escape' && grab?.tile === tile) {
232
+ e.preventDefault();
233
+ cancelGrab(root, true);
234
+ return;
235
+ }
236
+ const move = KEY_MOVES[e.key];
237
+ if (move && grab?.tile === tile) {
238
+ e.preventDefault();
239
+ keyMove(root, tile, move);
240
+ }
241
+ });
242
+ // Cancel when focus has really left a grabbed tile — checked a beat later, because moving
243
+ // the node (a row change re-inserts it) blurs it in some browsers before it is re-focused.
244
+ on('focusout', TILE, (tile) => {
245
+ if (moving)
246
+ return;
247
+ const root = rootOf(tile);
248
+ if (!root || grabs.get(root)?.tile !== tile)
249
+ return;
250
+ setTimeout(() => {
251
+ if (grabs.get(root)?.tile === tile && document.activeElement !== tile)
252
+ cancelGrab(root, false);
253
+ }, 0);
254
+ });
255
+ on('pointerdown', TILE, (tile, e) => {
256
+ const root = rootOf(tile);
257
+ if (!root || isReadonly(root) || e.button !== 0 || grabs.has(root))
258
+ return;
259
+ const cell = tile.closest('.cell');
260
+ const zone = cell.parentElement;
261
+ drag = {
262
+ root,
263
+ tile,
264
+ cell,
265
+ startX: e.clientX,
266
+ startY: e.clientY,
267
+ dragging: false,
268
+ ghost: null,
269
+ dx: 0,
270
+ dy: 0,
271
+ zone: zoneKey(zone),
272
+ index: cellsOf(zone).indexOf(cell),
273
+ };
274
+ });
275
+ // Document-level, not on the tile: re-inserting the cell while it moves between rows
276
+ // releases any pointer capture, so the rest of the drag must not depend on the tile.
277
+ document.addEventListener('pointermove', (e) => {
278
+ if (!drag)
279
+ return;
280
+ if (!drag.dragging) {
281
+ if (Math.hypot(e.clientX - drag.startX, e.clientY - drag.startY) < DRAG_THRESHOLD)
282
+ return;
283
+ startDrag(drag);
284
+ }
285
+ e.preventDefault();
286
+ moveDrag(drag, e.clientX, e.clientY);
287
+ });
288
+ document.addEventListener('pointerup', () => {
289
+ if (!drag)
290
+ return;
291
+ const d = drag;
292
+ drag = null;
293
+ if (d.dragging)
294
+ endDrag(d, false);
295
+ });
296
+ document.addEventListener('pointercancel', () => {
297
+ if (!drag)
298
+ return;
299
+ const d = drag;
300
+ drag = null;
301
+ if (d.dragging)
302
+ endDrag(d, true);
303
+ });
304
+ }
@@ -32,6 +32,7 @@ class VanillaToastStore {
32
32
  if (!el) {
33
33
  el = document.createElement('div');
34
34
  el.className = 'ss-toaster';
35
+ el.setAttribute('role', 'region');
35
36
  el.setAttribute('aria-label', 'Notifications');
36
37
  el.dataset.position = 'top-right';
37
38
  document.body.appendChild(el);