@lincyaw/addon-stage 0.0.0-stage → 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/LICENSE +202 -0
- package/README.md +29 -2
- package/package.json +45 -3
- package/src/client/drawings.ts +48 -0
- package/src/client/index.ts +3 -0
- package/src/client/locale.ts +7 -0
- package/src/client/screen.ts +6 -0
- package/src/client/viewer.ts +30 -0
- package/src/domain/config.ts +12 -0
- package/src/domain/drawings.ts +23 -0
- package/src/domain/footnotes.ts +38 -0
- package/src/domain/index.ts +8 -0
- package/src/domain/messages.ts +15 -0
- package/src/domain/screen.ts +10 -0
- package/src/node/drawings.ts +49 -0
- package/src/node/presmium.ts +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lincyaw/addon-stage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The stage: black screen, freezing the audience view, drawing on the slides, and footnotes shown next to the block that cites them.
|
|
4
|
+
|
|
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
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add -D @lincyaw/addon-stage
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Add this package to `metadata.plugins` in the document's `lecture.json`.
|
|
14
|
+
|
|
15
|
+
It is a Presmium plugin: its components work on the web page and in the slides. It needs `@lincyaw/cli` and the renderers in the same project, and declares the Presmium packages it shares with them as peer dependencies (Presmium 1.x).
|
|
16
|
+
|
|
17
|
+
## What it provides
|
|
18
|
+
|
|
19
|
+
- The presenter's stage controls in the slides and the present view: black screen (`b`), freeze (`f`).
|
|
20
|
+
- Drawing on the slides (`d`, or the pen in the presentation bar: four inks, the eraser, undo, clear), with drauu. Each slide's drawing is shared between the presenter's and the audience's windows and the classroom, and kept in the document's `.presmium/drawings/` while a server serves the page.
|
|
21
|
+
- A pass that places footnote definitions beside the blocks that cite them.
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/lecture.json) (Chinese: `site/guide/lecture.json`)
|
|
26
|
+
- [Document contract](https://github.com/Lincyaw/presmium/blob/main/.agents/skills/presmium-author/references/document.md): normalized entities, parameters, states and shared validation
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/addon-stage",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.2",
|
|
4
|
+
"description": "Presmium plugin: presenter notes per step, freezing the audience view, and annotations",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Lincyaw/presmium.git",
|
|
9
|
+
"directory": "packages/addon-stage"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Lincyaw/presmium/tree/main/packages/addon-stage#readme",
|
|
12
|
+
"bugs": "https://github.com/Lincyaw/presmium/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"presmium"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22.19"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./domain": "./src/domain/index.ts",
|
|
25
|
+
"./client": "./src/client/index.ts",
|
|
26
|
+
"./presmium": "./src/node/presmium.ts",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@drauu/core": "1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": "^18.3.1",
|
|
37
|
+
"@lincyaw/host": "^1.0.0-rc.2",
|
|
38
|
+
"@lincyaw/kernel": "^1.0.0-rc.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"react": "18.3.1",
|
|
42
|
+
"@types/react": "18.3.28",
|
|
43
|
+
"@lincyaw/cli": "1.0.0-rc.2",
|
|
44
|
+
"@lincyaw/viewer": "1.0.0-rc.2",
|
|
45
|
+
"@lincyaw/kernel": "1.0.0-rc.2",
|
|
46
|
+
"@lincyaw/host": "1.0.0-rc.2"
|
|
47
|
+
}
|
|
6
48
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createDrauu } from '@drauu/core'
|
|
2
|
+
import type { Drauu } from '@drauu/core'
|
|
3
|
+
import { createStore, getSharedStateStore, serverClient, useStore } from '@lincyaw/host'
|
|
4
|
+
import type { useViewer } from '@lincyaw/viewer'
|
|
5
|
+
import { createElement as h, useEffect, useRef } from 'react'
|
|
6
|
+
import type { ComponentType, ReactNode } from 'react'
|
|
7
|
+
import { DRAWINGS_KEY, DRAWINGS_ROUTE, INKS, PEN_WIDTH, STAGE_ID } from '../domain/index.ts'
|
|
8
|
+
import type { PenMode } from '../domain/index.ts'
|
|
9
|
+
import { messages } from './locale.ts'
|
|
10
|
+
const pen = createStore({ on: false, ink: INKS[0] as string, mode: 'stylus' as PenMode })
|
|
11
|
+
const pads = new Map<string, { pad: Drauu, commit(): void }>()
|
|
12
|
+
const timers = new Map<string, ReturnType<typeof setTimeout>>()
|
|
13
|
+
let loaded = false
|
|
14
|
+
const drawingsState = () => getSharedStateStore<Record<string, string>>(DRAWINGS_KEY, {}, { scope: 'remote' })
|
|
15
|
+
function loadSaved(): void {
|
|
16
|
+
const server = serverClient(), drawings = drawingsState()
|
|
17
|
+
if (loaded || !server.available) return
|
|
18
|
+
loaded = true
|
|
19
|
+
void server.fetch(STAGE_ID, DRAWINGS_ROUTE).then(response => response.ok ? response.json() as Promise<Record<string, string>> : {}).then(saved => drawings.set(current => ({ ...saved, ...current }))).catch(() => {})
|
|
20
|
+
}
|
|
21
|
+
function save(section: string, svg: string): void { const server = serverClient(); if (!server.available) return; clearTimeout(timers.get(section)); timers.set(section, setTimeout(() => { timers.delete(section); void server.fetch(STAGE_ID, `${DRAWINGS_ROUTE}/${encodeURIComponent(section)}`, { method: 'PUT', body: svg }).catch(() => {}) }, 800)) }
|
|
22
|
+
export function togglePen(): void { pen.set(value => ({ ...value, on: !value.on })) }
|
|
23
|
+
export function drawingLayer(viewerOf: typeof useViewer): ComponentType<{ view: string, section: string }> {
|
|
24
|
+
return function Drawing({ view, section }) {
|
|
25
|
+
const viewer = viewerOf(), position = useStore(viewer.position), currentPen = useStore(pen), drawings = drawingsState(), content = useStore(drawings)[section] ?? ''
|
|
26
|
+
const svg = useRef<SVGSVGElement>(null), pad = useRef<Drauu>()
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
loadSaved()
|
|
29
|
+
if (view === 'print' || !svg.current || svg.current.closest('[inert]')) return
|
|
30
|
+
const current = createDrauu({ el: svg.current, brush: { mode: pen.getSnapshot().mode, color: pen.getSnapshot().ink, size: PEN_WIDTH } }); pad.current = current; current.load(drawings.getSnapshot()[section] ?? '')
|
|
31
|
+
const commit = () => { const value = current.dump(); if (value !== drawings.getSnapshot()[section]) { drawings.set(old => ({ ...old, [section]: value })); save(section, value) } }
|
|
32
|
+
current.on('committed', commit); pads.set(section, { pad: current, commit })
|
|
33
|
+
return () => { if (pads.get(section)?.pad === current) pads.delete(section); current.unmount(); pad.current = undefined }
|
|
34
|
+
}, [view, section])
|
|
35
|
+
useEffect(() => { if (pad.current && content !== pad.current.dump()) pad.current.load(content) }, [content])
|
|
36
|
+
useEffect(() => { if (pad.current) { pad.current.brush = { ...pad.current.brush, color: currentPen.ink }; pad.current.mode = currentPen.mode } }, [currentPen.ink, currentPen.mode])
|
|
37
|
+
return h('svg', { ref: svg, className: 'pm-stage-drawing', 'aria-hidden': true, style: { position: 'absolute', inset: 0, width: '100%', height: '100%', pointerEvents: currentPen.on && section === position.section ? 'auto' : 'none', cursor: currentPen.on ? 'crosshair' : undefined, touchAction: currentPen.on ? 'none' : undefined }, ...(view === 'print' ? { dangerouslySetInnerHTML: { __html: content } } : {}) })
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function penControls(viewerOf: typeof useViewer): ComponentType {
|
|
41
|
+
return function Pen() {
|
|
42
|
+
const viewer = viewerOf(), position = useStore(viewer.position), currentPen = useStore(pen), t = messages().pen
|
|
43
|
+
const button = (label: string, onClick: () => void, extra: Record<string, unknown> = {}, children?: ReactNode) => h('button', { key: label, type: 'button', className: 'pm-present__button', 'aria-label': label, onClick, ...extra }, children ?? label)
|
|
44
|
+
const current = () => pads.get(position.section)
|
|
45
|
+
if (!currentPen.on) return button(t.draw, togglePen, { 'aria-pressed': false })
|
|
46
|
+
return h('span', {}, button(t.done, togglePen, { 'aria-pressed': true }), ...INKS.map((ink, i) => button(t.ink(i + 1), () => pen.set(old => ({ ...old, ink, mode: 'stylus' })), { 'aria-pressed': currentPen.mode === 'stylus' && currentPen.ink === ink }, h('span', { style: { display: 'block', width: 'var(--pm-size-icon)', height: 'var(--pm-size-icon)', borderRadius: 'var(--pm-radius-pill)', background: ink } }))), button(t.eraser, () => pen.set(old => ({ ...old, mode: 'eraseLine' })), { 'aria-pressed': currentPen.mode === 'eraseLine' }), button(t.undo, () => { const value = current(); if (value?.pad.undo()) value.commit() }), button(t.clear, () => { const value = current(); value?.pad.clear(); value?.commit() }))
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { getSharedStateStore, useStore } from '@lincyaw/host'
|
|
2
|
+
import { SCREEN_KEY, toggleScreen, toScreenMode } from '../domain/index.ts'
|
|
3
|
+
import type { ScreenMode } from '../domain/index.ts'
|
|
4
|
+
const state = () => getSharedStateStore<string>(SCREEN_KEY, 'live', { scope: 'remote' })
|
|
5
|
+
export function toggleScreenMode(wanted: Exclude<ScreenMode, 'live'>): void { state().set(current => toggleScreen(toScreenMode(current), wanted)) }
|
|
6
|
+
export function useScreen(): { mode: ScreenMode, toggle(wanted: Exclude<ScreenMode, 'live'>): void } { return { mode: toScreenMode(useStore(state())), toggle: toggleScreenMode } }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ViewerPluginContext } from '@lincyaw/viewer'
|
|
2
|
+
import { createElement as h, useLayoutEffect, useRef } from 'react'
|
|
3
|
+
import { drawingLayer, penControls, togglePen } from './drawings.ts'
|
|
4
|
+
import { messages } from './locale.ts'
|
|
5
|
+
import { toggleScreenMode, useScreen } from './screen.ts'
|
|
6
|
+
function freeze(frame: HTMLElement): void {
|
|
7
|
+
const present = frame.closest('.pm-present'), slide = present?.querySelector<HTMLElement>('.pm-present__slide')
|
|
8
|
+
if (!present || !slide) return
|
|
9
|
+
const copy = slide.cloneNode(true) as HTMLElement
|
|
10
|
+
for (const el of [copy, ...copy.querySelectorAll('[id]')]) el.removeAttribute('id')
|
|
11
|
+
const outer = present.getBoundingClientRect(), inner = slide.getBoundingClientRect()
|
|
12
|
+
Object.assign(frame.style, { top: `${inner.top - outer.top}px`, left: `${inner.left - outer.left}px`, width: `${inner.width}px`, height: `${inner.height}px`, backgroundColor: getComputedStyle(present).backgroundColor })
|
|
13
|
+
copy.style.margin = '0'; frame.replaceChildren(copy)
|
|
14
|
+
}
|
|
15
|
+
function ScreenLayer({ view }: { view: string }) {
|
|
16
|
+
const { mode } = useScreen(), frame = useRef<HTMLDivElement>(null)
|
|
17
|
+
useLayoutEffect(() => { if (!frame.current) return; if (mode === 'frozen') freeze(frame.current); else frame.current.replaceChildren() }, [mode])
|
|
18
|
+
if (view === 'presenter') return mode === 'live' ? null : h('div', { className: 'pm-stage-badge', 'data-mode': mode, style: { position: 'absolute', top: 'var(--pm-space-3)', right: 'var(--pm-space-3)', zIndex: 10, padding: 'var(--pm-space-1) var(--pm-space-3)', borderRadius: 'var(--pm-radius-pill)', background: mode === 'frozen' ? 'var(--pm-color-accent)' : 'var(--pm-color-ink)', color: mode === 'frozen' ? 'var(--pm-color-on-accent)' : 'var(--pm-color-bg)', fontFamily: 'var(--pm-font-sans)', fontSize: 'var(--pm-text-sm)', pointerEvents: 'none' } }, messages().badge[mode])
|
|
19
|
+
return h('div', { ref: frame, className: 'pm-stage-frozen', 'aria-hidden': true, style: { display: mode === 'frozen' ? 'block' : 'none', position: 'absolute', zIndex: 5, overflow: 'hidden' } })
|
|
20
|
+
}
|
|
21
|
+
function BlackLayer() { const { mode } = useScreen(); return mode === 'black' ? h('div', { className: 'pm-stage-black', style: { position: 'fixed', inset: 0, zIndex: 'var(--pm-z-screen)', background: 'var(--pm-color-terminal-bg)', cursor: 'none' } }) : null }
|
|
22
|
+
export default function stageViewerPlugin({ registerAction, registerLayer, useViewer }: ViewerPluginContext): void {
|
|
23
|
+
registerAction({ id: 'presmium.stage.black', keys: ['b'], run: () => toggleScreenMode('black') })
|
|
24
|
+
registerAction({ id: 'presmium.stage.freeze', keys: ['f'], run: () => toggleScreenMode('frozen') })
|
|
25
|
+
registerAction({ id: 'presmium.stage.draw', keys: ['d'], run: () => togglePen() })
|
|
26
|
+
registerLayer({ id: 'presmium.stage.drawing', component: drawingLayer(useViewer), views: ['present', 'presenter', 'print'], scope: 'slide' })
|
|
27
|
+
registerLayer({ id: 'presmium.stage.pen', component: penControls(useViewer), scope: 'bar' })
|
|
28
|
+
registerLayer({ id: 'presmium.stage.screen', component: ScreenLayer, scope: 'view' })
|
|
29
|
+
registerLayer({ id: 'presmium.stage.black', component: BlackLayer, views: ['present'], scope: 'viewport' })
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const STAGE_ID = 'presmium.stage'
|
|
2
|
+
|
|
3
|
+
/** Where a document's drawings are kept: a file of each slide's, named by its section (`encodeURIComponent(id).svg`). */
|
|
4
|
+
export const DRAWINGS_DIR = '.presmium/drawings'
|
|
5
|
+
|
|
6
|
+
/** The plugin's route of the drawings: `drawings` reads them all, `drawings/<section>` writes one (PUT; empty removes it). */
|
|
7
|
+
export const DRAWINGS_ROUTE = 'drawings'
|
|
8
|
+
|
|
9
|
+
/** The shared state of the drawings: each slide's SVG, by section, as the presenter's and the audience's windows have it. */
|
|
10
|
+
export const DRAWINGS_KEY = 'presmium.stage.drawings'
|
|
11
|
+
|
|
12
|
+
export const SCREEN_KEY = 'presmium.stage.screen'
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The pen's inks: drawn on the slides as they are, in light and in dark. */
|
|
2
|
+
export const INKS = ['#e5484d', '#2f6fde', '#2f9e44', '#f08c00'] as const
|
|
3
|
+
|
|
4
|
+
/** The pen's width on the slide's canvas. */
|
|
5
|
+
export const PEN_WIDTH = 3
|
|
6
|
+
|
|
7
|
+
/** What the pen does: draw (a stroke that follows the pen's pressure), or erase the strokes it crosses. */
|
|
8
|
+
export type PenMode = 'stylus' | 'eraseLine'
|
|
9
|
+
|
|
10
|
+
/** The file of a section's drawing below `DRAWINGS_DIR`. */
|
|
11
|
+
export function drawingFile(section: string): string {
|
|
12
|
+
return `${encodeURIComponent(section)}.svg`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The section of a file below `DRAWINGS_DIR`, if it is a drawing's. */
|
|
16
|
+
export function drawingSection(file: string): string | undefined {
|
|
17
|
+
if (!file.endsWith('.svg')) return undefined
|
|
18
|
+
try {
|
|
19
|
+
return decodeURIComponent(file.slice(0, -'.svg'.length))
|
|
20
|
+
} catch {
|
|
21
|
+
return undefined
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { IRBlock, IRDocument, IRSection, MdastNode } from '@lincyaw/kernel'
|
|
2
|
+
|
|
3
|
+
export function placeFootnotes(doc: IRDocument): void {
|
|
4
|
+
const definitions: { section: IRSection, block: IRBlock, identifier: string }[] = []
|
|
5
|
+
for (const section of doc.sections) {
|
|
6
|
+
for (const block of section.blocks) {
|
|
7
|
+
const identifier = definitionOf(block)
|
|
8
|
+
if (identifier !== undefined) definitions.push({ section, block, identifier })
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
for (const { section, block, identifier } of definitions) {
|
|
12
|
+
const owner = firstReference(doc, identifier)
|
|
13
|
+
if (!owner || (owner.section === section && owner.block.role === block.role)) continue
|
|
14
|
+
section.blocks.splice(section.blocks.indexOf(block), 1)
|
|
15
|
+
const ids = new Set(owner.section.blocks.map(b => b.id))
|
|
16
|
+
let id = block.id
|
|
17
|
+
for (let n = 2; ids.has(id); n++) id = `${block.id}-${n}`
|
|
18
|
+
owner.section.blocks.push({ ...block, id, role: owner.block.role, at: owner.block.at, ...owner.block.only ? { only: owner.block.only } : {} })
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function definitionOf(block: IRBlock): string | undefined {
|
|
23
|
+
if (block.node.type !== 'markdown') return undefined
|
|
24
|
+
const [first, ...rest] = block.node.mdast.children ?? []
|
|
25
|
+
return first?.type === 'footnoteDefinition' && rest.length === 0 && typeof first.identifier === 'string' ? first.identifier : undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function firstReference(doc: IRDocument, identifier: string): { section: IRSection, block: IRBlock } | undefined {
|
|
29
|
+
for (const section of doc.sections) {
|
|
30
|
+
const block = section.blocks.find(b => b.node.type === 'markdown' && references(b.node.mdast, identifier))
|
|
31
|
+
if (block) return { section, block }
|
|
32
|
+
}
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function references(node: MdastNode, identifier: string): boolean {
|
|
37
|
+
return (node.type === 'footnoteReference' && node.identifier === identifier) || (node.children ?? []).some(c => references(c, identifier))
|
|
38
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { DRAWINGS_DIR, DRAWINGS_KEY, DRAWINGS_ROUTE, SCREEN_KEY, STAGE_ID } from './config.ts'
|
|
2
|
+
export { drawingFile, drawingSection, INKS, PEN_WIDTH } from './drawings.ts'
|
|
3
|
+
export type { PenMode } from './drawings.ts'
|
|
4
|
+
export { placeFootnotes } from './footnotes.ts'
|
|
5
|
+
export { MESSAGES } from './messages.ts'
|
|
6
|
+
export type { StageMessages } from './messages.ts'
|
|
7
|
+
export { toggleScreen, toScreenMode } from './screen.ts'
|
|
8
|
+
export type { ScreenMode } from './screen.ts'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Locale } from '@lincyaw/kernel/contracts'
|
|
2
|
+
|
|
3
|
+
const zh = {
|
|
4
|
+
badge: { black: '观众视图已黑屏', frozen: '观众视图已冻结' },
|
|
5
|
+
pen: { draw: '画笔', ink: (n: number) => `墨水 ${n}`, eraser: '橡皮', undo: '撤消', clear: '清除', done: '完成' },
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type StageMessages = typeof zh
|
|
9
|
+
|
|
10
|
+
const en: StageMessages = {
|
|
11
|
+
badge: { black: 'Audience View blacked out', frozen: 'Audience View frozen' },
|
|
12
|
+
pen: { draw: 'Pen', ink: (n: number) => `Ink ${n}`, eraser: 'Eraser', undo: 'Undo', clear: 'Clear', done: 'Done' },
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const MESSAGES: Record<Locale, StageMessages> = { 'zh-CN': zh, 'en': en }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** What the audience sees: the live slide, a black screen, or the slide frozen when it was frozen. */
|
|
2
|
+
export type ScreenMode = 'live' | 'black' | 'frozen'
|
|
3
|
+
|
|
4
|
+
export function toggleScreen(current: ScreenMode, wanted: Exclude<ScreenMode, 'live'>): ScreenMode {
|
|
5
|
+
return current === wanted ? 'live' : wanted
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function toScreenMode(value: unknown): ScreenMode {
|
|
9
|
+
return value === 'black' || value === 'frozen' ? value : 'live'
|
|
10
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
2
|
+
import { join, relative, sep } from 'node:path'
|
|
3
|
+
import { DOCUMENT_FILES, jsonResponse } from '@lincyaw/kernel'
|
|
4
|
+
import type { Route, ServerContext } from '@lincyaw/kernel'
|
|
5
|
+
import { DRAWINGS_DIR, drawingFile, drawingSection } from '../domain/index.ts'
|
|
6
|
+
|
|
7
|
+
// A slide's drawing: an SVG of strokes.
|
|
8
|
+
const MAX_DRAWING = 4 << 20
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The drawings of the document, kept below its `.presmium/drawings/` while the presenter draws in a server's page:
|
|
12
|
+
* written through the workspace's document files where there are (they keep within the workspace), else directly.
|
|
13
|
+
*/
|
|
14
|
+
export function drawingsRoute(ctx: ServerContext): Route {
|
|
15
|
+
const dir = join(ctx.deckDir, DRAWINGS_DIR)
|
|
16
|
+
return {
|
|
17
|
+
role: 'editor',
|
|
18
|
+
maxBody: MAX_DRAWING,
|
|
19
|
+
async handle(request, { rest }) {
|
|
20
|
+
if (request.method === 'GET' && rest === '') {
|
|
21
|
+
const files = await readdir(dir).catch(() => [])
|
|
22
|
+
const drawings: Record<string, string> = {}
|
|
23
|
+
for (const file of files) {
|
|
24
|
+
const section = drawingSection(file)
|
|
25
|
+
if (section !== undefined) drawings[section] = await readFile(join(dir, file), 'utf8')
|
|
26
|
+
}
|
|
27
|
+
return jsonResponse(drawings)
|
|
28
|
+
}
|
|
29
|
+
if (request.method !== 'PUT' || rest === '') return new Response(null, { status: 405 })
|
|
30
|
+
let section: string
|
|
31
|
+
try {
|
|
32
|
+
section = decodeURIComponent(rest)
|
|
33
|
+
} catch {
|
|
34
|
+
return new Response(null, { status: 400 })
|
|
35
|
+
}
|
|
36
|
+
const file = join(dir, drawingFile(section))
|
|
37
|
+
const svg = await request.text()
|
|
38
|
+
const files = ctx.service(DOCUMENT_FILES)
|
|
39
|
+
if (files) {
|
|
40
|
+
const path = relative(ctx.workspaceRoot, file).split(sep).join('/')
|
|
41
|
+
await files.write([svg ? { path, content: Buffer.from(svg) } : { path, remove: true }])
|
|
42
|
+
} else if (svg) {
|
|
43
|
+
await mkdir(dir, { recursive: true })
|
|
44
|
+
await writeFile(file, svg)
|
|
45
|
+
} else await rm(file, { force: true })
|
|
46
|
+
return new Response(null, { status: 204 })
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import type { PresmiumNodePlugin } from '@lincyaw/kernel'
|
|
3
|
+
import { DRAWINGS_ROUTE, placeFootnotes, STAGE_ID } from '../domain/index.ts'
|
|
4
|
+
import { drawingsRoute } from './drawings.ts'
|
|
5
|
+
|
|
6
|
+
const { version } = createRequire(import.meta.url)('../../package.json') as { version: string }
|
|
7
|
+
|
|
8
|
+
const plugin: PresmiumNodePlugin = {
|
|
9
|
+
id: STAGE_ID,
|
|
10
|
+
version,
|
|
11
|
+
kernel: '^1.0.0',
|
|
12
|
+
passes: [{ name: 'presmium.stage.footnotes', run: doc => placeFootnotes(doc) }],
|
|
13
|
+
server: ctx => ({ routes: { [`${DRAWINGS_ROUTE}/*`]: drawingsRoute(ctx) } }),
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default plugin
|