@whity/ui 0.5.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -107
- package/package.json +223 -192
- package/src/access-denied.stories.tsx +179 -179
- package/src/access-denied.tsx +305 -305
- package/src/action-proposal.tsx +338 -302
- package/src/actor-chip.tsx +71 -71
- package/src/agent-activity.tsx +163 -163
- package/src/agent-collaboration.stories.tsx +156 -156
- package/src/agent-run-status.tsx +196 -196
- package/src/alert-dialog.stories.tsx +45 -45
- package/src/alert-dialog.tsx +147 -147
- package/src/alert.stories.tsx +91 -91
- package/src/alert.tsx +89 -89
- package/src/app-sidebar.stories.tsx +195 -195
- package/src/app-sidebar.tsx +604 -500
- package/src/approval-queue.tsx +109 -108
- package/src/audience-group-picker.stories.tsx +198 -198
- package/src/audience-group-picker.tsx +362 -362
- package/src/audience-people-picker.stories.tsx +102 -102
- package/src/audience-people-picker.tsx +279 -279
- package/src/avatar-pattern.ts +138 -0
- package/src/avatar.tsx +125 -83
- package/src/badge.stories.tsx +105 -105
- package/src/badge.tsx +68 -68
- package/src/beta-warning.stories.tsx +79 -79
- package/src/beta-warning.tsx +189 -189
- package/src/bilingual-input.stories.tsx +82 -82
- package/src/bilingual-input.tsx +188 -183
- package/src/board.tsx +216 -0
- package/src/breadcrumb.stories.tsx +51 -51
- package/src/breadcrumb.tsx +90 -90
- package/src/built-on-whity.tsx +52 -0
- package/src/button.stories.tsx +87 -87
- package/src/button.tsx +125 -118
- package/src/card.stories.tsx +423 -423
- package/src/card.tsx +180 -180
- package/src/chart.stories.tsx +66 -66
- package/src/chart.tsx +228 -228
- package/src/checkbox.stories.tsx +62 -62
- package/src/checkbox.tsx +54 -54
- package/src/collapsible.stories.tsx +48 -48
- package/src/collapsible.tsx +34 -34
- package/src/color-card.stories.tsx +61 -61
- package/src/color-card.tsx +270 -270
- package/src/color-picker.stories.tsx +65 -65
- package/src/color-picker.tsx +425 -425
- package/src/column-matcher.tsx +84 -0
- package/src/command-palette.tsx +399 -306
- package/src/confirm-action.tsx +98 -0
- package/src/copy-block.stories.tsx +38 -38
- package/src/copy-block.tsx +85 -85
- package/src/data-table.stories.tsx +200 -200
- package/src/data-table.tsx +1687 -1106
- package/src/date-picker.stories.tsx +61 -61
- package/src/date-picker.tsx +457 -422
- package/src/design-cards.stories.tsx +551 -458
- package/src/dialog.stories.tsx +48 -48
- package/src/dialog.tsx +179 -166
- package/src/display-preferences.tsx +59 -0
- package/src/documents/barcode-svg.stories.tsx +72 -72
- package/src/documents/barcode-svg.tsx +79 -79
- package/src/dropdown-menu.stories.tsx +77 -77
- package/src/dropdown-menu.tsx +271 -271
- package/src/editor/agent-panel.tsx +222 -0
- package/src/editor/agents.stories.tsx +81 -0
- package/src/editor/agents.ts +55 -0
- package/src/editor/behaviour.ts +279 -279
- package/src/editor/block-ids.ts +44 -44
- package/src/editor/block-list.tsx +104 -101
- package/src/editor/block-toolbar.tsx +490 -477
- package/src/editor/bubble.tsx +119 -93
- package/src/editor/catalogue.ts +169 -103
- package/src/editor/collab-rail.tsx +746 -746
- package/src/editor/collab.stories.tsx +207 -207
- package/src/editor/collab.ts +87 -87
- package/src/editor/contract.ts +476 -408
- package/src/editor/convert.ts +414 -362
- package/src/editor/doc-editor.stories.tsx +98 -98
- package/src/editor/doc-editor.tsx +1492 -1287
- package/src/editor/editor.css +636 -536
- package/src/editor/extensions.ts +86 -86
- package/src/editor/field-bar.tsx +154 -0
- package/src/editor/field-picker.tsx +109 -107
- package/src/editor/fields.ts +100 -100
- package/src/editor/gutter.tsx +443 -367
- package/src/editor/index.ts +23 -22
- package/src/editor/label-sheet.tsx +289 -289
- package/src/editor/labels.ts +394 -310
- package/src/editor/link-tools.tsx +141 -141
- package/src/editor/node-views.tsx +461 -450
- package/src/editor/page-view.tsx +372 -353
- package/src/editor/phone.tsx +310 -0
- package/src/editor/plugin-block-view.tsx +230 -0
- package/src/editor/plugin-blocks.stories.tsx +96 -0
- package/src/editor/plugin-blocks.ts +52 -0
- package/src/editor/publish-check.tsx +201 -182
- package/src/editor/rich-blocks.stories.tsx +146 -146
- package/src/editor/sample.ts +40 -40
- package/src/editor/save-block-dialog.tsx +141 -0
- package/src/editor/saved-block-view.tsx +205 -0
- package/src/editor/saved-blocks.stories.tsx +105 -0
- package/src/editor/saved-parts.ts +192 -0
- package/src/editor/schema-rich.ts +156 -150
- package/src/editor/schema.ts +368 -315
- package/src/editor/services.tsx +205 -127
- package/src/editor/slash.ts +64 -64
- package/src/editor/suggesting.ts +372 -372
- package/src/editor/suggestions.ts +763 -763
- package/src/editor/table-keys.ts +66 -66
- package/src/editor/templates.stories.tsx +324 -324
- package/src/editor/toolbar.tsx +364 -358
- package/src/empty-state.stories.tsx +51 -51
- package/src/empty-state.tsx +126 -100
- package/src/empty-state.usage-notes.md +30 -30
- package/src/form.stories.tsx +112 -112
- package/src/form.tsx +176 -176
- package/src/globals.css +20 -20
- package/src/icon-button.tsx +85 -85
- package/src/icon-map.ts +335 -244
- package/src/icon.tsx +41 -41
- package/src/index.ts +92 -67
- package/src/input.stories.tsx +157 -157
- package/src/input.tsx +768 -759
- package/src/kpi-strip.tsx +47 -0
- package/src/localized-text.ts +76 -0
- package/src/locked-screen.stories.tsx +29 -29
- package/src/locked-screen.tsx +75 -61
- package/src/lockup.tsx +42 -0
- package/src/lottie.tsx +441 -0
- package/src/mark.tsx +62 -62
- package/src/math-text.stories.tsx +33 -33
- package/src/math-text.tsx +43 -43
- package/src/menu-bar.stories.tsx +198 -198
- package/src/menu-bar.tsx +373 -373
- package/src/message-thread.tsx +181 -0
- package/src/money/currency.ts +244 -230
- package/src/money/money-input.tsx +131 -0
- package/src/money/money.tsx +33 -0
- package/src/open-on-computer.tsx +150 -0
- package/src/otp-input.tsx +161 -0
- package/src/page-header.stories.tsx +95 -95
- package/src/page-header.tsx +122 -122
- package/src/page-shell.stories.tsx +42 -42
- package/src/page-shell.tsx +32 -32
- package/src/page-states.tsx +277 -0
- package/src/pagination.stories.tsx +45 -45
- package/src/pagination.tsx +98 -98
- package/src/paper-sheet.stories.tsx +88 -0
- package/src/paper-sheet.tsx +85 -0
- package/src/phone-input.tsx +252 -0
- package/src/phone-tab-bar.tsx +152 -0
- package/src/plugin-card.stories.tsx +208 -180
- package/src/plugin-card.tsx +931 -848
- package/src/popover.stories.tsx +48 -48
- package/src/popover.tsx +55 -55
- package/src/radio-group.stories.tsx +26 -26
- package/src/radio-group.tsx +44 -44
- package/src/role-summary.stories.tsx +143 -0
- package/src/role-summary.tsx +120 -0
- package/src/route-flow/editor.tsx +1087 -1014
- package/src/route-flow/model.ts +771 -716
- package/src/save-bar.tsx +94 -0
- package/src/scope-grant.tsx +139 -139
- package/src/screen-too-small.stories.tsx +61 -61
- package/src/screen-too-small.tsx +104 -104
- package/src/search-field.tsx +65 -0
- package/src/select.stories.tsx +44 -44
- package/src/select.tsx +197 -197
- package/src/separator.stories.tsx +34 -34
- package/src/separator.tsx +34 -34
- package/src/sheet.stories.tsx +49 -49
- package/src/sheet.tsx +168 -168
- package/src/skeleton.stories.tsx +28 -28
- package/src/skeleton.tsx +19 -19
- package/src/slider.stories.tsx +78 -78
- package/src/slider.tsx +300 -300
- package/src/spinner.stories.tsx +33 -33
- package/src/spinner.tsx +55 -55
- package/src/stepper.tsx +85 -0
- package/src/switch.stories.tsx +42 -42
- package/src/switch.tsx +41 -41
- package/src/switcher.stories.tsx +111 -111
- package/src/switcher.tsx +131 -131
- package/src/system-state.tsx +55 -0
- package/src/table.stories.tsx +48 -48
- package/src/table.tsx +121 -121
- package/src/tabs.stories.tsx +74 -74
- package/src/tabs.tsx +90 -90
- package/src/tag-input.stories.tsx +67 -67
- package/src/tag-input.tsx +117 -117
- package/src/tenant-logo.tsx +63 -63
- package/src/tenant-switcher.stories.tsx +38 -38
- package/src/tenant-switcher.tsx +185 -164
- package/src/textarea.stories.tsx +27 -27
- package/src/textarea.tsx +18 -18
- package/src/theme-mode.ts +74 -52
- package/src/time-grid.tsx +163 -0
- package/src/time-picker.stories.tsx +62 -62
- package/src/time-picker.tsx +616 -615
- package/src/toggle-group.tsx +139 -138
- package/src/tooltip.stories.tsx +43 -43
- package/src/tooltip.tsx +73 -73
- package/src/top-nav.stories.tsx +58 -58
- package/src/top-nav.tsx +324 -324
- package/src/undo-bar.tsx +107 -107
- package/src/utils.ts +45 -45
- package/src/view-toggle.stories.tsx +84 -84
- package/src/view-toggle.tsx +111 -110
package/src/editor/convert.ts
CHANGED
|
@@ -1,362 +1,414 @@
|
|
|
1
|
-
import type { JSONContent } from "@tiptap/core"
|
|
2
|
-
|
|
3
|
-
import type { Block, Inline, Mark as DocMark, Text } from "./contract"
|
|
4
|
-
import { MARK_ORDER } from "./schema"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WhityDocument blocks ⇄ ProseMirror JSON. `toEditorContent` and
|
|
8
|
-
* `fromEditorContent` are inverses on normalised documents
|
|
9
|
-
* (`normalizeBlocks`): adjacent text runs with the same marks are one run,
|
|
10
|
-
* marks are in schema order, and empty runs are dropped. Blocks the editor
|
|
11
|
-
* doesn't edit yet travel through `unknownBlock` unchanged.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
type PmMark = { type: string; attrs?: Record<string, unknown> }
|
|
15
|
-
|
|
16
|
-
const TEXT_BLOCKS = new Set(["paragraph", "heading", "quote", "bullet", "number", "checklist", "code", "divider", "callout"])
|
|
17
|
-
|
|
18
|
-
/* ------------------------------------------------------------- inline */
|
|
19
|
-
|
|
20
|
-
function markToPm(mark: DocMark): PmMark {
|
|
21
|
-
switch (mark.type) {
|
|
22
|
-
case "link":
|
|
23
|
-
return { type: "link", attrs: { href: mark.href, recordRef: mark.recordRef ?? null } }
|
|
24
|
-
case "comment":
|
|
25
|
-
return { type: "comment", attrs: { threadId: mark.threadId } }
|
|
26
|
-
case "insertion":
|
|
27
|
-
case "deletion":
|
|
28
|
-
return { type: mark.type, attrs: { suggestionId: mark.suggestionId } }
|
|
29
|
-
|
|
30
|
-
return { type: mark.
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
case "
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return { type: mark.type
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
function
|
|
122
|
-
return
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
case "
|
|
138
|
-
return {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
case "
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
case "
|
|
153
|
-
return {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
case "
|
|
163
|
-
return {
|
|
164
|
-
type: "
|
|
165
|
-
attrs: common(block),
|
|
166
|
-
content:
|
|
167
|
-
}
|
|
168
|
-
case "
|
|
169
|
-
return {
|
|
170
|
-
type: "
|
|
171
|
-
attrs:
|
|
172
|
-
content: block.
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
case "
|
|
184
|
-
return { type: "
|
|
185
|
-
case "
|
|
186
|
-
return { type: "
|
|
187
|
-
case "
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
case "
|
|
246
|
-
return withCommon(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
a
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
case "
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
},
|
|
274
|
-
a
|
|
275
|
-
)
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
return withCommon(
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
a
|
|
291
|
-
)
|
|
292
|
-
case "
|
|
293
|
-
return withCommon({ id, type: "
|
|
294
|
-
case "
|
|
295
|
-
return withCommon(
|
|
296
|
-
optional<Of<"
|
|
297
|
-
{ id, type: "
|
|
298
|
-
"
|
|
299
|
-
a.
|
|
300
|
-
),
|
|
301
|
-
a
|
|
302
|
-
)
|
|
303
|
-
case "
|
|
304
|
-
return withCommon(
|
|
305
|
-
{ id, type: "
|
|
306
|
-
a
|
|
307
|
-
)
|
|
308
|
-
case "
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
return withCommon(
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
case "
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
1
|
+
import type { JSONContent } from "@tiptap/core"
|
|
2
|
+
|
|
3
|
+
import type { Block, Inline, Mark as DocMark, Text } from "./contract"
|
|
4
|
+
import { MARK_ORDER } from "./schema"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WhityDocument blocks ⇄ ProseMirror JSON. `toEditorContent` and
|
|
8
|
+
* `fromEditorContent` are inverses on normalised documents
|
|
9
|
+
* (`normalizeBlocks`): adjacent text runs with the same marks are one run,
|
|
10
|
+
* marks are in schema order, and empty runs are dropped. Blocks the editor
|
|
11
|
+
* doesn't edit yet travel through `unknownBlock` unchanged.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
type PmMark = { type: string; attrs?: Record<string, unknown> }
|
|
15
|
+
|
|
16
|
+
const TEXT_BLOCKS = new Set(["paragraph", "heading", "quote", "bullet", "number", "checklist", "code", "divider", "callout"])
|
|
17
|
+
|
|
18
|
+
/* ------------------------------------------------------------- inline */
|
|
19
|
+
|
|
20
|
+
function markToPm(mark: DocMark): PmMark {
|
|
21
|
+
switch (mark.type) {
|
|
22
|
+
case "link":
|
|
23
|
+
return { type: "link", attrs: { href: mark.href, recordRef: mark.recordRef ?? null } }
|
|
24
|
+
case "comment":
|
|
25
|
+
return { type: "comment", attrs: { threadId: mark.threadId } }
|
|
26
|
+
case "insertion":
|
|
27
|
+
case "deletion":
|
|
28
|
+
return { type: mark.type, attrs: { suggestionId: mark.suggestionId } }
|
|
29
|
+
case "perDocument":
|
|
30
|
+
return { type: "perDocument", attrs: { key: mark.key || null } }
|
|
31
|
+
default:
|
|
32
|
+
return { type: mark.type }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function markFromPm(mark: PmMark): DocMark {
|
|
37
|
+
const a = mark.attrs ?? {}
|
|
38
|
+
switch (mark.type) {
|
|
39
|
+
case "link":
|
|
40
|
+
return a.recordRef
|
|
41
|
+
? { type: "link", href: String(a.href ?? ""), recordRef: a.recordRef as { type: string; id: string } }
|
|
42
|
+
: { type: "link", href: String(a.href ?? "") }
|
|
43
|
+
case "comment":
|
|
44
|
+
return { type: "comment", threadId: String(a.threadId ?? "") }
|
|
45
|
+
case "insertion":
|
|
46
|
+
case "deletion":
|
|
47
|
+
return { type: mark.type, suggestionId: String(a.suggestionId ?? "") }
|
|
48
|
+
case "perDocument":
|
|
49
|
+
// E.23a: the part's stable key; a pre-3.4 mark has none until it is keyed.
|
|
50
|
+
return (typeof a.key === "string" && a.key ? { type: "perDocument", key: a.key } : { type: "perDocument" }) as DocMark
|
|
51
|
+
default:
|
|
52
|
+
return { type: mark.type } as DocMark
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const markRank = (m: DocMark) => MARK_ORDER.indexOf(m.type as (typeof MARK_ORDER)[number])
|
|
57
|
+
|
|
58
|
+
function sortMarks(marks: DocMark[] | undefined): DocMark[] | undefined {
|
|
59
|
+
if (!marks || marks.length === 0) return undefined
|
|
60
|
+
return [...marks].sort((a, b) => markRank(a) - markRank(b))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const sameMarks = (a?: DocMark[], b?: DocMark[]) => JSON.stringify(a ?? []) === JSON.stringify(b ?? [])
|
|
64
|
+
|
|
65
|
+
function inlineToPm(content: Inline[]): JSONContent[] {
|
|
66
|
+
const out: JSONContent[] = []
|
|
67
|
+
for (const run of content) {
|
|
68
|
+
if (run.type === "text") {
|
|
69
|
+
if (!run.text) continue
|
|
70
|
+
const node: JSONContent = { type: "text", text: run.text }
|
|
71
|
+
if (run.marks?.length) node.marks = run.marks.map(markToPm)
|
|
72
|
+
out.push(node)
|
|
73
|
+
} else if (run.type === "break") {
|
|
74
|
+
out.push({ type: "hardBreak" })
|
|
75
|
+
} else if (run.type === "math") {
|
|
76
|
+
out.push({ type: "mathInline", attrs: { tex: run.tex } })
|
|
77
|
+
} else {
|
|
78
|
+
const node: JSONContent = {
|
|
79
|
+
type: "field",
|
|
80
|
+
attrs: { path: run.path, label: run.label, required: run.required ?? null, perDocument: run.perDocument ?? null },
|
|
81
|
+
}
|
|
82
|
+
if (run.marks?.length) node.marks = run.marks.map(markToPm)
|
|
83
|
+
out.push(node)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return out
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function inlineFromPm(content: JSONContent[] | undefined): Inline[] {
|
|
90
|
+
const out: Inline[] = []
|
|
91
|
+
for (const node of content ?? []) {
|
|
92
|
+
if (node.type === "text") {
|
|
93
|
+
const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
|
|
94
|
+
const last = out[out.length - 1]
|
|
95
|
+
if (last && last.type === "text" && sameMarks(last.marks, marks)) {
|
|
96
|
+
last.text += node.text ?? ""
|
|
97
|
+
} else {
|
|
98
|
+
const run: Text = { type: "text", text: node.text ?? "" }
|
|
99
|
+
if (marks) run.marks = marks
|
|
100
|
+
out.push(run)
|
|
101
|
+
}
|
|
102
|
+
} else if (node.type === "hardBreak") {
|
|
103
|
+
out.push({ type: "break" })
|
|
104
|
+
} else if (node.type === "mathInline") {
|
|
105
|
+
out.push({ type: "math", tex: String(node.attrs?.tex ?? "") })
|
|
106
|
+
} else if (node.type === "field") {
|
|
107
|
+
const a = node.attrs ?? {}
|
|
108
|
+
const field: Inline = { type: "field", path: String(a.path ?? ""), label: String(a.label ?? "") }
|
|
109
|
+
if (a.required != null) field.required = Boolean(a.required)
|
|
110
|
+
// E.23a: the part's key; `true` is a pre-3.4 field not keyed yet.
|
|
111
|
+
if (a.perDocument != null) field.perDocument = (typeof a.perDocument === "string" ? a.perDocument : true) as string
|
|
112
|
+
const marks = sortMarks(node.marks?.map((m) => markFromPm(m as PmMark)))
|
|
113
|
+
if (marks) field.marks = marks
|
|
114
|
+
out.push(field)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The canonical form of inline content: what comes back out of the editor. */
|
|
121
|
+
export function normalizeInline(content: Inline[]): Inline[] {
|
|
122
|
+
return inlineFromPm(inlineToPm(content))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/* ------------------------------------------------------------- blocks */
|
|
126
|
+
|
|
127
|
+
function common(block: Block) {
|
|
128
|
+
return { id: block.id, suggestion: block.suggestion ?? null }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function blockToPm(block: Block): JSONContent {
|
|
132
|
+
const content = (inline: Inline[]) => {
|
|
133
|
+
const c = inlineToPm(inline)
|
|
134
|
+
return c.length ? { content: c } : {}
|
|
135
|
+
}
|
|
136
|
+
switch (block.type) {
|
|
137
|
+
case "paragraph":
|
|
138
|
+
return { type: "paragraph", attrs: common(block), ...content(block.content) }
|
|
139
|
+
case "heading":
|
|
140
|
+
return { type: "heading", attrs: { ...common(block), level: block.level }, ...content(block.content) }
|
|
141
|
+
case "quote":
|
|
142
|
+
return { type: "quote", attrs: common(block), ...content(block.content) }
|
|
143
|
+
case "code":
|
|
144
|
+
return {
|
|
145
|
+
type: "codeBlock",
|
|
146
|
+
attrs: { ...common(block), language: block.language ?? null },
|
|
147
|
+
...(block.text ? { content: [{ type: "text", text: block.text }] } : {}),
|
|
148
|
+
}
|
|
149
|
+
case "bullet":
|
|
150
|
+
case "number":
|
|
151
|
+
return { type: "listItem", attrs: { ...common(block), kind: block.type, level: block.level }, ...content(block.content) }
|
|
152
|
+
case "checklist":
|
|
153
|
+
return {
|
|
154
|
+
type: "checkItem",
|
|
155
|
+
attrs: { ...common(block), level: block.level, checked: block.checked },
|
|
156
|
+
...content(block.content),
|
|
157
|
+
}
|
|
158
|
+
case "divider":
|
|
159
|
+
return { type: "divider", attrs: common(block) }
|
|
160
|
+
case "callout":
|
|
161
|
+
return { type: "callout", attrs: { ...common(block), kind: block.kind }, ...content(block.content) }
|
|
162
|
+
case "toggle":
|
|
163
|
+
return {
|
|
164
|
+
type: "toggle",
|
|
165
|
+
attrs: { ...common(block), open: block.open },
|
|
166
|
+
content: [{ type: "toggleSummary", ...content(block.summary) }, ...block.children.map(blockToPm)],
|
|
167
|
+
}
|
|
168
|
+
case "columns":
|
|
169
|
+
return {
|
|
170
|
+
type: "columns",
|
|
171
|
+
attrs: common(block),
|
|
172
|
+
content: block.columns.map((col) => ({ type: "column", ...(col.length ? { content: col.map(blockToPm) } : {}) })),
|
|
173
|
+
}
|
|
174
|
+
case "table":
|
|
175
|
+
return {
|
|
176
|
+
type: "table",
|
|
177
|
+
attrs: { ...common(block), headerRow: block.headerRow, columns: block.columns },
|
|
178
|
+
content: block.rows.map((row) => ({
|
|
179
|
+
type: "tableRow",
|
|
180
|
+
content: row.map((cell) => ({ type: "tableCell", ...content(cell) })),
|
|
181
|
+
})),
|
|
182
|
+
}
|
|
183
|
+
case "pageBreak":
|
|
184
|
+
return { type: "pageBreak", attrs: common(block) }
|
|
185
|
+
case "image":
|
|
186
|
+
return { type: "image", attrs: { ...common(block), src: block.src, alt: block.alt, width: block.width, caption: block.caption ?? null } }
|
|
187
|
+
case "file":
|
|
188
|
+
return { type: "file", attrs: { ...common(block), fileId: block.fileId, name: block.name, size: block.size, mime: block.mime } }
|
|
189
|
+
case "record":
|
|
190
|
+
return { type: "record", attrs: { ...common(block), ref: block.ref, view: block.view } }
|
|
191
|
+
case "chart":
|
|
192
|
+
return { type: "chart", attrs: { ...common(block), chart: block.chart, source: block.source, title: block.title ?? null } }
|
|
193
|
+
case "signature":
|
|
194
|
+
// @date-display-ignore: the signature date is text the author typed or a merge field (Inline[]), not a record timestamp.
|
|
195
|
+
return { type: "signature", attrs: { ...common(block), signers: block.signers, date: block.date, stamp: block.stamp } }
|
|
196
|
+
case "barcode":
|
|
197
|
+
return {
|
|
198
|
+
type: "barcode",
|
|
199
|
+
attrs: {
|
|
200
|
+
...common(block),
|
|
201
|
+
symbology: block.symbology,
|
|
202
|
+
value: block.value,
|
|
203
|
+
showText: block.showText,
|
|
204
|
+
widthMm: block.widthMm ?? null,
|
|
205
|
+
heightMm: block.heightMm ?? null,
|
|
206
|
+
},
|
|
207
|
+
}
|
|
208
|
+
case "qr":
|
|
209
|
+
return { type: "qr", attrs: { ...common(block), value: block.value, caption: block.caption ?? null, sizeMm: block.sizeMm ?? null } }
|
|
210
|
+
case "math":
|
|
211
|
+
return { type: "math", attrs: { ...common(block), tex: block.tex } }
|
|
212
|
+
case "plugin":
|
|
213
|
+
return {
|
|
214
|
+
type: "plugin",
|
|
215
|
+
attrs: { ...common(block), plugin: block.plugin, blockType: block.blockType, schemaVersion: block.schemaVersion, data: block.data },
|
|
216
|
+
}
|
|
217
|
+
case "savedBlock":
|
|
218
|
+
return {
|
|
219
|
+
type: "savedBlock",
|
|
220
|
+
attrs: { ...common(block), savedBlockId: block.savedBlockId, version: block.version, overrides: block.overrides },
|
|
221
|
+
}
|
|
222
|
+
default:
|
|
223
|
+
return { type: "unknownBlock", attrs: { ...common(block), data: block } }
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Copy an optional contract field only when it is set, so a round trip adds nothing. */
|
|
228
|
+
function optional<T extends object>(block: T, key: string, value: unknown): T {
|
|
229
|
+
if (value !== null && value !== undefined) (block as Record<string, unknown>)[key] = value
|
|
230
|
+
return block
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
type Of<T extends Block["type"]> = Extract<Block, { type: T }>
|
|
234
|
+
|
|
235
|
+
function withCommon<T extends Block>(block: T, attrs: Record<string, unknown>): T {
|
|
236
|
+
if (attrs.suggestion) (block as Block).suggestion = attrs.suggestion as Block["suggestion"]
|
|
237
|
+
return block
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function blockFromPm(node: JSONContent): Block | null {
|
|
241
|
+
const a = node.attrs ?? {}
|
|
242
|
+
const id = String(a.id ?? "")
|
|
243
|
+
const inline = () => inlineFromPm(node.content)
|
|
244
|
+
switch (node.type) {
|
|
245
|
+
case "paragraph":
|
|
246
|
+
return withCommon({ id, type: "paragraph", content: inline() }, a)
|
|
247
|
+
case "heading":
|
|
248
|
+
return withCommon({ id, type: "heading", level: Number(a.level) as 1 | 2 | 3, content: inline() }, a)
|
|
249
|
+
case "quote":
|
|
250
|
+
return withCommon({ id, type: "quote", content: inline() }, a)
|
|
251
|
+
case "codeBlock": {
|
|
252
|
+
const block: Block = { id, type: "code", text: (node.content ?? []).map((t) => t.text ?? "").join("") }
|
|
253
|
+
if (a.language) block.language = String(a.language)
|
|
254
|
+
return withCommon(block, a)
|
|
255
|
+
}
|
|
256
|
+
case "listItem":
|
|
257
|
+
return withCommon(
|
|
258
|
+
{ id, type: a.kind === "number" ? "number" : "bullet", level: Number(a.level) as 0 | 1 | 2, content: inline() },
|
|
259
|
+
a
|
|
260
|
+
)
|
|
261
|
+
case "checkItem":
|
|
262
|
+
return withCommon(
|
|
263
|
+
{ id, type: "checklist", level: Number(a.level) as 0 | 1 | 2, checked: Boolean(a.checked), content: inline() },
|
|
264
|
+
a
|
|
265
|
+
)
|
|
266
|
+
case "divider":
|
|
267
|
+
return withCommon({ id, type: "divider" }, a)
|
|
268
|
+
case "callout":
|
|
269
|
+
return withCommon({ id, type: "callout", kind: a.kind === "important" ? "important" : "note", content: inline() }, a)
|
|
270
|
+
case "toggle": {
|
|
271
|
+
const [summary, ...children] = node.content ?? []
|
|
272
|
+
return withCommon(
|
|
273
|
+
{ id, type: "toggle", open: Boolean(a.open), summary: inlineFromPm(summary?.content), children: blocksFromPm(children) },
|
|
274
|
+
a
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
case "columns": {
|
|
278
|
+
const cols = (node.content ?? []).map((col) => blocksFromPm(col.content))
|
|
279
|
+
return withCommon({ id, type: "columns", columns: [cols[0] ?? [], cols[1] ?? []] }, a)
|
|
280
|
+
}
|
|
281
|
+
case "table":
|
|
282
|
+
return withCommon(
|
|
283
|
+
{
|
|
284
|
+
id,
|
|
285
|
+
type: "table",
|
|
286
|
+
headerRow: Boolean(a.headerRow),
|
|
287
|
+
columns: (a.columns ?? []) as Of<"table">["columns"],
|
|
288
|
+
rows: (node.content ?? []).map((row) => (row.content ?? []).map((cell) => inlineFromPm(cell.content))),
|
|
289
|
+
},
|
|
290
|
+
a
|
|
291
|
+
)
|
|
292
|
+
case "pageBreak":
|
|
293
|
+
return withCommon({ id, type: "pageBreak" }, a)
|
|
294
|
+
case "image":
|
|
295
|
+
return withCommon(
|
|
296
|
+
optional<Of<"image">>(
|
|
297
|
+
{ id, type: "image", src: String(a.src ?? ""), alt: String(a.alt ?? ""), width: (a.width ?? "full") as Of<"image">["width"] },
|
|
298
|
+
"caption",
|
|
299
|
+
a.caption
|
|
300
|
+
),
|
|
301
|
+
a
|
|
302
|
+
)
|
|
303
|
+
case "file":
|
|
304
|
+
return withCommon(
|
|
305
|
+
{ id, type: "file", fileId: String(a.fileId ?? ""), name: String(a.name ?? ""), size: Number(a.size ?? 0), mime: String(a.mime ?? "") },
|
|
306
|
+
a
|
|
307
|
+
)
|
|
308
|
+
case "record":
|
|
309
|
+
return withCommon({ id, type: "record", ref: a.ref as Of<"record">["ref"], view: a.view === "summary" ? "summary" : "card" }, a)
|
|
310
|
+
case "chart":
|
|
311
|
+
return withCommon(
|
|
312
|
+
optional<Of<"chart">>(
|
|
313
|
+
{ id, type: "chart", chart: (a.chart ?? "bar") as Of<"chart">["chart"], source: a.source as Of<"chart">["source"] },
|
|
314
|
+
"title",
|
|
315
|
+
a.title
|
|
316
|
+
),
|
|
317
|
+
a
|
|
318
|
+
)
|
|
319
|
+
case "signature":
|
|
320
|
+
return withCommon(
|
|
321
|
+
{ id, type: "signature", signers: (a.signers ?? []) as Of<"signature">["signers"], date: (a.date ?? []) as Inline[], stamp: Boolean(a.stamp) },
|
|
322
|
+
a
|
|
323
|
+
)
|
|
324
|
+
case "barcode": {
|
|
325
|
+
const block: Of<"barcode"> = {
|
|
326
|
+
id,
|
|
327
|
+
type: "barcode",
|
|
328
|
+
symbology: (a.symbology ?? "code128") as Of<"barcode">["symbology"],
|
|
329
|
+
value: (a.value ?? []) as Of<"barcode">["value"],
|
|
330
|
+
showText: Boolean(a.showText),
|
|
331
|
+
}
|
|
332
|
+
return withCommon(optional(optional(block, "widthMm", a.widthMm), "heightMm", a.heightMm), a)
|
|
333
|
+
}
|
|
334
|
+
case "qr": {
|
|
335
|
+
const block: Of<"qr"> = { id, type: "qr", value: (a.value ?? []) as Of<"qr">["value"] }
|
|
336
|
+
return withCommon(optional(optional(block, "caption", a.caption), "sizeMm", a.sizeMm), a)
|
|
337
|
+
}
|
|
338
|
+
case "math":
|
|
339
|
+
return withCommon({ id, type: "math", tex: String(a.tex ?? "") }, a)
|
|
340
|
+
case "plugin":
|
|
341
|
+
return withCommon(
|
|
342
|
+
{
|
|
343
|
+
id,
|
|
344
|
+
type: "plugin",
|
|
345
|
+
plugin: String(a.plugin ?? ""),
|
|
346
|
+
blockType: String(a.blockType ?? ""),
|
|
347
|
+
schemaVersion: Number(a.schemaVersion ?? 1),
|
|
348
|
+
data: (a.data ?? {}) as Of<"plugin">["data"],
|
|
349
|
+
},
|
|
350
|
+
a
|
|
351
|
+
)
|
|
352
|
+
case "savedBlock":
|
|
353
|
+
return withCommon(
|
|
354
|
+
{
|
|
355
|
+
id,
|
|
356
|
+
type: "savedBlock",
|
|
357
|
+
savedBlockId: String(a.savedBlockId ?? ""),
|
|
358
|
+
version: Number(a.version ?? 1),
|
|
359
|
+
overrides: (a.overrides ?? {}) as Of<"savedBlock">["overrides"],
|
|
360
|
+
},
|
|
361
|
+
a
|
|
362
|
+
)
|
|
363
|
+
case "unknownBlock":
|
|
364
|
+
return a.data ? { ...(a.data as Block), id } : null
|
|
365
|
+
default:
|
|
366
|
+
return null
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function blocksFromPm(nodes: JSONContent[] | undefined): Block[] {
|
|
371
|
+
return (nodes ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Blocks as fresh editor content: the same blocks with no ids, so the editor
|
|
376
|
+
* gives each a new one (a saved block's copy, or a detached synced block).
|
|
377
|
+
*/
|
|
378
|
+
export function freshEditorBlocks(blocks: Block[]): JSONContent[] {
|
|
379
|
+
const strip = (node: JSONContent): JSONContent => ({
|
|
380
|
+
...node,
|
|
381
|
+
...(node.attrs && "id" in node.attrs ? { attrs: { ...node.attrs, id: null } } : {}),
|
|
382
|
+
...(node.content ? { content: node.content.map(strip) } : {}),
|
|
383
|
+
})
|
|
384
|
+
return blocks.map((b) => strip(blockToPm(b)))
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The editor's content for a document's blocks. An empty document gets one empty paragraph. */
|
|
388
|
+
export function toEditorContent(blocks: Block[]): JSONContent {
|
|
389
|
+
const content = blocks.map(blockToPm)
|
|
390
|
+
return { type: "doc", content: content.length ? content : [{ type: "paragraph", attrs: { id: null, suggestion: null } }] }
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function fromEditorContent(doc: JSONContent): Block[] {
|
|
394
|
+
return (doc.content ?? []).map(blockFromPm).filter((b): b is Block => b !== null)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** The canonical form of a document's blocks: equal to what a round trip through the editor returns. */
|
|
398
|
+
export function normalizeBlocks(blocks: Block[]): Block[] {
|
|
399
|
+
return blocks.map((block) => {
|
|
400
|
+
switch (block.type) {
|
|
401
|
+
case "toggle":
|
|
402
|
+
return { ...block, summary: normalizeInline(block.summary), children: normalizeBlocks(block.children) }
|
|
403
|
+
case "columns":
|
|
404
|
+
return { ...block, columns: [normalizeBlocks(block.columns[0]), normalizeBlocks(block.columns[1])] as Of<"columns">["columns"] }
|
|
405
|
+
case "table":
|
|
406
|
+
return { ...block, rows: block.rows.map((row) => row.map(normalizeInline)) }
|
|
407
|
+
}
|
|
408
|
+
if (!TEXT_BLOCKS.has(block.type)) return block
|
|
409
|
+
if ("content" in block && Array.isArray(block.content)) {
|
|
410
|
+
return { ...block, content: normalizeInline(block.content) } as Block
|
|
411
|
+
}
|
|
412
|
+
return block
|
|
413
|
+
})
|
|
414
|
+
}
|