@lincyaw/addon-guide 0.0.0-stage → 1.0.0-rc.10
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 +28 -2
- package/components/PmCursor.tsx +7 -0
- package/components/PmHint.css +20 -0
- package/components/PmHint.tsx +19 -0
- package/components/PmSpotlight.css +5 -0
- package/components/PmSpotlight.tsx +23 -0
- package/package.json +50 -3
- package/src/client/CueNote.tsx +27 -0
- package/src/client/CursorPath.css +58 -0
- package/src/client/CursorPath.tsx +85 -0
- package/src/client/cue.ts +28 -0
- package/src/client/env.d.ts +1 -0
- package/src/client/geometry.ts +15 -0
- package/src/client/hints.css +7 -0
- package/src/client/hints.ts +40 -0
- package/src/client/index.ts +9 -0
- package/src/client/operation-cues.ts +122 -0
- package/src/client/plugin.ts +40 -0
- package/src/client/spotlight.css +32 -0
- package/src/client/spotlight.ts +64 -0
- package/src/client/stage.ts +5 -0
- package/src/client/targets.ts +25 -0
- package/src/client/viewer.ts +23 -0
- package/src/domain/cues.ts +54 -0
- package/src/domain/index.ts +4 -0
- package/src/domain/messages.ts +10 -0
- package/src/node/presmium.ts +53 -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,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lincyaw/addon-guide
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Guidance: a simulated cursor, spotlights and hints that follow the steps.
|
|
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-guide
|
|
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
|
+
- Cursor, spotlight and hint components declared through versioned definitions.
|
|
20
|
+
- Structured narration cues can target stable blocks and code lines through the shared host services.
|
|
21
|
+
|
|
22
|
+
## Documentation
|
|
23
|
+
|
|
24
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/lecture.json) (Chinese: `site/guide/lecture.json`)
|
|
25
|
+
- [Document contract](https://github.com/Lincyaw/presmium/blob/main/.agents/skills/presmium-author/references/document.md): normalized entities, parameters, states and shared validation
|
|
26
|
+
|
|
27
|
+
## License
|
|
28
|
+
|
|
29
|
+
Apache-2.0
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import CursorPath from '../src/client/CursorPath.tsx'
|
|
2
|
+
import { planCursorPath } from '../src/domain/index.ts'
|
|
3
|
+
import type { Waypoint } from '../src/domain/index.ts'
|
|
4
|
+
export default function PmCursor({ path }: { path: Waypoint[] }) {
|
|
5
|
+
const plan = planCursorPath(path ?? [])
|
|
6
|
+
return <CursorPath key={JSON.stringify(plan)} plan={plan} />
|
|
7
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[data-pm-hint='inline'] {
|
|
2
|
+
outline: var(--pm-size-stroke) solid var(--pm-color-accent);
|
|
3
|
+
outline-offset: var(--pm-size-rule);
|
|
4
|
+
animation: pm-hint-ring var(--pm-duration-guide-ring) ease-out infinite;
|
|
5
|
+
}
|
|
6
|
+
@keyframes pm-hint-ring {
|
|
7
|
+
from {
|
|
8
|
+
outline-offset: var(--pm-size-stroke);
|
|
9
|
+
outline-color: var(--pm-color-accent);
|
|
10
|
+
}
|
|
11
|
+
to {
|
|
12
|
+
outline-offset: var(--pm-guide-ring-spread);
|
|
13
|
+
outline-color: transparent;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
@media (prefers-reduced-motion: reduce) {
|
|
17
|
+
[data-pm-hint='inline'] {
|
|
18
|
+
animation: none;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import CueNote from '../src/client/CueNote.tsx'
|
|
2
|
+
import { requestHint, useCue } from '../src/client/index.ts'
|
|
3
|
+
import type { CueProps } from '../src/client/cue.ts'
|
|
4
|
+
import './PmHint.css'
|
|
5
|
+
export default function PmHint(props: CueProps) {
|
|
6
|
+
const { anchor, look, note } = useCue(props, 'data-pm-hint', requestHint)
|
|
7
|
+
return (
|
|
8
|
+
<>
|
|
9
|
+
<CueNote title={props.title} text={props.text} state={note} />
|
|
10
|
+
<span
|
|
11
|
+
ref={anchor}
|
|
12
|
+
className="pm-hint"
|
|
13
|
+
hidden
|
|
14
|
+
data-target={props.target}
|
|
15
|
+
data-look={look ?? 'off'}
|
|
16
|
+
/>
|
|
17
|
+
</>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import CueNote from '../src/client/CueNote.tsx'
|
|
2
|
+
import { requestSpotlight, useCue } from '../src/client/index.ts'
|
|
3
|
+
import type { CueProps } from '../src/client/cue.ts'
|
|
4
|
+
import './PmSpotlight.css'
|
|
5
|
+
export default function PmSpotlight(props: CueProps) {
|
|
6
|
+
const { anchor, look, note } = useCue(
|
|
7
|
+
props,
|
|
8
|
+
'data-pm-spotlight',
|
|
9
|
+
requestSpotlight,
|
|
10
|
+
)
|
|
11
|
+
return (
|
|
12
|
+
<>
|
|
13
|
+
<CueNote title={props.title} text={props.text} state={note} />
|
|
14
|
+
<span
|
|
15
|
+
ref={anchor}
|
|
16
|
+
className="pm-spotlight"
|
|
17
|
+
hidden
|
|
18
|
+
data-target={props.target}
|
|
19
|
+
data-look={look ?? 'off'}
|
|
20
|
+
/>
|
|
21
|
+
</>
|
|
22
|
+
)
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/addon-guide",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
|
+
"description": "Presmium plugin: guidance cues: a simulated cursor, spotlights and hints",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Lincyaw/presmium.git",
|
|
9
|
+
"directory": "packages/addon-guide"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Lincyaw/presmium/tree/main/packages/addon-guide#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
|
+
"components",
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"driver.js": "^1.9.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react": "^18.3.1",
|
|
38
|
+
"@lincyaw/host": "^1.0.0-rc.10",
|
|
39
|
+
"@lincyaw/kernel": "^1.0.0-rc.10"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"react": "18.3.1",
|
|
43
|
+
"@types/react": "18.3.28",
|
|
44
|
+
"@lincyaw/viewer": "1.0.0-rc.10",
|
|
45
|
+
"@lincyaw/kernel": "1.0.0-rc.10",
|
|
46
|
+
"@lincyaw/host": "1.0.0-rc.10"
|
|
47
|
+
},
|
|
48
|
+
"presmium": {
|
|
49
|
+
"preview": {
|
|
50
|
+
"restartable": true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
6
53
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useMessages } from '@lincyaw/host'
|
|
2
|
+
import { GUIDE_MESSAGES } from '../domain/index.ts'
|
|
3
|
+
export default function CueNote({
|
|
4
|
+
title,
|
|
5
|
+
text,
|
|
6
|
+
state,
|
|
7
|
+
}: {
|
|
8
|
+
title?: string
|
|
9
|
+
text?: string
|
|
10
|
+
state?: 'shown' | 'hidden'
|
|
11
|
+
}) {
|
|
12
|
+
const t = useMessages(GUIDE_MESSAGES)
|
|
13
|
+
return state && (title || text) ? (
|
|
14
|
+
<div
|
|
15
|
+
className="pm-cue-note"
|
|
16
|
+
data-state={state}
|
|
17
|
+
style={state === 'shown' ? undefined : { visibility: 'hidden' }}
|
|
18
|
+
aria-hidden={state === 'shown' ? undefined : true}
|
|
19
|
+
>
|
|
20
|
+
<p>
|
|
21
|
+
{title && <strong>{title}</strong>}
|
|
22
|
+
{title && text ? t.separator : ''}
|
|
23
|
+
{text}
|
|
24
|
+
</p>
|
|
25
|
+
</div>
|
|
26
|
+
) : null
|
|
27
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
.pm-cursor {
|
|
2
|
+
position: absolute;
|
|
3
|
+
left: 0;
|
|
4
|
+
top: 0;
|
|
5
|
+
width: 0;
|
|
6
|
+
height: 0;
|
|
7
|
+
z-index: 20;
|
|
8
|
+
pointer-events: none;
|
|
9
|
+
opacity: 0;
|
|
10
|
+
transition: opacity var(--pm-duration);
|
|
11
|
+
}
|
|
12
|
+
.pm-cursor:not([data-waypoint='-1']) {
|
|
13
|
+
opacity: 1;
|
|
14
|
+
}
|
|
15
|
+
.pm-cursor__arrow {
|
|
16
|
+
position: absolute;
|
|
17
|
+
left: calc(-1 * var(--pm-size-stroke));
|
|
18
|
+
top: calc(-1 * var(--pm-size-stroke));
|
|
19
|
+
/* The page's base styles cap an svg at the width of its box, which is 0 here. */
|
|
20
|
+
max-width: none;
|
|
21
|
+
width: var(--pm-size-pointer-arrow);
|
|
22
|
+
height: var(--pm-size-pointer-ring);
|
|
23
|
+
fill: var(--pm-color-pointer-fill);
|
|
24
|
+
stroke: var(--pm-color-pointer-stroke);
|
|
25
|
+
stroke-width: 1.5;
|
|
26
|
+
stroke-linejoin: round;
|
|
27
|
+
filter: drop-shadow(var(--pm-shadow-pointer));
|
|
28
|
+
}
|
|
29
|
+
.pm-cursor__ring {
|
|
30
|
+
position: absolute;
|
|
31
|
+
left: calc(-0.5 * var(--pm-size-pointer-ring));
|
|
32
|
+
top: calc(-0.5 * var(--pm-size-pointer-ring));
|
|
33
|
+
width: var(--pm-size-pointer-ring);
|
|
34
|
+
height: var(--pm-size-pointer-ring);
|
|
35
|
+
border-radius: 50%;
|
|
36
|
+
border: var(--pm-size-rule) solid var(--pm-color-accent);
|
|
37
|
+
opacity: 0;
|
|
38
|
+
}
|
|
39
|
+
.pm-cursor[data-action='hover'] .pm-cursor__ring {
|
|
40
|
+
opacity: 0.35;
|
|
41
|
+
}
|
|
42
|
+
.pm-cursor[data-action='click'] .pm-cursor__ring {
|
|
43
|
+
opacity: calc(0.7 * var(--pm-cursor-press, 1));
|
|
44
|
+
transform: scale(calc(2.2 - 1.2 * var(--pm-cursor-press, 1)));
|
|
45
|
+
}
|
|
46
|
+
.pm-cursor[data-motion='reduced'] {
|
|
47
|
+
transition: none;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
[data-pm-cursor='hover'] {
|
|
52
|
+
outline: var(--pm-size-stroke) dashed var(--pm-color-accent);
|
|
53
|
+
outline-offset: var(--pm-size-rule);
|
|
54
|
+
}
|
|
55
|
+
[data-pm-cursor='click'] {
|
|
56
|
+
outline: var(--pm-size-stroke) solid var(--pm-color-accent);
|
|
57
|
+
outline-offset: var(--pm-size-rule);
|
|
58
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useStep,
|
|
3
|
+
usePrefersReducedMotion,
|
|
4
|
+
useStepScope,
|
|
5
|
+
useStepTime,
|
|
6
|
+
} from '@lincyaw/host'
|
|
7
|
+
import { useLayoutEffect, useRef, useState } from 'react'
|
|
8
|
+
import type { CSSProperties } from 'react'
|
|
9
|
+
import type { PlannedWaypoint } from '../domain/index.ts'
|
|
10
|
+
import { centerIn } from './geometry.ts'
|
|
11
|
+
import type { Point } from './geometry.ts'
|
|
12
|
+
import { resolveTarget, useTargetMark } from './targets.ts'
|
|
13
|
+
import './CursorPath.css'
|
|
14
|
+
export default function CursorPath({ plan }: { plan: PlannedWaypoint[] }) {
|
|
15
|
+
const scope = useStepScope(),
|
|
16
|
+
hostStep = useStep(),
|
|
17
|
+
stepTime = useStepTime({ until: 850 }),
|
|
18
|
+
reduced = usePrefersReducedMotion()
|
|
19
|
+
const index =
|
|
20
|
+
plan.filter((point) => scope.localStepAt(hostStep) >= point.at).length - 1,
|
|
21
|
+
point = plan[index]
|
|
22
|
+
const time = point && scope.localStepAt(hostStep) === point.at ? stepTime : Infinity
|
|
23
|
+
const cursor = useRef<HTMLDivElement>(null)
|
|
24
|
+
const [points, setPoints] = useState<{ from?: Point; to?: Point }>({})
|
|
25
|
+
useLayoutEffect(() => {
|
|
26
|
+
const measure = () => {
|
|
27
|
+
const el = cursor.current,
|
|
28
|
+
frame = el?.offsetParent
|
|
29
|
+
if (!(frame instanceof HTMLElement)) return
|
|
30
|
+
const at = (i: number) => {
|
|
31
|
+
const target = resolveTarget(el ?? undefined, plan[i]?.to)
|
|
32
|
+
return target ? centerIn(target, frame) : undefined
|
|
33
|
+
}
|
|
34
|
+
const to = at(Math.max(index, 0))
|
|
35
|
+
setPoints({ to, from: index > 0 ? (at(index - 1) ?? to) : to })
|
|
36
|
+
}
|
|
37
|
+
const observer = new ResizeObserver(measure)
|
|
38
|
+
if (cursor.current?.parentElement)
|
|
39
|
+
observer.observe(cursor.current.parentElement)
|
|
40
|
+
for (const waypoint of plan) {
|
|
41
|
+
const target = resolveTarget(cursor.current ?? undefined, waypoint.to)
|
|
42
|
+
if (target) observer.observe(target)
|
|
43
|
+
}
|
|
44
|
+
measure()
|
|
45
|
+
return () => observer.disconnect()
|
|
46
|
+
}, [index, plan])
|
|
47
|
+
useTargetMark(
|
|
48
|
+
cursor,
|
|
49
|
+
point?.to ?? '',
|
|
50
|
+
'data-pm-cursor',
|
|
51
|
+
point?.action === 'move' ? undefined : point?.action,
|
|
52
|
+
)
|
|
53
|
+
const k = 1 - (1 - Math.min(1, time / 500)) ** 3
|
|
54
|
+
const position =
|
|
55
|
+
points.from && points.to
|
|
56
|
+
? {
|
|
57
|
+
x: points.from.x + (points.to.x - points.from.x) * k,
|
|
58
|
+
y: points.from.y + (points.to.y - points.from.y) * k,
|
|
59
|
+
}
|
|
60
|
+
: points.to
|
|
61
|
+
const press = Math.min(1, Math.max(0, (time - 450) / 400))
|
|
62
|
+
return (
|
|
63
|
+
<div
|
|
64
|
+
ref={cursor}
|
|
65
|
+
className="pm-cursor"
|
|
66
|
+
aria-hidden="true"
|
|
67
|
+
data-waypoint={index}
|
|
68
|
+
data-action={point?.action ?? 'none'}
|
|
69
|
+
data-motion={reduced ? 'reduced' : 'full'}
|
|
70
|
+
style={
|
|
71
|
+
{
|
|
72
|
+
...(position
|
|
73
|
+
? { transform: `translate(${position.x}px, ${position.y}px)` }
|
|
74
|
+
: {}),
|
|
75
|
+
'--pm-cursor-press': press,
|
|
76
|
+
} as CSSProperties
|
|
77
|
+
}
|
|
78
|
+
>
|
|
79
|
+
<span className="pm-cursor__ring" />
|
|
80
|
+
<svg className="pm-cursor__arrow" viewBox="-2 -2 20 26">
|
|
81
|
+
<path d="M0 0V18L5 13.5L8.5 21L11.5 19.5L8 12H14.5Z" />
|
|
82
|
+
</svg>
|
|
83
|
+
</div>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { usePrefersReducedMotion, useStep, useStepScope, useStepState, useViewRole } from '@lincyaw/host'
|
|
2
|
+
import { useLayoutEffect, useRef } from 'react'
|
|
3
|
+
import { cueRange, cueSteps } from '../domain/index.ts'
|
|
4
|
+
import { useIsOnStage } from './stage.ts'
|
|
5
|
+
import { resolveTarget, useTargetMark } from './targets.ts'
|
|
6
|
+
export interface CueProps { target: string, at?: number | string, until?: number | string, title?: string, text?: string }
|
|
7
|
+
export interface CueRequest { element: HTMLElement, title?: string, text?: string, animate: boolean }
|
|
8
|
+
export type ShowCue = (owner: object, request: CueRequest | undefined) => void
|
|
9
|
+
export function sameCue(a: CueRequest | undefined, b: CueRequest | undefined): boolean { return !!a && !!b && a.element === b.element && a.title === b.title && a.text === b.text && a.animate === b.animate }
|
|
10
|
+
export type CueLook = 'stage' | 'inline'
|
|
11
|
+
export function useCue(props: CueProps, attribute: string, show: ShowCue) {
|
|
12
|
+
const range = cueRange(props.at, props.until), state = useStepState(range), scope = useStepScope(), onStage = useIsOnStage(), reduced = usePrefersReducedMotion(), role = useViewRole(), step = useStep()
|
|
13
|
+
const anchor = useRef<HTMLSpanElement>(null), owner = useRef({})
|
|
14
|
+
const look: CueLook | undefined = state.active ? onStage ? 'stage' : 'inline' : undefined
|
|
15
|
+
const note = role !== 'reader' ? undefined : state.active || step === Infinity ? 'shown' as const : 'hidden' as const
|
|
16
|
+
useTargetMark(anchor, props.target, attribute, look)
|
|
17
|
+
useLayoutEffect(() => {
|
|
18
|
+
const element = look === 'stage' ? resolveTarget(anchor.current ?? undefined, props.target) : undefined
|
|
19
|
+
const request = element ? { element, title: props.title, text: props.text, animate: !reduced } : undefined
|
|
20
|
+
show(owner.current, request)
|
|
21
|
+
let frame = 0, still = 0, box = ''
|
|
22
|
+
const track = () => { frame = 0; if (!request) return; const rect = request.element.getBoundingClientRect(); const next = `${rect.x} ${rect.y} ${rect.width} ${rect.height}`; if (next !== box) { show(owner.current, request); box = next; still = 0 } else still++; if (still < 30) frame = requestAnimationFrame(track) }
|
|
23
|
+
const wake = () => { still = 0; if (request && !frame) frame = requestAnimationFrame(track) }
|
|
24
|
+
wake(); window.addEventListener('resize', wake); document.addEventListener('transitionrun', wake, true); document.addEventListener('transitionend', wake, true)
|
|
25
|
+
return () => { cancelAnimationFrame(frame); window.removeEventListener('resize', wake); document.removeEventListener('transitionrun', wake, true); document.removeEventListener('transitionend', wake, true); show(owner.current, undefined) }
|
|
26
|
+
}, [look, reduced, props.target, props.title, props.text, show])
|
|
27
|
+
return { anchor, look, note, steps: cueSteps(range).map(scope.step) }
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '*.css'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number
|
|
3
|
+
y: number
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function centerIn(target: Element, frame: HTMLElement): Point | undefined {
|
|
7
|
+
const box = frame.getBoundingClientRect()
|
|
8
|
+
if (!frame.offsetWidth || !box.width) return undefined
|
|
9
|
+
const scale = box.width / frame.offsetWidth
|
|
10
|
+
const rect = target.getBoundingClientRect()
|
|
11
|
+
return {
|
|
12
|
+
x: (rect.left + rect.width / 2 - box.left) / scale - frame.clientLeft + frame.scrollLeft,
|
|
13
|
+
y: (rect.top + rect.height / 2 - box.top) / scale - frame.clientTop + frame.scrollTop,
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { hints } from 'driver.js/hints'
|
|
2
|
+
import type { DriverHint, Hints } from 'driver.js/hints'
|
|
3
|
+
import 'driver.js/dist/hints.css'
|
|
4
|
+
import './hints.css'
|
|
5
|
+
import { sameCue } from './cue.ts'
|
|
6
|
+
import type { CueRequest } from './cue.ts'
|
|
7
|
+
|
|
8
|
+
const requests = new Map<object, CueRequest>()
|
|
9
|
+
let beacons: Hints | undefined
|
|
10
|
+
|
|
11
|
+
function beacon({ element, title, text, animate }: CueRequest): DriverHint {
|
|
12
|
+
return {
|
|
13
|
+
element,
|
|
14
|
+
beacon: { animate, className: 'pm-hint-beacon' },
|
|
15
|
+
popover: title || text ? { title, description: text, showButton: false } : undefined,
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function release(event: FocusEvent) {
|
|
20
|
+
const target = event.target
|
|
21
|
+
if (target instanceof HTMLElement && target.closest('.pm-hint-beacon, .pm-hint-popover')) target.blur()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function create(): Hints {
|
|
25
|
+
document.addEventListener('focusin', release)
|
|
26
|
+
return hints({ popoverClass: 'pm-hint-popover' })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function requestHint(owner: object, request: CueRequest | undefined): void {
|
|
30
|
+
if (sameCue(requests.get(owner), request)) {
|
|
31
|
+
beacons?.refresh()
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
if (request) requests.set(owner, request)
|
|
35
|
+
else requests.delete(owner)
|
|
36
|
+
beacons ??= create()
|
|
37
|
+
beacons.hide()
|
|
38
|
+
beacons.setHints([...requests.values()].map(beacon))
|
|
39
|
+
if (requests.size) beacons.show()
|
|
40
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { useCue } from './cue.ts'
|
|
2
|
+
export type { CueLook, CueProps, CueRequest, ShowCue } from './cue.ts'
|
|
3
|
+
export { centerIn } from './geometry.ts'
|
|
4
|
+
export type { Point } from './geometry.ts'
|
|
5
|
+
export { requestHint } from './hints.ts'
|
|
6
|
+
export { requestSpotlight } from './spotlight.ts'
|
|
7
|
+
export { STAGE_PROBE, useIsOnStage } from './stage.ts'
|
|
8
|
+
export type { StageProbe } from './stage.ts'
|
|
9
|
+
export { resolveTarget, useTargetMark } from './targets.ts'
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { blockElement, codeLinesBox } from '@lincyaw/host'
|
|
2
|
+
import type { Navigation } from '@lincyaw/host'
|
|
3
|
+
import type { CueRequest, ShowCue } from './cue.ts'
|
|
4
|
+
import { requestHint } from './hints.ts'
|
|
5
|
+
import { requestSpotlight } from './spotlight.ts'
|
|
6
|
+
|
|
7
|
+
export interface OperationCue {
|
|
8
|
+
block?: string
|
|
9
|
+
selector?: string
|
|
10
|
+
lines?: string
|
|
11
|
+
span?: number
|
|
12
|
+
title?: string
|
|
13
|
+
text?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const REFRESH_MS = 250
|
|
17
|
+
|
|
18
|
+
function firstVisible(elements: Iterable<HTMLElement>): HTMLElement | undefined {
|
|
19
|
+
for (const el of elements) {
|
|
20
|
+
if (!el.closest('[inert], .pm-hidden') && el.getClientRects().length > 0) return el
|
|
21
|
+
}
|
|
22
|
+
return undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sameTarget(a: OperationCue, b: OperationCue): boolean {
|
|
26
|
+
return a.block === b.block && a.selector === b.selector && a.lines === b.lines
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function toOperationCue(data: Record<string, unknown>): OperationCue | undefined {
|
|
30
|
+
const text = (value: unknown) => (typeof value === 'string' && value ? value : undefined)
|
|
31
|
+
const cue: OperationCue = {
|
|
32
|
+
block: text(data.block),
|
|
33
|
+
selector: text(data.selector),
|
|
34
|
+
lines: typeof data.lines === 'number' ? String(data.lines) : text(data.lines),
|
|
35
|
+
span: typeof data.span === 'number' && data.span > 0 ? data.span : undefined,
|
|
36
|
+
title: text(data.title),
|
|
37
|
+
text: text(data.text),
|
|
38
|
+
}
|
|
39
|
+
return cue.block || cue.selector ? cue : undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class OperationCues {
|
|
43
|
+
private current?: { cue: OperationCue, slide: number, step: number }
|
|
44
|
+
private readonly owner = {}
|
|
45
|
+
private box?: HTMLElement
|
|
46
|
+
private timer?: ReturnType<typeof setInterval>
|
|
47
|
+
private navigation?: Navigation
|
|
48
|
+
private unwatch?: () => void
|
|
49
|
+
private readonly show: ShowCue
|
|
50
|
+
|
|
51
|
+
constructor(show: ShowCue) {
|
|
52
|
+
this.show = show
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
bind(navigation: Navigation | undefined): void {
|
|
56
|
+
this.unwatch?.()
|
|
57
|
+
this.navigation = navigation
|
|
58
|
+
this.unwatch = navigation && navigation.position.subscribe(() => {
|
|
59
|
+
const { slide, step } = navigation.position.getSnapshot()
|
|
60
|
+
const now = this.current
|
|
61
|
+
if (now && (slide !== now.slide || step < now.step || step > now.step + (now.cue.span ?? 0))) this.hide()
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
unbind(): void {
|
|
66
|
+
this.hide()
|
|
67
|
+
this.unwatch?.()
|
|
68
|
+
this.unwatch = undefined
|
|
69
|
+
this.navigation = undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get shown(): OperationCue | undefined {
|
|
73
|
+
return this.current?.cue
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
apply(data: Record<string, unknown>, animate = true): void {
|
|
77
|
+
const cue = toOperationCue(data)
|
|
78
|
+
if (!cue) return this.hide()
|
|
79
|
+
const position = this.navigation?.position.getSnapshot() ?? { slide: 0, step: 0 }
|
|
80
|
+
this.current = { cue, slide: position.slide, step: position.step }
|
|
81
|
+
this.draw(animate)
|
|
82
|
+
clearInterval(this.timer)
|
|
83
|
+
this.timer = setInterval(() => this.draw(animate), REFRESH_MS)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
isShowing(data: Record<string, unknown>): boolean {
|
|
87
|
+
const cue = toOperationCue(data)
|
|
88
|
+
return Boolean(cue && this.current && sameTarget(cue, this.current.cue))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
hide(): void {
|
|
92
|
+
clearInterval(this.timer)
|
|
93
|
+
this.timer = undefined
|
|
94
|
+
this.current = undefined
|
|
95
|
+
this.box?.remove()
|
|
96
|
+
this.box = undefined
|
|
97
|
+
this.show(this.owner, undefined)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private draw(animate: boolean): void {
|
|
101
|
+
const cue = this.current?.cue
|
|
102
|
+
if (!cue) return
|
|
103
|
+
let element: HTMLElement | undefined
|
|
104
|
+
try {
|
|
105
|
+
element = cue.block ? blockElement(cue.block) : firstVisible(document.querySelectorAll<HTMLElement>(cue.selector!))
|
|
106
|
+
} catch {
|
|
107
|
+
element = undefined
|
|
108
|
+
}
|
|
109
|
+
const lines = element && cue.lines ? codeLinesBox(element, cue.lines) : undefined
|
|
110
|
+
if (lines) {
|
|
111
|
+
this.box ??= Object.assign(document.createElement('div'), { className: 'pm-cue-lines' })
|
|
112
|
+
Object.assign(this.box.style, { position: 'fixed', pointerEvents: 'none', left: `${lines.left}px`, top: `${lines.top}px`, width: `${lines.width}px`, height: `${lines.height}px` })
|
|
113
|
+
if (!this.box.isConnected) document.body.append(this.box)
|
|
114
|
+
element = this.box
|
|
115
|
+
}
|
|
116
|
+
const request: CueRequest | undefined = element && { element, title: cue.title, text: cue.text, animate }
|
|
117
|
+
this.show(this.owner, request)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const spotlightCues = new OperationCues(requestSpotlight)
|
|
122
|
+
export const hintCues = new OperationCues(requestHint)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NAVIGATION, OPERATIONS } from '@lincyaw/host'
|
|
2
|
+
import type { PluginDefinition, Unsubscribe } from '@lincyaw/kernel'
|
|
3
|
+
import { version } from '../../package.json'
|
|
4
|
+
import { GUIDE_ID } from '../domain/index.ts'
|
|
5
|
+
import { hintCues, spotlightCues } from './operation-cues.ts'
|
|
6
|
+
|
|
7
|
+
let unregister: Unsubscribe[] = []
|
|
8
|
+
|
|
9
|
+
const plugin: PluginDefinition = {
|
|
10
|
+
// dispose releases everything setup made; a paused preview starts the same definition again.
|
|
11
|
+
restartable: true,
|
|
12
|
+
manifest: {
|
|
13
|
+
id: GUIDE_ID,
|
|
14
|
+
name: { 'zh-CN': '导览提示', en: 'Guidance Cues' },
|
|
15
|
+
version,
|
|
16
|
+
kernel: '^1.0.0',
|
|
17
|
+
requires: [{ token: OPERATIONS, optional: true }, { token: NAVIGATION, optional: true }],
|
|
18
|
+
},
|
|
19
|
+
setup(ctx) {
|
|
20
|
+
const operations = ctx.tryGet(OPERATIONS)
|
|
21
|
+
const navigation = ctx.tryGet(NAVIGATION)
|
|
22
|
+
unregister = [spotlightCues, hintCues].map((cues, i) => {
|
|
23
|
+
cues.bind(navigation)
|
|
24
|
+
const off = operations?.onReplay(i === 0 ? 'spot' : 'hint', {
|
|
25
|
+
apply: (data, seeking) => cues.apply(data, !seeking),
|
|
26
|
+
clear: () => cues.hide(),
|
|
27
|
+
})
|
|
28
|
+
return () => {
|
|
29
|
+
off?.()
|
|
30
|
+
cues.unbind()
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
},
|
|
34
|
+
dispose() {
|
|
35
|
+
unregister.forEach(off => off())
|
|
36
|
+
unregister = []
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default plugin
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
.driver-overlay {
|
|
2
|
+
z-index: var(--pm-cue-z, 1) !important;
|
|
3
|
+
}
|
|
4
|
+
.driver-overlay,
|
|
5
|
+
.driver-overlay * {
|
|
6
|
+
pointer-events: none !important;
|
|
7
|
+
}
|
|
8
|
+
body .driver-popover.pm-spotlight-popover {
|
|
9
|
+
z-index: calc(var(--pm-cue-z, 1) + 1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
body .driver-popover:is(.pm-spotlight-popover, .pm-hint-popover) {
|
|
13
|
+
border: var(--pm-size-hairline) solid var(--pm-color-line);
|
|
14
|
+
border-radius: var(--pm-radius-card);
|
|
15
|
+
background: var(--pm-color-surface);
|
|
16
|
+
color: var(--pm-color-ink);
|
|
17
|
+
box-shadow: var(--pm-shadow-float);
|
|
18
|
+
font-family: var(--pm-font-sans);
|
|
19
|
+
}
|
|
20
|
+
body .driver-popover:is(.pm-spotlight-popover, .pm-hint-popover) .driver-popover-title {
|
|
21
|
+
font-family: var(--pm-font-serif);
|
|
22
|
+
font-weight: 600;
|
|
23
|
+
}
|
|
24
|
+
body .driver-popover:is(.pm-spotlight-popover, .pm-hint-popover) .driver-popover-description {
|
|
25
|
+
color: var(--pm-color-ink);
|
|
26
|
+
}
|
|
27
|
+
body .driver-popover:is(.pm-spotlight-popover, .pm-hint-popover) .driver-popover-arrow {
|
|
28
|
+
border-color: var(--pm-color-surface);
|
|
29
|
+
}
|
|
30
|
+
body .driver-popover:is(.pm-spotlight-popover, .pm-hint-popover) .driver-popover-close-btn {
|
|
31
|
+
color: var(--pm-color-muted);
|
|
32
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { driver } from 'driver.js'
|
|
2
|
+
import type { Config, Driver } from 'driver.js'
|
|
3
|
+
import 'driver.js/dist/driver.css'
|
|
4
|
+
import './spotlight.css'
|
|
5
|
+
import { sameCue } from './cue.ts'
|
|
6
|
+
import type { CueRequest } from './cue.ts'
|
|
7
|
+
|
|
8
|
+
const requests = new Map<object, CueRequest>()
|
|
9
|
+
let current: { driver: Driver; animate: boolean } | undefined
|
|
10
|
+
let lit: { element: HTMLElement; tabindex: boolean } | undefined
|
|
11
|
+
|
|
12
|
+
function config(animate: boolean): Config {
|
|
13
|
+
return {
|
|
14
|
+
animate,
|
|
15
|
+
allowClose: false,
|
|
16
|
+
allowKeyboardControl: false,
|
|
17
|
+
overlayClickBehavior: 'none',
|
|
18
|
+
showButtons: [],
|
|
19
|
+
smoothScroll: false,
|
|
20
|
+
stagePadding: 8,
|
|
21
|
+
stageRadius: 8,
|
|
22
|
+
popoverClass: 'pm-spotlight-popover',
|
|
23
|
+
onHighlightStarted: () => document.body.classList.remove('driver-active'),
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const FOCUSABLE = 'a[href], button, input, select, textarea, [tabindex], [contenteditable]'
|
|
28
|
+
|
|
29
|
+
function light(element: HTMLElement | undefined): void {
|
|
30
|
+
if (lit?.element === element) return
|
|
31
|
+
if (lit?.tabindex) lit.element.removeAttribute('tabindex')
|
|
32
|
+
lit = element && { element, tabindex: !element.matches(FOCUSABLE) }
|
|
33
|
+
if (lit?.tabindex) element!.setAttribute('tabindex', '-1')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function focusLit(event: KeyboardEvent): void {
|
|
37
|
+
if (event.key !== 'Tab' || event.altKey || event.ctrlKey || event.metaKey || !lit || lit.element.contains(document.activeElement)) return
|
|
38
|
+
event.preventDefault()
|
|
39
|
+
event.stopImmediatePropagation()
|
|
40
|
+
lit.element.focus({ preventScroll: true })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function requestSpotlight(owner: object, request: CueRequest | undefined): void {
|
|
44
|
+
if (sameCue(requests.get(owner), request)) {
|
|
45
|
+
current?.driver.refresh()
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
requests.delete(owner)
|
|
49
|
+
if (request) requests.set(owner, request)
|
|
50
|
+
const shown = [...requests.values()].at(-1)
|
|
51
|
+
light(shown?.element)
|
|
52
|
+
if (current && current.animate !== shown?.animate) {
|
|
53
|
+
current.driver.destroy()
|
|
54
|
+
current = undefined
|
|
55
|
+
window.removeEventListener('keydown', focusLit, true)
|
|
56
|
+
}
|
|
57
|
+
if (!shown) return
|
|
58
|
+
if (!current) {
|
|
59
|
+
current = { driver: driver(config(shown.animate)), animate: shown.animate }
|
|
60
|
+
window.addEventListener('keydown', focusLit, true)
|
|
61
|
+
}
|
|
62
|
+
const { element, title, text } = shown
|
|
63
|
+
current.driver.highlight({ element, popover: title || text ? { title, description: text } : undefined })
|
|
64
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { useViewRole } from '@lincyaw/host'
|
|
2
|
+
import { createContext, useContext } from 'react'
|
|
3
|
+
export type StageProbe = () => boolean
|
|
4
|
+
export const STAGE_PROBE = createContext<StageProbe | undefined>(undefined)
|
|
5
|
+
export function useIsOnStage(): boolean { const probe = useContext(STAGE_PROBE); const role = useViewRole(); return probe ? probe() : role === 'audience' }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useLayoutEffect } from 'react'
|
|
2
|
+
import type { RefObject } from 'react'
|
|
3
|
+
|
|
4
|
+
export function resolveTarget(from: Element | undefined, selector: string | undefined): HTMLElement | undefined {
|
|
5
|
+
if (!from || !selector) return undefined
|
|
6
|
+
try {
|
|
7
|
+
for (let scope = from.parentElement; scope; scope = scope.parentElement) {
|
|
8
|
+
const found = scope.querySelector<HTMLElement>(selector)
|
|
9
|
+
if (found) return found
|
|
10
|
+
}
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.warn(`[presmium] invalid cue target "${selector}"`, error)
|
|
13
|
+
}
|
|
14
|
+
return undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useTargetMark(anchor: RefObject<Element | null | undefined>, selector: string | (() => string | undefined), attribute: string, value: string | undefined | (() => string | undefined)): void {
|
|
18
|
+
const targetSelector = typeof selector === 'function' ? selector() : selector
|
|
19
|
+
const current = typeof value === 'function' ? value() : value
|
|
20
|
+
useLayoutEffect(() => {
|
|
21
|
+
const marked = current === undefined ? undefined : resolveTarget(anchor.current ?? undefined, targetSelector)
|
|
22
|
+
if (marked && current !== undefined) marked.setAttribute(attribute, current)
|
|
23
|
+
return () => marked?.removeAttribute(attribute)
|
|
24
|
+
}, [anchor, targetSelector, attribute, current])
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { blockAt, OPERATIONS, presmiumService } from '@lincyaw/host'
|
|
2
|
+
import type { ViewerPluginContext } from '@lincyaw/viewer'
|
|
3
|
+
import type { OperationCues } from './operation-cues.ts'
|
|
4
|
+
import { hintCues, spotlightCues } from './operation-cues.ts'
|
|
5
|
+
|
|
6
|
+
let pointer: { x: number, y: number } | undefined
|
|
7
|
+
|
|
8
|
+
function toggle(type: 'spot' | 'hint', cues: OperationCues): boolean {
|
|
9
|
+
const block = pointer && blockAt(pointer.x, pointer.y)?.key
|
|
10
|
+
if (!block) return false
|
|
11
|
+
const data = cues.isShowing({ block }) ? {} : { block }
|
|
12
|
+
cues.apply(data)
|
|
13
|
+
presmiumService(OPERATIONS)?.report({ type, data })
|
|
14
|
+
return true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function guideViewerPlugin({ registerAction }: ViewerPluginContext): void {
|
|
18
|
+
window.addEventListener('pointermove', (event) => {
|
|
19
|
+
pointer = { x: event.clientX, y: event.clientY }
|
|
20
|
+
}, { passive: true })
|
|
21
|
+
registerAction({ id: 'presmium.guide.spot', keys: ['s'], run: () => toggle('spot', spotlightCues) })
|
|
22
|
+
registerAction({ id: 'presmium.guide.hint', keys: ['h'], run: () => toggle('hint', hintCues) })
|
|
23
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { StepRange } from '@lincyaw/kernel/contracts'
|
|
2
|
+
|
|
3
|
+
export const GUIDE_ID = 'presmium.guide'
|
|
4
|
+
|
|
5
|
+
export type CueTarget = string
|
|
6
|
+
|
|
7
|
+
export type CursorAction = 'move' | 'hover' | 'click'
|
|
8
|
+
|
|
9
|
+
export interface Waypoint {
|
|
10
|
+
to: CueTarget
|
|
11
|
+
action?: CursorAction
|
|
12
|
+
at?: number | string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface PlannedWaypoint {
|
|
16
|
+
to: CueTarget
|
|
17
|
+
action: CursorAction
|
|
18
|
+
at: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ACTIONS: readonly string[] = ['move', 'hover', 'click']
|
|
22
|
+
|
|
23
|
+
function step(value: unknown): number | undefined {
|
|
24
|
+
const n = typeof value === 'string' && value.trim() !== '' ? Number(value) : value
|
|
25
|
+
return typeof n === 'number' && Number.isInteger(n) && n >= 0 ? n : undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function planCursorPath(path: readonly Waypoint[]): PlannedWaypoint[] {
|
|
29
|
+
const planned: PlannedWaypoint[] = []
|
|
30
|
+
for (const point of path) {
|
|
31
|
+
const previous = planned.at(-1)?.at ?? -1
|
|
32
|
+
const at = Math.max(step(point.at) ?? previous + 1, previous, 0)
|
|
33
|
+
const action = point.action && ACTIONS.includes(point.action) ? point.action : 'move'
|
|
34
|
+
planned.push({ to: point.to, action, at })
|
|
35
|
+
}
|
|
36
|
+
return planned
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function parseCursorPath(body: string): Waypoint[] {
|
|
40
|
+
return body.split('\n').map(line => line.trim()).filter(Boolean).map((line) => {
|
|
41
|
+
const [first = '', ...rest] = line.split(/\s+/)
|
|
42
|
+
return rest.length > 0 && ACTIONS.includes(first) ? { to: rest.join(' '), action: first as CursorAction } : { to: line }
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function cueRange(at?: number | string, until?: number | string): StepRange {
|
|
47
|
+
const from = step(at) ?? 0
|
|
48
|
+
const to = step(until)
|
|
49
|
+
return to !== undefined && to > from ? { from, to } : { from }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function cueSteps(range: StepRange): number[] {
|
|
53
|
+
return range.from > 0 ? [range.from] : []
|
|
54
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { cueRange, cueSteps, GUIDE_ID, parseCursorPath, planCursorPath } from './cues.ts'
|
|
2
|
+
export type { CueTarget, CursorAction, PlannedWaypoint, Waypoint } from './cues.ts'
|
|
3
|
+
export { GUIDE_MESSAGES } from './messages.ts'
|
|
4
|
+
export type { GuideMessages } from './messages.ts'
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { deckLocale } from '@lincyaw/kernel'
|
|
3
|
+
import type { ComponentNode, DirectiveSpec, MdastNode, PresmiumNodePlugin } from '@lincyaw/kernel'
|
|
4
|
+
import { GUIDE_ID, GUIDE_MESSAGES, parseCursorPath, planCursorPath } from '../domain/index.ts'
|
|
5
|
+
|
|
6
|
+
const { version } = createRequire(import.meta.url)('../../package.json') as { version: string }
|
|
7
|
+
|
|
8
|
+
function component(name: string, props: Record<string, unknown>, fallback: MdastNode): ComponentNode {
|
|
9
|
+
const defined = Object.fromEntries(Object.entries(props).filter(([, value]) => value !== undefined))
|
|
10
|
+
return { type: 'component', plugin: GUIDE_ID, name, props: defined, fallback }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function popover(lang: string | undefined, title?: string, text?: string): MdastNode {
|
|
14
|
+
const children: MdastNode[] = []
|
|
15
|
+
if (title) children.push({ type: 'strong', children: [{ type: 'text', value: title }] })
|
|
16
|
+
if (text) children.push({ type: 'text', value: title ? `${GUIDE_MESSAGES[deckLocale(lang)].separator}${text}` : text })
|
|
17
|
+
return { type: 'root', children: children.length > 0 ? [{ type: 'paragraph', children }] : [] }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const cursor: DirectiveSpec = {
|
|
21
|
+
name: 'cursor',
|
|
22
|
+
body: true,
|
|
23
|
+
toIR(input, ctx) {
|
|
24
|
+
const path = planCursorPath(parseCursorPath(input.body))
|
|
25
|
+
if (path.length === 0) ctx.report({ checkId: GUIDE_ID, severity: 'error', message: '{cursor} needs at least one waypoint, e.g. `click #run`' })
|
|
26
|
+
return { node: { ...component('PmCursor', { path }, popover(ctx.lang)), layer: 'overlay' }, span: path.at(-1)?.at ?? 0 }
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function cue(name: string, tag: string): DirectiveSpec {
|
|
31
|
+
return {
|
|
32
|
+
name,
|
|
33
|
+
arg: true,
|
|
34
|
+
body: true,
|
|
35
|
+
options: { title: 'string', span: 'number' },
|
|
36
|
+
toIR(input, ctx) {
|
|
37
|
+
if (!input.arg) ctx.report({ checkId: GUIDE_ID, severity: 'error', message: `{${name}} needs the CSS selector of its target, e.g. {${name}} #run` })
|
|
38
|
+
const span = typeof input.options.span === 'number' && Number.isInteger(input.options.span) && input.options.span > 0 ? input.options.span : 0
|
|
39
|
+
const title = typeof input.options.title === 'string' && input.options.title ? input.options.title : undefined
|
|
40
|
+
const text = input.body.trim() || undefined
|
|
41
|
+
return { node: component(tag, { target: input.arg ?? '', until: span + 1, title, text }, popover(ctx.lang, title, text)), span }
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const plugin: PresmiumNodePlugin = {
|
|
47
|
+
id: GUIDE_ID,
|
|
48
|
+
version,
|
|
49
|
+
kernel: '^1.0.0',
|
|
50
|
+
directives: [cursor, cue('spotlight', 'PmSpotlight'), cue('hint', 'PmHint')],
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default plugin
|