@arraypress/waveform-sounds 0.2.0 → 0.3.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 CHANGED
@@ -66,8 +66,9 @@ A sound is a one-shot unless it says otherwise. Mark loops with `loop: true`:
66
66
 
67
67
  A loop gets a small loop icon beside its name. Once a list has both kinds,
68
68
  a **All / Loops / One-shots** filter appears (`filters` entry `'loop'`;
69
- drop it to hide the control). And the Loop toggle repeats only loops: a
70
- one-shot always plays once. A list that marks nothing works as before.
69
+ drop it to hide the control). And the Loop toggle starts on and repeats
70
+ only loops: a one-shot always plays once (turn it off to hear loops once).
71
+ A list that marks nothing works as before.
71
72
  `waveform-gen --manifest` sets `loop: true` for files whose folder or name
72
73
  contains the word "loop".
73
74
 
@@ -377,7 +377,7 @@ function partsOf(menu) {
377
377
  value: menu.querySelector("[data-ws-menu-value]")
378
378
  };
379
379
  }
380
- var focusOwner = (p) => p.search || p.list;
380
+ var focusOwner = (p) => p.search || p.list || p.pop.querySelector("input") || p.pop.querySelector("button");
381
381
  var optionsOf = (p, visibleOnly = false) => [...p.list.querySelectorAll('[role="option"]')].filter((o) => !visibleOnly || !o.hidden);
382
382
  var Menus = class {
383
383
  /**
@@ -409,6 +409,15 @@ var Menus = class {
409
409
  this.open(menu);
410
410
  }
411
411
  }, sig);
412
+ if (!p.list) {
413
+ p.pop.addEventListener("keydown", (e) => {
414
+ if (e.key !== "Escape") return;
415
+ e.preventDefault();
416
+ e.stopPropagation();
417
+ this.close(true);
418
+ }, sig);
419
+ continue;
420
+ }
412
421
  p.list.addEventListener("click", (e) => {
413
422
  const opt = e.target.closest('[role="option"]');
414
423
  if (opt) this._pick(name, opt.dataset.value);
@@ -443,8 +452,8 @@ var Menus = class {
443
452
  }
444
453
  menu.classList.remove("ws-menu--end");
445
454
  if (p.pop.getBoundingClientRect().right > this.container.getBoundingClientRect().right + 1) menu.classList.add("ws-menu--end");
446
- this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
447
- focusOwner(p).focus();
455
+ if (p.list) this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
456
+ focusOwner(p)?.focus();
448
457
  }
449
458
  /**
450
459
  * Close the open menu, if any.
@@ -472,6 +481,7 @@ var Menus = class {
472
481
  const menu = this.menus[name];
473
482
  if (!menu) return;
474
483
  const p = partsOf(menu);
484
+ if (!p.list) return;
475
485
  let label = null;
476
486
  for (const opt of optionsOf(p)) {
477
487
  const on = opt.dataset.value === String(value ?? "");
@@ -609,6 +619,31 @@ function limitToReveal(limit, pos) {
609
619
  return pos >= limit ? pos + 1 : limit;
610
620
  }
611
621
 
622
+ // src/js/data/bpm.js
623
+ function bpmFilterFromRange(lo, hi, extent) {
624
+ const a = Math.min(lo, hi), b = Math.max(lo, hi);
625
+ return {
626
+ bpmMin: a > extent.min ? String(a) : "",
627
+ bpmMax: b < extent.max ? String(b) : ""
628
+ };
629
+ }
630
+ function bpmRangeFromFilter(filter, extent) {
631
+ const read = (v, fallback) => {
632
+ const n = Number(v);
633
+ return v === "" || v == null || !Number.isFinite(n) ? fallback : Math.min(Math.max(n, extent.min), extent.max);
634
+ };
635
+ const lo = read(filter.bpmMin, extent.min);
636
+ const hi = read(filter.bpmMax, extent.max);
637
+ return lo <= hi ? { lo, hi } : { lo: hi, hi: lo };
638
+ }
639
+ function bpmFraction(value, extent) {
640
+ const span = extent.max - extent.min;
641
+ return span > 0 ? Math.min(Math.max((value - extent.min) / span, 0), 1) : 0;
642
+ }
643
+ function hasBpmFilter(filter) {
644
+ return filter.bpmMin != null && filter.bpmMin !== "" || filter.bpmMax != null && filter.bpmMax !== "";
645
+ }
646
+
612
647
  // src/js/render/strings.js
613
648
  var DEFAULT_STRINGS = {
614
649
  search: "Search sounds",
@@ -624,6 +659,8 @@ var DEFAULT_STRINGS = {
624
659
  bpm: "BPM",
625
660
  bpmMin: "Min BPM",
626
661
  bpmMax: "Max BPM",
662
+ anyBpm: "Any BPM",
663
+ bpmRange: "{min}\u2013{max} BPM",
627
664
  sort: "Sort",
628
665
  sortBy: "Sort by",
629
666
  sortDefault: "Default",
@@ -658,6 +695,11 @@ var SORT_LABEL_KEYS = {
658
695
  function fill(template, vars = {}) {
659
696
  return String(template).replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
660
697
  }
698
+ function bpmLabel(filter, extent, strings = DEFAULT_STRINGS) {
699
+ if (!hasBpmFilter(filter)) return strings.anyBpm;
700
+ const { lo, hi } = bpmRangeFromFilter(filter, extent);
701
+ return fill(strings.bpmRange, { min: lo, max: hi });
702
+ }
661
703
  function countText(shown, total, strings = DEFAULT_STRINGS) {
662
704
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
663
705
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
@@ -715,7 +757,7 @@ var DEFAULT_OPTIONS = {
715
757
  progressColor: null,
716
758
  barWidth: 2,
717
759
  barGap: 1,
718
- loop: false,
760
+ loop: null,
719
761
  autoAdvance: false,
720
762
  arrowAudition: true,
721
763
  idPrefix: null,
@@ -848,6 +890,9 @@ function toolbarPlan(f, o) {
848
890
  count: !!o.showCount
849
891
  };
850
892
  }
893
+ function loopsByDefault(loop, f) {
894
+ return loop == null ? f.loops > 0 : !!loop;
895
+ }
851
896
  function renderSearch(s) {
852
897
  return h(
853
898
  "label",
@@ -901,15 +946,45 @@ function renderMenu(name, m) {
901
946
  )
902
947
  );
903
948
  }
904
- function renderBpmRange(s, range) {
905
- const field = (attr, label, placeholder) => h("input", { type: "number", inputmode: "numeric", [attr]: true, "aria-label": label, placeholder, min: "0", step: "1" });
949
+ function renderBpmMenu({ id, s, range }) {
950
+ const popId = `${id}-bpm-pop`;
951
+ const handle = (attr, label, value) => h("input", {
952
+ type: "range",
953
+ class: "ws-range-input",
954
+ [attr]: true,
955
+ "aria-label": label,
956
+ min: range.min,
957
+ max: range.max,
958
+ step: "1",
959
+ value
960
+ });
906
961
  return h(
907
- "span",
908
- { class: "ws-bpm-range", role: "group", "aria-label": s.bpm },
909
- field("data-ws-bpm-min", s.bpmMin, range.min),
910
- h("span", { "aria-hidden": "true" }, "\u2013"),
911
- field("data-ws-bpm-max", s.bpmMax, range.max),
912
- h("span", { class: "ws-bpm-unit", "aria-hidden": "true" }, text(s.bpm))
962
+ "div",
963
+ { class: "ws-menu ws-menu--bpm", "data-ws-menu": "bpm" },
964
+ h(
965
+ "button",
966
+ { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
967
+ h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
968
+ h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
969
+ ICONS.chevron
970
+ ),
971
+ h(
972
+ "div",
973
+ { class: "ws-menu-pop ws-bpm-pop", id: popId, role: "dialog", "aria-label": s.bpm, "data-ws-menu-pop": true, hidden: true },
974
+ h(
975
+ "div",
976
+ { class: "ws-bpm-head" },
977
+ h("span", { class: "ws-bpm-readout", "data-ws-bpm-readout": true, "aria-live": "polite" }, text(fill(s.bpmRange, range))),
978
+ h("button", { type: "button", class: "ws-bpm-clear", "data-ws-bpm-clear": true }, text(s.anyBpm))
979
+ ),
980
+ h(
981
+ "div",
982
+ { class: "ws-range", "data-ws-bpm-range": true },
983
+ handle("data-ws-bpm-min", s.bpmMin, range.min),
984
+ handle("data-ws-bpm-max", s.bpmMax, range.max)
985
+ ),
986
+ h("div", { class: "ws-range-ends", "aria-hidden": "true" }, h("span", {}, text(range.min)), h("span", {}, text(range.max)))
987
+ )
913
988
  );
914
989
  }
915
990
  function renderLoopFilter(s) {
@@ -926,8 +1001,8 @@ function renderLoopFilter(s) {
926
1001
  button("one-shot", s.oneShots)
927
1002
  );
928
1003
  }
929
- function renderLoopToggle(s) {
930
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": "false" }, ICONS.loop, h("span", {}, text(s.loop)));
1004
+ function renderLoopToggle(s, pressed) {
1005
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
931
1006
  }
932
1007
  function renderChip(value, label, count, pressed) {
933
1008
  return h(
@@ -953,7 +1028,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
953
1028
  placeholder: s.findKey,
954
1029
  options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
955
1030
  }),
956
- plan.bpm && renderBpmRange(s, f.bpm),
1031
+ plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
957
1032
  plan.loops && renderLoopFilter(s),
958
1033
  plan.sorts.length > 0 && menu("sort", {
959
1034
  label: s.sort,
@@ -961,7 +1036,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
961
1036
  value: plan.sorts[0],
962
1037
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
963
1038
  }),
964
- plan.loop && renderLoopToggle(s)
1039
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
965
1040
  ].filter(Boolean);
966
1041
  const chips = plan.types === "chips" && h(
967
1042
  "div",
@@ -1248,7 +1323,9 @@ var WaveformSounds = class _WaveformSounds {
1248
1323
  this._bind();
1249
1324
  this._observe();
1250
1325
  this._resolveColors();
1326
+ if (!this._loopSet) this.loop = loopsByDefault(this.options.loop, facets(this.sounds));
1251
1327
  this._setLoop(this.loop);
1328
+ this._bpmExtent = facets(this.sounds).bpm;
1252
1329
  this._sorts = availableSorts(this.render.sorts, facets(this.sounds));
1253
1330
  if (!this._sortSet) this.sortBy = this._sorts[0] || "default";
1254
1331
  this._readUrl();
@@ -1280,6 +1357,10 @@ var WaveformSounds = class _WaveformSounds {
1280
1357
  search: q("[data-ws-search]"),
1281
1358
  bpmMin: q("[data-ws-bpm-min]"),
1282
1359
  bpmMax: q("[data-ws-bpm-max]"),
1360
+ bpmRange: q("[data-ws-bpm-range]"),
1361
+ bpmReadout: q("[data-ws-bpm-readout]"),
1362
+ bpmClear: q("[data-ws-bpm-clear]"),
1363
+ bpmLabel: q('[data-ws-menu="bpm"] [data-ws-menu-value]'),
1283
1364
  loop: q("[data-ws-loop]"),
1284
1365
  count: q("[data-ws-count]"),
1285
1366
  empty: q("[data-ws-empty]"),
@@ -1319,12 +1400,20 @@ var WaveformSounds = class _WaveformSounds {
1319
1400
  this.setFilter({ query: "" });
1320
1401
  }
1321
1402
  }, sig);
1322
- const onBpm = () => {
1403
+ const onBpm = (moved) => {
1404
+ const lo = Number($.bpmMin.value), hi = Number($.bpmMax.value);
1405
+ if (lo > hi) moved.value = String(moved === $.bpmMin ? hi : lo);
1406
+ const f = bpmFilterFromRange(Number($.bpmMin.value), Number($.bpmMax.value), this._bpmExtent);
1407
+ this._showBpm(f);
1323
1408
  clearTimeout(bpmTimer);
1324
- bpmTimer = setTimeout(() => this.setFilter({ bpmMin: $.bpmMin?.value ?? "", bpmMax: $.bpmMax?.value ?? "" }), BPM_DELAY);
1409
+ bpmTimer = setTimeout(() => this.setFilter(f), BPM_DELAY);
1325
1410
  };
1326
- $.bpmMin?.addEventListener("input", onBpm, sig);
1327
- $.bpmMax?.addEventListener("input", onBpm, sig);
1411
+ $.bpmMin?.addEventListener("input", () => onBpm($.bpmMin), sig);
1412
+ $.bpmMax?.addEventListener("input", () => onBpm($.bpmMax), sig);
1413
+ $.bpmClear?.addEventListener("click", () => {
1414
+ clearTimeout(bpmTimer);
1415
+ this.setFilter({ bpmMin: "", bpmMax: "" });
1416
+ }, sig);
1328
1417
  this.menus = new Menus(root, {
1329
1418
  signal: this._ctl.signal,
1330
1419
  onPick: (name, value) => name === "sort" ? this.setSort(value) : this.setFilter({ [name]: value })
@@ -1616,11 +1705,32 @@ var WaveformSounds = class _WaveformSounds {
1616
1705
  this.menus?.setValue("type", f.type || "");
1617
1706
  this.menus?.setValue("key", f.key || "");
1618
1707
  this.menus?.setValue("sort", this.sortBy);
1619
- if ($.bpmMin && $.bpmMin.value !== String(f.bpmMin)) $.bpmMin.value = f.bpmMin;
1620
- if ($.bpmMax && $.bpmMax.value !== String(f.bpmMax)) $.bpmMax.value = f.bpmMax;
1708
+ if ($.bpmMin && $.bpmMax && this._bpmExtent) {
1709
+ const { lo, hi } = bpmRangeFromFilter(f, this._bpmExtent);
1710
+ $.bpmMin.value = String(lo);
1711
+ $.bpmMax.value = String(hi);
1712
+ this._showBpm(f);
1713
+ }
1621
1714
  $.chips.forEach((c) => c.setAttribute("aria-pressed", String(c.dataset.wsType === (f.type || ""))));
1622
1715
  $.loopFilter.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.wsLoopFilter === (f.loop || ""))));
1623
1716
  }
1717
+ /**
1718
+ * Show a BPM filter in its menu: the button label, the panel's readout,
1719
+ * the filled part of the track, and which handle sits on top (the one
1720
+ * that can still move, when they meet).
1721
+ * @private
1722
+ */
1723
+ _showBpm(f) {
1724
+ const $ = this.$, extent = this._bpmExtent;
1725
+ if (!$?.bpmRange || !extent) return;
1726
+ const { lo, hi } = bpmRangeFromFilter(f, extent);
1727
+ if ($.bpmLabel) $.bpmLabel.textContent = bpmLabel(f, extent, this.strings);
1728
+ if ($.bpmReadout) $.bpmReadout.textContent = fill(this.strings.bpmRange, { min: lo, max: hi });
1729
+ $.bpmRange.style.setProperty("--ws-lo", String(bpmFraction(lo, extent)));
1730
+ $.bpmRange.style.setProperty("--ws-hi", String(bpmFraction(hi, extent)));
1731
+ $.bpmRange.classList.toggle("is-min-top", lo >= hi && lo > extent.min);
1732
+ $.bpmRange.classList.toggle("is-active", lo > extent.min || hi < extent.max);
1733
+ }
1624
1734
  /**
1625
1735
  * Lay the rows out for the current filter, sort and page: order, hide,
1626
1736
  * "Show more", the empty state and the count. Then report it.
@@ -1797,6 +1907,7 @@ var WaveformSounds = class _WaveformSounds {
1797
1907
  * @param {boolean} on
1798
1908
  */
1799
1909
  setLoop(on) {
1910
+ this._loopSet = true;
1800
1911
  this.loop = !!on;
1801
1912
  this._setLoop(this.loop);
1802
1913
  }
@@ -338,7 +338,7 @@ function partsOf(menu) {
338
338
  value: menu.querySelector("[data-ws-menu-value]")
339
339
  };
340
340
  }
