@arraypress/waveform-sounds 2.1.0 → 2.2.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 (51) hide show
  1. package/README.md +21 -1
  2. package/dist/waveform-sounds-no-autoinit.esm.js +445 -144
  3. package/dist/waveform-sounds-render.esm.js +220 -78
  4. package/dist/waveform-sounds.css +4 -0
  5. package/dist/waveform-sounds.esm.js +445 -144
  6. package/dist/waveform-sounds.js +445 -144
  7. package/dist/waveform-sounds.min.css +1 -1
  8. package/dist/waveform-sounds.min.js +1 -1
  9. package/package.json +1 -1
  10. package/src/css/waveform-sounds.css +4 -0
  11. package/src/js/core/WaveformSounds.ts +224 -67
  12. package/src/js/core/element.ts +1 -1
  13. package/src/js/core/options.ts +1 -0
  14. package/src/js/data/keys.ts +156 -0
  15. package/src/js/data/navigation.ts +35 -8
  16. package/src/js/data/sounds.ts +29 -22
  17. package/src/js/data/url-state.ts +7 -3
  18. package/src/js/dom/draw.ts +16 -2
  19. package/src/js/dom/menus.ts +2 -1
  20. package/src/js/dom/rows.ts +38 -12
  21. package/src/js/render/icons.ts +59 -16
  22. package/src/js/render/markup.ts +115 -49
  23. package/src/js/render/options.ts +3 -1
  24. package/src/js/render/strings.ts +53 -1
  25. package/src/js/types.ts +24 -0
  26. package/types/core/WaveformSounds.d.ts +3 -3
  27. package/types/core/WaveformSounds.d.ts.map +1 -1
  28. package/types/core/options.d.ts.map +1 -1
  29. package/types/data/keys.d.ts +88 -0
  30. package/types/data/keys.d.ts.map +1 -0
  31. package/types/data/navigation.d.ts +27 -9
  32. package/types/data/navigation.d.ts.map +1 -1
  33. package/types/data/sounds.d.ts +20 -5
  34. package/types/data/sounds.d.ts.map +1 -1
  35. package/types/data/url-state.d.ts +6 -1
  36. package/types/data/url-state.d.ts.map +1 -1
  37. package/types/dom/draw.d.ts +5 -2
  38. package/types/dom/draw.d.ts.map +1 -1
  39. package/types/dom/menus.d.ts.map +1 -1
  40. package/types/dom/rows.d.ts +19 -8
  41. package/types/dom/rows.d.ts.map +1 -1
  42. package/types/render/icons.d.ts +41 -3
  43. package/types/render/icons.d.ts.map +1 -1
  44. package/types/render/markup.d.ts +27 -4
  45. package/types/render/markup.d.ts.map +1 -1
  46. package/types/render/options.d.ts +1 -1
  47. package/types/render/options.d.ts.map +1 -1
  48. package/types/render/strings.d.ts +22 -1
  49. package/types/render/strings.d.ts.map +1 -1
  50. package/types/types.d.ts +24 -0
  51. package/types/types.d.ts.map +1 -1
@@ -72,6 +72,94 @@
72
72
  });
73
73
  }
74
74
 
75
+ // src/js/data/keys.ts
76
+ var KEY_PITCH = {
77
+ C: 0,
78
+ "B#": 0,
79
+ "C#": 1,
80
+ Db: 1,
81
+ D: 2,
82
+ "D#": 3,
83
+ Eb: 3,
84
+ E: 4,
85
+ Fb: 4,
86
+ "E#": 5,
87
+ F: 5,
88
+ "F#": 6,
89
+ Gb: 6,
90
+ G: 7,
91
+ "G#": 8,
92
+ Ab: 8,
93
+ A: 9,
94
+ "A#": 10,
95
+ Bb: 10,
96
+ B: 11,
97
+ Cb: 11
98
+ };
99
+ var CONVENTIONAL_MAJOR = ["C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"];
100
+ var CONVENTIONAL_MINOR = ["Cm", "C#m", "Dm", "Ebm", "Em", "Fm", "F#m", "Gm", "G#m", "Am", "Bbm", "Bm"];
101
+ var SHARPS = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
102
+ var FLATS = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
103
+ function parseKey(key) {
104
+ const m = /^([A-G][#b]?)(m?)$/.exec(String(key ?? ""));
105
+ if (!m || KEY_PITCH[m[1]] === void 0) return null;
106
+ return { root: m[1], pitch: KEY_PITCH[m[1]], minor: m[2] === "m" };
107
+ }
108
+ function keyClass(key) {
109
+ const k = parseKey(key);
110
+ return k ? `${k.pitch}${k.minor ? "m" : ""}` : key;
111
+ }
112
+ function sameKey(a, b, merge = true) {
113
+ return a === b || merge && !!a && keyClass(a) === keyClass(b);
114
+ }
115
+ function keyRank(key) {
116
+ if (!key) return Infinity;
117
+ const k = parseKey(key);
118
+ return k ? k.pitch * 2 + (k.minor ? 1 : 0) : 1e3;
119
+ }
120
+ function enharmonics(key) {
121
+ const k = parseKey(key);
122
+ if (!k) return key ? [key] : [];
123
+ const mode = k.minor ? "m" : "";
124
+ return [.../* @__PURE__ */ new Set([key, SHARPS[k.pitch] + mode, FLATS[k.pitch] + mode])];
125
+ }
126
+ function canonicalSpelling(counts) {
127
+ let best = 0;
128
+ for (const n of counts.values()) if (n > best) best = n;
129
+ const tied = [...counts].filter(([, n]) => n === best).map(([k2]) => k2).sort();
130
+ if (tied.length === 1) return tied[0];
131
+ const k = parseKey(tied[0]);
132
+ const conventional = k ? (k.minor ? CONVENTIONAL_MINOR : CONVENTIONAL_MAJOR)[k.pitch] : "";
133
+ return tied.includes(conventional) ? conventional : tied[0];
134
+ }
135
+ function keyEntries(keys, merge = true) {
136
+ const groups = /* @__PURE__ */ new Map();
137
+ for (const key of keys) {
138
+ if (!key) continue;
139
+ const cls = merge ? keyClass(key) : key;
140
+ const g = groups.get(cls) ?? /* @__PURE__ */ new Map();
141
+ groups.set(cls, g);
142
+ g.set(key, (g.get(key) || 0) + 1);
143
+ }
144
+ const out = [];
145
+ const aliases = {};
146
+ for (const g of groups.values()) {
147
+ const entry = canonicalSpelling(g);
148
+ out.push(entry);
149
+ const others = [...g.keys()].filter((k) => k !== entry).sort();
150
+ if (others.length) aliases[entry] = others;
151
+ }
152
+ out.sort((a, b) => keyRank(a) - keyRank(b) || a.localeCompare(b));
153
+ return { keys: out, aliases };
154
+ }
155
+ function keyEntry(key, entries, merge = true) {
156
+ if (!key) return "";
157
+ if (entries.includes(key)) return key;
158
+ if (!merge) return "";
159
+ const cls = keyClass(key);
160
+ return entries.find((e) => keyClass(e) === cls) ?? "";
161
+ }
162
+
75
163
  // src/js/data/sounds.ts
76
164
  function encodePeaks(peaks) {
77
165
  if (!Array.isArray(peaks)) return "";
@@ -118,14 +206,6 @@
118
206
  const minor = m[3] && /^m(in(or)?)?$/i.test(m[3]) && m[3] !== "M";
119
207
  return root + acc + (minor ? "m" : "");
120
208
  }
121
- var KEY_PITCH = { C: 0, "C#": 1, Db: 1, D: 2, "D#": 3, Eb: 3, E: 4, F: 5, "F#": 6, Gb: 6, G: 7, "G#": 8, Ab: 8, A: 9, "A#": 10, Bb: 10, B: 11 };
122
- function keyRank(key) {
123
- if (!key) return Infinity;
124
- const minor = key.endsWith("m");
125
- const root = minor ? key.slice(0, -1) : key;
126
- const p = KEY_PITCH[root];
127
- return p === void 0 ? 1e3 : p * 2 + (minor ? 1 : 0);
128
- }
129
209
  function formatDuration(seconds) {
130
210
  if (seconds == null || seconds === "") return "";
131
211
  const s = Number(seconds);
@@ -199,13 +279,13 @@
199
279
  });
200
280
  return out;
201
281
  }
