@bendyline/squisq-react 0.1.2
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.d.ts +563 -0
- package/dist/index.js +3180 -0
- package/dist/index.js.map +1 -0
- package/dist/squisq-player.css +2 -0
- package/dist/squisq-player.css.map +1 -0
- package/dist/squisq-player.global.js +6 -0
- package/dist/squisq-player.global.js.map +1 -0
- package/dist/standalone-source.d.ts +2 -0
- package/dist/standalone-source.js +2 -0
- package/package.json +69 -0
- package/src/BlockRenderer.tsx +146 -0
- package/src/CaptionOverlay.tsx +86 -0
- package/src/DocControlsBottom.tsx +103 -0
- package/src/DocControlsOverlay.tsx +178 -0
- package/src/DocControlsSidebar.tsx +107 -0
- package/src/DocControlsSlideshow.tsx +132 -0
- package/src/DocPlayer.tsx +1005 -0
- package/src/DocPlayerWithSidebar.tsx +138 -0
- package/src/DocProgressBar.tsx +200 -0
- package/src/LinearDocView.tsx +313 -0
- package/src/MarkdownRenderer.tsx +360 -0
- package/src/__tests__/BlockRenderer.test.tsx +105 -0
- package/src/__tests__/DocControlsSlideshow.test.tsx +127 -0
- package/src/__tests__/LinearDocView.test.tsx +180 -0
- package/src/__tests__/MarkdownRenderer.test.tsx +234 -0
- package/src/__tests__/exports.test.ts +55 -0
- package/src/hooks/AudioProvider.ts +114 -0
- package/src/hooks/MediaContext.tsx +81 -0
- package/src/hooks/index.ts +6 -0
- package/src/hooks/useAudioSync.ts +390 -0
- package/src/hooks/useDocPlayback.ts +251 -0
- package/src/hooks/useViewportOrientation.ts +117 -0
- package/src/index.ts +46 -0
- package/src/layers/ImageLayer.tsx +182 -0
- package/src/layers/MapLayer.tsx +184 -0
- package/src/layers/ShapeLayer.tsx +107 -0
- package/src/layers/TextLayer.tsx +197 -0
- package/src/layers/VideoLayer.tsx +150 -0
- package/src/layers/index.ts +5 -0
- package/src/standalone-entry.tsx +228 -0
- package/src/styles/doc-animations.css +458 -0
- package/src/types.ts +152 -0
- package/src/utils/animationUtils.ts +13 -0
- package/src/utils/layerUtils.ts +42 -0
- package/src/utils/mapTileUtils.ts +375 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DocPlayerWithSidebar Component
|
|
3
|
+
*
|
|
4
|
+
* Wrapper that composes DocPlayer + DocControlsSidebar in a horizontal
|
|
5
|
+
* flex layout. The video renders on the left with only a scrubber at the
|
|
6
|
+
* bottom, and playback controls render in a vertical sidebar on the right.
|
|
7
|
+
*
|
|
8
|
+
* This layout eliminates black bars for portrait (9:16) video in portrait
|
|
9
|
+
* viewports by placing controls in the space that would otherwise be unused.
|
|
10
|
+
*
|
|
11
|
+
* Uses refs + forceUpdate pattern to avoid infinite render loops between
|
|
12
|
+
* the DocPlayer's callbacks and this component's state. The DocPlayer
|
|
13
|
+
* fires onPlaybackStateChange frequently (every currentTime tick), so we
|
|
14
|
+
* store state in a ref and only trigger re-renders at controlled intervals.
|
|
15
|
+
*
|
|
16
|
+
* Related Files:
|
|
17
|
+
* - DocPlayer.tsx -- Core player (renders with showControls=false, showScrubber=true)
|
|
18
|
+
* - DocControlsSidebar.tsx -- Vertical sidebar controls
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { useRef, useState, useCallback, useEffect } from 'react';
|
|
22
|
+
import type { Doc } from '@bendyline/squisq/schemas';
|
|
23
|
+
import type { ViewportConfig } from '@bendyline/squisq/schemas';
|
|
24
|
+
import type { AudioProvider } from './hooks/AudioProvider';
|
|
25
|
+
import { DocPlayer } from './DocPlayer';
|
|
26
|
+
import { DocControlsSidebar } from './DocControlsSidebar';
|
|
27
|
+
import type { PlaybackState, PlaybackActions } from './types';
|
|
28
|
+
|
|
29
|
+
interface DocPlayerWithSidebarProps {
|
|
30
|
+
script: Doc;
|
|
31
|
+
basePath: string;
|
|
32
|
+
autoPlay?: boolean;
|
|
33
|
+
onEnded?: () => void;
|
|
34
|
+
onTimeUpdate?: (time: number) => void;
|
|
35
|
+
audioProvider?: AudioProvider;
|
|
36
|
+
muted?: boolean;
|
|
37
|
+
captionsEnabled?: boolean;
|
|
38
|
+
isFullscreen?: boolean;
|
|
39
|
+
onFullscreenToggle?: () => void;
|
|
40
|
+
/** Force a specific viewport preset, bypassing window-based orientation detection. */
|
|
41
|
+
forceViewport?: ViewportConfig;
|
|
42
|
+
/** Called when playing state changes */
|
|
43
|
+
onPlayingChange?: (isPlaying: boolean) => void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const DEFAULT_STATE: PlaybackState = {
|
|
47
|
+
isPlaying: false,
|
|
48
|
+
currentTime: 0,
|
|
49
|
+
totalDuration: 0,
|
|
50
|
+
currentBlockIndex: 0,
|
|
51
|
+
totalBlocks: 0,
|
|
52
|
+
docProgress: 0,
|
|
53
|
+
hasCaptions: false,
|
|
54
|
+
captionsEnabled: false,
|
|
55
|
+
currentSegmentIndex: 0,
|
|
56
|
+
currentSegmentName: null,
|
|
57
|
+
currentBlock: null,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function DocPlayerWithSidebar({
|
|
61
|
+
script,
|
|
62
|
+
basePath,
|
|
63
|
+
autoPlay = false,
|
|
64
|
+
onEnded,
|
|
65
|
+
onTimeUpdate,
|
|
66
|
+
audioProvider,
|
|
67
|
+
muted,
|
|
68
|
+
captionsEnabled,
|
|
69
|
+
isFullscreen,
|
|
70
|
+
onFullscreenToggle,
|
|
71
|
+
forceViewport,
|
|
72
|
+
onPlayingChange,
|
|
73
|
+
}: DocPlayerWithSidebarProps) {
|
|
74
|
+
// Store playback state in a ref to avoid triggering re-renders from DocPlayer callbacks
|
|
75
|
+
const stateRef = useRef<PlaybackState>(DEFAULT_STATE);
|
|
76
|
+
const actionsRef = useRef<PlaybackActions | null>(null);
|
|
77
|
+
const wasPlayingRef = useRef(false);
|
|
78
|
+
|
|
79
|
+
// Use a counter to force sidebar re-renders at controlled intervals
|
|
80
|
+
const [, setTick] = useState(0);
|
|
81
|
+
|
|
82
|
+
// Update ref without triggering re-render
|
|
83
|
+
const handleStateChange = useCallback(
|
|
84
|
+
(state: PlaybackState) => {
|
|
85
|
+
stateRef.current = state;
|
|
86
|
+
if (onPlayingChange && state.isPlaying !== wasPlayingRef.current) {
|
|
87
|
+
wasPlayingRef.current = state.isPlaying;
|
|
88
|
+
onPlayingChange(state.isPlaying);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
[onPlayingChange],
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const handleControlsReady = useCallback(
|
|
95
|
+
(controls: PlaybackActions & { play: () => void; pause: () => void }) => {
|
|
96
|
+
const isFirst = !actionsRef.current;
|
|
97
|
+
actionsRef.current = controls;
|
|
98
|
+
// Force one re-render on first call to show the sidebar
|
|
99
|
+
if (isFirst) setTick((t) => t + 1);
|
|
100
|
+
},
|
|
101
|
+
[],
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// Periodically sync the ref to trigger sidebar UI updates
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
const interval = setInterval(() => {
|
|
107
|
+
setTick((t) => t + 1);
|
|
108
|
+
}, 250); // 4 updates per second is smooth enough for time/progress display
|
|
109
|
+
return () => clearInterval(interval);
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<div className="doc-player-sidebar-layout">
|
|
114
|
+
<div className="doc-player-sidebar-layout__video">
|
|
115
|
+
<DocPlayer
|
|
116
|
+
script={script}
|
|
117
|
+
basePath={basePath}
|
|
118
|
+
autoPlay={autoPlay}
|
|
119
|
+
onEnded={onEnded}
|
|
120
|
+
onTimeUpdate={onTimeUpdate}
|
|
121
|
+
audioProvider={audioProvider}
|
|
122
|
+
muted={muted}
|
|
123
|
+
captionsEnabled={captionsEnabled}
|
|
124
|
+
showControls={isFullscreen}
|
|
125
|
+
showScrubber={!isFullscreen}
|
|
126
|
+
onPlaybackStateChange={handleStateChange}
|
|
127
|
+
onControlsReady={handleControlsReady}
|
|
128
|
+
isFullscreen={isFullscreen}
|
|
129
|
+
onFullscreenToggle={onFullscreenToggle}
|
|
130
|
+
forceViewport={forceViewport}
|
|
131
|
+
/>
|
|
132
|
+
</div>
|
|
133
|
+
{actionsRef.current && (
|
|
134
|
+
<DocControlsSidebar state={stateRef.current} actions={actionsRef.current} />
|
|
135
|
+
)}
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DocProgressBar Component
|
|
3
|
+
*
|
|
4
|
+
* Interactive progress/scrubber bar for doc playback. Shows a track with
|
|
5
|
+
* progress fill, block markers as clickable dots, hover tooltip with time
|
|
6
|
+
* and block title, and a hover line indicator.
|
|
7
|
+
*
|
|
8
|
+
* Extracted from DocPlayer to enable reuse across different control layouts
|
|
9
|
+
* (overlay, sidebar, bottom). When used in sidebar/bottom layouts, this
|
|
10
|
+
* component renders at the bottom of the video while other controls are
|
|
11
|
+
* externalized.
|
|
12
|
+
*
|
|
13
|
+
* Related Files:
|
|
14
|
+
* - DocPlayer.tsx -- Parent component
|
|
15
|
+
* - DocControlsOverlay.tsx -- Uses this within the overlay
|
|
16
|
+
* - types.ts -- Shared type definitions
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { useRef, useState, useCallback } from 'react';
|
|
20
|
+
import type { PlaybackState, PlaybackActions, BlockMarker } from './types';
|
|
21
|
+
import { formatTime } from './types';
|
|
22
|
+
import type { Block } from '@bendyline/squisq/schemas';
|
|
23
|
+
|
|
24
|
+
interface DocProgressBarProps {
|
|
25
|
+
state: PlaybackState;
|
|
26
|
+
actions: PlaybackActions;
|
|
27
|
+
blockMarkers: BlockMarker[];
|
|
28
|
+
/** All expanded blocks for hover lookup */
|
|
29
|
+
expandedBlocks: Block[];
|
|
30
|
+
/** Optional: get block title for hover tooltip */
|
|
31
|
+
getBlockTitle?: (block: Block) => string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function DocProgressBar({
|
|
35
|
+
state,
|
|
36
|
+
actions,
|
|
37
|
+
blockMarkers,
|
|
38
|
+
expandedBlocks,
|
|
39
|
+
getBlockTitle,
|
|
40
|
+
}: DocProgressBarProps) {
|
|
41
|
+
const progressBarRef = useRef<HTMLDivElement>(null);
|
|
42
|
+
const [hoverPosition, setHoverPosition] = useState<number | null>(null);
|
|
43
|
+
|
|
44
|
+
const handleProgressHover = useCallback((e: React.MouseEvent) => {
|
|
45
|
+
const bar = progressBarRef.current;
|
|
46
|
+
if (!bar) return;
|
|
47
|
+
const rect = bar.getBoundingClientRect();
|
|
48
|
+
const x = e.clientX - rect.left;
|
|
49
|
+
const progress = Math.max(0, Math.min(1, x / rect.width));
|
|
50
|
+
setHoverPosition(progress);
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
const handleProgressLeave = useCallback(() => {
|
|
54
|
+
setHoverPosition(null);
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
const getBlockAtTimeLocal = useCallback(
|
|
58
|
+
(time: number): { block: Block; index: number } | null => {
|
|
59
|
+
for (let i = expandedBlocks.length - 1; i >= 0; i--) {
|
|
60
|
+
const blk = expandedBlocks[i];
|
|
61
|
+
if (time >= blk.startTime) {
|
|
62
|
+
return { block: blk, index: i };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return expandedBlocks.length > 0 ? { block: expandedBlocks[0], index: 0 } : null;
|
|
66
|
+
},
|
|
67
|
+
[expandedBlocks],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div
|
|
72
|
+
ref={progressBarRef}
|
|
73
|
+
style={{
|
|
74
|
+
flex: 1,
|
|
75
|
+
height: '24px',
|
|
76
|
+
cursor: 'pointer',
|
|
77
|
+
position: 'relative',
|
|
78
|
+
display: 'flex',
|
|
79
|
+
alignItems: 'center',
|
|
80
|
+
}}
|
|
81
|
+
onClick={(e) => {
|
|
82
|
+
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
|
83
|
+
const x = e.clientX - rect.left;
|
|
84
|
+
const progress = x / rect.width;
|
|
85
|
+
actions.seekTo(progress * state.totalDuration);
|
|
86
|
+
}}
|
|
87
|
+
onMouseMove={handleProgressHover}
|
|
88
|
+
onMouseLeave={handleProgressLeave}
|
|
89
|
+
>
|
|
90
|
+
{/* Track background */}
|
|
91
|
+
<div
|
|
92
|
+
style={{
|
|
93
|
+
position: 'absolute',
|
|
94
|
+
left: 0,
|
|
95
|
+
right: 0,
|
|
96
|
+
height: '6px',
|
|
97
|
+
background: 'rgba(255,255,255,0.2)',
|
|
98
|
+
borderRadius: '3px',
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
|
|
102
|
+
{/* Progress fill */}
|
|
103
|
+
<div
|
|
104
|
+
style={{
|
|
105
|
+
position: 'absolute',
|
|
106
|
+
left: 0,
|
|
107
|
+
width: `${state.docProgress * 100}%`,
|
|
108
|
+
height: '6px',
|
|
109
|
+
background: '#3d5a80',
|
|
110
|
+
borderRadius: '3px',
|
|
111
|
+
transition: 'width 0.1s',
|
|
112
|
+
}}
|
|
113
|
+
/>
|
|
114
|
+
|
|
115
|
+
{/* Block markers (dots) */}
|
|
116
|
+
{blockMarkers.map((marker, i) => (
|
|
117
|
+
<div
|
|
118
|
+
key={`${marker.block.id}-${i}`}
|
|
119
|
+
style={{
|
|
120
|
+
position: 'absolute',
|
|
121
|
+
left: `${marker.position}%`,
|
|
122
|
+
transform: 'translateX(-50%)',
|
|
123
|
+
width: '10px',
|
|
124
|
+
height: '10px',
|
|
125
|
+
borderRadius: '50%',
|
|
126
|
+
background:
|
|
127
|
+
marker.index === state.currentBlockIndex ? '#ffffff' : 'rgba(255,255,255,0.5)',
|
|
128
|
+
border: '2px solid #3d5a80',
|
|
129
|
+
cursor: 'pointer',
|
|
130
|
+
zIndex: 2,
|
|
131
|
+
transition: 'transform 0.15s, background 0.15s',
|
|
132
|
+
}}
|
|
133
|
+
title={marker.title}
|
|
134
|
+
onClick={(e) => {
|
|
135
|
+
e.stopPropagation();
|
|
136
|
+
actions.seekTo(marker.block.startTime);
|
|
137
|
+
}}
|
|
138
|
+
onMouseEnter={(e) => {
|
|
139
|
+
(e.currentTarget as HTMLElement).style.transform = 'translateX(-50%) scale(1.3)';
|
|
140
|
+
}}
|
|
141
|
+
onMouseLeave={(e) => {
|
|
142
|
+
(e.currentTarget as HTMLElement).style.transform = 'translateX(-50%)';
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
))}
|
|
146
|
+
|
|
147
|
+
{/* Hover tooltip */}
|
|
148
|
+
{hoverPosition !== null && (
|
|
149
|
+
<div
|
|
150
|
+
style={{
|
|
151
|
+
position: 'absolute',
|
|
152
|
+
left: `${hoverPosition * 100}%`,
|
|
153
|
+
bottom: '100%',
|
|
154
|
+
transform: 'translateX(-50%)',
|
|
155
|
+
marginBottom: '8px',
|
|
156
|
+
padding: '6px 10px',
|
|
157
|
+
background: 'rgba(0,0,0,0.9)',
|
|
158
|
+
borderRadius: '4px',
|
|
159
|
+
whiteSpace: 'nowrap',
|
|
160
|
+
pointerEvents: 'none',
|
|
161
|
+
zIndex: 10,
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
<div style={{ color: 'white', fontSize: '12px', fontFamily: 'monospace' }}>
|
|
165
|
+
{formatTime(hoverPosition * state.totalDuration)}
|
|
166
|
+
</div>
|
|
167
|
+
{(() => {
|
|
168
|
+
const hoverTime = hoverPosition * state.totalDuration;
|
|
169
|
+
const slideInfo = getBlockAtTimeLocal(hoverTime);
|
|
170
|
+
if (slideInfo && getBlockTitle) {
|
|
171
|
+
return (
|
|
172
|
+
<div style={{ color: 'rgba(255,255,255,0.7)', fontSize: '11px', marginTop: '2px' }}>
|
|
173
|
+
{getBlockTitle(slideInfo.block)}
|
|
174
|
+
</div>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return null;
|
|
178
|
+
})()}
|
|
179
|
+
</div>
|
|
180
|
+
)}
|
|
181
|
+
|
|
182
|
+
{/* Hover line indicator */}
|
|
183
|
+
{hoverPosition !== null && (
|
|
184
|
+
<div
|
|
185
|
+
style={{
|
|
186
|
+
position: 'absolute',
|
|
187
|
+
left: `${hoverPosition * 100}%`,
|
|
188
|
+
top: '50%',
|
|
189
|
+
transform: 'translate(-50%, -50%)',
|
|
190
|
+
width: '2px',
|
|
191
|
+
height: '16px',
|
|
192
|
+
background: 'rgba(255,255,255,0.6)',
|
|
193
|
+
pointerEvents: 'none',
|
|
194
|
+
zIndex: 1,
|
|
195
|
+
}}
|
|
196
|
+
/>
|
|
197
|
+
)}
|
|
198
|
+
</div>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LinearDocView Component
|
|
3
|
+
*
|
|
4
|
+
* Renders a Doc as a long-scrolling document view. Each block is displayed
|
|
5
|
+
* as a readable section: non-annotated blocks render their markdown content
|
|
6
|
+
* as HTML, while template-annotated blocks render as inline SVG visual cards
|
|
7
|
+
* via BlockRenderer.
|
|
8
|
+
*
|
|
9
|
+
* This is the view used when `displayMode === 'linear'` in DocPlayer.
|
|
10
|
+
*
|
|
11
|
+
* Layout:
|
|
12
|
+
* - Scrollable container with max-width for readability
|
|
13
|
+
* - Headings from the block hierarchy rendered as HTML headings
|
|
14
|
+
* - Body content rendered via MarkdownRenderer
|
|
15
|
+
* - Template-annotated sections show an SVG card (BlockRenderer)
|
|
16
|
+
* using `getLayers()` for on-demand layer computation
|
|
17
|
+
* - Blocks are rendered recursively to preserve the heading hierarchy
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { useMemo } from 'react';
|
|
21
|
+
import type { Doc, Block, DocBlock } from '@bendyline/squisq/schemas';
|
|
22
|
+
import type { ViewportConfig } from '@bendyline/squisq/schemas';
|
|
23
|
+
import type { Theme } from '@bendyline/squisq/schemas';
|
|
24
|
+
import { VIEWPORT_PRESETS } from '@bendyline/squisq/schemas';
|
|
25
|
+
import { getLayers, hasTemplate, DEFAULT_THEME } from '@bendyline/squisq/doc';
|
|
26
|
+
import type { RenderContext } from '@bendyline/squisq/doc';
|
|
27
|
+
import { extractPlainText } from '@bendyline/squisq/markdown';
|
|
28
|
+
import type { MarkdownBlockNode, MarkdownList } from '@bendyline/squisq/markdown';
|
|
29
|
+
import { BlockRenderer } from './BlockRenderer';
|
|
30
|
+
import { MarkdownRenderer } from './MarkdownRenderer';
|
|
31
|
+
|
|
32
|
+
// ── Props ──────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
export interface LinearDocViewProps {
|
|
35
|
+
/** The Doc to render */
|
|
36
|
+
doc: Doc;
|
|
37
|
+
/** Base path for resolving media URLs (images, etc.) */
|
|
38
|
+
basePath?: string;
|
|
39
|
+
/** Viewport config for SVG card rendering (default: landscape) */
|
|
40
|
+
viewport?: ViewportConfig;
|
|
41
|
+
/** Optional CSS class for the outer container */
|
|
42
|
+
className?: string;
|
|
43
|
+
/** Theme to use for rendering (default: DEFAULT_THEME from the theme library) */
|
|
44
|
+
theme?: Theme;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ── Helpers ────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Determine whether a block has a template annotation that should be
|
|
51
|
+
* rendered as a visual SVG card. A block is "annotated" when:
|
|
52
|
+
* 1. Its sourceHeading has a templateAnnotation, AND
|
|
53
|
+
* 2. The annotated template exists in the registry
|
|
54
|
+
*/
|
|
55
|
+
function isAnnotatedBlock(block: Block): boolean {
|
|
56
|
+
const annotation = block.sourceHeading?.templateAnnotation;
|
|
57
|
+
if (!annotation) return false;
|
|
58
|
+
return hasTemplate(annotation.template);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Count total blocks in a hierarchy (for RenderContext.totalBlocks).
|
|
63
|
+
*/
|
|
64
|
+
function countAll(blocks: Block[]): number {
|
|
65
|
+
let count = 0;
|
|
66
|
+
for (const b of blocks) {
|
|
67
|
+
count++;
|
|
68
|
+
if (b.children) count += countAll(b.children);
|
|
69
|
+
}
|
|
70
|
+
return count;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ── Block Section Renderer ─────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
interface BlockSectionProps {
|
|
76
|
+
block: Block;
|
|
77
|
+
basePath: string;
|
|
78
|
+
viewport: ViewportConfig;
|
|
79
|
+
renderContext: RenderContext;
|
|
80
|
+
blockIndex: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Render a single block section: heading + body content or SVG card.
|
|
85
|
+
* Recurses into children to render the full heading tree.
|
|
86
|
+
*/
|
|
87
|
+
function BlockSection({ block, basePath, viewport, renderContext, blockIndex }: BlockSectionProps) {
|
|
88
|
+
const isAnnotated = isAnnotatedBlock(block);
|
|
89
|
+
|
|
90
|
+
// For annotated blocks, compute layers and build a Block with them
|
|
91
|
+
const visualBlock = useMemo(() => {
|
|
92
|
+
if (!isAnnotated) return null;
|
|
93
|
+
|
|
94
|
+
const annotation = block.sourceHeading!.templateAnnotation!;
|
|
95
|
+
const headingText = extractPlainText(block.sourceHeading!);
|
|
96
|
+
const bodyText = extractBodyPlainText(block.contents);
|
|
97
|
+
|
|
98
|
+
// Build a TemplateBlock-compatible object
|
|
99
|
+
const templateBlock: Record<string, unknown> = {
|
|
100
|
+
id: block.id,
|
|
101
|
+
template: annotation.template,
|
|
102
|
+
startTime: 0,
|
|
103
|
+
duration: 1,
|
|
104
|
+
audioSegment: 0,
|
|
105
|
+
title: headingText,
|
|
106
|
+
...getTemplateDefaults(annotation.template, headingText, bodyText, block.contents),
|
|
107
|
+
...annotation.params,
|
|
108
|
+
...block.templateOverrides,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Compute layers via getLayers
|
|
112
|
+
const ctx: RenderContext = {
|
|
113
|
+
...renderContext,
|
|
114
|
+
blockIndex,
|
|
115
|
+
};
|
|
116
|
+
const layers = getLayers(templateBlock as unknown as DocBlock, ctx);
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
...block,
|
|
120
|
+
layers,
|
|
121
|
+
template: annotation.template,
|
|
122
|
+
} as Block;
|
|
123
|
+
}, [block, isAnnotated, renderContext, blockIndex]);
|
|
124
|
+
|
|
125
|
+
return (
|
|
126
|
+
<div
|
|
127
|
+
className="squisq-linear-section"
|
|
128
|
+
data-block-id={block.id}
|
|
129
|
+
data-template={isAnnotated ? block.sourceHeading?.templateAnnotation?.template : undefined}
|
|
130
|
+
>
|
|
131
|
+
{/* Render the heading (if present — preamble has no sourceHeading) */}
|
|
132
|
+
{block.sourceHeading && !isAnnotated && <MarkdownRenderer nodes={[block.sourceHeading]} />}
|
|
133
|
+
|
|
134
|
+
{/* Annotated block: render SVG card */}
|
|
135
|
+
{isAnnotated && visualBlock && (
|
|
136
|
+
<div className="squisq-linear-card">
|
|
137
|
+
{/* Optional heading label above the card */}
|
|
138
|
+
{block.sourceHeading && (
|
|
139
|
+
<div className="squisq-linear-card-label squisq-md">
|
|
140
|
+
<MarkdownRenderer nodes={[block.sourceHeading]} />
|
|
141
|
+
</div>
|
|
142
|
+
)}
|
|
143
|
+
<div
|
|
144
|
+
className="squisq-linear-card-svg"
|
|
145
|
+
style={{
|
|
146
|
+
width: '100%',
|
|
147
|
+
aspectRatio: `${viewport.width} / ${viewport.height}`,
|
|
148
|
+
overflow: 'hidden',
|
|
149
|
+
borderRadius: '8px',
|
|
150
|
+
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.12)',
|
|
151
|
+
marginBottom: '1em',
|
|
152
|
+
}}
|
|
153
|
+
>
|
|
154
|
+
<BlockRenderer
|
|
155
|
+
block={visualBlock}
|
|
156
|
+
blockTime={0}
|
|
157
|
+
basePath={basePath}
|
|
158
|
+
viewport={viewport}
|
|
159
|
+
/>
|
|
160
|
+
</div>
|
|
161
|
+
</div>
|
|
162
|
+
)}
|
|
163
|
+
|
|
164
|
+
{/* Body content (always render for non-annotated blocks, skipped for annotated) */}
|
|
165
|
+
{!isAnnotated && block.contents && block.contents.length > 0 && (
|
|
166
|
+
<MarkdownRenderer nodes={block.contents} />
|
|
167
|
+
)}
|
|
168
|
+
|
|
169
|
+
{/* Recurse into children */}
|
|
170
|
+
{block.children && block.children.length > 0 && (
|
|
171
|
+
<div className="squisq-linear-children">
|
|
172
|
+
{block.children.map((child, i) => (
|
|
173
|
+
<BlockSection
|
|
174
|
+
key={child.id}
|
|
175
|
+
block={child}
|
|
176
|
+
basePath={basePath}
|
|
177
|
+
viewport={viewport}
|
|
178
|
+
renderContext={renderContext}
|
|
179
|
+
blockIndex={blockIndex + i + 1}
|
|
180
|
+
/>
|
|
181
|
+
))}
|
|
182
|
+
</div>
|
|
183
|
+
)}
|
|
184
|
+
</div>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ── Template Defaults (mirrored from PreviewPanel) ─────────────────
|
|
189
|
+
|
|
190
|
+
/** Extract plain text from block contents. */
|
|
191
|
+
function extractBodyPlainText(contents?: MarkdownBlockNode[]): string {
|
|
192
|
+
if (!contents || contents.length === 0) return '';
|
|
193
|
+
return contents
|
|
194
|
+
.map((n) => extractPlainText(n))
|
|
195
|
+
.join('\n')
|
|
196
|
+
.trim();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Extract list items as plain text. */
|
|
200
|
+
function extractListItems(contents?: MarkdownBlockNode[]): string[] {
|
|
201
|
+
if (!contents) return [];
|
|
202
|
+
const items: string[] = [];
|
|
203
|
+
for (const node of contents) {
|
|
204
|
+
if (node.type === 'list') {
|
|
205
|
+
for (const item of (node as MarkdownList).children) {
|
|
206
|
+
const text = extractPlainText(item).trim();
|
|
207
|
+
if (text) items.push(text);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return items;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Provide sensible default fields for templates that require more than
|
|
216
|
+
* just a `title`. Prevents crashes from undefined required fields.
|
|
217
|
+
*/
|
|
218
|
+
function getTemplateDefaults(
|
|
219
|
+
templateName: string,
|
|
220
|
+
headingText: string,
|
|
221
|
+
bodyText: string,
|
|
222
|
+
contents?: MarkdownBlockNode[],
|
|
223
|
+
): Record<string, unknown> {
|
|
224
|
+
switch (templateName) {
|
|
225
|
+
case 'statHighlight':
|
|
226
|
+
return { stat: headingText, description: bodyText || headingText };
|
|
227
|
+
case 'quoteBlock':
|
|
228
|
+
case 'fullBleedQuote':
|
|
229
|
+
case 'pullQuote':
|
|
230
|
+
return { quote: bodyText || headingText };
|
|
231
|
+
case 'factCard':
|
|
232
|
+
return { fact: headingText, explanation: bodyText || headingText };
|
|
233
|
+
case 'comparisonBar':
|
|
234
|
+
return { leftLabel: 'A', leftValue: 60, rightLabel: 'B', rightValue: 40 };
|
|
235
|
+
case 'listBlock':
|
|
236
|
+
return { items: extractListItems(contents) || ['Item 1', 'Item 2', 'Item 3'] };
|
|
237
|
+
case 'definitionCard':
|
|
238
|
+
return { term: headingText, definition: bodyText || headingText };
|
|
239
|
+
case 'dateEvent':
|
|
240
|
+
return { date: headingText, description: bodyText || headingText };
|
|
241
|
+
default:
|
|
242
|
+
return {};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// ── Main Component ─────────────────────────────────────────────────
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Renders a Doc as a long-scrolling, readable document.
|
|
250
|
+
*
|
|
251
|
+
* Non-annotated blocks are rendered as HTML text (headings, paragraphs,
|
|
252
|
+
* lists, etc.) via MarkdownRenderer. Template-annotated blocks are
|
|
253
|
+
* rendered as inline SVG visual cards via BlockRenderer.
|
|
254
|
+
*
|
|
255
|
+
* @example
|
|
256
|
+
* ```tsx
|
|
257
|
+
* <LinearDocView doc={doc} basePath="/media/" />
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
export function LinearDocView({
|
|
261
|
+
doc,
|
|
262
|
+
basePath = '/',
|
|
263
|
+
viewport,
|
|
264
|
+
className,
|
|
265
|
+
theme,
|
|
266
|
+
}: LinearDocViewProps) {
|
|
267
|
+
const activeViewport = viewport ?? VIEWPORT_PRESETS.landscape;
|
|
268
|
+
const totalBlocks = useMemo(() => countAll(doc.blocks), [doc.blocks]);
|
|
269
|
+
|
|
270
|
+
const renderContext: RenderContext = useMemo(
|
|
271
|
+
() => ({
|
|
272
|
+
theme: theme ?? DEFAULT_THEME,
|
|
273
|
+
viewport: activeViewport,
|
|
274
|
+
totalBlocks,
|
|
275
|
+
}),
|
|
276
|
+
[activeViewport, totalBlocks, theme],
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
return (
|
|
280
|
+
<div
|
|
281
|
+
className={`squisq-linear ${className || ''}`}
|
|
282
|
+
style={{
|
|
283
|
+
width: '100%',
|
|
284
|
+
height: '100%',
|
|
285
|
+
overflowY: 'auto',
|
|
286
|
+
overflowX: 'hidden',
|
|
287
|
+
}}
|
|
288
|
+
>
|
|
289
|
+
<div
|
|
290
|
+
className="squisq-linear-content"
|
|
291
|
+
style={{
|
|
292
|
+
maxWidth: '720px',
|
|
293
|
+
margin: '0 auto',
|
|
294
|
+
padding: '24px 16px',
|
|
295
|
+
lineHeight: 1.7,
|
|
296
|
+
fontSize: '16px',
|
|
297
|
+
color: 'var(--squisq-text, #1f2937)',
|
|
298
|
+
}}
|
|
299
|
+
>
|
|
300
|
+
{doc.blocks.map((block, i) => (
|
|
301
|
+
<BlockSection
|
|
302
|
+
key={block.id}
|
|
303
|
+
block={block}
|
|
304
|
+
basePath={basePath}
|
|
305
|
+
viewport={activeViewport}
|
|
306
|
+
renderContext={renderContext}
|
|
307
|
+
blockIndex={i}
|
|
308
|
+
/>
|
|
309
|
+
))}
|
|
310
|
+
</div>
|
|
311
|
+
</div>
|
|
312
|
+
);
|
|
313
|
+
}
|