@wirecore/wirelayer-recipes 0.9.0 → 0.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.
Files changed (79) hide show
  1. package/dist/i18n.d.ts +4 -0
  2. package/dist/i18n.d.ts.map +1 -1
  3. package/dist/i18n.js +4 -0
  4. package/dist/i18n.js.map +1 -1
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/locales/ar.d.ts.map +1 -1
  10. package/dist/locales/ar.js +4 -0
  11. package/dist/locales/ar.js.map +1 -1
  12. package/dist/locales/cs.d.ts.map +1 -1
  13. package/dist/locales/cs.js +4 -0
  14. package/dist/locales/cs.js.map +1 -1
  15. package/dist/locales/de.d.ts +4 -0
  16. package/dist/locales/de.d.ts.map +1 -1
  17. package/dist/locales/de.js +4 -0
  18. package/dist/locales/de.js.map +1 -1
  19. package/dist/locales/es.d.ts.map +1 -1
  20. package/dist/locales/es.js +4 -0
  21. package/dist/locales/es.js.map +1 -1
  22. package/dist/locales/fr.d.ts.map +1 -1
  23. package/dist/locales/fr.js +4 -0
  24. package/dist/locales/fr.js.map +1 -1
  25. package/dist/locales/hu.d.ts.map +1 -1
  26. package/dist/locales/hu.js +4 -0
  27. package/dist/locales/hu.js.map +1 -1
  28. package/dist/locales/it.d.ts.map +1 -1
  29. package/dist/locales/it.js +4 -0
  30. package/dist/locales/it.js.map +1 -1
  31. package/dist/locales/lv.d.ts.map +1 -1
  32. package/dist/locales/lv.js +4 -0
  33. package/dist/locales/lv.js.map +1 -1
  34. package/dist/locales/nl.d.ts.map +1 -1
  35. package/dist/locales/nl.js +4 -0
  36. package/dist/locales/nl.js.map +1 -1
  37. package/dist/locales/pl.d.ts.map +1 -1
  38. package/dist/locales/pl.js +4 -0
  39. package/dist/locales/pl.js.map +1 -1
  40. package/dist/locales/pt-BR.d.ts.map +1 -1
  41. package/dist/locales/pt-BR.js +1 -0
  42. package/dist/locales/pt-BR.js.map +1 -1
  43. package/dist/locales/pt.d.ts.map +1 -1
  44. package/dist/locales/pt.js +4 -0
  45. package/dist/locales/pt.js.map +1 -1
  46. package/dist/locales/tr.d.ts.map +1 -1
  47. package/dist/locales/tr.js +4 -0
  48. package/dist/locales/tr.js.map +1 -1
  49. package/dist/native/index.d.ts +1 -0
  50. package/dist/native/index.d.ts.map +1 -1
  51. package/dist/native/index.js +1 -0
  52. package/dist/native/index.js.map +1 -1
  53. package/dist/sortable.d.ts +7 -0
  54. package/dist/sortable.d.ts.map +1 -1
  55. package/dist/sortable.js +76 -17
  56. package/dist/sortable.js.map +1 -1
  57. package/dist/tour-steps.d.ts +21 -0
  58. package/dist/tour-steps.d.ts.map +1 -0
  59. package/dist/tour-steps.js +41 -0
  60. package/dist/tour-steps.js.map +1 -0
  61. package/package.json +1 -1
  62. package/src/i18n.ts +5 -0
  63. package/src/index.ts +1 -0
  64. package/src/locales/ar.ts +5 -0
  65. package/src/locales/cs.ts +5 -0
  66. package/src/locales/de.ts +5 -0
  67. package/src/locales/es.ts +5 -0
  68. package/src/locales/fr.ts +5 -0
  69. package/src/locales/hu.ts +5 -0
  70. package/src/locales/it.ts +5 -0
  71. package/src/locales/lv.ts +5 -0
  72. package/src/locales/nl.ts +5 -0
  73. package/src/locales/pl.ts +5 -0
  74. package/src/locales/pt-BR.ts +2 -0
  75. package/src/locales/pt.ts +5 -0
  76. package/src/locales/tr.ts +5 -0
  77. package/src/native/index.ts +1 -0
  78. package/src/sortable.ts +78 -17
  79. package/src/tour-steps.ts +52 -0
