@lincyaw/workbench 0.2.0-rc.121 → 0.2.0-rc.123
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/package.json +4 -4
- package/src/StepProductionEditor.tsx +13 -8
- package/src/components.tsx +24 -4
- package/src/context.tsx +30 -0
- package/src/document-editor/BlockNoteSurface.tsx +4 -1
- package/src/document-editor/DocumentBlockFallback.tsx +0 -1
- package/src/document-editor/DocumentEditor.tsx +7 -1
- package/src/document-editor/DocumentStepNarration.tsx +4 -2
- package/src/document-editor/authoring.tsx +15 -0
- package/src/document-editor/inline.ts +5 -0
- package/src/document-editor/insertion-place.ts +2 -2
- package/src/document-editor/messages.ts +2 -2
- package/src/document-editor/projection.ts +6 -4
- package/src/document-editor/references.tsx +352 -0
- package/src/document-editor/schema.tsx +39 -2
- package/src/document-editor/style.css +10 -0
- package/src/document-production.ts +8 -0
- package/src/document-session.ts +5 -3
- package/src/document-tree.ts +271 -0
- package/src/import-messages.ts +1 -1
- package/src/index.ts +6 -1
- package/src/knowledge-components.tsx +574 -0
- package/src/knowledge.css +354 -0
- package/src/messages.ts +12 -0
- package/src/panels/DocumentPeekPanel.tsx +190 -0
- package/src/panels/DocumentView.tsx +46 -23
- package/src/panels/DocumentVoiceField.tsx +3 -2
- package/src/panels/HomePanel.tsx +12 -3
- package/src/panels/MaterialsDialog.tsx +67 -0
- package/src/panels/ProductionCaptionSize.tsx +8 -3
- package/src/panels/ReferencesPanel.tsx +115 -0
- package/src/panels/SourceDocumentPanel.tsx +21 -3
- package/src/panels/WorkspaceHome.tsx +109 -0
- package/src/panels/WorkspaceSidebar.tsx +331 -0
- package/src/styles.css +20 -0
- package/src/tree-messages.ts +345 -0
- package/src/workbench-state.ts +21 -0
- package/src/workbench.ts +76 -3
- package/src/workspace-client.ts +35 -1
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/* The workspace sidebar, the right pane's references and outline, and the home page: layout only; the controls are
|
|
2
|
+
the shared ones (docs/design-system.md). */
|
|
3
|
+
|
|
4
|
+
.pmw-sidebar {
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: column;
|
|
7
|
+
gap: var(--pm-space-2);
|
|
8
|
+
box-sizing: border-box;
|
|
9
|
+
height: 100%;
|
|
10
|
+
min-height: 0;
|
|
11
|
+
padding: var(--pm-space-3) var(--pm-space-2);
|
|
12
|
+
background: var(--pm-color-chrome);
|
|
13
|
+
color: var(--pm-color-ink);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.pmw-sidebar__header {
|
|
17
|
+
display: flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
gap: var(--pm-space-2);
|
|
20
|
+
padding: 0 var(--pm-space-1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.pmw-sidebar__mark {
|
|
24
|
+
display: inline-flex;
|
|
25
|
+
flex: none;
|
|
26
|
+
align-items: center;
|
|
27
|
+
justify-content: center;
|
|
28
|
+
width: var(--pm-size-avatar);
|
|
29
|
+
height: var(--pm-size-avatar);
|
|
30
|
+
border-radius: var(--pm-radius-small);
|
|
31
|
+
background: var(--pm-color-accent);
|
|
32
|
+
color: var(--pm-color-on-accent);
|
|
33
|
+
font-size: var(--pm-text-sm);
|
|
34
|
+
font-weight: var(--pm-weight-semibold);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.pmw-sidebar__workspace {
|
|
38
|
+
display: grid;
|
|
39
|
+
flex: 1;
|
|
40
|
+
min-width: 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.pmw-sidebar__name {
|
|
44
|
+
overflow: hidden;
|
|
45
|
+
font-size: var(--pm-text-sm);
|
|
46
|
+
font-weight: var(--pm-weight-semibold);
|
|
47
|
+
text-overflow: ellipsis;
|
|
48
|
+
white-space: nowrap;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.pmw-sidebar__meta {
|
|
52
|
+
color: var(--pm-color-muted);
|
|
53
|
+
font-size: var(--pm-text-xs);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.pmw-sidebar__places,
|
|
57
|
+
.pmw-recent {
|
|
58
|
+
display: grid;
|
|
59
|
+
gap: var(--pm-space-px);
|
|
60
|
+
margin: 0;
|
|
61
|
+
padding: 0;
|
|
62
|
+
list-style: none;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.pmw-sidebar__section {
|
|
66
|
+
display: flex;
|
|
67
|
+
align-items: center;
|
|
68
|
+
justify-content: space-between;
|
|
69
|
+
margin-top: var(--pm-space-2);
|
|
70
|
+
padding: 0 var(--pm-space-1) 0 var(--pm-space-2);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.pmw-sidebar__heading {
|
|
74
|
+
margin: 0;
|
|
75
|
+
color: var(--pm-color-muted);
|
|
76
|
+
font-size: var(--pm-text-xs);
|
|
77
|
+
font-weight: var(--pm-weight-medium);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.pmw-sidebar__tree {
|
|
81
|
+
display: flex;
|
|
82
|
+
flex: 1;
|
|
83
|
+
flex-direction: column;
|
|
84
|
+
min-height: 0;
|
|
85
|
+
overflow: hidden;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.pmw-sidebar__empty {
|
|
89
|
+
padding: 0 var(--pm-space-2);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.pmw-sidebar__trash {
|
|
93
|
+
flex: none;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.pmw-recent {
|
|
97
|
+
min-width: var(--pm-popover-min-width);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.pmw-recent__item {
|
|
101
|
+
display: grid;
|
|
102
|
+
grid-template-columns: auto 1fr auto;
|
|
103
|
+
column-gap: var(--pm-space-2);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.pmw-recent__item > .pm-ui-icon {
|
|
107
|
+
grid-row: span 2;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.pmw-recent__title {
|
|
111
|
+
overflow: hidden;
|
|
112
|
+
text-overflow: ellipsis;
|
|
113
|
+
white-space: nowrap;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.pmw-recent__path {
|
|
117
|
+
grid-column: 2;
|
|
118
|
+
color: var(--pm-color-muted);
|
|
119
|
+
font-size: var(--pm-text-xs);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.pmw-recent__time {
|
|
123
|
+
grid-column: 3;
|
|
124
|
+
grid-row: 1;
|
|
125
|
+
color: var(--pm-color-muted);
|
|
126
|
+
font-size: var(--pm-text-xs);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.pmw-recent__empty {
|
|
130
|
+
margin: 0;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.pmw-trash {
|
|
134
|
+
display: grid;
|
|
135
|
+
gap: var(--pm-space-2);
|
|
136
|
+
margin: 0;
|
|
137
|
+
padding: 0;
|
|
138
|
+
list-style: none;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.pmw-trash__item {
|
|
142
|
+
display: flex;
|
|
143
|
+
flex-wrap: wrap;
|
|
144
|
+
align-items: center;
|
|
145
|
+
gap: var(--pm-space-2);
|
|
146
|
+
padding: var(--pm-space-2) 0;
|
|
147
|
+
border-bottom: var(--pm-size-hairline) solid var(--pm-color-line);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.pmw-trash__text {
|
|
151
|
+
display: grid;
|
|
152
|
+
flex: 1;
|
|
153
|
+
min-width: var(--pm-menu-min-width);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.pmw-trash__meta {
|
|
157
|
+
color: var(--pm-color-muted);
|
|
158
|
+
font-size: var(--pm-text-xs);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
.pmw-references {
|
|
162
|
+
display: grid;
|
|
163
|
+
grid-template-columns: minmax(0, 1fr);
|
|
164
|
+
align-content: start;
|
|
165
|
+
gap: var(--pm-space-4);
|
|
166
|
+
min-width: 0;
|
|
167
|
+
padding: var(--pm-space-3);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.pmw-references__group {
|
|
171
|
+
display: grid;
|
|
172
|
+
grid-template-columns: minmax(0, 1fr);
|
|
173
|
+
gap: var(--pm-space-2);
|
|
174
|
+
min-width: 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.pmw-references__heading {
|
|
178
|
+
margin: 0;
|
|
179
|
+
color: var(--pm-color-muted);
|
|
180
|
+
font-size: var(--pm-text-xs);
|
|
181
|
+
font-weight: var(--pm-weight-medium);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.pmw-references__list,
|
|
185
|
+
.pmw-references__links {
|
|
186
|
+
display: grid;
|
|
187
|
+
grid-template-columns: minmax(0, 1fr);
|
|
188
|
+
gap: var(--pm-space-2);
|
|
189
|
+
margin: 0;
|
|
190
|
+
padding: 0;
|
|
191
|
+
list-style: none;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.pmw-references__links {
|
|
195
|
+
gap: var(--pm-space-px);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.pmw-references__link:not([aria-disabled]) {
|
|
199
|
+
color: var(--pm-color-accent);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
.pmw-references__title {
|
|
203
|
+
overflow: hidden;
|
|
204
|
+
text-overflow: ellipsis;
|
|
205
|
+
white-space: nowrap;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.pmw-references-summary {
|
|
209
|
+
max-width: var(--pm-sheet-width);
|
|
210
|
+
margin: var(--pm-space-4) auto var(--pm-space-8);
|
|
211
|
+
border: var(--pm-size-hairline) solid var(--pm-color-line);
|
|
212
|
+
border-radius: var(--pm-radius-control);
|
|
213
|
+
background: var(--pm-color-surface);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.pmw-references-summary__row {
|
|
217
|
+
display: flex;
|
|
218
|
+
align-items: center;
|
|
219
|
+
justify-content: space-between;
|
|
220
|
+
gap: var(--pm-space-2);
|
|
221
|
+
padding: var(--pm-space-2) var(--pm-space-3);
|
|
222
|
+
color: var(--pm-color-muted);
|
|
223
|
+
font-size: var(--pm-text-sm);
|
|
224
|
+
cursor: pointer;
|
|
225
|
+
list-style: none;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.pmw-references-summary__row::-webkit-details-marker {
|
|
229
|
+
display: none;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.pmw-references-summary[open] .pmw-references-summary__row > .pm-ui-icon {
|
|
233
|
+
transform: rotate(90deg);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
.pmw-rail {
|
|
237
|
+
display: flex;
|
|
238
|
+
flex-direction: column;
|
|
239
|
+
height: 100%;
|
|
240
|
+
min-height: 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.pmw-rail__header {
|
|
244
|
+
display: flex;
|
|
245
|
+
flex: none;
|
|
246
|
+
align-items: center;
|
|
247
|
+
gap: var(--pm-space-1);
|
|
248
|
+
padding-inline-end: var(--pm-space-2);
|
|
249
|
+
border-bottom: var(--pm-size-hairline) solid var(--pm-color-line);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.pmw-rail__header > .pm-ui-tabs-root {
|
|
253
|
+
flex: 1;
|
|
254
|
+
min-width: 0;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.pmw-rail__body {
|
|
258
|
+
flex: 1;
|
|
259
|
+
min-width: 0;
|
|
260
|
+
min-height: 0;
|
|
261
|
+
overflow-x: hidden;
|
|
262
|
+
overflow-y: auto;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
.pmw-rail {
|
|
266
|
+
min-width: 0;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
.pmw-rail__actions {
|
|
270
|
+
display: flex;
|
|
271
|
+
justify-content: flex-end;
|
|
272
|
+
padding: var(--pm-space-2) var(--pm-space-2) 0;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* The page outline as a tab of the right pane fills it instead of standing at the page's start edge. */
|
|
276
|
+
.pmw-document .pmw-rail .pmw-rail__body > .pmw-navigation {
|
|
277
|
+
position: static;
|
|
278
|
+
flex: 1 1 auto;
|
|
279
|
+
width: auto;
|
|
280
|
+
height: 100%;
|
|
281
|
+
border-right: 0;
|
|
282
|
+
box-shadow: none;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
.pmw-workspace-home__meta {
|
|
286
|
+
margin-top: calc(var(--pm-space-3) * -1);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.pmw-workspace-home__start {
|
|
290
|
+
display: grid;
|
|
291
|
+
grid-template-columns: repeat(auto-fill, minmax(var(--pm-menu-min-width), 1fr));
|
|
292
|
+
gap: var(--pm-space-3);
|
|
293
|
+
margin: 0;
|
|
294
|
+
padding: 0;
|
|
295
|
+
list-style: none;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
.pmw-workspace-home__start .pm-ui-card {
|
|
299
|
+
height: 100%;
|
|
300
|
+
gap: var(--pm-space-2);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.pmw-workspace-home__attention {
|
|
304
|
+
display: grid;
|
|
305
|
+
gap: var(--pm-space-2);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
.pmw-workspace-home__path,
|
|
309
|
+
.pmw-workspace-home__time {
|
|
310
|
+
flex: none;
|
|
311
|
+
color: var(--pm-color-muted);
|
|
312
|
+
font-size: var(--pm-text-xs);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
.pmw-workspace-home__path {
|
|
316
|
+
flex: 1;
|
|
317
|
+
overflow: hidden;
|
|
318
|
+
text-overflow: ellipsis;
|
|
319
|
+
white-space: nowrap;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* The right pane with a document on the side is wider: the page beside it keeps the rest. */
|
|
323
|
+
.pmw-document[data-aside="peek"] .pmw-document__aside {
|
|
324
|
+
flex-basis: min(var(--pm-peek-width), 50%);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
.pmw-document:not([data-roomy])[data-aside="peek"] .pmw-document__aside {
|
|
328
|
+
width: min(100%, var(--pm-peek-width));
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
.pmw-peek__page {
|
|
332
|
+
margin: var(--pm-space-6) 0 var(--pm-space-2);
|
|
333
|
+
font-size: var(--pm-text-lg);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
.pmw-peek__page:first-child {
|
|
337
|
+
margin-top: 0;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
.pmw-peek__block {
|
|
341
|
+
min-width: 0;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
.pmw-peek__block > :first-child { margin-top: 0; }
|
|
345
|
+
.pmw-peek__block > :last-child { margin-bottom: 0; }
|
|
346
|
+
.pmw-peek__block + .pmw-peek__block { margin-top: var(--pm-space-3); }
|
|
347
|
+
|
|
348
|
+
.pmw-peek__synced {
|
|
349
|
+
display: flex;
|
|
350
|
+
flex-wrap: wrap;
|
|
351
|
+
align-items: center;
|
|
352
|
+
gap: var(--pm-space-1);
|
|
353
|
+
color: var(--pm-color-muted);
|
|
354
|
+
}
|
package/src/messages.ts
CHANGED
|
@@ -321,6 +321,7 @@ const zh = {
|
|
|
321
321
|
historyFailureReasons: {
|
|
322
322
|
'history-target-changed': '版本历史已变化,这一更改不再是下一步可撤消或恢复的更改。请查看最新的历史后重试。',
|
|
323
323
|
'history-conflict': '这次更改之后,相关内容又被修改过,或有尚未记入版本的修改,无法自动撤消。',
|
|
324
|
+
'history-format-boundary': '这一版本是文稿格式的升级(或文稿的创建),无法撤消或还原到它之前:之前的版本是旧格式,当前版本无法打开。',
|
|
324
325
|
'history-source-changed': '文稿已在别处修改,请查看最新的历史后重试。',
|
|
325
326
|
'history-snapshot-changed': '版本历史已变化,请查看最新的历史后重试。',
|
|
326
327
|
'history-revision-not-found': '找不到这个版本,它可能已不在当前历史中。',
|
|
@@ -364,6 +365,11 @@ const zh = {
|
|
|
364
365
|
place.quote ? `\n原文:“${place.quote}”` : '',
|
|
365
366
|
].join('')).filter(Boolean),
|
|
366
367
|
].join('\n\n'),
|
|
368
|
+
materialsRequest: (files: string[], note: string) => [
|
|
369
|
+
'根据这份文稿文件夹中的来源材料写这份文稿:先读完材料,再定标题,写出页面与正文。',
|
|
370
|
+
`来源材料:\n${files.map(file => `- ${file}`).join('\n')}`,
|
|
371
|
+
note.trim() ? `作者的要求:${note.trim()}` : '',
|
|
372
|
+
].filter(Boolean).join('\n\n'),
|
|
367
373
|
animationRequest: (page: number, title: string, place: AgentInsertion) => {
|
|
368
374
|
const at = `第 ${page} 页${title ? `“${title}”` : ''}`
|
|
369
375
|
return place.end ? `在${at}的末尾加一个动画:` : place.after === undefined ? `在${at}的开头加一个动画:`
|
|
@@ -693,6 +699,7 @@ const en: WorkbenchMessages = {
|
|
|
693
699
|
historyFailureReasons: {
|
|
694
700
|
'history-target-changed': 'Version History changed; this change is no longer the next one to undo or redo. Review the latest history and try again.',
|
|
695
701
|
'history-conflict': 'The content was changed again after this change, or has changes not yet saved as a version, so it can’t be undone automatically.',
|
|
702
|
+
'history-format-boundary': 'This version upgraded the document’s format (or created the document), so it can’t be undone or restored to an earlier version: those are in the earlier format, which this version can’t open.',
|
|
696
703
|
'history-source-changed': 'The document changed elsewhere. Review the latest history and try again.',
|
|
697
704
|
'history-snapshot-changed': 'Version History changed. Review the latest history and try again.',
|
|
698
705
|
'history-revision-not-found': 'Couldn’t find this version; it may no longer be in the current history.',
|
|
@@ -736,6 +743,11 @@ const en: WorkbenchMessages = {
|
|
|
736
743
|
place.quote ? `\nText: “${place.quote}”` : '',
|
|
737
744
|
].join('')).filter(Boolean),
|
|
738
745
|
].join('\n\n'),
|
|
746
|
+
materialsRequest: (files: string[], note: string) => [
|
|
747
|
+
'Write this document from the source material in its folder: read all of it first, then choose a title and write the pages and their text.',
|
|
748
|
+
`Source material:\n${files.map(file => `- ${file}`).join('\n')}`,
|
|
749
|
+
note.trim() ? `What the author wants: ${note.trim()}` : '',
|
|
750
|
+
].filter(Boolean).join('\n\n'),
|
|
739
751
|
animationRequest: (page: number, title: string, place: AgentInsertion) => {
|
|
740
752
|
const at = `page ${page}${title ? `, “${title}”` : ''}`
|
|
741
753
|
return place.end ? `Add an animation at the end of ${at}: ` : place.after === undefined ? `Add an animation at the start of ${at}: `
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import type { EmbedBlock, PresmiumDocument, ReferenceTarget } from '@lincyaw/kernel'
|
|
4
|
+
import { PmButton, PmDocMention, PmDocPeek, PmIcon, PmNotice, PmSkeleton } from '../components.tsx'
|
|
5
|
+
import { useTreeMessages, useWorkbench, useWorkbenchState } from '../context.tsx'
|
|
6
|
+
import { ReadOnlyBlock } from '../document-editor/references.tsx'
|
|
7
|
+
import type { ReadOnlyBlocks } from '../document-editor/references.tsx'
|
|
8
|
+
|
|
9
|
+
/** A burst of saves to the document on the side is read once, after it settles. */
|
|
10
|
+
const RELOAD_DELAY = 300
|
|
11
|
+
/**
|
|
12
|
+
* A long document is drawn over several frames so that none holds the page for long: the first frame draws this
|
|
13
|
+
* many blocks; when drawing them (until the page is painted) was quick the next frame draws twice as many, when it
|
|
14
|
+
* was slow half as many.
|
|
15
|
+
*/
|
|
16
|
+
const FIRST_BLOCKS = 60
|
|
17
|
+
const MOST_BLOCKS = 960
|
|
18
|
+
const LEAST_BLOCKS = 15
|
|
19
|
+
const QUICK_DRAW_MS = 12
|
|
20
|
+
const SLOW_DRAW_MS = 30
|
|
21
|
+
|
|
22
|
+
type Read = { state: 'loading' } | { state: 'failed' } | { state: 'ready', document: string, version: string, content: PresmiumDocument }
|
|
23
|
+
type Item = { kind: 'page', id: string, title: string } | { kind: 'block', id: string }
|
|
24
|
+
|
|
25
|
+
/** The pages and their top-level blocks in reading order: what the side draws, a few frames at a time. */
|
|
26
|
+
function itemsOf(content: PresmiumDocument): Item[] {
|
|
27
|
+
return content.sectionOrder.flatMap(id => {
|
|
28
|
+
const section = content.sections[id]
|
|
29
|
+
return section ? [{ kind: 'page' as const, id, title: section.title }, ...section.children.map(child => ({ kind: 'block' as const, id: child }))] : []
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The top-level block that holds `block` (itself when it is one), where the side scrolls to. */
|
|
34
|
+
function topBlockOf(content: PresmiumDocument, block: string): string | undefined {
|
|
35
|
+
const parents = new Map<string, string>()
|
|
36
|
+
for (const item of Object.values(content.blocks)) {
|
|
37
|
+
const children = item.type === 'container' ? item.children : item.type === 'component' ? item.children ?? [] : []
|
|
38
|
+
for (const child of children) parents.set(child, item.id)
|
|
39
|
+
}
|
|
40
|
+
let current = block
|
|
41
|
+
for (let depth = 0; parents.has(current) && depth < 64; depth++) current = parents.get(current)!
|
|
42
|
+
return content.blocks[current] ? current : undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The document a reference leads to, read-only in the right pane beside the open one ("Open on the Side"): its
|
|
47
|
+
* breadcrumb, title, Open and Close, and its pages, scrolled to the page or block of the reference. References in it
|
|
48
|
+
* open their target here in its place; Esc closes it. It is read when it opens and again after its saves settle
|
|
49
|
+
* (`document` events), never while the open document is typed in; a newer target cancels the read in flight.
|
|
50
|
+
*/
|
|
51
|
+
export function DocumentPeekPanel({ target }: { target: ReferenceTarget }) {
|
|
52
|
+
const workbench = useWorkbench()
|
|
53
|
+
const t = useTreeMessages()
|
|
54
|
+
const dir = useWorkbenchState(state => state.documents?.documents.find(doc => doc.id === target.document)?.dir ?? state.documentTree?.nodes[target.document]?.path)
|
|
55
|
+
const treeTitle = useWorkbenchState(state => state.documentTree?.nodes[target.document]?.title)
|
|
56
|
+
const trashed = useWorkbenchState(state => Boolean(state.documentTree?.trash.some(entry => entry.id === target.document)))
|
|
57
|
+
const treeKnown = useWorkbenchState(state => Boolean(state.documentTree))
|
|
58
|
+
const pathText = useWorkbenchState(state => state.documentTree && JSON.stringify(workbench.tree.ancestors(target.document)))
|
|
59
|
+
const path = useMemo<{ id: string, title: string }[]>(() => pathText ? JSON.parse(pathText) : [], [pathText])
|
|
60
|
+
// Titles of the references in it follow the tree.
|
|
61
|
+
const tree = useWorkbenchState(state => state.documentTree)
|
|
62
|
+
const [read, setRead] = useState<Read>({ state: 'loading' })
|
|
63
|
+
const [reload, setReload] = useState(0)
|
|
64
|
+
// The version on screen, and the latest one its `document` events announced.
|
|
65
|
+
const loaded = useRef<{ document?: string, version?: string }>({})
|
|
66
|
+
const announced = useRef<{ document?: string, version?: string }>({})
|
|
67
|
+
const reloadTimer = useRef<ReturnType<typeof setTimeout>>()
|
|
68
|
+
const reloadSoon = () => { clearTimeout(reloadTimer.current); reloadTimer.current = setTimeout(() => setReload(value => value + 1), RELOAD_DELAY) }
|
|
69
|
+
useEffect(() => () => clearTimeout(reloadTimer.current), [])
|
|
70
|
+
const frame = useRef<HTMLElement>(null)
|
|
71
|
+
const body = useRef<HTMLDivElement | null>(null)
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (dir === undefined) return
|
|
75
|
+
const controller = new AbortController()
|
|
76
|
+
// The same document read again stays on screen until the new one is there; another one starts from loading.
|
|
77
|
+
setRead(current => current.state === 'ready' && current.document === target.document ? current : { state: 'loading' })
|
|
78
|
+
workbench.client.readDocument(dir, controller.signal).then(answer => {
|
|
79
|
+
if (controller.signal.aborted) return
|
|
80
|
+
loaded.current = { document: target.document, version: answer.version }
|
|
81
|
+
if (announced.current.document === target.document && announced.current.version !== answer.version) reloadSoon()
|
|
82
|
+
setRead({ state: 'ready', document: target.document, version: answer.version, content: answer.content })
|
|
83
|
+
}, () => { if (!controller.signal.aborted) setRead({ state: 'failed' }) })
|
|
84
|
+
return () => controller.abort()
|
|
85
|
+
}, [workbench, dir, target.document, reload])
|
|
86
|
+
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (dir === undefined) return
|
|
89
|
+
const stop = workbench.client.subscribeDocument(dir, event => {
|
|
90
|
+
announced.current = { document: target.document, version: event.version }
|
|
91
|
+
// While the document is being read, the read compares when it arrives.
|
|
92
|
+
if (loaded.current.document !== target.document || event.version === loaded.current.version) return
|
|
93
|
+
reloadSoon()
|
|
94
|
+
})
|
|
95
|
+
return () => { clearTimeout(reloadTimer.current); stop() }
|
|
96
|
+
}, [workbench, dir, target.document])
|
|
97
|
+
|
|
98
|
+
// Focus moves into the side when it opens and back where it was when it closes; Esc closes it.
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
const before = document.activeElement
|
|
101
|
+
frame.current?.focus({ preventScroll: true })
|
|
102
|
+
const escape = (event: KeyboardEvent) => {
|
|
103
|
+
if (event.key !== 'Escape' || event.defaultPrevented || event.isComposing) return
|
|
104
|
+
if (document.querySelector('[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"]')) return
|
|
105
|
+
event.preventDefault()
|
|
106
|
+
workbench.tree.closePeek()
|
|
107
|
+
}
|
|
108
|
+
window.addEventListener('keydown', escape)
|
|
109
|
+
return () => {
|
|
110
|
+
window.removeEventListener('keydown', escape)
|
|
111
|
+
const focused = document.activeElement
|
|
112
|
+
if (before instanceof HTMLElement && before.isConnected && (!focused || focused === document.body || frame.current?.contains(focused))) before.focus({ preventScroll: true })
|
|
113
|
+
}
|
|
114
|
+
}, [workbench])
|
|
115
|
+
|
|
116
|
+
const content = read.state === 'ready' ? read.content : undefined
|
|
117
|
+
const items = useMemo(() => content ? itemsOf(content) : [], [content])
|
|
118
|
+
const targetBlock = useMemo(() => content && target.block ? topBlockOf(content, target.block) : undefined, [content, target.block])
|
|
119
|
+
const targetIndex = useMemo(() => {
|
|
120
|
+
if (targetBlock) return items.findIndex(item => item.kind === 'block' && item.id === targetBlock)
|
|
121
|
+
if (target.section) return items.findIndex(item => item.kind === 'page' && item.id === target.section)
|
|
122
|
+
// The whole document: it opens at its start, with nothing marked.
|
|
123
|
+
return -1
|
|
124
|
+
}, [items, targetBlock, target.section])
|
|
125
|
+
|
|
126
|
+
// Drawn a few frames at a time; a document read again keeps what is drawn.
|
|
127
|
+
const [shown, setShown] = useState(0)
|
|
128
|
+
const pace = useRef({ size: FIRST_BLOCKS, requested: 0 })
|
|
129
|
+
useEffect(() => { setShown(0); pace.current = { size: FIRST_BLOCKS, requested: 0 } }, [read.state === 'ready' ? read.document : undefined])
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
// Effects run after the paint: the time since the request is what drawing the last blocks cost.
|
|
132
|
+
const current = pace.current
|
|
133
|
+
if (current.requested) {
|
|
134
|
+
const cost = performance.now() - current.requested
|
|
135
|
+
current.size = cost > SLOW_DRAW_MS ? Math.max(LEAST_BLOCKS, current.size / 2) : cost < QUICK_DRAW_MS ? Math.min(MOST_BLOCKS, current.size * 2) : current.size
|
|
136
|
+
current.requested = 0
|
|
137
|
+
}
|
|
138
|
+
if (shown >= items.length) return
|
|
139
|
+
const id = requestAnimationFrame(() => {
|
|
140
|
+
current.requested = performance.now()
|
|
141
|
+
setShown(drawn => Math.min(items.length, drawn + current.size))
|
|
142
|
+
})
|
|
143
|
+
return () => cancelAnimationFrame(id)
|
|
144
|
+
}, [shown, items.length])
|
|
145
|
+
|
|
146
|
+
// Scrolled to the reference's place once it is drawn, again for each new target, not for a document read again.
|
|
147
|
+
const scrolled = useRef('')
|
|
148
|
+
const targetKey = JSON.stringify([target.document, target.section ?? '', target.block ?? ''])
|
|
149
|
+
useLayoutEffect(() => {
|
|
150
|
+
if (!content || scrolled.current === targetKey || shown <= Math.max(0, targetIndex)) return
|
|
151
|
+
scrolled.current = targetKey
|
|
152
|
+
const scroller = body.current
|
|
153
|
+
const place = scroller?.querySelector<HTMLElement>('[data-peek-target]')
|
|
154
|
+
if (!scroller) return
|
|
155
|
+
scroller.scrollTop = place ? place.getBoundingClientRect().top - scroller.getBoundingClientRect().top + scroller.scrollTop - scroller.clientHeight / 8 : 0
|
|
156
|
+
}, [content, targetKey, targetIndex, shown])
|
|
157
|
+
|
|
158
|
+
const view = useMemo<ReadOnlyBlocks | undefined>(() => content && dir !== undefined ? {
|
|
159
|
+
blocks: content.blocks,
|
|
160
|
+
asset: id => content.assets[id] ? workbench.client.documentAssetURL(dir, id) : undefined,
|
|
161
|
+
componentLabel: title => t.syncedComponent(title || content.metadata.title),
|
|
162
|
+
title: reference => reference.section || reference.block ? undefined : workbench.tree.title(reference.document),
|
|
163
|
+
onReference: reference => workbench.tree.peek(reference),
|
|
164
|
+
embed: (block: EmbedBlock) => <p className="pmw-peek__synced"><PmIcon name="synced" /><span>{t.syncedFrom}</span>
|
|
165
|
+
<PmDocMention title={workbench.tree.title(block.source.document) ?? t.missingDocument} onActivate={() => workbench.tree.peek({ document: block.source.document, block: block.source.block })} /></p>,
|
|
166
|
+
// The tree is a dependency: the titles of references follow it.
|
|
167
|
+
} : undefined, [content, dir, workbench, t, tree])
|
|
168
|
+
|
|
169
|
+
const missing = treeKnown && dir === undefined && !trashed
|
|
170
|
+
const title = treeTitle ?? content?.metadata.title ?? (trashed ? workbench.getState().documentTree?.trash.find(entry => entry.id === target.document)?.title : undefined) ?? t.untitled
|
|
171
|
+
const notice: ReactNode = trashed ? <PmNotice tone="warn">{t.previewTrashed}</PmNotice>
|
|
172
|
+
: missing ? <PmNotice tone="warn">{t.previewMissing}</PmNotice> : undefined
|
|
173
|
+
const open = () => void workbench.tree.openPlace(target, { focus: 'body' })
|
|
174
|
+
let main: ReactNode
|
|
175
|
+
if (dir === undefined) main = null
|
|
176
|
+
else if (read.state === 'loading') main = <PmSkeleton shape="document" label={t.peekLoading} />
|
|
177
|
+
else if (read.state === 'failed') main = <PmNotice tone="danger" action={<PmButton small onClick={() => setReload(value => value + 1)}>{t.retry}</PmButton>}>{t.peekFailed}</PmNotice>
|
|
178
|
+
else if (!items.length) main = <p className="pmw-muted">{t.peekEmpty}</p>
|
|
179
|
+
else main = <div className="pmw-peek pm-document-editor__synced-content">{view && items.slice(0, shown).map((item, index) => item.kind === 'page'
|
|
180
|
+
? <h3 key={`page:${item.id}`} className="pmw-peek__page" data-peek-target={index === targetIndex && !targetBlock || undefined}>{item.title || t.untitled}</h3>
|
|
181
|
+
: <div key={item.id} className="pmw-peek__block" data-peek-target={index === targetIndex && targetBlock !== undefined || undefined}><ReadOnlyBlock view={view} id={item.id} /></div>)}</div>
|
|
182
|
+
return (
|
|
183
|
+
<PmDocPeek ref={frame} label={t.peekLabel} path={path} title={title} closeLabel={t.close} onClose={() => workbench.tree.closePeek()}
|
|
184
|
+
onPath={id => workbench.tree.peek({ document: id })} busy={read.state === 'loading' || shown < items.length} notice={notice}
|
|
185
|
+
actions={!missing && !trashed && <PmButton small variant="primary" onClick={open}>{t.open}</PmButton>}
|
|
186
|
+
bodyRef={node => { body.current = node }}>
|
|
187
|
+
{main}
|
|
188
|
+
</PmDocPeek>
|
|
189
|
+
)
|
|
190
|
+
}
|