202
- function facets(sounds) {
282
+ function facets(sounds, options = {}) {
203
283
  const types = /* @__PURE__ */ new Map();
204
- const keys = /* @__PURE__ */ new Set();
284
+ const keys = [];
205
285
  let min = Infinity, max = -Infinity, hasDuration = false, loops = 0;
206
286
  for (const s of sounds) {
207
287
  if (s.type) types.set(s.type, (types.get(s.type) || 0) + 1);
208
- if (s.key) keys.add(s.key);
288
+ if (s.key) keys.push(s.key);
209
289
  if (s.bpm != null) {
210
290
  min = Math.min(min, s.bpm);
211
291
  max = Math.max(max, s.bpm);
@@ -213,18 +293,21 @@
213
293
  if (s.duration != null) hasDuration = true;
214
294
  if (s.loop) loops++;
215
295
  }
296
+ const entries = keyEntries(keys, options.mergeEnharmonicKeys !== false);
216
297
  return {
217
298
  types: [...types].map(([name, count]) => ({ name, count })),
218
- keys: [...keys].sort((a, b) => keyRank(a) - keyRank(b) || a.localeCompare(b)),
299
+ keys: entries.keys,
300
+ keyAliases: entries.aliases,
219
301
  bpm: min === Infinity ? null : { min, max },
220
302
  hasDuration,
221
303
  loops,
222
304
  oneShots: sounds.length - loops
223
305
  };
224
306
  }
225
- function matches(sound, filter = {}) {
307
+ function matches(sound, filter = {}, options = {}) {
308
+ const merge = options.mergeEnharmonicKeys !== false;
226
309
  if (filter.type && sound.type !== filter.type) return false;
227
- if (filter.key && sound.key !== normalizeKey(filter.key)) return false;
310
+ if (filter.key && !sameKey(sound.key, normalizeKey(filter.key), merge)) return false;
228
311
  if (filter.loop === "loop" && !sound.loop) return false;
229
312
  if (filter.loop === "one-shot" && sound.loop) return false;
230
313
  const lo = Number(filter.bpmMin), hi = Number(filter.bpmMax);
@@ -235,7 +318,7 @@
235
318
  }
236
319
  const terms = words(filter.query || "");
237
320
  if (terms.length) {
238
- const hay = [sound.title, sound.type, sound.key, ...sound.tags || []].join(" ");
321
+ const hay = [sound.title, sound.type, ...merge ? enharmonics(sound.key) : [sound.key], ...sound.tags || []].join(" ");
239
322
  const folded = fold(hay);
240
323
  const rest = terms.filter((w) => !(/^\d+$/.test(w) && !folded.includes(w) && sound.bpm != null && Math.round(sound.bpm) === Number(w)));
241
324
  if (rest.length && !matchesAll(hay, rest.join(" "))) return false;
@@ -280,6 +363,16 @@
280
363
  if (!peaks?.length || count <= 0) return [];
281
364
  if (peaks.length === count) return peaks;
282
365
  const out = new Array(count);
366
+ if (count > peaks.length) {
367
+ const last = peaks.length - 1;
368
+ const ratio = peaks.length / count;
369
+ for (let i = 0; i < count; i++) {
370
+ const x = Math.min(Math.max((i + 0.5) * ratio - 0.5, 0), last);
371
+ const j = Math.floor(x), t = x - j;
372
+ out[i] = j >= last ? peaks[last] : peaks[j] + (peaks[j + 1] - peaks[j]) * t;
373
+ }
374
+ return out;
375
+ }
283
376
  const step = peaks.length / count;
284
377
  for (let i = 0; i < count; i++) {
285
378
  const start = Math.floor(i * step);
@@ -509,7 +602,7 @@
509
602
  const p = partsOf(menu);
510
603
  let first = null;
511
604
  for (const opt of optionsOf(p)) {
512
- opt.hidden = !optionMatches(opt.textContent, query);
605
+ opt.hidden = !optionMatches(`${opt.textContent} ${opt.dataset.alt ?? ""}`, query);
513
606
  if (!opt.hidden) first ??= opt;
514
607
  }
515
608
  p.none.hidden = first !== null;
@@ -625,9 +718,15 @@
625
718
  function sliderEdgeTarget(key) {
626
719
  return key === "Home" ? 0 : key === "End" ? SEEK_END : null;
627
720
  }
628
- function pageWindow(shown, limit) {
629
- const visible = new Set(shown.slice(0, limit));
630
- return { visible, remaining: shown.length - visible.size };
721
+ var RENDER_CHUNK = 50;
722
+ function renderedCount(matching, limit, budget) {
723
+ return Math.min(matching, limit === Infinity ? budget : limit);
724
+ }
725
+ function firstRender(pageSize) {
726
+ return pageSize > 0 ? pageSize : RENDER_CHUNK;
727
+ }
728
+ function budgetToReveal(budget, pos) {
729
+ return pos < budget ? budget : Math.ceil((pos + 1) / RENDER_CHUNK) * RENDER_CHUNK;
631
730
  }
632
731
  function limitToReveal(limit, pos) {
633
732
  return pos >= limit ? pos + 1 : limit;
@@ -704,7 +803,15 @@
704
803
  nowPlaying: "Playing {title}",
705
804
  playError: "Could not play {title}",
706
805
  loadError: "Could not load the sounds.",
707
- noSounds: "No sounds."
806
+ noSounds: "No sounds.",
807
+ rowBpm: "{bpm} BPM",
808
+ rowKey: "key {key}",
809
+ rowDuration: "{duration}",
810
+ keyMajor: "{note} major",
811
+ keyMinor: "{note} minor",
812
+ noteSharp: "{note} sharp",
813
+ noteFlat: "{note} flat",
814
+ listSeparator: ", "
708
815
  };
709
816
  var SORT_LABEL_KEYS = {
710
817
  default: "sortDefault",
@@ -725,6 +832,27 @@
725
832
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
726
833
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
727
834
  }
835
+ function spokenKey(key, strings = DEFAULT_STRINGS) {
836
+ const k = parseKey(key);
837
+ if (!k) return key;
838
+ const letter = k.root[0], acc = k.root.slice(1);
839
+ const note = acc === "#" ? fill(strings.noteSharp, { note: letter }) : acc === "b" ? fill(strings.noteFlat, { note: letter }) : letter;
840
+ return fill(k.minor ? strings.keyMinor : strings.keyMajor, { note });
841
+ }
842
+ function rowSummary(sound, columns, strings = DEFAULT_STRINGS) {
843
+ const parts = [];
844
+ for (const c of columns) {
845
+ if (c === "type" && sound.type) parts.push(sound.type);
846
+ else if (c === "bpm" && sound.bpm != null) parts.push(fill(strings.rowBpm, { bpm: sound.bpm }));
847
+ else if (c === "key" && sound.key) parts.push(fill(strings.rowKey, { key: spokenKey(sound.key, strings) }));
848
+ else if (c === "duration") {
849
+ const d = formatDuration(sound.duration);
850
+ if (d) parts.push(fill(strings.rowDuration, { duration: d }));
851
+ }
852
+ }
853
+ if (sound.loop) parts.push(strings.isLoop);
854
+ return parts.join(strings.listSeparator);
855
+ }
728
856
 
729
857
  // src/js/render/options.ts
730
858
  var FILTERS = ["type", "key", "bpm", "loop"];
@@ -739,7 +867,8 @@
739
867
  menuSearch: 8,
740
868
  pageSize: 50,
741
869
  columns: [...COLUMNS],
742
- maxTypeChips: 10
870
+ maxTypeChips: 10,
871
+ mergeEnharmonicKeys: true
743
872
  };
744
873
  var RENDER_OPTION_KEYS = [...Object.keys(RENDER_DEFAULTS), "idPrefix", "loop", "strings"];
745
874
  function pickRenderOptions(options) {
@@ -773,6 +902,7 @@
773
902
  menuSearch: wholeNumber(o.menuSearch, RENDER_DEFAULTS.menuSearch),
774
903
  maxTypeChips: wholeNumber(o.maxTypeChips, RENDER_DEFAULTS.maxTypeChips),
775
904
  pageSize: wholeNumber(o.pageSize, RENDER_DEFAULTS.pageSize),
905
+ mergeEnharmonicKeys: o.mergeEnharmonicKeys !== false,
776
906
  strings: { ...DEFAULT_STRINGS, ...defined(options.strings) }
777
907
  };
778
908
  }
@@ -846,6 +976,7 @@
846
976
  pageSize: parseNumber(d.pageSize),
847
977
  maxTypeChips: parseNumber(d.maxTypeChips),
848
978
  columns: parseList(d.columns),
979
+ mergeEnharmonicKeys: parseBool(d.mergeEnharmonicKeys),
849
980
  waveformStyle: d.waveformStyle || void 0,
850
981
  waveformColor: d.waveformColor || void 0,
851
982
  progressColor: d.progressColor || void 0,
@@ -889,19 +1020,27 @@
889
1020
  }
890
1021
 
891
1022
  // src/js/render/icons.ts
892
- function icon(path, extraClass = "") {
893
- const cls = extraClass ? `ws-icon ${extraClass}` : "ws-icon";
894
- return `<svg class="${cls}" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="${path}"/></svg>`;
895
- }
896
- var ICONS = {
897
- play: icon("M8 5.5v13l11-6.5z", "ws-icon-play"),
898
- pause: icon("M7 5h3.5v14H7zM13.5 5H17v14h-3.5z", "ws-icon-pause"),
899
- search: icon("M10.5 4a6.5 6.5 0 1 0 4.03 11.6l4.43 4.43 1.41-1.41-4.43-4.43A6.5 6.5 0 0 0 10.5 4zm0 2a4.5 4.5 0 1 1 0 9 4.5 4.5 0 0 1 0-9z"),
900
- chevron: icon("M6.4 8.6 12 14.2l5.6-5.6L19 10l-7 7-7-7z", "ws-menu-chevron"),
901
- check: icon("M9.5 16.2 5.3 12l-1.4 1.4 5.6 5.6 11-11-1.4-1.4z", "ws-menu-check"),
902
- download: icon("M11 4h2v8.6l3.3-3.3 1.4 1.4L12 16.4l-5.7-5.7 1.4-1.4 3.3 3.3zM5 18h14v2H5z"),
903
- loop: icon("M17 4l3 3-3 3V8H8a3 3 0 0 0-3 3v1H3v-1a5 5 0 0 1 5-5h9V4zM7 20l-3-3 3-3v2h9a3 3 0 0 0 3-3v-1h2v1a5 5 0 0 1-5 5H7v2z")
1023
+ var ICON_PATHS = {
1024
+ play: "M8 5.5v13l11-6.5z",
1025
+ pause: "M7 5h3.5v14H7zM13.5 5H17v14h-3.5z",
1026
+ search: "M10.5 4a6.5 6.5 0 1 0 4.03 11.6l4.43 4.43 1.41-1.41-4.43-4.43A6.5 6.5 0 0 0 10.5 4zm0 2a4.5 4.5 0 1 1 0 9 4.5 4.5 0 0 1 0-9z",
1027
+ chevron: "M6.4 8.6 12 14.2l5.6-5.6L19 10l-7 7-7-7z",
1028
+ check: "M9.5 16.2 5.3 12l-1.4 1.4 5.6 5.6 11-11-1.4-1.4z",
1029
+ download: "M11 4h2v8.6l3.3-3.3 1.4 1.4L12 16.4l-5.7-5.7 1.4-1.4 3.3 3.3zM5 18h14v2H5z",
1030
+ loop: "M17 4l3 3-3 3V8H8a3 3 0 0 0-3 3v1H3v-1a5 5 0 0 1 5-5h9V4zM7 20l-3-3 3-3v2h9a3 3 0 0 0 3-3v-1h2v1a5 5 0 0 1-5 5H7v2z"
904
1031
  };
1032
+ var ICON_NAMES = Object.keys(ICON_PATHS);
1033
+ function iconId(id, name) {
1034
+ return `${id}-i-${name}`;
1035
+ }
1036
+ function iconSprite(id, names) {
1037
+ const symbols = ICON_NAMES.filter((n) => names.includes(n)).map((n) => `<symbol id="${escapeHtml(iconId(id, n))}" viewBox="0 0 24 24"><path d="${ICON_PATHS[n]}"/></symbol>`).join("");
1038
+ return `<svg class="ws-sprite" data-ws-sprite="${escapeHtml(id)}" width="0" height="0" aria-hidden="true" focusable="false">${symbols}</svg>`;
1039
+ }
1040
+ function icon(id, name, extraClass = "") {
1041
+ const cls = extraClass ? `ws-icon ${extraClass}` : "ws-icon";
1042
+ return `<svg class="${cls}" aria-hidden="true" focusable="false"><use href="#${escapeHtml(iconId(id, name))}"/></svg>`;
1043
+ }
905
1044
 
906
1045
  // src/js/render/markup.ts
907
1046
  function idBase(sounds) {
@@ -925,11 +1064,11 @@
925
1064
  function loopsByDefault(loop, f) {
926
1065
  return loop == null ? f.loops > 0 : !!loop;
927
1066
  }
928
- function renderSearch(s) {
1067
+ function renderSearch(s, id) {
929
1068
  return h(
930
1069
  "label",
931
1070
  { class: "ws-search" },
932
- ICONS.search,
1071
+ icon(id, "search"),
933
1072
  h("span", { class: "ws-sr" }, text(s.search)),
934
1073
  h("input", { type: "search", class: "ws-search-input", "data-ws-search": true, placeholder: s.searchPlaceholder, autocomplete: "off", spellcheck: "false" })
935
1074
  );
@@ -943,7 +1082,7 @@
943
1082
  { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "listbox", "aria-expanded": "false", "aria-controls": listId },
944
1083
  m.prefix ? h("span", { class: "ws-menu-prefix" }, text(m.prefix)) : h("span", { class: "ws-sr" }, text(`${m.label}: `)),
945
1084
  h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(current.label)),
946
- ICONS.chevron
1085
+ icon(m.id, "chevron", "ws-menu-chevron")
947
1086
  );
948
1087
  const search = searchable && h("input", {
949
1088
  type: "search",
@@ -960,8 +1099,8 @@
960
1099
  });
961
1100
  const options = m.options.map((o, i) => h(
962
1101
  "li",
963
- { role: "option", id: `${m.id}-${name}-${i}`, class: "ws-menu-option", "data-value": o.value, "aria-selected": String(o === current) },
964
- ICONS.check,
1102
+ { role: "option", id: `${m.id}-${name}-${i}`, class: "ws-menu-option", "data-value": o.value, "data-alt": o.alt, "aria-selected": String(o === current) },
1103
+ icon(m.id, "check", "ws-menu-check"),
965
1104
  h("span", { class: "ws-menu-text" }, text(o.label)),
966
1105
  o.count != null && h("span", { class: "ws-menu-count" }, text(o.count))
967
1106
  ));
@@ -998,7 +1137,7 @@
998
1137
  { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
999
1138
  h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
1000
1139
  h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
1001
- ICONS.chevron
1140
+ icon(id, "chevron", "ws-menu-chevron")
1002
1141
  ),
1003
1142
  h(
1004
1143
  "div",
@@ -1033,8 +1172,8 @@
1033
1172
  button("one-shot", s.oneShots)
1034
1173
  );
1035
1174
  }
1036
- function renderLoopToggle(s, pressed) {
1037
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
1175
+ function renderLoopToggle(s, pressed, id) {
1176
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, icon(id, "loop"), h("span", {}, text(s.loop)));
1038
1177
  }
1039
1178
  function renderChip(value, label, count, pressed) {
1040
1179
  return h(
@@ -1058,7 +1197,7 @@
1058
1197
  label: s.key,
1059
1198
  value: "",
1060
1199
  placeholder: s.findKey,
1061
- options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
1200
+ options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k, alt: f.keyAliases[k]?.join(" ") }))]
1062
1201
  }),