package/src/sortable.ts CHANGED
@@ -77,6 +77,8 @@ export interface SortableTexts {
77
77
  movedTo: (item: string, container: string, position: number, count: number) => string;
78
78
  dropped: (item: string, position: number, count: number) => string;
79
79
  cancelled: (item: string, position: number, count: number) => string;
80
+ /** A copy out of a source list was dropped nowhere (or Escape) */
81
+ copyCancelled: (item: string) => string;
80
82
  }
81
83
 
82
84
  /** A move that changed the order: from and to which container (its `id`) and index */
@@ -105,6 +107,11 @@ export interface SortableContainer<T = unknown> {
105
107
  disabled: () => boolean;
106
108
  /** Whether an item from another container may come in (default: yes) */
107
109
  accepts?: (item: T, from: string) => boolean;
110
+ /**
111
+ * Makes the container a source (e.g. a palette): its items are copied out – this function makes the copy – and
112
+ * it keeps them, takes none in and is not sorted
113
+ */
114
+ clone?: () => ((item: T) => unknown) | undefined;
108
115
  texts: () => SortableTexts;
109
116
  /** Focuses the handle at `index` once the new order is rendered (default: in the next task) */
110
117
  focus?: (index: number) => void;
@@ -123,6 +130,8 @@ export interface SortableState {
123
130
  type Mode = 'keyboard' | 'pointer' | 'click';
124
131
  interface Active {
125
132
  item: unknown;
133
+ /** The item of a source the carried copy was made of (undefined: the item itself is carried) */
134
+ copyOf: unknown;
126
135
  origin: { container: SortableContainer; index: number };
127
136
  current: { container: SortableContainer; index: number; count: number };
128
137
  mode: Mode;
@@ -132,6 +141,8 @@ const idleState: SortableState = { grabbed: null, dragging: false, active: false
132
141
  const DRAG_THRESHOLD = 5;
133
142
  /** Attribute of the drag handles: the id of their container (several Sortables can be nested) */
134
143
  export const SORTABLE_HANDLE = 'data-sortable-handle';
144
+ const CONTAINER = '[data-sortable-container]';
145
+ const INDEX = 'data-sortable-index';
135
146
 
136
147
  /** Pick-up, keyboard and pointer moves of all Sortables on the page (one item at a time) */
137
148
  export class SortableController {
@@ -170,11 +181,11 @@ export class SortableController {
170
181
  const active = this.#active;
171
182
  if (!active) return idleState;
172
183
  const container = this.#containers.get(id);
173
- const sameGroup = !!container && (container === active.current.container || this.#sameGroup(container));
174
184
  return {
175
185
  grabbed: active.current.container.id === id ? active.current.index : null,
176
186
  dragging: active.mode === 'pointer' && active.current.container.id === id,
177
- active: sameGroup,
187
+ // Takes the item (a source only takes its copy back – not shown as a target)
188
+ active: !!container && !container.clone?.() && this.#accepts(container),
178
189
  };
179
190
  }
180
191
 
@@ -271,18 +282,30 @@ export class SortableController {
271
282
  const container = this.#containers.get(id);
272
283
  const active = this.#active;
273
284
  if (!container || !active || active.mode !== 'click') return;
274
- if ((event.target as Element | null)?.closest(`[${SORTABLE_HANDLE}]`)) return;
285
+ const target = event.target as Element | null;
286
+ if (target?.closest(`[${SORTABLE_HANDLE}]`)) return;
287
+ // A click in a nested container belongs to that one (it bubbles up here)
288
+ if (target?.closest(CONTAINER) !== container.element()) return;
275
289
  if (!this.#accepts(container)) return;
276
290
  if (container !== active.current.container) this.#moveTo(container, container.items().length);
277
291
  this.drop();
278
292
  }
279
293
 
280
294
  pickUp(container: SortableContainer, index: number, mode: Mode) {
281
- const item = container.items()[index];
282
- if (item === undefined) return;
295
+ const original = container.items()[index];
296
+ if (original === undefined) return;
297
+ // A source carries a copy – made now, so the targets decide on it (`accepts`)
298
+ const clone = container.clone?.();
299
+ const item = clone ? clone(original) : original;
283
300
  const count = container.items().length;
284
- this.#active = { item, origin: { container, index }, current: { container, index, count }, mode };
285
- announce(container.texts().picked(container.itemLabel(item), index + 1, count));
301
+ this.#active = {
302
+ item,
303
+ copyOf: clone ? original : undefined,
304
+ origin: { container, index },
305
+ current: { container, index, count },
306
+ mode,
307
+ };
308
+ announce(container.texts().picked(container.itemLabel(original), index + 1, count));
286
309
  this.#notify();
287
310
  }
288
311
 
@@ -292,6 +315,12 @@ export class SortableController {
292
315
  this.#active = null;
293
316
  const { container, index, count } = active.current;
294
317
  const { origin } = active;
318
+ // A copy still at its source was not put anywhere
319
+ if (active.copyOf !== undefined && container === origin.container) {
320
+ announce(container.texts().copyCancelled(container.itemLabel(active.copyOf)));
321
+ this.#notify();
322
+ return;
323
+ }
295
324
  announce(container.texts().dropped(container.itemLabel(active.item), index + 1, count));
296
325
  if (origin.container !== container || origin.index !== index) {
297
326
  container.onMove?.({
@@ -311,8 +340,9 @@ export class SortableController {
311
340
  this.#moveTo(origin.container, origin.index, false);
312
341
  const count = active.current.count;
313
342
  this.#active = null;
314
- const label = origin.container.itemLabel(active.item);
315
- announce(origin.container.texts().cancelled(label, origin.index + 1, count));
343
+ const texts = origin.container.texts();
344
+ if (active.copyOf !== undefined) announce(texts.copyCancelled(origin.container.itemLabel(active.copyOf)));
345
+ else announce(texts.cancelled(origin.container.itemLabel(active.item), origin.index + 1, count));
316
346
  this.#focus(origin.container, origin.index);
317
347
  this.#notify();
318
348
  }
@@ -328,16 +358,36 @@ export class SortableController {
328
358
  const active = this.#active;
329
359
  if (!active) return;
330
360
  const { container, index: from } = active.current;
361
+ const origin = active.origin;
362
+ if (active.copyOf !== undefined && target === origin.container) {
363
+ // A source is not sorted; a copy that comes back leaves the container it was put in
364
+ if (container === target) return;
365
+ const rest = [...container.items()];
366
+ rest.splice(from, 1);
367
+ container.setItems(rest);
368
+ const count = target.items().length;
369
+ active.current = { container: target, index: origin.index, count };
370
+ if (speak) announce(target.texts().picked(target.itemLabel(active.copyOf), origin.index + 1, count));
371
+ if (active.mode !== 'pointer') this.#focus(target, origin.index);
372
+ this.#notify();
373
+ return;
374
+ }
331
375
  let next: unknown[];
332
376
  if (target === container) {
333
377
  next = moveItem(target.items(), from, index);
334
378
  target.setItems(next);
335
379
  } else {
336
380
  if (!this.#accepts(target)) return;
337
- const moved = moveBetween(container.items(), target.items(), from, index);
338
- container.setItems(moved.from);
339
- target.setItems(moved.to);
340
- next = moved.to;
381
+ if (active.copyOf !== undefined && container === origin.container) {
382
+ // The copy leaves its source: put into the target, the source keeps its item
383
+ next = [...target.items()];
384
+ next.splice(Math.max(0, Math.min(index, next.length)), 0, active.item);
385
+ } else {
386
+ const moved = moveBetween(container.items(), target.items(), from, index);
387
+ container.setItems(moved.from);
388
+ next = moved.to;
389
+ }
390
+ target.setItems(next);
341
391
  }
342
392
  const position = Math.max(0, Math.min(index, next.length - 1));
343
393
  active.current = { container: target, index: position, count: next.length };
@@ -357,10 +407,20 @@ export class SortableController {
357
407
  const active = this.#active;
358
408
  if (!active || container.disabled()) return false;
359
409
  if (container === active.current.container) return true;
360
- if (!this.#sameGroup(container)) return false;
410
+ // A source takes nothing – only its own copy back
411
+ if (container.clone?.()) return active.copyOf !== undefined && container === active.origin.container;
412
+ if (!this.#sameGroup(container) || this.#inside(container)) return false;
361
413
  return container.accepts?.(active.item, active.current.container.id) ?? true;
362
414
  }
363
415
 
416
+ /** The container is nested in the carried item – it cannot take the item it is part of */
417
+ #inside(container: SortableContainer): boolean {
418
+ const { current } = this.#active!;
419
+ const item = current.container.element()?.querySelector(`:scope > [${INDEX}="${current.index}"]`);
420
+ const element = container.element();
421
+ return !!item && !!element && item.contains(element);
422
+ }
423
+
364
424
  #sameGroup(container: SortableContainer) {
365
425
  const group = this.#active?.current.container.group();
366
426
  return group !== undefined && container.group() === group;
@@ -386,12 +446,13 @@ export class SortableController {
386
446
  /** Container and index under the pointer: an item (its place) or the empty area of a container (the end) */
387
447
  #targetAt(x: number, y: number): { container: SortableContainer; index: number } | undefined {
388
448
  const element = document.elementFromPoint(x, y);
389
- const item = element?.closest<HTMLElement>('[data-sortable-index]');
390
- const owner = (item ?? element)?.closest<HTMLElement>('[data-sortable-container]');
449
+ // The innermost container: over a nested one (also its empty area) the item goes there, not to its parent
450
+ const owner = element?.closest<HTMLElement>(CONTAINER);
391
451
  const container = owner ? this.#containers.get(owner.dataset.sortableContainer ?? '') : undefined;
392
452
  if (!container || !this.#accepts(container)) return undefined;
393
453
  const active = this.#active!;
394
- if (item && owner?.contains(item)) return { container, index: Number(item.dataset.sortableIndex) };
454
+ const item = element?.closest<HTMLElement>(`[${INDEX}]`);
455
+ if (item && item.parentElement === owner) return { container, index: Number(item.dataset.sortableIndex) };
395
456
  const end = container.items().length - (container === active.current.container ? 1 : 0);
396
457
  return { container, index: end };
397
458
  }
@@ -0,0 +1,52 @@
1
+ // Steps of a tour (Tour) that need the app first: `before` runs (e.g. switches a tab), then the target is awaited
2
+ // for up to `waitFor` ms. Framework-neutral – Vue, React and React Native show the step once it is ready.
3
+
4
+ /** What a step needs before it is shown */
5
+ export interface TourStepPreparation<E> {
6
+ /** Runs first, may be async */
7
+ before?: () => void | Promise<void>;
8
+ /** Finds the target; without it the step has none */
9
+ find?: () => E | null | undefined;
10
+ /** How long (ms) to wait for the target after `before` (default 0: look once) */
11
+ waitFor?: number;
12
+ }
13
+
14
+ const POLL = 50;
15
+
16
+ /**
17
+ * Prepares a step: runs `before`, then looks for the target until it is there or `waitFor` has passed. `ready` gets
18
+ * the target – null without one or when it did not appear (`missing`). Returns the cancel function (the tour moved
19
+ * on or ended): `ready` is not called after it.
20
+ */
21
+ export function prepareTourStep<E>(
22
+ step: TourStepPreparation<E>,
23
+ ready: (target: E | null, missing: boolean) => void
24
+ ): () => void {
25
+ let cancelled = false;
26
+ let timer: ReturnType<typeof setTimeout> | undefined;
27
+ const look = (until: number) => {
28
+ if (cancelled) return;
29
+ const target = step.find?.() ?? null;
30
+ if (target || !step.find) return ready(target, false);
31
+ if (Date.now() >= until) return ready(null, true);
32
+ timer = setTimeout(() => look(until), Math.min(POLL, until - Date.now()));
33
+ };
34
+ Promise.resolve()
35
+ .then(step.before)
36
+ // A failing `before` does not stop the tour – the target decides
37
+ .catch((error: unknown) => console.error(error))
38
+ .then(() => look(Date.now() + (step.waitFor ?? 0)));
39
+ return () => {
40
+ cancelled = true;
41
+ clearTimeout(timer);
42
+ };
43
+ }
44
+
45
+ /**
46
+ * The step to go to instead of a skipped one at `index`, in the direction the tour was moving (from `current`):
47
+ * the next or previous index, `'end'` past the last step, undefined before the first (stay)
48
+ */
49
+ export function tourSkipTarget(index: number, current: number, count: number): number | 'end' | undefined {
50
+ if (index > current) return index + 1 < count ? index + 1 : 'end';
51
+ return index > 0 ? index - 1 : undefined;
52
+ }