@vocasync/astro 1.0.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +618 -80
  2. package/dist/api/client.d.ts.map +1 -1
  3. package/dist/api/client.js.map +1 -1
  4. package/dist/api/index.d.ts +1 -1
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/api/index.js +1 -1
  7. package/dist/api/index.js.map +1 -1
  8. package/dist/cli.js.map +1 -1
  9. package/dist/components/AudioPlayer.astro +298 -1141
  10. package/dist/components/index.d.ts +10 -32
  11. package/dist/components/index.d.ts.map +1 -1
  12. package/dist/components/index.js +1 -3
  13. package/dist/components/index.js.map +1 -1
  14. package/dist/components/types.d.ts +117 -0
  15. package/dist/components/types.d.ts.map +1 -0
  16. package/dist/components/types.js +2 -0
  17. package/dist/components/types.js.map +1 -0
  18. package/dist/config/index.d.ts +2 -2
  19. package/dist/config/index.d.ts.map +1 -1
  20. package/dist/config/index.js +1 -1
  21. package/dist/config/index.js.map +1 -1
  22. package/dist/config/schema.d.ts +124 -169
  23. package/dist/config/schema.d.ts.map +1 -1
  24. package/dist/config/schema.js +4 -4
  25. package/dist/config/schema.js.map +1 -1
  26. package/dist/core/content-loader.js +1 -1
  27. package/dist/core/content-loader.js.map +1 -1
  28. package/dist/core/index.d.ts +2 -2
  29. package/dist/core/index.d.ts.map +1 -1
  30. package/dist/core/index.js +2 -2
  31. package/dist/core/index.js.map +1 -1
  32. package/dist/core/speech-builder.js.map +1 -1
  33. package/dist/index.d.ts +6 -7
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +5 -6
  36. package/dist/index.js.map +1 -1
  37. package/dist/integration.d.ts +28 -2
  38. package/dist/integration.d.ts.map +1 -1
  39. package/dist/integration.js +40 -3
  40. package/dist/integration.js.map +1 -1
  41. package/dist/lib/latex-to-speech/index.d.ts.map +1 -1
  42. package/dist/lib/latex-to-speech/index.js.map +1 -1
  43. package/dist/player-core/boot.d.ts +9 -0
  44. package/dist/player-core/boot.d.ts.map +1 -0
  45. package/dist/player-core/boot.js +59 -0
  46. package/dist/player-core/boot.js.map +1 -0
  47. package/dist/player-core/highlighter.d.ts +31 -0
  48. package/dist/player-core/highlighter.d.ts.map +1 -0
  49. package/dist/player-core/highlighter.js +103 -0
  50. package/dist/player-core/highlighter.js.map +1 -0
  51. package/dist/player-core/index.d.ts +17 -0
  52. package/dist/player-core/index.d.ts.map +1 -0
  53. package/dist/player-core/index.js +24 -0
  54. package/dist/player-core/index.js.map +1 -0
  55. package/dist/player-core/media-url.d.ts +18 -0
  56. package/dist/player-core/media-url.d.ts.map +1 -0
  57. package/dist/player-core/media-url.js +33 -0
  58. package/dist/player-core/media-url.js.map +1 -0
  59. package/dist/player-core/player.d.ts +9 -0
  60. package/dist/player-core/player.d.ts.map +1 -0
  61. package/dist/player-core/player.js +622 -0
  62. package/dist/player-core/player.js.map +1 -0
  63. package/dist/player-core/registry.d.ts +20 -0
  64. package/dist/player-core/registry.d.ts.map +1 -0
  65. package/dist/player-core/registry.js +31 -0
  66. package/dist/player-core/registry.js.map +1 -0
  67. package/dist/player-core/seek-delegate.d.ts +24 -0
  68. package/dist/player-core/seek-delegate.d.ts.map +1 -0
  69. package/dist/player-core/seek-delegate.js +64 -0
  70. package/dist/player-core/seek-delegate.js.map +1 -0
  71. package/dist/player-core/storage.d.ts +38 -0
  72. package/dist/player-core/storage.d.ts.map +1 -0
  73. package/dist/player-core/storage.js +107 -0
  74. package/dist/player-core/storage.js.map +1 -0
  75. package/dist/player-core/strings.d.ts +53 -0
  76. package/dist/player-core/strings.d.ts.map +1 -0
  77. package/dist/player-core/strings.js +45 -0
  78. package/dist/player-core/strings.js.map +1 -0
  79. package/dist/player-core/timings.d.ts +56 -0
  80. package/dist/player-core/timings.d.ts.map +1 -0
  81. package/dist/player-core/timings.js +90 -0
  82. package/dist/player-core/timings.js.map +1 -0
  83. package/dist/rehype/audio-words.d.ts +9 -3
  84. package/dist/rehype/audio-words.d.ts.map +1 -1
  85. package/dist/rehype/audio-words.js +8 -8
  86. package/dist/rehype/audio-words.js.map +1 -1
  87. package/dist/rehype/index.d.ts +2 -2
  88. package/dist/rehype/index.d.ts.map +1 -1
  89. package/dist/rehype/index.js.map +1 -1
  90. package/dist/rehype/rehype-math-speech.d.ts.map +1 -1
  91. package/dist/rehype/rehype-math-speech.js +2 -0
  92. package/dist/rehype/rehype-math-speech.js.map +1 -1
  93. package/dist/styles/content.css +41 -0
  94. package/dist/styles/layers.css +13 -0
  95. package/dist/styles/player.css +517 -0
  96. package/dist/styles/preset-shadcn.css +31 -0
  97. package/dist/styles/tokens.css +266 -0
  98. package/dist/styles/vocasync.css +10 -0
  99. package/dist/sync/index.d.ts +1 -1
  100. package/dist/sync/index.d.ts.map +1 -1
  101. package/dist/sync/index.js +1 -1
  102. package/dist/sync/index.js.map +1 -1
  103. package/dist/sync/orchestrator.d.ts.map +1 -1
  104. package/dist/sync/orchestrator.js.map +1 -1
  105. package/package.json +40 -17
  106. package/src/api/index.ts +4 -4
  107. package/src/components/AudioPlayer.astro +298 -1141
  108. package/src/components/index.ts +19 -34
  109. package/src/components/types.ts +140 -0
  110. package/src/config/index.ts +23 -24
  111. package/src/config/schema.ts +4 -4
  112. package/src/core/content-loader.ts +1 -1
  113. package/src/core/index.ts +7 -7
  114. package/src/env.d.ts +2 -0
  115. package/src/index.ts +23 -28
  116. package/src/integration.ts +66 -4
  117. package/src/player-core/boot.ts +62 -0
  118. package/src/player-core/highlighter.ts +159 -0
  119. package/src/player-core/index.ts +45 -0
  120. package/src/player-core/media-url.ts +35 -0
  121. package/src/player-core/player.ts +725 -0
  122. package/src/player-core/registry.ts +40 -0
  123. package/src/player-core/seek-delegate.ts +92 -0
  124. package/src/player-core/storage.ts +112 -0
  125. package/src/player-core/strings.ts +89 -0
  126. package/src/player-core/timings.ts +115 -0
  127. package/src/rehype/audio-words.ts +17 -11
  128. package/src/rehype/index.ts +2 -3
  129. package/src/rehype/rehype-math-speech.ts +3 -1
  130. package/src/styles/content.css +41 -0
  131. package/src/styles/layers.css +13 -0
  132. package/src/styles/player.css +517 -0
  133. package/src/styles/preset-shadcn.css +31 -0
  134. package/src/styles/tokens.css +266 -0
  135. package/src/styles/vocasync.css +10 -0
  136. package/src/sync/index.ts +1 -1
  137. package/src/sync/orchestrator.ts +1 -1
  138. package/dist/styles/variables.css +0 -164
  139. package/src/styles/variables.css +0 -164
