@arraypress/waveform-sounds 0.3.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +43 -3
  3. package/dist/waveform-sounds-no-autoinit.esm.js +607 -178
  4. package/dist/waveform-sounds-render.esm.js +79 -15
  5. package/dist/waveform-sounds.css +136 -79
  6. package/dist/waveform-sounds.esm.js +611 -181
  7. package/dist/waveform-sounds.js +608 -181
  8. package/dist/waveform-sounds.min.css +1 -1
  9. package/dist/waveform-sounds.min.js +3 -3
  10. package/package.json +36 -33
  11. package/src/css/waveform-sounds.css +136 -79
  12. package/src/js/core/WaveformSounds.ts +1366 -0
  13. package/src/js/core/element.ts +208 -0
  14. package/src/js/core/engine.ts +107 -0
  15. package/src/js/core/{options.js → options.ts} +28 -28
  16. package/src/js/data/{bpm.js → bpm.ts} +27 -17
  17. package/src/js/data/{navigation.js → navigation.ts} +31 -18
  18. package/src/js/data/{sounds.js → sounds.ts} +97 -66
  19. package/src/js/data/url-state.ts +130 -0
  20. package/src/js/dom/colors.ts +86 -0
  21. package/src/js/dom/{draw.js → draw.ts} +16 -17
  22. package/src/js/dom/{menus.js → menus.ts} +70 -55
  23. package/src/js/dom/{rows.js → rows.ts} +15 -17
  24. package/src/js/entry.ts +60 -0
  25. package/src/js/{index.js → index.ts} +7 -3
  26. package/src/js/render/{html.js → html.ts} +16 -12
  27. package/src/js/render/{icons.js → icons.ts} +3 -4
  28. package/src/js/render/{markup.js → markup.ts} +114 -64
  29. package/src/js/render/options.ts +118 -0
  30. package/src/js/render/{strings.js → strings.ts} +18 -16
  31. package/src/js/{render-entry.js → render-entry.ts} +2 -1
  32. package/src/js/shared/{utils.js → utils.ts} +24 -24
  33. package/{index.d.ts → src/js/types.ts} +40 -88
  34. package/types/core/WaveformSounds.d.ts +175 -0
  35. package/types/core/WaveformSounds.d.ts.map +1 -0
  36. package/types/core/element.d.ts +84 -0
  37. package/types/core/element.d.ts.map +1 -0
  38. package/types/core/engine.d.ts +72 -0
  39. package/types/core/engine.d.ts.map +1 -0
  40. package/types/core/options.d.ts +71 -0
  41. package/types/core/options.d.ts.map +1 -0
  42. package/types/data/bpm.d.ts +80 -0
  43. package/types/data/bpm.d.ts.map +1 -0
  44. package/types/data/navigation.d.ts +62 -0
  45. package/types/data/navigation.d.ts.map +1 -0
  46. package/types/data/sounds.d.ts +168 -0
  47. package/types/data/sounds.d.ts.map +1 -0
  48. package/types/data/url-state.d.ts +109 -0
  49. package/types/data/url-state.d.ts.map +1 -0
  50. package/types/dom/colors.d.ts +48 -0
  51. package/types/dom/colors.d.ts.map +1 -0
  52. package/types/dom/draw.d.ts +72 -0
  53. package/types/dom/draw.d.ts.map +1 -0
  54. package/types/dom/menus.d.ts +75 -0
  55. package/types/dom/menus.d.ts.map +1 -0
  56. package/types/dom/rows.d.ts +39 -0
  57. package/types/dom/rows.d.ts.map +1 -0
  58. package/types/entry.d.ts +28 -0
  59. package/types/entry.d.ts.map +1 -0
  60. package/types/index.d.ts +16 -0
  61. package/types/index.d.ts.map +1 -0
  62. package/types/render/html.d.ts +37 -0
  63. package/types/render/html.d.ts.map +1 -0
  64. package/types/render/icons.d.ts +17 -0
  65. package/types/render/icons.d.ts.map +1 -0
  66. package/types/render/markup.d.ts +141 -0
  67. package/types/render/markup.d.ts.map +1 -0
  68. package/types/render/options.d.ts +69 -0
  69. package/types/render/options.d.ts.map +1 -0
  70. package/types/render/strings.d.ts +43 -0
  71. package/types/render/strings.d.ts.map +1 -0
  72. package/types/render-entry.d.ts +8 -0
  73. package/types/render-entry.d.ts.map +1 -0
  74. package/types/shared/utils.d.ts +57 -0
  75. package/types/shared/utils.d.ts.map +1 -0
  76. package/types/types.d.ts +280 -0
  77. package/types/types.d.ts.map +1 -0
  78. package/types/vendor/arraypress-text.d.ts +250 -0
  79. package/dist/waveform-sounds-no-autoinit.cjs +0 -2159
  80. package/dist/waveform-sounds-render.cjs +0 -614
  81. package/dist/waveform-sounds.cjs +0 -2181
  82. package/no-autoinit.d.ts +0 -2
  83. package/render.d.ts +0 -5
  84. package/src/js/core/WaveformSounds.js +0 -993
  85. package/src/js/core/engine.js +0 -57
  86. package/src/js/data/url-state.js +0 -106
  87. package/src/js/dom/colors.js +0 -59
  88. package/src/js/entry.js +0 -25
  89. package/src/js/render/options.js +0 -90
@@ -1,57 +0,0 @@
1
- /**
2
- * The audio engine: ONE `WaveformPlayer` (self mode) that every sound in
3
- * the list plays through. These helpers decide how it's configured and
4
- * what it's given per sound; `WaveformSounds.js` owns its lifecycle.
5
- *
6
- * @module engine
7
- */
8
-
9
- /** The engine's own callbacks, which the list must always receive. */
10
- const CALLBACKS = ['onLoad', 'onPlay', 'onPause', 'onEnd', 'onTimeUpdate', 'onError'];
11
-
12
- /**
13
- * The engine player's options. The site's `playerOptions` override the
14
- * defaults, except two things the list depends on:
15
- * - `audioMode` is always `'self'` (the list owns its audio);
16
- * - the six callbacks always reach the list — a site's own callback of
17
- * the same name runs as well, after it, with the same arguments.
18
- *
19
- * @param {Object|null|undefined} user - The site's `playerOptions`.
20
- * @param {'inline'|'strip'} layout - Strip shows the engine, so it's taller.
21
- * @param {Record<string, Function>} handlers - The list's callbacks, keyed by
22
- * option name (`onPlay`, …).
23
- * @returns {Object} Options for `new WaveformPlayer(el, options)`.
24
- */
25
- export function engineOptions(user, layout, handlers) {
26
- const site = user || {};
27
- const strip = layout === 'strip';
28
- const options = {
29
- height: strip ? 48 : 32,
30
- waveformStyle: strip ? 'mirror' : 'bars',
31
- preload: 'metadata',
32
- singlePlay: true,
33
- ...site,
34
- audioMode: 'self',
35
- };
36
- for (const name of CALLBACKS) {
37
- options[name] = (...args) => {
38
- handlers[name]?.(...args);
39
- if (typeof site[name] === 'function') site[name](...args);
40
- };
41
- }
42
- return options;
43
- }
44
-
45
- /**
46
- * The waveform data to hand the engine for a sound: in the strip a
47
- * full-resolution sidecar when there is one, otherwise the row's peaks.
48
- * Supplied peaks also stop a hidden engine from downloading and decoding
49
- * the whole file just to draw it.
50
- *
51
- * @param {{peaks: number[]|null, waveform: string|null}} sound
52
- * @param {'inline'|'strip'} layout
53
- * @returns {number[]|string|null}
54
- */
55
- export function enginePeaks(sound, layout) {
56
- return (layout === 'strip' && sound.waveform) || sound.peaks || null;
57
- }
@@ -1,106 +0,0 @@
1
- /**
2
- * Filters in the address (`urlState`): which query parameters hold them,
3
- * reading them into a filter, and writing a filter back into a URL. Pure:
4
- * the runtime passes `location.search` / `location.href` in and calls
5
- * `history.replaceState` with what comes out.
6
- *
7
- * ?q=bass&type=Bass+loops&key=Fm&bpm=120-130&loop=one-shot&sort=bpm
8
- *
9
- * @module url-state
10
- */
11
-
12
- import {LOOP_FILTERS, normalizeKey} from './sounds.js';
13
-
14
- /**
15
- * The parameter names, or null when urlState is off. A string urlState
16
- * prefixes them (`'pack'` → `pack-q`, `pack-type`, …) so two lists on one
17
- * page don't share a filter.
18
- *
19
- * @param {boolean|string|null|undefined} urlState
20
- * @returns {{q: string, type: string, key: string, bpm: string, loop: string, sort: string}|null}
21
- */
22
- export function urlKeys(urlState) {
23
- if (!urlState) return null;
24
- const p = typeof urlState === 'string' ? `${urlState}-` : '';
25
- return {q: `${p}q`, type: `${p}type`, key: `${p}key`, bpm: `${p}bpm`, loop: `${p}loop`, sort: `${p}sort`};
26
- }
27
-
28
- /**
29
- * A BPM range parameter: "120-130", "120-" or "-130".
30
- *
31
- * @param {string|null|undefined} value
32
- * @returns {{bpmMin: string, bpmMax: string}|null} null if malformed.
33
- */
34
- export function parseBpmRange(value) {
35
- const m = String(value ?? '').match(/^(\d*)-(\d*)$/);
36
- return m && (m[1] || m[2]) ? {bpmMin: m[1], bpmMax: m[2]} : null;
37
- }
38
-
39
- /**
40
- * The inverse of {@link parseBpmRange}; '' when neither end is set.
41
- *
42
- * @param {string|number|null|undefined} min
43
- * @param {string|number|null|undefined} max
44
- * @returns {string}
45
- */
46
- export function formatBpmRange(min, max) {
47
- const lo = min ?? '', hi = max ?? '';
48
- return lo !== '' || hi !== '' ? `${lo}-${hi}` : '';
49
- }
50
-
51
- /**
52
- * Read the filter and sort from a query string. Values the list can't use
53
- * (a type it doesn't have, a key no sound is in, a loops filter on a list
54
- * that isn't a mix of loops and one-shots, a sort it doesn't offer)
55
- * are ignored, never applied: a stale link shows the whole list, not an
56
- * empty one.
57
- *
58
- * @param {string} search - `location.search`.
59
- * @param {ReturnType<typeof urlKeys>} keys
60
- * @param {{types: {name: string}[], keys: string[], loops?: number, oneShots?: number}} available - From `facets()`.
61
- * @param {string[]} sorts - The sort orders offered.
62
- * @returns {{filter: Object, sort: string|null}} A filter patch, and the sort (or null).
63
- */
64
- export function readUrlState(search, keys, available, sorts) {
65
- const out = {filter: {}, sort: null};
66
- if (!keys) return out;
67
- const sp = new URLSearchParams(search);
68
- const q = sp.get(keys.q);
69
- if (q) out.filter.query = q;
70
- const type = sp.get(keys.type);
71
- if (type && available.types.some((t) => t.name === type)) out.filter.type = type;
72
- const key = normalizeKey(sp.get(keys.key));
73
- if (key && available.keys.includes(key)) out.filter.key = key;
74
- const bpm = parseBpmRange(sp.get(keys.bpm));
75
- if (bpm) Object.assign(out.filter, bpm);
76
- const loop = sp.get(keys.loop);
77
- if (LOOP_FILTERS.includes(loop) && available.loops > 0 && available.oneShots > 0) out.filter.loop = loop;
78
- const sort = sp.get(keys.sort);
79
- if (sort && sorts.includes(sort)) out.sort = sort;
80
- return out;
81
- }
82
-
83
- /**
84
- * The URL with the filter written in. Empty values (and the starting sort)
85
- * are removed rather than written empty; every other parameter and the
86
- * hash are kept.
87
- *
88
- * @param {string} href - `location.href`.
89
- * @param {ReturnType<typeof urlKeys>} keys
90
- * @param {{query: string, type: string, key: string, bpmMin: string, bpmMax: string, loop?: string}} filter
91
- * @param {string} sort
92
- * @param {string} defaultSort - The starting order (not written).
93
- * @returns {string} The new href (equal to `href` when nothing changed).
94
- */
95
- export function writeUrlState(href, keys, filter, sort, defaultSort) {
96
- if (!keys) return href;
97
- const url = new URL(href);
98
- const set = (name, v) => (v ? url.searchParams.set(name, v) : url.searchParams.delete(name));
99
- set(keys.q, String(filter.query ?? '').trim());
100
- set(keys.type, filter.type);
101
- set(keys.key, filter.key);
102
- set(keys.bpm, formatBpmRange(filter.bpmMin, filter.bpmMax));
103
- set(keys.loop, filter.loop);
104
- set(keys.sort, sort !== defaultSort ? sort : '');
105
- return url.href;
106
- }
@@ -1,59 +0,0 @@
1
- /**
2
- * Colours the CSS can't hand the runtime directly: the canvas needs real
3
- * colour values (it can't read `var()` or `color-mix()`), and the inverted
4
- * "on" states need the page surface behind the list.
5
- *
6
- * @module colors
7
- */
8
-
9
- /**
10
- * The alpha of a computed colour string: `rgba(0, 0, 0, 0)`,
11
- * `rgb(1 2 3 / 50%)`, `oklab(… / 0.9)`, `transparent`. Opaque when no alpha
12
- * is written.
13
- *
14
- * @param {string|null|undefined} color
15
- * @returns {number} 0..1.
16
- */
17
- export function parseAlpha(color) {
18
- const c = String(color ?? '').trim();
19
- if (!c || c === 'transparent') return 0;
20
- const slash = c.match(/\/\s*([\d.]+)(%?)\s*\)$/);
21
- if (slash) return Number(slash[1]) / (slash[2] ? 100 : 1);
22
- const rgba = c.match(/^rgba\(\s*[^,]+,[^,]+,[^,]+,\s*([\d.]+)\s*\)$/);
23
- if (rgba) return Number(rgba[1]);
24
- return 1;
25
- }
26
-
27
- /**
28
- * The first (near-)opaque background at or above an element — the page
29
- * surface the list sits on. Crosses shadow-root boundaries.
30
- *
31
- * @param {Element} el
32
- * @param {number} [minAlpha=0.95] - How opaque counts as "the surface".
33
- * @returns {string} A computed colour, or `#fff` when nothing is painted.
34
- */
35
- export function pageSurface(el, minAlpha = 0.95) {
36
- for (let node = el; node && node.nodeType === 1; node = node.parentElement || node.getRootNode?.().host) {
37
- const bg = getComputedStyle(node).backgroundColor;
38
- if (parseAlpha(bg) > minAlpha) return bg;
39
- }
40
- return '#fff';
41
- }
42
-
43
- /**
44
- * Resolve any CSS colour (a `var()`, `color-mix()`, `currentColor`) to a
45
- * concrete value, as it would compute inside `container`.
46
- *
47
- * @param {Element} container
48
- * @param {string} value - A CSS colour expression.
49
- * @returns {string} The computed colour.
50
- */
51
- export function resolveCssColor(container, value) {
52
- const probe = document.createElement('span');
53
- probe.style.cssText = 'position:absolute;width:0;height:0;overflow:hidden;visibility:hidden';
54
- probe.style.color = value;
55
- container.appendChild(probe);
56
- const color = getComputedStyle(probe).color;
57
- probe.remove();
58
- return color;
59
- }
package/src/js/entry.js DELETED
@@ -1,25 +0,0 @@
1
- /**
2
- * @arraypress/waveform-sounds — public surface, without the import-time
3
- * auto-init scan (`index.js` adds that). Anything new belongs here.
4
- *
5
- * @module entry
6
- */
7
- import {WaveformSounds, DEFAULT_OPTIONS} from './core/WaveformSounds.js';
8
- import {
9
- encodePeaks, decodePeaks, normalizeKey, normalizeSounds, parseManifest, facets, matches, sortSounds, formatDuration,
10
- } from './data/sounds.js';
11
- import {renderSounds, renderSoundsElement, DEFAULT_STRINGS} from './render/markup.js';
12
-
13
- WaveformSounds.utils = {encodePeaks, decodePeaks, normalizeKey, normalizeSounds, parseManifest, facets, matches, sortSounds, formatDuration, renderSounds};
14
- WaveformSounds.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
15
- WaveformSounds.DEFAULT_STRINGS = DEFAULT_STRINGS;
16
-
17
- // CDN / <script> usage: a global, like the rest of the family.
18
- if (typeof window !== 'undefined') window.WaveformSounds = WaveformSounds;
19
-
20
- export default WaveformSounds;
21
- export {
22
- WaveformSounds, DEFAULT_OPTIONS, DEFAULT_STRINGS,
23
- encodePeaks, decodePeaks, normalizeKey, normalizeSounds, parseManifest, facets, matches, sortSounds, formatDuration,
24
- renderSounds, renderSoundsElement,
25
- };
@@ -1,90 +0,0 @@
1
- /**
2
- * The options that shape the markup — the renderer's whole input — with
3
- * their defaults and validation. DOM-free: the server renderer uses it,
4
- * and the runtime's option surface (`core/options.js`) builds on it.
5
- *
6
- * @module render/options
7
- */
8
-
9
- import {SORTS} from '../data/sounds.js';
10
- import {DEFAULT_STRINGS} from './strings.js';
11
-
12
- /** The filter controls a list can offer. */
13
- export const FILTERS = ['type', 'key', 'bpm', 'loop'];
14
-
15
- /** The columns a row can show after its title. */
16
- export const COLUMNS = ['type', 'bpm', 'key', 'duration'];
17
-
18
- /** The options that shape the markup (the renderer's whole input). */
19
- export const RENDER_DEFAULTS = {
20
- player: 'inline',
21
- search: true,
22
- filters: [...FILTERS],
23
- sorts: [...SORTS],
24
- loopToggle: true,
25
- showCount: true,
26
- menuSearch: 8,
27
- pageSize: 50,
28
- columns: [...COLUMNS],
29
- maxTypeChips: 10,
30
- };
31
-
32
- /**
33
- * The object without its `undefined` entries (so spreading it can't erase
34
- * a default).
35
- *
36
- * @param {Object|null|undefined} obj
37
- * @returns {Object}
38
- */
39
- export function defined(obj) {
40
- const out = {};
41
- for (const k in obj ?? {}) if (obj[k] !== undefined) out[k] = obj[k];
42
- return out;
43
- }
44
-
45
- /**
46
- * The entries of `values` that are allowed, in the caller's order; the
47
- * fallback when `values` isn't a list.
48
- *
49
- * @param {unknown} values
50
- * @param {string[]} allowed
51
- * @param {string[]} fallback
52
- * @returns {string[]}
53
- */
54
- export function allowedList(values, allowed, fallback) {
55
- return Array.isArray(values) ? values.filter((v) => allowed.includes(v)) : fallback;
56
- }
57
-
58
- /**
59
- * A whole number ≥ 0, or the fallback.
60
- *
61
- * @param {unknown} value
62
- * @param {number} fallback
63
- * @returns {number}
64
- */
65
- export function wholeNumber(value, fallback) {
66
- const n = Number(value);
67
- return Number.isFinite(n) && n >= 0 ? Math.floor(n) : fallback;
68
- }
69
-
70
- /**
71
- * The render options, defaulted and validated: unknown filters, sorts and
72
- * columns are dropped, counts are whole numbers, `strings` is complete.
73
- *
74
- * @param {Object} [options]
75
- * @returns {typeof RENDER_DEFAULTS & {strings: typeof DEFAULT_STRINGS, idPrefix?: string}}
76
- */
77
- export function resolveRenderOptions(options = {}) {
78
- const o = {...RENDER_DEFAULTS, ...defined(options)};
79
- return {
80
- ...o,
81
- player: o.player === 'strip' ? 'strip' : 'inline',
82
- filters: allowedList(o.filters, FILTERS, RENDER_DEFAULTS.filters),
83
- sorts: allowedList(o.sorts, SORTS, RENDER_DEFAULTS.sorts),
84
- columns: allowedList(o.columns, COLUMNS, RENDER_DEFAULTS.columns),
85
- menuSearch: wholeNumber(o.menuSearch, RENDER_DEFAULTS.menuSearch),
86
- maxTypeChips: wholeNumber(o.maxTypeChips, RENDER_DEFAULTS.maxTypeChips),
87
- pageSize: wholeNumber(o.pageSize, RENDER_DEFAULTS.pageSize),
88
- strings: {...DEFAULT_STRINGS, ...defined(options.strings)},
89
- };
90
- }