@open-slide/core 2.3.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{build-8lRsgsO4.js → build-D7GwYl8d.js} +1 -1
- package/dist/cli/bin.js +4 -4
- package/dist/{config-DJ8ZOe7C.d.ts → config-CfhtJxQH.d.ts} +1 -1
- package/dist/{config-D2Mq1vSW.js → config-D4uPk_tM.js} +383 -138
- package/dist/{dev-BvefEp1Q.js → dev-DyCDMbHB.js} +10 -10
- package/dist/index.d.ts +2 -2
- package/dist/index.js +9 -1
- package/dist/locale/index.d.ts +1 -1
- package/dist/locale/index.js +1 -1
- package/dist/{preview-D8ognobz.js → preview-DjIFvt0K.js} +1 -1
- package/dist/{types-tdaXSyah.d.ts → types-ZfArJF0d.d.ts} +20 -13
- package/dist/{update-BivdNgL3.js → update-BKhRM7o4.js} +30 -3
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DXcqQTrA.js → zh-tw-D7LfGVmA.js} +215 -65
- package/package.json +4 -5
- package/src/agent/provider.ts +8 -1
- package/src/agent/types.ts +17 -2
- package/src/app/app.tsx +2 -0
- package/src/app/components/agent/agent-markdown.tsx +3 -6
- package/src/app/components/agent/agent-panel.tsx +36 -258
- package/src/app/components/agent/agent-presence.tsx +1 -1
- package/src/app/components/agent/composer.tsx +52 -24
- package/src/app/components/agent/effort-slider.tsx +172 -0
- package/src/app/components/agent/model-menu.tsx +170 -0
- package/src/app/components/agent/permission-menu.tsx +88 -0
- package/src/app/components/agent/transcript.tsx +23 -31
- package/src/app/components/command/home-command-menu.tsx +4 -1
- package/src/app/components/command/slide-command-menu.tsx +4 -1
- package/src/app/components/export-progress-toast.tsx +3 -1
- package/src/app/components/inspector/asset-picker-dialog.tsx +1 -1
- package/src/app/components/inspector/comment-layer.tsx +3 -3
- package/src/app/components/inspector/inline-text-editor.tsx +1 -12
- package/src/app/components/inspector/inspect-overlay.tsx +9 -1
- package/src/app/components/inspector/inspector-panel.tsx +8 -35
- package/src/app/components/inspector/inspector-provider.tsx +177 -36
- package/src/app/components/inspector/page-panel.tsx +270 -0
- package/src/app/components/language-toggle.tsx +13 -11
- package/src/app/components/note-markdown.test.tsx +12 -6
- package/src/app/components/note-markdown.tsx +16 -21
- package/src/app/components/overview-grid.tsx +3 -3
- package/src/app/components/panel/editor-sidebar.tsx +43 -8
- package/src/app/components/panel/panel-shell.tsx +9 -5
- package/src/app/components/panel/save-card.tsx +1 -1
- package/src/app/components/sidebar/folder-item.tsx +4 -1
- package/src/app/components/sidebar/sidebar.tsx +11 -8
- package/src/app/components/style-panel/design-provider.tsx +48 -11
- package/src/app/components/style-panel/use-design.ts +6 -1
- package/src/app/components/theme-toggle.tsx +16 -22
- package/src/app/components/thumbnail-rail.tsx +68 -49
- package/src/app/components/ui/context-menu.tsx +9 -10
- package/src/app/components/ui/dialog.tsx +2 -4
- package/src/app/components/ui/dropdown-menu.tsx +9 -10
- package/src/app/components/ui/popover.tsx +1 -1
- package/src/app/components/ui/select.tsx +2 -2
- package/src/app/components/ui/sonner.tsx +2 -2
- package/src/app/index.html +7 -0
- package/src/app/lib/agent/use-agent.ts +131 -37
- package/src/app/lib/desktop-host.ts +31 -0
- package/src/app/lib/desktop-routes.test.ts +54 -0
- package/src/app/lib/desktop-routes.ts +22 -0
- package/src/app/lib/desktop.ts +106 -0
- package/src/app/lib/export-pdf.ts +15 -7
- package/src/app/lib/inspector/color.ts +51 -0
- package/src/app/lib/inspector/fiber.ts +6 -0
- package/src/app/lib/inspector/use-editor.ts +2 -1
- package/src/app/lib/inspector/use-notes.test.ts +209 -0
- package/src/app/lib/inspector/use-notes.ts +331 -57
- package/src/app/lib/inspector/use-visual-editor.ts +9 -1
- package/src/app/lib/locale-store.ts +14 -2
- package/src/app/lib/note-markdown.test.ts +69 -5
- package/src/app/lib/note-markdown.ts +97 -22
- package/src/app/lib/transition.test.ts +84 -1
- package/src/app/lib/transition.ts +43 -0
- package/src/app/lib/unsaved.test.ts +253 -0
- package/src/app/lib/unsaved.ts +155 -0
- package/src/app/routes/home.tsx +1 -1
- package/src/app/routes/slide.tsx +114 -117
- package/src/app/styles.css +421 -1
- package/src/locale/en.ts +40 -15
- package/src/locale/es.ts +40 -17
- package/src/locale/ja.ts +40 -15
- package/src/locale/types.ts +19 -14
- package/src/locale/zh-cn.ts +40 -14
- package/src/locale/zh-tw.ts +40 -14
package/src/app/index.html
CHANGED
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
html.dark {
|
|
14
14
|
background-color: oklch(0.145 0 0);
|
|
15
15
|
}
|
|
16
|
+
html[data-translucent] {
|
|
17
|
+
background-color: transparent;
|
|
18
|
+
}
|
|
16
19
|
</style>
|
|
17
20
|
<script>
|
|
18
21
|
// Apply the stored theme before first paint; next-themes only runs
|
|
@@ -26,6 +29,10 @@
|
|
|
26
29
|
(stored !== 'light' && matchMedia('(prefers-color-scheme: dark)').matches);
|
|
27
30
|
document.documentElement.classList.toggle('dark', dark);
|
|
28
31
|
document.documentElement.style.colorScheme = dark ? 'dark' : 'light';
|
|
32
|
+
if (window.openSlideDesktop) {
|
|
33
|
+
document.documentElement.dataset.shell = 'desktop';
|
|
34
|
+
if (window.openSlideDesktop.translucent) document.documentElement.dataset.translucent = '';
|
|
35
|
+
}
|
|
29
36
|
</script>
|
|
30
37
|
</head>
|
|
31
38
|
<body>
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { useCallback, useEffect, useSyncExternalStore } from 'react';
|
|
2
2
|
import { toast } from 'sonner';
|
|
3
|
+
import { desktopHost, openDeckInHost } from '@/lib/desktop';
|
|
3
4
|
import { useLocale } from '@/lib/use-locale';
|
|
4
5
|
import {
|
|
6
|
+
AGENT_EVENT,
|
|
5
7
|
type AgentEffort,
|
|
6
8
|
type AgentItem,
|
|
7
9
|
type AgentModelOption,
|
|
10
|
+
type AgentPermissionMode,
|
|
8
11
|
type AgentProviderId,
|
|
9
12
|
type AgentSession,
|
|
10
13
|
type AgentSessionSummary,
|
|
14
|
+
type AgentSnapshot,
|
|
11
15
|
type AgentStatus,
|
|
12
16
|
type AgentStreamEvent,
|
|
13
17
|
type AgentTurnContext,
|
|
14
18
|
isAgentEffort,
|
|
19
|
+
isAgentPermissionMode,
|
|
15
20
|
isAgentProviderId,
|
|
16
21
|
type RespondPayload,
|
|
17
22
|
} from '../../../agent/types';
|
|
@@ -29,20 +34,27 @@ type State = {
|
|
|
29
34
|
activeId: string | null;
|
|
30
35
|
provider: AgentProviderId;
|
|
31
36
|
modelChoices: Partial<Record<AgentProviderId, ModelChoice>>;
|
|
32
|
-
|
|
37
|
+
permissionMode: AgentPermissionMode;
|
|
33
38
|
dockOpen: boolean;
|
|
34
39
|
request: AgentRequest | null;
|
|
35
40
|
installing: AgentProviderId | null;
|
|
36
41
|
connectErrors: Partial<Record<AgentProviderId, string>>;
|
|
37
42
|
newDeck: string | null;
|
|
43
|
+
handoff: Handoff | null;
|
|
44
|
+
// Bumped per handoff taken, so one naming the already active session still registers.
|
|
45
|
+
handoffs: number;
|
|
38
46
|
};
|
|
39
47
|
|
|
48
|
+
type Handoff = { slideId: string; sessionId: string };
|
|
49
|
+
|
|
40
50
|
const ACTIVE_KEY = 'open-slide:agent-session';
|
|
41
51
|
const PROVIDER_KEY = 'open-slide:agent-provider';
|
|
42
52
|
const MODEL_KEY = 'open-slide:agent-model';
|
|
43
|
-
const
|
|
53
|
+
const PERMISSION_KEY = 'open-slide:agent-permission-mode';
|
|
44
54
|
const DOCK_KEY = 'open-slide:agent-dock';
|
|
45
55
|
const NEW_DECK_KEY = 'open-slide:agent-new-deck';
|
|
56
|
+
const HANDOFF_KEY = 'open-slide:agent-handoff';
|
|
57
|
+
const HANDOFF_TTL_MS = 30_000;
|
|
46
58
|
|
|
47
59
|
function readStorage(key: string): string | null {
|
|
48
60
|
try {
|
|
@@ -77,6 +89,18 @@ function readModelChoices(): Partial<Record<AgentProviderId, ModelChoice>> {
|
|
|
77
89
|
return choices;
|
|
78
90
|
}
|
|
79
91
|
|
|
92
|
+
function parseHandoff(raw: string | null): Handoff | null {
|
|
93
|
+
if (!raw) return null;
|
|
94
|
+
try {
|
|
95
|
+
const { slideId, sessionId, at } = JSON.parse(raw) as Record<string, unknown>;
|
|
96
|
+
if (typeof slideId !== 'string' || typeof sessionId !== 'string') return null;
|
|
97
|
+
if (typeof at !== 'number' || Date.now() - at > HANDOFF_TTL_MS) return null;
|
|
98
|
+
return { slideId, sessionId };
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
80
104
|
const available = import.meta.env.DEV && typeof window !== 'undefined';
|
|
81
105
|
|
|
82
106
|
let state: State = {
|
|
@@ -91,12 +115,14 @@ let state: State = {
|
|
|
91
115
|
return isAgentProviderId(stored) ? stored : 'claude';
|
|
92
116
|
})(),
|
|
93
117
|
modelChoices: readModelChoices(),
|
|
94
|
-
|
|
118
|
+
permissionMode: readPermissionMode(),
|
|
95
119
|
dockOpen: readStorage(DOCK_KEY) === '1',
|
|
96
120
|
request: null,
|
|
97
121
|
installing: null,
|
|
98
122
|
connectErrors: {},
|
|
99
123
|
newDeck: readStorage(NEW_DECK_KEY),
|
|
124
|
+
handoff: parseHandoff(readStorage(HANDOFF_KEY)),
|
|
125
|
+
handoffs: 0,
|
|
100
126
|
};
|
|
101
127
|
|
|
102
128
|
const listeners = new Set<() => void>();
|
|
@@ -122,28 +148,27 @@ export function useAgentState(): State {
|
|
|
122
148
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
123
149
|
}
|
|
124
150
|
|
|
125
|
-
let
|
|
126
|
-
|
|
151
|
+
let streaming = false;
|
|
152
|
+
|
|
153
|
+
// Anything streamed while the socket was down is gone, so reload what is on
|
|
154
|
+
// screen once it reconnects.
|
|
155
|
+
function resync() {
|
|
156
|
+
void refreshStatus();
|
|
157
|
+
if (state.sessionsLoaded) void loadSessions();
|
|
158
|
+
for (const id of Object.keys(state.full)) void loadSession(id);
|
|
159
|
+
}
|
|
127
160
|
|
|
128
161
|
function syncStream(enabled: boolean) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
162
|
+
const hot = import.meta.hot;
|
|
163
|
+
if (!hot || enabled === streaming) return;
|
|
164
|
+
streaming = enabled;
|
|
165
|
+
if (enabled) {
|
|
166
|
+
hot.on(AGENT_EVENT, applyEvent);
|
|
167
|
+
hot.on('vite:ws:connect', resync);
|
|
168
|
+
} else {
|
|
169
|
+
hot.off(AGENT_EVENT, applyEvent);
|
|
170
|
+
hot.off('vite:ws:connect', resync);
|
|
133
171
|
}
|
|
134
|
-
if (stream) return;
|
|
135
|
-
stream = new EventSource('/__agent/events');
|
|
136
|
-
stream.onmessage = (message) => applyEvent(JSON.parse(message.data) as AgentStreamEvent);
|
|
137
|
-
stream.onopen = () => {
|
|
138
|
-
// Anything streamed while disconnected is gone, so reload what is on screen.
|
|
139
|
-
if (!streamOpened) {
|
|
140
|
-
streamOpened = true;
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
void refreshStatus();
|
|
144
|
-
if (state.sessionsLoaded) void loadSessions();
|
|
145
|
-
for (const id of Object.keys(state.full)) void loadSession(id);
|
|
146
|
-
};
|
|
147
172
|
}
|
|
148
173
|
|
|
149
174
|
function upsertSummary(list: AgentSessionSummary[], summary: AgentSessionSummary) {
|
|
@@ -153,7 +178,36 @@ function upsertSummary(list: AgentSessionSummary[], summary: AgentSessionSummary
|
|
|
153
178
|
);
|
|
154
179
|
}
|
|
155
180
|
|
|
181
|
+
// Events that arrive while each snapshot request is in flight.
|
|
182
|
+
const recovering = new Set<AgentStreamEvent[]>();
|
|
183
|
+
|
|
156
184
|
function applyEvent(event: AgentStreamEvent) {
|
|
185
|
+
for (const missed of recovering) missed.push(event);
|
|
186
|
+
reduceEvent(event);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// A snapshot can land after newer events; replaying the ones it lacks keeps them.
|
|
190
|
+
async function loadSnapshot<T>(
|
|
191
|
+
path: string,
|
|
192
|
+
apply: (snapshot: T) => void,
|
|
193
|
+
affects: (event: AgentStreamEvent) => boolean,
|
|
194
|
+
): Promise<T> {
|
|
195
|
+
const missed: AgentStreamEvent[] = [];
|
|
196
|
+
recovering.add(missed);
|
|
197
|
+
try {
|
|
198
|
+
const { rev, ...rest } = await request<AgentSnapshot<T>>(path);
|
|
199
|
+
const snapshot = rest as unknown as T;
|
|
200
|
+
apply(snapshot);
|
|
201
|
+
for (const event of missed) {
|
|
202
|
+
if ((event.rev ?? 0) > rev && affects(event)) reduceEvent(event);
|
|
203
|
+
}
|
|
204
|
+
return snapshot;
|
|
205
|
+
} finally {
|
|
206
|
+
recovering.delete(missed);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function reduceEvent(event: AgentStreamEvent) {
|
|
157
211
|
switch (event.type) {
|
|
158
212
|
case 'session':
|
|
159
213
|
setState((prev) => {
|
|
@@ -262,13 +316,15 @@ export async function refreshStatus(): Promise<void> {
|
|
|
262
316
|
async function loadSessions(): Promise<void> {
|
|
263
317
|
if (!available) return;
|
|
264
318
|
try {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
sessions,
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
319
|
+
await loadSnapshot<{ sessions: AgentSessionSummary[] }>(
|
|
320
|
+
'/__agent/sessions',
|
|
321
|
+
({ sessions }) => setState({ sessions, sessionsLoaded: true }),
|
|
322
|
+
(event) => event.type === 'session' || event.type === 'session-removed',
|
|
323
|
+
);
|
|
324
|
+
// Only after the replay, which restores sessions created while the request was out.
|
|
325
|
+
setState((prev) =>
|
|
326
|
+
prev.activeId && !prev.sessions.some((s) => s.id === prev.activeId) ? { activeId: null } : {},
|
|
327
|
+
);
|
|
272
328
|
} catch {
|
|
273
329
|
setState({ sessionsLoaded: true });
|
|
274
330
|
}
|
|
@@ -277,9 +333,14 @@ async function loadSessions(): Promise<void> {
|
|
|
277
333
|
async function loadSession(id: string): Promise<AgentSession | null> {
|
|
278
334
|
if (!available) return null;
|
|
279
335
|
try {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
336
|
+
return await loadSnapshot<AgentSession>(
|
|
337
|
+
`/__agent/sessions/${encodeURIComponent(id)}`,
|
|
338
|
+
(session) => setState((prev) => ({ full: { ...prev.full, [id]: session } })),
|
|
339
|
+
(event) =>
|
|
340
|
+
event.type === 'session'
|
|
341
|
+
? event.session.id === id
|
|
342
|
+
: event.type !== 'slide-renamed' && event.sessionId === id,
|
|
343
|
+
);
|
|
283
344
|
} catch {
|
|
284
345
|
setState((prev) => {
|
|
285
346
|
const { [id]: _gone, ...full } = prev.full;
|
|
@@ -312,7 +373,7 @@ function turnSettings(id: string) {
|
|
|
312
373
|
const status = state.status?.providers.find((p) => p.id === provider);
|
|
313
374
|
const choice = status ? resolveModelChoice(status.models, state.modelChoices[provider]) : null;
|
|
314
375
|
return {
|
|
315
|
-
|
|
376
|
+
permissionMode: state.permissionMode,
|
|
316
377
|
model: choice?.model.id || null,
|
|
317
378
|
effort: choice?.effort ?? null,
|
|
318
379
|
};
|
|
@@ -390,6 +451,32 @@ export function setActiveSession(id: string | null): void {
|
|
|
390
451
|
setState({ activeId: id });
|
|
391
452
|
}
|
|
392
453
|
|
|
454
|
+
if (available) {
|
|
455
|
+
window.addEventListener('storage', (e) => {
|
|
456
|
+
if (e.key === HANDOFF_KEY) setState({ handoff: parseHandoff(e.newValue) });
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// Each desktop tab is a separate page on one origin, so a session picked for
|
|
461
|
+
// another deck goes through storage, reaching that deck's tab whether it is
|
|
462
|
+
// already open or yet to load.
|
|
463
|
+
export function openSessionInHost(slideId: string, sessionId: string): boolean {
|
|
464
|
+
if (!desktopHost) return false;
|
|
465
|
+
writeStorage(HANDOFF_KEY, JSON.stringify({ slideId, sessionId, at: Date.now() }));
|
|
466
|
+
return openDeckInHost(slideId);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function useSessionHandoff(slideId: string): void {
|
|
470
|
+
const { handoff } = useAgentState();
|
|
471
|
+
useEffect(() => {
|
|
472
|
+
if (handoff?.slideId !== slideId) return;
|
|
473
|
+
writeStorage(HANDOFF_KEY, null);
|
|
474
|
+
setState((prev) => ({ handoff: null, handoffs: prev.handoffs + 1 }));
|
|
475
|
+
setActiveSession(handoff.sessionId);
|
|
476
|
+
setDockOpen(true);
|
|
477
|
+
}, [handoff, slideId]);
|
|
478
|
+
}
|
|
479
|
+
|
|
393
480
|
export function setModelChoice(provider: AgentProviderId, choice: ModelChoice): void {
|
|
394
481
|
const modelChoices = { ...state.modelChoices, [provider]: choice };
|
|
395
482
|
writeStorage(MODEL_KEY, JSON.stringify(modelChoices));
|
|
@@ -407,9 +494,14 @@ export function resolveModelChoice(
|
|
|
407
494
|
return { model, effort };
|
|
408
495
|
}
|
|
409
496
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
497
|
+
function readPermissionMode(): AgentPermissionMode {
|
|
498
|
+
const stored = readStorage(PERMISSION_KEY);
|
|
499
|
+
return isAgentPermissionMode(stored) ? stored : 'auto';
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export function setPermissionMode(mode: AgentPermissionMode): void {
|
|
503
|
+
writeStorage(PERMISSION_KEY, mode);
|
|
504
|
+
setState({ permissionMode: mode });
|
|
413
505
|
}
|
|
414
506
|
|
|
415
507
|
export function setDockOpen(open: boolean): void {
|
|
@@ -432,7 +524,9 @@ function startBlankDeck(): Promise<void> {
|
|
|
432
524
|
writeStorage(NEW_DECK_KEY, slideId);
|
|
433
525
|
setActiveSession(null);
|
|
434
526
|
setDockOpen(true);
|
|
435
|
-
|
|
527
|
+
if (!openDeckInHost(slideId)) {
|
|
528
|
+
window.location.assign(`${basePath()}/s/${encodeURIComponent(slideId)}`);
|
|
529
|
+
}
|
|
436
530
|
})().finally(() => {
|
|
437
531
|
creatingDeck = null;
|
|
438
532
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type HapticPattern = 'alignment' | 'levelChange' | 'generic';
|
|
2
|
+
|
|
3
|
+
// Drafts wait for an explicit Save; pending work is already on its way to disk.
|
|
4
|
+
export type UnsavedStatus = { drafts: boolean; pending: boolean };
|
|
5
|
+
|
|
6
|
+
// `discard` drops drafts but lets autosaved work land; `abandon` drops everything.
|
|
7
|
+
export type UnsavedAction = 'save' | 'discard' | 'abandon';
|
|
8
|
+
|
|
9
|
+
export type UnsavedOutcome = { ok: true } | { ok: false; error: string };
|
|
10
|
+
|
|
11
|
+
export type UnsavedGuard = {
|
|
12
|
+
status: () => UnsavedStatus;
|
|
13
|
+
settle: (action: UnsavedAction) => Promise<UnsavedOutcome>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Injected by the desktop app's preload into every tab. This file imports
|
|
17
|
+
// nothing so the preload can type-check against it; optional members are
|
|
18
|
+
// missing in older desktop builds.
|
|
19
|
+
export type DesktopHost = {
|
|
20
|
+
translucent: boolean;
|
|
21
|
+
openDeck: (slideId: string) => void;
|
|
22
|
+
openHome: (path?: string) => void;
|
|
23
|
+
setTheme: (theme: string) => void;
|
|
24
|
+
haptic?: (pattern: HapticPattern) => void;
|
|
25
|
+
// Resolves false when the user cancels the save dialog.
|
|
26
|
+
printToPdf?: (filename: string) => Promise<boolean>;
|
|
27
|
+
onNavigate: (listener: (path: string) => void) => () => void;
|
|
28
|
+
// Destroying a page runs no React cleanup, so the host asks before it closes or reloads one.
|
|
29
|
+
guardUnsaved?: (guard: UnsavedGuard) => () => void;
|
|
30
|
+
onTranslucentChange?: (listener: (translucent: boolean) => void) => () => void;
|
|
31
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { deckIdFromPath, isHomePath, stripBase } from './desktop-routes.ts';
|
|
3
|
+
|
|
4
|
+
describe('deckIdFromPath', () => {
|
|
5
|
+
it('reads the deck id from a deck root', () => {
|
|
6
|
+
expect(deckIdFromPath('/s/intro')).toBe('intro');
|
|
7
|
+
expect(deckIdFromPath('/s/intro/')).toBe('intro');
|
|
8
|
+
expect(deckIdFromPath('/s/my%20deck')).toBe('my deck');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('ignores paths below or outside a deck root', () => {
|
|
12
|
+
expect(deckIdFromPath('/s/intro/presenter')).toBeNull();
|
|
13
|
+
expect(deckIdFromPath('/')).toBeNull();
|
|
14
|
+
expect(deckIdFromPath('/s/')).toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns null for a malformed escape instead of throwing', () => {
|
|
18
|
+
expect(deckIdFromPath('/s/%E0%A4%A')).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('strips the base first', () => {
|
|
22
|
+
expect(deckIdFromPath('/docs/s/intro', '/docs')).toBe('intro');
|
|
23
|
+
expect(deckIdFromPath('/s/intro', '/docs')).toBe('intro');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('isHomePath', () => {
|
|
28
|
+
it('treats anything outside /s/ as home', () => {
|
|
29
|
+
expect(isHomePath('/')).toBe(true);
|
|
30
|
+
expect(isHomePath('/?folder=a')).toBe(true);
|
|
31
|
+
expect(isHomePath('/s/intro')).toBe(false);
|
|
32
|
+
expect(isHomePath('/s/intro/presenter')).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('strips the base first', () => {
|
|
36
|
+
expect(isHomePath('/docs', '/docs')).toBe(true);
|
|
37
|
+
expect(isHomePath('/docs/s/intro', '/docs')).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('stripBase', () => {
|
|
42
|
+
it('leaves paths alone without a base', () => {
|
|
43
|
+
expect(stripBase('/s/intro')).toBe('/s/intro');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('maps the bare base to the root', () => {
|
|
47
|
+
expect(stripBase('/docs', '/docs')).toBe('/');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('only strips a whole path segment', () => {
|
|
51
|
+
expect(stripBase('/docs/s/a', '/docs')).toBe('/s/a');
|
|
52
|
+
expect(stripBase('/docsy/s/a', '/docs')).toBe('/docsy/s/a');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const DECK_PATH_RE = /^\/s\/([^/]+)\/?$/;
|
|
2
|
+
|
|
3
|
+
export function stripBase(pathname: string, base = ''): string {
|
|
4
|
+
if (!base || !pathname.startsWith(base)) return pathname;
|
|
5
|
+
const rest = pathname.slice(base.length);
|
|
6
|
+
if (rest === '') return '/';
|
|
7
|
+
return rest.startsWith('/') ? rest : pathname;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function deckIdFromPath(pathname: string, base = ''): string | null {
|
|
11
|
+
const m = DECK_PATH_RE.exec(stripBase(pathname, base));
|
|
12
|
+
if (!m) return null;
|
|
13
|
+
try {
|
|
14
|
+
return decodeURIComponent(m[1]);
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isHomePath(pathname: string, base = ''): boolean {
|
|
21
|
+
return !stripBase(pathname, base).startsWith('/s/');
|
|
22
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { useTheme } from 'next-themes';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { useNavigate } from 'react-router-dom';
|
|
4
|
+
import type { DesktopHost, HapticPattern } from './desktop-host';
|
|
5
|
+
import { deckIdFromPath, isHomePath, stripBase } from './desktop-routes';
|
|
6
|
+
import { slideIds } from './slides';
|
|
7
|
+
import { hasUnsaved, settleUnsaved, unsavedStatus } from './unsaved';
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
interface Window {
|
|
11
|
+
openSlideDesktop?: DesktopHost;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const desktopHost: DesktopHost | undefined =
|
|
16
|
+
typeof window === 'undefined' ? undefined : window.openSlideDesktop;
|
|
17
|
+
|
|
18
|
+
if (desktopHost) {
|
|
19
|
+
desktopHost.guardUnsaved?.({ status: unsavedStatus, settle: settleUnsaved });
|
|
20
|
+
// Covers unloads the host does not start itself, such as a dev-server reload.
|
|
21
|
+
window.addEventListener('beforeunload', (e) => {
|
|
22
|
+
if (hasUnsaved()) e.preventDefault();
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const BASE = import.meta.env.BASE_URL.replace(/\/$/, '');
|
|
27
|
+
|
|
28
|
+
// True when the host took over, so the caller skips its own navigation.
|
|
29
|
+
export function openDeckInHost(slideId: string): boolean {
|
|
30
|
+
if (!desktopHost) return false;
|
|
31
|
+
desktopHost.openDeck(slideId);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function haptic(pattern: HapticPattern) {
|
|
36
|
+
desktopHost?.haptic?.(pattern);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function openHomeInHost(path?: string): boolean {
|
|
40
|
+
if (!desktopHost) return false;
|
|
41
|
+
desktopHost.openHome(path);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function interceptLinkClick(host: DesktopHost, e: MouseEvent) {
|
|
46
|
+
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const anchor = (e.target as Element | null)?.closest?.('a[href]');
|
|
50
|
+
if (!(anchor instanceof HTMLAnchorElement) || (anchor.target && anchor.target !== '_self')) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (anchor.hasAttribute('download')) return;
|
|
54
|
+
const url = new URL(anchor.href);
|
|
55
|
+
if (url.origin !== window.location.origin) return;
|
|
56
|
+
const currentDeck = deckIdFromPath(window.location.pathname, BASE);
|
|
57
|
+
const targetDeck = deckIdFromPath(url.pathname, BASE);
|
|
58
|
+
if (targetDeck && targetDeck !== currentDeck) {
|
|
59
|
+
e.preventDefault();
|
|
60
|
+
host.openDeck(targetDeck);
|
|
61
|
+
} else if (currentDeck && isHomePath(url.pathname, BASE)) {
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
host.openHome(stripBase(url.pathname, BASE) + url.search);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function DesktopBridge() {
|
|
68
|
+
const navigate = useNavigate();
|
|
69
|
+
const { theme } = useTheme();
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (!desktopHost) return;
|
|
73
|
+
const host = desktopHost;
|
|
74
|
+
const onClick = (e: MouseEvent) => interceptLinkClick(host, e);
|
|
75
|
+
document.addEventListener('click', onClick, true);
|
|
76
|
+
return () => document.removeEventListener('click', onClick, true);
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
useEffect(
|
|
80
|
+
() =>
|
|
81
|
+
desktopHost?.onNavigate((path) => {
|
|
82
|
+
// A pre-warmed page's slide list predates decks created after it booted.
|
|
83
|
+
const deck = deckIdFromPath(path, BASE);
|
|
84
|
+
if (deck && !slideIds.includes(deck)) {
|
|
85
|
+
window.location.replace(`${BASE}${path}`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
navigate(path);
|
|
89
|
+
}),
|
|
90
|
+
[navigate],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (theme) desktopHost?.setTheme(theme);
|
|
95
|
+
}, [theme]);
|
|
96
|
+
|
|
97
|
+
useEffect(
|
|
98
|
+
() =>
|
|
99
|
+
desktopHost?.onTranslucentChange?.((translucent) => {
|
|
100
|
+
document.documentElement.toggleAttribute('data-translucent', translucent);
|
|
101
|
+
}),
|
|
102
|
+
[],
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createElement } from 'react';
|
|
2
2
|
import { createRoot, type Root } from 'react-dom/client';
|
|
3
3
|
import { designToCssVars } from './design';
|
|
4
|
+
import { desktopHost } from './desktop';
|
|
4
5
|
import { nextPaint, sleep } from './dom';
|
|
5
6
|
import { SlidePageProvider } from './page-context';
|
|
6
7
|
import { isFrameAnimationSettled, waitForDataWaitfor, waitForFonts } from './print-ready';
|
|
@@ -88,11 +89,11 @@ export function isSafari(): boolean {
|
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
export type PdfExportProgress = {
|
|
91
|
-
phase: 'processing' | 'printing' | 'done';
|
|
92
|
+
phase: 'processing' | 'printing' | 'saving' | 'done';
|
|
92
93
|
/** Number of pages whose intro animations have finished (0..total). */
|
|
93
94
|
current: number;
|
|
94
95
|
total: number;
|
|
95
|
-
/** 0–99 while processing, 99 during printing, 100 when done. */
|
|
96
|
+
/** 0–99 while processing, 99 during printing or saving, 100 when done. */
|
|
96
97
|
percent: number;
|
|
97
98
|
};
|
|
98
99
|
|
|
@@ -155,6 +156,7 @@ export async function exportSlideAsPdf(
|
|
|
155
156
|
|
|
156
157
|
const previousTitle = document.title;
|
|
157
158
|
document.title = slide.meta?.title ?? slideId;
|
|
159
|
+
let finished = false;
|
|
158
160
|
|
|
159
161
|
try {
|
|
160
162
|
await waitForFonts();
|
|
@@ -178,12 +180,18 @@ export async function exportSlideAsPdf(
|
|
|
178
180
|
neutralizeGradientBackgrounds(root);
|
|
179
181
|
await sleep(100); // flush layout
|
|
180
182
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
if (desktopHost?.printToPdf) {
|
|
184
|
+
onProgress?.({ phase: 'saving', current: total, total, percent: 99 });
|
|
185
|
+
finished = await desktopHost.printToPdf(`${document.title}.pdf`);
|
|
186
|
+
} else {
|
|
187
|
+
onProgress?.({ phase: 'printing', current: total, total, percent: 99 });
|
|
188
|
+
const printDone = waitForAfterPrint();
|
|
189
|
+
window.print();
|
|
190
|
+
await printDone;
|
|
191
|
+
finished = true;
|
|
192
|
+
}
|
|
185
193
|
} finally {
|
|
186
|
-
onProgress?.({ phase: 'done', current: total, total, percent: 100 });
|
|
194
|
+
if (finished) onProgress?.({ phase: 'done', current: total, total, percent: 100 });
|
|
187
195
|
document.title = previousTitle;
|
|
188
196
|
for (const r of reactRoots) r.unmount();
|
|
189
197
|
root.remove();
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parseColor, type Rgba, toHex } from '../pptx/css';
|
|
2
|
+
|
|
3
|
+
let painter: CanvasRenderingContext2D | null | undefined;
|
|
4
|
+
const painted = new Map<string, Rgba | null>();
|
|
5
|
+
|
|
6
|
+
// Computed styles keep oklch(), lab(), color() and the like as written, so
|
|
7
|
+
// anything that isn't rgb() is painted onto a pixel and read back as sRGB.
|
|
8
|
+
function paintRgb(value: string): Rgba | null {
|
|
9
|
+
if (painted.has(value)) return painted.get(value) ?? null;
|
|
10
|
+
if (painter === undefined) {
|
|
11
|
+
try {
|
|
12
|
+
const canvas = document.createElement('canvas');
|
|
13
|
+
canvas.width = 1;
|
|
14
|
+
canvas.height = 1;
|
|
15
|
+
painter = canvas.getContext('2d', { willReadFrequently: true });
|
|
16
|
+
} catch {
|
|
17
|
+
painter = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
let result: Rgba | null = null;
|
|
21
|
+
if (painter && typeof CSS !== 'undefined' && CSS.supports('color', value)) {
|
|
22
|
+
painter.clearRect(0, 0, 1, 1);
|
|
23
|
+
painter.fillStyle = value;
|
|
24
|
+
painter.fillRect(0, 0, 1, 1);
|
|
25
|
+
const [r, g, b, a] = painter.getImageData(0, 0, 1, 1).data;
|
|
26
|
+
result = { r, g, b, a: a / 255 };
|
|
27
|
+
}
|
|
28
|
+
if (painted.size >= 256) painted.clear();
|
|
29
|
+
painted.set(value, result);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function toRgba(value: string): Rgba | null {
|
|
34
|
+
if (!value) return null;
|
|
35
|
+
return parseColor(value) ?? paintRgb(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function rgbToHex(value: string): string | null {
|
|
39
|
+
const rgba = toRgba(value);
|
|
40
|
+
return rgba ? `#${toHex(rgba).toLowerCase()}` : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isTransparent(value: string): boolean {
|
|
44
|
+
if (!value) return true;
|
|
45
|
+
const rgba = toRgba(value);
|
|
46
|
+
return rgba ? rgba.a === 0 : false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function opaqueHex(value: string): string | null {
|
|
50
|
+
return isTransparent(value) ? null : rgbToHex(value);
|
|
51
|
+
}
|
|
@@ -16,6 +16,7 @@ type FiberLike = {
|
|
|
16
16
|
stateNode?: unknown;
|
|
17
17
|
_debugSource?: { fileName?: string; lineNumber?: number; columnNumber?: number };
|
|
18
18
|
memoizedProps?: { __source?: { fileName?: string; lineNumber?: number; columnNumber?: number } };
|
|
19
|
+
_debugOwner?: { type?: unknown } | null;
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
function getFiber(el: Element): FiberLike | null {
|
|
@@ -83,3 +84,8 @@ export function findSlideSource(
|
|
|
83
84
|
}
|
|
84
85
|
return null;
|
|
85
86
|
}
|
|
87
|
+
|
|
88
|
+
// The component whose render produced this element's JSX — dev builds only.
|
|
89
|
+
export function jsxOwnerType(el: Element): unknown {
|
|
90
|
+
return getFiber(el)?._debugOwner?.type;
|
|
91
|
+
}
|
|
@@ -12,7 +12,8 @@ export type EditOp =
|
|
|
12
12
|
prevText?: string;
|
|
13
13
|
}
|
|
14
14
|
| { kind: 'set-attr-asset'; attr: string; assetPath: string; previewUrl: string }
|
|
15
|
-
| { kind: 'replace-placeholder-with-image'; assetPath: string }
|
|
15
|
+
| { kind: 'replace-placeholder-with-image'; assetPath: string }
|
|
16
|
+
| { kind: 'remove-element' };
|
|
16
17
|
|
|
17
18
|
export type Edit = { line: number; column: number; ops: EditOp[]; dependsOn?: number };
|
|
18
19
|
|