@@ -1,1179 +1,336 @@
1
1
  ---
2
2
  /**
3
- * VocaSync AudioPlayer Component
4
- *
5
- * A full-featured audio player with word-level highlighting support.
6
- * Uses native HTML5 audio and range inputs - no external dependencies.
7
- *
3
+ * VocaSync AudioPlayer.
4
+ *
5
+ * Markup only. Playback, highlighting and the view-transition lifecycle live in
6
+ * `@vocasync/astro/player-core`, which the hoisted script below installs once per page.
7
+ *
8
8
  * @example
9
9
  * ```astro
10
- * <AudioPlayer
11
- * slug={post.slug}
12
- * label="Listen to this article"
13
- * enableMiniPlayer={true}
10
+ * <AudioPlayer
11
+ * slug={post.slug}
12
+ * variant="minimal"
13
+ * controls={["play", "progress", "speed"]}
14
+ * strings={{ play: "Lire", pause: "Pause" }}
14
15
  * />
15
16
  * ```
16
17
  */
17
-
18
- import type { AlignedWord } from "../types/index.js";
19
-
20
- interface AudioEntry {
21
- audioUrl: string;
22
- words?: AlignedWord[];
23
- duration?: number;
24
- /** Publishable key for the synthesis stream (v3 audio-map). */
25
- synthesisPublishableKey?: string;
26
- }
27
-
28
- interface Props {
29
- /** Post slug to lookup audio in audio-map */
30
- slug: string;
31
- /** Audio entry data (from audio-map.json import) */
32
- audioEntry?: AudioEntry;
33
- /** Accessible label for the player */
34
- label?: string;
35
- /** Show placeholder when no audio available */
36
- showPlaceholder?: boolean;
37
- /** Additional CSS classes */
38
- class?: string;
39
- /** Selector for article body (for word highlighting) */
40
- articleSelector?: string;
41
- /** Enable floating mini player when main player scrolls out of view */
42
- enableMiniPlayer?: boolean;
43
- /** Enable word highlighting during playback */
44
- enableHighlighting?: boolean;
45
- /** Enable click on words to seek to that position */
46
- enableClickToSeek?: boolean;
47
- /** Number of trailing highlighted words */
48
- trailLength?: number;
49
- }
18
+ import { defaultStrings, formatString } from "../player-core/strings.js";
19
+ import type { AudioPlayerProps } from "./types.js";
20
+ import { DEFAULT_CONTROLS } from "./types.js";
50
21
 
51
22
  const {
52
23
  slug,
53
24
  audioEntry,
54
- label = "Listen to this article",
25
+ variant = "bar",
26
+ size = "md",
27
+ controls = DEFAULT_CONTROLS,
28
+ speeds = [0.5, 0.75, 1, 1.25, 1.5, 2],
29
+ skipSeconds = 15,
30
+ strings = {},
31
+ articleSelector = "[data-article-body]",
32
+ highlight = {},
33
+ dock = {},
34
+ autoScroll = "off",
35
+ exclusive = true,
36
+ rememberPreferences = true,
37
+ rememberPosition = false,
38
+ mediaSession = true,
39
+ title,
40
+ artwork,
55
41
  showPlaceholder = true,
56
42
  class: className = "",
57
- articleSelector = "[data-article-body]",
58
- enableMiniPlayer = true,
59
- enableHighlighting = true,
60
- enableClickToSeek = true,
61
- trailLength = 4,
62
- } = Astro.props;
63
-
64
- // Generate unique IDs for this player instance
65
- const playerId = `vocasync-player-${slug.replace(/[^a-z0-9]/gi, "-")}`;
66
- const audioId = `${playerId}-audio`;
67
-
68
- // Serialize audio data for client-side use
69
- const audioDataJson = audioEntry ? JSON.stringify({
70
- audioUrl: audioEntry.audioUrl,
71
- words: audioEntry.words || [],
72
- duration: audioEntry.duration,
73
- publishableKey: audioEntry.synthesisPublishableKey,
74
- }) : null;
43
+ id,
44
+ } = Astro.props as AudioPlayerProps;
45
+
46
+ const s = { ...defaultStrings, ...strings };
47
+
48
+ const { enabled: highlightEnabled = true, trailLength = 4, clickToSeek = true } = highlight;
49
+ const { enabled: dockEnabled = true } = dock;
50
+
51
+ const playerId = id ?? `vocasync-player-${slug.replace(/[^a-z0-9]/gi, "-")}`;
52
+
53
+ // One config object rather than a scatter of data-* attributes. It also carries the
54
+ // audio payload: attribute-escaping JSON inflates it ~1.6x, since every `"` becomes
55
+ // `&quot;`. Only `<` needs neutralising inside a JSON script tag.
56
+ const config = {
57
+ audio: audioEntry
58
+ ? {
59
+ audioUrl: audioEntry.audioUrl,
60
+ words: audioEntry.words ?? [],
61
+ duration: audioEntry.duration,
62
+ publishableKey: audioEntry.synthesisPublishableKey,
63
+ }
64
+ : null,
65
+ options: {
66
+ articleSelector,
67
+ trailLength,
68
+ highlighting: highlightEnabled,
69
+ clickToSeek,
70
+ autoScroll,
71
+ dock: dockEnabled,
72
+ exclusive,
73
+ skipSeconds,
74
+ rememberPreferences,
75
+ rememberPosition,
76
+ mediaSession,
77
+ slug,
78
+ title,
79
+ artwork,
80
+ },
81
+ strings: {
82
+ play: s.play,
83
+ pause: s.pause,
84
+ mute: s.mute,
85
+ unmute: s.unmute,
86
+ highlightOn: s.highlightOn,
87
+ highlightOff: s.highlightOff,
88
+ // Only the strings the runtime needs. skipBack/skipForward are functions, so they
89
+ // are not serialisable -- and they are only used server-side, on the markup below.
90
+ label: s.label,
91
+ speedValue: s.speedValue,
92
+ seekPosition: s.seekPosition,
93
+ },
94
+ };
95
+ const configJson = JSON.stringify(config).replace(/</g, "\\u003c");
75
96
  ---
76
97
 
77
- <div
98
+ <div
78
99
  class:list={["vocasync-player", className]}
79
100
  id={playerId}
80
101
  data-slug={slug}
81
- data-audio-entry={audioDataJson}
82
- data-article-selector={articleSelector}
83
- data-enable-mini={enableMiniPlayer}
84
- data-enable-highlighting={enableHighlighting}
85
- data-enable-click-to-seek={enableClickToSeek}
86
- data-trail-length={trailLength}
87
- aria-label={label}
102
+ data-variant={variant}
103
+ data-size={size}
104
+ role="group"
105
+ aria-label={s.label}
88
106
  tabindex="0"
89
107
  >
90
- <!-- Hidden audio element -->
91
- <audio id={audioId} preload="metadata"></audio>
92
-
93
- <!-- Loading state -->
108
+ <audio preload="metadata"></audio>
109
+ <script type="application/json" class="vocasync-data" set:html={configJson} />
110
+
111
+ <slot name="before" />
112
+
94
113
  <div class="vocasync-player__loading" data-state="loading">
95
114
  <div class="vocasync-player__spinner"></div>
96
- <span>Loading audio...</span>
115
+ <span>{s.loading}</span>
97
116
  </div>
98
-
99
- <!-- No audio placeholder -->
117
+
100
118
  <div class="vocasync-player__placeholder" data-state="no-audio" hidden>
