@tremolo-ui/dom 0.7.0 → 0.8.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/dist/index.cjs +1298 -235
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +767 -84
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +767 -84
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1263 -237
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/file/accept.ts +17 -0
- package/src/file/drop-zone.ts +8 -10
- package/src/index.ts +76 -1
- package/src/input/check-steps.ts +144 -0
- package/src/input/defaults.ts +32 -0
- package/src/input/direction.ts +107 -0
- package/src/knob/geometry.ts +100 -0
- package/src/number-input/stepper-drag.ts +139 -0
- package/src/number-input/text.ts +100 -0
- package/src/number-input/value.ts +129 -0
- package/src/options/replace.ts +22 -0
- package/src/piano/index.ts +128 -2
- package/src/piano/layout.ts +19 -0
- package/src/piano/shortcuts.ts +86 -0
- package/src/pointer/long-press.ts +103 -0
- package/src/points-editor/index.ts +361 -0
- package/src/position.ts +20 -0
- package/src/slider/decimal-digits.ts +23 -0
- package/src/slider/marks.ts +56 -0
- package/src/style.ts +41 -0
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { clamp, isBlackKey, isWhiteKey, linearScale, normalizeValue, noteKey, noteKeys, stepValue, toPrecision } from "@tremolo-ui/functions";
|
|
1
|
+
import { clamp, isBlackKey, isWhiteKey, linearScale, normalizeValue, noteKey, noteKeys, radian, stepValue, toFixed, toPrecision } from "@tremolo-ui/functions";
|
|
2
2
|
//#region src/canvas/context.ts
|
|
3
3
|
/**
|
|
4
4
|
* The assignable parts of a 2D context's drawing state. They have to be
|
|
@@ -267,6 +267,20 @@ function matchesAccept(candidate, accept) {
|
|
|
267
267
|
} else if (type === rule) return true;
|
|
268
268
|
return undecided;
|
|
269
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Split files into those that satisfy `accept` and those that do not, keeping
|
|
272
|
+
* their order. See {@link matchesAccept}.
|
|
273
|
+
*/
|
|
274
|
+
function partitionByAccept(files, accept) {
|
|
275
|
+
const accepted = [];
|
|
276
|
+
const rejected = [];
|
|
277
|
+
for (const file of files) if (matchesAccept(file, accept)) accepted.push(file);
|
|
278
|
+
else rejected.push(file);
|
|
279
|
+
return {
|
|
280
|
+
accepted,
|
|
281
|
+
rejected
|
|
282
|
+
};
|
|
283
|
+
}
|
|
270
284
|
//#endregion
|
|
271
285
|
//#region src/file/drop-zone.ts
|
|
272
286
|
/**
|
|
@@ -346,14 +360,10 @@ function createDropZone(element, options = {}) {
|
|
|
346
360
|
invalid: false
|
|
347
361
|
});
|
|
348
362
|
if (opts.disabled) return;
|
|
349
|
-
const
|
|
350
|
-
const accepted = [];
|
|
351
|
-
const rejected = [];
|
|
352
|
-
for (const file of dropped) if (matchesAccept(file, opts.accept)) accepted.push(file);
|
|
353
|
-
else rejected.push(file);
|
|
354
|
-
if (rejected.length > 0) opts.onReject?.(rejected, event);
|
|
363
|
+
const { accepted, rejected } = partitionByAccept(event.dataTransfer?.files ?? [], opts.accept);
|
|
355
364
|
const taken = opts.multiple ? accepted : accepted.slice(0, 1);
|
|
356
365
|
if (taken.length > 0) opts.onDrop?.(taken, event);
|
|
366
|
+
if (rejected.length > 0) opts.onReject?.(rejected, event);
|
|
357
367
|
}
|
|
358
368
|
/**
|
|
359
369
|
* A drag that ends anywhere else — dropped on another element, cancelled
|
|
@@ -500,6 +510,247 @@ function applyDelta(value, direction, options, { min, max, step, scale = linearS
|
|
|
500
510
|
return clamp(toPrecision(stepped), min, max);
|
|
501
511
|
}
|
|
502
512
|
//#endregion
|
|
513
|
+
//#region src/input/check-steps.ts
|
|
514
|
+
/** Positions probed across the travel. The ends are left out so that the
|
|
515
|
+
* clamp at `min` and `max` cannot be mistaken for a press that does nothing. */
|
|
516
|
+
const PROBES = [
|
|
517
|
+
.1,
|
|
518
|
+
.2,
|
|
519
|
+
.3,
|
|
520
|
+
.4,
|
|
521
|
+
.5,
|
|
522
|
+
.6,
|
|
523
|
+
.7,
|
|
524
|
+
.8,
|
|
525
|
+
.9
|
|
526
|
+
];
|
|
527
|
+
const NONE = {
|
|
528
|
+
shiftKey: false,
|
|
529
|
+
altKey: false,
|
|
530
|
+
ctrlKey: false,
|
|
531
|
+
metaKey: false
|
|
532
|
+
};
|
|
533
|
+
const MODIFIER_STATE = {
|
|
534
|
+
shift: {
|
|
535
|
+
...NONE,
|
|
536
|
+
shiftKey: true
|
|
537
|
+
},
|
|
538
|
+
alt: {
|
|
539
|
+
...NONE,
|
|
540
|
+
altKey: true
|
|
541
|
+
},
|
|
542
|
+
ctrl: {
|
|
543
|
+
...NONE,
|
|
544
|
+
ctrlKey: true
|
|
545
|
+
},
|
|
546
|
+
meta: {
|
|
547
|
+
...NONE,
|
|
548
|
+
metaKey: true
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
/** The held keys that select each entry of a modifier-aware input option. */
|
|
552
|
+
function entries(options) {
|
|
553
|
+
if (Array.isArray(options)) return [NONE];
|
|
554
|
+
return Object.keys(options).map((key) => MODIFIER_STATE[key] ?? NONE);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Press every entry of `options` at nine points along the travel and report
|
|
558
|
+
* whether anything came of it.
|
|
559
|
+
*
|
|
560
|
+
* Run against `applyDelta` itself rather than against a reading of `step`:
|
|
561
|
+
* the whole point is that the amount, the step and the scale interact, and
|
|
562
|
+
* the pipeline is the only thing that knows how.
|
|
563
|
+
*/
|
|
564
|
+
function probe(options, range, format) {
|
|
565
|
+
const { min, max, scale = linearScale } = range;
|
|
566
|
+
const outcome = {
|
|
567
|
+
moved: false,
|
|
568
|
+
visible: false
|
|
569
|
+
};
|
|
570
|
+
for (const modifiers of entries(options)) for (const position of PROBES) {
|
|
571
|
+
const value = scale.denormalize(position, min, max);
|
|
572
|
+
const up = applyDelta(value, 1, options, range, modifiers);
|
|
573
|
+
const down = applyDelta(value, -1, options, range, modifiers);
|
|
574
|
+
if (up !== value || down !== value) outcome.moved = true;
|
|
575
|
+
if (!format) continue;
|
|
576
|
+
const shown = format(value);
|
|
577
|
+
if (format(up) !== shown || format(down) !== shown) outcome.visible = true;
|
|
578
|
+
}
|
|
579
|
+
return outcome;
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* The warnings to show when a key press or a wheel notch cannot produce a
|
|
583
|
+
* change the user can see. Empty when there is nothing to say.
|
|
584
|
+
*
|
|
585
|
+
* Two settings that are each fine on their own can cancel out, and nothing
|
|
586
|
+
* fails when they do — the control simply sits there:
|
|
587
|
+
*
|
|
588
|
+
* - **`step` coarser than the amount.** `keyboard={['raw', 0.1]}` with
|
|
589
|
+
* `step={1}` rounds every press straight back to where it started
|
|
590
|
+
* - **the display coarser than the amount.** A `format` showing two decimals
|
|
591
|
+
* of a kHz value cannot show a press worth 1 Hz
|
|
592
|
+
*
|
|
593
|
+
* The second is only reported when the press is invisible at *every* point
|
|
594
|
+
* along the travel. A display that rounds is a deliberate choice and is
|
|
595
|
+
* normally right — it is being too coarse everywhere that makes it a mistake.
|
|
596
|
+
*
|
|
597
|
+
* **Meant for development builds only.** It probes the whole travel, so call
|
|
598
|
+
* it behind an inline `process.env.NODE_ENV` check: the bundler then drops
|
|
599
|
+
* the call, and this function and its messages with it.
|
|
600
|
+
*/
|
|
601
|
+
function checkSteps({ component, axis, range, keyboard, wheel, format }) {
|
|
602
|
+
if (!range || !(range.min < range.max)) return [];
|
|
603
|
+
const where = axis ? `${component} (${axis})` : component;
|
|
604
|
+
const warnings = [];
|
|
605
|
+
for (const [name, options] of [["keyboard", keyboard], ["wheel", wheel]]) {
|
|
606
|
+
if (!options) continue;
|
|
607
|
+
const { moved, visible } = probe(options, range, format);
|
|
608
|
+
if (!moved) warnings.push(`[tremolo-ui] ${where}: \`${name}\` cannot move the value.` + (range.step !== void 0 ? ` Each press is smaller than \`step\` (${range.step}), so it rounds` : " Each press rounds") + " straight back to where it started.");
|
|
609
|
+
else if (format && !visible) warnings.push(`[tremolo-ui] ${where}: \`${name}\` moves the value, but \`format\` shows the same text before and after, everywhere in the range. The display is too coarse for it to be seen.`);
|
|
610
|
+
}
|
|
611
|
+
return warnings;
|
|
612
|
+
}
|
|
613
|
+
//#endregion
|
|
614
|
+
//#region src/input/defaults.ts
|
|
615
|
+
/**
|
|
616
|
+
* The keyboard amount used by Knob, NumberInput, Slider, and XYPad by default:
|
|
617
|
+
* 1 per press in the units of the value, and 0.1 with shift held. That is one
|
|
618
|
+
* `step` only while `step` is 1; a coarser `step` rounds 1 straight back, which
|
|
619
|
+
* `checkSteps` warns about.
|
|
620
|
+
*
|
|
621
|
+
* A modifier entry is not snapped to `step`, which is what lets the finer
|
|
622
|
+
* amount move at all — see `applyDelta`.
|
|
623
|
+
*/
|
|
624
|
+
const DEFAULT_KEYBOARD_OPTIONS = {
|
|
625
|
+
default: ["raw", 1],
|
|
626
|
+
shift: ["raw", .1]
|
|
627
|
+
};
|
|
628
|
+
/**
|
|
629
|
+
* The wheel amount used by Knob, NumberInput, Slider, and XYPad by default.
|
|
630
|
+
*
|
|
631
|
+
* Browsers turn shift+wheel into horizontal scrolling, which empties `deltaY`
|
|
632
|
+
* and fills `deltaX`, so no modifier is bound here.
|
|
633
|
+
*/
|
|
634
|
+
const DEFAULT_WHEEL_OPTIONS = ["raw", 1];
|
|
635
|
+
/**
|
|
636
|
+
* The drag sensitivity used by value controls by default. Shift makes the
|
|
637
|
+
* same movement cover a tenth of the range, matching the arrow keys.
|
|
638
|
+
*/
|
|
639
|
+
const DEFAULT_DRAG_SENSITIVITY = {
|
|
640
|
+
default: 1,
|
|
641
|
+
shift: .1
|
|
642
|
+
};
|
|
643
|
+
//#endregion
|
|
644
|
+
//#region src/input/direction.ts
|
|
645
|
+
/** Is `key` one of the four arrow keys? */
|
|
646
|
+
function isArrowKey(key) {
|
|
647
|
+
return key === "ArrowRight" || key === "ArrowLeft" || key === "ArrowUp" || key === "ArrowDown";
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* The direction an arrow key moves a single value: right and up raise it.
|
|
651
|
+
* `null` for any other key.
|
|
652
|
+
*/
|
|
653
|
+
function arrowKeyDirection(key) {
|
|
654
|
+
if (!isArrowKey(key)) return null;
|
|
655
|
+
return key === "ArrowRight" || key === "ArrowUp" ? 1 : -1;
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* The axis and direction an arrow key moves a position on screen. `null` for
|
|
659
|
+
* any other key.
|
|
660
|
+
*
|
|
661
|
+
* The key picks the axis, whichever element inside the control holds the
|
|
662
|
+
* focus: a two-dimensional control is one control to the person moving it.
|
|
663
|
+
*/
|
|
664
|
+
function arrowKeyMove(key) {
|
|
665
|
+
if (!isArrowKey(key)) return null;
|
|
666
|
+
return {
|
|
667
|
+
axis: key === "ArrowRight" || key === "ArrowLeft" ? 0 : 1,
|
|
668
|
+
direction: key === "ArrowLeft" || key === "ArrowUp" ? -1 : 1
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* The direction one wheel event moves a single value: scrolling up raises it.
|
|
673
|
+
* `null` when the event carries no movement the control reads.
|
|
674
|
+
*/
|
|
675
|
+
function wheelDirection(event, { horizontal = false } = {}) {
|
|
676
|
+
if (horizontal && event.deltaX !== 0) return event.deltaX > 0 ? 1 : -1;
|
|
677
|
+
if (event.deltaY === 0) return null;
|
|
678
|
+
return event.deltaY > 0 ? -1 : 1;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* The axis and direction one wheel event moves a position on screen. `null`
|
|
682
|
+
* when the event carries no movement.
|
|
683
|
+
*
|
|
684
|
+
* Scrolling moves y, and shift switches to x. Browsers turn shift+wheel into
|
|
685
|
+
* horizontal scrolling: `deltaY` comes out empty and `deltaX` carries the
|
|
686
|
+
* movement. Reading whichever axis moved keeps shift working as the x-axis
|
|
687
|
+
* modifier — and picks up a trackpad's own horizontal gesture, which never
|
|
688
|
+
* had a modifier.
|
|
689
|
+
*/
|
|
690
|
+
function wheelMove(event) {
|
|
691
|
+
const horizontal = event.deltaX !== 0;
|
|
692
|
+
const delta = horizontal ? event.deltaX : event.deltaY;
|
|
693
|
+
if (delta === 0) return null;
|
|
694
|
+
return {
|
|
695
|
+
axis: horizontal || event.shiftKey ? 0 : 1,
|
|
696
|
+
direction: delta < 0 ? -1 : 1
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
//#endregion
|
|
700
|
+
//#region src/knob/geometry.ts
|
|
701
|
+
/**
|
|
702
|
+
* Width and height of the viewBox a knob is drawn in. The arcs and the thumb
|
|
703
|
+
* are laid out in these units, and the SVG scales them to the knob's size.
|
|
704
|
+
*/
|
|
705
|
+
const KNOB_VIEWBOX_SIZE = 100;
|
|
706
|
+
const center = 50;
|
|
707
|
+
function knobAngles({ value, min, max, scale, startValue, angleRange }) {
|
|
708
|
+
const p = scale.normalize(value, min, max);
|
|
709
|
+
const s = scale.normalize(startValue, min, max);
|
|
710
|
+
const r1 = -angleRange / 2;
|
|
711
|
+
return {
|
|
712
|
+
p,
|
|
713
|
+
r1,
|
|
714
|
+
r2: r1 + Math.min(p, s) * angleRange,
|
|
715
|
+
r3: r1 + Math.max(p, s) * angleRange,
|
|
716
|
+
r4: angleRange / 2
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* The point at `angle` on a circle of `radius` around the centre of the
|
|
721
|
+
* viewBox.
|
|
722
|
+
*
|
|
723
|
+
* The radius depends on the stroke of the line being drawn, which each arc
|
|
724
|
+
* has its own of, so the point is found per arc rather than once for the knob.
|
|
725
|
+
*/
|
|
726
|
+
function knobArcPoint(angle, radius) {
|
|
727
|
+
return {
|
|
728
|
+
x: center + radius * Math.cos(radian(angle - 90)),
|
|
729
|
+
y: center + radius * Math.sin(radian(angle - 90))
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* The radius that keeps a stroke of `strokeWidth` inside the viewBox: half of
|
|
734
|
+
* the stroke falls outside the path it is drawn along.
|
|
735
|
+
*/
|
|
736
|
+
function knobArcRadius(strokeWidth) {
|
|
737
|
+
const width = typeof strokeWidth === "number" ? strokeWidth : Number.parseFloat(String(strokeWidth));
|
|
738
|
+
return Number.isFinite(width) ? center - width / 2 : center;
|
|
739
|
+
}
|
|
740
|
+
/** Build an SVG path for an arc, splitting full turns into drawable segments. */
|
|
741
|
+
function knobArcPath(startAngle, endAngle, radius) {
|
|
742
|
+
const start = knobArcPoint(startAngle, radius);
|
|
743
|
+
const sweep = endAngle - startAngle;
|
|
744
|
+
const segmentCount = Math.max(1, Math.ceil(Math.abs(sweep) / 180));
|
|
745
|
+
const segmentSweep = sweep / segmentCount;
|
|
746
|
+
let path = `M ${start.x} ${start.y}`;
|
|
747
|
+
for (let i = 1; i <= segmentCount; i += 1) {
|
|
748
|
+
const end = knobArcPoint(startAngle + segmentSweep * i, radius);
|
|
749
|
+
path += ` A ${radius} ${radius} 0 0 ${segmentSweep >= 0 ? 1 : 0} ${end.x} ${end.y}`;
|
|
750
|
+
}
|
|
751
|
+
return path;
|
|
752
|
+
}
|
|
753
|
+
//#endregion
|
|
503
754
|
//#region src/midi/access.ts
|
|
504
755
|
/** @private */
|
|
505
756
|
const PERMISSION_DENIED = "PERMISSION_DENIED";
|
|
@@ -713,113 +964,6 @@ function createMIDIInput(midiAccess, handlers) {
|
|
|
713
964
|
};
|
|
714
965
|
}
|
|
715
966
|
//#endregion
|
|
716
|
-
//#region src/piano/layout.ts
|
|
717
|
-
/**
|
|
718
|
-
* `[noteRange.first, noteRange.first + 1, ..., noteRange.last]`
|
|
719
|
-
*/
|
|
720
|
-
function getNoteRangeArray(noteRange) {
|
|
721
|
-
return Array.from({ length: noteRange.last - noteRange.first + 1 }, (_, i) => i + noteRange.first);
|
|
722
|
-
}
|
|
723
|
-
const DEFAULT_KEY_GAP = 1;
|
|
724
|
-
const DEFAULT_BLACK_KEY_WIDTH_RATIO = .65;
|
|
725
|
-
const DEFAULT_BLACK_KEY_HEIGHT_RATIO = .6;
|
|
726
|
-
/**
|
|
727
|
-
* How many white keys sit at or before each pitch class, counting from C.
|
|
728
|
-
*
|
|
729
|
-
* A black key shares the number of the white key to its left plus one, which
|
|
730
|
-
* puts it on the boundary between the two; {@link notePosition} then shifts it
|
|
731
|
-
* back by half its width to centre it there.
|
|
732
|
-
*/
|
|
733
|
-
const whiteKeysBefore = {
|
|
734
|
-
C: 0,
|
|
735
|
-
"C#": 1,
|
|
736
|
-
D: 1,
|
|
737
|
-
"D#": 2,
|
|
738
|
-
E: 2,
|
|
739
|
-
F: 3,
|
|
740
|
-
"F#": 4,
|
|
741
|
-
G: 4,
|
|
742
|
-
"G#": 5,
|
|
743
|
-
A: 5,
|
|
744
|
-
"A#": 6,
|
|
745
|
-
B: 6
|
|
746
|
-
};
|
|
747
|
-
/** Width of a black key in pixels. */
|
|
748
|
-
function blackKeyWidth(layout) {
|
|
749
|
-
return layout.whiteKeyWidth * (layout.blackKeyWidthRatio ?? DEFAULT_BLACK_KEY_WIDTH_RATIO);
|
|
750
|
-
}
|
|
751
|
-
function rawNotePosition(note, layout) {
|
|
752
|
-
const slot = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
753
|
-
const target = noteKey(note);
|
|
754
|
-
const first = noteKey(layout.noteRange.first);
|
|
755
|
-
const octave = Math.floor((note - layout.noteRange.first) / 12);
|
|
756
|
-
const octaveOffset = noteKeys.indexOf(first) > noteKeys.indexOf(target) ? 1 : 0;
|
|
757
|
-
const whiteKeysIn = whiteKeysBefore[target] - whiteKeysBefore[first] + (octave + octaveOffset) * 7;
|
|
758
|
-
return isBlackKey(note) ? whiteKeysIn * slot - blackKeyWidth(layout) / 2 : whiteKeysIn * slot;
|
|
759
|
-
}
|
|
760
|
-
function pianoBounds(layout) {
|
|
761
|
-
const notes = getNoteRangeArray(layout.noteRange);
|
|
762
|
-
if (notes.length === 0) return {
|
|
763
|
-
left: 0,
|
|
764
|
-
right: 0
|
|
765
|
-
};
|
|
766
|
-
let left = Infinity;
|
|
767
|
-
let right = -Infinity;
|
|
768
|
-
const whiteWidth = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
769
|
-
for (const note of notes) {
|
|
770
|
-
const noteLeft = rawNotePosition(note, layout);
|
|
771
|
-
const width = isBlackKey(note) ? blackKeyWidth(layout) : whiteWidth;
|
|
772
|
-
left = Math.min(left, noteLeft);
|
|
773
|
-
right = Math.max(right, noteLeft + width);
|
|
774
|
-
}
|
|
775
|
-
return {
|
|
776
|
-
left,
|
|
777
|
-
right
|
|
778
|
-
};
|
|
779
|
-
}
|
|
780
|
-
/** Width of the whole keyboard in pixels. */
|
|
781
|
-
function pianoWidth(layout) {
|
|
782
|
-
const { left, right } = pianoBounds(layout);
|
|
783
|
-
return right - left;
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Offset of the left edge of a key from the left edge of the keyboard, in
|
|
787
|
-
* pixels.
|
|
788
|
-
*
|
|
789
|
-
* Notes outside `noteRange` are placed too, so the value is negative below
|
|
790
|
-
* `noteRange.first`.
|
|
791
|
-
*/
|
|
792
|
-
function notePosition(note, layout) {
|
|
793
|
-
return rawNotePosition(note, layout) - pianoBounds(layout).left;
|
|
794
|
-
}
|
|
795
|
-
/**
|
|
796
|
-
* The note drawn at a point, or null where there is none.
|
|
797
|
-
*
|
|
798
|
-
* Black keys are tested first, so they win where they overlap a white one. A
|
|
799
|
-
* white key covers its gap as well as its width, so the whole width of the
|
|
800
|
-
* keyboard belongs to some key and a click cannot fall between two.
|
|
801
|
-
*
|
|
802
|
-
* @param x offset from the left edge of the keyboard, in pixels
|
|
803
|
-
* @param y offset from its top edge, in pixels
|
|
804
|
-
* @param height height of the keyboard, in pixels
|
|
805
|
-
*/
|
|
806
|
-
function noteAt(x, y, height, layout) {
|
|
807
|
-
if (x < 0 || x >= pianoWidth(layout) || y < 0 || y >= height) return null;
|
|
808
|
-
const notes = getNoteRangeArray(layout.noteRange);
|
|
809
|
-
if (y < height * (layout.blackKeyHeightRatio ?? DEFAULT_BLACK_KEY_HEIGHT_RATIO)) for (const note of notes) {
|
|
810
|
-
if (isWhiteKey(note)) continue;
|
|
811
|
-
const left = notePosition(note, layout);
|
|
812
|
-
if (left <= x && x < left + blackKeyWidth(layout)) return note;
|
|
813
|
-
}
|
|
814
|
-
const slot = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
815
|
-
for (const note of notes) {
|
|
816
|
-
if (isBlackKey(note)) continue;
|
|
817
|
-
const left = notePosition(note, layout);
|
|
818
|
-
if (left <= x && x < left + slot) return note;
|
|
819
|
-
}
|
|
820
|
-
return null;
|
|
821
|
-
}
|
|
822
|
-
//#endregion
|
|
823
967
|
//#region src/pointer/drag.ts
|
|
824
968
|
/**
|
|
825
969
|
* Applied to the element for the lifetime of the instance.
|
|
@@ -1056,74 +1200,67 @@ function createDrag(element, options = {}) {
|
|
|
1056
1200
|
};
|
|
1057
1201
|
}
|
|
1058
1202
|
//#endregion
|
|
1059
|
-
//#region src/
|
|
1060
|
-
const DEFAULT_SOURCE = "api";
|
|
1203
|
+
//#region src/number-input/stepper-drag.ts
|
|
1061
1204
|
/**
|
|
1062
|
-
*
|
|
1063
|
-
*
|
|
1064
|
-
* Every way of playing a note goes through the one instance — pointers here,
|
|
1065
|
-
* keyboard shortcuts and MIDI through {@link PianoInputInstance.noteOn} — so
|
|
1066
|
-
* there is a single answer to what is currently held, and drawing the keys is
|
|
1067
|
-
* left entirely to the wrapper.
|
|
1205
|
+
* Drag up and down on the steppers of a number input to move its value, one
|
|
1206
|
+
* `step` every `pixels` — up raises it, as on a knob.
|
|
1068
1207
|
*
|
|
1069
|
-
*
|
|
1208
|
+
* Counted from where the drag started moving rather than added up per
|
|
1209
|
+
* event, so rounding cannot accumulate. The start is taken on the first
|
|
1210
|
+
* move, not on pointerdown: a stepper button acts on pointerdown, so by then
|
|
1211
|
+
* the value may already have been nudged once, and the drag carries on from
|
|
1212
|
+
* there.
|
|
1070
1213
|
*/
|
|
1071
|
-
function
|
|
1214
|
+
function createStepperDrag(element, options) {
|
|
1072
1215
|
let opts = options;
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
/**
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
if (note > (opts.midiMax ?? 127)) return;
|
|
1083
|
-
const sources = held.get(note);
|
|
1084
|
-
if (sources) {
|
|
1085
|
-
sources.add(source);
|
|
1086
|
-
return;
|
|
1087
|
-
}
|
|
1088
|
-
held.set(note, /* @__PURE__ */ new Set([source]));
|
|
1089
|
-
opts.onPlayNote?.(note, velocity);
|
|
1090
|
-
opts.onActiveNotesChange?.(activeNotes());
|
|
1091
|
-
}
|
|
1092
|
-
function noteOff(note, { source = DEFAULT_SOURCE } = {}) {
|
|
1093
|
-
if (!Number.isInteger(note) || note < 0 || note > 127) throw new RangeError("note: requirements: an integer from 0 to 127");
|
|
1094
|
-
const sources = held.get(note);
|
|
1095
|
-
if (!sources) return;
|
|
1096
|
-
sources.delete(source);
|
|
1097
|
-
if (sources.size > 0) return;
|
|
1098
|
-
held.delete(note);
|
|
1099
|
-
opts.onStopNote?.(note);
|
|
1100
|
-
opts.onActiveNotesChange?.(activeNotes());
|
|
1101
|
-
}
|
|
1102
|
-
/** The note under a pointer, or null where the pointer is off the keys. */
|
|
1103
|
-
function noteUnder(clientX, clientY) {
|
|
1104
|
-
const { left, top, height } = element.getBoundingClientRect();
|
|
1105
|
-
return noteAt(clientX - left, clientY - top, height, opts.layout);
|
|
1106
|
-
}
|
|
1107
|
-
/** Move a pointer onto a note, releasing whatever it held before. */
|
|
1108
|
-
function movePointer(pointerId, note) {
|
|
1109
|
-
const source = `pointer:${pointerId}`;
|
|
1110
|
-
const previous = pointerNotes.get(pointerId);
|
|
1111
|
-
if (previous === note) return;
|
|
1112
|
-
if (previous !== void 0) noteOff(previous, { source });
|
|
1113
|
-
if (note === null || note > (opts.midiMax ?? 127)) pointerNotes.delete(pointerId);
|
|
1114
|
-
else {
|
|
1115
|
-
pointerNotes.set(pointerId, note);
|
|
1116
|
-
noteOn(note, { source });
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1216
|
+
let origin = null;
|
|
1217
|
+
let moved = false;
|
|
1218
|
+
/**
|
|
1219
|
+
* Which sensitivity the drag is counting at, and where the previous event
|
|
1220
|
+
* was — a key produces no pointer event of its own, so a change is only
|
|
1221
|
+
* seen on the next move and has to be dated back to the one before it.
|
|
1222
|
+
*/
|
|
1223
|
+
let factor = 1;
|
|
1224
|
+
let previousY = 0;
|
|
1119
1225
|
const drag = createDrag(element, {
|
|
1120
|
-
|
|
1121
|
-
|
|
1226
|
+
threshold: 1,
|
|
1227
|
+
cursor: "ns-resize",
|
|
1228
|
+
pointerLock: opts.pointerLock,
|
|
1229
|
+
onDragStart: (state) => {
|
|
1230
|
+
origin = null;
|
|
1231
|
+
moved = false;
|
|
1232
|
+
factor = selectModifier(opts.sensitivity ?? DEFAULT_DRAG_SENSITIVITY, state.event).value;
|
|
1233
|
+
},
|
|
1122
1234
|
onDrag: (state) => {
|
|
1123
|
-
|
|
1124
|
-
|
|
1235
|
+
const { y } = state;
|
|
1236
|
+
if (!origin) {
|
|
1237
|
+
origin = {
|
|
1238
|
+
y,
|
|
1239
|
+
value: opts.getValue()
|
|
1240
|
+
};
|
|
1241
|
+
previousY = y;
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
const sensitivity = opts.sensitivity ?? DEFAULT_DRAG_SENSITIVITY;
|
|
1245
|
+
const next = selectModifier(sensitivity, state.event).value;
|
|
1246
|
+
if (next !== factor) {
|
|
1247
|
+
origin = {
|
|
1248
|
+
y: previousY,
|
|
1249
|
+
value: opts.getValue()
|
|
1250
|
+
};
|
|
1251
|
+
factor = next;
|
|
1252
|
+
}
|
|
1253
|
+
previousY = y;
|
|
1254
|
+
const steps = Math.round(-(y - origin.y) / (opts.pixels ?? 1));
|
|
1255
|
+
if (steps === 0) return;
|
|
1256
|
+
moved = true;
|
|
1257
|
+
const step = opts.range.step ?? 1;
|
|
1258
|
+
const amounts = mapModifier(sensitivity, (f) => ["raw", step * f]);
|
|
1259
|
+
opts.onChange(applyDelta(origin.value, steps, amounts, opts.range, state.event));
|
|
1125
1260
|
},
|
|
1126
|
-
onDragEnd: (
|
|
1261
|
+
onDragEnd: () => {
|
|
1262
|
+
moved = false;
|
|
1263
|
+
}
|
|
1127
1264
|
});
|
|
1128
1265
|
return {
|
|
1129
1266
|
update: (next) => {
|
|
@@ -1131,8 +1268,535 @@ function createPianoInput(element, options) {
|
|
|
1131
1268
|
...opts,
|
|
1132
1269
|
...next
|
|
1133
1270
|
};
|
|
1134
|
-
|
|
1135
|
-
|
|
1271
|
+
if ("pointerLock" in next) drag.update({ pointerLock: opts.pointerLock });
|
|
1272
|
+
},
|
|
1273
|
+
moved: () => moved,
|
|
1274
|
+
destroy: () => drag.destroy()
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
//#endregion
|
|
1278
|
+
//#region src/number-input/value.ts
|
|
1279
|
+
/**
|
|
1280
|
+
* The ranges a number input moves its value across, with the open ends
|
|
1281
|
+
* filled in.
|
|
1282
|
+
*
|
|
1283
|
+
* A normalized amount needs a finite span even when an end is unbounded or
|
|
1284
|
+
* clamping is off. Safe integers provide one without overflowing the span a
|
|
1285
|
+
* scale calculates. A raw amount needs no span, so its open ends can cover
|
|
1286
|
+
* every finite number instead of stopping at the safe-integer range.
|
|
1287
|
+
*/
|
|
1288
|
+
function numberInputRanges({ min, max, step, scale, clampValue = true }) {
|
|
1289
|
+
const lo = clampValue ? min : void 0;
|
|
1290
|
+
const hi = clampValue ? max : void 0;
|
|
1291
|
+
return {
|
|
1292
|
+
normalized: {
|
|
1293
|
+
min: lo ?? Number.MIN_SAFE_INTEGER,
|
|
1294
|
+
max: hi ?? Number.MAX_SAFE_INTEGER,
|
|
1295
|
+
step,
|
|
1296
|
+
scale
|
|
1297
|
+
},
|
|
1298
|
+
raw: {
|
|
1299
|
+
min: lo ?? -Number.MAX_VALUE,
|
|
1300
|
+
max: hi ?? Number.MAX_VALUE,
|
|
1301
|
+
step,
|
|
1302
|
+
scale
|
|
1303
|
+
}
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
/**
|
|
1307
|
+
* Move a number input's value by one press of a key, a wheel notch or a
|
|
1308
|
+
* stepper, picking the range that suits the kind of amount. See `applyDelta`.
|
|
1309
|
+
*/
|
|
1310
|
+
function nudgeNumberInput(value, direction, options, ranges, modifiers) {
|
|
1311
|
+
const [mode] = selectModifier(options, modifiers).value;
|
|
1312
|
+
return applyDelta(value, direction, options, mode === "raw" ? ranges.raw : ranges.normalized, modifiers);
|
|
1313
|
+
}
|
|
1314
|
+
/**
|
|
1315
|
+
* Where the value stands against the ends: whether it can go no further
|
|
1316
|
+
* down or up, and whether it lies outside them — which only an unclamped
|
|
1317
|
+
* input, or a value set from outside, can do.
|
|
1318
|
+
*/
|
|
1319
|
+
function numberInputBounds(value, { min, max, clampValue = true }) {
|
|
1320
|
+
return {
|
|
1321
|
+
atMin: clampValue && min !== void 0 && value <= min,
|
|
1322
|
+
atMax: clampValue && max !== void 0 && value >= max,
|
|
1323
|
+
outOfRange: min !== void 0 && value < min || max !== void 0 && value > max
|
|
1324
|
+
};
|
|
1325
|
+
}
|
|
1326
|
+
/**
|
|
1327
|
+
* The value typed text commits to, or `null` when there is no number in it.
|
|
1328
|
+
*
|
|
1329
|
+
* Text with no number is not a value: the input should go back to what it
|
|
1330
|
+
* was showing rather than commit a zero the user never typed. What is read
|
|
1331
|
+
* is clamped here and not while typing, since clamping as the user types
|
|
1332
|
+
* would make "1500" impossible to enter into an input whose max is 100.
|
|
1333
|
+
*/
|
|
1334
|
+
function commitNumberInputText(text, parse, { min, max, clampValue = true }) {
|
|
1335
|
+
const parsed = parse(text);
|
|
1336
|
+
if (!Number.isFinite(parsed)) return null;
|
|
1337
|
+
let committed = parsed;
|
|
1338
|
+
if (clampValue && min !== void 0) committed = Math.max(committed, min);
|
|
1339
|
+
if (clampValue && max !== void 0) committed = Math.min(committed, max);
|
|
1340
|
+
return committed;
|
|
1341
|
+
}
|
|
1342
|
+
//#endregion
|
|
1343
|
+
//#region src/number-input/text.ts
|
|
1344
|
+
/**
|
|
1345
|
+
* Reading a number out of the text of a number input, and keeping the caret
|
|
1346
|
+
* in place while the number under it changes.
|
|
1347
|
+
*
|
|
1348
|
+
* The text is whatever `format` made of the value — `"440 Hz"`, `"-6.0 dB"` —
|
|
1349
|
+
* or a half-typed entry, so none of this assumes the text is a number alone.
|
|
1350
|
+
*/
|
|
1351
|
+
/** The signs a number can carry. U+2212 is what some `Intl.NumberFormat` locales write. */
|
|
1352
|
+
const SIGNS = "+-−";
|
|
1353
|
+
/**
|
|
1354
|
+
* Where the number is in the text: from its first digit to its last, with the
|
|
1355
|
+
* sign and the decimal point in front of it. Whatever is left on either side
|
|
1356
|
+
* is taken for the unit, so it does not matter whether a space separates them,
|
|
1357
|
+
* or what the number looks like — `+6.0`, `1e+21`, `1,000` and `1:30` are each
|
|
1358
|
+
* one number. `null` when the text has no digit at all.
|
|
1359
|
+
*/
|
|
1360
|
+
function numberSpan(text) {
|
|
1361
|
+
const first = text.search(/\d/);
|
|
1362
|
+
if (first === -1) return null;
|
|
1363
|
+
let start = first;
|
|
1364
|
+
if (text[start - 1] === ".") start -= 1;
|
|
1365
|
+
if (start > 0 && SIGNS.includes(text[start - 1])) start -= 1;
|
|
1366
|
+
return {
|
|
1367
|
+
start,
|
|
1368
|
+
end: text.search(/\d\D*$/) + 1
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
/** A plain number, and nothing else: no grouping, no other separators. */
|
|
1372
|
+
const PLAIN_NUMBER = /^[+\-\u2212]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+\-\u2212]?\d+)?$/;
|
|
1373
|
+
/**
|
|
1374
|
+
* The number in the text, with the unit after it ignored: the default `parse`
|
|
1375
|
+
* of a number input.
|
|
1376
|
+
*
|
|
1377
|
+
* `NaN`, which leaves the value alone, whenever the number cannot be read
|
|
1378
|
+
* safely, rather than a part of it: text with no number, a number that is not
|
|
1379
|
+
* a plain one (`1,000` would otherwise read as 1, and `1:30` as 1), and text
|
|
1380
|
+
* with something in front of the number, which can change what it means (the
|
|
1381
|
+
* `L` of a pan reading `L 30`). A `format` that writes any of those needs its
|
|
1382
|
+
* own `parse`.
|
|
1383
|
+
*/
|
|
1384
|
+
function parseNumberText(text) {
|
|
1385
|
+
const span = numberSpan(text);
|
|
1386
|
+
if (!span || text.slice(0, span.start).trim() !== "") return NaN;
|
|
1387
|
+
const number = text.slice(span.start, span.end);
|
|
1388
|
+
return PLAIN_NUMBER.test(number) ? Number(number.replace(/\u2212/g, "-")) : NaN;
|
|
1389
|
+
}
|
|
1390
|
+
/**
|
|
1391
|
+
* The index the caret is measured against: the decimal point, or where one
|
|
1392
|
+
* would go if the number has none — in front of an exponent, if there is one.
|
|
1393
|
+
*
|
|
1394
|
+
* Measuring from an end instead would slide the caret across a digit whenever
|
|
1395
|
+
* the number changed length — `9.9` to `10.0` gains a character in front, `10`
|
|
1396
|
+
* to `9` loses one, and so does `9e+9` to `1e+10` behind — which is exactly
|
|
1397
|
+
* what stepping does.
|
|
1398
|
+
*/
|
|
1399
|
+
function decimalAnchor(text, span) {
|
|
1400
|
+
const number = text.slice(span.start, span.end);
|
|
1401
|
+
const exponent = number.search(/[eE][+\-\u2212]?\d/);
|
|
1402
|
+
const mantissa = exponent === -1 ? number : number.slice(0, exponent);
|
|
1403
|
+
const dot = mantissa.indexOf(".");
|
|
1404
|
+
return span.start + (dot === -1 ? mantissa.length : dot);
|
|
1405
|
+
}
|
|
1406
|
+
const NO_NUMBER = {
|
|
1407
|
+
start: 0,
|
|
1408
|
+
end: 0
|
|
1409
|
+
};
|
|
1410
|
+
/**
|
|
1411
|
+
* Where the caret is, relative to the decimal point, so that it can be put
|
|
1412
|
+
* back at the same digit once the value has changed. See
|
|
1413
|
+
* {@link caretAtDecimalOffset}.
|
|
1414
|
+
*/
|
|
1415
|
+
function caretDecimalOffset(text, caret) {
|
|
1416
|
+
return caret - decimalAnchor(text, numberSpan(text) ?? NO_NUMBER);
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* The caret position `offset` characters from the decimal point of the new
|
|
1420
|
+
* text, kept within the number.
|
|
1421
|
+
*
|
|
1422
|
+
* @example
|
|
1423
|
+
* // The caret sits in front of the point of "9.9" when ArrowUp turns it
|
|
1424
|
+
* // into "10.0"
|
|
1425
|
+
* const offset = caretDecimalOffset('9.9', 1) // 0
|
|
1426
|
+
* caretAtDecimalOffset('10.0', offset) // 2: still in front of the point
|
|
1427
|
+
*/
|
|
1428
|
+
function caretAtDecimalOffset(text, offset) {
|
|
1429
|
+
const span = numberSpan(text) ?? NO_NUMBER;
|
|
1430
|
+
const place = decimalAnchor(text, span) + offset;
|
|
1431
|
+
return Math.max(span.start, Math.min(place, span.end));
|
|
1432
|
+
}
|
|
1433
|
+
//#endregion
|
|
1434
|
+
//#region src/options/replace.ts
|
|
1435
|
+
/**
|
|
1436
|
+
* The `update()` argument that leaves an instance with exactly `next` as its
|
|
1437
|
+
* options.
|
|
1438
|
+
*
|
|
1439
|
+
* `update()` merges into what the instance has, which suits a wrapper that
|
|
1440
|
+
* pushes one setting at a time. A wrapper handed the whole new set instead —
|
|
1441
|
+
* a Svelte action, a Vue composable — has to clear what the new set no longer
|
|
1442
|
+
* carries, or a handler taken away, or an option left to its default, keeps
|
|
1443
|
+
* working.
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* instance.update(replaceOptions(previous, next))
|
|
1447
|
+
*/
|
|
1448
|
+
function replaceOptions(previous, next) {
|
|
1449
|
+
return {
|
|
1450
|
+
...Object.fromEntries(Object.keys(previous ?? {}).map((key) => [key, void 0])),
|
|
1451
|
+
...next
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
//#endregion
|
|
1455
|
+
//#region src/piano/layout.ts
|
|
1456
|
+
/**
|
|
1457
|
+
* `[noteRange.first, noteRange.first + 1, ..., noteRange.last]`
|
|
1458
|
+
*/
|
|
1459
|
+
function getNoteRangeArray(noteRange) {
|
|
1460
|
+
return Array.from({ length: noteRange.last - noteRange.first + 1 }, (_, i) => i + noteRange.first);
|
|
1461
|
+
}
|
|
1462
|
+
const DEFAULT_KEY_GAP = 1;
|
|
1463
|
+
const DEFAULT_BLACK_KEY_WIDTH_RATIO = .65;
|
|
1464
|
+
const DEFAULT_BLACK_KEY_HEIGHT_RATIO = .6;
|
|
1465
|
+
/**
|
|
1466
|
+
* How many white keys sit at or before each pitch class, counting from C.
|
|
1467
|
+
*
|
|
1468
|
+
* A black key shares the number of the white key to its left plus one, which
|
|
1469
|
+
* puts it on the boundary between the two; {@link notePosition} then shifts it
|
|
1470
|
+
* back by half its width to centre it there.
|
|
1471
|
+
*/
|
|
1472
|
+
const whiteKeysBefore = {
|
|
1473
|
+
C: 0,
|
|
1474
|
+
"C#": 1,
|
|
1475
|
+
D: 1,
|
|
1476
|
+
"D#": 2,
|
|
1477
|
+
E: 2,
|
|
1478
|
+
F: 3,
|
|
1479
|
+
"F#": 4,
|
|
1480
|
+
G: 4,
|
|
1481
|
+
"G#": 5,
|
|
1482
|
+
A: 5,
|
|
1483
|
+
"A#": 6,
|
|
1484
|
+
B: 6
|
|
1485
|
+
};
|
|
1486
|
+
/** Width of a black key in pixels. */
|
|
1487
|
+
function blackKeyWidth(layout) {
|
|
1488
|
+
return layout.whiteKeyWidth * (layout.blackKeyWidthRatio ?? DEFAULT_BLACK_KEY_WIDTH_RATIO);
|
|
1489
|
+
}
|
|
1490
|
+
function rawNotePosition(note, layout) {
|
|
1491
|
+
const slot = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
1492
|
+
const target = noteKey(note);
|
|
1493
|
+
const first = noteKey(layout.noteRange.first);
|
|
1494
|
+
const octave = Math.floor((note - layout.noteRange.first) / 12);
|
|
1495
|
+
const octaveOffset = noteKeys.indexOf(first) > noteKeys.indexOf(target) ? 1 : 0;
|
|
1496
|
+
const whiteKeysIn = whiteKeysBefore[target] - whiteKeysBefore[first] + (octave + octaveOffset) * 7;
|
|
1497
|
+
return isBlackKey(note) ? whiteKeysIn * slot - blackKeyWidth(layout) / 2 : whiteKeysIn * slot;
|
|
1498
|
+
}
|
|
1499
|
+
function pianoBounds(layout) {
|
|
1500
|
+
const notes = getNoteRangeArray(layout.noteRange);
|
|
1501
|
+
if (notes.length === 0) return {
|
|
1502
|
+
left: 0,
|
|
1503
|
+
right: 0
|
|
1504
|
+
};
|
|
1505
|
+
let left = Infinity;
|
|
1506
|
+
let right = -Infinity;
|
|
1507
|
+
const whiteWidth = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
1508
|
+
for (const note of notes) {
|
|
1509
|
+
const noteLeft = rawNotePosition(note, layout);
|
|
1510
|
+
const width = isBlackKey(note) ? blackKeyWidth(layout) : whiteWidth;
|
|
1511
|
+
left = Math.min(left, noteLeft);
|
|
1512
|
+
right = Math.max(right, noteLeft + width);
|
|
1513
|
+
}
|
|
1514
|
+
return {
|
|
1515
|
+
left,
|
|
1516
|
+
right
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1519
|
+
/** Width of the whole keyboard in pixels. */
|
|
1520
|
+
function pianoWidth(layout) {
|
|
1521
|
+
const { left, right } = pianoBounds(layout);
|
|
1522
|
+
return right - left;
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* Offset of the left edge of a key from the left edge of the keyboard, in
|
|
1526
|
+
* pixels.
|
|
1527
|
+
*
|
|
1528
|
+
* Notes outside `noteRange` are placed too, so the value is negative below
|
|
1529
|
+
* `noteRange.first`.
|
|
1530
|
+
*/
|
|
1531
|
+
function notePosition(note, layout) {
|
|
1532
|
+
return rawNotePosition(note, layout) - pianoBounds(layout).left;
|
|
1533
|
+
}
|
|
1534
|
+
/**
|
|
1535
|
+
* The note drawn at a point, or null where there is none.
|
|
1536
|
+
*
|
|
1537
|
+
* Black keys are tested first, so they win where they overlap a white one. A
|
|
1538
|
+
* white key covers its gap as well as its width, so the whole width of the
|
|
1539
|
+
* keyboard belongs to some key and a click cannot fall between two.
|
|
1540
|
+
*
|
|
1541
|
+
* @param x offset from the left edge of the keyboard, in pixels
|
|
1542
|
+
* @param y offset from its top edge, in pixels
|
|
1543
|
+
* @param height height of the keyboard, in pixels
|
|
1544
|
+
*/
|
|
1545
|
+
function noteAt(x, y, height, layout) {
|
|
1546
|
+
if (x < 0 || x >= pianoWidth(layout) || y < 0 || y >= height) return null;
|
|
1547
|
+
const notes = getNoteRangeArray(layout.noteRange);
|
|
1548
|
+
if (y < height * (layout.blackKeyHeightRatio ?? DEFAULT_BLACK_KEY_HEIGHT_RATIO)) for (const note of notes) {
|
|
1549
|
+
if (isWhiteKey(note)) continue;
|
|
1550
|
+
const left = notePosition(note, layout);
|
|
1551
|
+
if (left <= x && x < left + blackKeyWidth(layout)) return note;
|
|
1552
|
+
}
|
|
1553
|
+
const slot = layout.whiteKeyWidth + (layout.keyGap ?? DEFAULT_KEY_GAP);
|
|
1554
|
+
for (const note of notes) {
|
|
1555
|
+
if (isBlackKey(note)) continue;
|
|
1556
|
+
const left = notePosition(note, layout);
|
|
1557
|
+
if (left <= x && x < left + slot) return note;
|
|
1558
|
+
}
|
|
1559
|
+
return null;
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* The white key width that makes the keyboard exactly `width` wide, for a
|
|
1563
|
+
* keyboard that follows the size of its container.
|
|
1564
|
+
*
|
|
1565
|
+
* Solved from {@link pianoWidth} rather than by dividing among the white
|
|
1566
|
+
* keys, so that a range that starts or ends on a black key — which sticks out
|
|
1567
|
+
* by a fraction of a white key — still fills the container. The width grows
|
|
1568
|
+
* linearly with the white key width, so two samples pin it down.
|
|
1569
|
+
*/
|
|
1570
|
+
function fitWhiteKeyWidth(width, layout) {
|
|
1571
|
+
const at = (whiteKeyWidth) => pianoWidth({
|
|
1572
|
+
...layout,
|
|
1573
|
+
whiteKeyWidth
|
|
1574
|
+
});
|
|
1575
|
+
const slope = at(2) - at(1);
|
|
1576
|
+
if (slope <= 0) return 0;
|
|
1577
|
+
return (width - (at(1) - slope)) / slope;
|
|
1578
|
+
}
|
|
1579
|
+
//#endregion
|
|
1580
|
+
//#region src/piano/shortcuts.ts
|
|
1581
|
+
/**
|
|
1582
|
+
* Ready-made keyboard layouts. Both assume `noteRange.first` is a C.
|
|
1583
|
+
*/
|
|
1584
|
+
const SHORTCUTS = {
|
|
1585
|
+
/** Every semitone from C, over the two rows of a QWERTY keyboard. */
|
|
1586
|
+
HOME_ROW: { keys: [
|
|
1587
|
+
"a",
|
|
1588
|
+
"w",
|
|
1589
|
+
"s",
|
|
1590
|
+
"e",
|
|
1591
|
+
"d",
|
|
1592
|
+
"f",
|
|
1593
|
+
"t",
|
|
1594
|
+
"g",
|
|
1595
|
+
"y",
|
|
1596
|
+
"h",
|
|
1597
|
+
"u",
|
|
1598
|
+
"j",
|
|
1599
|
+
"k",
|
|
1600
|
+
"o",
|
|
1601
|
+
"l",
|
|
1602
|
+
"p",
|
|
1603
|
+
";"
|
|
1604
|
+
] },
|
|
1605
|
+
/** The white keys only, on the home row. Black keys have no shortcut. */
|
|
1606
|
+
HOME_ROW_NATURAL: { keys: [
|
|
1607
|
+
"a",
|
|
1608
|
+
"",
|
|
1609
|
+
"s",
|
|
1610
|
+
"",
|
|
1611
|
+
"d",
|
|
1612
|
+
"f",
|
|
1613
|
+
"",
|
|
1614
|
+
"g",
|
|
1615
|
+
"",
|
|
1616
|
+
"h",
|
|
1617
|
+
"",
|
|
1618
|
+
"j",
|
|
1619
|
+
"k",
|
|
1620
|
+
"",
|
|
1621
|
+
"l",
|
|
1622
|
+
"",
|
|
1623
|
+
";"
|
|
1624
|
+
] }
|
|
1625
|
+
};
|
|
1626
|
+
/**
|
|
1627
|
+
* Whether a key press was typed into something: shortcuts must not play a
|
|
1628
|
+
* note for a letter that is going into a text field.
|
|
1629
|
+
*/
|
|
1630
|
+
function isEditableTarget(target) {
|
|
1631
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
1632
|
+
if (target.matches("input, textarea, select")) return true;
|
|
1633
|
+
for (let element = target; element;) {
|
|
1634
|
+
const contentEditable = element.getAttribute("contenteditable");
|
|
1635
|
+
if (contentEditable !== null) return contentEditable.toLowerCase() !== "false";
|
|
1636
|
+
element = element.parentElement;
|
|
1637
|
+
}
|
|
1638
|
+
return false;
|
|
1639
|
+
}
|
|
1640
|
+
//#endregion
|
|
1641
|
+
//#region src/piano/index.ts
|
|
1642
|
+
const DEFAULT_SOURCE = "api";
|
|
1643
|
+
/**
|
|
1644
|
+
* Drive a piano keyboard with pointers, and own what is sounding.
|
|
1645
|
+
*
|
|
1646
|
+
* Every way of playing a note goes through the one instance — pointers here,
|
|
1647
|
+
* keyboard shortcuts and MIDI through {@link PianoInputInstance.noteOn} — so
|
|
1648
|
+
* there is a single answer to what is currently held, and drawing the keys is
|
|
1649
|
+
* left entirely to the wrapper.
|
|
1650
|
+
*
|
|
1651
|
+
* Tracks every pointer at once, so a chord can be played with several fingers.
|
|
1652
|
+
*/
|
|
1653
|
+
function createPianoInput(element, options) {
|
|
1654
|
+
let opts = options;
|
|
1655
|
+
/** Which sources are holding each sounding note. */
|
|
1656
|
+
const held = /* @__PURE__ */ new Map();
|
|
1657
|
+
/** The note each pointer is currently on. */
|
|
1658
|
+
const pointerNotes = /* @__PURE__ */ new Map();
|
|
1659
|
+
/**
|
|
1660
|
+
* The note each held shortcut key is playing, by `code` — the physical key,
|
|
1661
|
+
* so that releasing it with a different modifier or layout still matches.
|
|
1662
|
+
*/
|
|
1663
|
+
const shortcutNotes = /* @__PURE__ */ new Map();
|
|
1664
|
+
function activeNotes() {
|
|
1665
|
+
return [...held.keys()].sort((a, b) => a - b);
|
|
1666
|
+
}
|
|
1667
|
+
function noteOn(note, { source = DEFAULT_SOURCE, velocity } = {}) {
|
|
1668
|
+
if (!Number.isInteger(note) || note < 0 || note > 127) throw new RangeError("note: requirements: an integer from 0 to 127");
|
|
1669
|
+
if (note > (opts.midiMax ?? 127)) return;
|
|
1670
|
+
const sources = held.get(note);
|
|
1671
|
+
if (sources) {
|
|
1672
|
+
sources.add(source);
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
held.set(note, /* @__PURE__ */ new Set([source]));
|
|
1676
|
+
opts.onPlayNote?.(note, velocity);
|
|
1677
|
+
opts.onActiveNotesChange?.(activeNotes());
|
|
1678
|
+
}
|
|
1679
|
+
function noteOff(note, { source = DEFAULT_SOURCE } = {}) {
|
|
1680
|
+
if (!Number.isInteger(note) || note < 0 || note > 127) throw new RangeError("note: requirements: an integer from 0 to 127");
|
|
1681
|
+
const sources = held.get(note);
|
|
1682
|
+
if (!sources) return;
|
|
1683
|
+
sources.delete(source);
|
|
1684
|
+
if (sources.size > 0) return;
|
|
1685
|
+
held.delete(note);
|
|
1686
|
+
opts.onStopNote?.(note);
|
|
1687
|
+
opts.onActiveNotesChange?.(activeNotes());
|
|
1688
|
+
}
|
|
1689
|
+
/** The note under a pointer, or null where the pointer is off the keys. */
|
|
1690
|
+
function noteUnder(clientX, clientY) {
|
|
1691
|
+
const { left, top, height } = element.getBoundingClientRect();
|
|
1692
|
+
return noteAt(clientX - left, clientY - top, height, opts.layout);
|
|
1693
|
+
}
|
|
1694
|
+
/** Move a pointer onto a note, releasing whatever it held before. */
|
|
1695
|
+
function movePointer(pointerId, note) {
|
|
1696
|
+
const source = `pointer:${pointerId}`;
|
|
1697
|
+
const previous = pointerNotes.get(pointerId);
|
|
1698
|
+
if (previous === note) return;
|
|
1699
|
+
if (previous !== void 0) noteOff(previous, { source });
|
|
1700
|
+
if (note === null || note > (opts.midiMax ?? 127)) pointerNotes.delete(pointerId);
|
|
1701
|
+
else {
|
|
1702
|
+
pointerNotes.set(pointerId, note);
|
|
1703
|
+
noteOn(note, { source });
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
/** The note a shortcut key plays, or null when it has none. */
|
|
1707
|
+
function shortcutNote(key) {
|
|
1708
|
+
const keys = opts.keyboardShortcuts?.keys;
|
|
1709
|
+
if (!keys || key === "") return null;
|
|
1710
|
+
const { first, last } = opts.layout.noteRange;
|
|
1711
|
+
const index = keys.indexOf(key);
|
|
1712
|
+
const note = first + index;
|
|
1713
|
+
return index === -1 || note > last ? null : note;
|
|
1714
|
+
}
|
|
1715
|
+
function onKeyDown(event) {
|
|
1716
|
+
const id = event.code || event.key;
|
|
1717
|
+
if (event.repeat || shortcutNotes.has(id)) return;
|
|
1718
|
+
if (isEditableTarget(event.target)) return;
|
|
1719
|
+
const note = shortcutNote(event.key);
|
|
1720
|
+
if (note === null) return;
|
|
1721
|
+
shortcutNotes.set(id, note);
|
|
1722
|
+
noteOn(note, { source: `keyboard:${id}` });
|
|
1723
|
+
}
|
|
1724
|
+
function onKeyUp(event) {
|
|
1725
|
+
const id = event.code || event.key;
|
|
1726
|
+
const note = shortcutNotes.get(id);
|
|
1727
|
+
if (note === void 0) return;
|
|
1728
|
+
shortcutNotes.delete(id);
|
|
1729
|
+
noteOff(note, { source: `keyboard:${id}` });
|
|
1730
|
+
}
|
|
1731
|
+
function releaseShortcuts() {
|
|
1732
|
+
for (const [id, note] of shortcutNotes) noteOff(note, { source: `keyboard:${id}` });
|
|
1733
|
+
shortcutNotes.clear();
|
|
1734
|
+
}
|
|
1735
|
+
function onFocusOut(event) {
|
|
1736
|
+
const next = event.relatedTarget;
|
|
1737
|
+
if (next instanceof Node && element.contains(next)) return;
|
|
1738
|
+
releaseShortcuts();
|
|
1739
|
+
}
|
|
1740
|
+
/** The scope the listeners are bound for, or null when none are. */
|
|
1741
|
+
let boundScope = null;
|
|
1742
|
+
function bindShortcuts() {
|
|
1743
|
+
const scope = opts.keyboardShortcuts ? opts.keyboardShortcutsScope ?? "root" : null;
|
|
1744
|
+
if (scope === boundScope) return;
|
|
1745
|
+
unbindShortcuts();
|
|
1746
|
+
const win = globalThis.window;
|
|
1747
|
+
if (!scope || !win) return;
|
|
1748
|
+
const target = scope === "window" ? win : element;
|
|
1749
|
+
target.addEventListener("keydown", onKeyDown);
|
|
1750
|
+
target.addEventListener("keyup", onKeyUp);
|
|
1751
|
+
if (scope === "root") element.addEventListener("focusout", onFocusOut);
|
|
1752
|
+
win.addEventListener("blur", releaseShortcuts);
|
|
1753
|
+
boundScope = scope;
|
|
1754
|
+
}
|
|
1755
|
+
function unbindShortcuts() {
|
|
1756
|
+
const win = globalThis.window;
|
|
1757
|
+
if (!boundScope || !win) return;
|
|
1758
|
+
const target = boundScope === "window" ? win : element;
|
|
1759
|
+
target.removeEventListener("keydown", onKeyDown);
|
|
1760
|
+
target.removeEventListener("keyup", onKeyUp);
|
|
1761
|
+
element.removeEventListener("focusout", onFocusOut);
|
|
1762
|
+
win.removeEventListener("blur", releaseShortcuts);
|
|
1763
|
+
boundScope = null;
|
|
1764
|
+
}
|
|
1765
|
+
/**
|
|
1766
|
+
* What the held keys were started against. Compared by value: a caller
|
|
1767
|
+
* who writes the keys inline hands over a new array on every update, and
|
|
1768
|
+
* releasing on that would stop a note as soon as anything re-renders.
|
|
1769
|
+
*/
|
|
1770
|
+
function shortcutMapping() {
|
|
1771
|
+
const { first, last } = opts.layout.noteRange;
|
|
1772
|
+
return [
|
|
1773
|
+
opts.keyboardShortcuts?.keys.join("\0") ?? "",
|
|
1774
|
+
opts.keyboardShortcutsScope ?? "root",
|
|
1775
|
+
first,
|
|
1776
|
+
last
|
|
1777
|
+
].join("");
|
|
1778
|
+
}
|
|
1779
|
+
bindShortcuts();
|
|
1780
|
+
const drag = createDrag(element, {
|
|
1781
|
+
multiPointer: true,
|
|
1782
|
+
onDragStart: (state) => movePointer(state.pointerId, noteUnder(state.clientX, state.clientY)),
|
|
1783
|
+
onDrag: (state) => {
|
|
1784
|
+
if (opts.glissando === false) return;
|
|
1785
|
+
movePointer(state.pointerId, noteUnder(state.clientX, state.clientY));
|
|
1786
|
+
},
|
|
1787
|
+
onDragEnd: (state) => movePointer(state.pointerId, null)
|
|
1788
|
+
});
|
|
1789
|
+
return {
|
|
1790
|
+
update: (next) => {
|
|
1791
|
+
const mapping = shortcutMapping();
|
|
1792
|
+
opts = {
|
|
1793
|
+
...opts,
|
|
1794
|
+
...next
|
|
1795
|
+
};
|
|
1796
|
+
if (shortcutMapping() !== mapping) releaseShortcuts();
|
|
1797
|
+
bindShortcuts();
|
|
1798
|
+
const midiMax = opts.midiMax ?? 127;
|
|
1799
|
+
const stoppedNotes = activeNotes().filter((note) => note > midiMax);
|
|
1136
1800
|
if (stoppedNotes.length === 0) return;
|
|
1137
1801
|
for (const note of stoppedNotes) {
|
|
1138
1802
|
held.delete(note);
|
|
@@ -1146,6 +1810,8 @@ function createPianoInput(element, options) {
|
|
|
1146
1810
|
activeNotes,
|
|
1147
1811
|
destroy: () => {
|
|
1148
1812
|
drag.destroy();
|
|
1813
|
+
unbindShortcuts();
|
|
1814
|
+
shortcutNotes.clear();
|
|
1149
1815
|
const notes = activeNotes();
|
|
1150
1816
|
for (const note of notes) {
|
|
1151
1817
|
held.delete(note);
|
|
@@ -1157,6 +1823,267 @@ function createPianoInput(element, options) {
|
|
|
1157
1823
|
};
|
|
1158
1824
|
}
|
|
1159
1825
|
//#endregion
|
|
1826
|
+
//#region src/selection/box.ts
|
|
1827
|
+
function rectOf(from, to) {
|
|
1828
|
+
return {
|
|
1829
|
+
x: Math.min(from[0], to[0]),
|
|
1830
|
+
y: Math.min(from[1], to[1]),
|
|
1831
|
+
width: Math.abs(to[0] - from[0]),
|
|
1832
|
+
height: Math.abs(to[1] - from[1])
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1835
|
+
/** Whether the box covers the point, edges included. */
|
|
1836
|
+
function selectionBoxCovers(box, [x, y]) {
|
|
1837
|
+
return x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
1838
|
+
}
|
|
1839
|
+
/**
|
|
1840
|
+
* Selecting by dragging a box over a set of items.
|
|
1841
|
+
*
|
|
1842
|
+
* The drag itself is not here: the caller owns the pointer, and reports where
|
|
1843
|
+
* it went in the same 0..1 space the items are given in.
|
|
1844
|
+
*/
|
|
1845
|
+
function createSelectionBox(options) {
|
|
1846
|
+
let opts = options;
|
|
1847
|
+
let drag = null;
|
|
1848
|
+
let box = null;
|
|
1849
|
+
function setBox(next) {
|
|
1850
|
+
box = next;
|
|
1851
|
+
opts.onBoxChange?.(next);
|
|
1852
|
+
}
|
|
1853
|
+
function apply(rect, base) {
|
|
1854
|
+
const inside = [];
|
|
1855
|
+
for (const [id, at] of opts.items()) if (selectionBoxCovers(rect, at) && !base.includes(id)) inside.push(id);
|
|
1856
|
+
opts.onSelectionChange?.([...base, ...inside]);
|
|
1857
|
+
}
|
|
1858
|
+
return {
|
|
1859
|
+
update: (next) => {
|
|
1860
|
+
opts = {
|
|
1861
|
+
...opts,
|
|
1862
|
+
...next
|
|
1863
|
+
};
|
|
1864
|
+
},
|
|
1865
|
+
begin: (at, { additive = false, selection = [] } = {}) => {
|
|
1866
|
+
drag = {
|
|
1867
|
+
from: at,
|
|
1868
|
+
base: additive ? [...selection] : []
|
|
1869
|
+
};
|
|
1870
|
+
setBox(rectOf(at, at));
|
|
1871
|
+
if (!additive) opts.onSelectionChange?.([]);
|
|
1872
|
+
},
|
|
1873
|
+
move: (to) => {
|
|
1874
|
+
if (!drag) return;
|
|
1875
|
+
const rect = rectOf(drag.from, to);
|
|
1876
|
+
setBox(rect);
|
|
1877
|
+
apply(rect, drag.base);
|
|
1878
|
+
},
|
|
1879
|
+
end: () => {
|
|
1880
|
+
const running = drag !== null;
|
|
1881
|
+
drag = null;
|
|
1882
|
+
if (box !== null) setBox(null);
|
|
1883
|
+
return running;
|
|
1884
|
+
},
|
|
1885
|
+
box: () => box,
|
|
1886
|
+
destroy: () => {
|
|
1887
|
+
drag = null;
|
|
1888
|
+
box = null;
|
|
1889
|
+
}
|
|
1890
|
+
};
|
|
1891
|
+
}
|
|
1892
|
+
//#endregion
|
|
1893
|
+
//#region src/points-editor/index.ts
|
|
1894
|
+
/**
|
|
1895
|
+
* A point's value is its position in the editor, so its range is the editor:
|
|
1896
|
+
* no scaling, and no rounding to a step.
|
|
1897
|
+
*/
|
|
1898
|
+
const POINT_AXIS = {
|
|
1899
|
+
min: 0,
|
|
1900
|
+
max: 1
|
|
1901
|
+
};
|
|
1902
|
+
/**
|
|
1903
|
+
* How much one wheel notch moves a point by default: a hundredth of the
|
|
1904
|
+
* editor, whatever its pixel size.
|
|
1905
|
+
*/
|
|
1906
|
+
const POINTS_EDITOR_DEFAULT_WHEEL = ["normalized", .01];
|
|
1907
|
+
/**
|
|
1908
|
+
* How much one arrow key press moves a point by default. Shift is the
|
|
1909
|
+
* fine-adjustment key everywhere else, so it is bound here too — but only on
|
|
1910
|
+
* the keyboard. On the wheel it already means the x axis, and browsers hand
|
|
1911
|
+
* shift+wheel over as horizontal scrolling anyway.
|
|
1912
|
+
*/
|
|
1913
|
+
const POINTS_EDITOR_DEFAULT_KEYBOARD = {
|
|
1914
|
+
default: ["normalized", .01],
|
|
1915
|
+
shift: ["normalized", .001]
|
|
1916
|
+
};
|
|
1917
|
+
/** Keep a point within its range. An axis left out runs from 0 to 1. */
|
|
1918
|
+
function clampPoint(point, min, max) {
|
|
1919
|
+
return {
|
|
1920
|
+
x: clamp(point.x, min?.x ?? 0, max?.x ?? 1),
|
|
1921
|
+
y: clamp(point.y, min?.y ?? 0, max?.y ?? 1)
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
const EMPTY = [];
|
|
1925
|
+
/**
|
|
1926
|
+
* How far a group may move before something in it leaves its range.
|
|
1927
|
+
*
|
|
1928
|
+
* Clamping each point on its own would break the shape of the selection: the
|
|
1929
|
+
* one that reached the edge would stop while the rest carried on. One amount
|
|
1930
|
+
* for all of them means the whole selection stops together.
|
|
1931
|
+
*/
|
|
1932
|
+
function allowedDelta(delta, entries) {
|
|
1933
|
+
let loX = -Infinity;
|
|
1934
|
+
let hiX = Infinity;
|
|
1935
|
+
let loY = -Infinity;
|
|
1936
|
+
let hiY = Infinity;
|
|
1937
|
+
for (const { start, point } of entries) {
|
|
1938
|
+
if (point.readonly) continue;
|
|
1939
|
+
loX = Math.max(loX, (point.min?.x ?? 0) - start.x);
|
|
1940
|
+
hiX = Math.min(hiX, (point.max?.x ?? 1) - start.x);
|
|
1941
|
+
loY = Math.max(loY, (point.min?.y ?? 0) - start.y);
|
|
1942
|
+
hiY = Math.min(hiY, (point.max?.y ?? 1) - start.y);
|
|
1943
|
+
}
|
|
1944
|
+
return {
|
|
1945
|
+
x: hiX < loX ? 0 : clamp(delta.x, loX, hiX),
|
|
1946
|
+
y: hiY < loY ? 0 : clamp(delta.y, loY, hiY)
|
|
1947
|
+
};
|
|
1948
|
+
}
|
|
1949
|
+
/**
|
|
1950
|
+
* The selection and the moves of a points editor: which points a press or a
|
|
1951
|
+
* box selects, and how a selection moves as one.
|
|
1952
|
+
*
|
|
1953
|
+
* The points stay with the wrapper, which registers each one; the editor only
|
|
1954
|
+
* reads them when it needs to, so a point's value can change on every frame
|
|
1955
|
+
* of a drag without anything being re-registered.
|
|
1956
|
+
*/
|
|
1957
|
+
function createPointsEditor(options = {}) {
|
|
1958
|
+
let opts = options;
|
|
1959
|
+
const points = /* @__PURE__ */ new Map();
|
|
1960
|
+
/** What the current drag picked up, and where those points started. */
|
|
1961
|
+
let dragged = [];
|
|
1962
|
+
const selection = () => opts.selectable ? opts.selection ?? EMPTY : EMPTY;
|
|
1963
|
+
function changeSelection(next) {
|
|
1964
|
+
opts = {
|
|
1965
|
+
...opts,
|
|
1966
|
+
selection: next
|
|
1967
|
+
};
|
|
1968
|
+
opts.onSelectionChange?.(next);
|
|
1969
|
+
}
|
|
1970
|
+
function snapshot(ids) {
|
|
1971
|
+
return ids.flatMap((id) => {
|
|
1972
|
+
const point = points.get(id)?.();
|
|
1973
|
+
return point ? [{
|
|
1974
|
+
id,
|
|
1975
|
+
start: { ...point.value }
|
|
1976
|
+
}] : [];
|
|
1977
|
+
});
|
|
1978
|
+
}
|
|
1979
|
+
function moveFrom(entries, delta) {
|
|
1980
|
+
const withPoint = entries.flatMap((entry) => {
|
|
1981
|
+
const point = points.get(entry.id)?.();
|
|
1982
|
+
return point ? [{
|
|
1983
|
+
...entry,
|
|
1984
|
+
point
|
|
1985
|
+
}] : [];
|
|
1986
|
+
});
|
|
1987
|
+
const allowed = allowedDelta(delta, withPoint);
|
|
1988
|
+
for (const { start, point } of withPoint) {
|
|
1989
|
+
if (point.readonly) continue;
|
|
1990
|
+
point.onChange?.({
|
|
1991
|
+
x: toPrecision(start.x + allowed.x),
|
|
1992
|
+
y: toPrecision(start.y + allowed.y)
|
|
1993
|
+
});
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
function nudgeSelection(id, delta) {
|
|
1997
|
+
const current = selection();
|
|
1998
|
+
moveFrom(snapshot(current.includes(id) ? current : [id]), delta);
|
|
1999
|
+
}
|
|
2000
|
+
function nudgePoint(id, axis, direction, option, modifiers) {
|
|
2001
|
+
const point = points.get(id)?.();
|
|
2002
|
+
if (!point) return;
|
|
2003
|
+
const { value } = point;
|
|
2004
|
+
const next = applyDelta(value[axis], direction, option, POINT_AXIS, modifiers);
|
|
2005
|
+
nudgeSelection(id, {
|
|
2006
|
+
x: axis === "x" ? next - value.x : 0,
|
|
2007
|
+
y: axis === "y" ? next - value.y : 0
|
|
2008
|
+
});
|
|
2009
|
+
}
|
|
2010
|
+
const box = createSelectionBox({
|
|
2011
|
+
*items() {
|
|
2012
|
+
for (const [id, read] of points) {
|
|
2013
|
+
const { x, y } = read().value;
|
|
2014
|
+
yield [id, [x, y]];
|
|
2015
|
+
}
|
|
2016
|
+
},
|
|
2017
|
+
onBoxChange: (rect) => opts.onSelectionBoxChange?.(rect),
|
|
2018
|
+
onSelectionChange: changeSelection
|
|
2019
|
+
});
|
|
2020
|
+
return {
|
|
2021
|
+
update: (next) => {
|
|
2022
|
+
opts = {
|
|
2023
|
+
...opts,
|
|
2024
|
+
...next
|
|
2025
|
+
};
|
|
2026
|
+
},
|
|
2027
|
+
registerPoint: (id, read) => {
|
|
2028
|
+
points.set(id, read);
|
|
2029
|
+
return () => {
|
|
2030
|
+
if (points.get(id) === read) points.delete(id);
|
|
2031
|
+
};
|
|
2032
|
+
},
|
|
2033
|
+
isPointElement: (element) => {
|
|
2034
|
+
if (!element) return false;
|
|
2035
|
+
for (const read of points.values()) if (read().element?.contains(element)) return true;
|
|
2036
|
+
return false;
|
|
2037
|
+
},
|
|
2038
|
+
beginPointDrag: (id, modifiers) => {
|
|
2039
|
+
if (!opts.selectable) {
|
|
2040
|
+
dragged = snapshot([id]);
|
|
2041
|
+
return;
|
|
2042
|
+
}
|
|
2043
|
+
const current = selection();
|
|
2044
|
+
const additive = modifiers.ctrlKey || modifiers.metaKey;
|
|
2045
|
+
let next;
|
|
2046
|
+
if (additive) next = current.includes(id) ? current.filter((x) => x !== id) : [...current, id];
|
|
2047
|
+
else if (current.includes(id)) next = current;
|
|
2048
|
+
else next = [id];
|
|
2049
|
+
changeSelection([...next]);
|
|
2050
|
+
dragged = next.includes(id) ? snapshot(next) : [];
|
|
2051
|
+
},
|
|
2052
|
+
movePointDrag: (delta) => moveFrom(dragged, delta),
|
|
2053
|
+
nudgeSelection,
|
|
2054
|
+
nudgePoint,
|
|
2055
|
+
nudgeFocusedPoint: (axis, direction, modifiers) => {
|
|
2056
|
+
for (const [id, read] of points) {
|
|
2057
|
+
const { element, wheel, readonly, onChange } = read();
|
|
2058
|
+
const active = element?.ownerDocument.activeElement;
|
|
2059
|
+
if (!active || !element?.contains(active)) continue;
|
|
2060
|
+
if (!wheel || readonly || !onChange) return false;
|
|
2061
|
+
nudgePoint(id, axis, direction, wheel, modifiers);
|
|
2062
|
+
return true;
|
|
2063
|
+
}
|
|
2064
|
+
return false;
|
|
2065
|
+
},
|
|
2066
|
+
beginSelectionBox: (at, modifiers) => {
|
|
2067
|
+
if (!opts.selectable) return;
|
|
2068
|
+
box.begin([at.x, at.y], {
|
|
2069
|
+
additive: modifiers.ctrlKey || modifiers.metaKey,
|
|
2070
|
+
selection: selection()
|
|
2071
|
+
});
|
|
2072
|
+
},
|
|
2073
|
+
moveSelectionBox: (to) => box.move([to.x, to.y]),
|
|
2074
|
+
endSelectionBox: () => {
|
|
2075
|
+
if (!box.end()) return;
|
|
2076
|
+
const [first] = selection();
|
|
2077
|
+
const element = first ? points.get(first)?.().element : null;
|
|
2078
|
+
if (element && "focus" in element) element.focus();
|
|
2079
|
+
},
|
|
2080
|
+
destroy: () => {
|
|
2081
|
+
box.destroy();
|
|
2082
|
+
dragged = [];
|
|
2083
|
+
}
|
|
2084
|
+
};
|
|
2085
|
+
}
|
|
2086
|
+
//#endregion
|
|
1160
2087
|
//#region src/xy.ts
|
|
1161
2088
|
function isPair(value) {
|
|
1162
2089
|
return Array.isArray(value);
|
|
@@ -1335,6 +2262,73 @@ function createDragValue(element, options) {
|
|
|
1335
2262
|
};
|
|
1336
2263
|
}
|
|
1337
2264
|
//#endregion
|
|
2265
|
+
//#region src/pointer/long-press.ts
|
|
2266
|
+
/**
|
|
2267
|
+
* Repeat an action while a pointer is held down: once on the press, then
|
|
2268
|
+
* again every `interval` after `delay` — the way a stepper button or a
|
|
2269
|
+
* key held on a keyboard behaves.
|
|
2270
|
+
*
|
|
2271
|
+
* The release is listened for on the window, not on the element: the
|
|
2272
|
+
* pointer may be let go anywhere. Only the pointer that started the press
|
|
2273
|
+
* ends it, so a second finger lifting elsewhere does not. The window losing
|
|
2274
|
+
* focus ends it too, since the release would never arrive.
|
|
2275
|
+
*/
|
|
2276
|
+
function createLongPress(options) {
|
|
2277
|
+
let opts = options;
|
|
2278
|
+
let pointerId = null;
|
|
2279
|
+
let active = false;
|
|
2280
|
+
let timer = null;
|
|
2281
|
+
function clearTimer() {
|
|
2282
|
+
if (timer !== null) clearTimeout(timer);
|
|
2283
|
+
timer = null;
|
|
2284
|
+
}
|
|
2285
|
+
/** Wait `wait`, fire, and keep going at `interval` from then on. */
|
|
2286
|
+
function schedule(wait) {
|
|
2287
|
+
timer = setTimeout(() => {
|
|
2288
|
+
opts.onPress();
|
|
2289
|
+
if (active) schedule(opts.interval ?? 40);
|
|
2290
|
+
}, wait);
|
|
2291
|
+
}
|
|
2292
|
+
function onPointerEnd(event) {
|
|
2293
|
+
if (pointerId !== null && event.pointerId !== pointerId) return;
|
|
2294
|
+
stop();
|
|
2295
|
+
}
|
|
2296
|
+
function listen(add) {
|
|
2297
|
+
const target = globalThis.window;
|
|
2298
|
+
if (!target) return;
|
|
2299
|
+
const method = add ? "addEventListener" : "removeEventListener";
|
|
2300
|
+
target[method]("pointerup", onPointerEnd);
|
|
2301
|
+
target[method]("pointercancel", onPointerEnd);
|
|
2302
|
+
target[method]("blur", stop);
|
|
2303
|
+
}
|
|
2304
|
+
function stop() {
|
|
2305
|
+
if (!active) return;
|
|
2306
|
+
active = false;
|
|
2307
|
+
pointerId = null;
|
|
2308
|
+
clearTimer();
|
|
2309
|
+
listen(false);
|
|
2310
|
+
}
|
|
2311
|
+
return {
|
|
2312
|
+
start: (event) => {
|
|
2313
|
+
if (active || event && event.button !== 0) return;
|
|
2314
|
+
active = true;
|
|
2315
|
+
pointerId = event?.pointerId ?? null;
|
|
2316
|
+
listen(true);
|
|
2317
|
+
opts.onPress();
|
|
2318
|
+
if (active) schedule(opts.delay ?? 500);
|
|
2319
|
+
},
|
|
2320
|
+
stop,
|
|
2321
|
+
update: (next) => {
|
|
2322
|
+
opts = {
|
|
2323
|
+
...opts,
|
|
2324
|
+
...next
|
|
2325
|
+
};
|
|
2326
|
+
},
|
|
2327
|
+
pressed: () => active,
|
|
2328
|
+
destroy: stop
|
|
2329
|
+
};
|
|
2330
|
+
}
|
|
2331
|
+
//#endregion
|
|
1338
2332
|
//#region src/pointer/wheel.ts
|
|
1339
2333
|
/**
|
|
1340
2334
|
* Listen to wheel events on an element.
|
|
@@ -1369,73 +2363,105 @@ function createWheel(element, onWheel, options = {}) {
|
|
|
1369
2363
|
};
|
|
1370
2364
|
}
|
|
1371
2365
|
//#endregion
|
|
1372
|
-
//#region src/
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
2366
|
+
//#region src/position.ts
|
|
2367
|
+
/**
|
|
2368
|
+
* Where a value sits along a track, as a whole percentage from its start.
|
|
2369
|
+
*
|
|
2370
|
+
* Normalized along the scale, so a thumb, the fill behind it and the marks
|
|
2371
|
+
* all sit on the curve the drag follows. `reversed` measures from the other
|
|
2372
|
+
* end — for a track that grows upwards or leftwards on screen, since CSS
|
|
2373
|
+
* places things from the top and the left.
|
|
2374
|
+
*/
|
|
2375
|
+
function valuePercent(value, { min, max, scale = linearScale }, reversed = false) {
|
|
2376
|
+
const normalized = scale.normalize(value, min, max);
|
|
2377
|
+
return toFixed((reversed ? 1 - normalized : normalized) * 100);
|
|
1380
2378
|
}
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
2379
|
+
//#endregion
|
|
2380
|
+
//#region src/slider/decimal-digits.ts
|
|
2381
|
+
/**
|
|
2382
|
+
* How many digits a number carries after the decimal point.
|
|
2383
|
+
*
|
|
2384
|
+
* `Slider.Marks` builds its labels by multiplying an interval, which leaves
|
|
2385
|
+
* binary debris in the last digits (`0.1 * 3` is `0.30000000000000004`). The
|
|
2386
|
+
* count is what tells `toFixed` how far to round that back.
|
|
2387
|
+
*
|
|
2388
|
+
* The exponent form has to be handled separately: `String(1e-7)` is `'1e-7'`,
|
|
2389
|
+
* which has no decimal point at all, so reading the text after the point
|
|
2390
|
+
* reports no digits and the interval rounds away to whole numbers.
|
|
2391
|
+
*/
|
|
2392
|
+
function decimalDigits(x) {
|
|
2393
|
+
if (!Number.isFinite(x)) return 0;
|
|
2394
|
+
const text = String(x);
|
|
2395
|
+
const e = text.indexOf("e");
|
|
2396
|
+
if (e === -1) return text.split(".")[1]?.length ?? 0;
|
|
2397
|
+
const fraction = text.slice(0, e).split(".")[1]?.length ?? 0;
|
|
2398
|
+
return Math.max(0, fraction - Number(text.slice(e + 1)));
|
|
1384
2399
|
}
|
|
2400
|
+
//#endregion
|
|
2401
|
+
//#region src/slider/marks.ts
|
|
1385
2402
|
/**
|
|
1386
|
-
*
|
|
2403
|
+
* The marks `options` asks for between `min` and `max`, in ascending order.
|
|
1387
2404
|
*
|
|
1388
|
-
*
|
|
1389
|
-
*
|
|
2405
|
+
* Each value is a whole multiple of the interval, rounded to the digits the
|
|
2406
|
+
* interval has: stepping by 0.1 would otherwise put binary debris in the
|
|
2407
|
+
* labels (`0.1 * 3` is `0.30000000000000004`).
|
|
1390
2408
|
*/
|
|
1391
|
-
function
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
2409
|
+
function sliderMarks(options, min, max, step) {
|
|
2410
|
+
const { per, mark = true, label = true } = typeof options === "object" ? options : { per: options };
|
|
2411
|
+
const optionsList = [];
|
|
2412
|
+
const interval = per === "step" ? step : per;
|
|
2413
|
+
const count = Math.floor(max / interval) - Math.ceil(min / interval) + 1;
|
|
2414
|
+
for (let i = 0; i < count; i++) {
|
|
2415
|
+
const value = toFixed(interval * (Math.ceil(min / interval) + i), decimalDigits(interval));
|
|
2416
|
+
optionsList.push({
|
|
2417
|
+
value,
|
|
2418
|
+
mark,
|
|
2419
|
+
label
|
|
2420
|
+
});
|
|
1403
2421
|
}
|
|
1404
|
-
return
|
|
1405
|
-
update: (next) => {
|
|
1406
|
-
opts = {
|
|
1407
|
-
...opts,
|
|
1408
|
-
...next
|
|
1409
|
-
};
|
|
1410
|
-
},
|
|
1411
|
-
begin: (at, { additive = false, selection = [] } = {}) => {
|
|
1412
|
-
drag = {
|
|
1413
|
-
from: at,
|
|
1414
|
-
base: additive ? [...selection] : []
|
|
1415
|
-
};
|
|
1416
|
-
setBox(rectOf(at, at));
|
|
1417
|
-
if (!additive) opts.onSelectionChange?.([]);
|
|
1418
|
-
},
|
|
1419
|
-
move: (to) => {
|
|
1420
|
-
if (!drag) return;
|
|
1421
|
-
const rect = rectOf(drag.from, to);
|
|
1422
|
-
setBox(rect);
|
|
1423
|
-
apply(rect, drag.base);
|
|
1424
|
-
},
|
|
1425
|
-
end: () => {
|
|
1426
|
-
const running = drag !== null;
|
|
1427
|
-
drag = null;
|
|
1428
|
-
if (box !== null) setBox(null);
|
|
1429
|
-
return running;
|
|
1430
|
-
},
|
|
1431
|
-
box: () => box,
|
|
1432
|
-
destroy: () => {
|
|
1433
|
-
drag = null;
|
|
1434
|
-
box = null;
|
|
1435
|
-
}
|
|
1436
|
-
};
|
|
2422
|
+
return optionsList;
|
|
1437
2423
|
}
|
|
1438
2424
|
//#endregion
|
|
1439
|
-
|
|
2425
|
+
//#region src/style.ts
|
|
2426
|
+
/**
|
|
2427
|
+
* A number as a CSS length, for a custom property.
|
|
2428
|
+
*
|
|
2429
|
+
* A custom property takes whatever text it is given, so `--size: 50` comes out
|
|
2430
|
+
* as the invalid `50` rather than `50px`. React appends `px` to a bare number
|
|
2431
|
+
* only for the properties it knows take a length, and a custom property is
|
|
2432
|
+
* never one of them; Vue and Svelte append nothing at all. Anything already a
|
|
2433
|
+
* string is passed through, so `'3rem'` and `'100%'` still work.
|
|
2434
|
+
*/
|
|
2435
|
+
function cssLength(value) {
|
|
2436
|
+
return typeof value === "number" ? `${value}px` : value;
|
|
2437
|
+
}
|
|
2438
|
+
/**
|
|
2439
|
+
* Take an element out of sight while leaving it in the accessibility tree and
|
|
2440
|
+
* in the tab order.
|
|
2441
|
+
*
|
|
2442
|
+
* `display: none` and `visibility: hidden` would remove it from both, and the
|
|
2443
|
+
* native control underneath a headless component is what carries the ARIA and
|
|
2444
|
+
* the keyboard behaviour. `pointer-events: none` is safe because nothing is
|
|
2445
|
+
* ever clicked here directly: a `<label>` forwards its click, and a drag is
|
|
2446
|
+
* handled by the part that is visible.
|
|
2447
|
+
*
|
|
2448
|
+
* Every value is a string with its unit, so the object can be handed to any
|
|
2449
|
+
* framework's `style` binding as it is.
|
|
2450
|
+
*/
|
|
2451
|
+
const visuallyHiddenStyle = {
|
|
2452
|
+
position: "absolute",
|
|
2453
|
+
width: "1px",
|
|
2454
|
+
height: "1px",
|
|
2455
|
+
padding: "0",
|
|
2456
|
+
margin: "-1px",
|
|
2457
|
+
overflow: "hidden",
|
|
2458
|
+
clip: "rect(0, 0, 0, 0)",
|
|
2459
|
+
clipPath: "inset(50%)",
|
|
2460
|
+
whiteSpace: "nowrap",
|
|
2461
|
+
border: "0",
|
|
2462
|
+
pointerEvents: "none"
|
|
2463
|
+
};
|
|
2464
|
+
//#endregion
|
|
2465
|
+
export { DEFAULT_DRAG_SENSITIVITY, DEFAULT_KEYBOARD_OPTIONS, DEFAULT_WHEEL_OPTIONS, KNOB_VIEWBOX_SIZE, NOT_SUPPORTED, PERMISSION_DENIED, PITCH_BEND_CENTER, POINTS_EDITOR_DEFAULT_KEYBOARD, POINTS_EDITOR_DEFAULT_WHEEL, POINT_AXIS, SHORTCUTS, UNAVAILABLE, applyDelta, arrowKeyDirection, arrowKeyMove, blackKeyWidth, caretAtDecimalOffset, caretDecimalOffset, checkSteps, clampPoint, commitNumberInputText, createAnimationCanvas, createDrag, createDragValue, createDropZone, createLongPress, createMIDIAccess, createMIDIInput, createMIDIMessage, createPianoInput, createPointsEditor, createSelectionBox, createStepperDrag, createWheel, cssLength, drawingState, elementMapping, fitWhiteKeyWidth, getNoteRangeArray, isArrowKey, isDrawingState, knobAngles, knobArcPath, knobArcPoint, knobArcRadius, mapModifier, matchesAccept, noteAt, notePosition, nudgeNumberInput, numberInputBounds, numberInputRanges, numberSpan, parseNumberText, partitionByAccept, pianoWidth, relativeMapping, replaceOptions, selectModifier, selectionBoxCovers, sliderMarks, toXY, valuePercent, visuallyHiddenStyle, wheelDirection, wheelMove };
|
|
1440
2466
|
|
|
1441
2467
|
//# sourceMappingURL=index.js.map
|