@openmaic/renderer 0.0.1 → 0.0.3
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/DESIGN.md +7 -1
- package/README.md +11 -3
- package/dist/SlideCanvas.d.ts +12 -1
- package/dist/SlideCanvas.js +105 -0
- package/dist/SlideCanvas.js.map +1 -0
- package/dist/SlideElement.d.ts +3 -2
- package/dist/SlideElement.js +60 -0
- package/dist/SlideElement.js.map +1 -0
- package/dist/context.d.ts +2 -1
- package/dist/context.js +27 -0
- package/dist/context.js.map +1 -0
- package/dist/editing/EditableSlideCanvas.d.ts +26 -0
- package/dist/editing/EditableSlideCanvas.js +352 -0
- package/dist/editing/EditableSlideCanvas.js.map +1 -0
- package/dist/editing/core/drag.d.ts +108 -0
- package/dist/editing/core/drag.js +119 -0
- package/dist/editing/core/drag.js.map +1 -0
- package/dist/editing/core/geometry.d.ts +48 -0
- package/dist/editing/core/geometry.js +186 -0
- package/dist/editing/core/geometry.js.map +1 -0
- package/dist/editing/core/intent.d.ts +39 -0
- package/dist/editing/core/intent.js +35 -0
- package/dist/editing/core/intent.js.map +1 -0
- package/dist/editing/core/line-drag.d.ts +19 -0
- package/dist/editing/core/line-drag.js +163 -0
- package/dist/editing/core/line-drag.js.map +1 -0
- package/dist/editing/core/marquee.d.ts +56 -0
- package/dist/editing/core/marquee.js +74 -0
- package/dist/editing/core/marquee.js.map +1 -0
- package/dist/editing/core/resize.d.ts +121 -0
- package/dist/editing/core/resize.js +392 -0
- package/dist/editing/core/resize.js.map +1 -0
- package/dist/editing/core/rotate.d.ts +37 -0
- package/dist/editing/core/rotate.js +74 -0
- package/dist/editing/core/rotate.js.map +1 -0
- package/dist/editing/core/selection.d.ts +68 -0
- package/dist/editing/core/selection.js +108 -0
- package/dist/editing/core/selection.js.map +1 -0
- package/dist/editing/core/snapping.d.ts +54 -0
- package/dist/editing/core/snapping.js +96 -0
- package/dist/editing/core/snapping.js.map +1 -0
- package/dist/editing/handles/BorderLine.d.ts +15 -0
- package/dist/editing/handles/BorderLine.js +17 -0
- package/dist/editing/handles/BorderLine.js.map +1 -0
- package/dist/editing/handles/LineHandles.d.ts +28 -0
- package/dist/editing/handles/LineHandles.js +51 -0
- package/dist/editing/handles/LineHandles.js.map +1 -0
- package/dist/editing/handles/MarqueeBox.d.ts +19 -0
- package/dist/editing/handles/MarqueeBox.js +26 -0
- package/dist/editing/handles/MarqueeBox.js.map +1 -0
- package/dist/editing/handles/ResizeHandles.d.ts +31 -0
- package/dist/editing/handles/ResizeHandles.js +44 -0
- package/dist/editing/handles/ResizeHandles.js.map +1 -0
- package/dist/editing/handles/RotateHandle.d.ts +26 -0
- package/dist/editing/handles/RotateHandle.js +52 -0
- package/dist/editing/handles/RotateHandle.js.map +1 -0
- package/dist/editing/handles/SelectionOverlay.d.ts +18 -0
- package/dist/editing/handles/SelectionOverlay.js +35 -0
- package/dist/editing/handles/SelectionOverlay.js.map +1 -0
- package/dist/editing/index.d.ts +3 -0
- package/dist/editing/index.js +4 -0
- package/dist/editing/index.js.map +1 -0
- package/dist/editing/types.d.ts +107 -0
- package/dist/editing/types.js +7 -0
- package/dist/editing/types.js.map +1 -0
- package/dist/editing/useEditGesture.d.ts +46 -0
- package/dist/editing/useEditGesture.js +217 -0
- package/dist/editing/useEditGesture.js.map +1 -0
- package/dist/editing/useLineHandleGesture.d.ts +35 -0
- package/dist/editing/useLineHandleGesture.js +112 -0
- package/dist/editing/useLineHandleGesture.js.map +1 -0
- package/dist/editing/useMarqueeGesture.d.ts +43 -0
- package/dist/editing/useMarqueeGesture.js +157 -0
- package/dist/editing/useMarqueeGesture.js.map +1 -0
- package/dist/editing/useResizeGesture.d.ts +43 -0
- package/dist/editing/useResizeGesture.js +137 -0
- package/dist/editing/useResizeGesture.js.map +1 -0
- package/dist/editing/useRotateGesture.d.ts +47 -0
- package/dist/editing/useRotateGesture.js +128 -0
- package/dist/editing/useRotateGesture.js.map +1 -0
- package/dist/effects/HighlightOverlay.js +44 -0
- package/dist/effects/HighlightOverlay.js.map +1 -0
- package/dist/effects/LaserOverlay.js +40 -0
- package/dist/effects/LaserOverlay.js.map +1 -0
- package/dist/effects/SpotlightOverlay.d.ts +3 -1
- package/dist/effects/SpotlightOverlay.js +99 -0
- package/dist/effects/SpotlightOverlay.js.map +1 -0
- package/dist/effects/ZoomWrapper.js +19 -0
- package/dist/effects/ZoomWrapper.js.map +1 -0
- package/dist/elements/chart/BaseChartElement.js +22 -0
- package/dist/elements/chart/BaseChartElement.js.map +1 -0
- package/dist/elements/chart/Chart.js +83 -0
- package/dist/elements/chart/Chart.js.map +1 -0
- package/dist/elements/chart/chartOption.js +178 -0
- package/dist/elements/chart/chartOption.js.map +1 -0
- package/dist/elements/code/BaseCodeElement.js +525 -0
- package/dist/elements/code/BaseCodeElement.js.map +1 -0
- package/dist/elements/image/BaseImageElement.d.ts +4 -4
- package/dist/elements/image/BaseImageElement.js +76 -0
- package/dist/elements/image/BaseImageElement.js.map +1 -0
- package/dist/elements/image/ImageOutline.js +30 -0
- package/dist/elements/image/ImageOutline.js.map +1 -0
- package/dist/elements/image/clipPaths.js +130 -0
- package/dist/elements/image/clipPaths.js.map +1 -0
- package/dist/elements/image/useClipImage.js +40 -0
- package/dist/elements/image/useClipImage.js.map +1 -0
- package/dist/elements/image/useFilter.d.ts +1 -0
- package/dist/elements/image/useFilter.js +35 -0
- package/dist/elements/image/useFilter.js.map +1 -0
- package/dist/elements/index.js +14 -9
- package/dist/elements/index.js.map +1 -1
- package/dist/elements/latex/BaseLatexElement.js +94 -0
- package/dist/elements/latex/BaseLatexElement.js.map +1 -0
- package/dist/elements/line/BaseLineElement.js +72 -0
- package/dist/elements/line/BaseLineElement.js.map +1 -0
- package/dist/elements/line/LinePointMarker.js +19 -0
- package/dist/elements/line/LinePointMarker.js.map +1 -0
- package/dist/elements/shape/BaseShapeElement.js +174 -0
- package/dist/elements/shape/BaseShapeElement.js.map +1 -0
- package/dist/elements/shape/GradientDefs.js +11 -0
- package/dist/elements/shape/GradientDefs.js.map +1 -0
- package/dist/elements/shape/PatternDefs.js +8 -0
- package/dist/elements/shape/PatternDefs.js.map +1 -0
- package/dist/elements/shared/ElementOutline.js +13 -0
- package/dist/elements/shared/ElementOutline.js.map +1 -0
- package/dist/elements/shared/useElementFill.js +15 -0
- package/dist/elements/shared/useElementFill.js.map +1 -0
- package/dist/elements/shared/useElementFlip.js +18 -0
- package/dist/elements/shared/useElementFlip.js.map +1 -0
- package/dist/elements/shared/useElementOutline.js +19 -0
- package/dist/elements/shared/useElementOutline.js.map +1 -0
- package/dist/elements/shared/useElementShadow.js +15 -0
- package/dist/elements/shared/useElementShadow.js.map +1 -0
- package/dist/elements/table/BaseTableElement.js +21 -0
- package/dist/elements/table/BaseTableElement.js.map +1 -0
- package/dist/elements/table/StaticTable.js +117 -0
- package/dist/elements/table/StaticTable.js.map +1 -0
- package/dist/elements/table/tableUtils.js +28 -0
- package/dist/elements/table/tableUtils.js.map +1 -0
- package/dist/elements/text/BaseTextElement.js +46 -0
- package/dist/elements/text/BaseTextElement.js.map +1 -0
- package/dist/elements/video/BaseVideoElement.d.ts +3 -1
- package/dist/elements/video/BaseVideoElement.js +36 -0
- package/dist/elements/video/BaseVideoElement.js.map +1 -0
- package/dist/hooks/useSlideBackgroundStyle.js +43 -0
- package/dist/hooks/useSlideBackgroundStyle.js.map +1 -0
- package/dist/hooks/useViewportSize.d.ts +2 -0
- package/dist/hooks/useViewportSize.js +71 -0
- package/dist/hooks/useViewportSize.js.map +1 -0
- package/dist/index.js +11 -31
- package/dist/index.js.map +1 -1
- package/dist/snapshot/index.d.ts +1 -0
- package/dist/snapshot/index.js +93 -28
- package/dist/snapshot/index.js.map +1 -1
- package/dist/snapshot/katex-fonts-embed.d.ts +10 -0
- package/dist/snapshot/katex-fonts-embed.js +13 -0
- package/dist/snapshot/katex-fonts-embed.js.map +1 -0
- package/dist/snapshot/measure.d.ts +32 -0
- package/dist/snapshot/measure.js +129 -0
- package/dist/snapshot/measure.js.map +1 -0
- package/dist/styles.js +50 -0
- package/dist/styles.js.map +1 -0
- package/dist/types/effects.d.ts +2 -0
- package/dist/utils/cn.js +9 -0
- package/dist/utils/cn.js.map +1 -0
- package/dist/utils/element.js +85 -0
- package/dist/utils/element.js.map +1 -0
- package/dist/utils/geometry.d.ts +2 -2
- package/dist/utils/geometry.js +44 -0
- package/dist/utils/geometry.js.map +1 -0
- package/package.json +19 -4
- package/dist/chunks/BaseCodeElement-Bh-Bfdz6.js +0 -1742
- package/dist/chunks/BaseCodeElement-Bh-Bfdz6.js.map +0 -1
- package/dist/chunks/SlideCanvas-BwlcjqMl.js +0 -469
- package/dist/chunks/SlideCanvas-BwlcjqMl.js.map +0 -1
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { useRef, useEffect, useCallback, useState, useMemo } from 'react';
|
|
4
|
+
import { AnimatePresence, motion } from 'motion/react';
|
|
5
|
+
|
|
6
|
+
// ==================== Shiki Singleton ====================
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
let highlighterPromise = null;
|
|
9
|
+
function getHighlighter() {
|
|
10
|
+
if (!highlighterPromise) {
|
|
11
|
+
highlighterPromise = import('shiki').then(({ createHighlighter }) => createHighlighter({
|
|
12
|
+
themes: ['github-light'],
|
|
13
|
+
langs: [
|
|
14
|
+
'python',
|
|
15
|
+
'javascript',
|
|
16
|
+
'typescript',
|
|
17
|
+
'json',
|
|
18
|
+
'go',
|
|
19
|
+
'rust',
|
|
20
|
+
'java',
|
|
21
|
+
'c',
|
|
22
|
+
'cpp',
|
|
23
|
+
'html',
|
|
24
|
+
'css',
|
|
25
|
+
'bash',
|
|
26
|
+
'sql',
|
|
27
|
+
'yaml',
|
|
28
|
+
'markdown',
|
|
29
|
+
'jsx',
|
|
30
|
+
'tsx',
|
|
31
|
+
],
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
return highlighterPromise;
|
|
35
|
+
}
|
|
36
|
+
// ==================== Helpers ====================
|
|
37
|
+
function parseShikiLines(html) {
|
|
38
|
+
const codeMatch = html.match(/<code>([\s\S]*?)<\/code>/);
|
|
39
|
+
if (!codeMatch)
|
|
40
|
+
return [];
|
|
41
|
+
const parts = codeMatch[1].split('<span class="line">');
|
|
42
|
+
const lines = [];
|
|
43
|
+
for (const part of parts) {
|
|
44
|
+
if (!part)
|
|
45
|
+
continue;
|
|
46
|
+
const endIdx = part.lastIndexOf('</span>');
|
|
47
|
+
if (endIdx !== -1) {
|
|
48
|
+
lines.push(part.substring(0, endIdx));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return lines;
|
|
52
|
+
}
|
|
53
|
+
// ==================== Typing Easing ====================
|
|
54
|
+
const STUTTER_COUNT = 5;
|
|
55
|
+
const STUTTER_AMOUNT = 0.04;
|
|
56
|
+
const LINE_GAP_MS = 120;
|
|
57
|
+
const TAB_SIZE = 4;
|
|
58
|
+
function visualLength(s) {
|
|
59
|
+
let len = 0;
|
|
60
|
+
for (const ch of s) {
|
|
61
|
+
len += ch === '\t' ? TAB_SIZE : 1;
|
|
62
|
+
}
|
|
63
|
+
return len;
|
|
64
|
+
}
|
|
65
|
+
function getTypingCharCount(content) {
|
|
66
|
+
const trimmed = content.replace(/^[\t ]+/, '');
|
|
67
|
+
return trimmed.length;
|
|
68
|
+
}
|
|
69
|
+
function computeRevealSteps(content) {
|
|
70
|
+
const total = visualLength(content);
|
|
71
|
+
if (total === 0)
|
|
72
|
+
return [1];
|
|
73
|
+
const trimmed = content.replace(/^[\t ]+/, '');
|
|
74
|
+
const indentPart = content.slice(0, content.length - trimmed.length);
|
|
75
|
+
let cumVisual = 0;
|
|
76
|
+
for (const ch of indentPart) {
|
|
77
|
+
cumVisual += ch === '\t' ? TAB_SIZE : 1;
|
|
78
|
+
}
|
|
79
|
+
const steps = [cumVisual / total];
|
|
80
|
+
for (const ch of trimmed) {
|
|
81
|
+
cumVisual += ch === '\t' ? TAB_SIZE : 1;
|
|
82
|
+
steps.push(cumVisual / total);
|
|
83
|
+
}
|
|
84
|
+
return steps;
|
|
85
|
+
}
|
|
86
|
+
function humanTypingEase(t) {
|
|
87
|
+
const eased = 0.5 - 0.5 * Math.cos(Math.PI * t);
|
|
88
|
+
const stutter = Math.sin(t * Math.PI * STUTTER_COUNT) * STUTTER_AMOUNT * 4 * t * (1 - t);
|
|
89
|
+
return Math.min(Math.max(eased + stutter, 0), 1);
|
|
90
|
+
}
|
|
91
|
+
// ==================== TypingReveal ====================
|
|
92
|
+
function TypingReveal({ html, durationMs, revealSteps, onComplete, }) {
|
|
93
|
+
const typingUnitCount = revealSteps.length - 1;
|
|
94
|
+
const [revealPct, setRevealPct] = useState(revealSteps[0]);
|
|
95
|
+
const rafRef = useRef(0);
|
|
96
|
+
const startRef = useRef(0);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (typingUnitCount <= 0) {
|
|
99
|
+
const t = setTimeout(() => {
|
|
100
|
+
setRevealPct(1);
|
|
101
|
+
onComplete();
|
|
102
|
+
}, 0);
|
|
103
|
+
return () => clearTimeout(t);
|
|
104
|
+
}
|
|
105
|
+
startRef.current = performance.now();
|
|
106
|
+
const tick = (now) => {
|
|
107
|
+
const elapsed = now - startRef.current;
|
|
108
|
+
const linearT = Math.min(elapsed / durationMs, 1);
|
|
109
|
+
const easedProgress = humanTypingEase(linearT);
|
|
110
|
+
const stepIdx = Math.min(Math.floor(easedProgress * (typingUnitCount + 1)), typingUnitCount);
|
|
111
|
+
setRevealPct(revealSteps[stepIdx]);
|
|
112
|
+
if (linearT < 1) {
|
|
113
|
+
rafRef.current = requestAnimationFrame(tick);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
setRevealPct(1);
|
|
117
|
+
onComplete();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
rafRef.current = requestAnimationFrame(tick);
|
|
121
|
+
return () => cancelAnimationFrame(rafRef.current);
|
|
122
|
+
}, [durationMs, typingUnitCount, revealSteps, onComplete]);
|
|
123
|
+
return (jsxs("span", { style: { position: 'relative', display: 'inline-block' }, children: [jsx("span", { style: {
|
|
124
|
+
clipPath: `inset(0 ${(1 - revealPct) * 100}% 0 0)`,
|
|
125
|
+
}, dangerouslySetInnerHTML: { __html: html } }), revealPct < 1 && (jsx("span", { style: {
|
|
126
|
+
position: 'absolute',
|
|
127
|
+
top: 0,
|
|
128
|
+
width: '2px',
|
|
129
|
+
backgroundColor: '#1f2937',
|
|
130
|
+
left: `${revealPct * 100}%`,
|
|
131
|
+
height: '1.1em',
|
|
132
|
+
animation: 'slide-renderer-code-cursor-blink 0.6s step-end infinite',
|
|
133
|
+
} }))] }));
|
|
134
|
+
}
|
|
135
|
+
// ==================== CodeLineRow ====================
|
|
136
|
+
function getTypingDuration(content) {
|
|
137
|
+
return Math.max(getTypingCharCount(content) * 40, 250);
|
|
138
|
+
}
|
|
139
|
+
const REPLACE_SELECT_MS = 350;
|
|
140
|
+
function CodeLineRow({ line, lineNumber, highlightedHtml, showLineNumbers, animState, animate, typingDelay, }) {
|
|
141
|
+
const isNewLine = animate && !!animState && animState.type !== 'replaced';
|
|
142
|
+
const isReplace = animate && animState?.type === 'replaced';
|
|
143
|
+
const [mounted, setMounted] = useState(!isNewLine || typingDelay === 0);
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (isNewLine && typingDelay > 0) {
|
|
146
|
+
const timer = setTimeout(() => setMounted(true), typingDelay);
|
|
147
|
+
return () => clearTimeout(timer);
|
|
148
|
+
}
|
|
149
|
+
}, [isNewLine, typingDelay]);
|
|
150
|
+
const [typing, setTyping] = useState(isNewLine && typingDelay === 0);
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (mounted && isNewLine) {
|
|
153
|
+
const t = setTimeout(() => setTyping(true), 0);
|
|
154
|
+
return () => clearTimeout(t);
|
|
155
|
+
}
|
|
156
|
+
}, [mounted, isNewLine]);
|
|
157
|
+
const handleTypingComplete = useCallback(() => {
|
|
158
|
+
setTyping(false);
|
|
159
|
+
}, []);
|
|
160
|
+
const prevHtmlRef = useRef(highlightedHtml);
|
|
161
|
+
const [replacePhase, setReplacePhase] = useState('idle');
|
|
162
|
+
const [oldHtml, setOldHtml] = useState(null);
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (isReplace && prevHtmlRef.current !== highlightedHtml) {
|
|
165
|
+
setOldHtml(prevHtmlRef.current);
|
|
166
|
+
setReplacePhase('select');
|
|
167
|
+
const timer = setTimeout(() => {
|
|
168
|
+
setReplacePhase('type');
|
|
169
|
+
setTyping(true);
|
|
170
|
+
setOldHtml(null);
|
|
171
|
+
prevHtmlRef.current = highlightedHtml;
|
|
172
|
+
}, REPLACE_SELECT_MS);
|
|
173
|
+
return () => clearTimeout(timer);
|
|
174
|
+
}
|
|
175
|
+
prevHtmlRef.current = highlightedHtml;
|
|
176
|
+
}, [isReplace, highlightedHtml]);
|
|
177
|
+
const [highlight, setHighlight] = useState(() => {
|
|
178
|
+
if (!animState)
|
|
179
|
+
return null;
|
|
180
|
+
if (animState.type === 'inserted')
|
|
181
|
+
return 'rgba(34, 197, 94, 0.12)';
|
|
182
|
+
return null;
|
|
183
|
+
});
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
if (animState?.type === 'inserted') {
|
|
186
|
+
const t = setTimeout(() => setHighlight('rgba(34, 197, 94, 0.12)'), 0);
|
|
187
|
+
return () => clearTimeout(t);
|
|
188
|
+
}
|
|
189
|
+
}, [animState]);
|
|
190
|
+
const [highlightFading, setHighlightFading] = useState(false);
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
if (!highlight || highlightFading || typing)
|
|
193
|
+
return;
|
|
194
|
+
const fadeTimer = setTimeout(() => setHighlightFading(true), 0);
|
|
195
|
+
const clearTimer = setTimeout(() => {
|
|
196
|
+
setHighlight(null);
|
|
197
|
+
setHighlightFading(false);
|
|
198
|
+
}, 2000);
|
|
199
|
+
return () => {
|
|
200
|
+
clearTimeout(fadeTimer);
|
|
201
|
+
clearTimeout(clearTimer);
|
|
202
|
+
};
|
|
203
|
+
}, [highlight, typing, highlightFading]);
|
|
204
|
+
const typingDuration = getTypingDuration(line.content);
|
|
205
|
+
const revealSteps = useMemo(() => computeRevealSteps(line.content), [line.content]);
|
|
206
|
+
if (!mounted)
|
|
207
|
+
return null;
|
|
208
|
+
let bgColor = highlight || 'transparent';
|
|
209
|
+
if (replacePhase === 'select')
|
|
210
|
+
bgColor = 'rgba(59, 130, 246, 0.18)';
|
|
211
|
+
let contentNode;
|
|
212
|
+
if (replacePhase === 'select' && oldHtml) {
|
|
213
|
+
contentNode = jsx("span", { dangerouslySetInnerHTML: { __html: oldHtml } });
|
|
214
|
+
}
|
|
215
|
+
else if (typing) {
|
|
216
|
+
contentNode = (jsx(TypingReveal, { html: highlightedHtml, durationMs: typingDuration, revealSteps: revealSteps, onComplete: handleTypingComplete }));
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
contentNode = jsx("span", { dangerouslySetInnerHTML: { __html: highlightedHtml } });
|
|
220
|
+
}
|
|
221
|
+
return (jsxs(motion.div, { initial: isNewLine ? { opacity: 0, height: 0 } : false, animate: {
|
|
222
|
+
opacity: 1,
|
|
223
|
+
height: 'auto',
|
|
224
|
+
backgroundColor: bgColor,
|
|
225
|
+
transition: {
|
|
226
|
+
duration: 0.3,
|
|
227
|
+
ease: [0.16, 1, 0.3, 1],
|
|
228
|
+
backgroundColor: { duration: 0.4 },
|
|
229
|
+
},
|
|
230
|
+
}, exit: {
|
|
231
|
+
backgroundColor: 'rgba(59, 130, 246, 0.18)',
|
|
232
|
+
height: 0,
|
|
233
|
+
opacity: 0,
|
|
234
|
+
transition: {
|
|
235
|
+
backgroundColor: { duration: 0 },
|
|
236
|
+
height: { duration: 0, delay: 0.3 },
|
|
237
|
+
opacity: { duration: 0, delay: 0.3 },
|
|
238
|
+
},
|
|
239
|
+
}, style: {
|
|
240
|
+
display: 'flex',
|
|
241
|
+
lineHeight: 1.6,
|
|
242
|
+
fontSize: 'inherit',
|
|
243
|
+
overflow: 'hidden',
|
|
244
|
+
}, children: [showLineNumbers && (jsx("span", { style: {
|
|
245
|
+
flexShrink: 0,
|
|
246
|
+
textAlign: 'right',
|
|
247
|
+
paddingRight: '16px',
|
|
248
|
+
paddingLeft: '8px',
|
|
249
|
+
width: '3.5em',
|
|
250
|
+
color: '#9ca3af',
|
|
251
|
+
userSelect: 'none',
|
|
252
|
+
}, children: lineNumber })), jsx("span", { style: { flex: 1, paddingRight: '16px', whiteSpace: 'pre', tabSize: 4 }, children: contentNode })] }));
|
|
253
|
+
}
|
|
254
|
+
// ==================== BaseCodeElement ====================
|
|
255
|
+
function BaseCodeElement({ elementInfo, animate }) {
|
|
256
|
+
const { language, lines, fileName, showLineNumbers = true, fontSize = 14 } = elementInfo;
|
|
257
|
+
const wrapperRef = useRef(null);
|
|
258
|
+
const codeBodyRef = useRef(null);
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
const el = codeBodyRef.current;
|
|
261
|
+
if (!el)
|
|
262
|
+
return;
|
|
263
|
+
let dragging = false;
|
|
264
|
+
let startX = 0;
|
|
265
|
+
let startY = 0;
|
|
266
|
+
let startScrollLeft = 0;
|
|
267
|
+
let startScrollTop = 0;
|
|
268
|
+
let activePointer = null;
|
|
269
|
+
const endDrag = () => {
|
|
270
|
+
if (activePointer !== null && el.hasPointerCapture(activePointer)) {
|
|
271
|
+
el.releasePointerCapture(activePointer);
|
|
272
|
+
}
|
|
273
|
+
dragging = false;
|
|
274
|
+
activePointer = null;
|
|
275
|
+
el.style.cursor = 'grab';
|
|
276
|
+
};
|
|
277
|
+
const onPointerDown = (e) => {
|
|
278
|
+
if (e.button !== 0)
|
|
279
|
+
return;
|
|
280
|
+
dragging = true;
|
|
281
|
+
activePointer = e.pointerId;
|
|
282
|
+
startX = e.clientX;
|
|
283
|
+
startY = e.clientY;
|
|
284
|
+
startScrollLeft = el.scrollLeft;
|
|
285
|
+
startScrollTop = el.scrollTop;
|
|
286
|
+
el.setPointerCapture(e.pointerId);
|
|
287
|
+
el.style.cursor = 'grabbing';
|
|
288
|
+
};
|
|
289
|
+
const onPointerMove = (e) => {
|
|
290
|
+
if (!dragging || e.pointerId !== activePointer)
|
|
291
|
+
return;
|
|
292
|
+
el.scrollLeft = startScrollLeft - (e.clientX - startX);
|
|
293
|
+
el.scrollTop = startScrollTop - (e.clientY - startY);
|
|
294
|
+
};
|
|
295
|
+
const onPointerEnd = (e) => {
|
|
296
|
+
if (e.pointerId !== activePointer)
|
|
297
|
+
return;
|
|
298
|
+
endDrag();
|
|
299
|
+
};
|
|
300
|
+
const onLostCapture = () => {
|
|
301
|
+
endDrag();
|
|
302
|
+
};
|
|
303
|
+
const onWheel = (e) => {
|
|
304
|
+
e.stopPropagation();
|
|
305
|
+
};
|
|
306
|
+
el.style.cursor = 'grab';
|
|
307
|
+
el.addEventListener('pointerdown', onPointerDown);
|
|
308
|
+
el.addEventListener('pointermove', onPointerMove);
|
|
309
|
+
el.addEventListener('pointerup', onPointerEnd);
|
|
310
|
+
el.addEventListener('pointercancel', onPointerEnd);
|
|
311
|
+
el.addEventListener('lostpointercapture', onLostCapture);
|
|
312
|
+
el.addEventListener('wheel', onWheel, { passive: true });
|
|
313
|
+
return () => {
|
|
314
|
+
endDrag();
|
|
315
|
+
el.removeEventListener('pointerdown', onPointerDown);
|
|
316
|
+
el.removeEventListener('pointermove', onPointerMove);
|
|
317
|
+
el.removeEventListener('pointerup', onPointerEnd);
|
|
318
|
+
el.removeEventListener('pointercancel', onPointerEnd);
|
|
319
|
+
el.removeEventListener('lostpointercapture', onLostCapture);
|
|
320
|
+
el.removeEventListener('wheel', onWheel);
|
|
321
|
+
};
|
|
322
|
+
}, []);
|
|
323
|
+
useEffect(() => {
|
|
324
|
+
const el = wrapperRef.current;
|
|
325
|
+
if (!el)
|
|
326
|
+
return;
|
|
327
|
+
const stopWheelOutsideBody = (e) => {
|
|
328
|
+
const body = codeBodyRef.current;
|
|
329
|
+
if (body && body.contains(e.target))
|
|
330
|
+
return;
|
|
331
|
+
e.stopPropagation();
|
|
332
|
+
};
|
|
333
|
+
el.addEventListener('wheel', stopWheelOutsideBody);
|
|
334
|
+
return () => el.removeEventListener('wheel', stopWheelOutsideBody);
|
|
335
|
+
}, []);
|
|
336
|
+
const stopPointer = useCallback((e) => {
|
|
337
|
+
e.stopPropagation();
|
|
338
|
+
}, []);
|
|
339
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
340
|
+
const [highlighter, setHighlighter] = useState(null);
|
|
341
|
+
const prevLinesRef = useRef([]);
|
|
342
|
+
const isFirstRenderRef = useRef(true);
|
|
343
|
+
const [animStates, setAnimStates] = useState(() => new Map());
|
|
344
|
+
useEffect(() => {
|
|
345
|
+
const states = new Map();
|
|
346
|
+
if (animate) {
|
|
347
|
+
if (isFirstRenderRef.current) {
|
|
348
|
+
isFirstRenderRef.current = false;
|
|
349
|
+
lines.forEach((line, i) => {
|
|
350
|
+
states.set(line.id, { type: 'typing', timestamp: i * 80 });
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
const prevById = new Map(prevLinesRef.current.map((l) => [l.id, l]));
|
|
355
|
+
for (const line of lines) {
|
|
356
|
+
const prev = prevById.get(line.id);
|
|
357
|
+
if (!prev) {
|
|
358
|
+
states.set(line.id, { type: 'inserted', timestamp: 0 });
|
|
359
|
+
}
|
|
360
|
+
else if (prev.content !== line.content) {
|
|
361
|
+
states.set(line.id, { type: 'replaced', timestamp: 0 });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
prevLinesRef.current = lines;
|
|
366
|
+
}
|
|
367
|
+
const t = setTimeout(() => setAnimStates(states), 0);
|
|
368
|
+
return () => clearTimeout(t);
|
|
369
|
+
}, [lines, animate]);
|
|
370
|
+
useEffect(() => {
|
|
371
|
+
getHighlighter().then(setHighlighter);
|
|
372
|
+
}, []);
|
|
373
|
+
const highlightedLines = useMemo(() => {
|
|
374
|
+
if (!highlighter)
|
|
375
|
+
return null;
|
|
376
|
+
const code = lines.map((l) => l.content).join('\n');
|
|
377
|
+
let lang = language;
|
|
378
|
+
try {
|
|
379
|
+
highlighter.getLoadedLanguages();
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
lang = 'text';
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
const html = highlighter.codeToHtml(code, { lang, theme: 'github-light' });
|
|
386
|
+
const parsed = parseShikiLines(html);
|
|
387
|
+
return lines.map((line, i) => ({
|
|
388
|
+
id: line.id,
|
|
389
|
+
html: parsed[i] || escapeHtml(line.content),
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
return lines.map((line) => ({
|
|
394
|
+
id: line.id,
|
|
395
|
+
html: escapeHtml(line.content),
|
|
396
|
+
}));
|
|
397
|
+
}
|
|
398
|
+
}, [highlighter, lines, language]);
|
|
399
|
+
const fallbackLines = useMemo(() => {
|
|
400
|
+
return lines.map((line) => ({
|
|
401
|
+
id: line.id,
|
|
402
|
+
html: escapeHtml(line.content),
|
|
403
|
+
}));
|
|
404
|
+
}, [lines]);
|
|
405
|
+
const lineHtmlMap = highlightedLines || fallbackLines;
|
|
406
|
+
const typingDelays = useMemo(() => {
|
|
407
|
+
const delays = new Map();
|
|
408
|
+
let cumulative = 0;
|
|
409
|
+
for (const line of lines) {
|
|
410
|
+
if (animStates.has(line.id)) {
|
|
411
|
+
delays.set(line.id, cumulative);
|
|
412
|
+
cumulative += getTypingDuration(line.content) + LINE_GAP_MS;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return delays;
|
|
416
|
+
}, [lines, animStates]);
|
|
417
|
+
const langDisplay = LANG_DISPLAY_NAMES[language] || language;
|
|
418
|
+
return (jsx("div", { ref: wrapperRef, className: "base-element-code", style: {
|
|
419
|
+
position: 'absolute',
|
|
420
|
+
top: `${elementInfo.top}px`,
|
|
421
|
+
left: `${elementInfo.left}px`,
|
|
422
|
+
width: `${elementInfo.width}px`,
|
|
423
|
+
height: `${elementInfo.height}px`,
|
|
424
|
+
}, onPointerDown: stopPointer, onClick: stopPointer, children: jsx("div", { className: "rotate-wrapper", style: {
|
|
425
|
+
width: '100%',
|
|
426
|
+
height: '100%',
|
|
427
|
+
transform: `rotate(${elementInfo.rotate}deg)`,
|
|
428
|
+
}, children: jsxs("div", { className: "element-content", style: {
|
|
429
|
+
width: '100%',
|
|
430
|
+
height: '100%',
|
|
431
|
+
display: 'flex',
|
|
432
|
+
flexDirection: 'column',
|
|
433
|
+
overflow: 'hidden',
|
|
434
|
+
borderRadius: '8px',
|
|
435
|
+
border: '1px solid #d1d5db',
|
|
436
|
+
background: '#fafbfc',
|
|
437
|
+
boxShadow: '0 2px 6px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05)',
|
|
438
|
+
fontFamily: '"JetBrains Mono", "Fira Code", "SF Mono", "Cascadia Code", ui-monospace, SFMono-Regular, "Liberation Mono", Menlo, Monaco, Consolas, monospace',
|
|
439
|
+
fontSize: `${fontSize}px`,
|
|
440
|
+
}, children: [jsxs("div", { style: {
|
|
441
|
+
display: 'flex',
|
|
442
|
+
alignItems: 'center',
|
|
443
|
+
justifyContent: 'space-between',
|
|
444
|
+
flexShrink: 0,
|
|
445
|
+
paddingLeft: '12px',
|
|
446
|
+
paddingRight: '12px',
|
|
447
|
+
height: '32px',
|
|
448
|
+
background: '#f8f9fa',
|
|
449
|
+
borderBottom: '1px solid #e5e7eb',
|
|
450
|
+
}, children: [jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: '8px' }, children: [jsxs("div", { style: { display: 'flex', gap: '6px' }, children: [jsx("div", { style: {
|
|
451
|
+
width: '10px',
|
|
452
|
+
height: '10px',
|
|
453
|
+
borderRadius: '9999px',
|
|
454
|
+
background: '#ff5f57',
|
|
455
|
+
} }), jsx("div", { style: {
|
|
456
|
+
width: '10px',
|
|
457
|
+
height: '10px',
|
|
458
|
+
borderRadius: '9999px',
|
|
459
|
+
background: '#febc2e',
|
|
460
|
+
} }), jsx("div", { style: {
|
|
461
|
+
width: '10px',
|
|
462
|
+
height: '10px',
|
|
463
|
+
borderRadius: '9999px',
|
|
464
|
+
background: '#28c840',
|
|
465
|
+
} })] }), fileName && (jsx("span", { style: {
|
|
466
|
+
marginLeft: '8px',
|
|
467
|
+
overflow: 'hidden',
|
|
468
|
+
textOverflow: 'ellipsis',
|
|
469
|
+
whiteSpace: 'nowrap',
|
|
470
|
+
color: '#6b7280',
|
|
471
|
+
fontSize: '11px',
|
|
472
|
+
letterSpacing: '0.01em',
|
|
473
|
+
}, children: fileName }))] }), jsx("span", { style: {
|
|
474
|
+
color: '#9ca3af',
|
|
475
|
+
fontSize: '10px',
|
|
476
|
+
textTransform: 'uppercase',
|
|
477
|
+
letterSpacing: '0.05em',
|
|
478
|
+
fontWeight: 500,
|
|
479
|
+
}, children: langDisplay })] }), jsx("div", { ref: codeBodyRef, style: {
|
|
480
|
+
flex: 1,
|
|
481
|
+
overflow: 'auto',
|
|
482
|
+
paddingTop: '8px',
|
|
483
|
+
paddingBottom: '8px',
|
|
484
|
+
background: '#fafbfc',
|
|
485
|
+
color: '#24292e',
|
|
486
|
+
userSelect: 'none',
|
|
487
|
+
WebkitUserSelect: 'none',
|
|
488
|
+
touchAction: 'none',
|
|
489
|
+
}, children: jsx("div", { style: { minWidth: 'max-content' }, children: jsx(AnimatePresence, { initial: false, mode: "popLayout", children: lineHtmlMap.map((lineData, index) => {
|
|
490
|
+
const line = lines[index];
|
|
491
|
+
if (!line)
|
|
492
|
+
return null;
|
|
493
|
+
return (jsx(CodeLineRow, { line: line, lineNumber: index + 1, highlightedHtml: lineData.html, showLineNumbers: showLineNumbers, animState: animStates.get(line.id), animate: !!animate, typingDelay: typingDelays.get(line.id) ?? 0 }, line.id));
|
|
494
|
+
}) }) }) })] }) }) }));
|
|
495
|
+
}
|
|
496
|
+
// ==================== Utilities ====================
|
|
497
|
+
function escapeHtml(text) {
|
|
498
|
+
return text
|
|
499
|
+
.replace(/&/g, '&')
|
|
500
|
+
.replace(/</g, '<')
|
|
501
|
+
.replace(/>/g, '>')
|
|
502
|
+
.replace(/"/g, '"');
|
|
503
|
+
}
|
|
504
|
+
const LANG_DISPLAY_NAMES = {
|
|
505
|
+
python: 'Python',
|
|
506
|
+
javascript: 'JavaScript',
|
|
507
|
+
typescript: 'TypeScript',
|
|
508
|
+
json: 'JSON',
|
|
509
|
+
go: 'Go',
|
|
510
|
+
rust: 'Rust',
|
|
511
|
+
java: 'Java',
|
|
512
|
+
c: 'C',
|
|
513
|
+
cpp: 'C++',
|
|
514
|
+
html: 'HTML',
|
|
515
|
+
css: 'CSS',
|
|
516
|
+
bash: 'Bash',
|
|
517
|
+
sql: 'SQL',
|
|
518
|
+
yaml: 'YAML',
|
|
519
|
+
markdown: 'Markdown',
|
|
520
|
+
jsx: 'JSX',
|
|
521
|
+
tsx: 'TSX',
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
export { BaseCodeElement };
|
|
525
|
+
//# sourceMappingURL=BaseCodeElement.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseCodeElement.js","sources":["../../../src/elements/code/BaseCodeElement.tsx"],"sourcesContent":["'use client';\n\nimport { useRef, useState, useEffect, useMemo, useCallback } from 'react';\nimport { motion, AnimatePresence } from 'motion/react';\nimport type { PPTCodeElement, CodeLine } from '@openmaic/dsl';\n\n// ==================== Shiki Singleton ====================\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet highlighterPromise: Promise<any> | null = null;\n\nfunction getHighlighter() {\n if (!highlighterPromise) {\n highlighterPromise = import('shiki').then(({ createHighlighter }) =>\n createHighlighter({\n themes: ['github-light'],\n langs: [\n 'python',\n 'javascript',\n 'typescript',\n 'json',\n 'go',\n 'rust',\n 'java',\n 'c',\n 'cpp',\n 'html',\n 'css',\n 'bash',\n 'sql',\n 'yaml',\n 'markdown',\n 'jsx',\n 'tsx',\n ],\n }),\n );\n }\n return highlighterPromise;\n}\n\n// ==================== Helpers ====================\n\nfunction parseShikiLines(html: string): string[] {\n const codeMatch = html.match(/<code>([\\s\\S]*?)<\\/code>/);\n if (!codeMatch) return [];\n\n const parts = codeMatch[1].split('<span class=\"line\">');\n const lines: string[] = [];\n for (const part of parts) {\n if (!part) continue;\n const endIdx = part.lastIndexOf('</span>');\n if (endIdx !== -1) {\n lines.push(part.substring(0, endIdx));\n }\n }\n return lines;\n}\n\n// ==================== Types ====================\n\nexport interface BaseCodeElementProps {\n elementInfo: PPTCodeElement;\n animate?: boolean;\n}\n\ninterface LineAnimationState {\n type: 'typing' | 'inserted' | 'replaced';\n timestamp: number;\n}\n\n// ==================== Typing Easing ====================\n\nconst STUTTER_COUNT = 5;\nconst STUTTER_AMOUNT = 0.04;\nconst LINE_GAP_MS = 120;\nconst TAB_SIZE = 4;\n\nfunction visualLength(s: string): number {\n let len = 0;\n for (const ch of s) {\n len += ch === '\\t' ? TAB_SIZE : 1;\n }\n return len;\n}\n\nfunction getTypingCharCount(content: string): number {\n const trimmed = content.replace(/^[\\t ]+/, '');\n return trimmed.length;\n}\n\nfunction computeRevealSteps(content: string): number[] {\n const total = visualLength(content);\n if (total === 0) return [1];\n\n const trimmed = content.replace(/^[\\t ]+/, '');\n const indentPart = content.slice(0, content.length - trimmed.length);\n\n let cumVisual = 0;\n for (const ch of indentPart) {\n cumVisual += ch === '\\t' ? TAB_SIZE : 1;\n }\n\n const steps: number[] = [cumVisual / total];\n\n for (const ch of trimmed) {\n cumVisual += ch === '\\t' ? TAB_SIZE : 1;\n steps.push(cumVisual / total);\n }\n\n return steps;\n}\n\nfunction humanTypingEase(t: number): number {\n const eased = 0.5 - 0.5 * Math.cos(Math.PI * t);\n const stutter = Math.sin(t * Math.PI * STUTTER_COUNT) * STUTTER_AMOUNT * 4 * t * (1 - t);\n return Math.min(Math.max(eased + stutter, 0), 1);\n}\n\n// ==================== TypingReveal ====================\n\nfunction TypingReveal({\n html,\n durationMs,\n revealSteps,\n onComplete,\n}: {\n html: string;\n durationMs: number;\n revealSteps: number[];\n onComplete: () => void;\n}) {\n const typingUnitCount = revealSteps.length - 1;\n\n const [revealPct, setRevealPct] = useState(revealSteps[0]);\n const rafRef = useRef<number>(0);\n const startRef = useRef<number>(0);\n\n useEffect(() => {\n if (typingUnitCount <= 0) {\n const t = setTimeout(() => {\n setRevealPct(1);\n onComplete();\n }, 0);\n return () => clearTimeout(t);\n }\n\n startRef.current = performance.now();\n const tick = (now: number) => {\n const elapsed = now - startRef.current;\n const linearT = Math.min(elapsed / durationMs, 1);\n const easedProgress = humanTypingEase(linearT);\n\n const stepIdx = Math.min(Math.floor(easedProgress * (typingUnitCount + 1)), typingUnitCount);\n\n setRevealPct(revealSteps[stepIdx]);\n\n if (linearT < 1) {\n rafRef.current = requestAnimationFrame(tick);\n } else {\n setRevealPct(1);\n onComplete();\n }\n };\n rafRef.current = requestAnimationFrame(tick);\n return () => cancelAnimationFrame(rafRef.current);\n }, [durationMs, typingUnitCount, revealSteps, onComplete]);\n\n return (\n <span style={{ position: 'relative', display: 'inline-block' }}>\n <span\n style={{\n clipPath: `inset(0 ${(1 - revealPct) * 100}% 0 0)`,\n }}\n dangerouslySetInnerHTML={{ __html: html }}\n />\n {revealPct < 1 && (\n <span\n style={{\n position: 'absolute',\n top: 0,\n width: '2px',\n backgroundColor: '#1f2937',\n left: `${revealPct * 100}%`,\n height: '1.1em',\n animation: 'slide-renderer-code-cursor-blink 0.6s step-end infinite',\n }}\n />\n )}\n </span>\n );\n}\n\n// ==================== CodeLineRow ====================\n\nfunction getTypingDuration(content: string): number {\n return Math.max(getTypingCharCount(content) * 40, 250);\n}\n\nconst REPLACE_SELECT_MS = 350;\n\nfunction CodeLineRow({\n line,\n lineNumber,\n highlightedHtml,\n showLineNumbers,\n animState,\n animate,\n typingDelay,\n}: {\n line: CodeLine;\n lineNumber: number;\n highlightedHtml: string;\n showLineNumbers: boolean;\n animState?: LineAnimationState;\n animate: boolean;\n typingDelay: number;\n}) {\n const isNewLine = animate && !!animState && animState.type !== 'replaced';\n const isReplace = animate && animState?.type === 'replaced';\n\n const [mounted, setMounted] = useState(!isNewLine || typingDelay === 0);\n\n useEffect(() => {\n if (isNewLine && typingDelay > 0) {\n const timer = setTimeout(() => setMounted(true), typingDelay);\n return () => clearTimeout(timer);\n }\n }, [isNewLine, typingDelay]);\n\n const [typing, setTyping] = useState(isNewLine && typingDelay === 0);\n\n useEffect(() => {\n if (mounted && isNewLine) {\n const t = setTimeout(() => setTyping(true), 0);\n return () => clearTimeout(t);\n }\n }, [mounted, isNewLine]);\n\n const handleTypingComplete = useCallback(() => {\n setTyping(false);\n }, []);\n\n const prevHtmlRef = useRef(highlightedHtml);\n const [replacePhase, setReplacePhase] = useState<'idle' | 'select' | 'type'>('idle');\n const [oldHtml, setOldHtml] = useState<string | null>(null);\n\n useEffect(() => {\n if (isReplace && prevHtmlRef.current !== highlightedHtml) {\n setOldHtml(prevHtmlRef.current);\n setReplacePhase('select');\n const timer = setTimeout(() => {\n setReplacePhase('type');\n setTyping(true);\n setOldHtml(null);\n prevHtmlRef.current = highlightedHtml;\n }, REPLACE_SELECT_MS);\n return () => clearTimeout(timer);\n }\n prevHtmlRef.current = highlightedHtml;\n }, [isReplace, highlightedHtml]);\n\n const [highlight, setHighlight] = useState<string | null>(() => {\n if (!animState) return null;\n if (animState.type === 'inserted') return 'rgba(34, 197, 94, 0.12)';\n return null;\n });\n\n useEffect(() => {\n if (animState?.type === 'inserted') {\n const t = setTimeout(() => setHighlight('rgba(34, 197, 94, 0.12)'), 0);\n return () => clearTimeout(t);\n }\n }, [animState]);\n\n const [highlightFading, setHighlightFading] = useState(false);\n useEffect(() => {\n if (!highlight || highlightFading || typing) return;\n const fadeTimer = setTimeout(() => setHighlightFading(true), 0);\n const clearTimer = setTimeout(() => {\n setHighlight(null);\n setHighlightFading(false);\n }, 2000);\n return () => {\n clearTimeout(fadeTimer);\n clearTimeout(clearTimer);\n };\n }, [highlight, typing, highlightFading]);\n\n const typingDuration = getTypingDuration(line.content);\n const revealSteps = useMemo(() => computeRevealSteps(line.content), [line.content]);\n\n if (!mounted) return null;\n\n let bgColor = highlight || 'transparent';\n if (replacePhase === 'select') bgColor = 'rgba(59, 130, 246, 0.18)';\n\n let contentNode: React.ReactNode;\n if (replacePhase === 'select' && oldHtml) {\n contentNode = <span dangerouslySetInnerHTML={{ __html: oldHtml }} />;\n } else if (typing) {\n contentNode = (\n <TypingReveal\n html={highlightedHtml}\n durationMs={typingDuration}\n revealSteps={revealSteps}\n onComplete={handleTypingComplete}\n />\n );\n } else {\n contentNode = <span dangerouslySetInnerHTML={{ __html: highlightedHtml }} />;\n }\n\n return (\n <motion.div\n initial={isNewLine ? { opacity: 0, height: 0 } : false}\n animate={{\n opacity: 1,\n height: 'auto',\n backgroundColor: bgColor,\n transition: {\n duration: 0.3,\n ease: [0.16, 1, 0.3, 1],\n backgroundColor: { duration: 0.4 },\n },\n }}\n exit={{\n backgroundColor: 'rgba(59, 130, 246, 0.18)',\n height: 0,\n opacity: 0,\n transition: {\n backgroundColor: { duration: 0 },\n height: { duration: 0, delay: 0.3 },\n opacity: { duration: 0, delay: 0.3 },\n },\n }}\n style={{\n display: 'flex',\n lineHeight: 1.6,\n fontSize: 'inherit',\n overflow: 'hidden',\n }}\n >\n {showLineNumbers && (\n <span\n style={{\n flexShrink: 0,\n textAlign: 'right',\n paddingRight: '16px',\n paddingLeft: '8px',\n width: '3.5em',\n color: '#9ca3af',\n userSelect: 'none',\n }}\n >\n {lineNumber}\n </span>\n )}\n <span style={{ flex: 1, paddingRight: '16px', whiteSpace: 'pre', tabSize: 4 }}>\n {contentNode}\n </span>\n </motion.div>\n );\n}\n\n// ==================== BaseCodeElement ====================\n\nexport function BaseCodeElement({ elementInfo, animate }: BaseCodeElementProps) {\n const { language, lines, fileName, showLineNumbers = true, fontSize = 14 } = elementInfo;\n const wrapperRef = useRef<HTMLDivElement>(null);\n const codeBodyRef = useRef<HTMLDivElement>(null);\n\n useEffect(() => {\n const el = codeBodyRef.current;\n if (!el) return;\n\n let dragging = false;\n let startX = 0;\n let startY = 0;\n let startScrollLeft = 0;\n let startScrollTop = 0;\n let activePointer: number | null = null;\n\n const endDrag = () => {\n if (activePointer !== null && el.hasPointerCapture(activePointer)) {\n el.releasePointerCapture(activePointer);\n }\n dragging = false;\n activePointer = null;\n el.style.cursor = 'grab';\n };\n\n const onPointerDown = (e: PointerEvent) => {\n if (e.button !== 0) return;\n dragging = true;\n activePointer = e.pointerId;\n startX = e.clientX;\n startY = e.clientY;\n startScrollLeft = el.scrollLeft;\n startScrollTop = el.scrollTop;\n el.setPointerCapture(e.pointerId);\n el.style.cursor = 'grabbing';\n };\n\n const onPointerMove = (e: PointerEvent) => {\n if (!dragging || e.pointerId !== activePointer) return;\n el.scrollLeft = startScrollLeft - (e.clientX - startX);\n el.scrollTop = startScrollTop - (e.clientY - startY);\n };\n\n const onPointerEnd = (e: PointerEvent) => {\n if (e.pointerId !== activePointer) return;\n endDrag();\n };\n\n const onLostCapture = () => {\n endDrag();\n };\n\n const onWheel = (e: WheelEvent) => {\n e.stopPropagation();\n };\n\n el.style.cursor = 'grab';\n el.addEventListener('pointerdown', onPointerDown);\n el.addEventListener('pointermove', onPointerMove);\n el.addEventListener('pointerup', onPointerEnd);\n el.addEventListener('pointercancel', onPointerEnd);\n el.addEventListener('lostpointercapture', onLostCapture);\n el.addEventListener('wheel', onWheel, { passive: true });\n return () => {\n endDrag();\n el.removeEventListener('pointerdown', onPointerDown);\n el.removeEventListener('pointermove', onPointerMove);\n el.removeEventListener('pointerup', onPointerEnd);\n el.removeEventListener('pointercancel', onPointerEnd);\n el.removeEventListener('lostpointercapture', onLostCapture);\n el.removeEventListener('wheel', onWheel);\n };\n }, []);\n\n useEffect(() => {\n const el = wrapperRef.current;\n if (!el) return;\n\n const stopWheelOutsideBody = (e: WheelEvent) => {\n const body = codeBodyRef.current;\n if (body && body.contains(e.target as Node)) return;\n e.stopPropagation();\n };\n\n el.addEventListener('wheel', stopWheelOutsideBody);\n return () => el.removeEventListener('wheel', stopWheelOutsideBody);\n }, []);\n\n const stopPointer = useCallback((e: React.SyntheticEvent) => {\n e.stopPropagation();\n }, []);\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const [highlighter, setHighlighter] = useState<any>(null);\n const prevLinesRef = useRef<CodeLine[]>([]);\n const isFirstRenderRef = useRef(true);\n\n const [animStates, setAnimStates] = useState<Map<string, LineAnimationState>>(\n () => new Map<string, LineAnimationState>(),\n );\n\n useEffect(() => {\n const states = new Map<string, LineAnimationState>();\n\n if (animate) {\n if (isFirstRenderRef.current) {\n isFirstRenderRef.current = false;\n lines.forEach((line, i) => {\n states.set(line.id, { type: 'typing', timestamp: i * 80 });\n });\n } else {\n const prevById = new Map(prevLinesRef.current.map((l) => [l.id, l]));\n\n for (const line of lines) {\n const prev = prevById.get(line.id);\n if (!prev) {\n states.set(line.id, { type: 'inserted', timestamp: 0 });\n } else if (prev.content !== line.content) {\n states.set(line.id, { type: 'replaced', timestamp: 0 });\n }\n }\n }\n\n prevLinesRef.current = lines;\n }\n\n const t = setTimeout(() => setAnimStates(states), 0);\n return () => clearTimeout(t);\n }, [lines, animate]);\n\n useEffect(() => {\n getHighlighter().then(setHighlighter);\n }, []);\n\n const highlightedLines = useMemo(() => {\n if (!highlighter) return null;\n\n const code = lines.map((l) => l.content).join('\\n');\n\n let lang = language;\n try {\n highlighter.getLoadedLanguages();\n } catch {\n lang = 'text';\n }\n\n try {\n const html = highlighter.codeToHtml(code, { lang, theme: 'github-light' });\n const parsed = parseShikiLines(html);\n\n return lines.map((line, i) => ({\n id: line.id,\n html: parsed[i] || escapeHtml(line.content),\n }));\n } catch {\n return lines.map((line) => ({\n id: line.id,\n html: escapeHtml(line.content),\n }));\n }\n }, [highlighter, lines, language]);\n\n const fallbackLines = useMemo(() => {\n return lines.map((line) => ({\n id: line.id,\n html: escapeHtml(line.content),\n }));\n }, [lines]);\n\n const lineHtmlMap = highlightedLines || fallbackLines;\n\n const typingDelays = useMemo(() => {\n const delays = new Map<string, number>();\n let cumulative = 0;\n for (const line of lines) {\n if (animStates.has(line.id)) {\n delays.set(line.id, cumulative);\n cumulative += getTypingDuration(line.content) + LINE_GAP_MS;\n }\n }\n return delays;\n }, [lines, animStates]);\n\n const langDisplay = LANG_DISPLAY_NAMES[language] || language;\n\n return (\n <div\n ref={wrapperRef}\n className=\"base-element-code\"\n style={{\n position: 'absolute',\n top: `${elementInfo.top}px`,\n left: `${elementInfo.left}px`,\n width: `${elementInfo.width}px`,\n height: `${elementInfo.height}px`,\n }}\n onPointerDown={stopPointer}\n onClick={stopPointer}\n >\n <div\n className=\"rotate-wrapper\"\n style={{\n width: '100%',\n height: '100%',\n transform: `rotate(${elementInfo.rotate}deg)`,\n }}\n >\n <div\n className=\"element-content\"\n style={{\n width: '100%',\n height: '100%',\n display: 'flex',\n flexDirection: 'column',\n overflow: 'hidden',\n borderRadius: '8px',\n border: '1px solid #d1d5db',\n background: '#fafbfc',\n boxShadow: '0 2px 6px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05)',\n fontFamily:\n '\"JetBrains Mono\", \"Fira Code\", \"SF Mono\", \"Cascadia Code\", ui-monospace, SFMono-Regular, \"Liberation Mono\", Menlo, Monaco, Consolas, monospace',\n fontSize: `${fontSize}px`,\n }}\n >\n <div\n style={{\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'space-between',\n flexShrink: 0,\n paddingLeft: '12px',\n paddingRight: '12px',\n height: '32px',\n background: '#f8f9fa',\n borderBottom: '1px solid #e5e7eb',\n }}\n >\n <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>\n <div style={{ display: 'flex', gap: '6px' }}>\n <div\n style={{\n width: '10px',\n height: '10px',\n borderRadius: '9999px',\n background: '#ff5f57',\n }}\n />\n <div\n style={{\n width: '10px',\n height: '10px',\n borderRadius: '9999px',\n background: '#febc2e',\n }}\n />\n <div\n style={{\n width: '10px',\n height: '10px',\n borderRadius: '9999px',\n background: '#28c840',\n }}\n />\n </div>\n {fileName && (\n <span\n style={{\n marginLeft: '8px',\n overflow: 'hidden',\n textOverflow: 'ellipsis',\n whiteSpace: 'nowrap',\n color: '#6b7280',\n fontSize: '11px',\n letterSpacing: '0.01em',\n }}\n >\n {fileName}\n </span>\n )}\n </div>\n <span\n style={{\n color: '#9ca3af',\n fontSize: '10px',\n textTransform: 'uppercase',\n letterSpacing: '0.05em',\n fontWeight: 500,\n }}\n >\n {langDisplay}\n </span>\n </div>\n\n <div\n ref={codeBodyRef}\n style={{\n flex: 1,\n overflow: 'auto',\n paddingTop: '8px',\n paddingBottom: '8px',\n background: '#fafbfc',\n color: '#24292e',\n userSelect: 'none',\n WebkitUserSelect: 'none',\n touchAction: 'none',\n }}\n >\n <div style={{ minWidth: 'max-content' }}>\n <AnimatePresence initial={false} mode=\"popLayout\">\n {lineHtmlMap.map((lineData, index) => {\n const line = lines[index];\n if (!line) return null;\n return (\n <CodeLineRow\n key={line.id}\n line={line}\n lineNumber={index + 1}\n highlightedHtml={lineData.html}\n showLineNumbers={showLineNumbers}\n animState={animStates.get(line.id)}\n animate={!!animate}\n typingDelay={typingDelays.get(line.id) ?? 0}\n />\n );\n })}\n </AnimatePresence>\n </div>\n </div>\n </div>\n </div>\n </div>\n );\n}\n\n// ==================== Utilities ====================\n\nfunction escapeHtml(text: string): string {\n return text\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"');\n}\n\nconst LANG_DISPLAY_NAMES: Record<string, string> = {\n python: 'Python',\n javascript: 'JavaScript',\n typescript: 'TypeScript',\n json: 'JSON',\n go: 'Go',\n rust: 'Rust',\n java: 'Java',\n c: 'C',\n cpp: 'C++',\n html: 'HTML',\n css: 'CSS',\n bash: 'Bash',\n sql: 'SQL',\n yaml: 'YAML',\n markdown: 'Markdown',\n jsx: 'JSX',\n tsx: 'TSX',\n};\n"],"names":[],"mappings":";;;;;AAMA;AAEA;AACA;AAEA;;AAEI;;AAGI;;;;;;;;;;;;;;;;;;AAkBC;AACF;;AAGL;AACF;AAEA;AAEA;;AAEE;AAAgB;;;AAIhB;AACE;;;AAEA;AACE;;;AAGJ;AACF;AAcA;AAEA;AACA;AACA;AACA;AAEA;;AAEE;AACE;;AAEF;AACF;AAEA;;;AAGA;AAEA;AACE;;;;AAIA;;AAGA;AACE;;AAGF;AAEA;AACE;AACA;;AAGF;AACF;AAEA;AACE;;AAEA;AACF;AAEA;AAEA;AAWE;AAEA;AACA;AACA;;AAGE;AACE;;AAEE;;AAEF;;AAGF;AACA;AACE;AACA;AACA;;AAIA;AAEA;AACE;;;;AAGA;;AAEJ;AACA;;;AAIF;;AAKO;AAMG;AACA;AACA;AACA;AACA;AACA;AACA;;AAMZ;AAEA;AAEA;AACE;AACF;AAEA;AAEA;AAiBE;;AAGA;;AAGE;AACE;AACA;;AAEJ;AAEA;;AAGE;AACE;AACA;;AAEJ;AAEA;;;AAIA;;;;;AAMI;;AAEA;;;;AAIE;;AAEF;;AAEF;AACF;;AAGE;AAAgB;AAChB;AAAmC;AACnC;AACF;;AAGE;AACE;AACA;;AAEJ;;;AAIE;;AACA;AACA;;;;AAIA;;;AAGA;;;;AAMF;AAAc;AAEd;;;AAGA;AACA;;;;;;;;;;AAmBM;AACA;AACA;AACA;AACE;;AAEA;AACD;AACF;AAEC;AACA;AACA;AACA;AACE;;;AAGD;AACF;AAEC;AACA;AACA;AACA;AACD;AAKK;AACA;AACA;AACA;AACA;AACA;AACA;AACD;AAUX;AAEA;;AAGE;AACA;AACA;;AAGE;AACA;;;;;;;;;;AAWI;;;;AAIF;AACF;AAEA;AACE;;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACF;AAEA;AACE;;AACA;AACA;AACF;AAEA;AACE;;AACA;AACF;;AAGE;AACF;AAEA;;AAEA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF;;;AAIA;AACA;;AAEA;AACE;;;;AAGF;AAEA;;;AAIF;;;;;AAMA;AACA;AAEA;;AAKE;;AAGE;AACE;;AAEE;AACF;;;;AAIA;;;AAGI;;;AAEA;;;;AAKN;;AAGF;AACA;AACF;;AAGE;;AAGF;AACE;AAAkB;;;AAKlB;;;AAEE;;;AAIF;AACE;AACA;;;;AAKC;;AACD;;;AAGE;AACD;;;AAIL;;;AAGI;AACD;AACH;AAEA;AAEA;AACE;;AAEA;;;;;;AAMA;AACF;;;AASM;AACA;AACA;AACA;AACA;AACD;AAOG;AACA;AACA;AACD;AAKG;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAOE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACD;AAMO;AACA;AACA;AACA;;AAKA;AACA;AACA;AACA;;AAKA;AACA;AACA;AACA;AACD;AAMC;AACA;AACA;AACA;AACA;AACA;AACA;AACD;AAQH;AACA;AACA;AACA;AACA;;AAUF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACD;AAKK;AACA;AAAW;;;AAqB7B;AAEA;AAEA;AACE;AACG;AACA;AACA;AACA;AACL;AAEA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;"}
|
|
@@ -4,10 +4,10 @@ export interface BaseImageElementProps {
|
|
|
4
4
|
elementInfo: PPTImageElement;
|
|
5
5
|
/**
|
|
6
6
|
* Optional render slot: replace the default <img> with custom content.
|
|
7
|
-
* The slot receives `(element, resolvedSrc)` and is responsible
|
|
8
|
-
* placeholders, retry UI,
|
|
9
|
-
*
|
|
7
|
+
* The slot receives `(element, resolvedSrc, defaultContent)` and is responsible
|
|
8
|
+
* for selecting placeholders, retry UI, or the renderer's prepared image.
|
|
9
|
+
* Its return value is authoritative, including `null`.
|
|
10
10
|
*/
|
|
11
|
-
renderImage?: (element: PPTImageElement, resolvedSrc: string) => ReactNode;
|
|
11
|
+
renderImage?: (element: PPTImageElement, resolvedSrc: string, defaultContent: ReactNode) => ReactNode;
|
|
12
12
|
}
|
|
13
13
|
export declare function BaseImageElement({ elementInfo, renderImage }: BaseImageElementProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { useElementShadow } from '../shared/useElementShadow.js';
|
|
4
|
+
import { useElementFlip } from '../shared/useElementFlip.js';
|
|
5
|
+
import { useClipImage } from './useClipImage.js';
|
|
6
|
+
import { useFilter } from './useFilter.js';
|
|
7
|
+
import { ImageOutline } from './ImageOutline.js';
|
|
8
|
+
|
|
9
|
+
function BaseImageElement({ elementInfo, renderImage }) {
|
|
10
|
+
const { shadowStyle } = useElementShadow(elementInfo.shadow);
|
|
11
|
+
const { flipStyle } = useElementFlip(elementInfo.flipH, elementInfo.flipV);
|
|
12
|
+
const { clipShape, imgPosition } = useClipImage(elementInfo);
|
|
13
|
+
const { filter } = useFilter(elementInfo.filters);
|
|
14
|
+
const src = elementInfo.src;
|
|
15
|
+
// Soft-edge feather (a:softEdge): fade the image alpha to transparent over
|
|
16
|
+
// `softEdge` px at every edge. Two intersecting linear-gradient masks feather
|
|
17
|
+
// all four sides (corners get both). html2canvas-pro ignores CSS masks, so
|
|
18
|
+
// slideToPng bakes the same feather into pixels via the data-soft-edge hook.
|
|
19
|
+
const softEdge = elementInfo.softEdge && elementInfo.softEdge > 0 ? elementInfo.softEdge : undefined;
|
|
20
|
+
const softEdgeMaskStyle = softEdge
|
|
21
|
+
? (() => {
|
|
22
|
+
const grad = (dir) => `linear-gradient(${dir}, transparent 0, #000 ${softEdge}px, #000 calc(100% - ${softEdge}px), transparent 100%)`;
|
|
23
|
+
const mask = `${grad('to right')}, ${grad('to bottom')}`;
|
|
24
|
+
return {
|
|
25
|
+
maskImage: mask,
|
|
26
|
+
WebkitMaskImage: mask,
|
|
27
|
+
maskComposite: 'intersect',
|
|
28
|
+
WebkitMaskComposite: 'source-in',
|
|
29
|
+
maskRepeat: 'no-repeat',
|
|
30
|
+
WebkitMaskRepeat: 'no-repeat',
|
|
31
|
+
};
|
|
32
|
+
})()
|
|
33
|
+
: {};
|
|
34
|
+
const defaultContent = src ? (jsxs(Fragment, { children: [jsx("img", { src: src, draggable: false, "data-soft-edge": softEdge || undefined, style: {
|
|
35
|
+
position: 'absolute',
|
|
36
|
+
top: imgPosition.top,
|
|
37
|
+
left: imgPosition.left,
|
|
38
|
+
width: imgPosition.width,
|
|
39
|
+
height: imgPosition.height,
|
|
40
|
+
maxWidth: 'none',
|
|
41
|
+
maxHeight: 'none',
|
|
42
|
+
filter,
|
|
43
|
+
...softEdgeMaskStyle,
|
|
44
|
+
}, alt: "", onDragStart: (e) => e.preventDefault() }), elementInfo.colorMask && (jsx("div", { style: {
|
|
45
|
+
position: 'absolute',
|
|
46
|
+
inset: 0,
|
|
47
|
+
backgroundColor: elementInfo.colorMask,
|
|
48
|
+
} }))] })) : null;
|
|
49
|
+
return (jsx("div", { className: "element-content", style: {
|
|
50
|
+
position: 'absolute',
|
|
51
|
+
top: `${elementInfo.top}px`,
|
|
52
|
+
left: `${elementInfo.left}px`,
|
|
53
|
+
width: `${elementInfo.width}px`,
|
|
54
|
+
height: `${elementInfo.height}px`,
|
|
55
|
+
}, children: jsx("div", { style: {
|
|
56
|
+
width: '100%',
|
|
57
|
+
height: '100%',
|
|
58
|
+
transform: `rotate(${elementInfo.rotate}deg)`,
|
|
59
|
+
}, children: jsxs("div", { style: {
|
|
60
|
+
position: 'relative',
|
|
61
|
+
width: '100%',
|
|
62
|
+
height: '100%',
|
|
63
|
+
filter: shadowStyle ? `drop-shadow(${shadowStyle})` : '',
|
|
64
|
+
transform: flipStyle,
|
|
65
|
+
}, children: [jsx(ImageOutline, { elementInfo: elementInfo }), jsx("div", { style: {
|
|
66
|
+
position: 'relative',
|
|
67
|
+
width: '100%',
|
|
68
|
+
height: '100%',
|
|
69
|
+
overflow: 'hidden',
|
|
70
|
+
clipPath: clipShape.style,
|
|
71
|
+
pointerEvents: renderImage ? 'auto' : undefined,
|
|
72
|
+
}, children: renderImage ? renderImage(elementInfo, src, defaultContent) : defaultContent })] }) }) }));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { BaseImageElement };
|
|
76
|
+
//# sourceMappingURL=BaseImageElement.js.map
|