@lincyaw/web 1.0.0-rc.0 → 1.0.0-rc.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/README.md +33 -4
- package/package.json +20 -19
- package/src/app/base.css +117 -0
- package/src/app/block-playback.ts +81 -0
- package/src/app/block-report.ts +104 -0
- package/src/app/document-modules.ts +30 -0
- package/src/app/env.d.ts +12 -0
- package/src/app/main.ts +198 -0
- package/src/app/page-app.ts +166 -0
- package/src/app/router.ts +109 -0
- package/src/app/server.ts +32 -0
- package/src/app/site/home.tsx +117 -0
- package/src/app/site/layout.tsx +92 -0
- package/src/app/site/search.tsx +22 -0
- package/src/app/site.css +1450 -0
- package/src/client/document.css +172 -0
- package/src/client/host.ts +22 -94
- package/src/client/preview-frame.ts +152 -0
- package/src/client/runtime.ts +159 -0
- package/src/domain/app-config.ts +91 -0
- package/src/domain/config.ts +17 -9
- package/src/domain/dev-updates.ts +12 -0
- package/src/domain/index.ts +17 -7
- package/src/domain/landing.ts +83 -0
- package/src/domain/links.ts +23 -18
- package/src/domain/messages.ts +74 -63
- package/src/domain/pages.ts +23 -41
- package/src/domain/themes.ts +59 -0
- package/src/domain/workspace-home.ts +12 -0
- package/src/node/client-modules.ts +92 -0
- package/src/node/document-bundle.ts +227 -0
- package/src/node/landing-template.ts +34 -0
- package/src/node/landing.ts +82 -0
- package/src/node/offline.ts +34 -11
- package/src/node/publish.ts +83 -15
- package/src/node/renderer.ts +78 -117
- package/src/node/site-assets.ts +17 -0
- package/src/node/site-build.ts +304 -0
- package/src/node/site-files.ts +87 -0
- package/src/node/site-model.ts +337 -0
- package/src/node/site-page.ts +82 -0
- package/src/node/site-server.ts +822 -0
- package/src/node/slides.ts +135 -0
- package/src/node/viewer-app.ts +504 -0
- package/src/node/viewer-build.ts +38 -0
- package/custom-nav-controls.vue +0 -17
- package/src/client/env.d.ts +0 -1
- package/src/client/home.ts +0 -182
- package/src/client/index.ts +0 -4
- package/src/client/page.ts +0 -38
- package/src/client/preview.ts +0 -71
- package/src/client/style.css +0 -427
- package/src/client/theme.ts +0 -322
- package/src/node/addons.ts +0 -76
- package/src/node/site.ts +0 -349
- package/src/node/sites.ts +0 -531
- package/src/node/vite.ts +0 -166
- package/src/node/vitepress.ts +0 -72
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @lincyaw/web
|
|
2
2
|
|
|
3
|
-
Presmium's web renderer: a
|
|
3
|
+
Presmium's web renderer: one prebuilt viewer app shows teaching-document pages in `presmium dev`, `presmium serve`, the workbench and a built site. A built site holds the app once and each document as data beside its pages (its IR, search index, own code bundle and published files); its pages are rendered on the server and then hydrated. The site shell is Presmium's own: a page per chapter, navigation, outline, pager, search, translations with a language menu, light and dark, the phone layout, links to the slides, and workspace sites of several documents with a home page, a 404 page and a sitemap. Independent homepages use their own site-page data or a freely designed HTML entry. The slides form is the viewer app's slides page (`@lincyaw/render-slides`).
|
|
4
4
|
|
|
5
|
-
Part of [Presmium](https://github.com/Lincyaw/presmium), which
|
|
5
|
+
Part of [Presmium](https://github.com/Lincyaw/presmium), which compiles canonical lecture.json teaching documents into web pages, slides, Jupyter notebooks and narrated playback.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -14,11 +14,40 @@ pnpm add -D @lincyaw/cli @lincyaw/web
|
|
|
14
14
|
|
|
15
15
|
## Configuration
|
|
16
16
|
|
|
17
|
-
In
|
|
17
|
+
In `lecture.json`, `metadata.renderers.web`: `base` (the site path), `port` (the dev server), `theme`, `params`, `layout`, and the site shell `nav`, `footer`, `head` and `sitemap`. At a workspace root, the shell options go under `site` in `presmium.workspace.yaml`, with `landing`, `home`, `lang` and `locales.<lang>.title`.
|
|
18
|
+
|
|
19
|
+
Choose the built-in `course`, `docs` or `showcase` theme; `docs` preserves the existing default. Theme parameters select an accent (`blue`, `teal`, `violet`, `amber`), typography (`reading`, `sans`), content width (`comfortable`, `wide`), and an optional `logo` with `logoAlt`. Omit `logoAlt` or set it to an empty string for a decorative logo beside the site title. Document layouts are `article` (default), `landing` and `wide`; `landing` combines all sections into one full-width HTML page. A workspace's theme and brand apply across its documents; each document can choose its own layout.
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
title: My course
|
|
23
|
+
documents: ['*/lecture.json']
|
|
24
|
+
site:
|
|
25
|
+
theme: course
|
|
26
|
+
params:
|
|
27
|
+
accent: teal
|
|
28
|
+
typography: reading
|
|
29
|
+
contentWidth: comfortable
|
|
30
|
+
landing:
|
|
31
|
+
source: ./site/home.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`landing.source` selects an independent site page with a hero and sections, separate from teaching documents. The same page object can be written inline in `landing`. For a freely designed HTML/CSS/JavaScript page, use `landing: { template: './site/home.html' }` instead; Vite bundles that HTML entry and its assets. The template receives reader-facing site data in `script#presmium-site-data` (`schemaVersion`, `title`, `base`, `lang`, `documents`). Teaching content, steps and scripts still belong only in `lecture.json`. Existing `landing: welcome` remains supported: it displays all sections of `welcome/lecture.json` at the site root while keeping that document's normal pages. `home` retains its redirect behavior and cannot be set together with `landing`.
|
|
35
|
+
|
|
36
|
+
For a ready-to-publish workspace:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
presmium new my-site --type site --theme course --title 'My course'
|
|
40
|
+
cd my-site
|
|
41
|
+
pnpm install
|
|
42
|
+
pnpm serve
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`pnpm build` writes `dist/web/`. The generated GitHub Pages workflow builds and deploys that directory using the repository's Pages base path. The [site theme guide](https://github.com/Lincyaw/presmium/blob/main/docs/site-themes.md) covers parameters, page layouts, standalone documents and deployment.
|
|
18
46
|
|
|
19
47
|
## Documentation
|
|
20
48
|
|
|
21
|
-
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/lecture.
|
|
49
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/lecture.json): media, workspaces and translations
|
|
50
|
+
- [Site themes](https://github.com/Lincyaw/presmium/blob/main/docs/site-themes.md): presets, independent homepages and GitHub Pages
|
|
22
51
|
|
|
23
52
|
## License
|
|
24
53
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/web",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
4
|
-
"description": "Presmium's web renderer:
|
|
3
|
+
"version": "1.0.0-rc.2",
|
|
4
|
+
"description": "Presmium's web renderer: one prebuilt viewer that shows the web form in the workbench, in a dev server and as a built site, with search and form switching",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -11,10 +11,7 @@
|
|
|
11
11
|
"homepage": "https://github.com/Lincyaw/presmium/tree/main/packages/web#readme",
|
|
12
12
|
"bugs": "https://github.com/Lincyaw/presmium/issues",
|
|
13
13
|
"keywords": [
|
|
14
|
-
"slidev-addon",
|
|
15
|
-
"slidev",
|
|
16
14
|
"presmium",
|
|
17
|
-
"vitepress",
|
|
18
15
|
"renderer"
|
|
19
16
|
],
|
|
20
17
|
"type": "module",
|
|
@@ -26,34 +23,38 @@
|
|
|
26
23
|
},
|
|
27
24
|
"exports": {
|
|
28
25
|
"./domain": "./src/domain/index.ts",
|
|
29
|
-
"./client": "./src/client/index.ts",
|
|
30
26
|
"./presmium": "./src/node/presmium.ts",
|
|
27
|
+
"./slides": "./src/node/slides.ts",
|
|
31
28
|
"./package.json": "./package.json"
|
|
32
29
|
},
|
|
33
30
|
"files": [
|
|
34
|
-
"custom-nav-controls.vue",
|
|
35
31
|
"src"
|
|
36
32
|
],
|
|
37
|
-
"slidev": {},
|
|
38
33
|
"dependencies": {
|
|
34
|
+
"escape-html": "1.0.3",
|
|
39
35
|
"http-proxy-3": "2.0.1",
|
|
40
36
|
"katex": "0.18.10",
|
|
41
37
|
"launch-editor": "2.14.1",
|
|
42
38
|
"minisearch": "7.2.0",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
39
|
+
"send": "1.2.1",
|
|
40
|
+
"sitemap": "9.0.1",
|
|
41
|
+
"vite": "8.3.4",
|
|
42
|
+
"react": "18.3.1",
|
|
43
|
+
"react-dom": "18.3.1"
|
|
45
44
|
},
|
|
46
45
|
"peerDependencies": {
|
|
47
|
-
"@lincyaw/host": "^1.0.0-rc.
|
|
48
|
-
"@lincyaw/kernel": "^1.0.0-rc.
|
|
49
|
-
"@lincyaw/viewer": "^1.0.0-rc.
|
|
46
|
+
"@lincyaw/host": "^1.0.0-rc.2",
|
|
47
|
+
"@lincyaw/kernel": "^1.0.0-rc.2",
|
|
48
|
+
"@lincyaw/viewer": "^1.0.0-rc.2"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
|
-
"@
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"@lincyaw/
|
|
57
|
-
"@lincyaw/
|
|
51
|
+
"@types/escape-html": "1.0.4",
|
|
52
|
+
"@types/send": "^1.2.1",
|
|
53
|
+
"@types/react": "18.3.28",
|
|
54
|
+
"@types/react-dom": "18.3.7",
|
|
55
|
+
"@lincyaw/cli": "1.0.0-rc.2",
|
|
56
|
+
"@lincyaw/host": "1.0.0-rc.2",
|
|
57
|
+
"@lincyaw/kernel": "1.0.0-rc.2",
|
|
58
|
+
"@lincyaw/viewer": "1.0.0-rc.2"
|
|
58
59
|
}
|
|
59
60
|
}
|
package/src/app/base.css
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/* The viewer app's page: one set of resets for every form, so that components look the same everywhere. */
|
|
2
|
+
|
|
3
|
+
*,
|
|
4
|
+
::before,
|
|
5
|
+
::after {
|
|
6
|
+
box-sizing: border-box;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
html {
|
|
10
|
+
line-height: 1.4;
|
|
11
|
+
font-size: 100%;
|
|
12
|
+
-webkit-text-size-adjust: 100%;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
html.dark {
|
|
16
|
+
color-scheme: dark;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
body {
|
|
20
|
+
margin: 0;
|
|
21
|
+
width: 100%;
|
|
22
|
+
min-height: 100vh;
|
|
23
|
+
background: var(--pm-color-bg);
|
|
24
|
+
color: var(--pm-color-ink);
|
|
25
|
+
font-family: var(--pm-font-sans);
|
|
26
|
+
font-size: 1rem;
|
|
27
|
+
line-height: 1.5rem;
|
|
28
|
+
-webkit-font-smoothing: antialiased;
|
|
29
|
+
text-rendering: optimizeLegibility;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
:is(h1, h2, h3, h4, h5, h6) {
|
|
33
|
+
margin: 0;
|
|
34
|
+
font-size: 1rem;
|
|
35
|
+
font-weight: 400;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
:is(p, blockquote, figure, fieldset) {
|
|
39
|
+
margin: 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
:is(ol, ul) {
|
|
43
|
+
margin: 0;
|
|
44
|
+
padding: 0;
|
|
45
|
+
list-style: none;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
a {
|
|
49
|
+
color: inherit;
|
|
50
|
+
text-decoration: inherit;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
:is(strong, b) {
|
|
54
|
+
font-weight: 600;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
:is(pre, code, kbd, samp) {
|
|
58
|
+
font-family: var(--pm-font-mono);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
:is(img, svg, video, canvas, audio, iframe, embed, object) {
|
|
62
|
+
display: block;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
:is(img, video) {
|
|
66
|
+
max-width: 100%;
|
|
67
|
+
height: auto;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
:is(button, input, optgroup, select, textarea) {
|
|
71
|
+
padding: 0;
|
|
72
|
+
border: 0;
|
|
73
|
+
color: inherit;
|
|
74
|
+
line-height: inherit;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
button {
|
|
78
|
+
background-color: transparent;
|
|
79
|
+
background-image: none;
|
|
80
|
+
font-family: inherit;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
:is(button, [role='button']):enabled {
|
|
84
|
+
cursor: pointer;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
table {
|
|
88
|
+
border-collapse: collapse;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
input {
|
|
92
|
+
background-color: transparent;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
textarea {
|
|
96
|
+
resize: vertical;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fieldset {
|
|
100
|
+
padding: 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
:is(h1, h2, h3, h4, h5, h6, li, p) {
|
|
104
|
+
overflow-wrap: break-word;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* A compiled block widget has its own read-only host, without a page shell. */
|
|
108
|
+
html.pm-block-document,
|
|
109
|
+
.pm-block-document body {
|
|
110
|
+
min-height: 0;
|
|
111
|
+
background: transparent;
|
|
112
|
+
}
|
|
113
|
+
.pm-block-document #app,
|
|
114
|
+
.pm-block-preview {
|
|
115
|
+
width: 100%;
|
|
116
|
+
min-width: 0;
|
|
117
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { presmiumService, PLAYBACK_CONTROL, runtimeRevision } from '@lincyaw/host'
|
|
2
|
+
import type { PlaybackControl } from '@lincyaw/host'
|
|
3
|
+
import { irBlocks, PREVIEW_REVISION, toBlockPreviewPlaybackGranted } from '@lincyaw/kernel'
|
|
4
|
+
import type { BlockPreviewPlaybackCancelled, BlockPreviewPlaybackRequest, IRDocument } from '@lincyaw/kernel'
|
|
5
|
+
import type { Viewer } from '@lincyaw/viewer'
|
|
6
|
+
import type { AppConfig } from '../domain/index.ts'
|
|
7
|
+
|
|
8
|
+
/** Authorise the existing player; this bridge owns no clock or content. */
|
|
9
|
+
export function blockPlaybackGate(config: AppConfig, doc: IRDocument, viewer: Viewer, origin: string | undefined, navigation: () => string | undefined, onGranted: (navigation: string) => void) {
|
|
10
|
+
const random = crypto.getRandomValues(new Uint32Array(2))
|
|
11
|
+
let sequence = (random[0]! & 0x000f_ffff) * 0x1_0000_0000 + random[1]!
|
|
12
|
+
const pending = new Map<number, (error?: Error, navigation?: string) => void>()
|
|
13
|
+
const active = new Map<number, () => void>()
|
|
14
|
+
let disposed = false, armed = false, control: PlaybackControl | undefined
|
|
15
|
+
let stopGate = () => {}, stopRuntime = () => {}
|
|
16
|
+
const revision = doc.meta[PREVIEW_REVISION]
|
|
17
|
+
const abortError = () => new DOMException('Playback cancelled', 'AbortError')
|
|
18
|
+
const cancelled = (request: number) => {
|
|
19
|
+
if (!origin || typeof revision !== 'string') return
|
|
20
|
+
const message: BlockPreviewPlaybackCancelled = { type: 'presmium:block-preview-playback-cancelled', document: config.document, blockId: config.block!, revision, request }
|
|
21
|
+
try { window.parent.postMessage(message, origin) } catch {}
|
|
22
|
+
}
|
|
23
|
+
const cancel = () => {
|
|
24
|
+
for (const finish of [...pending.values()]) finish(abortError())
|
|
25
|
+
for (const release of [...active.values()]) release()
|
|
26
|
+
}
|
|
27
|
+
const authorize = (signal: AbortSignal): Promise<void> => {
|
|
28
|
+
if (disposed || !origin || typeof revision !== 'string' || signal.aborted) return Promise.reject(abortError())
|
|
29
|
+
const position = viewer.position.getSnapshot(), section = viewer.section(position.section)
|
|
30
|
+
const stepId = section?.stepIds?.[position.step], selectedNavigation = navigation()
|
|
31
|
+
if (!section || !stepId || !selectedNavigation || ![...irBlocks(section.blocks)].some(block => block.id === config.block)) return Promise.reject(new Error('The selected preview step is unavailable.'))
|
|
32
|
+
const request = ++sequence
|
|
33
|
+
return new Promise<void>((resolve, reject) => {
|
|
34
|
+
let granted = false
|
|
35
|
+
const abort = () => { if (granted) active.get(request)?.(); else finish(abortError()) }
|
|
36
|
+
const finish = (error?: Error, acceptedNavigation?: string) => {
|
|
37
|
+
if (!pending.delete(request)) return
|
|
38
|
+
clearTimeout(timeout)
|
|
39
|
+
if (error) {
|
|
40
|
+
signal.removeEventListener('abort', abort)
|
|
41
|
+
cancelled(request)
|
|
42
|
+
reject(error)
|
|
43
|
+
} else {
|
|
44
|
+
granted = true
|
|
45
|
+
active.set(request, () => {
|
|
46
|
+
if (!active.delete(request)) return
|
|
47
|
+
signal.removeEventListener('abort', abort)
|
|
48
|
+
cancelled(request)
|
|
49
|
+
})
|
|
50
|
+
onGranted(acceptedNavigation!)
|
|
51
|
+
resolve()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const timeout = setTimeout(() => finish(new Error('Playback authorization timed out.')), 15_000)
|
|
55
|
+
pending.set(request, finish)
|
|
56
|
+
signal.addEventListener('abort', abort, { once: true })
|
|
57
|
+
const message: BlockPreviewPlaybackRequest = { type: 'presmium:block-preview-playback-request', document: config.document, blockId: config.block!, revision, request, sectionId: section.id, stepId, navigation: selectedNavigation }
|
|
58
|
+
try { window.parent.postMessage(message, origin) } catch { finish(new Error('Playback authorization is unavailable.')) }
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
const attach = () => {
|
|
62
|
+
const next = presmiumService(PLAYBACK_CONTROL)
|
|
63
|
+
if (next === control) return
|
|
64
|
+
stopGate(); cancel(); control = next
|
|
65
|
+
stopGate = next?.setGate(authorize) ?? (() => {})
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
/** An authorised embedded surface always joins the parent's playback focus. */
|
|
69
|
+
enable() {
|
|
70
|
+
if (armed || disposed || !origin || window.parent === window) return
|
|
71
|
+
armed = true
|
|
72
|
+
attach()
|
|
73
|
+
stopRuntime = runtimeRevision.subscribe(attach)
|
|
74
|
+
},
|
|
75
|
+
grant(data: unknown) {
|
|
76
|
+
const value = toBlockPreviewPlaybackGranted(data)
|
|
77
|
+
if (!disposed && value?.document === config.document && value.blockId === config.block && value.revision === revision) pending.get(value.request)?.(value.error === undefined ? undefined : new Error(value.error || 'Playback authorization is unavailable.'), value.navigation)
|
|
78
|
+
},
|
|
79
|
+
dispose() { if (disposed) return; disposed = true; stopRuntime(); stopGate(); cancel() },
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { componentErrors, componentErrorRevision, presmiumService, PLAYBACK_CONTROL, STEP_CLOCK } from '@lincyaw/host'
|
|
2
|
+
import { deckLocale, irBlocks, PREVIEW_REVISION, toBlockPreviewCommand } from '@lincyaw/kernel'
|
|
3
|
+
import type { IRDocument } from '@lincyaw/kernel'
|
|
4
|
+
import type { Viewer } from '@lincyaw/viewer'
|
|
5
|
+
import { WEB_MESSAGES } from '../domain/index.ts'
|
|
6
|
+
import type { AppConfig } from '../domain/index.ts'
|
|
7
|
+
import { blockPlaybackGate } from './block-playback.ts'
|
|
8
|
+
|
|
9
|
+
/** A read-only widget reports snapshot status, geometry and discrete navigation to its authorised parent. */
|
|
10
|
+
export function blockPreviewReporter(config: AppConfig, doc: IRDocument, viewer: Viewer): { ready(): Promise<void>, failed(): void } {
|
|
11
|
+
let origin: string | undefined
|
|
12
|
+
try { const referrer = new URL(document.referrer).origin; if (config.origins.includes(referrer)) origin = referrer } catch {}
|
|
13
|
+
const root = document.getElementById('app')!
|
|
14
|
+
let localPosition = false
|
|
15
|
+
let navigation: string | undefined
|
|
16
|
+
const playback = blockPlaybackGate(config, doc, viewer, origin, () => navigation, accepted => { navigation = accepted; localPosition = true })
|
|
17
|
+
playback.enable()
|
|
18
|
+
let enabled = false, fatal = false, queued = false, disposed = false
|
|
19
|
+
let lastReport: string | undefined
|
|
20
|
+
let stopPosition: (() => void) | undefined
|
|
21
|
+
const failed = () => { fatal = true; send() }
|
|
22
|
+
const send = () => {
|
|
23
|
+
if (!enabled || !origin || window.parent === window || disposed) return
|
|
24
|
+
const unavailable = root.querySelector('[data-status="failed"], [data-status="missing"], .pm-component-missing')
|
|
25
|
+
const error = fatal || unavailable || componentErrors().length > 0
|
|
26
|
+
const loading = root.querySelector('.pm-client-only, [data-status="loading"]')
|
|
27
|
+
const revision = doc.meta[PREVIEW_REVISION], status = error ? 'failed' : loading ? 'loading' : 'ready'
|
|
28
|
+
const height = Math.ceil(root.querySelector('.pm-block-preview')?.getBoundingClientRect().height ?? root.scrollHeight)
|
|
29
|
+
const report = JSON.stringify([revision, status, height])
|
|
30
|
+
if (report === lastReport) return
|
|
31
|
+
lastReport = report
|
|
32
|
+
window.parent.postMessage({ type: 'presmium:block-preview', blockId: config.block, document: config.document, revision, status, height, ...error ? { message: WEB_MESSAGES[deckLocale(config.lang)].blockPreviewFailed } : {} }, origin)
|
|
33
|
+
}
|
|
34
|
+
const changed = () => { if (queued || disposed) return; queued = true; queueMicrotask(() => { queued = false; send() }) }
|
|
35
|
+
const resize = new ResizeObserver(changed)
|
|
36
|
+
const mutations = new MutationObserver(changed)
|
|
37
|
+
resize.observe(root)
|
|
38
|
+
mutations.observe(root, { subtree: true, childList: true, attributes: true, attributeFilter: ['data-status', 'class'] })
|
|
39
|
+
const stopErrors = componentErrorRevision.subscribe(changed)
|
|
40
|
+
const sendPosition = () => {
|
|
41
|
+
if (!enabled || !origin || disposed || fatal || !localPosition || !navigation || window.parent === window) return
|
|
42
|
+
const position = viewer.position.getSnapshot(), section = viewer.section(position.section)
|
|
43
|
+
if (!section || ![...irBlocks(section.blocks)].some(block => block.id === config.block)) return
|
|
44
|
+
const stepId = section.stepIds?.[position.step], revision = doc.meta[PREVIEW_REVISION]
|
|
45
|
+
if (typeof stepId !== 'string' || typeof revision !== 'string') return
|
|
46
|
+
window.parent.postMessage({ type: 'presmium:block-preview-position', document: config.document, blockId: config.block, revision, sectionId: section.id, stepId, navigation }, origin)
|
|
47
|
+
}
|
|
48
|
+
const command = (event: MessageEvent) => {
|
|
49
|
+
if (!enabled || disposed || fatal || !origin || event.source !== window.parent || event.origin !== origin) return
|
|
50
|
+
playback.grant(event.data)
|
|
51
|
+
const value = toBlockPreviewCommand(event.data), revision = doc.meta[PREVIEW_REVISION]
|
|
52
|
+
if (!value || value.type !== 'presmium:block-preview-command' || value.document !== config.document || value.blockId !== config.block || typeof revision !== 'string' || value.revision !== revision) return
|
|
53
|
+
// A draft may name a step not in this older snapshot yet; its author can
|
|
54
|
+
// still pause this exact frame without adopting that target.
|
|
55
|
+
if (value.action === 'pause') {
|
|
56
|
+
localPosition = false
|
|
57
|
+
if (value.navigation) navigation = value.navigation
|
|
58
|
+
playback.enable()
|
|
59
|
+
presmiumService(PLAYBACK_CONTROL)?.pause()
|
|
60
|
+
const section = viewer.section(value.sectionId), step = section?.stepIds?.indexOf(value.stepId) ?? -1
|
|
61
|
+
const current = viewer.position.getSnapshot()
|
|
62
|
+
if (value.navigation && section && step >= 0 && [...irBlocks(section.blocks)].some(block => block.id === config.block)
|
|
63
|
+
&& (current.section !== section.id || current.step !== step)) viewer.show({ section: section.id, step, boundary: 'end' })
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
const section = viewer.section(value.sectionId), step = section?.stepIds?.indexOf(value.stepId) ?? -1
|
|
67
|
+
if (!section || step < 0 || ![...irBlocks(section.blocks)].some(block => block.id === config.block)) return
|
|
68
|
+
navigation = value.navigation
|
|
69
|
+
// Acknowledging this frame's accepted position must not seek its active clock.
|
|
70
|
+
if (value.action === 'sync') {
|
|
71
|
+
const current = viewer.position.getSnapshot()
|
|
72
|
+
// Local input may have advanced again while this acknowledgement was in
|
|
73
|
+
// transit. Re-publish that actual cursor under the accepted generation.
|
|
74
|
+
if (current.section !== section.id || current.step !== step) sendPosition()
|
|
75
|
+
return
|
|
76
|
+
}
|
|
77
|
+
// An externally selected step may cause intermediate local React navigation.
|
|
78
|
+
// Only a new local author action or granted clock can publish a new target.
|
|
79
|
+
localPosition = false
|
|
80
|
+
playback.enable()
|
|
81
|
+
presmiumService(PLAYBACK_CONTROL)?.pause()
|
|
82
|
+
viewer.show({ section: section.id, step, boundary: value.at ?? 'end' })
|
|
83
|
+
}
|
|
84
|
+
const localIntent = (event: Event) => { if (event.isTrusted) localPosition = true }
|
|
85
|
+
document.addEventListener('pointerdown', localIntent, true)
|
|
86
|
+
document.addEventListener('keydown', localIntent, true)
|
|
87
|
+
window.addEventListener('message', command)
|
|
88
|
+
window.addEventListener('error', failed)
|
|
89
|
+
window.addEventListener('unhandledrejection', failed)
|
|
90
|
+
window.addEventListener('pagehide', () => { disposed = true; playback.dispose(); resize.disconnect(); mutations.disconnect(); stopErrors(); stopPosition?.(); document.removeEventListener('pointerdown', localIntent, true); document.removeEventListener('keydown', localIntent, true); window.removeEventListener('message', command); window.removeEventListener('error', failed); window.removeEventListener('unhandledrejection', failed) }, { once: true })
|
|
91
|
+
return {
|
|
92
|
+
async ready() {
|
|
93
|
+
await new Promise<void>(done => requestAnimationFrame(() => requestAnimationFrame(() => done())))
|
|
94
|
+
await presmiumService(STEP_CLOCK)?.settled()
|
|
95
|
+
if (disposed) return
|
|
96
|
+
enabled = true
|
|
97
|
+
send()
|
|
98
|
+
// The initial viewer position is only a loading default. Publish subsequent
|
|
99
|
+
// navigation, using the canonical step identity of this block's section.
|
|
100
|
+
stopPosition ??= viewer.position.subscribe(sendPosition)
|
|
101
|
+
},
|
|
102
|
+
failed() { enabled = true; failed() },
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The modules of the document's own bundle that stand for plugins' document modules (`PresmiumNodePlugin.documentModules`):
|
|
3
|
+
* the viewer app is built once for all documents, so a plugin's import of such a module gets a proxy here, which reads
|
|
4
|
+
* the default export the current document's bundle gave.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const current = new Map<string, unknown>()
|
|
8
|
+
|
|
9
|
+
export function setDocumentModules(modules: Record<string, unknown>): void {
|
|
10
|
+
current.clear()
|
|
11
|
+
for (const [id, value] of Object.entries(modules)) current.set(id, value)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const target = (id: string): object => {
|
|
15
|
+
const value = current.get(id)
|
|
16
|
+
return value !== null && typeof value === 'object' ? value : {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The default export of document module `id`, as the current document gives it (an object: its keys are read when used). */
|
|
20
|
+
export function documentModule(id: string): unknown {
|
|
21
|
+
return new Proxy({}, {
|
|
22
|
+
get: (_, key) => Reflect.get(target(id), key),
|
|
23
|
+
has: (_, key) => Reflect.has(target(id), key),
|
|
24
|
+
ownKeys: () => Reflect.ownKeys(target(id)),
|
|
25
|
+
getOwnPropertyDescriptor: (_, key) => {
|
|
26
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(target(id), key)
|
|
27
|
+
return descriptor && { ...descriptor, configurable: true }
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
}
|
package/src/app/env.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
declare module 'virtual:presmium-app/registry' {
|
|
3
|
+
/** The browser modules of each installed plugin package, as the viewer app was built with them. */
|
|
4
|
+
export const packages: Record<string, {
|
|
5
|
+
/** The package's own name. */
|
|
6
|
+
name: string
|
|
7
|
+
components: Record<string, () => Promise<unknown>>
|
|
8
|
+
plugins: (() => Promise<unknown>)[]
|
|
9
|
+
viewers: (() => Promise<unknown>)[]
|
|
10
|
+
restartable: boolean
|
|
11
|
+
}>
|
|
12
|
+
}
|