@whity/ui 0.5.0 → 1.0.0
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 +221 -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 +123 -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 +398 -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 +1686 -1106
- package/src/date-picker.stories.tsx +61 -61
- package/src/date-picker.tsx +456 -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 +50 -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 +90 -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-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/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 +164 -164
- package/src/textarea.stories.tsx +27 -27
- package/src/textarea.tsx +18 -18
- package/src/theme-mode.ts +52 -52
- package/src/time-grid.tsx +163 -0
- package/src/time-picker.stories.tsx +62 -62
- package/src/time-picker.tsx +615 -615
- package/src/toggle-group.tsx +138 -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 +110 -110
package/src/editor/gutter.tsx
CHANGED
|
@@ -1,367 +1,443 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import * as React from "react"
|
|
4
|
-
import type { Editor } from "@tiptap/core"
|
|
5
|
-
import { NodeSelection } from "@tiptap/pm/state"
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
DropdownMenu,
|
|
9
|
-
DropdownMenuContent,
|
|
10
|
-
DropdownMenuItem,
|
|
11
|
-
DropdownMenuSeparator,
|
|
12
|
-
DropdownMenuShortcut,
|
|
13
|
-
DropdownMenuSub,
|
|
14
|
-
DropdownMenuSubContent,
|
|
15
|
-
DropdownMenuSubTrigger,
|
|
16
|
-
DropdownMenuTrigger,
|
|
17
|
-
} from "../dropdown-menu"
|
|
18
|
-
import { Icon } from "../icon"
|
|
19
|
-
import { cn } from "../utils"
|
|
20
|
-
import { deleteBlock, duplicateBlock, moveBlockTo, turnInto } from "./behaviour"
|
|
21
|
-
import { BlockList } from "./block-list"
|
|
22
|
-
import { CATALOGUE, applyItem, filterCatalogue, type CatalogueItem } from "./catalogue"
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
setDrag(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
<
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
<
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import type { Editor, JSONContent } from "@tiptap/core"
|
|
5
|
+
import { NodeSelection } from "@tiptap/pm/state"
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
DropdownMenu,
|
|
9
|
+
DropdownMenuContent,
|
|
10
|
+
DropdownMenuItem,
|
|
11
|
+
DropdownMenuSeparator,
|
|
12
|
+
DropdownMenuShortcut,
|
|
13
|
+
DropdownMenuSub,
|
|
14
|
+
DropdownMenuSubContent,
|
|
15
|
+
DropdownMenuSubTrigger,
|
|
16
|
+
DropdownMenuTrigger,
|
|
17
|
+
} from "../dropdown-menu"
|
|
18
|
+
import { Icon } from "../icon"
|
|
19
|
+
import { cn } from "../utils"
|
|
20
|
+
import { deleteBlock, duplicateBlock, moveBlockTo, turnInto } from "./behaviour"
|
|
21
|
+
import { BlockList } from "./block-list"
|
|
22
|
+
import { CATALOGUE, applyItem, filterCatalogue, type CatalogueItem } from "./catalogue"
|
|
23
|
+
import type { SavedBlock } from "./contract"
|
|
24
|
+
import { fromEditorContent } from "./convert"
|
|
25
|
+
import { EDITOR_LABELS, type EditorLabels } from "./labels"
|
|
26
|
+
import { SaveBlockDialog, type SaveBlockChoice } from "./save-block-dialog"
|
|
27
|
+
import type { NewSavedBlock } from "./services"
|
|
28
|
+
|
|
29
|
+
/** Top padding of a block before its first line, so the gutter sits on that line. */
|
|
30
|
+
function firstLineOffset(el: HTMLElement): number {
|
|
31
|
+
return parseFloat(getComputedStyle(el).paddingTop) || 0
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface Hovered {
|
|
35
|
+
pos: number
|
|
36
|
+
index: number
|
|
37
|
+
top: number
|
|
38
|
+
height: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The block gutter (E.8, EditorBlockChrome): hovering a block, or putting the
|
|
43
|
+
* caret in it, shows + and the drag handle in the margin at its first line.
|
|
44
|
+
* + opens the inserter for a new block below; the handle drags the block or,
|
|
45
|
+
* clicked, opens its menu. Agents never use it: they only suggest (E.4).
|
|
46
|
+
*/
|
|
47
|
+
function BlockGutter({
|
|
48
|
+
editor,
|
|
49
|
+
labels,
|
|
50
|
+
container,
|
|
51
|
+
menuAt,
|
|
52
|
+
onMenuAtChange,
|
|
53
|
+
blockUrl,
|
|
54
|
+
savedItems = [],
|
|
55
|
+
saveBlock,
|
|
56
|
+
canShareSavedBlocks = false,
|
|
57
|
+
}: {
|
|
58
|
+
editor: Editor
|
|
59
|
+
labels: EditorLabels
|
|
60
|
+
/** The positioned element around the editable content. */
|
|
61
|
+
container: React.RefObject<HTMLDivElement | null>
|
|
62
|
+
/** A block position whose menu should open (Esc then Enter). */
|
|
63
|
+
menuAt: number | null
|
|
64
|
+
onMenuAtChange: (pos: number | null) => void
|
|
65
|
+
/** The address "Copy link to block" copies. */
|
|
66
|
+
blockUrl: (blockId: string) => string
|
|
67
|
+
/** The Saved blocks group for the + inserter (E.21). */
|
|
68
|
+
savedItems?: CatalogueItem[]
|
|
69
|
+
/** Save as block; without it the block menu doesn't offer it. */
|
|
70
|
+
saveBlock?: (block: NewSavedBlock) => Promise<SavedBlock>
|
|
71
|
+
canShareSavedBlocks?: boolean
|
|
72
|
+
}) {
|
|
73
|
+
const [hovered, setHovered] = React.useState<Hovered | null>(null)
|
|
74
|
+
// Save as block: the first and last top-level block ids, found again by id when saving.
|
|
75
|
+
const [saving, setSaving] = React.useState<{ first: string; last: string; count: number } | null>(null)
|
|
76
|
+
const [inserter, setInserter] = React.useState<{ pos: number; query: string; active: number } | null>(null)
|
|
77
|
+
const [drag, setDrag] = React.useState<{ from: number; x: number; y: number; dropIndex: number; dropTop: number; text: string } | null>(null)
|
|
78
|
+
const pointerDown = React.useRef<{ x: number; y: number; pos: number } | null>(null)
|
|
79
|
+
|
|
80
|
+
const measure = React.useCallback(
|
|
81
|
+
(index: number): Hovered | null => {
|
|
82
|
+
const wrap = container.current
|
|
83
|
+
const el = editor.view.dom.children[index] as HTMLElement | undefined
|
|
84
|
+
if (!wrap || !el) return null
|
|
85
|
+
let pos = 0
|
|
86
|
+
for (let i = 0; i < index; i++) pos += editor.state.doc.child(i).nodeSize
|
|
87
|
+
const box = wrap.getBoundingClientRect()
|
|
88
|
+
const r = el.getBoundingClientRect()
|
|
89
|
+
return { pos, index, top: r.top - box.top + firstLineOffset(el), height: r.height }
|
|
90
|
+
},
|
|
91
|
+
[editor, container]
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
// Hovering a block shows its gutter.
|
|
95
|
+
React.useEffect(() => {
|
|
96
|
+
const wrap = container.current
|
|
97
|
+
if (!wrap) return
|
|
98
|
+
const onMove = (e: PointerEvent) => {
|
|
99
|
+
if (drag) return
|
|
100
|
+
const children = Array.from(editor.view.dom.children) as HTMLElement[]
|
|
101
|
+
const index = children.findIndex((el) => {
|
|
102
|
+
const r = el.getBoundingClientRect()
|
|
103
|
+
return e.clientY >= r.top - 2 && e.clientY <= r.bottom + 2
|
|
104
|
+
})
|
|
105
|
+
if (index >= 0) setHovered(measure(index))
|
|
106
|
+
}
|
|
107
|
+
wrap.addEventListener("pointermove", onMove)
|
|
108
|
+
return () => wrap.removeEventListener("pointermove", onMove)
|
|
109
|
+
}, [editor, container, measure, drag])
|
|
110
|
+
|
|
111
|
+
// So does putting the caret in it.
|
|
112
|
+
React.useEffect(() => {
|
|
113
|
+
const onSelection = () => {
|
|
114
|
+
if (!editor.isFocused) return
|
|
115
|
+
const { $from } = editor.state.selection
|
|
116
|
+
setHovered(measure($from.depth === 0 ? $from.index(0) : $from.index(0)))
|
|
117
|
+
}
|
|
118
|
+
editor.on("selectionUpdate", onSelection)
|
|
119
|
+
editor.on("update", onSelection)
|
|
120
|
+
return () => {
|
|
121
|
+
editor.off("selectionUpdate", onSelection)
|
|
122
|
+
editor.off("update", onSelection)
|
|
123
|
+
}
|
|
124
|
+
}, [editor, measure])
|
|
125
|
+
|
|
126
|
+
// Esc then Enter on a block opens its menu.
|
|
127
|
+
React.useEffect(() => {
|
|
128
|
+
if (menuAt === null) return
|
|
129
|
+
const index = editor.state.doc.resolve(menuAt).index(0)
|
|
130
|
+
setHovered(measure(index))
|
|
131
|
+
}, [menuAt, editor, measure])
|
|
132
|
+
|
|
133
|
+
if (!editor.isEditable) return null
|
|
134
|
+
|
|
135
|
+
const dropIndexAt = (clientY: number) => {
|
|
136
|
+
const wrap = container.current!
|
|
137
|
+
const box = wrap.getBoundingClientRect()
|
|
138
|
+
const children = Array.from(editor.view.dom.children) as HTMLElement[]
|
|
139
|
+
let index = children.length
|
|
140
|
+
let top = children.length ? children[children.length - 1].getBoundingClientRect().bottom - box.top : 0
|
|
141
|
+
for (let i = 0; i < children.length; i++) {
|
|
142
|
+
const r = children[i].getBoundingClientRect()
|
|
143
|
+
if (clientY < r.top + r.height / 2) {
|
|
144
|
+
index = i
|
|
145
|
+
top = r.top - box.top
|
|
146
|
+
break
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { index, top }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const startPointer = (e: React.PointerEvent) => {
|
|
153
|
+
if (!hovered || e.button !== 0) return
|
|
154
|
+
pointerDown.current = { x: e.clientX, y: e.clientY, pos: hovered.pos }
|
|
155
|
+
;(e.target as HTMLElement).setPointerCapture(e.pointerId)
|
|
156
|
+
}
|
|
157
|
+
const movePointer = (e: React.PointerEvent) => {
|
|
158
|
+
const down = pointerDown.current
|
|
159
|
+
if (!down) return
|
|
160
|
+
if (!drag && Math.hypot(e.clientX - down.x, e.clientY - down.y) < 4) return
|
|
161
|
+
const node = editor.state.doc.nodeAt(down.pos)
|
|
162
|
+
const box = container.current!.getBoundingClientRect()
|
|
163
|
+
const { index, top } = dropIndexAt(e.clientY)
|
|
164
|
+
const el = editor.view.nodeDOM(down.pos) as HTMLElement | null
|
|
165
|
+
el?.classList.add("is-dragging")
|
|
166
|
+
setDrag({ from: down.pos, x: e.clientX - box.left, y: e.clientY - box.top, dropIndex: index, dropTop: top, text: node?.textContent.slice(0, 80) ?? "" })
|
|
167
|
+
}
|
|
168
|
+
const endPointer = () => {
|
|
169
|
+
const down = pointerDown.current
|
|
170
|
+
pointerDown.current = null
|
|
171
|
+
if (!down) return
|
|
172
|
+
if (!drag) {
|
|
173
|
+
onMenuAtChange(down.pos)
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
;(editor.view.nodeDOM(down.pos) as HTMLElement | null)?.classList.remove("is-dragging")
|
|
177
|
+
const fromIndex = editor.state.doc.resolve(down.pos).index(0)
|
|
178
|
+
// Dropping below its own place: the block isn't counted once it's lifted out.
|
|
179
|
+
const target = drag.dropIndex > fromIndex ? drag.dropIndex - 1 : drag.dropIndex
|
|
180
|
+
setDrag(null)
|
|
181
|
+
if (target !== fromIndex) moveBlockTo(editor, down.pos, target)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const blockAt = (pos: number) => editor.state.doc.nodeAt(pos)
|
|
185
|
+
const select = (pos: number) => editor.chain().focus().command(({ tr }) => {
|
|
186
|
+
tr.setSelection(NodeSelection.create(tr.doc, pos))
|
|
187
|
+
return true
|
|
188
|
+
}).run()
|
|
189
|
+
const inside = (pos: number) => editor.chain().focus().setTextSelection(pos + 1).run()
|
|
190
|
+
|
|
191
|
+
const menuOpen = menuAt !== null
|
|
192
|
+
const menuBlock = menuOpen ? blockAt(menuAt) : null
|
|
193
|
+
const inserterItems: CatalogueItem[] = inserter ? filterCatalogue(inserter.query, labels, EDITOR_LABELS, savedItems) : []
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The top-level blocks Save as block takes: those the selection spans when
|
|
197
|
+
* it covers this block and others (E.21: select several, then save), else
|
|
198
|
+
* this block alone.
|
|
199
|
+
*/
|
|
200
|
+
const blocksToSave = (pos: number) => {
|
|
201
|
+
const { doc, selection } = editor.state
|
|
202
|
+
const at = doc.resolve(pos).index(0)
|
|
203
|
+
const from = doc.resolve(selection.from).index(0)
|
|
204
|
+
const to = doc.resolve(Math.max(selection.from, selection.to - 1)).index(0)
|
|
205
|
+
const [a, b] = to > from && at >= from && at <= to ? [from, to] : [at, at]
|
|
206
|
+
return { first: String(doc.child(a).attrs.id ?? ""), last: String(doc.child(b).attrs.id ?? ""), count: b - a + 1 }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Where the blocks being saved are now, by their ids: the document may have changed meanwhile. */
|
|
210
|
+
const savingRange = () => {
|
|
211
|
+
if (!saving) return null
|
|
212
|
+
let from = -1
|
|
213
|
+
let to = -1
|
|
214
|
+
editor.state.doc.forEach((node, offset) => {
|
|
215
|
+
if (node.attrs.id === saving.first) from = offset
|
|
216
|
+
if (node.attrs.id === saving.last) to = offset + node.nodeSize
|
|
217
|
+
})
|
|
218
|
+
return from >= 0 && to > from ? { from, to } : null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const saveSelection = async (choice: SaveBlockChoice) => {
|
|
222
|
+
const range = savingRange()
|
|
223
|
+
if (!range || !saveBlock) throw new Error("The blocks have changed")
|
|
224
|
+
const nodes: JSONContent[] = []
|
|
225
|
+
editor.state.doc.nodesBetween(range.from, range.to, (node, pos) => {
|
|
226
|
+
if (pos >= range.from && pos < range.to) nodes.push(node.toJSON() as JSONContent)
|
|
227
|
+
return false
|
|
228
|
+
})
|
|
229
|
+
const saved = await saveBlock({ ...choice, blocks: fromEditorContent({ type: "doc", content: nodes }), perDocumentParts: [] })
|
|
230
|
+
if (choice.mode !== "synced") return
|
|
231
|
+
// Synced: the blocks become the saved block, drawn from the library from now on.
|
|
232
|
+
const now = savingRange()
|
|
233
|
+
if (!now) return
|
|
234
|
+
editor
|
|
235
|
+
.chain()
|
|
236
|
+
.focus()
|
|
237
|
+
.insertContentAt(now, { type: "savedBlock", attrs: { savedBlockId: saved.id, version: saved.version, overrides: {} } })
|
|
238
|
+
.run()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<>
|
|
243
|
+
{hovered && !drag && (
|
|
244
|
+
<div
|
|
245
|
+
data-slot="block-gutter"
|
|
246
|
+
className="absolute end-full z-10 flex pe-1.5"
|
|
247
|
+
style={{ top: hovered.top }}
|
|
248
|
+
onPointerDown={(e) => e.stopPropagation()}
|
|
249
|
+
>
|
|
250
|
+
<button
|
|
251
|
+
type="button"
|
|
252
|
+
aria-label={labels.addBelow}
|
|
253
|
+
className={cn(
|
|
254
|
+
"grid size-6 place-items-center rounded-[6px] text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring",
|
|
255
|
+
inserter && "bg-muted text-foreground"
|
|
256
|
+
)}
|
|
257
|
+
onMouseDown={(e) => e.preventDefault()}
|
|
258
|
+
onClick={() => setInserter({ pos: hovered.pos, query: "", active: 0 })}
|
|
259
|
+
>
|
|
260
|
+
<Icon name="plus" />
|
|
261
|
+
</button>
|
|
262
|
+
<DropdownMenu
|
|
263
|
+
modal={false}
|
|
264
|
+
open={menuOpen && menuAt === hovered.pos}
|
|
265
|
+
onOpenChange={(open) => onMenuAtChange(open ? hovered.pos : null)}
|
|
266
|
+
>
|
|
267
|
+
<DropdownMenuTrigger asChild>
|
|
268
|
+
<button
|
|
269
|
+
type="button"
|
|
270
|
+
aria-label={labels.dragHandle}
|
|
271
|
+
className="grid h-6 w-[18px] cursor-grab touch-none place-items-center rounded-[6px] text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"
|
|
272
|
+
onPointerDown={(e) => {
|
|
273
|
+
e.preventDefault()
|
|
274
|
+
startPointer(e)
|
|
275
|
+
}}
|
|
276
|
+
onPointerMove={movePointer}
|
|
277
|
+
onPointerUp={endPointer}
|
|
278
|
+
onKeyDown={(e) => {
|
|
279
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
280
|
+
e.preventDefault()
|
|
281
|
+
onMenuAtChange(hovered.pos)
|
|
282
|
+
}
|
|
283
|
+
}}
|
|
284
|
+
>
|
|
285
|
+
<Icon name="drag" />
|
|
286
|
+
</button>
|
|
287
|
+
</DropdownMenuTrigger>
|
|
288
|
+
{menuBlock && menuAt !== null && (
|
|
289
|
+
<DropdownMenuContent
|
|
290
|
+
align="start"
|
|
291
|
+
aria-label={labels.blockMenu}
|
|
292
|
+
onCloseAutoFocus={(e) => {
|
|
293
|
+
e.preventDefault()
|
|
294
|
+
editor.commands.focus()
|
|
295
|
+
}}
|
|
296
|
+
>
|
|
297
|
+
{menuBlock.isTextblock && (
|
|
298
|
+
<DropdownMenuSub>
|
|
299
|
+
<DropdownMenuSubTrigger>
|
|
300
|
+
<Icon name="sync" /> {labels.turnInto}
|
|
301
|
+
</DropdownMenuSubTrigger>
|
|
302
|
+
<DropdownMenuSubContent>
|
|
303
|
+
{CATALOGUE.filter((i) => i.textBlock).map((item) => (
|
|
304
|
+
<DropdownMenuItem
|
|
305
|
+
key={item.id}
|
|
306
|
+
onSelect={() => {
|
|
307
|
+
inside(menuAt)
|
|
308
|
+
turnInto(editor, item.node.type, item.node.attrs)
|
|
309
|
+
}}
|
|
310
|
+
>
|
|
311
|
+
<Icon name={item.icon} /> {labels[item.label]}
|
|
312
|
+
</DropdownMenuItem>
|
|
313
|
+
))}
|
|
314
|
+
</DropdownMenuSubContent>
|
|
315
|
+
</DropdownMenuSub>
|
|
316
|
+
)}
|
|
317
|
+
<DropdownMenuItem onSelect={() => duplicateBlock(editor, menuAt)}>
|
|
318
|
+
<Icon name="copy" /> {labels.duplicate}
|
|
319
|
+
<DropdownMenuShortcut>⌘D</DropdownMenuShortcut>
|
|
320
|
+
</DropdownMenuItem>
|
|
321
|
+
<DropdownMenuItem
|
|
322
|
+
onSelect={() => {
|
|
323
|
+
const id = menuBlock.attrs.id as string | null
|
|
324
|
+
if (id) void navigator.clipboard?.writeText(blockUrl(id))
|
|
325
|
+
}}
|
|
326
|
+
>
|
|
327
|
+
<Icon name="link" /> {labels.copyLinkToBlock}
|
|
328
|
+
</DropdownMenuItem>
|
|
329
|
+
{saveBlock && menuBlock.type.name !== "savedBlock" && (
|
|
330
|
+
<DropdownMenuItem onSelect={() => setSaving(blocksToSave(menuAt))}>
|
|
331
|
+
<Icon name="template" /> {labels.saveAsBlock}
|
|
332
|
+
</DropdownMenuItem>
|
|
333
|
+
)}
|
|
334
|
+
<DropdownMenuSeparator />
|
|
335
|
+
<DropdownMenuItem
|
|
336
|
+
disabled={editor.state.doc.resolve(menuAt).index(0) === 0}
|
|
337
|
+
onSelect={() => moveBlockTo(editor, menuAt, editor.state.doc.resolve(menuAt).index(0) - 1)}
|
|
338
|
+
>
|
|
339
|
+
<Icon name="arrow-up" /> {labels.moveUp}
|
|
340
|
+
<DropdownMenuShortcut>⌥⇧↑</DropdownMenuShortcut>
|
|
341
|
+
</DropdownMenuItem>
|
|
342
|
+
<DropdownMenuItem
|
|
343
|
+
disabled={editor.state.doc.resolve(menuAt).index(0) >= editor.state.doc.childCount - 1}
|
|
344
|
+
onSelect={() => moveBlockTo(editor, menuAt, editor.state.doc.resolve(menuAt).index(0) + 1)}
|
|
345
|
+
>
|
|
346
|
+
<Icon name="arrow-down" /> {labels.moveDown}
|
|
347
|
+
<DropdownMenuShortcut>⌥⇧↓</DropdownMenuShortcut>
|
|
348
|
+
</DropdownMenuItem>
|
|
349
|
+
<DropdownMenuSeparator />
|
|
350
|
+
<DropdownMenuItem variant="destructive" onSelect={() => deleteBlock(editor, menuAt)}>
|
|
351
|
+
<Icon name="delete" /> {labels.delete}
|
|
352
|
+
<DropdownMenuShortcut>Del</DropdownMenuShortcut>
|
|
353
|
+
</DropdownMenuItem>
|
|
354
|
+
</DropdownMenuContent>
|
|
355
|
+
)}
|
|
356
|
+
</DropdownMenu>
|
|
357
|
+
</div>
|
|
358
|
+
)}
|
|
359
|
+
|
|
360
|
+
{inserter && hovered && (
|
|
361
|
+
<div className="absolute start-0 z-30" style={{ top: hovered.top + 28 }}>
|
|
362
|
+
<BlockList
|
|
363
|
+
id="wy-inserter"
|
|
364
|
+
items={inserterItems}
|
|
365
|
+
active={Math.min(inserter.active, Math.max(0, inserterItems.length - 1))}
|
|
366
|
+
onActive={(active) => setInserter({ ...inserter, active })}
|
|
367
|
+
onPick={(item) => {
|
|
368
|
+
const node = blockAt(inserter.pos)
|
|
369
|
+
setInserter(null)
|
|
370
|
+
if (node) applyItem(editor, item, { after: inserter.pos + node.nodeSize })
|
|
371
|
+
}}
|
|
372
|
+
labels={labels}
|
|
373
|
+
footer={
|
|
374
|
+
<>
|
|
375
|
+
<span>{labels.slashMove}</span>
|
|
376
|
+
<span>{labels.slashInsert}</span>
|
|
377
|
+
<span>{labels.slashClose}</span>
|
|
378
|
+
</>
|
|
379
|
+
}
|
|
380
|
+
search={
|
|
381
|
+
<div className="-mx-1 -mt-1 mb-1 flex h-9 items-center gap-2 border-b border-border px-2.5 text-muted-foreground">
|
|
382
|
+
<Icon name="search" size={14} />
|
|
383
|
+
<input
|
|
384
|
+
autoFocus
|
|
385
|
+
aria-label={labels.searchBlocks}
|
|
386
|
+
aria-controls="wy-inserter"
|
|
387
|
+
placeholder={labels.searchBlocks}
|
|
388
|
+
value={inserter.query}
|
|
389
|
+
onChange={(e) => setInserter({ ...inserter, query: e.target.value, active: 0 })}
|
|
390
|
+
onBlur={() => setInserter(null)}
|
|
391
|
+
onKeyDown={(e) => {
|
|
392
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
393
|
+
e.preventDefault()
|
|
394
|
+
const n = inserterItems.length || 1
|
|
395
|
+
setInserter({ ...inserter, active: (inserter.active + (e.key === "ArrowDown" ? 1 : n - 1)) % n })
|
|
396
|
+
} else if (e.key === "Enter") {
|
|
397
|
+
e.preventDefault()
|
|
398
|
+
const item = inserterItems[inserter.active]
|
|
399
|
+
const node = blockAt(inserter.pos)
|
|
400
|
+
setInserter(null)
|
|
401
|
+
if (item && node) applyItem(editor, item, { after: inserter.pos + node.nodeSize })
|
|
402
|
+
} else if (e.key === "Escape") {
|
|
403
|
+
e.preventDefault()
|
|
404
|
+
setInserter(null)
|
|
405
|
+
select(inserter.pos)
|
|
406
|
+
}
|
|
407
|
+
}}
|
|
408
|
+
className="h-full min-w-0 flex-1 bg-transparent text-sm text-foreground outline-none placeholder:text-muted-foreground"
|
|
409
|
+
/>
|
|
410
|
+
</div>
|
|
411
|
+
}
|
|
412
|
+
/>
|
|
413
|
+
</div>
|
|
414
|
+
)}
|
|
415
|
+
|
|
416
|
+
{saveBlock && (
|
|
417
|
+
<SaveBlockDialog
|
|
418
|
+
open={saving !== null}
|
|
419
|
+
onOpenChange={(open) => !open && setSaving(null)}
|
|
420
|
+
blockCount={saving?.count ?? 1}
|
|
421
|
+
canShare={canShareSavedBlocks}
|
|
422
|
+
onSave={saveSelection}
|
|
423
|
+
labels={labels}
|
|
424
|
+
/>
|
|
425
|
+
)}
|
|
426
|
+
|
|
427
|
+
{drag && (
|
|
428
|
+
<>
|
|
429
|
+
<div aria-hidden="true" className="pointer-events-none absolute inset-x-0 z-20 h-0.5 rounded-[1px] bg-primary" style={{ top: drag.dropTop - 1 }} />
|
|
430
|
+
<div
|
|
431
|
+
aria-hidden="true"
|
|
432
|
+
className="pointer-events-none absolute z-30 max-w-[420px] -rotate-1 truncate rounded-control border border-border bg-card px-2.5 py-1.5 text-base shadow-float"
|
|
433
|
+
style={{ top: drag.y + 8, left: drag.x + 12 }}
|
|
434
|
+
>
|
|
435
|
+
{drag.text || "…"}
|
|
436
|
+
</div>
|
|
437
|
+
</>
|
|
438
|
+
)}
|
|
439
|
+
</>
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export { BlockGutter }
|