MCPcopy Create free account
hub / github.com/CoderLine/alphaTab / SelectionHandles

Class SelectionHandles

packages/playground/src/components/SelectionHandles.ts:38–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36type DragSide = 'start' | 'end';
37
38export class SelectionHandles implements Mountable {
39 readonly root: HTMLElement;
40 private startHandle: HTMLElement;
41 private endHandle: HTMLElement;
42 private currentHighlight: alphaTab.PlaybackHighlightChangeEventArgs | undefined;
43 private dragging: DragSide | undefined;
44 private unsubHighlight: () => void;
45
46 private onMouseMove = (e: MouseEvent) => {
47 if (!this.dragging) {
48 return;
49 }
50 e.preventDefault();
51 const beat = this.beatFromEvent(e);
52 if (!beat || !this.currentHighlight) {
53 return;
54 }
55 if (this.dragging === 'start') {
56 this.api.highlightPlaybackRange(beat, this.currentHighlight.endBeat!);
57 } else {
58 this.api.highlightPlaybackRange(this.currentHighlight.startBeat!, beat);
59 }
60 };
61
62 private onMouseUp = (e: MouseEvent) => {
63 if (!this.dragging) {
64 return;
65 }
66 e.preventDefault();
67 this.endDrag();
68 this.api.applyPlaybackRangeFromHighlight();
69 };
70
71 constructor(
72 private api: alphaTab.AlphaTabApi,
73 private viewportEl: HTMLElement
74 ) {
75 this.root = parseHtml(html`
76 <div class="at-selection-handles">
77 <div class="at-selection-handle at-selection-handle-start"></div>
78 <div class="at-selection-handle at-selection-handle-end"></div>
79 </div>
80 `);
81 this.startHandle = this.root.querySelector('.at-selection-handle-start')!;
82 this.endHandle = this.root.querySelector('.at-selection-handle-end')!;
83
84 this.startHandle.addEventListener('mousedown', e => this.beginDrag(e, 'start'));
85 this.endHandle.addEventListener('mousedown', e => this.beginDrag(e, 'end'));
86 document.addEventListener('mousemove', this.onMouseMove, true);
87 document.addEventListener('mouseup', this.onMouseUp, true);
88
89 this.unsubHighlight = api.playbackRangeHighlightChanged.on(e => this.update(e));
90 }
91
92 private beginDrag(e: MouseEvent, side: DragSide): void {
93 e.preventDefault();
94 this.dragging = side;
95 this.viewportEl.classList.add('at-selection-handle-drag');

Callers

nothing calls this directly

Calls 5

beatFromEventMethod · 0.95
endDragMethod · 0.95
preventDefaultMethod · 0.65

Tested by

no test coverage detected