@bendyline/squisq-editor-react 1.6.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.
- package/README.md +12 -3
- package/dist/index.d.ts +1304 -208
- package/dist/index.js +22838 -14494
- package/dist/index.js.map +1 -1
- package/dist/monaco.d.ts +1 -0
- package/dist/monaco.js +4 -0
- package/dist/monaco.js.map +1 -0
- package/dist/styles/index.css +2384 -408
- package/package.json +9 -5
- package/src/BlockPropertiesPopover.tsx +48 -17
- package/src/DocumentSettingsDialog.tsx +13 -21
- package/src/EditorContext.tsx +199 -42
- package/src/EditorShell.tsx +403 -270
- package/src/ImageEditor.tsx +22 -1
- package/src/InlinePreviewGutter.tsx +16 -8
- package/src/MediaBin.tsx +276 -40
- package/src/OutlinePanel.tsx +202 -7
- package/src/PlainHtmlPreview.tsx +40 -1
- package/src/PreviewControls.tsx +466 -93
- package/src/PreviewPanel.tsx +313 -77
- package/src/RawEditor.tsx +13 -2
- package/src/StatusBar.tsx +5 -2
- package/src/TemplateAnnotation.ts +34 -10
- package/src/TemplateContentPreview.tsx +56 -0
- package/src/TemplatePicker.tsx +355 -134
- package/src/ThemeCustomizerPanel.tsx +30 -15
- package/src/ThemePicker.tsx +10 -5
- package/src/TimelineBlockPreview.tsx +1 -1
- package/src/Toolbar.tsx +719 -238
- package/src/TransitionPicker.tsx +44 -5
- package/src/ViewMenuPanel.tsx +12 -14
- package/src/ViewSwitcher.tsx +4 -4
- package/src/WysiwygEditor.tsx +167 -92
- package/src/__tests__/blockPropertiesPopoverTheme.test.tsx +33 -0
- package/src/__tests__/blockTagActivity.test.ts +183 -0
- package/src/__tests__/buildDocumentPreviewMarkdown.test.ts +75 -0
- package/src/__tests__/buildPreviewDocContent.test.ts +48 -0
- package/src/__tests__/buildPreviewDocTransition.test.ts +22 -3
- package/src/__tests__/canvasSurfaceTextEditing.test.tsx +60 -0
- package/src/__tests__/customLayoutManagerTheme.test.tsx +34 -0
- package/src/__tests__/documentSettingsDialog.test.tsx +29 -1
- package/src/__tests__/editorScrollHandoff.test.tsx +102 -0
- package/src/__tests__/editorScrollSync.test.ts +65 -0
- package/src/__tests__/editorShellProps.test.tsx +390 -1
- package/src/__tests__/emojiPicker.test.tsx +2 -42
- package/src/__tests__/headingTransition.test.ts +53 -15
- package/src/__tests__/imageEditAffordance.test.tsx +5 -1
- package/src/__tests__/imageEditModalContract.test.tsx +119 -0
- package/src/__tests__/imageEditorShell.test.tsx +38 -1
- package/src/__tests__/jsonEditor.test.tsx +56 -1
- package/src/__tests__/layersPanel.test.tsx +75 -5
- package/src/__tests__/majorApiTypes.test.ts +26 -0
- package/src/__tests__/mediaBinDrop.test.tsx +90 -0
- package/src/__tests__/mediaReferences.test.ts +82 -0
- package/src/__tests__/narrationSave.test.ts +148 -0
- package/src/__tests__/outlinePanel.test.tsx +93 -3
- package/src/__tests__/outlineSource.test.ts +217 -0
- package/src/__tests__/plainHtmlPreview.test.tsx +17 -1
- package/src/__tests__/presentationMode.test.tsx +290 -0
- package/src/__tests__/previewControls.test.tsx +312 -6
- package/src/__tests__/previewPanelAudioFailure.test.tsx +46 -0
- package/src/__tests__/previewPanelPresentation.test.tsx +126 -0
- package/src/__tests__/previewPanelTransforms.test.tsx +117 -0
- package/src/__tests__/rawEditorModelIsolation.test.ts +19 -0
- package/src/__tests__/resolveBlockVisual.test.ts +43 -0
- package/src/__tests__/statusBar.test.tsx +27 -0
- package/src/__tests__/teleprompter.test.tsx +190 -0
- package/src/__tests__/templateAnnotationRoundTrip.test.ts +49 -2
- package/src/__tests__/templateContentPreview.test.ts +101 -0
- package/src/__tests__/templatePickerPortal.test.tsx +62 -0
- package/src/__tests__/tiptapBridge.test.ts +70 -0
- package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +109 -0
- package/src/__tests__/transformStyleId.test.ts +13 -0
- package/src/__tests__/useImageEditor.test.tsx +67 -0
- package/src/__tests__/useMediaRecorder.test.ts +75 -0
- package/src/__tests__/viewMenuPanel.test.tsx +130 -0
- package/src/__tests__/wysiwygImageUpload.test.ts +69 -0
- package/src/__tests__/wysiwygTemplateBadgeFocus.test.tsx +56 -0
- package/src/asciiDiagram/AsciiDiagramExtension.ts +343 -0
- package/src/asciiDiagram/AsciiDiagramWidget.tsx +224 -0
- package/src/asciiDiagram/RepairableDiagramExtension.ts +200 -0
- package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +145 -0
- package/src/asciiDiagram/__tests__/RepairableDiagramExtension.test.ts +159 -0
- package/src/asciiDiagram/__tests__/asciiDiagramCommands.test.ts +227 -0
- package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +114 -0
- package/src/asciiDiagram/__tests__/asciiPaste.test.ts +43 -0
- package/src/asciiDiagram/asciiDiagramCommands.ts +148 -0
- package/src/asciiDiagram/asciiDiagramData.ts +116 -0
- package/src/asciiDiagram/asciiDiagramOps.ts +219 -0
- package/src/asciiDiagram/asciiPaste.ts +19 -0
- package/src/blockTagActivity.ts +233 -0
- package/src/buildDocumentPreviewMarkdown.ts +168 -0
- package/src/buildPreviewDoc.ts +19 -12
- package/src/customTemplates/CustomLayoutManager.tsx +3 -2
- package/src/customTemplates/CustomTemplateContext.tsx +6 -0
- package/src/customTemplates/TemplateDesigner.tsx +7 -4
- package/src/customTemplates/__tests__/library.test.ts +8 -0
- package/src/customTemplates/designer.css +158 -99
- package/src/customTemplates/library.ts +16 -3
- package/src/customThemes/CustomThemeContext.tsx +6 -0
- package/src/customThemes/CustomThemeDialog.tsx +35 -5
- package/src/customThemes/ImportThemeSection.tsx +178 -0
- package/src/customThemes/__tests__/customThemeLibrary.test.ts +9 -0
- package/src/customThemes/__tests__/importThemeSection.test.tsx +192 -0
- package/src/customThemes/customThemeLibrary.ts +7 -4
- package/src/customThemes/index.ts +7 -1
- package/src/customThemes/themeDraft.ts +23 -7
- package/src/diagram/DiagramCanvas.tsx +9 -4
- package/src/diagram/types.ts +35 -0
- package/src/editorScrollSync.ts +73 -0
- package/src/emojiData.ts +3 -23
- package/src/headingTransition.ts +85 -25
- package/src/imageEditor/CanvasSurface.tsx +29 -23
- package/src/imageEditor/LayersPanel.tsx +78 -3
- package/src/imageEditor/Toolbar.tsx +5 -1
- package/src/imageEditor/icons.tsx +4 -0
- package/src/imageEditor/image-editor.css +75 -0
- package/src/imageEditor/layers/SelectionHandles.tsx +1 -1
- package/src/imageEditor/useImageEditor.ts +71 -14
- package/src/index.ts +193 -20
- package/src/jsonEditor/JsonEditorContext.tsx +12 -6
- package/src/jsonEditor/RenderNode.tsx +24 -3
- package/src/jsonEditor/editors.tsx +86 -19
- package/src/mediaEntries.ts +12 -0
- package/src/mediaReferences.ts +299 -0
- package/src/monaco.ts +35 -0
- package/src/monacoWorkers.ts +89 -0
- package/src/outlineSource.ts +171 -0
- package/src/presentation/PresentationMode.tsx +596 -0
- package/src/rawEditorIsolation.ts +18 -0
- package/src/recorder/hooks/useMediaRecorder.ts +97 -53
- package/src/resolveBlockVisual.ts +10 -4
- package/src/scene/Scene.tsx +101 -25
- package/src/scene/SceneBlockExtension.ts +17 -10
- package/src/scene/SceneBlockWidget.tsx +11 -4
- package/src/scene/SceneSelection.tsx +19 -15
- package/src/scene/SceneSideToolbar.tsx +89 -0
- package/src/scene/SceneViewport.tsx +7 -3
- package/src/scene/ShapePalette.tsx +17 -2
- package/src/scene/__tests__/DiagramAdapter.test.ts +86 -0
- package/src/scene/__tests__/SceneBlockExtension.test.ts +33 -0
- package/src/scene/__tests__/sceneIsolation.test.tsx +93 -0
- package/src/scene/adapters/DiagramAdapter.ts +12 -101
- package/src/scene/commands/SceneCommand.ts +4 -1
- package/src/scene/index.ts +1 -6
- package/src/scene/layers/DiagramEdges.tsx +24 -9
- package/src/scene/layers/edgeGeometry.ts +31 -4
- package/src/scene/layers/nodeCard.tsx +27 -8
- package/src/scene/scene.css +145 -2
- package/src/scene/text/SceneTextOverlay.tsx +5 -5
- package/src/scene/text/sceneTextChannel.ts +26 -20
- package/src/scene/text/sceneTextConfig.ts +4 -0
- package/src/scene/tools/ConnectTool.ts +126 -28
- package/src/scene/tools/DrawingConnectTool.ts +86 -16
- package/src/scene/tools/SceneTool.ts +10 -0
- package/src/scene/tools/SelectTool.ts +34 -18
- package/src/scene/tools/ShapeTool.ts +2 -3
- package/src/styles/ascii-diagram.css +79 -0
- package/src/styles/ascii-timeline.css +499 -0
- package/src/styles/editor.css +1566 -219
- package/src/styles/index.css +3 -0
- package/src/styles/tree-view.css +139 -0
- package/src/teleprompter/TeleprompterControls.tsx +218 -0
- package/src/teleprompter/TeleprompterSelfView.tsx +22 -0
- package/src/teleprompter/TeleprompterSurface.tsx +267 -0
- package/src/teleprompter/TeleprompterView.tsx +338 -0
- package/src/teleprompter/canvasRenderer.ts +161 -0
- package/src/teleprompter/floatingWindow.ts +352 -0
- package/src/teleprompter/index.ts +61 -0
- package/src/teleprompter/pcmWorklet.ts +62 -0
- package/src/teleprompter/recording/insertPreamble.ts +80 -0
- package/src/teleprompter/recording/narrationSave.ts +134 -0
- package/src/teleprompter/recording/useNarrationRecorder.ts +367 -0
- package/src/teleprompter/scrollModel.ts +85 -0
- package/src/teleprompter/teleprompterTheme.ts +303 -0
- package/src/teleprompter/types.ts +38 -0
- package/src/teleprompter/useFloatingWindow.ts +74 -0
- package/src/teleprompter/useMicAnalysis.ts +211 -0
- package/src/teleprompter/useTeleprompter.ts +421 -0
- package/src/templateContentPreviewResolver.ts +360 -0
- package/src/timeline/TimelineEditorWidget.tsx +702 -0
- package/src/timeline/TimelineViewExtension.ts +252 -0
- package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +355 -0
- package/src/timeline/__tests__/TimelineViewExtension.test.ts +163 -0
- package/src/timeline/__tests__/timelineCommands.test.ts +327 -0
- package/src/timeline/__tests__/timelineOps.test.ts +222 -0
- package/src/timeline/__tests__/timelinePaste.test.ts +34 -0
- package/src/timeline/timelineCommands.ts +367 -0
- package/src/timeline/timelineData.ts +54 -0
- package/src/timeline/timelineOps.ts +277 -0
- package/src/timeline/timelinePaste.ts +8 -0
- package/src/tiptapBridge.ts +84 -49
- package/src/transformStyleId.ts +17 -0
- package/src/treeview/TreeOutlineWidget.tsx +240 -0
- package/src/treeview/TreeViewExtension.ts +250 -0
- package/src/treeview/__tests__/TreeViewExtension.test.ts +122 -0
- package/src/treeview/__tests__/treeOps.test.ts +125 -0
- package/src/treeview/__tests__/treePaste.test.ts +40 -0
- package/src/treeview/__tests__/treeViewCommands.test.ts +163 -0
- package/src/treeview/treeOps.ts +172 -0
- package/src/treeview/treePaste.ts +13 -0
- package/src/treeview/treeViewCommands.ts +94 -0
- package/src/treeview/treeViewData.ts +41 -0
- package/src/useMonacoLoader.ts +11 -34
- package/src/wysiwygImageUpload.ts +113 -0
- package/src/diagram/DiagramExtension.ts +0 -209
- package/src/diagram/DiagramWidget.tsx +0 -270
- package/src/diagram/useDiagramData.ts +0 -126
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TeleprompterView — the Narrate display-mode root mounted by
|
|
3
|
+
* PreviewPanel. Owns the controller (single source of truth in the main
|
|
4
|
+
* window) and the floating-window handle; renders the surface docked,
|
|
5
|
+
* portals it into a Document-PiP/popup window, or pumps the canvas
|
|
6
|
+
* rendition for the Safari video-PiP tier.
|
|
7
|
+
*
|
|
8
|
+
* Recording (scenario 1) is prop-gated: PreviewPanel passes
|
|
9
|
+
* `recording` deps (media provider + markdown writers) when the host
|
|
10
|
+
* allows it; without them this is a pure prompter (scenario 2) with
|
|
11
|
+
* zero capture code paths.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
15
|
+
import type { SyntheticEvent } from 'react';
|
|
16
|
+
import { createPortal } from 'react-dom';
|
|
17
|
+
import type { Doc, MediaProvider, Theme } from '@bendyline/squisq/schemas';
|
|
18
|
+
import type { ContentContainer } from '@bendyline/squisq/storage';
|
|
19
|
+
import { wordIndexAtTime } from '@bendyline/squisq/narration';
|
|
20
|
+
import { useTeleprompter } from './useTeleprompter';
|
|
21
|
+
import { useFloatingWindow } from './useFloatingWindow';
|
|
22
|
+
import { TeleprompterSurface } from './TeleprompterSurface';
|
|
23
|
+
import { TeleprompterControls } from './TeleprompterControls';
|
|
24
|
+
import { TeleprompterSelfView } from './TeleprompterSelfView';
|
|
25
|
+
import { drawPrompterFrame, type CanvasPrompterFrame } from './canvasRenderer';
|
|
26
|
+
import { ensureTeleprompterStyles, TELEPROMPTER_CSS } from './teleprompterTheme';
|
|
27
|
+
import { useNarrationRecorder } from './recording/useNarrationRecorder';
|
|
28
|
+
import { buildNarrationSavePlan, executeNarrationSave } from './recording/narrationSave';
|
|
29
|
+
|
|
30
|
+
/** Editor plumbing the recording flow needs; omit for prompter-only use. */
|
|
31
|
+
export interface TeleprompterRecordingDeps {
|
|
32
|
+
mediaProvider: MediaProvider;
|
|
33
|
+
container: ContentContainer | null;
|
|
34
|
+
markdownSource: string;
|
|
35
|
+
setMarkdownSource: (next: string) => void;
|
|
36
|
+
bumpMediaRevision: () => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface TeleprompterViewProps {
|
|
40
|
+
doc: Doc | null;
|
|
41
|
+
theme: Theme;
|
|
42
|
+
/** Kept for API symmetry with PreviewPanel; recording uses `recording.container`. */
|
|
43
|
+
workspaceContainer?: ContentContainer | null;
|
|
44
|
+
/** Media base path (reserved for future preview integrations). */
|
|
45
|
+
basePath?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Optional audience-window portal owned by the editor's Presentation mode.
|
|
48
|
+
* The main controller remains authoritative; only this live surface is
|
|
49
|
+
* mirrored into the target.
|
|
50
|
+
*/
|
|
51
|
+
presentationTarget?: HTMLElement | null;
|
|
52
|
+
/** Recording deps; null/omitted disables the Record affordance. */
|
|
53
|
+
recording?: TeleprompterRecordingDeps | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function TeleprompterView(props: TeleprompterViewProps) {
|
|
57
|
+
const { doc, theme, presentationTarget = null, recording = null } = props;
|
|
58
|
+
const controller = useTeleprompter({ doc });
|
|
59
|
+
const float = useFloatingWindow(TELEPROMPTER_CSS);
|
|
60
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
61
|
+
const controllerRef = useRef(controller);
|
|
62
|
+
controllerRef.current = controller;
|
|
63
|
+
|
|
64
|
+
const [saveNotice, setSaveNotice] = useState<string | null>(null);
|
|
65
|
+
|
|
66
|
+
const recorder = useNarrationRecorder({
|
|
67
|
+
mic: controller.mic,
|
|
68
|
+
getScript: () => controllerRef.current.script,
|
|
69
|
+
getWordPos: () => controllerRef.current.wordPos,
|
|
70
|
+
getMicDeviceId: () => controllerRef.current.prefs.micDeviceId,
|
|
71
|
+
onRecordingStart: () => controllerRef.current.play(),
|
|
72
|
+
onRecordingStop: () => controllerRef.current.pause(),
|
|
73
|
+
});
|
|
74
|
+
const recorderRef = useRef(recorder);
|
|
75
|
+
recorderRef.current = recorder;
|
|
76
|
+
|
|
77
|
+
// The docked chrome (controls, float note) also uses prompter classes.
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
const ownerDoc = rootRef.current?.ownerDocument;
|
|
80
|
+
if (ownerDoc) ensureTeleprompterStyles(ownerDoc);
|
|
81
|
+
}, []);
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (presentationTarget) ensureTeleprompterStyles(presentationTarget.ownerDocument);
|
|
85
|
+
}, [presentationTarget]);
|
|
86
|
+
|
|
87
|
+
// ── Video-PiP pump: draw on analysis ticks, never rAF ──────────────
|
|
88
|
+
const canvasFrameRef = useRef<Omit<CanvasPrompterFrame, 'wordPos'> | null>(null);
|
|
89
|
+
canvasFrameRef.current = controller.script
|
|
90
|
+
? {
|
|
91
|
+
script: controller.script,
|
|
92
|
+
fontSizePx: controller.prefs.fontSizePx,
|
|
93
|
+
mirrored: controller.prefs.mirrored,
|
|
94
|
+
colors: {
|
|
95
|
+
bg: theme.colors.background,
|
|
96
|
+
text: theme.colors.text,
|
|
97
|
+
accent: theme.colors.primary,
|
|
98
|
+
muted: theme.colors.textMuted ?? theme.colors.text,
|
|
99
|
+
},
|
|
100
|
+
countdownRemaining: controller.countdownRemaining,
|
|
101
|
+
recording: recorder.state === 'recording',
|
|
102
|
+
}
|
|
103
|
+
: null;
|
|
104
|
+
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
const sink = float.canvasSink;
|
|
107
|
+
if (float.tier !== 'video-pip' || !sink) return;
|
|
108
|
+
const draw = (wordPos: number) => {
|
|
109
|
+
const config = canvasFrameRef.current;
|
|
110
|
+
if (!config) return;
|
|
111
|
+
drawPrompterFrame(sink.canvas, { ...config, wordPos });
|
|
112
|
+
sink.requestFrame();
|
|
113
|
+
};
|
|
114
|
+
draw(controllerRef.current.wordPos);
|
|
115
|
+
const unsubscribe = controllerRef.current.subscribeTick(draw);
|
|
116
|
+
// Manual mode has no analysis ticks; a coarse interval keeps the PiP
|
|
117
|
+
// moving (best-effort under occlusion — voice mode is the robust path).
|
|
118
|
+
const interval = setInterval(() => draw(controllerRef.current.wordPos), 200);
|
|
119
|
+
return () => {
|
|
120
|
+
unsubscribe();
|
|
121
|
+
clearInterval(interval);
|
|
122
|
+
};
|
|
123
|
+
}, [float.tier, float.canvasSink]);
|
|
124
|
+
|
|
125
|
+
// ── Review playback: the prompter re-scrolls along the aligned take ──
|
|
126
|
+
const reviewAudioUrl = useMemo(
|
|
127
|
+
() => (recorder.take ? URL.createObjectURL(recorder.take.audioBlob) : null),
|
|
128
|
+
[recorder.take],
|
|
129
|
+
);
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
return () => {
|
|
132
|
+
if (reviewAudioUrl) URL.revokeObjectURL(reviewAudioUrl);
|
|
133
|
+
};
|
|
134
|
+
}, [reviewAudioUrl]);
|
|
135
|
+
|
|
136
|
+
const handleReviewTimeUpdate = useCallback((event: SyntheticEvent<HTMLAudioElement>) => {
|
|
137
|
+
const alignment = recorderRef.current.take?.alignment;
|
|
138
|
+
if (!alignment || alignment.words.length === 0) return;
|
|
139
|
+
controllerRef.current.seekToToken(
|
|
140
|
+
wordIndexAtTime(alignment.words, event.currentTarget.currentTime),
|
|
141
|
+
);
|
|
142
|
+
}, []);
|
|
143
|
+
|
|
144
|
+
const handleSave = useCallback(async () => {
|
|
145
|
+
const take = recorderRef.current.take;
|
|
146
|
+
if (!take || !recording) return;
|
|
147
|
+
recorderRef.current.beginSave();
|
|
148
|
+
try {
|
|
149
|
+
const plan = buildNarrationSavePlan({
|
|
150
|
+
script: take.script,
|
|
151
|
+
alignment: take.alignment,
|
|
152
|
+
durationSec: take.durationSec,
|
|
153
|
+
audioExt: take.audioExt,
|
|
154
|
+
cameraExt: take.cameraExt,
|
|
155
|
+
baseWpm: controllerRef.current.prefs.baseWpm,
|
|
156
|
+
...(take.cameraOffsetSec !== undefined ? { cameraOffsetSec: take.cameraOffsetSec } : {}),
|
|
157
|
+
});
|
|
158
|
+
const result = await executeNarrationSave(plan, take, {
|
|
159
|
+
mediaProvider: recording.mediaProvider,
|
|
160
|
+
container: recording.container,
|
|
161
|
+
markdownSource: recording.markdownSource,
|
|
162
|
+
setMarkdownSource: recording.setMarkdownSource,
|
|
163
|
+
bumpMediaRevision: recording.bumpMediaRevision,
|
|
164
|
+
});
|
|
165
|
+
recorderRef.current.finishSave(true);
|
|
166
|
+
setSaveNotice(
|
|
167
|
+
`Saved ${result.audioPath}${take.alignment ? ' — blocks re-timed to your voice' : ''}`,
|
|
168
|
+
);
|
|
169
|
+
} catch (err: unknown) {
|
|
170
|
+
recorderRef.current.finishSave(false, err instanceof Error ? err : new Error(String(err)));
|
|
171
|
+
}
|
|
172
|
+
}, [recording]);
|
|
173
|
+
|
|
174
|
+
const surfaceProps = useMemo(
|
|
175
|
+
() => ({
|
|
176
|
+
wordPos: controller.wordPos,
|
|
177
|
+
fontSizePx: controller.prefs.fontSizePx,
|
|
178
|
+
mirrored: controller.prefs.mirrored,
|
|
179
|
+
lineGuide: controller.prefs.lineGuide,
|
|
180
|
+
countdownRemaining: controller.countdownRemaining,
|
|
181
|
+
recordingIndicator: recorder.state === 'recording',
|
|
182
|
+
theme,
|
|
183
|
+
onSeekToken: controller.seekToToken,
|
|
184
|
+
}),
|
|
185
|
+
[
|
|
186
|
+
controller.wordPos,
|
|
187
|
+
controller.prefs.fontSizePx,
|
|
188
|
+
controller.prefs.mirrored,
|
|
189
|
+
controller.prefs.lineGuide,
|
|
190
|
+
controller.countdownRemaining,
|
|
191
|
+
controller.seekToToken,
|
|
192
|
+
recorder.state,
|
|
193
|
+
theme,
|
|
194
|
+
],
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
if (!controller.script) {
|
|
198
|
+
return (
|
|
199
|
+
<>
|
|
200
|
+
<div ref={rootRef} className="squisq-teleprompter-root" data-testid="teleprompter-view">
|
|
201
|
+
<div className="squisq-teleprompter-float-note">
|
|
202
|
+
<p>Nothing to narrate yet — add some content to the document.</p>
|
|
203
|
+
</div>
|
|
204
|
+
</div>
|
|
205
|
+
{presentationTarget
|
|
206
|
+
? createPortal(
|
|
207
|
+
<div className="squisq-presentation-teleprompter" aria-label="Audience presentation">
|
|
208
|
+
<div className="squisq-teleprompter-float-note">
|
|
209
|
+
<p>Nothing to narrate yet — add some content to the document.</p>
|
|
210
|
+
</div>
|
|
211
|
+
</div>,
|
|
212
|
+
presentationTarget,
|
|
213
|
+
)
|
|
214
|
+
: null}
|
|
215
|
+
</>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const script = controller.script;
|
|
220
|
+
const portalOpen = float.portalTarget !== null;
|
|
221
|
+
const busyRecording = recorder.state === 'recording' || recorder.state === 'starting';
|
|
222
|
+
|
|
223
|
+
const recordSlot = recording ? (
|
|
224
|
+
<span className="squisq-teleprompter-group" data-testid="teleprompter-record">
|
|
225
|
+
{recorder.state === 'idle' || recorder.state === 'error' ? (
|
|
226
|
+
<>
|
|
227
|
+
<button
|
|
228
|
+
type="button"
|
|
229
|
+
onClick={() => void recorder.start()}
|
|
230
|
+
title="Record narration while you read"
|
|
231
|
+
>
|
|
232
|
+
⏺ Record
|
|
233
|
+
</button>
|
|
234
|
+
<label title="Also capture your camera as a separate video file">
|
|
235
|
+
<input
|
|
236
|
+
type="checkbox"
|
|
237
|
+
checked={recorder.withCamera}
|
|
238
|
+
onChange={(e) => recorder.setWithCamera(e.target.checked)}
|
|
239
|
+
/>
|
|
240
|
+
camera
|
|
241
|
+
</label>
|
|
242
|
+
{recorder.state === 'error' ? (
|
|
243
|
+
<span title={recorder.error?.message}>⚠ {recorder.error?.message}</span>
|
|
244
|
+
) : null}
|
|
245
|
+
</>
|
|
246
|
+
) : busyRecording ? (
|
|
247
|
+
<button type="button" onClick={() => void recorder.stop()}>
|
|
248
|
+
⏹ Stop
|
|
249
|
+
</button>
|
|
250
|
+
) : recorder.state === 'processing' ? (
|
|
251
|
+
<span>Aligning take…</span>
|
|
252
|
+
) : recorder.state === 'saving' ? (
|
|
253
|
+
<span>Saving…</span>
|
|
254
|
+
) : null}
|
|
255
|
+
</span>
|
|
256
|
+
) : null;
|
|
257
|
+
|
|
258
|
+
return (
|
|
259
|
+
<div
|
|
260
|
+
ref={rootRef}
|
|
261
|
+
className="squisq-teleprompter-root"
|
|
262
|
+
data-testid="teleprompter-view"
|
|
263
|
+
tabIndex={0}
|
|
264
|
+
onKeyDown={controller.handleKeyDown}
|
|
265
|
+
>
|
|
266
|
+
<div className="squisq-teleprompter-stage">
|
|
267
|
+
{portalOpen ? (
|
|
268
|
+
<div className="squisq-teleprompter-float-note">
|
|
269
|
+
<p>The prompter is floating in its own window.</p>
|
|
270
|
+
<button type="button" onClick={float.close}>
|
|
271
|
+
⇤ Bring it back
|
|
272
|
+
</button>
|
|
273
|
+
</div>
|
|
274
|
+
) : (
|
|
275
|
+
<TeleprompterSurface key="docked" script={script} {...surfaceProps} />
|
|
276
|
+
)}
|
|
277
|
+
{portalOpen && float.portalTarget
|
|
278
|
+
? createPortal(
|
|
279
|
+
<TeleprompterSurface
|
|
280
|
+
key={`float-${float.tier}`}
|
|
281
|
+
script={script}
|
|
282
|
+
{...surfaceProps}
|
|
283
|
+
compact
|
|
284
|
+
/>,
|
|
285
|
+
float.portalTarget,
|
|
286
|
+
)
|
|
287
|
+
: null}
|
|
288
|
+
{presentationTarget
|
|
289
|
+
? createPortal(
|
|
290
|
+
<div className="squisq-presentation-teleprompter" aria-label="Audience presentation">
|
|
291
|
+
<TeleprompterSurface
|
|
292
|
+
key="presentation-audience"
|
|
293
|
+
script={script}
|
|
294
|
+
{...surfaceProps}
|
|
295
|
+
/>
|
|
296
|
+
</div>,
|
|
297
|
+
presentationTarget,
|
|
298
|
+
)
|
|
299
|
+
: null}
|
|
300
|
+
<TeleprompterSelfView stream={recorder.cameraStream} />
|
|
301
|
+
</div>
|
|
302
|
+
|
|
303
|
+
{recorder.state === 'review' && recorder.take ? (
|
|
304
|
+
<div className="squisq-teleprompter-review" data-testid="teleprompter-review">
|
|
305
|
+
<span>
|
|
306
|
+
Take: {recorder.take.durationSec.toFixed(1)}s
|
|
307
|
+
{recorder.take.alignment
|
|
308
|
+
? ` · ${recorder.take.alignment.detectedSyllables} syllables aligned`
|
|
309
|
+
: ' · timing unavailable (saved without re-timing)'}
|
|
310
|
+
</span>
|
|
311
|
+
{reviewAudioUrl ? (
|
|
312
|
+
<audio controls src={reviewAudioUrl} onTimeUpdate={handleReviewTimeUpdate} />
|
|
313
|
+
) : null}
|
|
314
|
+
<button type="button" onClick={() => void handleSave()}>
|
|
315
|
+
✓ Save narration
|
|
316
|
+
</button>
|
|
317
|
+
<button type="button" onClick={recorder.retake}>
|
|
318
|
+
↺ Retake
|
|
319
|
+
</button>
|
|
320
|
+
<button type="button" onClick={recorder.discard}>
|
|
321
|
+
✕ Discard
|
|
322
|
+
</button>
|
|
323
|
+
{recorder.error ? <span>⚠ {recorder.error.message}</span> : null}
|
|
324
|
+
</div>
|
|
325
|
+
) : null}
|
|
326
|
+
{saveNotice ? (
|
|
327
|
+
<div className="squisq-teleprompter-review" data-testid="teleprompter-save-notice">
|
|
328
|
+
<span>{saveNotice}</span>
|
|
329
|
+
<button type="button" onClick={() => setSaveNotice(null)}>
|
|
330
|
+
Dismiss
|
|
331
|
+
</button>
|
|
332
|
+
</div>
|
|
333
|
+
) : null}
|
|
334
|
+
|
|
335
|
+
<TeleprompterControls controller={controller} float={float} recordSlot={recordSlot} />
|
|
336
|
+
</div>
|
|
337
|
+
);
|
|
338
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas renderer for the video-PiP tier.
|
|
3
|
+
*
|
|
4
|
+
* Safari has no Document Picture-in-Picture, so the float there is a
|
|
5
|
+
* `<canvas>.captureStream()` piped into a `<video>` in PiP. No script
|
|
6
|
+
* runs in that window — the main window draws this read-only rendition
|
|
7
|
+
* of the prompter on analysis ticks (never rAF, which throttles when
|
|
8
|
+
* the browser is occluded).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { NarrationScript } from '@bendyline/squisq/narration';
|
|
12
|
+
|
|
13
|
+
export interface CanvasPrompterFrame {
|
|
14
|
+
script: NarrationScript;
|
|
15
|
+
/** Fractional word position (floor = active token). */
|
|
16
|
+
wordPos: number;
|
|
17
|
+
fontSizePx: number;
|
|
18
|
+
mirrored: boolean;
|
|
19
|
+
colors: { bg: string; text: string; accent: string; muted: string };
|
|
20
|
+
countdownRemaining: number | null;
|
|
21
|
+
recording: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface LayoutCache {
|
|
25
|
+
key: string;
|
|
26
|
+
/** Line index of each token. */
|
|
27
|
+
tokenLine: number[];
|
|
28
|
+
/** Tokens per line, in order. */
|
|
29
|
+
lines: number[][];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let layoutCache: LayoutCache | null = null;
|
|
33
|
+
|
|
34
|
+
/** Nearest spoken token at or before `idx` (falling forward for leading punctuation). */
|
|
35
|
+
function nearestSpoken(tokens: NarrationScript['tokens'], idx: number): number {
|
|
36
|
+
for (let i = idx; i >= 0; i--) {
|
|
37
|
+
if (tokens[i].spoken) return i;
|
|
38
|
+
}
|
|
39
|
+
for (let i = idx + 1; i < tokens.length; i++) {
|
|
40
|
+
if (tokens[i].spoken) return i;
|
|
41
|
+
}
|
|
42
|
+
return idx;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Wrap script tokens into lines for a given canvas width/font (cached). */
|
|
46
|
+
function layoutTokens(
|
|
47
|
+
ctx: CanvasRenderingContext2D,
|
|
48
|
+
script: NarrationScript,
|
|
49
|
+
fontPx: number,
|
|
50
|
+
maxWidth: number,
|
|
51
|
+
): LayoutCache {
|
|
52
|
+
const key = `${script.tokens.length}:${script.sourceText.length}:${fontPx}:${maxWidth}`;
|
|
53
|
+
if (layoutCache && layoutCache.key === key) return layoutCache;
|
|
54
|
+
const spaceW = ctx.measureText(' ').width;
|
|
55
|
+
const tokenLine = new Array<number>(script.tokens.length);
|
|
56
|
+
const lines: number[][] = [];
|
|
57
|
+
let current: number[] = [];
|
|
58
|
+
let x = 0;
|
|
59
|
+
for (let i = 0; i < script.tokens.length; i++) {
|
|
60
|
+
const token = script.tokens[i];
|
|
61
|
+
const w = ctx.measureText(token.text).width;
|
|
62
|
+
const startsBlock = i === 0 || script.tokens[i - 1].blockIndex !== token.blockIndex;
|
|
63
|
+
const startsPara = i > 0 && script.tokens[i - 1].pauseAfter >= 2;
|
|
64
|
+
if (current.length > 0 && (x + w > maxWidth || startsBlock || startsPara)) {
|
|
65
|
+
lines.push(current);
|
|
66
|
+
current = [];
|
|
67
|
+
x = 0;
|
|
68
|
+
}
|
|
69
|
+
tokenLine[i] = lines.length;
|
|
70
|
+
current.push(i);
|
|
71
|
+
x += w + spaceW;
|
|
72
|
+
}
|
|
73
|
+
if (current.length > 0) lines.push(current);
|
|
74
|
+
layoutCache = { key, tokenLine, lines };
|
|
75
|
+
return layoutCache;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Draw one prompter frame. Cheap enough to run at analysis-tick rate. */
|
|
79
|
+
export function drawPrompterFrame(canvas: HTMLCanvasElement, frame: CanvasPrompterFrame): void {
|
|
80
|
+
const ctx = canvas.getContext('2d');
|
|
81
|
+
if (!ctx) return;
|
|
82
|
+
const { width, height } = canvas;
|
|
83
|
+
const fontPx = Math.round(frame.fontSizePx * 0.72);
|
|
84
|
+
const lineH = Math.round(fontPx * 1.4);
|
|
85
|
+
const marginX = Math.round(width * 0.06);
|
|
86
|
+
|
|
87
|
+
ctx.save();
|
|
88
|
+
ctx.fillStyle = frame.colors.bg;
|
|
89
|
+
ctx.fillRect(0, 0, width, height);
|
|
90
|
+
if (frame.mirrored) {
|
|
91
|
+
ctx.translate(width, 0);
|
|
92
|
+
ctx.scale(-1, 1);
|
|
93
|
+
}
|
|
94
|
+
ctx.font = `500 ${fontPx}px system-ui, sans-serif`;
|
|
95
|
+
ctx.textBaseline = 'top';
|
|
96
|
+
|
|
97
|
+
const layout = layoutTokens(ctx, frame.script, fontPx, width - marginX * 2);
|
|
98
|
+
const count = frame.script.tokens.length;
|
|
99
|
+
if (count > 0) {
|
|
100
|
+
const rawActive = Math.min(Math.max(Math.floor(frame.wordPos), 0), count - 1);
|
|
101
|
+
const frac = Math.min(Math.max(frame.wordPos - rawActive, 0), 1);
|
|
102
|
+
// Keep the highlight off standalone punctuation (em-dashes, bullets).
|
|
103
|
+
const active = nearestSpoken(frame.script.tokens, rawActive);
|
|
104
|
+
const activeLine = layout.tokenLine[active] ?? 0;
|
|
105
|
+
const eyeY = height * 0.38;
|
|
106
|
+
const spaceW = ctx.measureText(' ').width;
|
|
107
|
+
const scrollY = (activeLine + frac) * lineH - eyeY;
|
|
108
|
+
|
|
109
|
+
const firstLine = Math.max(0, Math.floor(scrollY / lineH) - 1);
|
|
110
|
+
const lastLine = Math.min(layout.lines.length - 1, Math.ceil((scrollY + height) / lineH) + 1);
|
|
111
|
+
for (let line = firstLine; line <= lastLine; line++) {
|
|
112
|
+
const y = line * lineH - scrollY;
|
|
113
|
+
let x = marginX;
|
|
114
|
+
for (const tokenIdx of layout.lines[line]) {
|
|
115
|
+
const token = frame.script.tokens[tokenIdx];
|
|
116
|
+
if (tokenIdx === active) {
|
|
117
|
+
ctx.fillStyle = frame.colors.accent;
|
|
118
|
+
ctx.font = `800 ${fontPx}px system-ui, sans-serif`;
|
|
119
|
+
} else {
|
|
120
|
+
ctx.fillStyle = frame.colors.text;
|
|
121
|
+
ctx.font = `500 ${fontPx}px system-ui, sans-serif`;
|
|
122
|
+
ctx.globalAlpha = tokenIdx < active ? 0.9 : 0.45;
|
|
123
|
+
}
|
|
124
|
+
ctx.fillText(token.text, x, y);
|
|
125
|
+
ctx.globalAlpha = 1;
|
|
126
|
+
x += ctx.measureText(token.text).width + spaceW;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Eye-line chevron.
|
|
131
|
+
ctx.fillStyle = frame.colors.accent;
|
|
132
|
+
ctx.beginPath();
|
|
133
|
+
ctx.moveTo(4, eyeY + lineH * 0.2);
|
|
134
|
+
ctx.lineTo(14, eyeY + lineH * 0.5);
|
|
135
|
+
ctx.lineTo(4, eyeY + lineH * 0.8);
|
|
136
|
+
ctx.closePath();
|
|
137
|
+
ctx.fill();
|
|
138
|
+
}
|
|
139
|
+
ctx.restore();
|
|
140
|
+
|
|
141
|
+
// Overlays draw unmirrored so they stay readable.
|
|
142
|
+
if (frame.countdownRemaining !== null) {
|
|
143
|
+
ctx.save();
|
|
144
|
+
ctx.fillStyle = `${frame.colors.bg}cc`;
|
|
145
|
+
ctx.fillRect(0, 0, width, height);
|
|
146
|
+
ctx.fillStyle = frame.colors.accent;
|
|
147
|
+
ctx.font = `800 ${Math.round(height * 0.4)}px system-ui, sans-serif`;
|
|
148
|
+
ctx.textAlign = 'center';
|
|
149
|
+
ctx.textBaseline = 'middle';
|
|
150
|
+
ctx.fillText(String(frame.countdownRemaining), width / 2, height / 2);
|
|
151
|
+
ctx.restore();
|
|
152
|
+
}
|
|
153
|
+
if (frame.recording) {
|
|
154
|
+
ctx.save();
|
|
155
|
+
ctx.fillStyle = '#e5484d';
|
|
156
|
+
ctx.beginPath();
|
|
157
|
+
ctx.arc(width - 20, 20, 8, 0, Math.PI * 2);
|
|
158
|
+
ctx.fill();
|
|
159
|
+
ctx.restore();
|
|
160
|
+
}
|
|
161
|
+
}
|