1063
1202
  plan.bpm && renderBpmMenu({ id, s, range: bpmExtent(f.bpm) }),
1064
1203
  plan.loops && renderLoopFilter(s),
@@ -1068,7 +1207,7 @@
1068
1207
  value: plan.sorts[0],
1069
1208
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
1070
1209
  }),
1071
- plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
1210
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f), id)
1072
1211
  ].filter(Boolean);
1073
1212
  const chips = plan.types === "chips" && h(
1074
1213
  "div",
@@ -1080,7 +1219,7 @@
1080
1219
  return h(
1081
1220
  "div",
1082
1221
  { class: "ws-toolbar" },
1083
- plan.search && renderSearch(s),
1222
+ plan.search && renderSearch(s, id),
1084
1223
  controls.length > 0 && h("div", { class: "ws-controls" }, controls),
1085
1224
  // The chips and the count share a row (the count alone without chips).
1086
1225
  (chips || count) && h("div", { class: "ws-meta" }, chips, count)
@@ -1092,49 +1231,66 @@
1092
1231
  key: (sound) => text(sound.key),
1093
1232
  duration: (sound) => text(formatDuration(sound.duration))
1094
1233
  };
1095
- function renderRow(sound, index, o, hidden = false) {
1234
+ function renderRow(sound, index, o, id) {
1096
1235
  const s = o.strings;
1097
1236
  const vars = { title: sound.title };
1098
1237
  const cells = o.columns.map((c) => h("span", { class: `ws-cell ws-${c}` }, CELLS[c](sound)));
1238
+ const summary = rowSummary(sound, o.columns, s);
1239
+ const described = summary ? `${id}-d${index}` : null;
1099
1240
  return h(
1100
1241
  "li",
1101
- {
1102
- class: "ws-row",
1103
- "data-ws-index": index,
1104
- "data-ws-id": sound.id,
1105
- "data-url": sound.url,
1106
- "data-title": sound.title,
1107
- "data-type": sound.type || null,
1108
- "data-bpm": sound.bpm,
1109
- "data-key": sound.key || null,
1110
- "data-duration": sound.duration,
1111
- "data-tags": sound.tags?.length ? sound.tags.join(",") : null,
1112
- "data-peaks": sound.peaks ? encodePeaks(sound.peaks) : null,
1113
- "data-waveform": sound.waveform,
1114
- "data-download": sound.download,
1115
- "data-loop": sound.loop ? "true" : null,
1116
- hidden
1117
- },
1118
- h("button", { type: "button", class: "ws-play", "aria-pressed": "false", "aria-label": fill(s.play, vars) }, ICONS.play, ICONS.pause),
1242
+ { class: "ws-row", "data-ws-index": index },
1243
+ h(
1244
+ "button",
1245
+ { type: "button", class: "ws-play", "aria-pressed": "false", "aria-label": fill(s.play, vars), "aria-describedby": described },
1246
+ icon(id, "play", "ws-icon-play"),
1247
+ icon(id, "pause", "ws-icon-pause")
1248
+ ),
1119
1249
  h(
1120
1250
  "span",
1121
1251
  { class: "ws-cell ws-title" },
1122
1252
  h("span", { class: "ws-title-text" }, text(sound.title)),
1123
- // A loop says so beside its name; a one-shot carries nothing.
1124
- sound.loop && h("span", { class: "ws-loop-mark", title: s.isLoop }, ICONS.loop, h("span", { class: "ws-sr" }, text(s.isLoop)))
1253
+ // A loop says so beside its name (and in the read-out); a one-shot carries nothing.
1254
+ sound.loop && h("span", { class: "ws-loop-mark", title: s.isLoop, "aria-hidden": "true" }, icon(id, "loop"))
1125
1255
  ),
1126
1256
  // Wide rows: `display: contents` makes each cell a column. Narrow
1127
1257
  // rows: one line under the title.
1128
- cells.length > 0 && h("span", { class: "ws-cells" }, cells),
1129
- o.player === "inline" && h(
1130
- "span",
1131
- { class: "ws-wave", role: "slider", "aria-label": fill(s.seek, vars), "aria-valuemin": "0", "aria-valuemax": "100", "aria-valuenow": "0", tabindex: "-1" },
1132
- h("canvas", { class: "ws-canvas", "aria-hidden": "true" })
1133
- ),
1258
+ cells.length > 0 && h("span", { class: "ws-cells", "aria-hidden": "true" }, cells),
1259
+ summary && h("span", { class: "ws-sr", id: described }, text(summary)),
1260
+ o.player === "inline" && h("span", { class: "ws-wave", "aria-hidden": "true" }, h("canvas", { class: "ws-canvas" })),
1134
1261
  // Optional, per sound: a plain link. Rows without one carry nothing.
1135
- sound.download && h("a", { class: "ws-download", href: sound.download, download: true, "aria-label": fill(s.download, vars) }, ICONS.download)
1262
+ sound.download && h("a", { class: "ws-download", href: sound.download, download: true, "aria-label": fill(s.download, vars) }, icon(id, "download"))
1136
1263
  );
1137
1264
  }
1265
+ function renderSoundsData(list) {
1266
+ const data = list.map((s, i) => {
1267
+ const d = {};
1268
+ if (s.id !== `sound-${i + 1}`) d.id = s.id;
1269
+ d.url = s.url;
1270
+ d.title = s.title;
1271
+ if (s.type) d.type = s.type;
1272
+ if (s.bpm != null) d.bpm = s.bpm;
1273
+ if (s.key) d.key = s.key;
1274
+ if (s.duration != null) d.duration = s.duration;
1275
+ if (s.tags.length) d.tags = s.tags;
1276
+ if (s.peaks) d.peaks = encodePeaks(s.peaks);
1277
+ if (s.waveform) d.waveform = s.waveform;
1278
+ if (s.download) d.download = s.download;
1279
+ if (s.loop) d.loop = true;
1280
+ return d;
1281
+ });
1282
+ return `<script type="application/json" data-ws-sounds>${JSON.stringify(data).replace(/</g, "\\u003c")}<\/script>`;
1283
+ }
1284
+ function iconsUsed(plan, list) {
1285
+ const names = [];
1286
+ if (list.length) names.push("play", "pause");
1287
+ if (plan.search) names.push("search");
1288
+ if (plan.types === "menu" || plan.key || plan.sorts.length) names.push("chevron", "check");
1289
+ else if (plan.bpm) names.push("chevron");
1290
+ if (plan.loop || list.some((s) => s.loop)) names.push("loop");
1291
+ if (list.some((s) => s.download)) names.push("download");
1292
+ return names;
1293
+ }
1138
1294
  function renderFooter({ o, s, total }) {
1139
1295
  const page = o.pageSize > 0 ? o.pageSize : total;
1140
1296
  const more = Math.max(0, total - page);
@@ -1153,15 +1309,19 @@
1153
1309
  ].join("");
1154
1310
  }
1155
1311
  function renderSounds(sounds, options = {}) {
1312
+ return renderList(normalizeSounds(sounds), options, true);
1313
+ }
1314
+ function renderList(list, options, withData) {
1156
1315
  const o = resolveRenderOptions(options);
1157
- const list = normalizeSounds(sounds);
1158
- const ctx = { o, s: o.strings, f: facets(list), total: list.length, id: o.idPrefix || idBase(list) };
1159
- const page = o.pageSize > 0 ? o.pageSize : Infinity;
1160
- return renderToolbar(toolbarPlan(ctx.f, o), ctx) + h(
1316
+ const ctx = { o, s: o.strings, f: facets(list, o), total: list.length, id: o.idPrefix || idBase(list) };
1317
+ const plan = toolbarPlan(ctx.f, o);
1318
+ const index = new Map(list.map((sound, i) => [sound, i]));
1319
+ const first = sortSounds(list, availableSorts(o.sorts, ctx.f)[0] || "default").slice(0, firstRender(o.pageSize));
1320
+ return iconSprite(ctx.id, iconsUsed(plan, list)) + renderToolbar(plan, ctx) + h(
1161
1321
  "ul",
1162
1322
  { class: `ws-list ws-list--${o.player}`, role: "list", "data-ws-list": true },
1163
- list.map((sound, i) => renderRow(sound, i, o, i >= page))
1164
- ) + renderFooter(ctx);
1323
+ first.map((sound) => renderRow(sound, index.get(sound), o, ctx.id))
1324
+ ) + renderFooter(ctx) + (withData ? renderSoundsData(list) : "");
1165
1325
  }
1166
1326
  function renderOptionAttributes(options = {}) {
1167
1327
  const set = pickRenderOptions(options);
@@ -1190,6 +1350,17 @@
1190
1350
 
1191
1351
  // src/js/dom/rows.ts
1192
1352
  var ROW = ":scope > [data-ws-index]";
1353
+ function readSoundsData(root) {
1354
+ const script = root.querySelector(":scope > script[data-ws-sounds]");
1355
+ if (!script) return null;
1356
+ try {
1357
+ const list = JSON.parse(script.textContent || "");
1358
+ if (Array.isArray(list)) return normalizeSounds(list);
1359
+ } catch {
1360
+ }
1361
+ console.warn(`${LOG} Ignoring unreadable sounds JSON (data-ws-sounds)`);
1362
+ return null;
1363
+ }
1193
1364
  function readRows(list) {
1194
1365
  const rows = [...list.querySelectorAll(ROW)].sort((a, b) => Number(a.dataset.wsIndex) - Number(b.dataset.wsIndex));
1195
1366
  const sounds = [];
@@ -1228,10 +1399,14 @@
1228
1399
  function visibleRows(list) {
1229
1400
  return [...list.querySelectorAll(ROW)].filter((r) => !r.hidden);
1230
1401
  }
1231
- function orderRows(list, rows) {
1232
- const frag = document.createDocumentFragment();
1233
- for (const row of rows) if (row) frag.appendChild(row);
1234
- list.appendChild(frag);
1402
+ function layoutRows(list, rows) {
1403
+ const keep = new Set(rows);
1404
+ for (const el of [...list.children]) if (!keep.has(el)) el.remove();
1405
+ let cursor = list.firstElementChild;
1406
+ for (const row of rows) {
1407
+ if (row === cursor) cursor = cursor.nextElementSibling;
1408
+ else list.insertBefore(row, cursor);
1409
+ }
1235
1410
  }
1236
1411
 
1237
1412
  // src/js/dom/colors.ts
@@ -1295,7 +1470,7 @@
1295
1470
  const lo = min ?? "", hi = max ?? "";
1296
1471
  return lo !== "" || hi !== "" ? `${lo}-${hi}` : "";
1297
1472
  }
1298
- function readUrlState(search, keys, available, sorts) {
1473
+ function readUrlState(search, keys, available, sorts, options = {}) {
1299
1474
  const out = { filter: {}, sort: null };
1300
1475
  if (!keys) return out;
1301
1476
  const sp = new URLSearchParams(search);
@@ -1303,8 +1478,8 @@
1303
1478
  if (q) out.filter.query = q;
1304
1479
  const type = sp.get(keys.type);
1305
1480
  if (type && available.types.some((t) => t.name === type)) out.filter.type = type;
1306
- const key = normalizeKey(sp.get(keys.key));
1307
- if (key && available.keys.includes(key)) out.filter.key = key;
1481
+ const key = keyEntry(normalizeKey(sp.get(keys.key)), available.keys, options.mergeEnharmonicKeys !== false);
1482
+ if (key) out.filter.key = key;
1308
1483
  const parsed = parseBpmRange(sp.get(keys.bpm));
1309
1484
  const bpm = parsed && available.bpm !== void 0 ? bpmRangeWithin(parsed, available.bpm) : parsed;
1310
1485
  if (bpm) Object.assign(out.filter, bpm);
@@ -1354,6 +1529,9 @@
1354
1529
  this.filter = { ...NO_FILTER };
1355
1530
  this.sortBy = "default";
1356
1531
  this.limit = this._pageSize();
1532
+ this._budget = this._firstBudget();
1533
+ this._index = /* @__PURE__ */ new Map();
1534
+ this._failed = /* @__PURE__ */ new Set();
1357
1535
  this.currentIndex = null;
1358
1536
  this.playing = false;
1359
1537
  this.progress = 0;
@@ -1386,28 +1564,31 @@
1386
1564
  const adopted = el.querySelector("[data-ws-list]");
1387
1565
  if (adopted) {
1388
1566
  this._adoptedHTML = el.innerHTML;
1389
- this.sounds = readRows(adopted);
1567
+ this.sounds = readSoundsData(el) ?? readRows(adopted);
1390
1568
  } else {
1391
1569
  this._originalHTML = el.innerHTML;
1392
1570
  this.sounds = await this._loadSounds();
1393
1571
  if (this.destroyed) return;
1394
- el.innerHTML = renderSounds(this.sounds, { ...this.render, idPrefix: this.options.idPrefix || el.id || void 0 });
1572
+ el.innerHTML = renderList(this.sounds, { ...this.render, idPrefix: this.options.idPrefix || el.id || void 0 }, false);
1395
1573
  }
1396
1574
  if (this.destroyed) return;
1575
+ this._index = new Map(this.sounds.map((sound, i) => [sound, i]));
1397
1576
  this._addedClasses = ["waveform-sounds", `waveform-sounds--${this.render.player}`].filter((c) => !el.classList.contains(c));
1398
1577
  el.classList.add(...this._addedClasses);
1578
+ this._facets = facets(this.sounds, this.render);
1399
1579
  this._cacheRefs();
1400
1580
  this._bind();
1401
1581
  this._observe();
1402
1582
  this._resolveColors();
1403
- if (!this._loopSet) this.loop = loopsByDefault(this.options.loop, facets(this.sounds));
1583
+ if (!this._loopSet) this.loop = loopsByDefault(this.options.loop, this._facets);
1404
1584
  this._setLoop(this.loop);
1405
- this._bpmExtent = bpmExtent(facets(this.sounds).bpm);
1406
- this._sorts = availableSorts(this.render.sorts, facets(this.sounds));
1585
+ this._bpmExtent = bpmExtent(this._facets.bpm);
1586
+ this._sorts = availableSorts(this.render.sorts, this._facets);
1407
1587
  if (!this._sortSet) this.sortBy = this._sorts[0] || "default";
1408
1588
  this._readUrl();
1589
+ this.filter = { ...this.filter, key: this._keyEntry(this.filter.key) };
1409
1590
  this._syncControls();
1410
- this._apply({ resort: this.sortBy !== "default" });
1591
+ this._apply();
1411
1592
  if (this._playerClass()) this._ensureEngine();
1412
1593
  if (this.render.player === "strip" && this.engine && this.currentIndex == null) this._cueFirst();
1413
1594
  this._emit("ready", { sounds: this.sounds.length });
@@ -1462,9 +1643,42 @@
1462
1643
  chips: [...this.container.querySelectorAll("[data-ws-type]")],
1463
1644
  loopFilter: [...this.container.querySelectorAll("[data-ws-loop-filter]")]
1464
1645
  };
1465
- this.rows = indexRows(this.$.list);
1646
+ const sprite = q("[data-ws-sprite]");
1647
+ this._iconId = sprite?.dataset.wsSprite ?? (this.options.idPrefix || this.container.id || idBase(this.sounds));
1648
+ if (!sprite) this.$.list.insertAdjacentHTML("beforebegin", iconSprite(this._iconId, ICON_NAMES));
1649
+ this.rows = [];
1466
1650
  this._active = null;
1467
- for (const row of this.rows) if (row) this._rowStops(row, false);
1651
+ for (const row of indexRows(this.$.list)) if (row) this._adoptRow(row);
1652
+ }
1653
+ /**
1654
+ * Take a row (server-rendered, or just rendered) into the list's
1655
+ * keeping: its index, its Tab stops off, the state of its sound (current,
1656
+ * failed), and the observer that draws it.
1657
+ * @internal
1658
+ */
1659
+ _adoptRow(row) {
1660
+ const i = Number(row.dataset.wsIndex);
1661
+ this.rows[i] = row;
1662
+ this._rowStops(row, false);
1663
+ const wave = row.querySelector(".ws-wave");
1664
+ if (i === this.currentIndex || this._failed.has(i) || wave?.hasAttribute("role")) this._paintRow(i);
1665
+ if (i === this.currentIndex) this._paintSlider();
1666
+ if (this._io) this._io.observe(row);
1667
+ else this._drawn?.add(i);
1668
+ }
1669
+ /**
1670
+ * Rows for these sounds, rendered (in one pass) where they have none.
1671
+ * @returns The rows, in the order given.
1672
+ * @internal
1673
+ */
1674
+ _rowsFor(indices) {
1675
+ const missing = indices.filter((i) => !this.rows[i]);
1676
+ if (missing.length) {
1677
+ const tpl = document.createElement("template");
1678
+ tpl.innerHTML = missing.map((i) => renderRow(this.sounds[i], i, this.render, this._iconId)).join("");
1679
+ for (const row of [...tpl.content.children]) this._adoptRow(row);
1680
+ }
1681
+ return indices.map((i) => this.rows[i]);
1468
1682
  }
1469
1683
  /**
1470
1684
  * Make a row's own controls (play, download, and the seek slider when it
@@ -1477,7 +1691,7 @@
1477
1691
  const dl = row.querySelector(".ws-download");
1478
1692
  if (dl) dl.tabIndex = on ? 0 : -1;
1479
1693
  const wave = row.querySelector(".ws-wave");
1480
- if (wave) wave.tabIndex = on && Number(row.dataset.wsIndex) === this.currentIndex ? 0 : -1;
1694
+ if (wave && Number(row.dataset.wsIndex) === this.currentIndex) wave.tabIndex = on ? 0 : -1;
1481
1695
  }
1482
1696
  /**
1483
1697
  * Move the list's Tab stop to `row` (roving tabindex).
@@ -1639,16 +1853,35 @@
1639
1853
  this._seekRow(index, seek);
1640
1854
  return;
1641
1855
  }
1642
- const rows = visibleRows(this.$.list);
1643
- const target = rowTarget(e.key, rows.indexOf(row), rows.length);
1856
+ const shown = this._shown ?? [];
1857
+ const target = rowTarget(e.key, shown.indexOf(this.sounds[index]), Math.min(shown.length, this.limit));
1644
1858
  if (target === null) return;
1645
1859
  e.preventDefault();
1646
1860
  if (target === "search") {
1647
1861
  this.$.search?.focus();
1648
1862
  return;
1649
1863
  }
1650
- this._focusRow(rows[target]);
1651
- if (this.options.arrowAudition && this.playing) this.play(Number(rows[target].dataset.wsIndex));
1864
+ this._focusRow(this._reveal(target));
1865
+ if (this.options.arrowAudition && this.playing) this.play(this._index.get(shown[target]));
1866
+ }
1867
+ /**
1868
+ * The row at position `pos` of the matching sounds, revealing it first:
1869
+ * past the page, the page grows to it (as `next()` always did); with
1870
+ * paging off, rows are rendered up to it.
1871
+ * @internal
1872
+ */
1873
+ _reveal(pos) {
1874
+ const sound = this._shown?.[pos];
1875
+ if (!sound) return void 0;
1876
+ const limit = limitToReveal(this.limit, pos);
1877
+ if (limit !== this.limit) {
1878
+ this.limit = limit;
1879
+ this._apply();
1880
+ } else if (this.limit === Infinity && pos >= this._budget) {
1881
+ this._budget = budgetToReveal(this._budget, pos);
1882
+ this._layout();
1883
+ }
1884
+ return this.rows[this._index.get(sound)];
1652
1885
  }
1653
1886
  /**
1654
1887
  * Focus a row's play button.
@@ -1758,7 +1991,7 @@
1758
1991
  const row = this.rows[index];
1759
1992
  const canvas = row?.querySelector("canvas");
1760
1993
  if (!canvas) return;
1761
- if (row.hidden) {
1994
+ if (!row.isConnected) {
1762
1995
  this._dirty?.add(index);
1763
1996
  return;
1764
1997
  }
@@ -1793,8 +2026,8 @@
1793
2026
  _readUrl() {
1794
2027
  const keys = urlKeys(this.options.urlState);
1795
2028
  if (!keys || typeof location === "undefined") return;
1796
- const f = facets(this.sounds);
1797
- const { filter, sort } = readUrlState(location.search, keys, { ...f, bpm: bpmExtent(f.bpm) }, this._sorts);
2029
+ const f = this._facets;
2030
+ const { filter, sort } = readUrlState(location.search, keys, { ...f, bpm: bpmExtent(f.bpm) }, this._sorts, this.render);
1798
2031
  this.filter = { ...this.filter, ...filter };
1799
2032
  if (sort) {
1800
2033
  this.sortBy = sort;
@@ -1826,13 +2059,26 @@
1826
2059
  * @internal
1827
2060
  */
1828
2061
  _match() {
1829
- return sortSounds(this.sounds.filter((s) => matches(s, this.filter)), this.sortBy);
2062
+ return sortSounds(this.sounds.filter((s) => matches(s, this.filter, this.render)), this.sortBy);
2063
+ }
2064
+ /**
2065
+ * A key filter in the list's own spelling: with enharmonic keys merged,
2066
+ * `A#m` on a list whose entry is `Bbm` becomes `Bbm` (so the menu and
2067
+ * the address show the entry). A key the list doesn't have stays as
2068
+ * given (it matches nothing, as before).
2069
+ * @internal
2070
+ */
2071
+ _keyEntry(key) {
2072
+ const k = normalizeKey(key);
2073
+ return this._facets && keyEntry(k, this._facets.keys, this.render.mergeEnharmonicKeys) || k;
1830
2074
  }
1831
2075
  setFilter(patch = {}) {
1832
2076
  this.filter = { ...this.filter, ...patch };
2077
+ if (patch.key) this.filter.key = this._keyEntry(patch.key);
1833
2078
  this.limit = this._pageSize();
2079
+ this._budget = this._firstBudget();
1834
2080
  this._syncControls();
1835
- this._apply({ resort: false });
2081
+ this._apply();
1836
2082
  }
1837
2083
  /** Reset every filter (the sort stays). */
1838
2084
  clearFilters() {
@@ -1843,13 +2089,13 @@
1843
2089
  this._sortSet = true;
1844
2090
  this.sortBy = SORTS.includes(by) ? by : "default";
1845
2091
  this.menus?.setValue("sort", this.sortBy);
1846
- this._apply({ resort: true });
2092
+ this._apply();
1847
2093
  }
1848
2094
  /** Reveal the next page of results. */
1849
2095
  showMore() {
1850
2096
  if (this.limit === Infinity) return;
1851
2097
  this.limit += this.render.pageSize;
1852
- this._apply({ resort: false });
2098
+ this._apply();
1853
2099
  }
1854
2100
  /**
1855
2101
  * Rows per page (Infinity when paging is off).
@@ -1858,6 +2104,15 @@
1858
2104
  _pageSize() {
1859
2105
  return this.render.pageSize > 0 ? this.render.pageSize : Infinity;
1860
2106
  }
2107
+ /**
2108
+ * Paging off: the rows a list (or a new filter) starts with — a chunk,
2109
+ * more as the list scrolls (see `_watchEnd`); all of them where nothing
2110
+ * can tell it scrolled (no IntersectionObserver).
2111
+ * @internal
2112
+ */
2113
+ _firstBudget() {
2114
+ return typeof IntersectionObserver === "undefined" ? Infinity : RENDER_CHUNK;
2115
+ }
1861
2116
  /**
1862
2117
  * Show the current filter and sort in the controls (they can be set from
1863
2118
  * code or the address, not only by the controls themselves).
@@ -1897,27 +2152,17 @@
1897
2152
  $.bpmRange.classList.toggle("is-active", lo > extent.min || hi < extent.max);
1898
2153
  }
1899
2154
  /**
1900
- * Lay the rows out for the current filter, sort and page: order, hide,
2155
+ * Lay the list out for the current filter, sort and page: its rows,
1901
2156
  * "Show more", the empty state and the count. Then report it.
1902
- *
1903
- * @param opts - Reorder the rows even if the sort
1904
- * hasn't changed.
1905
2157
  * @internal
1906
2158
  */
1907
- _apply({ resort }) {
2159
+ _apply() {
1908
2160
  const $ = this.$;
1909
2161
  if (!$) return;
1910
- if (resort || this.sortBy !== this._lastSort) {
1911
- const index = new Map(this.sounds.map((s, i) => [s, i]));
1912
- orderRows($.list, sortSounds(this.sounds, this.sortBy).map((s) => this.rows[index.get(s)]));
1913
- this._lastSort = this.sortBy;
1914
- }
1915
2162
  const shown = this._match();
1916
2163
  this._shown = shown;
1917
- const { visible, remaining } = pageWindow(shown, this.limit);
1918
- this.sounds.forEach((s, i) => {
1919
- if (this.rows[i]) this.rows[i].hidden = !visible.has(s);
1920
- });
2164
+ this._layout();
2165
+ const remaining = shown.length - Math.min(shown.length, this.limit);
1921
2166
  if ($.more) {
1922
2167
  $.more.hidden = remaining <= 0;
1923
2168
  $.more.textContent = fill(this.strings.showMore, { count: Math.min(remaining, this.render.pageSize || remaining) });
@@ -1927,21 +2172,52 @@
1927
2172
  if (!this.sounds.length && $.empty.textContent !== this.strings.noSounds) $.empty.textContent = this.strings.noSounds;
1928
2173
  }
1929
2174
  if ($.count) $.count.textContent = countText(shown.length, this.sounds.length, this.strings);
1930
- if (!this._active || this._active.hidden) {
1931
- const lost = !!this._active?.contains(document.activeElement);
1932
- const first = visibleRows($.list)[0];
2175
+ if (this._cued && !this.playing) this._cueFirst();
2176
+ this._queueUrl();
2177
+ this._emit("filter", { visible: shown.length, total: this.sounds.length, filter: { ...this.filter }, sort: this.sortBy });
2178
+ this.options.onFilter?.(shown, this);
2179
+ }
2180
+ /**
2181
+ * Put exactly the rows shown in the list, in order: the page (or with
2182
+ * paging off the rows rendered so far) of the matching sounds. Rows are
2183
+ * rendered the first time they are shown; rows that stop being shown
2184
+ * leave the DOM (kept, with their drawing, for when they come back).
2185
+ * @internal
2186
+ */
2187
+ _layout() {
2188
+ const $ = this.$, shown = this._shown;
2189
+ const count = renderedCount(shown.length, this.limit, this._budget);
2190
+ const focused = $.list.contains(document.activeElement) ? document.activeElement : null;
2191
+ const rows = this._rowsFor(shown.slice(0, count).map((sound) => this._index.get(sound)));
2192
+ layoutRows($.list, rows);
2193
+ if (focused?.isConnected && document.activeElement !== focused) focused.focus({ preventScroll: true });
2194
+ if (!this._io && this._drawn) for (const row of rows) this._drawRow(Number(row.dataset.wsIndex));
2195
+ if (!this._active || !this._active.isConnected) {
2196
+ const first = rows[0];
1933
2197
  if (first) {
1934
2198
  this._rove(first);
1935
- if (lost) this._focusRowQuietly(first);
2199
+ if (focused && !focused.isConnected) this._focusRowQuietly(first);
1936
2200
  } else if (this._active) {
1937
2201
  this._rowStops(this._active, false);
1938
2202
  this._active = null;
1939
2203
  }
1940
2204
  }
1941
- if (this._cued && !this.playing) this._cueFirst();
1942
- this._queueUrl();
1943
- this._emit("filter", { visible: shown.length, total: this.sounds.length, filter: { ...this.filter }, sort: this.sortBy });
1944
- this.options.onFilter?.(shown, this);
2205
+ this._watchEnd(count < Math.min(shown.length, this.limit) ? rows[rows.length - 1] : null);
2206
+ }
2207
+ /**
2208
+ * Render the next chunk of rows once `row` (the last one rendered) comes
2209
+ * within ~1200px of the viewport; nothing to watch for `null`.
2210
+ * @internal
2211
+ */
2212
+ _watchEnd(row) {
2213
+ this._endIo?.disconnect();
2214
+ if (!row || typeof IntersectionObserver === "undefined") return;
2215
+ this._endIo ??= new IntersectionObserver((entries) => {
2216
+ if (this.destroyed || !entries.some((entry) => entry.isIntersecting)) return;
2217
+ this._budget += RENDER_CHUNK;
2218
+ this._layout();
2219
+ }, { rootMargin: "0px 0px 1200px 0px" });
2220
+ this._endIo.observe(row);
1945
2221
  }
1946
2222
  /* ── Playback ─────────────────────────────────────────────────────── */
1947
2223
  /**
@@ -1958,7 +2234,7 @@
1958
2234
  return first ? this.sounds.indexOf(first) : null;
1959
2235
  }
1960
2236
  if (typeof target === "number") return this.sounds[target] ? target : null;
1961
- const i = typeof target === "object" ? this.sounds.indexOf(target) : this.sounds.findIndex((s) => s.id === String(target));
2237
+ const i = typeof target === "object" ? this._index.get(target) ?? this.sounds.indexOf(target) : this.sounds.findIndex((s) => s.id === String(target));
1962
2238
  return i >= 0 ? i : null;
1963
2239
  }
1964
2240
  /**
@@ -2114,11 +2390,7 @@
2114
2390
  const at = this.currentIndex == null || this._cued ? -1 : shown.indexOf(this.sounds[this.currentIndex]);
2115
2391
  const target = shown[at + dir];
2116
2392
  if (!target) return false;
2117
- const limit = limitToReveal(this.limit, at + dir);
2118
- if (limit !== this.limit) {
2119
- this.limit = limit;
2120
- this._apply({ resort: false });
2121
- }
2393
+ if (this.$) this._reveal(at + dir);
2122
2394
  this.play(this.sounds.indexOf(target));
2123
2395
  return true;
2124
2396
  }
@@ -2178,7 +2450,7 @@
2178
2450
  * @internal
2179
2451
  */
2180
2452
  _onEngineLoad() {
2181
- if (this.currentIndex != null) this.rows[this.currentIndex]?.classList.remove("is-error");
2453
+ if (this.currentIndex != null) this._markFailed(this.currentIndex, false);
2182
2454
  if (this._eng()?.audio) this._eng().audio.loop = this._repeats();
2183
2455
  const sound = this.sounds[this.currentIndex];
2184
2456
  if (sound && !sound.peaks && this._eng()?.waveformData?.length) {
@@ -2232,7 +2504,7 @@
2232
2504
  _onEngineError(err) {
2233
2505
  const index = this.currentIndex;
2234
2506
  const sound = index == null ? void 0 : this.sounds[index];
2235
- if (index != null) this.rows[index]?.classList.add("is-error");
2507
+ if (index != null) this._markFailed(index, true);
2236
2508
  this._setPlaying(false);
2237
2509
  if (sound && !this._cued && this.$?.status) this.$.status.textContent = fill(this.strings.playError, { title: sound.title });
2238
2510
  this._emit("error", { error: err, sound, index });
@@ -2254,7 +2526,7 @@
2254
2526
  if (on) this._cued = false;
2255
2527
  const index = this.currentIndex;
2256
2528
  if (index == null) return;
2257
- if (on) this.rows[index]?.classList.remove("is-error");
2529
+ if (on) this._markFailed(index, false);
2258
2530
  this._paintRow(index);
2259
2531
  const was = this._announced ?? null;
2260
2532
  if (on) {
@@ -2296,16 +2568,43 @@
2296
2568
  btn.setAttribute("aria-pressed", String(on));
2297
2569
  btn.setAttribute("aria-label", fill(on ? this.strings.pause : this.strings.play, { title: this.sounds[index].title }));
2298
2570
  }
2571
+ row.classList.toggle("is-error", this._failed.has(index));
2299
2572
  const wave = row.querySelector(".ws-wave");
2300
- if (wave) {
2301
- wave.tabIndex = current && row === this._active ? 0 : -1;
2302
- if (!current) {
2303
- wave.setAttribute("aria-valuenow", "0");
2304
- wave.removeAttribute("aria-valuetext");
2305
- }
2306
- }
2573
+ if (wave) this._paintWave(wave, current, row === this._active, this.sounds[index].title);
2307
2574
  this._drawRow(index);
2308
2575
  }
2576
+ /**
2577
+ * A row's waveform: the current sound's is THE seek slider (focusable,
2578
+ * labelled, with a value); every other one is a picture, hidden from
2579
+ * assistive tech and out of the Tab order (a pointer still seeks on it).
2580
+ * So a screen reader meets one slider, not one per row. Since 2.2.0.
2581
+ * @internal
2582
+ */
2583
+ _paintWave(wave, current, active, title) {
2584
+ if (!current) {
2585
+ if (wave.getAttribute("aria-hidden") === "true" && !wave.hasAttribute("role")) return;
2586
+ for (const name of ["role", "aria-label", "aria-valuemin", "aria-valuemax", "aria-valuenow", "aria-valuetext", "tabindex"]) wave.removeAttribute(name);
2587
+ wave.setAttribute("aria-hidden", "true");
2588
+ return;
2589
+ }
2590
+ wave.removeAttribute("aria-hidden");
2591
+ wave.setAttribute("role", "slider");
2592
+ wave.setAttribute("aria-label", fill(this.strings.seek, { title }));
2593
+ wave.setAttribute("aria-valuemin", "0");
2594
+ wave.setAttribute("aria-valuemax", "100");
2595
+ if (!wave.hasAttribute("aria-valuenow")) wave.setAttribute("aria-valuenow", String(Math.round(this.progress * 100)));
2596
+ wave.tabIndex = active ? 0 : -1;
2597
+ }
2598
+ /**
2599
+ * Record that a sound failed (or played after all), on its row if it
2600
+ * has one; a row rendered later picks it up from `_failed`.
2601
+ * @internal
2602
+ */
2603
+ _markFailed(index, failed) {
2604
+ if (failed) this._failed.add(index);
2605
+ else this._failed.delete(index);
2606
+ this.rows[index]?.classList.toggle("is-error", failed);
2607
+ }
2309
2608
  /**
2310
2609
  * The current row's seek slider: its position (0–100) and, for a screen
2311
2610
  * reader, the time ("0:12 of 0:30").
@@ -2352,6 +2651,7 @@
2352
2651
  this.destroyed = true;
2353
2652
  this._ctl.abort();
2354
2653
  this._io?.disconnect();
2654
+ this._endIo?.disconnect();
2355
2655
  this._ro?.disconnect();
2356
2656
  this._mo?.disconnect();
2357
2657
  if (this._raf) cancelAnimationFrame(this._raf);
@@ -2429,7 +2729,8 @@
2429
2729
  "autoAdvance",
2430
2730
  "arrowAudition",
2431
2731
  "strings",
2432
- "playerOptions"
2732
+ "playerOptions",
2733
+ "mergeEnharmonicKeys"
2433
2734
  ];
2434
2735
  var kebab = (key) => key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
2435
2736
  function readElementOptions(el) {