@arraypress/waveform-sounds 2.0.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.
- package/README.md +44 -2
- package/dist/waveform-sounds-no-autoinit.esm.js +914 -208
- package/dist/waveform-sounds-render.esm.js +274 -82
- package/dist/waveform-sounds.css +92 -17
- package/dist/waveform-sounds.esm.js +917 -210
- package/dist/waveform-sounds.js +913 -210
- package/dist/waveform-sounds.min.css +1 -1
- package/dist/waveform-sounds.min.js +3 -3
- package/package.json +1 -1
- package/src/css/waveform-sounds.css +92 -17
- package/src/js/core/WaveformSounds.ts +540 -120
- package/src/js/core/element.ts +208 -0
- package/src/js/core/engine.ts +9 -3
- package/src/js/core/options.ts +1 -5
- package/src/js/data/bpm.ts +14 -4
- package/src/js/data/keys.ts +156 -0
- package/src/js/data/navigation.ts +51 -11
- package/src/js/data/sounds.ts +73 -36
- package/src/js/data/url-state.ts +39 -12
- package/src/js/dom/colors.ts +28 -1
- package/src/js/dom/draw.ts +16 -3
- package/src/js/dom/menus.ts +2 -3
- package/src/js/dom/rows.ts +38 -15
- package/src/js/entry.ts +13 -3
- package/src/js/index.ts +7 -3
- package/src/js/render/html.ts +0 -2
- package/src/js/render/icons.ts +59 -17
- package/src/js/render/markup.ts +156 -62
- package/src/js/render/options.ts +29 -5
- package/src/js/render/strings.ts +57 -4
- package/src/js/render-entry.ts +1 -1
- package/src/js/shared/utils.ts +4 -4
- package/src/js/types.ts +54 -3
- package/types/core/WaveformSounds.d.ts +41 -11
- package/types/core/WaveformSounds.d.ts.map +1 -1
- package/types/core/element.d.ts +84 -0
- package/types/core/element.d.ts.map +1 -0
- package/types/core/engine.d.ts +8 -3
- package/types/core/engine.d.ts.map +1 -1
- package/types/core/options.d.ts +0 -5
- package/types/core/options.d.ts.map +1 -1
- package/types/data/bpm.d.ts +17 -4
- package/types/data/bpm.d.ts.map +1 -1
- package/types/data/keys.d.ts +88 -0
- package/types/data/keys.d.ts.map +1 -0
- package/types/data/navigation.d.ts +38 -11
- package/types/data/navigation.d.ts.map +1 -1
- package/types/data/sounds.d.ts +33 -17
- package/types/data/sounds.d.ts.map +1 -1
- package/types/data/url-state.d.ts +38 -7
- package/types/data/url-state.d.ts.map +1 -1
- package/types/dom/colors.d.ts +13 -0
- package/types/dom/colors.d.ts.map +1 -1
- package/types/dom/draw.d.ts +5 -3
- package/types/dom/draw.d.ts.map +1 -1
- package/types/dom/menus.d.ts +0 -2
- package/types/dom/menus.d.ts.map +1 -1
- package/types/dom/rows.d.ts +19 -11
- package/types/dom/rows.d.ts.map +1 -1
- package/types/entry.d.ts +10 -4
- package/types/entry.d.ts.map +1 -1
- package/types/index.d.ts +2 -2
- package/types/index.d.ts.map +1 -1
- package/types/render/html.d.ts +0 -2
- package/types/render/html.d.ts.map +1 -1
- package/types/render/icons.d.ts +41 -3
- package/types/render/icons.d.ts.map +1 -1
- package/types/render/markup.d.ts +44 -12
- package/types/render/markup.d.ts.map +1 -1
- package/types/render/options.d.ts +18 -5
- package/types/render/options.d.ts.map +1 -1
- package/types/render/strings.d.ts +22 -4
- package/types/render/strings.d.ts.map +1 -1
- package/types/render-entry.d.ts +1 -1
- package/types/render-entry.d.ts.map +1 -1
- package/types/shared/utils.d.ts +3 -3
- package/types/shared/utils.d.ts.map +1 -1
- package/types/types.d.ts +53 -3
- package/types/types.d.ts.map +1 -1
|
@@ -25,6 +25,76 @@ var ACCENTS = Object.freeze([
|
|
|
25
25
|
[65056, 65071]
|
|
26
26
|
]);
|
|
27
27
|
|
|
28
|
+
// src/js/data/keys.ts
|
|
29
|
+
var KEY_PITCH = {
|
|
30
|
+
C: 0,
|
|
31
|
+
"B#": 0,
|
|
32
|
+
"C#": 1,
|
|
33
|
+
Db: 1,
|
|
34
|
+
D: 2,
|
|
35
|
+
"D#": 3,
|
|
36
|
+
Eb: 3,
|
|
37
|
+
E: 4,
|
|
38
|
+
Fb: 4,
|
|
39
|
+
"E#": 5,
|
|
40
|
+
F: 5,
|
|
41
|
+
"F#": 6,
|
|
42
|
+
Gb: 6,
|
|
43
|
+
G: 7,
|
|
44
|
+
"G#": 8,
|
|
45
|
+
Ab: 8,
|
|
46
|
+
A: 9,
|
|
47
|
+
"A#": 10,
|
|
48
|
+
Bb: 10,
|
|
49
|
+
B: 11,
|
|
50
|
+
Cb: 11
|
|
51
|
+
};
|
|
52
|
+
var CONVENTIONAL_MAJOR = ["C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B"];
|
|
53
|
+
var CONVENTIONAL_MINOR = ["Cm", "C#m", "Dm", "Ebm", "Em", "Fm", "F#m", "Gm", "G#m", "Am", "Bbm", "Bm"];
|
|
54
|
+
function parseKey(key) {
|
|
55
|
+
const m = /^([A-G][#b]?)(m?)$/.exec(String(key ?? ""));
|
|
56
|
+
if (!m || KEY_PITCH[m[1]] === void 0) return null;
|
|
57
|
+
return { root: m[1], pitch: KEY_PITCH[m[1]], minor: m[2] === "m" };
|
|
58
|
+
}
|
|
59
|
+
function keyClass(key) {
|
|
60
|
+
const k = parseKey(key);
|
|
61
|
+
return k ? `${k.pitch}${k.minor ? "m" : ""}` : key;
|
|
62
|
+
}
|
|
63
|
+
function keyRank(key) {
|
|
64
|
+
if (!key) return Infinity;
|
|
65
|
+
const k = parseKey(key);
|
|
66
|
+
return k ? k.pitch * 2 + (k.minor ? 1 : 0) : 1e3;
|
|
67
|
+
}
|
|
68
|
+
function canonicalSpelling(counts) {
|
|
69
|
+
let best = 0;
|
|
70
|
+
for (const n of counts.values()) if (n > best) best = n;
|
|
71
|
+
const tied = [...counts].filter(([, n]) => n === best).map(([k2]) => k2).sort();
|
|
72
|
+
if (tied.length === 1) return tied[0];
|
|
73
|
+
const k = parseKey(tied[0]);
|
|
74
|
+
const conventional = k ? (k.minor ? CONVENTIONAL_MINOR : CONVENTIONAL_MAJOR)[k.pitch] : "";
|
|
75
|
+
return tied.includes(conventional) ? conventional : tied[0];
|
|
76
|
+
}
|
|
77
|
+
function keyEntries(keys, merge = true) {
|
|
78
|
+
const groups = /* @__PURE__ */ new Map();
|
|
79
|
+
for (const key of keys) {
|
|
80
|
+
if (!key) continue;
|
|
81
|
+
const cls = merge ? keyClass(key) : key;
|
|
82
|
+
const g = groups.get(cls) ?? /* @__PURE__ */ new Map();
|
|
83
|
+
groups.set(cls, g);
|
|
84
|
+
g.set(key, (g.get(key) || 0) + 1);
|
|
85
|
+
}
|
|
86
|
+
const out = [];
|
|
87
|
+
const aliases = {};
|
|
88
|
+
for (const g of groups.values()) {
|
|
89
|
+
const entry = canonicalSpelling(g);
|
|
90
|
+
out.push(entry);
|
|
91
|
+
const others = [...g.keys()].filter((k) => k !== entry).sort();
|
|
92
|
+
if (others.length) aliases[entry] = others;
|
|
93
|
+
}
|
|
94
|
+
out.sort((a, b) => keyRank(a) - keyRank(b) || a.localeCompare(b));
|
|
95
|
+
return { keys: out, aliases };
|
|
96
|
+
}
|
|
97
|
+
|
|
28
98
|
// src/js/data/sounds.ts
|
|
29
99
|
function encodePeaks(peaks) {
|
|
30
100
|
if (!Array.isArray(peaks)) return "";
|
|
@@ -35,12 +105,25 @@ function encodePeaks(peaks) {
|
|
|
35
105
|
}
|
|
36
106
|
return out;
|
|
37
107
|
}
|
|
108
|
+
var COMPACT_PREFIX = "wp1:";
|
|
109
|
+
function decodeCompact(body) {
|
|
110
|
+
if (!body || !/^[\w-]+$/.test(body) || body.length % 4 === 1) return null;
|
|
111
|
+
try {
|
|
112
|
+
const bytes = atob(body.replace(/-/g, "+").replace(/_/g, "/"));
|
|
113
|
+
const out = new Array(bytes.length);
|
|
114
|
+
for (let i = 0; i < bytes.length; i++) out[i] = bytes.charCodeAt(i) / 255;
|
|
115
|
+
return out;
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
38
120
|
function decodePeaks(value, scale = 1) {
|
|
39
121
|
if (value == null || value === "") return null;
|
|
40
122
|
if (typeof value === "string") {
|
|
123
|
+
if (value.startsWith(COMPACT_PREFIX)) return decodeCompact(value.slice(COMPACT_PREFIX.length));
|
|
41
124
|
if (!/^[0-9a-f]+$/i.test(value) || value.length % 2) return null;
|
|
42
125
|
const out2 = new Array(value.length / 2);
|
|
43
|
-
for (let i = 0; i < out2.length; i++) out2[i] = parseInt(value.
|
|
126
|
+
for (let i = 0; i < out2.length; i++) out2[i] = parseInt(value.slice(i * 2, i * 2 + 2), 16) / 255;
|
|
44
127
|
return out2;
|
|
45
128
|
}
|
|
46
129
|
if (!Array.isArray(value)) return null;
|
|
@@ -58,14 +141,6 @@ function normalizeKey(key) {
|
|
|
58
141
|
const minor = m[3] && /^m(in(or)?)?$/i.test(m[3]) && m[3] !== "M";
|
|
59
142
|
return root + acc + (minor ? "m" : "");
|
|
60
143
|
}
|
|
61
|
-
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 };
|
|
62
|
-
function keyRank(key) {
|
|
63
|
-
if (!key) return Infinity;
|
|
64
|
-
const minor = key.endsWith("m");
|
|
65
|
-
const root = minor ? key.slice(0, -1) : key;
|
|
66
|
-
const p = KEY_PITCH[root];
|
|
67
|
-
return p === void 0 ? 1e3 : p * 2 + (minor ? 1 : 0);
|
|
68
|
-
}
|
|
69
144
|
function formatDuration(seconds) {
|
|
70
145
|
if (seconds == null || seconds === "") return "";
|
|
71
146
|
const s = Number(seconds);
|
|
@@ -79,8 +154,8 @@ function parseDuration(value) {
|
|
|
79
154
|
if (typeof value === "number") return Number.isFinite(value) && value >= 0 ? value : null;
|
|
80
155
|
const str = String(value).trim();
|
|
81
156
|
if (/^\d+(\.\d+)?$/.test(str)) return Number(str);
|
|
157
|
+
if (!/^\d+(\.\d+)?(:\d+(\.\d+)?)+$/.test(str)) return null;
|
|
82
158
|
const parts = str.split(":").map(Number);
|
|
83
|
-
if (parts.length < 2 || parts.some((n) => !Number.isFinite(n))) return null;
|
|
84
159
|
return parts.reduce((acc, n) => acc * 60 + n, 0);
|
|
85
160
|
}
|
|
86
161
|
function titleFromUrl(url) {
|
|
@@ -133,13 +208,13 @@ function normalizeSounds(list, peakScale = 1) {
|
|
|
133
208
|
});
|
|
134
209
|
return out;
|
|
135
210
|
}
|
|
136
|
-
function facets(sounds) {
|
|
211
|
+
function facets(sounds, options = {}) {
|
|
137
212
|
const types = /* @__PURE__ */ new Map();
|
|
138
|
-
const keys =
|
|
213
|
+
const keys = [];
|
|
139
214
|
let min = Infinity, max = -Infinity, hasDuration = false, loops = 0;
|
|
140
215
|
for (const s of sounds) {
|
|
141
216
|
if (s.type) types.set(s.type, (types.get(s.type) || 0) + 1);
|
|
142
|
-
if (s.key) keys.
|
|
217
|
+
if (s.key) keys.push(s.key);
|
|
143
218
|
if (s.bpm != null) {
|
|
144
219
|
min = Math.min(min, s.bpm);
|
|
145
220
|
max = Math.max(max, s.bpm);
|
|
@@ -147,9 +222,11 @@ function facets(sounds) {
|
|
|
147
222
|
if (s.duration != null) hasDuration = true;
|
|
148
223
|
if (s.loop) loops++;
|
|
149
224
|
}
|
|
225
|
+
const entries = keyEntries(keys, options.mergeEnharmonicKeys !== false);
|
|
150
226
|
return {
|
|
151
227
|
types: [...types].map(([name, count]) => ({ name, count })),
|
|
152
|
-
keys:
|
|
228
|
+
keys: entries.keys,
|
|
229
|
+
keyAliases: entries.aliases,
|
|
153
230
|
bpm: min === Infinity ? null : { min, max },
|
|
154
231
|
hasDuration,
|
|
155
232
|
loops,
|
|
@@ -167,6 +244,31 @@ var SORT_NEEDS = {
|
|
|
167
244
|
function availableSorts(sorts, f) {
|
|
168
245
|
return sorts.filter((sort) => SORT_NEEDS[sort]?.(f));
|
|
169
246
|
}
|
|
247
|
+
function sortSounds(sounds, by = "default") {
|
|
248
|
+
const list = sounds.map((s, i) => ({ s, i }));
|
|
249
|
+
const last = (v) => v == null ? Infinity : v;
|
|
250
|
+
const cmp = {
|
|
251
|
+
title: (a, b) => a.s.title.localeCompare(b.s.title, void 0, { numeric: true, sensitivity: "base" }),
|
|
252
|
+
bpm: (a, b) => last(a.s.bpm) - last(b.s.bpm),
|
|
253
|
+
key: (a, b) => keyRank(a.s.key) - keyRank(b.s.key),
|
|
254
|
+
duration: (a, b) => last(a.s.duration) - last(b.s.duration)
|
|
255
|
+
}[by];
|
|
256
|
+
if (cmp) list.sort((a, b) => cmp(a, b) || a.i - b.i);
|
|
257
|
+
return list.map((x) => x.s);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/js/shared/utils.ts
|
|
261
|
+
function hashString(str) {
|
|
262
|
+
let hash = 5381;
|
|
263
|
+
for (const char of str) hash = hash * 33 + char.codePointAt(0) >>> 0;
|
|
264
|
+
return hash;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// src/js/data/navigation.ts
|
|
268
|
+
var RENDER_CHUNK = 50;
|
|
269
|
+
function firstRender(pageSize) {
|
|
270
|
+
return pageSize > 0 ? pageSize : RENDER_CHUNK;
|
|
271
|
+
}
|
|
170
272
|
|
|
171
273
|
// src/js/render/html.ts
|
|
172
274
|
var VOID = /* @__PURE__ */ new Set(["input", "br", "img"]);
|
|
@@ -188,21 +290,32 @@ function h(tag, attributes = {}, ...children) {
|
|
|
188
290
|
}
|
|
189
291
|
|
|
190
292
|
// src/js/render/icons.ts
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
chevron: icon("M6.4 8.6 12 14.2l5.6-5.6L19 10l-7 7-7-7z", "ws-menu-chevron"),
|
|
200
|
-
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"),
|
|
201
|
-
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"),
|
|
202
|
-
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")
|
|
293
|
+
var ICON_PATHS = {
|
|
294
|
+
play: "M8 5.5v13l11-6.5z",
|
|
295
|
+
pause: "M7 5h3.5v14H7zM13.5 5H17v14h-3.5z",
|
|
296
|
+
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",
|
|
297
|
+
chevron: "M6.4 8.6 12 14.2l5.6-5.6L19 10l-7 7-7-7z",
|
|
298
|
+
check: "M9.5 16.2 5.3 12l-1.4 1.4 5.6 5.6 11-11-1.4-1.4z",
|
|
299
|
+
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",
|
|
300
|
+
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"
|
|
203
301
|
};
|
|
302
|
+
var ICON_NAMES = Object.keys(ICON_PATHS);
|
|
303
|
+
function iconId(id, name) {
|
|
304
|
+
return `${id}-i-${name}`;
|
|
305
|
+
}
|
|
306
|
+
function iconSprite(id, names) {
|
|
307
|
+
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("");
|
|
308
|
+
return `<svg class="ws-sprite" data-ws-sprite="${escapeHtml(id)}" width="0" height="0" aria-hidden="true" focusable="false">${symbols}</svg>`;
|
|
309
|
+
}
|
|
310
|
+
function icon(id, name, extraClass = "") {
|
|
311
|
+
const cls = extraClass ? `ws-icon ${extraClass}` : "ws-icon";
|
|
312
|
+
return `<svg class="${cls}" aria-hidden="true" focusable="false"><use href="#${escapeHtml(iconId(id, name))}"/></svg>`;
|
|
313
|
+
}
|
|
204
314
|
|
|
205
315
|
// src/js/data/bpm.ts
|
|
316
|
+
function bpmExtent(range) {
|
|
317
|
+
return range ? { min: Math.floor(range.min), max: Math.ceil(range.max) } : null;
|
|
318
|
+
}
|
|
206
319
|
function bpmRangeFromFilter(filter, extent) {
|
|
207
320
|
const read = (v, fallback) => {
|
|
208
321
|
const n = Number(v);
|
|
@@ -248,6 +361,7 @@ var DEFAULT_STRINGS = {
|
|
|
248
361
|
play: "Play {title}",
|
|
249
362
|
pause: "Pause {title}",
|
|
250
363
|
seek: "Seek {title}",
|
|
364
|
+
seekValue: "{time} of {duration}",
|
|
251
365
|
download: "Download {title}",
|
|
252
366
|
count: "{count} sounds",
|
|
253
367
|
countOne: "1 sound",
|
|
@@ -255,7 +369,18 @@ var DEFAULT_STRINGS = {
|
|
|
255
369
|
showMore: "Show {count} more",
|
|
256
370
|
empty: "No sounds match.",
|
|
257
371
|
clear: "Clear filters",
|
|
258
|
-
nowPlaying: "Playing {title}"
|
|
372
|
+
nowPlaying: "Playing {title}",
|
|
373
|
+
playError: "Could not play {title}",
|
|
374
|
+
loadError: "Could not load the sounds.",
|
|
375
|
+
noSounds: "No sounds.",
|
|
376
|
+
rowBpm: "{bpm} BPM",
|
|
377
|
+
rowKey: "key {key}",
|
|
378
|
+
rowDuration: "{duration}",
|
|
379
|
+
keyMajor: "{note} major",
|
|
380
|
+
keyMinor: "{note} minor",
|
|
381
|
+
noteSharp: "{note} sharp",
|
|
382
|
+
noteFlat: "{note} flat",
|
|
383
|
+
listSeparator: ", "
|
|
259
384
|
};
|
|
260
385
|
var SORT_LABEL_KEYS = {
|
|
261
386
|
default: "sortDefault",
|
|
@@ -276,6 +401,27 @@ function countText(shown, total, strings = DEFAULT_STRINGS) {
|
|
|
276
401
|
if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
|
|
277
402
|
return total === 1 ? strings.countOne : fill(strings.count, { count: total });
|
|
278
403
|
}
|
|
404
|
+
function spokenKey(key, strings = DEFAULT_STRINGS) {
|
|
405
|
+
const k = parseKey(key);
|
|
406
|
+
if (!k) return key;
|
|
407
|
+
const letter = k.root[0], acc = k.root.slice(1);
|
|
408
|
+
const note = acc === "#" ? fill(strings.noteSharp, { note: letter }) : acc === "b" ? fill(strings.noteFlat, { note: letter }) : letter;
|
|
409
|
+
return fill(k.minor ? strings.keyMinor : strings.keyMajor, { note });
|
|
410
|
+
}
|
|
411
|
+
function rowSummary(sound, columns, strings = DEFAULT_STRINGS) {
|
|
412
|
+
const parts = [];
|
|
413
|
+
for (const c of columns) {
|
|
414
|
+
if (c === "type" && sound.type) parts.push(sound.type);
|
|
415
|
+
else if (c === "bpm" && sound.bpm != null) parts.push(fill(strings.rowBpm, { bpm: sound.bpm }));
|
|
416
|
+
else if (c === "key" && sound.key) parts.push(fill(strings.rowKey, { key: spokenKey(sound.key, strings) }));
|
|
417
|
+
else if (c === "duration") {
|
|
418
|
+
const d = formatDuration(sound.duration);
|
|
419
|
+
if (d) parts.push(fill(strings.rowDuration, { duration: d }));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (sound.loop) parts.push(strings.isLoop);
|
|
423
|
+
return parts.join(strings.listSeparator);
|
|
424
|
+
}
|
|
279
425
|
|
|
280
426
|
// src/js/render/options.ts
|
|
281
427
|
var FILTERS = ["type", "key", "bpm", "loop"];
|
|
@@ -290,8 +436,18 @@ var RENDER_DEFAULTS = {
|
|
|
290
436
|
menuSearch: 8,
|
|
291
437
|
pageSize: 50,
|
|
292
438
|
columns: [...COLUMNS],
|
|
293
|
-
maxTypeChips: 10
|
|
439
|
+
maxTypeChips: 10,
|
|
440
|
+
mergeEnharmonicKeys: true
|
|
294
441
|
};
|
|
442
|
+
var RENDER_OPTION_KEYS = [...Object.keys(RENDER_DEFAULTS), "idPrefix", "loop", "strings"];
|
|
443
|
+
function pickRenderOptions(options) {
|
|
444
|
+
const out = {};
|
|
445
|
+
for (const key of RENDER_OPTION_KEYS) {
|
|
446
|
+
const value = options?.[key];
|
|
447
|
+
if (value != null) out[key] = value;
|
|
448
|
+
}
|
|
449
|
+
return out;
|
|
450
|
+
}
|
|
295
451
|
function defined(obj) {
|
|
296
452
|
const out = {};
|
|
297
453
|
for (const k in obj ?? {}) if (obj[k] !== void 0) out[k] = obj[k];
|
|
@@ -315,17 +471,11 @@ function resolveRenderOptions(options = {}) {
|
|
|
315
471
|
menuSearch: wholeNumber(o.menuSearch, RENDER_DEFAULTS.menuSearch),
|
|
316
472
|
maxTypeChips: wholeNumber(o.maxTypeChips, RENDER_DEFAULTS.maxTypeChips),
|
|
317
473
|
pageSize: wholeNumber(o.pageSize, RENDER_DEFAULTS.pageSize),
|
|
474
|
+
mergeEnharmonicKeys: o.mergeEnharmonicKeys !== false,
|
|
318
475
|
strings: { ...DEFAULT_STRINGS, ...defined(options.strings) }
|
|
319
476
|
};
|
|
320
477
|
}
|
|
321
478
|
|
|
322
|
-
// src/js/shared/utils.ts
|
|
323
|
-
function hashString(str) {
|
|
324
|
-
let hash = 5381;
|
|
325
|
-
for (const char of str) hash = hash * 33 + char.codePointAt(0) >>> 0;
|
|
326
|
-
return hash;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
479
|
// src/js/render/markup.ts
|
|
330
480
|
function idBase(sounds) {
|
|
331
481
|
const signature = [sounds.length, ...sounds.map((s) => s.url)].join("|");
|
|
@@ -348,11 +498,11 @@ function toolbarPlan(f, o) {
|
|
|
348
498
|
function loopsByDefault(loop, f) {
|
|
349
499
|
return loop == null ? f.loops > 0 : !!loop;
|
|
350
500
|
}
|
|
351
|
-
function renderSearch(s) {
|
|
501
|
+
function renderSearch(s, id) {
|
|
352
502
|
return h(
|
|
353
503
|
"label",
|
|
354
504
|
{ class: "ws-search" },
|
|
355
|
-
|
|
505
|
+
icon(id, "search"),
|
|
356
506
|
h("span", { class: "ws-sr" }, text(s.search)),
|
|
357
507
|
h("input", { type: "search", class: "ws-search-input", "data-ws-search": true, placeholder: s.searchPlaceholder, autocomplete: "off", spellcheck: "false" })
|
|
358
508
|
);
|
|
@@ -366,7 +516,7 @@ function renderMenu(name, m) {
|
|
|
366
516
|
{ type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "listbox", "aria-expanded": "false", "aria-controls": listId },
|
|
367
517
|
m.prefix ? h("span", { class: "ws-menu-prefix" }, text(m.prefix)) : h("span", { class: "ws-sr" }, text(`${m.label}: `)),
|
|
368
518
|
h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(current.label)),
|
|
369
|
-
|
|
519
|
+
icon(m.id, "chevron", "ws-menu-chevron")
|
|
370
520
|
);
|
|
371
521
|
const search = searchable && h("input", {
|
|
372
522
|
type: "search",
|
|
@@ -383,8 +533,8 @@ function renderMenu(name, m) {
|
|
|
383
533
|
});
|
|
384
534
|
const options = m.options.map((o, i) => h(
|
|
385
535
|
"li",
|
|
386
|
-
{ role: "option", id: `${m.id}-${name}-${i}`, class: "ws-menu-option", "data-value": o.value, "aria-selected": String(o === current) },
|
|
387
|
-
|
|
536
|
+
{ role: "option", id: `${m.id}-${name}-${i}`, class: "ws-menu-option", "data-value": o.value, "data-alt": o.alt, "aria-selected": String(o === current) },
|
|
537
|
+
icon(m.id, "check", "ws-menu-check"),
|
|
388
538
|
h("span", { class: "ws-menu-text" }, text(o.label)),
|
|
389
539
|
o.count != null && h("span", { class: "ws-menu-count" }, text(o.count))
|
|
390
540
|
));
|
|
@@ -421,7 +571,7 @@ function renderBpmMenu({ id, s, range }) {
|
|
|
421
571
|
{ type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
|
|
422
572
|
h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
|
|
423
573
|
h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
|
|
424
|
-
|
|
574
|
+
icon(id, "chevron", "ws-menu-chevron")
|
|
425
575
|
),
|
|
426
576
|
h(
|
|
427
577
|
"div",
|
|
@@ -456,8 +606,8 @@ function renderLoopFilter(s) {
|
|
|
456
606
|
button("one-shot", s.oneShots)
|
|
457
607
|
);
|
|
458
608
|
}
|
|
459
|
-
function renderLoopToggle(s, pressed) {
|
|
460
|
-
return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) },
|
|
609
|
+
function renderLoopToggle(s, pressed, id) {
|
|
610
|
+
return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, icon(id, "loop"), h("span", {}, text(s.loop)));
|
|
461
611
|
}
|
|
462
612
|
function renderChip(value, label, count, pressed) {
|
|
463
613
|
return h(
|
|
@@ -481,9 +631,9 @@ function renderToolbar(plan, { o, s, f, total, id }) {
|
|
|
481
631
|
label: s.key,
|
|
482
632
|
value: "",
|
|
483
633
|
placeholder: s.findKey,
|
|
484
|
-
options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
|
|
634
|
+
options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k, alt: f.keyAliases[k]?.join(" ") }))]
|
|
485
635
|
}),
|
|
486
|
-
plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
|
|
636
|
+
plan.bpm && renderBpmMenu({ id, s, range: bpmExtent(f.bpm) }),
|
|
487
637
|
plan.loops && renderLoopFilter(s),
|
|
488
638
|
plan.sorts.length > 0 && menu("sort", {
|
|
489
639
|
label: s.sort,
|
|
@@ -491,7 +641,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
|
|
|
491
641
|
value: plan.sorts[0],
|
|
492
642
|
options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
|
|
493
643
|
}),
|
|
494
|
-
plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
|
|
644
|
+
plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f), id)
|
|
495
645
|
].filter(Boolean);
|
|
496
646
|
const chips = plan.types === "chips" && h(
|
|
497
647
|
"div",
|
|
@@ -503,7 +653,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
|
|
|
503
653
|
return h(
|
|
504
654
|
"div",
|
|
505
655
|
{ class: "ws-toolbar" },
|
|
506
|
-
plan.search && renderSearch(s),
|
|
656
|
+
plan.search && renderSearch(s, id),
|
|
507
657
|
controls.length > 0 && h("div", { class: "ws-controls" }, controls),
|
|
508
658
|
// The chips and the count share a row (the count alone without chips).
|
|
509
659
|
(chips || count) && h("div", { class: "ws-meta" }, chips, count)
|
|
@@ -515,49 +665,66 @@ var CELLS = {
|
|
|
515
665
|
key: (sound) => text(sound.key),
|
|
516
666
|
duration: (sound) => text(formatDuration(sound.duration))
|
|
517
667
|
};
|
|
518
|
-
function renderRow(sound, index, o,
|
|
668
|
+
function renderRow(sound, index, o, id) {
|
|
519
669
|
const s = o.strings;
|
|
520
670
|
const vars = { title: sound.title };
|
|
521
671
|
const cells = o.columns.map((c) => h("span", { class: `ws-cell ws-${c}` }, CELLS[c](sound)));
|
|
672
|
+
const summary = rowSummary(sound, o.columns, s);
|
|
673
|
+
const described = summary ? `${id}-d${index}` : null;
|
|
522
674
|
return h(
|
|
523
675
|
"li",
|
|
524
|
-
{
|
|
525
|
-
|
|
526
|
-
"
|
|
527
|
-
"
|
|
528
|
-
"
|
|
529
|
-
"
|
|
530
|
-
|
|
531
|
-
"data-bpm": sound.bpm,
|
|
532
|
-
"data-key": sound.key || null,
|
|
533
|
-
"data-duration": sound.duration,
|
|
534
|
-
"data-tags": sound.tags?.length ? sound.tags.join(",") : null,
|
|
535
|
-
"data-peaks": sound.peaks ? encodePeaks(sound.peaks) : null,
|
|
536
|
-
"data-waveform": sound.waveform,
|
|
537
|
-
"data-download": sound.download,
|
|
538
|
-
"data-loop": sound.loop ? "true" : null,
|
|
539
|
-
hidden
|
|
540
|
-
},
|
|
541
|
-
h("button", { type: "button", class: "ws-play", "aria-pressed": "false", "aria-label": fill(s.play, vars) }, ICONS.play, ICONS.pause),
|
|
676
|
+
{ class: "ws-row", "data-ws-index": index },
|
|
677
|
+
h(
|
|
678
|
+
"button",
|
|
679
|
+
{ type: "button", class: "ws-play", "aria-pressed": "false", "aria-label": fill(s.play, vars), "aria-describedby": described },
|
|
680
|
+
icon(id, "play", "ws-icon-play"),
|
|
681
|
+
icon(id, "pause", "ws-icon-pause")
|
|
682
|
+
),
|
|
542
683
|
h(
|
|
543
684
|
"span",
|
|
544
685
|
{ class: "ws-cell ws-title" },
|
|
545
686
|
h("span", { class: "ws-title-text" }, text(sound.title)),
|
|
546
|
-
// A loop says so beside its name; a one-shot carries nothing.
|
|
547
|
-
sound.loop && h("span", { class: "ws-loop-mark", title: s.isLoop
|
|
687
|
+
// A loop says so beside its name (and in the read-out); a one-shot carries nothing.
|
|
688
|
+
sound.loop && h("span", { class: "ws-loop-mark", title: s.isLoop, "aria-hidden": "true" }, icon(id, "loop"))
|
|
548
689
|
),
|
|
549
690
|
// Wide rows: `display: contents` makes each cell a column. Narrow
|
|
550
691
|
// rows: one line under the title.
|
|
551
|
-
cells.length > 0 && h("span", { class: "ws-cells" }, cells),
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
{ class: "ws-wave", role: "slider", "aria-label": fill(s.seek, vars), "aria-valuemin": "0", "aria-valuemax": "100", "aria-valuenow": "0", tabindex: "-1" },
|
|
555
|
-
h("canvas", { class: "ws-canvas", "aria-hidden": "true" })
|
|
556
|
-
),
|
|
692
|
+
cells.length > 0 && h("span", { class: "ws-cells", "aria-hidden": "true" }, cells),
|
|
693
|
+
summary && h("span", { class: "ws-sr", id: described }, text(summary)),
|
|
694
|
+
o.player === "inline" && h("span", { class: "ws-wave", "aria-hidden": "true" }, h("canvas", { class: "ws-canvas" })),
|
|
557
695
|
// Optional, per sound: a plain link. Rows without one carry nothing.
|
|
558
|
-
sound.download && h("a", { class: "ws-download", href: sound.download, download: true, "aria-label": fill(s.download, vars) },
|
|
696
|
+
sound.download && h("a", { class: "ws-download", href: sound.download, download: true, "aria-label": fill(s.download, vars) }, icon(id, "download"))
|
|
559
697
|
);
|
|
560
698
|
}
|
|
699
|
+
function renderSoundsData(list) {
|
|
700
|
+
const data = list.map((s, i) => {
|
|
701
|
+
const d = {};
|
|
702
|
+
if (s.id !== `sound-${i + 1}`) d.id = s.id;
|
|
703
|
+
d.url = s.url;
|
|
704
|
+
d.title = s.title;
|
|
705
|
+
if (s.type) d.type = s.type;
|
|
706
|
+
if (s.bpm != null) d.bpm = s.bpm;
|
|
707
|
+
if (s.key) d.key = s.key;
|
|
708
|
+
if (s.duration != null) d.duration = s.duration;
|
|
709
|
+
if (s.tags.length) d.tags = s.tags;
|
|
710
|
+
if (s.peaks) d.peaks = encodePeaks(s.peaks);
|
|
711
|
+
if (s.waveform) d.waveform = s.waveform;
|
|
712
|
+
if (s.download) d.download = s.download;
|
|
713
|
+
if (s.loop) d.loop = true;
|
|
714
|
+
return d;
|
|
715
|
+
});
|
|
716
|
+
return `<script type="application/json" data-ws-sounds>${JSON.stringify(data).replace(/</g, "\\u003c")}<\/script>`;
|
|
717
|
+
}
|
|
718
|
+
function iconsUsed(plan, list) {
|
|
719
|
+
const names = [];
|
|
720
|
+
if (list.length) names.push("play", "pause");
|
|
721
|
+
if (plan.search) names.push("search");
|
|
722
|
+
if (plan.types === "menu" || plan.key || plan.sorts.length) names.push("chevron", "check");
|
|
723
|
+
else if (plan.bpm) names.push("chevron");
|
|
724
|
+
if (plan.loop || list.some((s) => s.loop)) names.push("loop");
|
|
725
|
+
if (list.some((s) => s.download)) names.push("download");
|
|
726
|
+
return names;
|
|
727
|
+
}
|
|
561
728
|
function renderFooter({ o, s, total }) {
|
|
562
729
|
const page = o.pageSize > 0 ? o.pageSize : total;
|
|
563
730
|
const more = Math.max(0, total - page);
|
|
@@ -576,23 +743,47 @@ function renderFooter({ o, s, total }) {
|
|
|
576
743
|
].join("");
|
|
577
744
|
}
|
|
578
745
|
function renderSounds(sounds, options = {}) {
|
|
746
|
+
return renderList(normalizeSounds(sounds), options, true);
|
|
747
|
+
}
|
|
748
|
+
function renderList(list, options, withData) {
|
|
579
749
|
const o = resolveRenderOptions(options);
|
|
580
|
-
const
|
|
581
|
-
const
|
|
582
|
-
const
|
|
583
|
-
|
|
750
|
+
const ctx = { o, s: o.strings, f: facets(list, o), total: list.length, id: o.idPrefix || idBase(list) };
|
|
751
|
+
const plan = toolbarPlan(ctx.f, o);
|
|
752
|
+
const index = new Map(list.map((sound, i) => [sound, i]));
|
|
753
|
+
const first = sortSounds(list, availableSorts(o.sorts, ctx.f)[0] || "default").slice(0, firstRender(o.pageSize));
|
|
754
|
+
return iconSprite(ctx.id, iconsUsed(plan, list)) + renderToolbar(plan, ctx) + h(
|
|
584
755
|
"ul",
|
|
585
756
|
{ class: `ws-list ws-list--${o.player}`, role: "list", "data-ws-list": true },
|
|
586
|
-
|
|
587
|
-
) + renderFooter(ctx);
|
|
757
|
+
first.map((sound) => renderRow(sound, index.get(sound), o, ctx.id))
|
|
758
|
+
) + renderFooter(ctx) + (withData ? renderSoundsData(list) : "");
|
|
759
|
+
}
|
|
760
|
+
function renderOptionAttributes(options = {}) {
|
|
761
|
+
const set = pickRenderOptions(options);
|
|
762
|
+
const o = resolveRenderOptions(options);
|
|
763
|
+
const out = {};
|
|
764
|
+
for (const key of RENDER_OPTION_KEYS) {
|
|
765
|
+
if (key === "player" || !(key in set)) continue;
|
|
766
|
+
const name = `data-${key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}`;
|
|
767
|
+
if (key === "strings") {
|
|
768
|
+
const strings = defined(set.strings);
|
|
769
|
+
if (Object.keys(strings).length) out[name] = JSON.stringify(strings);
|
|
770
|
+
} else if (key === "loop" || key === "idPrefix") {
|
|
771
|
+
out[name] = String(set[key]);
|
|
772
|
+
} else {
|
|
773
|
+
const value = o[key];
|
|
774
|
+
out[name] = Array.isArray(value) ? value.join(",") : String(value);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
return out;
|
|
588
778
|
}
|
|
589
779
|
function renderSoundsElement(sounds, options = {}, className = "") {
|
|
590
780
|
const o = resolveRenderOptions(options);
|
|
591
781
|
const cls = ["waveform-sounds", `waveform-sounds--${o.player}`, className].filter(Boolean).join(" ");
|
|
592
|
-
return h("div", { class: cls, "data-waveform-sounds": true, "data-player": o.player }, renderSounds(sounds, options));
|
|
782
|
+
return h("div", { class: cls, "data-waveform-sounds": true, "data-player": o.player, ...renderOptionAttributes(options) }, renderSounds(sounds, options));
|
|
593
783
|
}
|
|
594
784
|
export {
|
|
595
785
|
DEFAULT_STRINGS,
|
|
786
|
+
RENDER_OPTION_KEYS,
|
|
596
787
|
decodePeaks,
|
|
597
788
|
encodePeaks,
|
|
598
789
|
escapeHtml,
|
|
@@ -601,6 +792,7 @@ export {
|
|
|
601
792
|
normalizeKey,
|
|
602
793
|
normalizeSounds,
|
|
603
794
|
parseManifest,
|
|
795
|
+
pickRenderOptions,
|
|
604
796
|
renderSounds,
|
|
605
797
|
renderSoundsElement
|
|
606
798
|
};
|