101
- {showPlaceholder && (
102
- <span class="vocasync-player__placeholder-text">
103
- Audio not available for this article
104
- </span>
105
- )}
119
+ <slot name="placeholder">
120
+ {showPlaceholder && (
121
+ <span class="vocasync-player__placeholder-text">{s.unavailable}</span>
122
+ )}
123
+ </slot>
124
+ </div>
125
+
126
+ <div class="vocasync-player__error" data-state="error" hidden>
127
+ <slot name="error">
128
+ <span class="vocasync-player__error-text">{s.error}</span>
129
+ <button type="button" class="vocasync-player__retry-btn" data-action="retry">
130
+ {s.retry}
131
+ </button>
132
+ </slot>
106
133
  </div>
107
-
108
- <!-- Main player controls -->
134
+
109
135
  <div class="vocasync-player__controls" data-state="ready" hidden>
110
- <!-- Play/Pause button -->
111
- <button
112
- type="button"
113
- class="vocasync-player__play-btn"
114
- aria-label="Play"
115
- data-action="play-pause"
116
- title="Play/Pause (Space)"
117
- >
118
- <svg class="vocasync-icon vocasync-icon--play" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
119
- <path d="M6 4l15 8-15 8V4z"/>
120
- </svg>
121
- <svg class="vocasync-icon vocasync-icon--pause vocasync-hidden" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
122
- <path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z"/>
123
- </svg>
124
- </button>
125
-
126
- <!-- Progress section -->
127
- <div class="vocasync-player__progress-section">
128
- <!-- Time display -->
129
- <div class="vocasync-player__time">
130
- <span data-time="current">0:00</span>
131
- <span class="vocasync-player__time-sep">/</span>
132
- <span data-time="duration">0:00</span>
133
- </div>
134
-
135
- <!-- Progress bar -->
136
- <input
137
- type="range"
138
- class="vocasync-player__progress"
139
- min="0"
140
- max="100"
141
- value="0"
142
- step="0.1"
143
- aria-label="Seek"
144
- data-action="seek"
145
- title="Seek (Arrow keys)"
146
- />
147
- </div>
148
-
149
- <!-- Control buttons group -->
150
- <div class="vocasync-player__button-group">
151
- <!-- Highlighting toggle -->
152
- <button
153
- type="button"
154
- class="vocasync-player__icon-btn"
155
- aria-label="Toggle word highlighting"
156
- data-action="toggle-highlighting"
157
- data-highlighting="on"
158
- title="Toggle highlighting (H)"
159
- >
160
- <svg class="vocasync-icon vocasync-icon--highlight-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
161
- <path d="M12 3v18"/>
162
- <rect x="4" y="8" width="16" height="8" rx="1" fill="currentColor" opacity="0.3"/>
163
- <path d="M8 12h8"/>
136
+ {controls.map((control) => (
137
+ <Fragment>
138
+ {control === "play" && (
139
+ <button
140
+ type="button"
141
+ class="vocasync-player__play-btn"
142
+ aria-label={s.play}
143
+ data-action="play-pause"
144
+ >
145
+ <slot name="icon-play">
146
+ <svg class="vocasync-icon vocasync-icon--play" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
147
+ <path d="M6 4l15 8-15 8V4z" />
148
+ </svg>
149
+ </slot>
150
+ <slot name="icon-pause">
151
+ <svg class="vocasync-icon vocasync-icon--pause vocasync-hidden" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
152
+ <path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z" />
153
+ </svg>
154
+ </slot>
155
+ </button>
156
+ )}
157
+
158
+ {control === "progress" && (
159
+ <div class="vocasync-player__progress-section">
160
+ <div class="vocasync-player__time">
161
+ <span data-time="current">0:00</span>
162
+ <span class="vocasync-player__time-sep">/</span>
163
+ <span data-time="duration">0:00</span>
164
+ </div>
165
+ <input
166
+ type="range"
167
+ class="vocasync-player__progress"
168
+ min="0"
169
+ max="100"
170
+ value="0"
171
+ step="0.1"
172
+ aria-label={s.seek}
173
+ data-action="seek"
174
+ />
175
+ </div>
176
+ )}
177
+
178
+ {control === "time" && (
179
+ <div class="vocasync-player__time">
180
+ <span data-time="current">0:00</span>
181
+ <span class="vocasync-player__time-sep">/</span>
182
+ <span data-time="duration">0:00</span>
183
+ </div>
184
+ )}
185
+
186
+ {control === "skip-back" && (
187
+ <button
188
+ type="button"
189
+ class="vocasync-player__icon-btn"
190
+ aria-label={formatString(s.skipBack, { seconds: skipSeconds })}
191
+ data-action="skip-back"
192
+ >
193
+ <slot name="icon-skip-back">
194
+ <svg class="vocasync-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
195
+ <path d="M11 17l-5-5 5-5" />
196
+ <path d="M18 17l-5-5 5-5" />
197
+ </svg>
198
+ </slot>
199
+ </button>
200
+ )}
201
+
202
+ {control === "skip-forward" && (
203
+ <button
204
+ type="button"
205
+ class="vocasync-player__icon-btn"
206
+ aria-label={formatString(s.skipForward, { seconds: skipSeconds })}
207
+ data-action="skip-forward"
208
+ >
209
+ <slot name="icon-skip-forward">
210
+ <svg class="vocasync-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
211
+ <path d="M13 17l5-5-5-5" />
212
+ <path d="M6 17l5-5-5-5" />
213
+ </svg>
214
+ </slot>
215
+ </button>
216
+ )}
217
+
218
+ {control === "highlight" && (
219
+ <button
220
+ type="button"
221
+ class="vocasync-player__icon-btn"
222
+ aria-label={s.highlightOn}
223
+ data-action="toggle-highlighting"
224
+ data-highlighting="on"
225
+ >
226
+ <slot name="icon-highlight-on">
227
+ <svg class="vocasync-icon vocasync-icon--highlight-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
228
+ <path d="M12 3v18" />
229
+ <rect x="4" y="8" width="16" height="8" rx="1" fill="currentColor" opacity="0.3" />
230
+ <path d="M8 12h8" />
231
+ </svg>
232
+ </slot>
233
+ <slot name="icon-highlight-off">
234
+ <svg class="vocasync-icon vocasync-icon--highlight-off vocasync-hidden" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
235
+ <path d="M12 3v18" opacity="0.3" />
236
+ <rect x="4" y="8" width="16" height="8" rx="1" opacity="0.3" />
237
+ <path d="M4 4l16 16" />
238
+ </svg>
239
+ </slot>
240
+ </button>
241
+ )}
242
+
243
+ {control === "speed" && (
244
+ <div class="vocasync-player__speed">
245
+ <button
246
+ type="button"
247
+ class="vocasync-player__speed-btn"
248
+ aria-label={s.speed}
249
+ aria-haspopup="menu"
250
+ aria-expanded="false"
251
+ data-action="speed"
252
+ >
253
+ <span data-speed-label>{formatString(s.speedValue, { rate: 1 })}</span>
254
+ </button>
255
+ <div class="vocasync-player__speed-menu" role="menu" hidden>
256
+ {speeds.map((rate) => (
257
+ <button
258
+ type="button"
259
+ role="menuitemradio"
260
+ aria-checked={rate === 1 ? "true" : "false"}
261
+ data-speed={String(rate)}
262
+ {...(rate === 1 ? { "data-active": "" } : {})}
263
+ >
264
+ {formatString(s.speedValue, { rate })}
265
+ </button>
266
+ ))}
267
+ </div>
268
+ </div>
269
+ )}
270
+
271
+ {control === "volume" && (
272
+ <div class="vocasync-player__volume">
273
+ <button type="button" class="vocasync-player__icon-btn" aria-label={s.mute} data-action="mute">
274
+ <slot name="icon-volume">
275
+ <svg class="vocasync-icon vocasync-icon--volume-high" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
276
+ <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" fill="currentColor" />
277
+ <path d="M15.54 8.46a5 5 0 0 1 0 7.07" />
278
+ <path d="M19.07 4.93a10 10 0 0 1 0 14.14" />
279
+ </svg>
280
+ </slot>
281
+ <slot name="icon-muted">
282
+ <svg class="vocasync-icon vocasync-icon--volume-mute vocasync-hidden" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
283
+ <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" fill="currentColor" opacity="0.5" />
284
+ <line x1="23" y1="9" x2="17" y2="15" />
285
+ <line x1="17" y1="9" x2="23" y2="15" />
286
+ </svg>
287
+ </slot>
288
+ </button>
289
+ <input
290
+ type="range"
291
+ class="vocasync-player__volume-slider"
292
+ min="0"
293
+ max="100"
294
+ value="100"
295
+ aria-label={s.volume}
296
+ data-action="volume"
297
+ />
298
+ </div>
299
+ )}
300
+ </Fragment>
301
+ ))}
302
+ </div>
303
+
304
+ <slot name="after" />
305
+
306
+ {dockEnabled && (
307
+ <div class="vocasync-mini-player" data-mini-player role="group" aria-label={s.label} hidden>
308
+ <button type="button" class="vocasync-mini-player__play-btn" aria-label={s.play} data-action="play-pause">
309
+ <svg class="vocasync-icon vocasync-icon--play" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
310
+ <path d="M6 4l15 8-15 8V4z" />
164
311
  </svg>
165
- <svg class="vocasync-icon vocasync-icon--highlight-off vocasync-hidden" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
166
- <path d="M12 3v18" opacity="0.3"/>
167
- <rect x="4" y="8" width="16" height="8" rx="1" opacity="0.3"/>
168
- <path d="M4 4l16 16"/>
312
+ <svg class="vocasync-icon vocasync-icon--pause vocasync-hidden" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
313
+ <path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z" />
169
314
  </svg>
170
315
  </button>
171
-
172
- <!-- Speed control -->
173
- <div class="vocasync-player__speed">
174
- <button
175
- type="button"
176
- class="vocasync-player__speed-btn"
177
- aria-label="Playback speed"
178
- data-action="speed"
179
- >
180
- <span data-speed-label>1x</span>
181
- </button>
182
- <div class="vocasync-player__speed-menu" hidden>
183
- <button type="button" data-speed="0.5">0.5x</button>
184
- <button type="button" data-speed="0.75">0.75x</button>
185
- <button type="button" data-speed="1" data-active>1x</button>
186
- <button type="button" data-speed="1.25">1.25x</button>
187
- <button type="button" data-speed="1.5">1.5x</button>
188
- <button type="button" data-speed="2">2x</button>
189
- </div>
316
+ <div class="vocasync-mini-player__progress">
317
+ <div class="vocasync-mini-player__progress-bar" data-progress></div>
190
318
  </div>
191
-
192
- <!-- Volume control -->
193
- <div class="vocasync-player__volume">
194
- <button
195
- type="button"
196
- class="vocasync-player__icon-btn"
197
- aria-label="Toggle mute"
198
- data-action="mute"
199
- title="Toggle mute (M)"
200
- >
201
- <svg class="vocasync-icon vocasync-icon--volume-high" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
202
- <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" fill="currentColor"/>
203
- <path d="M15.54 8.46a5 5 0 0 1 0 7.07"/>
204
- <path d="M19.07 4.93a10 10 0 0 1 0 14.14"/>
205
- </svg>
206
- <svg class="vocasync-icon vocasync-icon--volume-mute vocasync-hidden" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
207
- <polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5" fill="currentColor" opacity="0.5"/>
208
- <line x1="23" y1="9" x2="17" y2="15"/>
209
- <line x1="17" y1="9" x2="23" y2="15"/>
319
+ <span class="vocasync-mini-player__time" data-time="current">0:00</span>
320
+ <button type="button" class="vocasync-mini-player__close-btn" aria-label={s.retry} data-action="close-mini">
321
+ <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
322
+ <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
210
323
  </svg>
211
324
  </button>
212
- <input
213
- type="range"
214
- class="vocasync-player__volume-slider"
215
- min="0"
216
- max="100"
217
- value="100"
218
- aria-label="Volume"
219
- data-action="volume"
220
- />
221
- </div>
222
- </div>
223
- </div>
224
-
225
- <!-- Mini player (floating) -->
226
- {enableMiniPlayer && (
227
- <div class="vocasync-mini-player" id={`${playerId}-mini`} data-mini-player hidden>
228
- <button
229
- type="button"
230
- class="vocasync-mini-player__play-btn"
231
- aria-label="Play"
232
- data-action="play-pause"
233
- >
234
- <svg class="vocasync-icon vocasync-icon--play" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
235
- <path d="M6 4l15 8-15 8V4z"/>
236
- </svg>
237
- <svg class="vocasync-icon vocasync-icon--pause vocasync-hidden" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
238
- <path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z"/>
239
- </svg>
240
- </button>
241
- <div class="vocasync-mini-player__progress">
242
- <div class="vocasync-mini-player__progress-bar" data-progress></div>
243
325
  </div>
244
- <span class="vocasync-mini-player__time" data-time="current">0:00</span>
245
- <button
246
- type="button"
247
- class="vocasync-mini-player__close-btn"
248
- aria-label="Close mini player"
249
- data-action="close-mini"
250
- >
251
- <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
252
- <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
253
- </svg>
254
- </button>
255
- </div>
256
326
  )}
257
327
  </div>
258
328
 
259
- <style>
260
- /* ========================================
261
- * MAIN PLAYER STYLES
262
- * ======================================== */
263
-
264
- .vocasync-player {
265
- display: flex;
266
- flex-direction: column;
267
- gap: var(--vocasync-player-gap);
268
- padding: var(--vocasync-player-padding);
269
- background: var(--vocasync-surface);
270
- border: 1px solid var(--vocasync-border);
271
- border-radius: var(--vocasync-player-radius);
272
- box-shadow: var(--vocasync-shadow-sm);
273
- }
274
-
275
- .vocasync-player:focus-visible {
276
- outline: 2px solid var(--vocasync-border-focus);
277
- outline-offset: 2px;
278
- }
279
-
280
- .vocasync-player__loading,
281
- .vocasync-player__placeholder {
282
- display: flex;
283
- align-items: center;
284
- justify-content: center;
285
- gap: 0.5rem;
286
- padding: 0.75rem;
287
- color: var(--vocasync-text-muted);
288
- font-size: 0.875rem;
289
- }
290
-
291
- /* Hidden state for loading/placeholder/controls - overrides display: flex */
292
- .vocasync-player__loading[hidden],
293
- .vocasync-player__placeholder[hidden],
294
- .vocasync-player__controls[hidden] {
295
- display: none !important;
296
- }
297
-
298
- .vocasync-player__spinner {
299
- width: 1.25rem;
300
- height: 1.25rem;
301
- border: 2px solid var(--vocasync-border);
302
- border-top-color: var(--vocasync-primary);
303
- border-radius: 50%;
304
- animation: vocasync-spin 0.75s linear infinite;
305
- }
306
-
307
- @keyframes vocasync-spin {
308
- to { transform: rotate(360deg); }
309
- }
310
-
311
- .vocasync-player__controls {
312
- display: flex;
313
- align-items: center;
314
- gap: var(--vocasync-player-gap);
315
- }
316
-
317
- /* Play button */
318
- .vocasync-player__play-btn {
319
- display: flex;
320
- align-items: center;
321
- justify-content: center;
322
- width: var(--vocasync-button-size);
323
- height: var(--vocasync-button-size);
324
- flex-shrink: 0;
325
- background: var(--vocasync-primary);
326
- color: var(--vocasync-primary-content);
327
- border: none;
328
- border-radius: var(--vocasync-button-radius);
329
- cursor: pointer;
330
- transition: background-color var(--vocasync-transition-fast);
331
- }
332
-
333
- .vocasync-player__play-btn:hover {
334
- background: var(--vocasync-primary-hover);
335
- }
336
-
337
- .vocasync-player__play-btn:focus-visible {
338
- outline: 2px solid var(--vocasync-border-focus);
339
- outline-offset: 2px;
340
- }
341
-
342
- .vocasync-icon {
343
- width: 1.25rem;
344
- height: 1.25rem;
345
- }
346
-
347
- .vocasync-hidden {
348
- display: none !important;
349
- }
350
-
351
- /* Progress section */
352
- .vocasync-player__progress-section {
353
- display: flex;
354
- flex-direction: column;
355
- gap: 0.25rem;
356
- flex: 1;
357
- min-width: 0;
358
- }
359
-
360
- .vocasync-player__time {
361
- display: flex;
362
- gap: 0.25rem;
363
- font-size: 0.75rem;
364
- font-variant-numeric: tabular-nums;
365
- color: var(--vocasync-text-muted);
366
- }
367
-
368
- .vocasync-player__time-sep {
369
- color: var(--vocasync-text-faint);
370
- }
371
-
372
- /* Range input (progress & volume) */
373
- .vocasync-player__progress,
374
- .vocasync-player__volume-slider {
375
- -webkit-appearance: none;
376
- appearance: none;
377
- width: 100%;
378
- height: var(--vocasync-slider-height);
379
- background: var(--vocasync-border);
380
- border-radius: var(--vocasync-slider-radius);
381
- cursor: pointer;
382
- }
383
-
384
- .vocasync-player__progress::-webkit-slider-thumb,
385
- .vocasync-player__volume-slider::-webkit-slider-thumb {
386
- -webkit-appearance: none;
387
- appearance: none;
388
- width: var(--vocasync-slider-thumb-size);
389
- height: var(--vocasync-slider-thumb-size);
390
- background: var(--vocasync-primary);
391
- border-radius: 50%;
392
- border: none;
393
- box-shadow: var(--vocasync-shadow);
394
- transition: transform var(--vocasync-transition-fast);
395
- }
396
-
397
- .vocasync-player__progress::-webkit-slider-thumb:hover,
398
- .vocasync-player__volume-slider::-webkit-slider-thumb:hover {
399
- transform: scale(1.1);
400
- }
401
-
402
- .vocasync-player__progress::-moz-range-thumb,
403
- .vocasync-player__volume-slider::-moz-range-thumb {
404
- width: var(--vocasync-slider-thumb-size);
405
- height: var(--vocasync-slider-thumb-size);
406
- background: var(--vocasync-primary);
407
- border-radius: 50%;
408
- border: none;
409
- box-shadow: var(--vocasync-shadow);
410
- }
411
-
412
- /* Button group */
413
- .vocasync-player__button-group {
414
- display: flex;
415
- align-items: center;
416
- gap: 0.25rem;
417
- }
418
-
419
- /* Icon buttons (highlight toggle, mute) */
420
- .vocasync-player__icon-btn {
421
- display: flex;
422
- align-items: center;
423
- justify-content: center;
424
- width: 2rem;
425
- height: 2rem;
426
- color: var(--vocasync-text-muted);
427
- background: transparent;
428
- border: none;
429
- border-radius: 0.375rem;
430
- cursor: pointer;
431
- transition: color var(--vocasync-transition-fast), background-color var(--vocasync-transition-fast);
432
- }
433
-
434
- .vocasync-player__icon-btn:hover {
435
- color: var(--vocasync-text);
436
- background: var(--vocasync-border);
437
- }
438
-
439
- .vocasync-player__icon-btn:focus-visible {
440
- outline: 2px solid var(--vocasync-border-focus);
441
- outline-offset: 2px;
442
- }
443
-
444
- .vocasync-player__icon-btn[data-highlighting="off"] {
445
- color: var(--vocasync-text-faint);
446
- }
447
-
448
- /* Speed control */
449
- .vocasync-player__speed {
450
- position: relative;
451
- }
452
-
453
- .vocasync-player__speed-btn {
454
- display: flex;
455
- align-items: center;
456
- justify-content: center;
457
- min-width: 2.5rem;
458
- height: 1.75rem;
459
- padding: 0 0.5rem;
460
- font-size: 0.75rem;
461
- font-weight: 500;
462
- color: var(--vocasync-text-muted);
463
- background: var(--vocasync-surface-elevated);
464
- border: 1px solid var(--vocasync-border);
465
- border-radius: 0.375rem;
466
- cursor: pointer;
467
- transition: all var(--vocasync-transition-fast);
468
- }
469
-
470
- .vocasync-player__speed-btn:hover {
471
- color: var(--vocasync-text);
472
- border-color: var(--vocasync-primary);
473
- }
474
-
475
- .vocasync-player__speed-menu {
476
- position: absolute;
477
- bottom: 100%;
478
- left: 50%;
479
- transform: translateX(-50%);
480
- display: flex;
481
- flex-direction: column;
482
- gap: 0.125rem;
483
- padding: 0.25rem;
484
- margin-bottom: 0.5rem;
485
- background: var(--vocasync-surface-elevated);
486
- border: 1px solid var(--vocasync-border);
487
- border-radius: 0.5rem;
488
- box-shadow: var(--vocasync-shadow-lg);
489
- z-index: 10;
490
- }
491
-
492
- .vocasync-player__speed-menu[hidden] {
493
- display: none !important;
494
- }
495
-
496
- .vocasync-player__speed-menu button {
497
- padding: 0.375rem 0.75rem;
498
- font-size: 0.75rem;
499
- color: var(--vocasync-text-muted);
500
- background: transparent;
501
- border: none;
502
- border-radius: 0.25rem;
503
- cursor: pointer;
504
- white-space: nowrap;
505
- transition: all var(--vocasync-transition-fast);
506
- }
507
-
508
- .vocasync-player__speed-menu button:hover {
509
- background: var(--vocasync-border);
510
- color: var(--vocasync-text);
511
- }
512
-
513
- .vocasync-player__speed-menu button[data-active] {
514
- background: var(--vocasync-primary);
515
- color: var(--vocasync-primary-content);
516
- }
517
-
518
- /* Volume control */
519
- .vocasync-player__volume {
520
- display: flex;
521
- align-items: center;
522
- gap: 0.25rem;
523
- }
524
-
525
- .vocasync-player__volume-slider {
526
- width: 4rem;
527
- }
528
-
529
- /* Hide volume slider on mobile */
530
- @media (max-width: 640px) {
531
- .vocasync-player__volume-slider {
532
- display: none;
533
- }
534
- }
535
-
536
- /* ========================================
537
- * MINI PLAYER STYLES
538
- * ======================================== */
539
-
540
- .vocasync-mini-player {
541
- position: fixed;
542
- bottom: var(--vocasync-mini-offset);
543
- left: var(--vocasync-mini-offset);
544
- right: var(--vocasync-mini-offset);
545
- display: flex;
546
- align-items: center;
547
- gap: 0.75rem;
548
- height: var(--vocasync-mini-height);
549
- padding: 0 1rem;
550
- background: var(--vocasync-surface-elevated);
551
- border: 1px solid var(--vocasync-border);
552
- border-radius: 9999px;
553
- box-shadow: var(--vocasync-shadow-lg);
554
- z-index: 1000;
555
- transition: transform var(--vocasync-transition), opacity var(--vocasync-transition);
556
- }
557
-
558
- .vocasync-mini-player[hidden] {
559
- display: flex; /* Override hidden for animation */
560
- opacity: 0;
561
- transform: translateY(100%);
562
- pointer-events: none;
563
- }
564
-
565
- .vocasync-mini-player:not([hidden]) {
566
- opacity: 1;
567
- transform: translateY(0);
568
- }
569
-
570
- .vocasync-mini-player__play-btn {
571
- display: flex;
572
- align-items: center;
573
- justify-content: center;
574
- width: 2rem;
575
- height: 2rem;
576
- flex-shrink: 0;
577
- background: var(--vocasync-primary);
578
- color: var(--vocasync-primary-content);
579
- border: none;
580
- border-radius: 50%;
581
- cursor: pointer;
582
- }
583
-
584
- .vocasync-mini-player__play-btn .vocasync-icon {
585
- width: 1rem;
586
- height: 1rem;
587
- }
588
-
589
- .vocasync-mini-player__progress {
590
- flex: 1;
591
- height: 0.25rem;
592
- background: var(--vocasync-border);
593
- border-radius: 9999px;
594
- overflow: hidden;
595
- }
596
-
597
- .vocasync-mini-player__progress-bar {
598
- height: 100%;
599
- background: var(--vocasync-primary);
600
- border-radius: 9999px;
601
- transition: width 100ms linear;
602
- }
603
-
604
- .vocasync-mini-player__time {
605
- font-size: 0.75rem;
606
- font-variant-numeric: tabular-nums;
607
- color: var(--vocasync-text-muted);
608
- min-width: 2.5rem;
609
- }
610
-
611
- .vocasync-mini-player__close-btn {
612
- display: flex;
613
- align-items: center;
614
- justify-content: center;
615
- width: 1.5rem;
616
- height: 1.5rem;
617
- color: var(--vocasync-text-faint);
618
- background: transparent;
619
- border: none;
620
- border-radius: 50%;
621
- cursor: pointer;
622
- transition: color var(--vocasync-transition-fast);
623
- }
624
-
625
- .vocasync-mini-player__close-btn:hover {
626
- color: var(--vocasync-text);
627
- }
628
-
629
- .vocasync-mini-player__close-btn svg {
630
- width: 1rem;
631
- height: 1rem;
632
- }
633
-
634
- /* Desktop: constrain mini player width */
635
- @media (min-width: 640px) {
636
- .vocasync-mini-player {
637
- left: auto;
638
- right: var(--vocasync-mini-offset);
639
- max-width: 20rem;
640
- }
641
- }
642
-
643
- /* Click-to-seek: show a pointer cursor on words when the feature is on. */
644
- :global(.vocasync-click-seek .vocasync-word) {
645
- cursor: pointer;
646
- }
647
- </style>
329
+ <script>
330
+ // A hoisted module script: bundled once per page by Vite, typechecked, and shared by
331
+ // every player on the page. `define:vars` inlined ~530 lines per instance and could
332
+ // not be checked at all.
333
+ import { install } from "../player-core/index.js";
648
334
 
649
- <script define:vars={{ playerId, audioId, slug, articleSelector, enableHighlighting, enableMiniPlayer, trailLength }}>
650
- // Self-invoking function to avoid global scope pollution
651
- (function() {
652
- const player = document.getElementById(playerId);
653
- const audio = document.getElementById(audioId);
654
- const miniPlayer = enableMiniPlayer ? document.getElementById(`${playerId}-mini`) : null;
655
-
656
- if (!player || !audio) return;
657
-
658
- // State
659
- let isPlaying = false;
660
- let isMiniVisible = false;
661
- let animationFrame = null;
662
- let miniDismissed = false;
663
- let highlightingEnabled = enableHighlighting;
664
-
665
- // DOM elements
666
- const loadingEl = player.querySelector('[data-state="loading"]');
667
- const placeholderEl = player.querySelector('[data-state="no-audio"]');
668
- const controlsEl = player.querySelector('[data-state="ready"]');
669
- const playBtn = player.querySelector('[data-action="play-pause"]');
670
- const progressInput = player.querySelector('[data-action="seek"]');
671
- const volumeInput = player.querySelector('[data-action="volume"]');
672
- const muteBtn = player.querySelector('[data-action="mute"]');
673
- const speedBtn = player.querySelector('[data-action="speed"]');
674
- const speedMenu = player.querySelector('.vocasync-player__speed-menu');
675
- const highlightBtn = player.querySelector('[data-action="toggle-highlighting"]');
676
- const currentTimeEl = player.querySelector('[data-time="current"]');
677
- const durationTimeEl = player.querySelector('[data-time="duration"]');
678
-
679
- // Format time as M:SS
680
- function formatTime(seconds) {
681
- if (!seconds || !isFinite(seconds)) return '0:00';
682
- const m = Math.floor(seconds / 60);
683
- const s = Math.floor(seconds % 60);
684
- return `${m}:${s.toString().padStart(2, '0')}`;
685
- }
686
-
687
- // Update play/pause button icon
688
- function updatePlayButton(playing) {
689
- // Update main player play button
690
- if (playBtn) {
691
- const playIcon = playBtn.querySelector('.vocasync-icon--play');
692
- const pauseIcon = playBtn.querySelector('.vocasync-icon--pause');
693
- if (playIcon && pauseIcon) {
694
- playIcon.classList.toggle('vocasync-hidden', playing);
695
- pauseIcon.classList.toggle('vocasync-hidden', !playing);
696
- }
697
- playBtn.setAttribute('aria-label', playing ? 'Pause' : 'Play');
698
- }
699
-
700
- // Update mini player too
701
- if (miniPlayer) {
702
- const miniPlayBtn = miniPlayer.querySelector('[data-action="play-pause"]');
703
- if (miniPlayBtn) {
704
- const miniPlayIcon = miniPlayBtn.querySelector('.vocasync-icon--play');
705
- const miniPauseIcon = miniPlayBtn.querySelector('.vocasync-icon--pause');
706
- if (miniPlayIcon && miniPauseIcon) {
707
- miniPlayIcon.classList.toggle('vocasync-hidden', playing);
708
- miniPauseIcon.classList.toggle('vocasync-hidden', !playing);
709
- }
710
- }
711
- }
712
- }
713
-
714
- // Update volume icon
715
- function updateVolumeIcon(muted) {
716
- if (muteBtn) {
717
- const highIcon = muteBtn.querySelector('.vocasync-icon--volume-high');
718
- const muteIcon = muteBtn.querySelector('.vocasync-icon--volume-mute');
719
- if (highIcon && muteIcon) {
720
- highIcon.classList.toggle('vocasync-hidden', muted);
721
- muteIcon.classList.toggle('vocasync-hidden', !muted);
722
- }
723
- }
724
- }
725
-
726
- // Update highlighting toggle button
727
- function updateHighlightButton() {
728
- if (!highlightBtn) return;
729
- const onIcon = highlightBtn.querySelector('.vocasync-icon--highlight-on');
730
- const offIcon = highlightBtn.querySelector('.vocasync-icon--highlight-off');
731
- if (onIcon && offIcon) {
732
- onIcon.classList.toggle('vocasync-hidden', !highlightingEnabled);
733
- offIcon.classList.toggle('vocasync-hidden', highlightingEnabled);
734
- }
735
- highlightBtn.setAttribute('data-highlighting', highlightingEnabled ? 'on' : 'off');
736
- highlightBtn.setAttribute('aria-label', highlightingEnabled ? 'Disable word highlighting' : 'Enable word highlighting');
737
- }
738
-
739
- // Word highlighting state
740
- const EPS = 0.0001; // Epsilon for floating-point comparisons
741
- const PARAGRAPH_SELECTOR = 'p, li, blockquote, h1, h2, h3, h4, h5, h6, td, th';
742
- let words = []; // alignment timings, indexed by data-i
743
- let spanTimings = []; // sorted [{ start, end, el }]
744
- let activeEl = null;
745
- let trailStart = 0;
746
- let trailEnd = 0;
747
- let lastScrolledPara = null;
748
-
749
- // Build per-span timings from alignment words + each span's data-i/data-n.
750
- // A visible word/math unit spanning tokens [i, i+n) takes min(start)..max(end).
751
- function initWordHighlighting() {
752
- const article = document.querySelector(articleSelector);
753
- if (!article || !words.length) return false;
754
- const spans = Array.from(article.querySelectorAll('.vocasync-word[data-i]'));
755
- if (!spans.length) return false;
756
-
757
- const out = [];
758
- for (const el of spans) {
759
- const dataI = parseInt(el.dataset.i || '', 10);
760
- const dataN = parseInt(el.dataset.n || '', 10);
761
- if (!Number.isFinite(dataI) || !Number.isFinite(dataN) || dataN < 1) continue;
762
- let minStart = Infinity, maxEnd = -Infinity;
763
- for (let k = dataI; k < dataI + dataN; k++) {
764
- const t = words[k];
765
- if (!t) continue;
766
- if (t.start < minStart) minStart = t.start;
767
- if (t.end > maxEnd) maxEnd = t.end;
768
- }
769
- if (Number.isFinite(minStart) && Number.isFinite(maxEnd)) {
770
- if (!(maxEnd >= minStart)) maxEnd = minStart + EPS;
771
- out.push({ start: minStart, end: maxEnd, el });
772
- }
773
- }
774
- out.sort((a, b) => a.start - b.start);
775
- spanTimings = out;
776
- return spanTimings.length > 0;
777
- }
778
-
779
- // Binary search: first span whose end > t (the candidate active span).
780
- function idxForTime(t) {
781
- const n = spanTimings.length;
782
- if (!n) return -1;
783
- let lo = 0, hi = n - 1;
784
- while (lo <= hi) {
785
- const mid = (lo + hi) >> 1;
786
- if (spanTimings[mid].end <= t) lo = mid + 1; else hi = mid - 1;
787
- }
788
- return lo;
789
- }
790
-
791
- function clearActive() {
792
- if (activeEl) { activeEl.classList.remove('is-active'); activeEl = null; }
793
- }
794
-
795
- function setActive(el) {
796
- if (activeEl === el) return;
797
- if (activeEl) activeEl.classList.remove('is-active');
798
- if (el) el.classList.add('is-active');
799
- activeEl = el;
800
- }
801
-
802
- function clearTrail() {
803
- for (let i = trailStart; i < trailEnd; i++) {
804
- if (spanTimings[i]) spanTimings[i].el.classList.remove('is-trail');
805
- }
806
- trailStart = 0;
807
- trailEnd = 0;
808
- }
809
-
810
- function clearHighlights() {
811
- clearActive();
812
- clearTrail();
813
- }
814
-
815
- // Update active word + differential trail + autoscroll for the current time.
816
- function updateHighlighting() {
817
- if (!spanTimings.length) return;
818
- const t = Number(audio.currentTime || 0);
819
- if (!Number.isFinite(t)) return;
820
- const lo = idxForTime(t + EPS);
821
- const entry = spanTimings[lo];
822
- const el = entry && t >= entry.start - EPS && t < entry.end + EPS ? entry.el : null;
823
-
824
- if (highlightingEnabled) setActive(el);
825
-
826
- // Autoscroll the active word's paragraph into view (only when it changes).
827
- if (highlightingEnabled && el) {
828
- const para = el.closest(PARAGRAPH_SELECTOR);
829
- if (para && para !== lastScrolledPara) {
830
- const reduced = window.matchMedia &&
831
- window.matchMedia('(prefers-reduced-motion: reduce)').matches;
832
- para.scrollIntoView({ behavior: reduced ? 'auto' : 'smooth', block: 'center' });
833
- lastScrolledPara = para;
834
- }
835
- }
836
-
837
- // Differential trail update (only add/remove what changed).
838
- if (highlightingEnabled) {
839
- const newEnd = lo;
840
- const newStart = Math.max(0, lo - trailLength);
841
- for (let i = trailStart; i < trailEnd; i++) {
842
- if ((i < newStart || i >= newEnd) && spanTimings[i]) {
843
- spanTimings[i].el.classList.remove('is-trail');
844
- }
845
- }
846
- for (let i = newStart; i < newEnd; i++) {
847
- if ((i < trailStart || i >= trailEnd) && spanTimings[i]) {
848
- spanTimings[i].el.classList.add('is-trail');
849
- }
850
- }
851
- trailStart = newStart;
852
- trailEnd = newEnd;
853
- }
854
- }
855
-
856
- // Animation loop for highlighting.
857
- function highlightTick() {
858
- if (!highlightingEnabled) return;
859
- updateHighlighting();
860
- if (!audio.paused && !audio.ended) {
861
- animationFrame = requestAnimationFrame(highlightTick);
862
- }
863
- }
864
-
865
- function startHighlightLoop() {
866
- cancelAnimationFrame(animationFrame); // Always cancel first to prevent stacking
867
- animationFrame = requestAnimationFrame(highlightTick);
868
- }
869
-
870
- function stopHighlightLoop() {
871
- cancelAnimationFrame(animationFrame);
872
- animationFrame = null;
873
- }
874
-
875
- function onSeeking() {
876
- if (highlightingEnabled) updateHighlighting();
877
- }
878
-
879
- function onSeeked() {
880
- if (highlightingEnabled) updateHighlighting();
881
- if (!audio.paused && !audio.ended) startHighlightLoop();
882
- }
883
-
884
- // Toggle highlighting on/off
885
- function toggleHighlighting() {
886
- highlightingEnabled = !highlightingEnabled;
887
- updateHighlightButton();
888
- if (!highlightingEnabled) {
889
- clearHighlights();
890
- } else {
891
- updateHighlighting();
892
- // Toggling off stops the rAF loop; restart it so highlighting resumes
893
- // following the audio when re-enabled mid-playback.
894
- if (!audio.paused && !audio.ended) {
895
- startHighlightLoop();
896
- }
897
- }
898
- }
899
-
900
- // Setup intersection observer for mini player
901
- function setupMiniPlayer() {
902
- if (!miniPlayer || !enableMiniPlayer) return;
903
-
904
- const observer = new IntersectionObserver(
905
- (entries) => {
906
- const [entry] = entries;
907
- const shouldShow = !entry.isIntersecting && isPlaying && !miniDismissed;
908
-
909
- if (shouldShow && !isMiniVisible) {
910
- miniPlayer.hidden = false;
911
- isMiniVisible = true;
912
- } else if (!shouldShow && isMiniVisible) {
913
- miniPlayer.hidden = true;
914
- isMiniVisible = false;
915
- }
916
- },
917
- { threshold: 0 }
918
- );
919
-
920
- observer.observe(player);
921
-
922
- // Mini player controls
923
- const miniPlayBtn = miniPlayer.querySelector('[data-action="play-pause"]');
924
- const miniCloseBtn = miniPlayer.querySelector('[data-action="close-mini"]');
925
-
926
- miniPlayBtn?.addEventListener('click', togglePlay);
927
- miniCloseBtn?.addEventListener('click', () => {
928
- miniDismissed = true;
929
- miniPlayer.hidden = true;
930
- isMiniVisible = false;
931
- });
932
- }
933
-
934
- // Update mini player progress
935
- function updateMiniProgress() {
936
- if (!miniPlayer) return;
937
-
938
- const progressBar = miniPlayer.querySelector('[data-progress]');
939
- const timeEl = miniPlayer.querySelector('[data-time="current"]');
940
-
941
- if (progressBar) {
942
- const percent = (audio.currentTime / audio.duration) * 100 || 0;
943
- progressBar.style.width = `${percent}%`;
944
- }
945
-
946
- if (timeEl) {
947
- timeEl.textContent = formatTime(audio.currentTime);
948
- }
949
- }
950
-
951
- // Document-level click-to-seek delegate (reads data-i -> word start time).
952
- function setupWordClickHandlers() {
953
- const clickEnabled = player.dataset.enableClickToSeek === 'true';
954
- if (!clickEnabled) return;
955
-
956
- const article = document.querySelector(articleSelector);
957
- if (article) article.classList.add('vocasync-click-seek');
958
-
959
- document.addEventListener('click', (e) => {
960
- const target = e.target;
961
- const span = target && target.closest ? target.closest('.vocasync-word[data-i]') : null;
962
- if (!span) return;
963
- // Don't hijack text selections.
964
- const sel = window.getSelection && window.getSelection();
965
- if (sel && sel.toString().length > 0) return;
966
- const dataI = parseInt(span.dataset.i || '', 10);
967
- if (!Number.isFinite(dataI)) return;
968
- const t = words[dataI];
969
- if (!t || typeof t.start !== 'number') return;
970
- audio.currentTime = t.start;
971
- if (audio.paused) audio.play();
972
- });
973
- }
974
-
975
- // Load audio map and initialize
976
- async function init() {
977
- try {
978
- // Get audio data from embedded data attribute
979
- const audioDataAttr = player.dataset.audioEntry;
980
-
981
- if (!audioDataAttr) {
982
- showNoAudio();
983
- return;
984
- }
985
-
986
- let entry;
987
- try {
988
- entry = JSON.parse(audioDataAttr);
989
- } catch {
990
- showNoAudio();
991
- return;
992
- }
993
-
994
- if (!entry || !entry.audioUrl) {
995
- showNoAudio();
996
- return;
997
- }
998
-
999
- // Set audio source (append publishable key if provided)
1000
- let audioSrc = entry.audioUrl;
1001
- if (entry.publishableKey) {
1002
- const url = new URL(entry.audioUrl);
1003
- url.searchParams.set('pk', entry.publishableKey);
1004
- audioSrc = url.toString();
1005
- }
1006
- audio.src = audioSrc;
1007
-
1008
- // Initialize word highlighting from embedded alignment timings + rehype spans.
1009
- // Defer to DOMContentLoaded since the content div comes after the player in the HTML.
1010
- if (enableHighlighting) {
1011
- words = Array.isArray(entry.words) ? entry.words : [];
1012
- const initHighlighting = () => {
1013
- initWordHighlighting();
1014
- setupWordClickHandlers();
1015
- };
1016
- if (document.readyState === 'loading') {
1017
- document.addEventListener('DOMContentLoaded', initHighlighting);
1018
- } else {
1019
- // DOM already loaded
1020
- initHighlighting();
1021
- }
1022
- }
1023
-
1024
- // Show controls
1025
- showReady();
1026
-
1027
- // Setup mini player
1028
- setupMiniPlayer();
1029
-
1030
- // Initialize highlight button state
1031
- updateHighlightButton();
1032
-
1033
- } catch (error) {
1034
- console.error('VocaSync: Failed to initialize player', error);
1035
- showNoAudio();
1036
- }
1037
- }
1038
-
1039
- function showLoading() {
1040
- loadingEl.hidden = false;
1041
- placeholderEl.hidden = true;
1042
- controlsEl.hidden = true;
1043
- }
1044
-
1045
- function showNoAudio() {
1046
- loadingEl.hidden = true;
1047
- placeholderEl.hidden = false;
1048
- controlsEl.hidden = true;
1049
- }
1050
-
1051
- function showReady() {
1052
- loadingEl.hidden = true;
1053
- placeholderEl.hidden = true;
1054
- controlsEl.hidden = false;
1055
- }
1056
-
1057
- function togglePlay() {
1058
- if (audio.paused) {
1059
- audio.play();
1060
- } else {
1061
- audio.pause();
1062
- }
1063
- }
1064
-
1065
- // Event listeners
1066
- playBtn?.addEventListener('click', togglePlay);
1067
- highlightBtn?.addEventListener('click', toggleHighlighting);
1068
-
1069
- audio.addEventListener('play', () => {
1070
- isPlaying = true;
1071
- updatePlayButton(true);
1072
- startHighlightLoop();
1073
- miniDismissed = false;
1074
- });
1075
-
1076
- audio.addEventListener('pause', () => {
1077
- isPlaying = false;
1078
- updatePlayButton(false);
1079
- stopHighlightLoop();
1080
- });
1081
-
1082
- audio.addEventListener('ended', () => {
1083
- isPlaying = false;
1084
- updatePlayButton(false);
1085
- stopHighlightLoop();
1086
- });
1087
-
1088
- // Handle seeking for smooth highlight updates
1089
- audio.addEventListener('seeking', onSeeking);
1090
- audio.addEventListener('seeked', onSeeked);
1091
-
1092
- audio.addEventListener('loadedmetadata', () => {
1093
- durationTimeEl.textContent = formatTime(audio.duration);
1094
- progressInput.max = audio.duration;
1095
- });
1096
-
1097
- audio.addEventListener('timeupdate', () => {
1098
- currentTimeEl.textContent = formatTime(audio.currentTime);
1099
- progressInput.value = audio.currentTime;
1100
- updateMiniProgress();
1101
- });
1102
-
1103
- progressInput?.addEventListener('input', () => {
1104
- audio.currentTime = parseFloat(progressInput.value);
1105
- });
1106
-
1107
- volumeInput?.addEventListener('input', () => {
1108
- audio.volume = parseFloat(volumeInput.value) / 100;
1109
- updateVolumeIcon(audio.volume === 0);
1110
- });
1111
-
1112
- muteBtn?.addEventListener('click', () => {
1113
- audio.muted = !audio.muted;
1114
- updateVolumeIcon(audio.muted);
1115
- });
1116
-
1117
- speedBtn?.addEventListener('click', () => {
1118
- speedMenu.hidden = !speedMenu.hidden;
1119
- });
1120
-
1121
- speedMenu?.addEventListener('click', (e) => {
1122
- const btn = e.target.closest('[data-speed]');
1123
- if (!btn) return;
1124
-
1125
- const speed = parseFloat(btn.dataset.speed);
1126
- audio.playbackRate = speed;
1127
-
1128
- // Update active state
1129
- speedMenu.querySelectorAll('[data-speed]').forEach(b => b.removeAttribute('data-active'));
1130
- btn.setAttribute('data-active', '');
1131
-
1132
- // Update label
1133
- const label = player.querySelector('[data-speed-label]');
1134
- if (label) label.textContent = `${speed}x`;
1135
-
1136
- speedMenu.hidden = true;
1137
- });
1138
-
1139
- // Close speed menu on outside click
1140
- document.addEventListener('click', (e) => {
1141
- if (!speedBtn?.contains(e.target) && !speedMenu?.contains(e.target)) {
1142
- if (speedMenu) speedMenu.hidden = true;
1143
- }
1144
- });
1145
-
1146
- // Keyboard shortcuts
1147
- player.addEventListener('keydown', (e) => {
1148
- // Don't handle if focus is on an input
1149
- if (e.target.tagName === 'INPUT') return;
1150
-
1151
- switch (e.code) {
1152
- case 'Space':
1153
- e.preventDefault();
1154
- togglePlay();
1155
- break;
1156
- case 'ArrowLeft':
1157
- e.preventDefault();
1158
- audio.currentTime = Math.max(0, audio.currentTime - 5);
1159
- break;
1160
- case 'ArrowRight':
1161
- e.preventDefault();
1162
- audio.currentTime = Math.min(audio.duration || 0, audio.currentTime + 5);
1163
- break;
1164
- case 'KeyM':
1165
- e.preventDefault();
1166
- audio.muted = !audio.muted;
1167
- updateVolumeIcon(audio.muted);
1168
- break;
1169
- case 'KeyH':
1170
- e.preventDefault();
1171
- toggleHighlighting();
1172
- break;
1173
- }
1174
- });
1175
-
1176
- // Initialize
1177
- init();
1178
- })();
335
+ install();
1179
336
  </script>