341
- var focusOwner = (p) => p.search || p.list;
341
+ var focusOwner = (p) => p.search || p.list || p.pop.querySelector("input") || p.pop.querySelector("button");
342
342
  var optionsOf = (p, visibleOnly = false) => [...p.list.querySelectorAll('[role="option"]')].filter((o) => !visibleOnly || !o.hidden);
343
343
  var Menus = class {
344
344
  /**
@@ -370,6 +370,15 @@ var Menus = class {
370
370
  this.open(menu);
371
371
  }
372
372
  }, sig);
373
+ if (!p.list) {
374
+ p.pop.addEventListener("keydown", (e) => {
375
+ if (e.key !== "Escape") return;
376
+ e.preventDefault();
377
+ e.stopPropagation();
378
+ this.close(true);
379
+ }, sig);
380
+ continue;
381
+ }
373
382
  p.list.addEventListener("click", (e) => {
374
383
  const opt = e.target.closest('[role="option"]');
375
384
  if (opt) this._pick(name, opt.dataset.value);
@@ -404,8 +413,8 @@ var Menus = class {
404
413
  }
405
414
  menu.classList.remove("ws-menu--end");
406
415
  if (p.pop.getBoundingClientRect().right > this.container.getBoundingClientRect().right + 1) menu.classList.add("ws-menu--end");
407
- this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
408
- focusOwner(p).focus();
416
+ if (p.list) this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
417
+ focusOwner(p)?.focus();
409
418
  }
410
419
  /**
411
420
  * Close the open menu, if any.
@@ -433,6 +442,7 @@ var Menus = class {
433
442
  const menu = this.menus[name];
434
443
  if (!menu) return;
435
444
  const p = partsOf(menu);
445
+ if (!p.list) return;
436
446
  let label = null;
437
447
  for (const opt of optionsOf(p)) {
438
448
  const on = opt.dataset.value === String(value ?? "");
@@ -570,6 +580,31 @@ function limitToReveal(limit, pos) {
570
580
  return pos >= limit ? pos + 1 : limit;
571
581
  }
572
582
 
583
+ // src/js/data/bpm.js
584
+ function bpmFilterFromRange(lo, hi, extent) {
585
+ const a = Math.min(lo, hi), b = Math.max(lo, hi);
586
+ return {
587
+ bpmMin: a > extent.min ? String(a) : "",
588
+ bpmMax: b < extent.max ? String(b) : ""
589
+ };
590
+ }
591
+ function bpmRangeFromFilter(filter, extent) {
592
+ const read = (v, fallback) => {
593
+ const n = Number(v);
594
+ return v === "" || v == null || !Number.isFinite(n) ? fallback : Math.min(Math.max(n, extent.min), extent.max);
595
+ };
596
+ const lo = read(filter.bpmMin, extent.min);
597
+ const hi = read(filter.bpmMax, extent.max);
598
+ return lo <= hi ? { lo, hi } : { lo: hi, hi: lo };
599
+ }
600
+ function bpmFraction(value, extent) {
601
+ const span = extent.max - extent.min;
602
+ return span > 0 ? Math.min(Math.max((value - extent.min) / span, 0), 1) : 0;
603
+ }
604
+ function hasBpmFilter(filter) {
605
+ return filter.bpmMin != null && filter.bpmMin !== "" || filter.bpmMax != null && filter.bpmMax !== "";
606
+ }
607
+
573
608
  // src/js/render/strings.js
574
609
  var DEFAULT_STRINGS = {
575
610
  search: "Search sounds",
@@ -585,6 +620,8 @@ var DEFAULT_STRINGS = {
585
620
  bpm: "BPM",
586
621
  bpmMin: "Min BPM",
587
622
  bpmMax: "Max BPM",
623
+ anyBpm: "Any BPM",
624
+ bpmRange: "{min}\u2013{max} BPM",
588
625
  sort: "Sort",
589
626
  sortBy: "Sort by",
590
627
  sortDefault: "Default",
@@ -619,6 +656,11 @@ var SORT_LABEL_KEYS = {
619
656
  function fill(template, vars = {}) {
620
657
  return String(template).replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
621
658
  }
659
+ function bpmLabel(filter, extent, strings = DEFAULT_STRINGS) {
660
+ if (!hasBpmFilter(filter)) return strings.anyBpm;
661
+ const { lo, hi } = bpmRangeFromFilter(filter, extent);
662
+ return fill(strings.bpmRange, { min: lo, max: hi });
663
+ }
622
664
  function countText(shown, total, strings = DEFAULT_STRINGS) {
623
665
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
624
666
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
@@ -676,7 +718,7 @@ var DEFAULT_OPTIONS = {
676
718
  progressColor: null,
677
719
  barWidth: 2,
678
720
  barGap: 1,
679
- loop: false,
721
+ loop: null,
680
722
  autoAdvance: false,
681
723
  arrowAudition: true,
682
724
  idPrefix: null,
@@ -809,6 +851,9 @@ function toolbarPlan(f, o) {
809
851
  count: !!o.showCount
810
852
  };
811
853
  }
854
+ function loopsByDefault(loop, f) {
855
+ return loop == null ? f.loops > 0 : !!loop;
856
+ }
812
857
  function renderSearch(s) {
813
858
  return h(
814
859
  "label",
@@ -862,15 +907,45 @@ function renderMenu(name, m) {
862
907
  )
863
908
  );
864
909
  }
865
- function renderBpmRange(s, range) {
866
- const field = (attr, label, placeholder) => h("input", { type: "number", inputmode: "numeric", [attr]: true, "aria-label": label, placeholder, min: "0", step: "1" });
910
+ function renderBpmMenu({ id, s, range }) {
911
+ const popId = `${id}-bpm-pop`;
912
+ const handle = (attr, label, value) => h("input", {
913
+ type: "range",
914
+ class: "ws-range-input",
915
+ [attr]: true,
916
+ "aria-label": label,
917
+ min: range.min,
918
+ max: range.max,
919
+ step: "1",
920
+ value
921
+ });
867
922
  return h(
868
- "span",
869
- { class: "ws-bpm-range", role: "group", "aria-label": s.bpm },
870
- field("data-ws-bpm-min", s.bpmMin, range.min),
871
- h("span", { "aria-hidden": "true" }, "\u2013"),
872
- field("data-ws-bpm-max", s.bpmMax, range.max),
873
- h("span", { class: "ws-bpm-unit", "aria-hidden": "true" }, text(s.bpm))
923
+ "div",
924
+ { class: "ws-menu ws-menu--bpm", "data-ws-menu": "bpm" },
925
+ h(
926
+ "button",
927
+ { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
928
+ h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
929
+ h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
930
+ ICONS.chevron
931
+ ),
932
+ h(
933
+ "div",
934
+ { class: "ws-menu-pop ws-bpm-pop", id: popId, role: "dialog", "aria-label": s.bpm, "data-ws-menu-pop": true, hidden: true },
935
+ h(
936
+ "div",
937
+ { class: "ws-bpm-head" },
938
+ h("span", { class: "ws-bpm-readout", "data-ws-bpm-readout": true, "aria-live": "polite" }, text(fill(s.bpmRange, range))),
939
+ h("button", { type: "button", class: "ws-bpm-clear", "data-ws-bpm-clear": true }, text(s.anyBpm))
940
+ ),
941
+ h(
942
+ "div",
943
+ { class: "ws-range", "data-ws-bpm-range": true },
944
+ handle("data-ws-bpm-min", s.bpmMin, range.min),
945
+ handle("data-ws-bpm-max", s.bpmMax, range.max)
946
+ ),
947
+ h("div", { class: "ws-range-ends", "aria-hidden": "true" }, h("span", {}, text(range.min)), h("span", {}, text(range.max)))
948
+ )
874
949
  );
875
950
  }
876
951
  function renderLoopFilter(s) {
@@ -887,8 +962,8 @@ function renderLoopFilter(s) {
887
962
  button("one-shot", s.oneShots)
888
963
  );
889
964
  }
890
- function renderLoopToggle(s) {
891
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": "false" }, ICONS.loop, h("span", {}, text(s.loop)));
965
+ function renderLoopToggle(s, pressed) {
966
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
892
967
  }
893
968
  function renderChip(value, label, count, pressed) {
894
969
  return h(
@@ -914,7 +989,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
914
989
  placeholder: s.findKey,
915
990
  options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
916
991
  }),
917
- plan.bpm && renderBpmRange(s, f.bpm),
992
+ plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
918
993
  plan.loops && renderLoopFilter(s),
919
994
  plan.sorts.length > 0 && menu("sort", {
920
995
  label: s.sort,
@@ -922,7 +997,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
922
997
  value: plan.sorts[0],
923
998
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
924
999
  }),
925
- plan.loop && renderLoopToggle(s)
1000
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
926
1001
  ].filter(Boolean);
927
1002
  const chips = plan.types === "chips" && h(
928
1003
  "div",
@@ -1209,7 +1284,9 @@ var WaveformSounds = class _WaveformSounds {
1209
1284
  this._bind();
1210
1285
  this._observe();
1211
1286
  this._resolveColors();
1287
+ if (!this._loopSet) this.loop = loopsByDefault(this.options.loop, facets(this.sounds));
1212
1288
  this._setLoop(this.loop);
1289
+ this._bpmExtent = facets(this.sounds).bpm;
1213
1290
  this._sorts = availableSorts(this.render.sorts, facets(this.sounds));
1214
1291
  if (!this._sortSet) this.sortBy = this._sorts[0] || "default";
1215
1292
  this._readUrl();
@@ -1241,6 +1318,10 @@ var WaveformSounds = class _WaveformSounds {
1241
1318
  search: q("[data-ws-search]"),
1242
1319
  bpmMin: q("[data-ws-bpm-min]"),
1243
1320
  bpmMax: q("[data-ws-bpm-max]"),
1321
+ bpmRange: q("[data-ws-bpm-range]"),
1322
+ bpmReadout: q("[data-ws-bpm-readout]"),
1323
+ bpmClear: q("[data-ws-bpm-clear]"),
1324
+ bpmLabel: q('[data-ws-menu="bpm"] [data-ws-menu-value]'),
1244
1325
  loop: q("[data-ws-loop]"),
1245
1326
  count: q("[data-ws-count]"),
1246
1327
  empty: q("[data-ws-empty]"),
@@ -1280,12 +1361,20 @@ var WaveformSounds = class _WaveformSounds {
1280
1361
  this.setFilter({ query: "" });
1281
1362
  }
1282
1363
  }, sig);
1283
- const onBpm = () => {
1364
+ const onBpm = (moved) => {
1365
+ const lo = Number($.bpmMin.value), hi = Number($.bpmMax.value);
1366
+ if (lo > hi) moved.value = String(moved === $.bpmMin ? hi : lo);
1367
+ const f = bpmFilterFromRange(Number($.bpmMin.value), Number($.bpmMax.value), this._bpmExtent);
1368
+ this._showBpm(f);
1284
1369
  clearTimeout(bpmTimer);
1285
- bpmTimer = setTimeout(() => this.setFilter({ bpmMin: $.bpmMin?.value ?? "", bpmMax: $.bpmMax?.value ?? "" }), BPM_DELAY);
1370
+ bpmTimer = setTimeout(() => this.setFilter(f), BPM_DELAY);
1286
1371
  };
1287
- $.bpmMin?.addEventListener("input", onBpm, sig);
1288
- $.bpmMax?.addEventListener("input", onBpm, sig);
1372
+ $.bpmMin?.addEventListener("input", () => onBpm($.bpmMin), sig);
1373
+ $.bpmMax?.addEventListener("input", () => onBpm($.bpmMax), sig);
1374
+ $.bpmClear?.addEventListener("click", () => {
1375
+ clearTimeout(bpmTimer);
1376
+ this.setFilter({ bpmMin: "", bpmMax: "" });
1377
+ }, sig);
1289
1378
  this.menus = new Menus(root, {
1290
1379
  signal: this._ctl.signal,
1291
1380
  onPick: (name, value) => name === "sort" ? this.setSort(value) : this.setFilter({ [name]: value })
@@ -1577,11 +1666,32 @@ var WaveformSounds = class _WaveformSounds {
1577
1666
  this.menus?.setValue("type", f.type || "");
1578
1667
  this.menus?.setValue("key", f.key || "");
1579
1668
  this.menus?.setValue("sort", this.sortBy);
1580
- if ($.bpmMin && $.bpmMin.value !== String(f.bpmMin)) $.bpmMin.value = f.bpmMin;
1581
- if ($.bpmMax && $.bpmMax.value !== String(f.bpmMax)) $.bpmMax.value = f.bpmMax;
1669
+ if ($.bpmMin && $.bpmMax && this._bpmExtent) {
1670
+ const { lo, hi } = bpmRangeFromFilter(f, this._bpmExtent);
1671
+ $.bpmMin.value = String(lo);
1672
+ $.bpmMax.value = String(hi);
1673
+ this._showBpm(f);
1674
+ }
1582
1675
  $.chips.forEach((c) => c.setAttribute("aria-pressed", String(c.dataset.wsType === (f.type || ""))));
1583
1676
  $.loopFilter.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.wsLoopFilter === (f.loop || ""))));
1584
1677
  }
1678
+ /**
1679
+ * Show a BPM filter in its menu: the button label, the panel's readout,
1680
+ * the filled part of the track, and which handle sits on top (the one
1681
+ * that can still move, when they meet).
1682
+ * @private
1683
+ */
1684
+ _showBpm(f) {
1685
+ const $ = this.$, extent = this._bpmExtent;
1686
+ if (!$?.bpmRange || !extent) return;
1687
+ const { lo, hi } = bpmRangeFromFilter(f, extent);
1688
+ if ($.bpmLabel) $.bpmLabel.textContent = bpmLabel(f, extent, this.strings);
1689
+ if ($.bpmReadout) $.bpmReadout.textContent = fill(this.strings.bpmRange, { min: lo, max: hi });
1690
+ $.bpmRange.style.setProperty("--ws-lo", String(bpmFraction(lo, extent)));
1691
+ $.bpmRange.style.setProperty("--ws-hi", String(bpmFraction(hi, extent)));
1692
+ $.bpmRange.classList.toggle("is-min-top", lo >= hi && lo > extent.min);
1693
+ $.bpmRange.classList.toggle("is-active", lo > extent.min || hi < extent.max);
1694
+ }
1585
1695
  /**
1586
1696
  * Lay the rows out for the current filter, sort and page: order, hide,
1587
1697
  * "Show more", the empty state and the count. Then report it.
@@ -1758,6 +1868,7 @@ var WaveformSounds = class _WaveformSounds {
1758
1868
  * @param {boolean} on
1759
1869
  */
1760
1870
  setLoop(on) {
1871
+ this._loopSet = true;
1761
1872
  this.loop = !!on;
1762
1873
  this._setLoop(this.loop);
1763
1874
  }