|
| 1 | +import {ScoreTime, TimeManagerListener, UpdateSource} from "./TimeManager"; |
| 2 | +import {Annotation, AnnotationCode} from "./data/annotations"; |
| 3 | +import {bar_to_page} from "./data/barToPage"; |
| 4 | +import {globals} from "./globals"; |
| 5 | + |
| 6 | +// Maps each annotation code to the CSS custom-property that defines its colour. |
| 7 | +// Reading from the computed style keeps the colour values in one place (styles.css). |
| 8 | +const CODE_CSS_VAR: Record<AnnotationCode, string> = { |
| 9 | + 'dy': '--dynamiques-color', |
| 10 | + 'du': '--duree-color', |
| 11 | + 'for': '--formes-color', |
| 12 | + 'int': '--intonation-color', |
| 13 | + 'mo': '--motifs-color', |
| 14 | + 'tim': '--timbre-color', |
| 15 | + 'graph': '--graph-color', |
| 16 | +}; |
| 17 | + |
| 18 | +function annotationColor(codes: AnnotationCode[]): string { |
| 19 | + for (const code of codes) { |
| 20 | + if (code !== 'graph') { |
| 21 | + const cssVar = CODE_CSS_VAR[code]; |
| 22 | + return getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim(); |
| 23 | + } |
| 24 | + } |
| 25 | + return ''; |
| 26 | +} |
| 27 | + |
| 28 | +export class CurrentPageAnnotations extends TimeManagerListener { |
| 29 | + private readonly container: HTMLElement; |
| 30 | + private readonly getAnnotations: () => Annotation[]; |
| 31 | + private readonly imageHolder: HTMLElement | null; |
| 32 | + |
| 33 | + constructor(getAnnotations: () => Annotation[]) { |
| 34 | + super(); |
| 35 | + this.getAnnotations = getAnnotations; |
| 36 | + |
| 37 | + this.container = document.createElement('div'); |
| 38 | + this.container.id = 'current-page-annotations'; |
| 39 | + |
| 40 | + this.imageHolder = document.getElementById('image-holder'); |
| 41 | + this.imageHolder?.appendChild(this.container); |
| 42 | + |
| 43 | + // Set up delegated listeners on imageHolder so bar-overlay events are |
| 44 | + // caught regardless of when ScoreManager adds or removes those elements. |
| 45 | + if (this.imageHolder) { |
| 46 | + this.setupBarHoverDelegation(this.imageHolder); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + async timeUpdated(scoreTime: ScoreTime, _updateSource: UpdateSource) { |
| 51 | + this.render(scoreTime); |
| 52 | + } |
| 53 | + |
| 54 | + // ── Page membership ─────────────────────────────────────── |
| 55 | + |
| 56 | + private barsOnCurrentPage(scoreTime: ScoreTime): Set<number> { |
| 57 | + const currentImage = bar_to_page[scoreTime.act - 1][scoreTime.bar].image; |
| 58 | + const actBars = bar_to_page[scoreTime.act - 1]; |
| 59 | + const result = new Set<number>(); |
| 60 | + for (const barNum in actBars) { |
| 61 | + if (actBars[barNum].image === currentImage) { |
| 62 | + result.add(Number(barNum)); |
| 63 | + } |
| 64 | + } |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | + private annotationsForPage(scoreTime: ScoreTime): Annotation[] { |
| 69 | + const onPage = this.barsOnCurrentPage(scoreTime); |
| 70 | + return this.getAnnotations().filter(annotation => { |
| 71 | + if (annotation.act !== scoreTime.act) return false; |
| 72 | + for (let bar = annotation.measure_range[0]; bar <= annotation.measure_range[1]; bar++) { |
| 73 | + if (onPage.has(bar)) return true; |
| 74 | + } |
| 75 | + return false; |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private highlightBarsForAnnotation(range: [number, number], codes: AnnotationCode[]) { |
| 80 | + if (!this.imageHolder) return; |
| 81 | + const color = annotationColor(codes); |
| 82 | + for (const el of this.imageHolder.querySelectorAll<HTMLElement>('[data-bar]')) { |
| 83 | + const bar = Number(el.dataset.bar); |
| 84 | + const inRange = bar >= range[0] && bar <= range[1]; |
| 85 | + el.classList.toggle('annotation-highlight', inRange); |
| 86 | + // Override the default pink background with the annotation-category colour. |
| 87 | + // Inline styles beat class rules, so this shows through the opacity set by |
| 88 | + // .annotation-highlight. Cleared back to '' when the hover ends. |
| 89 | + el.style.background = inRange && color ? color : ''; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private clearBarHighlights() { |
| 94 | + this.imageHolder |
| 95 | + ?.querySelectorAll<HTMLElement>('[data-bar].annotation-highlight') |
| 96 | + .forEach(el => { |
| 97 | + el.classList.remove('annotation-highlight'); |
| 98 | + el.style.background = ''; |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + // ── Hover: bar overlays → annotation divs (delegated) ──── |
| 103 | + |
| 104 | + private setupBarHoverDelegation(imageHolder: HTMLElement) { |
| 105 | + imageHolder.addEventListener('mouseover', (e: MouseEvent) => { |
| 106 | + const from = (e.relatedTarget as HTMLElement | null) |
| 107 | + ?.closest<HTMLElement>('[data-bar]'); |
| 108 | + const to = (e.target as HTMLElement) |
| 109 | + .closest<HTMLElement>('[data-bar]'); |
| 110 | + // Only act when entering a different bar overlay (ignores moves within one). |
| 111 | + if (to && to !== from) { |
| 112 | + this.highlightAnnotationsForBar(Number(to.dataset.bar)); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + imageHolder.addEventListener('mouseout', (e: MouseEvent) => { |
| 117 | + const from = (e.target as HTMLElement) |
| 118 | + .closest<HTMLElement>('[data-bar]'); |
| 119 | + const to = (e.relatedTarget as HTMLElement | null) |
| 120 | + ?.closest<HTMLElement>('[data-bar]'); |
| 121 | + // Only clear when leaving a bar overlay entirely (ignores moves within one). |
| 122 | + if (from && from !== to) { |
| 123 | + this.clearAnnotationHighlights(); |
| 124 | + } |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + private highlightAnnotationsForBar(bar: number) { |
| 129 | + for (const el of this.container.querySelectorAll<HTMLElement>('.page-annotation')) { |
| 130 | + const start = Number(el.dataset.measureStart); |
| 131 | + const end = Number(el.dataset.measureEnd); |
| 132 | + el.classList.toggle('annotation-highlight', bar >= start && bar <= end); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private clearAnnotationHighlights() { |
| 137 | + this.container |
| 138 | + .querySelectorAll<HTMLElement>('.page-annotation.annotation-highlight') |
| 139 | + .forEach(el => el.classList.remove('annotation-highlight')); |
| 140 | + } |
| 141 | + |
| 142 | + // ── Render ──────────────────────────────────────────────── |
| 143 | + |
| 144 | + private render(scoreTime: ScoreTime) { |
| 145 | + const annotations = this.annotationsForPage(scoreTime); |
| 146 | + |
| 147 | + this.container.classList.toggle('has-annotations', annotations.length > 0); |
| 148 | + this.container.innerHTML = ''; |
| 149 | + |
| 150 | + for (const annotation of annotations) { |
| 151 | + const div = document.createElement('div'); |
| 152 | + div.classList.add('page-annotation'); |
| 153 | + for (const code of annotation.code) { |
| 154 | + div.classList.add(`${code}-annotation`); |
| 155 | + } |
| 156 | + if (annotation.code.length === 0) { |
| 157 | + div.classList.add('unclassified-annotation'); |
| 158 | + } |
| 159 | + |
| 160 | + // Store measure range as data attributes so the bar-hover handler |
| 161 | + // can determine which annotations to highlight without holding a |
| 162 | + // closure over the full annotation object for each bar overlay. |
| 163 | + div.dataset.measureStart = String(annotation.measure_range[0]); |
| 164 | + div.dataset.measureEnd = String(annotation.measure_range[1]); |
| 165 | + |
| 166 | + div.addEventListener('mouseenter', () => |
| 167 | + this.highlightBarsForAnnotation(annotation.measure_range, annotation.code)); |
| 168 | + div.addEventListener('mouseleave', () => |
| 169 | + this.clearBarHighlights()); |
| 170 | + |
| 171 | + const text = document.createElement('div'); |
| 172 | + text.classList.add('page-annotation-text'); |
| 173 | + text.innerHTML = annotation.annotation[globals.language]; |
| 174 | + div.appendChild(text); |
| 175 | + |
| 176 | + this.container.appendChild(div); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments