@waveform-playlist/annotations 13.0.1 → 14.0.0-next.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.mjs CHANGED
@@ -18,202 +18,13 @@ function serializeAeneas(annotation) {
18
18
  };
19
19
  }
20
20
 
21
- // src/components/Annotation.tsx
22
- import { useState } from "react";
23
- import styled from "styled-components";
24
- import { jsx, jsxs } from "react/jsx-runtime";
25
- var AnnotationOverlay = styled.div.attrs((props) => ({
26
- style: {
27
- left: `${props.$left}px`,
28
- width: `${props.$width}px`
29
- }
30
- }))`
31
- position: absolute;
32
- top: 0;
33
- background: ${(props) => props.$color};
34
- height: 100%;
35
- z-index: 10;
36
- pointer-events: auto;
37
- opacity: 0.3;
38
- border: 2px solid ${(props) => props.$color};
39
- border-radius: 4px;
40
- cursor: pointer;
41
-
42
- &:hover {
43
- opacity: 0.5;
44
- border-color: ${(props) => props.$color};
45
- }
46
- `;
47
- var AnnotationText = styled.div`
48
- position: absolute;
49
- bottom: 0;
50
- left: 0;
51
- right: 0;
52
- background: rgba(0, 0, 0, 0.7);
53
- color: white;
54
- padding: 4px 8px;
55
- font-size: 12px;
56
- line-height: 1.3;
57
- max-height: 60%;
58
- overflow: hidden;
59
- text-overflow: ellipsis;
60
- pointer-events: none;
61
- white-space: pre-wrap;
62
- word-break: break-word;
63
- `;
64
- var EditableText = styled.textarea`
65
- position: absolute;
66
- bottom: 0;
67
- left: 0;
68
- right: 0;
69
- background: rgba(0, 0, 0, 0.9);
70
- color: white;
71
- padding: 4px 8px;
72
- font-size: 12px;
73
- line-height: 1.3;
74
- max-height: 60%;
75
- overflow: auto;
76
- border: 1px solid #fff;
77
- resize: none;
78
- font-family: inherit;
79
-
80
- &:focus {
81
- outline: none;
82
- border-color: #4caf50;
83
- }
84
- `;
85
- var ControlsBar = styled.div`
86
- position: absolute;
87
- top: 0;
88
- left: 0;
89
- right: 0;
90
- background: rgba(0, 0, 0, 0.8);
91
- display: flex;
92
- gap: 4px;
93
- padding: 4px;
94
- justify-content: flex-start;
95
- align-items: center;
96
- `;
97
- var ControlButton = styled.button`
98
- background: transparent;
99
- border: 1px solid rgba(255, 255, 255, 0.5);
100
- color: white;
101
- padding: 4px 8px;
102
- font-size: 10px;
103
- cursor: pointer;
104
- border-radius: 3px;
105
- display: flex;
106
- align-items: center;
107
- justify-content: center;
108
- min-width: 24px;
109
- height: 24px;
110
-
111
- &:hover {
112
- background: rgba(255, 255, 255, 0.2);
113
- border-color: white;
114
- }
115
-
116
- &:active {
117
- background: rgba(255, 255, 255, 0.3);
118
- }
119
- `;
120
- var Annotation = ({
121
- annotation,
122
- index,
123
- allAnnotations,
124
- startPosition,
125
- endPosition,
126
- color = "#ff9800",
127
- editable = false,
128
- controls = [],
129
- onAnnotationUpdate,
130
- annotationListConfig,
131
- onClick
132
- }) => {
133
- const [isEditing, setIsEditing] = useState(false);
134
- const [editedText, setEditedText] = useState(annotation.lines.join("\n"));
135
- const width = Math.max(0, endPosition - startPosition);
136
- if (width <= 0) {
137
- return null;
138
- }
139
- const handleClick = () => {
140
- if (onClick) {
141
- onClick(annotation);
142
- }
143
- };
144
- const handleDoubleClick = () => {
145
- if (editable) {
146
- setIsEditing(true);
147
- }
148
- };
149
- const handleTextChange = (e) => {
150
- setEditedText(e.target.value);
151
- };
152
- const handleTextBlur = () => {
153
- setIsEditing(false);
154
- const newLines = editedText.split("\n");
155
- if (newLines.join("\n") !== annotation.lines.join("\n")) {
156
- const updatedAnnotations = [...allAnnotations];
157
- updatedAnnotations[index] = { ...annotation, lines: newLines };
158
- if (onAnnotationUpdate) {
159
- onAnnotationUpdate(updatedAnnotations);
160
- }
161
- }
162
- };
163
- const handleControlClick = (control) => {
164
- const annotationsCopy = [...allAnnotations];
165
- control.action(annotationsCopy[index], index, annotationsCopy, annotationListConfig || {});
166
- if (onAnnotationUpdate) {
167
- onAnnotationUpdate(annotationsCopy);
168
- }
169
- };
170
- const getIconClass = (classString) => {
171
- return classString.replace(/\./g, " ");
172
- };
173
- return /* @__PURE__ */ jsxs(
174
- AnnotationOverlay,
175
- {
176
- $left: startPosition,
177
- $width: width,
178
- $color: color,
179
- onClick: handleClick,
180
- onDoubleClick: handleDoubleClick,
181
- children: [
182
- controls.length > 0 && /* @__PURE__ */ jsx(ControlsBar, { children: controls.map((control, idx) => /* @__PURE__ */ jsx(
183
- ControlButton,
184
- {
185
- title: control.title,
186
- onClick: (e) => {
187
- e.stopPropagation();
188
- handleControlClick(control);
189
- },
190
- children: control.text ? control.text : /* @__PURE__ */ jsx("i", { className: getIconClass(control.class || "") })
191
- },
192
- idx
193
- )) }),
194
- isEditing ? /* @__PURE__ */ jsx(
195
- EditableText,
196
- {
197
- value: editedText,
198
- onChange: handleTextChange,
199
- onBlur: handleTextBlur,
200
- autoFocus: true,
201
- onClick: (e) => e.stopPropagation(),
202
- onDoubleClick: (e) => e.stopPropagation()
203
- }
204
- ) : /* @__PURE__ */ jsx(AnnotationText, { children: annotation.lines.join("\n") })
205
- ]
206
- }
207
- );
208
- };
209
-
210
21
  // src/components/AnnotationBox.tsx
211
- import styled2 from "styled-components";
22
+ import styled from "styled-components";
212
23
  import { useDraggable } from "@dnd-kit/react";
213
24
  import { Feedback } from "@dnd-kit/dom";
214
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
25
+ import { jsx, jsxs } from "react/jsx-runtime";
215
26
  var EDGE_HANDLE_PLUGINS = [Feedback.configure({ feedback: "none" })];
216
- var Wrapper = styled2.div.attrs((props) => ({
27
+ var Wrapper = styled.div.attrs((props) => ({
217
28
  style: {
218
29
  left: `${props.$left}px`,
219
30
  width: `${props.$width}px`
@@ -224,7 +35,7 @@ var Wrapper = styled2.div.attrs((props) => ({
224
35
  height: 100%;
225
36
  pointer-events: none; /* Let events pass through to children */
226
37
  `;
227
- var Box = styled2.div`
38
+ var Box = styled.div`
228
39
  position: absolute;
229
40
  top: 0;
230
41
  left: 0;
@@ -241,7 +52,11 @@ var Box = styled2.div`
241
52
  justify-content: center;
242
53
  overflow: hidden;
243
54
  transition: all 0.2s ease;
244
- box-shadow: ${(props) => props.$isActive ? "0 2px 8px rgba(255, 152, 0, 0.4), inset 0 0 0 1px rgba(255, 152, 0, 0.2)" : "0 1px 3px rgba(0, 0, 0, 0.1)"};
55
+ box-shadow: ${(props) => props.$isActive ? (
56
+ // The glow of the active box is the box colour (the theme's
57
+ // `annotationBoxBorder`, through the `color` prop) at 40% and 20%.
58
+ `0 2px 8px color-mix(in srgb, ${props.$color} 40%, transparent), inset 0 0 0 1px color-mix(in srgb, ${props.$color} 20%, transparent)`
59
+ ) : "0 1px 3px rgba(0, 0, 0, 0.1)"};
245
60
 
246
61
  &:hover {
247
62
  background: ${(props) => props.theme?.annotationBoxHoverBackground || "rgba(255, 255, 255, 0.98)"};
@@ -250,7 +65,7 @@ var Box = styled2.div`
250
65
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
251
66
  }
252
67
  `;
253
- var Label = styled2.span`
68
+ var Label = styled.span`
254
69
  font-size: 12px;
255
70
  font-weight: 600;
256
71
  color: ${(props) => props.theme?.annotationLabelColor || "#2a2a2a"};
@@ -261,7 +76,7 @@ var Label = styled2.span`
261
76
  letter-spacing: 0.3px;
262
77
  user-select: none;
263
78
  `;
264
- var ResizeHandle = styled2.div`
79
+ var ResizeHandle = styled.div`
265
80
  position: absolute;
266
81
  top: 0;
267
82
  ${(props) => props.$position === "left" ? "left: 0" : "right: 0"};
@@ -331,9 +146,9 @@ var AnnotationBox = ({
331
146
  const handleHandleClick = (e) => {
332
147
  e.stopPropagation();
333
148
  };
334
- return /* @__PURE__ */ jsxs2(Wrapper, { $left: startPosition, $width: width, children: [
335
- /* @__PURE__ */ jsx2(Box, { $color: color, $isActive: isActive, onClick, children: label && /* @__PURE__ */ jsx2(Label, { children: label }) }),
336
- editable && /* @__PURE__ */ jsx2(
149
+ return /* @__PURE__ */ jsxs(Wrapper, { $left: startPosition, $width: width, children: [
150
+ /* @__PURE__ */ jsx(Box, { $color: color, $isActive: isActive, onClick, children: label && /* @__PURE__ */ jsx(Label, { children: label }) }),
151
+ editable && /* @__PURE__ */ jsx(
337
152
  ResizeHandle,
338
153
  {
339
154
  ref: leftRef,
@@ -342,7 +157,7 @@ var AnnotationBox = ({
342
157
  onClick: handleHandleClick
343
158
  }
344
159
  ),
345
- editable && /* @__PURE__ */ jsx2(
160
+ editable && /* @__PURE__ */ jsx(
346
161
  ResizeHandle,
347
162
  {
348
163
  ref: rightRef,
@@ -355,19 +170,19 @@ var AnnotationBox = ({
355
170
  };
356
171
 
357
172
  // src/components/AnnotationBoxesWrapper.tsx
358
- import styled3 from "styled-components";
359
- import { jsx as jsx3 } from "react/jsx-runtime";
360
- var Container = styled3.div.attrs((props) => ({
173
+ import styled2 from "styled-components";
174
+ import { jsx as jsx2 } from "react/jsx-runtime";
175
+ var Container = styled2.div.attrs((props) => ({
361
176
  style: {
362
- height: `${props.$height}px`
177
+ height: `${props.$height}px`,
178
+ width: props.$width !== void 0 ? `${props.$width}px` : void 0
363
179
  }
364
180
  }))`
365
181
  position: relative;
366
- ${(props) => props.$width !== void 0 && `width: ${props.$width}px;`}
367
182
  background: transparent;
368
183
  z-index: 110;
369
184
  `;
370
- var BoxesContainer = styled3.div`
185
+ var BoxesContainer = styled2.div`
371
186
  position: relative;
372
187
  height: 100%;
373
188
  padding-left: ${(props) => props.$offset || 0}px;
@@ -379,78 +194,53 @@ var AnnotationBoxesWrapper = ({
379
194
  offset = 0,
380
195
  width
381
196
  }) => {
382
- return /* @__PURE__ */ jsx3(Container, { className, $height: height, $width: width, children: /* @__PURE__ */ jsx3(BoxesContainer, { $offset: offset, children }) });
383
- };
384
-
385
- // src/components/AnnotationsTrack.tsx
386
- import styled4 from "styled-components";
387
- import { jsx as jsx4 } from "react/jsx-runtime";
388
- var Container2 = styled4.div.attrs((props) => ({
389
- style: {
390
- height: `${props.$height}px`
391
- }
392
- }))`
393
- position: relative;
394
- ${(props) => props.$width !== void 0 && `width: ${props.$width}px;`}
395
- background: transparent;
396
- `;
397
- var AnnotationsContainer = styled4.div`
398
- position: relative;
399
- height: 100%;
400
- padding-left: ${(props) => props.$offset || 0}px;
401
- `;
402
- var AnnotationsTrack = ({
403
- children,
404
- className,
405
- height = 100,
406
- offset = 0,
407
- width
408
- }) => {
409
- return /* @__PURE__ */ jsx4(Container2, { className, $height: height, $width: width, children: /* @__PURE__ */ jsx4(AnnotationsContainer, { $offset: offset, children }) });
197
+ return /* @__PURE__ */ jsx2(Container, { className, $height: height, $width: width, children: /* @__PURE__ */ jsx2(BoxesContainer, { $offset: offset, children }) });
410
198
  };
411
199
 
412
200
  // src/components/AnnotationText.tsx
413
- import React2, { useRef, useEffect } from "react";
414
- import styled5 from "styled-components";
415
- import { jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
416
- var Container3 = styled5.div`
201
+ import React, { useCallback, useRef, useEffect } from "react";
202
+ import styled3 from "styled-components";
203
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
204
+ var activeColor = (props) => props.theme?.annotationTextItemActiveColor || "#ff9800";
205
+ var activeTint = (percent) => (props) => `color-mix(in srgb, ${activeColor(props)} ${percent}%, transparent)`;
206
+ var Container2 = styled3.div`
417
207
  background: ${(props) => props.theme?.backgroundColor || "#fff"};
418
208
  ${(props) => props.$height ? `height: ${props.$height}px;` : "max-height: 200px;"}
419
209
  overflow-y: auto;
420
210
  padding: 8px;
421
211
  `;
422
- var AnnotationItem = styled5.div`
212
+ var AnnotationItem = styled3.div`
423
213
  padding: 12px;
424
214
  margin-bottom: 6px;
425
- border-left: 4px solid ${(props) => props.$isActive ? "#ff9800" : "transparent"};
426
- background: ${(props) => props.$isActive ? "rgba(255, 152, 0, 0.15)" : "transparent"};
215
+ border-left: 4px solid ${(props) => props.$isActive ? activeColor(props) : "transparent"};
216
+ background: ${(props) => props.$isActive ? activeTint(15)(props) : "transparent"};
427
217
  border-radius: 4px;
428
218
  transition: all 0.2s;
429
219
  cursor: pointer;
430
- box-shadow: ${(props) => props.$isActive ? "0 2px 8px rgba(255, 152, 0, 0.25), inset 0 0 0 1px rgba(255, 152, 0, 0.3)" : "none"};
220
+ box-shadow: ${(props) => props.$isActive ? `0 2px 8px ${activeTint(25)(props)}, inset 0 0 0 1px ${activeTint(30)(props)}` : "none"};
431
221
 
432
222
  &:hover {
433
- background: ${(props) => props.$isActive ? "rgba(255, 152, 0, 0.2)" : props.theme?.annotationTextItemHoverBackground || "rgba(0, 0, 0, 0.05)"};
434
- border-left-color: ${(props) => props.$isActive ? "#ff9800" : props.theme?.borderColor || "#ddd"};
223
+ background: ${(props) => props.$isActive ? activeTint(20)(props) : props.theme?.annotationTextItemHoverBackground || "rgba(0, 0, 0, 0.05)"};
224
+ border-left-color: ${(props) => props.$isActive ? activeColor(props) : props.theme?.borderColor || "#ddd"};
435
225
  }
436
226
 
437
227
  &:focus-visible {
438
- outline: 2px solid #ff9800;
228
+ outline: 2px solid ${activeColor};
439
229
  outline-offset: 2px;
440
230
  }
441
231
  `;
442
- var AnnotationHeader = styled5.div`
232
+ var AnnotationHeader = styled3.div`
443
233
  display: flex;
444
234
  justify-content: space-between;
445
235
  align-items: center;
446
236
  margin-bottom: 6px;
447
237
  `;
448
- var AnnotationInfo = styled5.div`
238
+ var AnnotationInfo = styled3.div`
449
239
  display: flex;
450
240
  align-items: center;
451
241
  gap: 8px;
452
242
  `;
453
- var AnnotationIdLabel = styled5.span`
243
+ var AnnotationIdLabel = styled3.span`
454
244
  font-size: 11px;
455
245
  font-weight: 600;
456
246
  color: ${(props) => props.theme?.textColorMuted || "#666"};
@@ -461,22 +251,22 @@ var AnnotationIdLabel = styled5.span`
461
251
  outline: ${(props) => props.$isEditable ? `1px dashed ${props.theme?.borderColor || "#ddd"}` : "none"};
462
252
 
463
253
  &[contenteditable='true']:focus {
464
- outline: 2px solid #ff9800;
465
- background: rgba(255, 152, 0, 0.1);
254
+ outline: 2px solid ${activeColor};
255
+ background: ${activeTint(10)};
466
256
  }
467
257
  `;
468
- var TimeRange = styled5.span`
258
+ var TimeRange = styled3.span`
469
259
  font-size: 12px;
470
260
  font-weight: 500;
471
261
  color: ${(props) => props.theme?.textColorMuted || "#555"};
472
262
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
473
263
  letter-spacing: 0.5px;
474
264
  `;
475
- var AnnotationControls = styled5.div`
265
+ var AnnotationControls = styled3.div`
476
266
  display: flex;
477
267
  gap: 6px;
478
268
  `;
479
- var ControlButton2 = styled5.button`
269
+ var ControlButton = styled3.button`
480
270
  background: ${(props) => props.theme?.surfaceColor || "#f5f5f5"};
481
271
  border: 1px solid ${(props) => props.theme?.borderColor || "#ccc"};
482
272
  color: ${(props) => props.theme?.textColor || "#333"};
@@ -496,7 +286,7 @@ var ControlButton2 = styled5.button`
496
286
  transform: scale(0.95);
497
287
  }
498
288
  `;
499
- var AnnotationTextContent = styled5.div`
289
+ var AnnotationTextContent = styled3.div`
500
290
  font-size: 14px;
501
291
  line-height: 1.6;
502
292
  color: ${(props) => props.theme?.textColor || "#2a2a2a"};
@@ -508,10 +298,99 @@ var AnnotationTextContent = styled5.div`
508
298
  min-height: 20px;
509
299
 
510
300
  &[contenteditable='true']:focus {
511
- outline: 2px solid #ff9800;
512
- background: rgba(255, 152, 0, 0.1);
301
+ outline: 2px solid ${activeColor};
302
+ background: ${activeTint(10)};
513
303
  }
514
304
  `;
305
+ var NO_CONTROLS = [];
306
+ var formatTime = (seconds) => {
307
+ if (isNaN(seconds) || !isFinite(seconds)) {
308
+ return "0:00.000";
309
+ }
310
+ const mins = Math.floor(seconds / 60);
311
+ const secs = (seconds % 60).toFixed(3);
312
+ return `${mins}:${secs.padStart(6, "0")}`;
313
+ };
314
+ var getIconClass = (classString) => {
315
+ return classString.replace(/\./g, " ");
316
+ };
317
+ var blurOnEnter = (e) => {
318
+ if (e.key === "Enter") {
319
+ e.preventDefault();
320
+ e.currentTarget.blur();
321
+ }
322
+ };
323
+ var AnnotationListItem = React.memo(function AnnotationListItem2({
324
+ annotation,
325
+ index,
326
+ isActive,
327
+ itemRef,
328
+ editable,
329
+ controls,
330
+ onAnnotationClick,
331
+ onTextEdit,
332
+ onIdEdit,
333
+ onControlClick,
334
+ renderAnnotationItem
335
+ }) {
336
+ const handleClick = () => onAnnotationClick?.(annotation);
337
+ const handleRowClick = (event) => {
338
+ const target = event.target;
339
+ if (target?.closest?.('[contenteditable="true"]')) return;
340
+ handleClick();
341
+ };
342
+ if (renderAnnotationItem) {
343
+ return /* @__PURE__ */ jsx3("div", { ref: itemRef, children: renderAnnotationItem({
344
+ annotation,
345
+ index,
346
+ isActive,
347
+ onClick: handleClick,
348
+ formatTime
349
+ }) });
350
+ }
351
+ return /* @__PURE__ */ jsxs2(AnnotationItem, { ref: itemRef, $isActive: isActive, onClick: handleRowClick, children: [
352
+ /* @__PURE__ */ jsxs2(AnnotationHeader, { children: [
353
+ /* @__PURE__ */ jsxs2(AnnotationInfo, { children: [
354
+ /* @__PURE__ */ jsx3(
355
+ AnnotationIdLabel,
356
+ {
357
+ $isEditable: editable,
358
+ contentEditable: editable,
359
+ suppressContentEditableWarning: true,
360
+ onBlur: (e) => onIdEdit(index, e.currentTarget.textContent || ""),
361
+ onKeyDown: blurOnEnter,
362
+ children: annotation.id
363
+ }
364
+ ),
365
+ /* @__PURE__ */ jsxs2(TimeRange, { children: [
366
+ formatTime(annotation.start),
367
+ " - ",
368
+ formatTime(annotation.end)
369
+ ] })
370
+ ] }),
371
+ controls.length > 0 && /* @__PURE__ */ jsx3(AnnotationControls, { onClick: (e) => e.stopPropagation(), children: controls.map((control, idx) => /* @__PURE__ */ jsx3(
372
+ ControlButton,
373
+ {
374
+ title: control.title,
375
+ onClick: () => onControlClick(control, index),
376
+ children: control.text ? control.text : /* @__PURE__ */ jsx3("i", { className: getIconClass(control.class || "") })
377
+ },
378
+ idx
379
+ )) })
380
+ ] }),
381
+ /* @__PURE__ */ jsx3(
382
+ AnnotationTextContent,
383
+ {
384
+ $isEditable: editable,
385
+ contentEditable: editable,
386
+ suppressContentEditableWarning: true,
387
+ onBlur: (e) => onTextEdit(index, e.currentTarget.textContent || ""),
388
+ onKeyDown: blurOnEnter,
389
+ children: annotation.lines.join("\n")
390
+ }
391
+ )
392
+ ] });
393
+ });
515
394
  var AnnotationTextComponent = ({
516
395
  annotations,
517
396
  activeAnnotationId,
@@ -519,7 +398,7 @@ var AnnotationTextComponent = ({
519
398
  scrollActivePosition = "center",
520
399
  scrollActiveContainer = "nearest",
521
400
  editable = false,
522
- controls = [],
401
+ controls = NO_CONTROLS,
523
402
  annotationListConfig,
524
403
  height,
525
404
  onAnnotationClick,
@@ -527,18 +406,6 @@ var AnnotationTextComponent = ({
527
406
  renderAnnotationItem
528
407
  }) => {
529
408
  const activeAnnotationRef = useRef(null);
530
- const containerRef = useRef(null);
531
- const prevActiveIdRef = useRef(void 0);
532
- useEffect(() => {
533
- });
534
- useEffect(() => {
535
- const container = containerRef.current;
536
- if (!container) return;
537
- const handleScroll = () => {
538
- };
539
- container.addEventListener("scroll", handleScroll);
540
- return () => container.removeEventListener("scroll", handleScroll);
541
- }, []);
542
409
  useEffect(() => {
543
410
  if (activeAnnotationId && activeAnnotationRef.current && shouldScrollToActive) {
544
411
  activeAnnotationRef.current.scrollIntoView({
@@ -547,216 +414,149 @@ var AnnotationTextComponent = ({
547
414
  container: scrollActiveContainer
548
415
  });
549
416
  }
550
- prevActiveIdRef.current = activeAnnotationId;
551
417
  }, [activeAnnotationId, shouldScrollToActive, scrollActivePosition, scrollActiveContainer]);
552
- const formatTime = (seconds) => {
553
- if (isNaN(seconds) || !isFinite(seconds)) {
554
- return "0:00.000";
555
- }
556
- const mins = Math.floor(seconds / 60);
557
- const secs = (seconds % 60).toFixed(3);
558
- return `${mins}:${secs.padStart(6, "0")}`;
559
- };
560
- const handleTextEdit = (index, newText) => {
561
- if (!editable || !onAnnotationUpdate) return;
562
- const updatedAnnotations = [...annotations];
563
- updatedAnnotations[index] = {
564
- ...updatedAnnotations[index],
565
- lines: newText.split("\n")
566
- };
567
- onAnnotationUpdate(updatedAnnotations);
568
- };
569
- const handleIdEdit = (index, newId) => {
570
- if (!editable || !onAnnotationUpdate) return;
571
- const trimmedId = newId.trim();
572
- if (!trimmedId) return;
573
- const updatedAnnotations = [...annotations];
574
- updatedAnnotations[index] = {
575
- ...updatedAnnotations[index],
576
- id: trimmedId
577
- };
578
- onAnnotationUpdate(updatedAnnotations);
579
- };
580
- const handleControlClick = (control, annotation, index) => {
581
- if (!onAnnotationUpdate) return;
582
- const annotationsCopy = [...annotations];
583
- control.action(annotationsCopy[index], index, annotationsCopy, annotationListConfig || {});
584
- onAnnotationUpdate(annotationsCopy);
585
- };
586
- const getIconClass = (classString) => {
587
- return classString.replace(/\./g, " ");
588
- };
589
- return /* @__PURE__ */ jsx5(Container3, { ref: containerRef, $height: height, children: annotations.map((annotation, index) => {
418
+ const annotationsRef = useRef(annotations);
419
+ annotationsRef.current = annotations;
420
+ const annotationListConfigRef = useRef(annotationListConfig);
421
+ annotationListConfigRef.current = annotationListConfig;
422
+ const handleTextEdit = useCallback(
423
+ (index, newText) => {
424
+ if (!editable || !onAnnotationUpdate) return;
425
+ const updatedAnnotations = [...annotationsRef.current];
426
+ updatedAnnotations[index] = {
427
+ ...updatedAnnotations[index],
428
+ lines: newText.split("\n")
429
+ };
430
+ onAnnotationUpdate(updatedAnnotations);
431
+ },
432
+ [editable, onAnnotationUpdate]
433
+ );
434
+ const handleIdEdit = useCallback(
435
+ (index, newId) => {
436
+ if (!editable || !onAnnotationUpdate) return;
437
+ const trimmedId = newId.trim();
438
+ if (!trimmedId) return;
439
+ const updatedAnnotations = [...annotationsRef.current];
440
+ updatedAnnotations[index] = {
441
+ ...updatedAnnotations[index],
442
+ id: trimmedId
443
+ };
444
+ onAnnotationUpdate(updatedAnnotations);
445
+ },
446
+ [editable, onAnnotationUpdate]
447
+ );
448
+ const handleControlClick = useCallback(
449
+ (control, index) => {
450
+ if (!onAnnotationUpdate) return;
451
+ const annotationsCopy = [...annotationsRef.current];
452
+ control.action(
453
+ annotationsCopy[index],
454
+ index,
455
+ annotationsCopy,
456
+ annotationListConfigRef.current || {}
457
+ );
458
+ onAnnotationUpdate(annotationsCopy);
459
+ },
460
+ [onAnnotationUpdate]
461
+ );
462
+ return /* @__PURE__ */ jsx3(Container2, { $height: height, children: annotations.map((annotation, index) => {
590
463
  const isActive = annotation.id === activeAnnotationId;
591
- const handleClick = () => onAnnotationClick?.(annotation);
592
- if (renderAnnotationItem) {
593
- return /* @__PURE__ */ jsx5("div", { ref: isActive ? activeAnnotationRef : null, children: renderAnnotationItem({
464
+ return /* @__PURE__ */ jsx3(
465
+ AnnotationListItem,
466
+ {
594
467
  annotation,
595
468
  index,
596
469
  isActive,
597
- onClick: handleClick,
598
- formatTime
599
- }) }, annotation.id);
600
- }
601
- return /* @__PURE__ */ jsxs3(
602
- AnnotationItem,
603
- {
604
- ref: isActive ? activeAnnotationRef : null,
605
- $isActive: isActive,
606
- onClick: handleClick,
607
- children: [
608
- /* @__PURE__ */ jsxs3(AnnotationHeader, { children: [
609
- /* @__PURE__ */ jsxs3(AnnotationInfo, { children: [
610
- /* @__PURE__ */ jsx5(
611
- AnnotationIdLabel,
612
- {
613
- $isEditable: editable,
614
- contentEditable: editable,
615
- suppressContentEditableWarning: true,
616
- onBlur: (e) => handleIdEdit(index, e.currentTarget.textContent || ""),
617
- onKeyDown: (e) => {
618
- if (e.key === "Enter") {
619
- e.preventDefault();
620
- e.currentTarget.blur();
621
- }
622
- },
623
- children: annotation.id
624
- }
625
- ),
626
- /* @__PURE__ */ jsxs3(TimeRange, { children: [
627
- formatTime(annotation.start),
628
- " - ",
629
- formatTime(annotation.end)
630
- ] })
631
- ] }),
632
- controls.length > 0 && /* @__PURE__ */ jsx5(AnnotationControls, { onClick: (e) => e.stopPropagation(), children: controls.map((control, idx) => /* @__PURE__ */ jsx5(
633
- ControlButton2,
634
- {
635
- title: control.title,
636
- onClick: () => handleControlClick(control, annotation, index),
637
- children: control.text ? control.text : /* @__PURE__ */ jsx5("i", { className: getIconClass(control.class || "") })
638
- },
639
- idx
640
- )) })
641
- ] }),
642
- /* @__PURE__ */ jsx5(
643
- AnnotationTextContent,
644
- {
645
- $isEditable: editable,
646
- contentEditable: editable,
647
- suppressContentEditableWarning: true,
648
- onBlur: (e) => handleTextEdit(index, e.currentTarget.textContent || ""),
649
- onKeyDown: (e) => {
650
- if (e.key === "Enter") {
651
- e.preventDefault();
652
- e.currentTarget.blur();
653
- }
654
- },
655
- children: annotation.lines.join("\n")
656
- }
657
- )
658
- ]
470
+ itemRef: isActive ? activeAnnotationRef : void 0,
471
+ editable,
472
+ controls,
473
+ onAnnotationClick,
474
+ onTextEdit: handleTextEdit,
475
+ onIdEdit: handleIdEdit,
476
+ onControlClick: handleControlClick,
477
+ renderAnnotationItem
659
478
  },
660
479
  annotation.id
661
480
  );
662
481
  }) });
663
482
  };
664
- var AnnotationText2 = React2.memo(AnnotationTextComponent);
483
+ var AnnotationText = React.memo(AnnotationTextComponent);
665
484
 
666
- // src/components/ContinuousPlayCheckbox.tsx
485
+ // src/components/AnnotationCheckboxes.tsx
486
+ import { useId } from "react";
667
487
  import {
668
488
  BaseCheckboxWrapper,
669
489
  BaseCheckbox,
670
490
  BaseCheckboxLabel
671
491
  } from "@waveform-playlist/ui-components";
672
- import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
673
- var ContinuousPlayCheckbox = ({
492
+ import { useAnnotationOptions, usePlaybackControls } from "@waveform-playlist/browser";
493
+ import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
494
+ var OptionCheckbox = ({
674
495
  checked,
675
496
  onChange,
676
- disabled = false,
677
- className
497
+ inputClassName,
498
+ className,
499
+ disabled,
500
+ children
678
501
  }) => {
679
- const handleChange = (e) => {
680
- onChange(e.target.checked);
681
- };
682
- return /* @__PURE__ */ jsxs4(BaseCheckboxWrapper, { className, children: [
683
- /* @__PURE__ */ jsx6(
502
+ const id = useId();
503
+ return /* @__PURE__ */ jsxs3(BaseCheckboxWrapper, { className, children: [
504
+ /* @__PURE__ */ jsx4(
684
505
  BaseCheckbox,
685
506
  {
686
507
  type: "checkbox",
687
- id: "continuous-play",
688
- className: "continuous-play",
508
+ id,
509
+ className: inputClassName,
689
510
  checked,
690
- onChange: handleChange,
511
+ onChange: (e) => onChange(e.target.checked),
691
512
  disabled
692
513
  }
693
514
  ),
694
- /* @__PURE__ */ jsx6(BaseCheckboxLabel, { htmlFor: "continuous-play", children: "Continuous Play" })
515
+ /* @__PURE__ */ jsx4(BaseCheckboxLabel, { htmlFor: id, children })
695
516
  ] });
696
517
  };
697
-
698
- // src/components/LinkEndpointsCheckbox.tsx
699
- import {
700
- BaseCheckboxWrapper as BaseCheckboxWrapper2,
701
- BaseCheckbox as BaseCheckbox2,
702
- BaseCheckboxLabel as BaseCheckboxLabel2
703
- } from "@waveform-playlist/ui-components";
704
- import { jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
705
- var LinkEndpointsCheckbox = ({
706
- checked,
707
- onChange,
708
- disabled = false,
709
- className
518
+ var ContinuousPlayCheckbox = ({
519
+ children,
520
+ ...rest
710
521
  }) => {
711
- const handleChange = (e) => {
712
- onChange(e.target.checked);
713
- };
714
- return /* @__PURE__ */ jsxs5(BaseCheckboxWrapper2, { className, children: [
715
- /* @__PURE__ */ jsx7(
716
- BaseCheckbox2,
717
- {
718
- type: "checkbox",
719
- id: "link-endpoints",
720
- className: "link-endpoints",
721
- checked,
722
- onChange: handleChange,
723
- disabled
724
- }
725
- ),
726
- /* @__PURE__ */ jsx7(BaseCheckboxLabel2, { htmlFor: "link-endpoints", children: "Link Endpoints" })
727
- ] });
522
+ const { continuousPlay } = useAnnotationOptions();
523
+ const { setContinuousPlay } = usePlaybackControls();
524
+ return /* @__PURE__ */ jsx4(
525
+ OptionCheckbox,
526
+ {
527
+ ...rest,
528
+ checked: continuousPlay,
529
+ onChange: setContinuousPlay,
530
+ inputClassName: "continuous-play",
531
+ children: children ?? "Continuous Play"
532
+ }
533
+ );
728
534
  };
729
-
730
- // src/components/EditableCheckbox.tsx
731
- import {
732
- BaseCheckboxWrapper as BaseCheckboxWrapper3,
733
- BaseCheckbox as BaseCheckbox3,
734
- BaseCheckboxLabel as BaseCheckboxLabel3
735
- } from "@waveform-playlist/ui-components";
736
- import { jsx as jsx8, jsxs as jsxs6 } from "react/jsx-runtime";
737
- var EditableCheckbox = ({
738
- checked,
739
- onChange,
740
- className
741
- }) => {
742
- return /* @__PURE__ */ jsxs6(BaseCheckboxWrapper3, { className, children: [
743
- /* @__PURE__ */ jsx8(
744
- BaseCheckbox3,
745
- {
746
- type: "checkbox",
747
- id: "editable-annotations",
748
- checked,
749
- onChange: (e) => onChange(e.target.checked)
750
- }
751
- ),
752
- /* @__PURE__ */ jsx8(BaseCheckboxLabel3, { htmlFor: "editable-annotations", children: "Editable Annotations" })
753
- ] });
535
+ var LinkEndpointsCheckbox = ({ children, ...rest }) => {
536
+ const { linkEndpoints } = useAnnotationOptions();
537
+ const { setLinkEndpoints } = usePlaybackControls();
538
+ return /* @__PURE__ */ jsx4(
539
+ OptionCheckbox,
540
+ {
541
+ ...rest,
542
+ checked: linkEndpoints,
543
+ onChange: setLinkEndpoints,
544
+ inputClassName: "link-endpoints",
545
+ children: children ?? "Link Endpoints"
546
+ }
547
+ );
548
+ };
549
+ var EditableCheckbox = ({ children, ...rest }) => {
550
+ const { editable } = useAnnotationOptions();
551
+ const { setAnnotationsEditable } = usePlaybackControls();
552
+ return /* @__PURE__ */ jsx4(OptionCheckbox, { ...rest, checked: editable, onChange: setAnnotationsEditable, children: children ?? "Editable Annotations" });
754
553
  };
755
554
 
756
555
  // src/components/DownloadAnnotationsButton.tsx
757
- import styled6 from "styled-components";
758
- import { jsx as jsx9 } from "react/jsx-runtime";
759
- var StyledButton = styled6.button`
556
+ import styled4 from "styled-components";
557
+ import { useAnnotations } from "@waveform-playlist/browser";
558
+ import { jsx as jsx5 } from "react/jsx-runtime";
559
+ var StyledButton = styled4.button`
760
560
  padding: 0.5rem 1rem;
761
561
  background: ${(props) => props.theme?.surfaceColor || "#f5f5f5"};
762
562
  color: ${(props) => props.theme?.textColor || "#333"};
@@ -784,16 +584,14 @@ var StyledButton = styled6.button`
784
584
  }
785
585
  `;
786
586
  var DownloadAnnotationsButton = ({
787
- annotations,
788
587
  filename = "annotations.json",
789
588
  disabled = false,
790
589
  className,
791
- children = "Download JSON"
590
+ children = "Download JSON",
591
+ title
792
592
  }) => {
593
+ const { annotations } = useAnnotations();
793
594
  const handleDownload = () => {
794
- if (annotations.length === 0) {
795
- return;
796
- }
797
595
  const jsonData = annotations.map((annotation) => serializeAeneas(annotation));
798
596
  const jsonString = JSON.stringify(jsonData, null, 2);
799
597
  const blob = new Blob([jsonString], { type: "application/json" });
@@ -806,13 +604,13 @@ var DownloadAnnotationsButton = ({
806
604
  document.body.removeChild(link);
807
605
  URL.revokeObjectURL(url);
808
606
  };
809
- return /* @__PURE__ */ jsx9(
607
+ return /* @__PURE__ */ jsx5(
810
608
  StyledButton,
811
609
  {
812
610
  onClick: handleDownload,
813
611
  disabled: disabled || annotations.length === 0,
814
612
  className,
815
- title: annotations.length === 0 ? "No annotations to download" : "Download the annotations as JSON",
613
+ title: title ?? (annotations.length === 0 ? "No annotations to download" : "Download the annotations as JSON"),
816
614
  children
817
615
  }
818
616
  );
@@ -820,54 +618,27 @@ var DownloadAnnotationsButton = ({
820
618
 
821
619
  // src/AnnotationProvider.tsx
822
620
  import { AnnotationIntegrationProvider } from "@waveform-playlist/browser";
823
- import { jsx as jsx10 } from "react/jsx-runtime";
621
+ import { jsx as jsx6 } from "react/jsx-runtime";
824
622
  var annotationIntegration = {
825
623
  parseAeneas,
826
624
  serializeAeneas,
827
- AnnotationText: AnnotationText2,
625
+ AnnotationText,
828
626
  AnnotationBox,
829
- AnnotationBoxesWrapper,
830
- ContinuousPlayCheckbox,
831
- LinkEndpointsCheckbox,
832
- EditableCheckbox,
833
- DownloadAnnotationsButton
627
+ AnnotationBoxesWrapper
834
628
  };
835
629
  var AnnotationProvider = ({ children }) => {
836
- return /* @__PURE__ */ jsx10(AnnotationIntegrationProvider, { value: annotationIntegration, children });
837
- };
838
-
839
- // src/hooks/useAnnotationControls.ts
840
- import { useState as useState2, useCallback } from "react";
841
- import { updateAnnotationBoundaries as computeAnnotationBoundaries } from "@waveform-playlist/core";
842
- var useAnnotationControls = (options = {}) => {
843
- const { initialContinuousPlay = false, initialLinkEndpoints = true } = options;
844
- const [continuousPlay, setContinuousPlay] = useState2(initialContinuousPlay);
845
- const [linkEndpoints, setLinkEndpoints] = useState2(initialLinkEndpoints);
846
- const updateAnnotationBoundaries = useCallback(
847
- (params) => computeAnnotationBoundaries(params),
848
- []
849
- );
850
- return {
851
- continuousPlay,
852
- linkEndpoints,
853
- setContinuousPlay,
854
- setLinkEndpoints,
855
- updateAnnotationBoundaries
856
- };
630
+ return /* @__PURE__ */ jsx6(AnnotationIntegrationProvider, { value: annotationIntegration, children });
857
631
  };
858
632
  export {
859
- Annotation,
860
633
  AnnotationBox,
861
634
  AnnotationBoxesWrapper,
862
635
  AnnotationProvider,
863
- AnnotationText2 as AnnotationText,
864
- AnnotationsTrack,
636
+ AnnotationText,
865
637
  ContinuousPlayCheckbox,
866
638
  DownloadAnnotationsButton,
867
639
  EditableCheckbox,
868
640
  LinkEndpointsCheckbox,
869
641
  parseAeneas,
870
- serializeAeneas,
871
- useAnnotationControls
642
+ serializeAeneas
872
643
  };
873
644
  //# sourceMappingURL=index.mjs.map