svelora 3.0.1 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Accordion/Accordion.svelte +66 -97
- package/dist/Alert/Alert.svelte +39 -64
- package/dist/Alert/Alert.svelte.d.ts +1 -1
- package/dist/Avatar/Avatar.svelte +35 -75
- package/dist/AvatarGroup/AvatarGroup.svelte +38 -55
- package/dist/Badge/Badge.svelte +28 -50
- package/dist/Banner/Banner.svelte +46 -41
- package/dist/Banner/Banner.svelte.d.ts +1 -1
- package/dist/Breadcrumb/Breadcrumb.svelte +32 -26
- package/dist/Button/Button.svelte +70 -138
- package/dist/Calendar/Calendar.svelte +94 -157
- package/dist/Calendar/Calendar.svelte.d.ts +1 -1
- package/dist/Card/Card.svelte +18 -31
- package/dist/Carousel/Carousel.svelte +118 -173
- package/dist/Checkbox/Checkbox.svelte +52 -97
- package/dist/CheckboxGroup/CheckboxGroup.svelte +62 -107
- package/dist/CheckboxGroup/CheckboxGroup.svelte.d.ts +1 -1
- package/dist/Chip/Chip.svelte +22 -34
- package/dist/CodeBlock/CodeBlock.svelte +42 -59
- package/dist/Collapsible/Collapsible.svelte +22 -38
- package/dist/Collapsible/Collapsible.svelte.d.ts +1 -1
- package/dist/Collapsible/CollapsibleTestWrapper.svelte +2 -5
- package/dist/Collapsible/CollapsibleTestWrapper.svelte.d.ts +1 -1
- package/dist/Command/Command.svelte +40 -77
- package/dist/Command/Command.svelte.d.ts +1 -1
- package/dist/Command/CommandTestWrapper.svelte +2 -10
- package/dist/Command/CommandTestWrapper.svelte.d.ts +1 -1
- package/dist/Container/Container.svelte +11 -14
- package/dist/ContextMenu/ContextMenu.svelte +51 -114
- package/dist/ContextMenu/ContextMenu.svelte.d.ts +1 -1
- package/dist/Drawer/Drawer.svelte +72 -110
- package/dist/Drawer/DrawerTriggerTestWrapper.svelte +1 -2
- package/dist/DropdownMenu/DropdownMenu.svelte +63 -124
- package/dist/DropdownMenu/DropdownMenu.svelte.d.ts +1 -1
- package/dist/DropdownMenu/DropdownMenuTriggerTestWrapper.svelte +2 -5
- package/dist/Editor/Editor.svelte +441 -576
- package/dist/Editor/Editor.svelte.d.ts +1 -1
- package/dist/Editor/EditorUrlPrompt.svelte +40 -53
- package/dist/Editor/SlashPopup.svelte +12 -24
- package/dist/Empty/Empty.svelte +32 -63
- package/dist/FieldGroup/FieldGroup.svelte +23 -38
- package/dist/FileUpload/FileUpload.svelte +242 -320
- package/dist/FileUpload/FileUpload.svelte.d.ts +1 -1
- package/dist/Fonts/Fonts.svelte +15 -37
- package/dist/Form/Form.svelte +112 -170
- package/dist/FormField/FormField.svelte +102 -135
- package/dist/Icon/Icon.svelte +7 -32
- package/dist/Input/Input.svelte +71 -141
- package/dist/Input/Input.svelte.d.ts +2 -2
- package/dist/Kbd/Kbd.svelte +18 -34
- package/dist/Link/Link.svelte +129 -196
- package/dist/LocaleButton/LocaleButton.svelte +165 -0
- package/dist/LocaleButton/LocaleButton.svelte.d.ts +5 -0
- package/dist/LocaleButton/index.d.ts +2 -0
- package/dist/LocaleButton/index.js +1 -0
- package/dist/LocaleButton/locale-button.types.d.ts +182 -0
- package/dist/LocaleButton/locale-button.types.js +1 -0
- package/dist/LocaleButton/locale-button.variants.d.ts +61 -0
- package/dist/LocaleButton/locale-button.variants.js +34 -0
- package/dist/Modal/Modal.svelte +52 -106
- package/dist/Modal/ModalTriggerTestWrapper.svelte +1 -2
- package/dist/Pagination/Pagination.svelte +48 -92
- package/dist/Pagination/pagination.variants.d.ts +1 -1
- package/dist/PinInput/PinInput.svelte +57 -111
- package/dist/PinInput/PinInput.svelte.d.ts +1 -1
- package/dist/Popover/Popover.svelte +28 -61
- package/dist/Popover/Popover.svelte.d.ts +1 -1
- package/dist/Progress/Progress.svelte +75 -94
- package/dist/RadioGroup/RadioGroup.svelte +54 -99
- package/dist/RadioGroup/RadioGroup.svelte.d.ts +1 -1
- package/dist/Select/Select.svelte +112 -269
- package/dist/Select/Select.svelte.d.ts +1 -1
- package/dist/SelectMenu/SelectMenu.svelte +211 -409
- package/dist/SelectMenu/SelectMenu.svelte.d.ts +1 -1
- package/dist/SelectMenu/SelectMenuFormFieldTestWrapper.svelte +3 -6
- package/dist/Separator/Separator.svelte +29 -44
- package/dist/Skeleton/Skeleton.svelte +11 -23
- package/dist/Slideover/Slideover.svelte +52 -106
- package/dist/Slideover/SlideoverTriggerTestWrapper.svelte +1 -2
- package/dist/Slider/Slider.svelte +48 -84
- package/dist/Slider/Slider.svelte.d.ts +1 -1
- package/dist/Stepper/Stepper.svelte +139 -132
- package/dist/Stepper/Stepper.svelte.d.ts +1 -1
- package/dist/Switch/Switch.svelte +62 -98
- package/dist/Table/Table.svelte +232 -283
- package/dist/Table/table.variants.d.ts +1 -1
- package/dist/Tabs/Tabs.svelte +96 -129
- package/dist/Tabs/Tabs.svelte.d.ts +1 -1
- package/dist/Textarea/Textarea.svelte +90 -173
- package/dist/Textarea/Textarea.svelte.d.ts +1 -1
- package/dist/ThemeModeButton/ThemeModeButton.svelte +16 -38
- package/dist/Timeline/Timeline.svelte +75 -54
- package/dist/Toast/Toaster.svelte +8 -25
- package/dist/Tooltip/Tooltip.svelte +34 -66
- package/dist/Tooltip/Tooltip.svelte.d.ts +1 -1
- package/dist/Tooltip/TooltipTestWrapper.svelte +2 -5
- package/dist/User/User.svelte +33 -49
- package/dist/docs/navigation.js +6 -0
- package/dist/hooks/HookContextProbe.svelte +2 -4
- package/dist/hooks/HookContextProvider.svelte +8 -6
- package/dist/hooks/HookEmitProbe.svelte +8 -11
- package/dist/i18n.d.ts +2 -0
- package/dist/i18n.js +19 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mcp/svelora-docs.data.json +4 -2
- package/package.json +16 -8
|
@@ -1,581 +1,446 @@
|
|
|
1
|
-
<script lang="ts" module>
|
|
2
|
-
export type Props = EditorProps;
|
|
1
|
+
<script lang="ts" module>export {};
|
|
3
2
|
</script>
|
|
4
3
|
|
|
5
|
-
<script lang="ts">
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
urlPrompt = {
|
|
446
|
-
open: true,
|
|
447
|
-
title: opts.title,
|
|
448
|
-
description: opts.description,
|
|
449
|
-
placeholder: opts.placeholder ?? 'https://',
|
|
450
|
-
initialValue: opts.initialValue ?? '',
|
|
451
|
-
confirmLabel: opts.confirmLabel ?? 'Insert',
|
|
452
|
-
schema: opts.schema,
|
|
453
|
-
onConfirm: opts.onConfirm,
|
|
454
|
-
onCancel: opts.onCancel
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
let fileInput: HTMLInputElement | null = $state(null)
|
|
459
|
-
|
|
460
|
-
async function handleFileSelected(event: Event): Promise<void> {
|
|
461
|
-
if (!editor) return
|
|
462
|
-
const input = event.currentTarget as HTMLInputElement
|
|
463
|
-
const file = input.files?.[0]
|
|
464
|
-
input.value = ''
|
|
465
|
-
if (!file) return
|
|
466
|
-
if (!onImageUpload) return
|
|
467
|
-
try {
|
|
468
|
-
const url = await onImageUpload(file)
|
|
469
|
-
if (!isSafeImageSrc(url)) {
|
|
470
|
-
console.warn(
|
|
471
|
-
`[svelora] Blocked unsafe image src returned by onImageUpload: ${url}`
|
|
472
|
-
)
|
|
473
|
-
return
|
|
474
|
-
}
|
|
475
|
-
editor.chain().focus().setImage({ src: url }).run()
|
|
476
|
-
} catch (err) {
|
|
477
|
-
if (onImageUploadError) {
|
|
478
|
-
onImageUploadError(err)
|
|
479
|
-
} else {
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function openImagePicker(): void {
|
|
485
|
-
if (!editor) return
|
|
486
|
-
if (!onImageUpload) {
|
|
487
|
-
openUrlPrompt({
|
|
488
|
-
title: 'Image URL',
|
|
489
|
-
placeholder: 'https://example.com/image.png',
|
|
490
|
-
schema: httpUrlSchema,
|
|
491
|
-
onConfirm: (url) => {
|
|
492
|
-
editor?.chain().focus().setImage({ src: url }).run()
|
|
493
|
-
}
|
|
494
|
-
})
|
|
495
|
-
return
|
|
496
|
-
}
|
|
497
|
-
fileInput?.click()
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
function openYoutubePrompt(): void {
|
|
501
|
-
if (!editor) return
|
|
502
|
-
openUrlPrompt({
|
|
503
|
-
title: 'Embed YouTube video',
|
|
504
|
-
description: 'Paste the share link or full URL.',
|
|
505
|
-
placeholder: 'https://youtu.be/...',
|
|
506
|
-
confirmLabel: 'Embed',
|
|
507
|
-
schema: youtubeUrlSchema,
|
|
508
|
-
onConfirm: (url) => {
|
|
509
|
-
editor?.commands.setYoutubeVideo({ src: url })
|
|
510
|
-
}
|
|
511
|
-
})
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
function openLinkPrompt(): void {
|
|
515
|
-
if (!editor) return
|
|
516
|
-
const previous = (editor.getAttributes('link').href as string | undefined) ?? ''
|
|
517
|
-
openUrlPrompt({
|
|
518
|
-
title: 'Insert link',
|
|
519
|
-
placeholder: 'https://',
|
|
520
|
-
initialValue: previous,
|
|
521
|
-
schema: httpUrlSchema,
|
|
522
|
-
onConfirm: (url) => {
|
|
523
|
-
editor?.chain().focus().extendMarkRange('link').setLink({ href: url }).run()
|
|
524
|
-
}
|
|
525
|
-
})
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
let tableMenuOpen = $state(false)
|
|
529
|
-
let tablePickerRows = $state(0)
|
|
530
|
-
let tablePickerCols = $state(0)
|
|
531
|
-
let tablePickerEl: HTMLDivElement | null = $state(null)
|
|
532
|
-
let tableButtonEl: HTMLButtonElement | null = $state(null)
|
|
533
|
-
const TABLE_MAX_ROWS = 8
|
|
534
|
-
const TABLE_MAX_COLS = 8
|
|
535
|
-
|
|
536
|
-
function insertTable(rows: number, cols: number): void {
|
|
537
|
-
if (!editor) return
|
|
538
|
-
editor.chain().focus().insertTable({ rows, cols, withHeaderRow: true }).run()
|
|
539
|
-
tableMenuOpen = false
|
|
540
|
-
tablePickerRows = 0
|
|
541
|
-
tablePickerCols = 0
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
$effect(() => {
|
|
545
|
-
if (!tableMenuOpen) return
|
|
546
|
-
function onDocPointerDown(event: PointerEvent): void {
|
|
547
|
-
const target = event.target as Node | null
|
|
548
|
-
if (!target) return
|
|
549
|
-
if (tablePickerEl?.contains(target)) return
|
|
550
|
-
if (tableButtonEl?.contains(target)) return
|
|
551
|
-
tableMenuOpen = false
|
|
552
|
-
tablePickerRows = 0
|
|
553
|
-
tablePickerCols = 0
|
|
554
|
-
}
|
|
555
|
-
document.addEventListener('pointerdown', onDocPointerDown, true)
|
|
556
|
-
return () => document.removeEventListener('pointerdown', onDocPointerDown, true)
|
|
557
|
-
})
|
|
558
|
-
|
|
559
|
-
function runAction(def: ToolbarActionDef, id: ToolbarAction): void {
|
|
560
|
-
if (!editor) return
|
|
561
|
-
if (id === 'image') {
|
|
562
|
-
openImagePicker()
|
|
563
|
-
return
|
|
564
|
-
}
|
|
565
|
-
if (id === 'youtube') {
|
|
566
|
-
openYoutubePrompt()
|
|
567
|
-
return
|
|
568
|
-
}
|
|
569
|
-
if (id === 'link') {
|
|
570
|
-
openLinkPrompt()
|
|
571
|
-
return
|
|
572
|
-
}
|
|
573
|
-
if (id === 'table') {
|
|
574
|
-
tableMenuOpen = !tableMenuOpen
|
|
575
|
-
return
|
|
576
|
-
}
|
|
577
|
-
def.run(editor)
|
|
578
|
-
}
|
|
4
|
+
<script lang="ts">import { Editor } from "@tiptap/core";
|
|
5
|
+
import BubbleMenuExt from "@tiptap/extension-bubble-menu";
|
|
6
|
+
import { untrack } from "svelte";
|
|
7
|
+
import { getComponentConfig } from "../config.js";
|
|
8
|
+
import { useFormField, useFormFieldEmit } from "../hooks/useFormField.svelte.js";
|
|
9
|
+
import Icon from "../Icon/Icon.svelte";
|
|
10
|
+
import Tooltip from "../Tooltip/Tooltip.svelte";
|
|
11
|
+
import EditorUrlPrompt from "./EditorUrlPrompt.svelte";
|
|
12
|
+
import { buildExtensions } from "./editor.extensions.js";
|
|
13
|
+
import { httpUrlSchema, isSafeImageSrc, youtubeUrlSchema } from "./editor.schemas.js";
|
|
14
|
+
import { buildDefaultSlashCommands } from "./editor.slash.svelte.js";
|
|
15
|
+
import { buildMentionSuggestion } from "./editor.suggestion.js";
|
|
16
|
+
import { DEFAULT_TOOLBAR, TOOLBAR_ACTIONS } from "./editor.toolbar.js";
|
|
17
|
+
import { editorDefaults, editorVariants } from "./editor.variants.js";
|
|
18
|
+
const config = getComponentConfig("editor", editorDefaults);
|
|
19
|
+
let { ref = $bindable(null), api = $bindable(), value = $bindable(), output = "html", placeholder, id, name, onValueChange, onFocus, onBlur, readonly = false, disabled = false, autofocus = false, maxLength, showCount = false, toolbar = true, stickyToolbar = false, bubbleMenu = false, headingLevels = [
|
|
20
|
+
1,
|
|
21
|
+
2,
|
|
22
|
+
3
|
|
23
|
+
], autolink = true, linkOpenInNewTab = true, markdownAllowHtml = false, image = false, onImageUpload, onImageUploadError, tables = false, onMention, mentionTrigger = "@", slash = false, slashCommands, slashTrigger = "/", youtube = false, dragHandle = false, extensions: extraExtensions, extensionsOverride, size = config.defaultVariants.size ?? "md", color = config.defaultVariants.color ?? "primary", class: className, ui, toolbarSlot, bubbleMenuSlot, header, footer, ...restProps } = $props();
|
|
24
|
+
const formFieldContext = useFormField();
|
|
25
|
+
const emit = useFormFieldEmit();
|
|
26
|
+
const resolvedOutput = untrack(() => output);
|
|
27
|
+
function getMarkdownStorage(ed) {
|
|
28
|
+
return ed.storage.markdown;
|
|
29
|
+
}
|
|
30
|
+
const hasError = $derived(formFieldContext?.error !== undefined && formFieldContext?.error !== false);
|
|
31
|
+
const resolvedColor = $derived(hasError ? "error" : color);
|
|
32
|
+
const resolvedId = $derived(id ?? formFieldContext?.ariaId);
|
|
33
|
+
const resolvedName = $derived(name ?? formFieldContext?.name);
|
|
34
|
+
const ariaDescribedBy = $derived(!formFieldContext ? undefined : hasError ? `${formFieldContext.ariaId}-error` : `${formFieldContext.ariaId}-description ${formFieldContext.ariaId}-help`);
|
|
35
|
+
let contentElement = $state(null);
|
|
36
|
+
let bubbleElement = $state(null);
|
|
37
|
+
let editor = $state(null);
|
|
38
|
+
let editorState = $state({
|
|
39
|
+
active: {},
|
|
40
|
+
can: {
|
|
41
|
+
undo: false,
|
|
42
|
+
redo: false
|
|
43
|
+
},
|
|
44
|
+
charCount: 0,
|
|
45
|
+
wordCount: 0,
|
|
46
|
+
isEmpty: true,
|
|
47
|
+
isFocused: false
|
|
48
|
+
});
|
|
49
|
+
function syncState(ed) {
|
|
50
|
+
const cc = ed.storage.characterCount;
|
|
51
|
+
const can = ed.can();
|
|
52
|
+
editorState = {
|
|
53
|
+
active: {
|
|
54
|
+
bold: ed.isActive("bold"),
|
|
55
|
+
italic: ed.isActive("italic"),
|
|
56
|
+
underline: ed.isActive("underline"),
|
|
57
|
+
strike: ed.isActive("strike"),
|
|
58
|
+
code: ed.isActive("code"),
|
|
59
|
+
h1: ed.isActive("heading", { level: 1 }),
|
|
60
|
+
h2: ed.isActive("heading", { level: 2 }),
|
|
61
|
+
h3: ed.isActive("heading", { level: 3 }),
|
|
62
|
+
paragraph: ed.isActive("paragraph"),
|
|
63
|
+
bulletList: ed.isActive("bulletList"),
|
|
64
|
+
orderedList: ed.isActive("orderedList"),
|
|
65
|
+
blockquote: ed.isActive("blockquote"),
|
|
66
|
+
codeBlock: ed.isActive("codeBlock"),
|
|
67
|
+
link: ed.isActive("link"),
|
|
68
|
+
alignLeft: ed.isActive({ textAlign: "left" }),
|
|
69
|
+
alignCenter: ed.isActive({ textAlign: "center" }),
|
|
70
|
+
alignRight: ed.isActive({ textAlign: "right" }),
|
|
71
|
+
alignJustify: ed.isActive({ textAlign: "justify" }),
|
|
72
|
+
image: ed.isActive("image"),
|
|
73
|
+
table: ed.isActive("table"),
|
|
74
|
+
youtube: ed.isActive("youtube")
|
|
75
|
+
},
|
|
76
|
+
can: {
|
|
77
|
+
undo: can.undo(),
|
|
78
|
+
redo: can.redo()
|
|
79
|
+
},
|
|
80
|
+
charCount: typeof cc?.characters === "function" ? cc.characters() : 0,
|
|
81
|
+
wordCount: typeof cc?.words === "function" ? cc.words() : 0,
|
|
82
|
+
isEmpty: ed.isEmpty,
|
|
83
|
+
isFocused: ed.isFocused
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function serialize(ed) {
|
|
87
|
+
if (resolvedOutput === "json") return ed.getJSON();
|
|
88
|
+
if (resolvedOutput === "markdown") {
|
|
89
|
+
const md = getMarkdownStorage(ed);
|
|
90
|
+
if (md && typeof md.getMarkdown === "function") {
|
|
91
|
+
return md.getMarkdown();
|
|
92
|
+
}
|
|
93
|
+
return ed.getHTML();
|
|
94
|
+
}
|
|
95
|
+
return ed.getHTML();
|
|
96
|
+
}
|
|
97
|
+
function isContentEqual(a, b) {
|
|
98
|
+
if (a === b) return true;
|
|
99
|
+
if (typeof a !== typeof b) return false;
|
|
100
|
+
if (typeof a === "string" && typeof b === "string") return a === b;
|
|
101
|
+
try {
|
|
102
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function resolveSlashCommands() {
|
|
108
|
+
if (slashCommands) return slashCommands;
|
|
109
|
+
if (!slash) return undefined;
|
|
110
|
+
return buildDefaultSlashCommands({
|
|
111
|
+
image,
|
|
112
|
+
tables,
|
|
113
|
+
youtube,
|
|
114
|
+
promptUrl: (opts) => new Promise((resolve) => {
|
|
115
|
+
let done = false;
|
|
116
|
+
const settle = (value) => {
|
|
117
|
+
if (done) return;
|
|
118
|
+
done = true;
|
|
119
|
+
resolve(value);
|
|
120
|
+
};
|
|
121
|
+
openUrlPrompt({
|
|
122
|
+
...opts,
|
|
123
|
+
onConfirm: (value) => settle(value),
|
|
124
|
+
onCancel: () => settle(null)
|
|
125
|
+
});
|
|
126
|
+
})
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function resolveExtensions() {
|
|
130
|
+
if (extensionsOverride) return extensionsOverride;
|
|
131
|
+
return buildExtensions({
|
|
132
|
+
headingLevels,
|
|
133
|
+
placeholder,
|
|
134
|
+
autolink,
|
|
135
|
+
linkOpenInNewTab,
|
|
136
|
+
maxLength,
|
|
137
|
+
image,
|
|
138
|
+
tables,
|
|
139
|
+
youtube,
|
|
140
|
+
dragHandle,
|
|
141
|
+
markdown: resolvedOutput === "markdown",
|
|
142
|
+
markdownAllowHtml,
|
|
143
|
+
mentionTrigger,
|
|
144
|
+
mentionSuggestion: onMention ? buildMentionSuggestion({ onQuery: onMention }) : undefined,
|
|
145
|
+
slashCommands: resolveSlashCommands(),
|
|
146
|
+
slashTrigger,
|
|
147
|
+
extra: [...extraExtensions ?? [], ...bubbleMenu && bubbleElement ? [BubbleMenuExt.configure({
|
|
148
|
+
element: bubbleElement,
|
|
149
|
+
options: { placement: "top" }
|
|
150
|
+
})] : []]
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
let suppressUpdate = false;
|
|
154
|
+
let lastEmitted;
|
|
155
|
+
$effect(() => {
|
|
156
|
+
if (!contentElement) return;
|
|
157
|
+
const initialContent = untrack(() => value ?? "");
|
|
158
|
+
const initialEditable = untrack(() => !disabled && !readonly);
|
|
159
|
+
const initialAutofocus = untrack(() => autofocus);
|
|
160
|
+
const initialAttrs = untrack(() => ({
|
|
161
|
+
id: resolvedId,
|
|
162
|
+
...resolvedName ? { "data-name": resolvedName } : {},
|
|
163
|
+
...ariaDescribedBy ? { "aria-describedby": ariaDescribedBy } : {},
|
|
164
|
+
...hasError ? { "aria-invalid": "true" } : {}
|
|
165
|
+
}));
|
|
166
|
+
const el = contentElement;
|
|
167
|
+
const result = untrack(() => resolveExtensions());
|
|
168
|
+
let ed = null;
|
|
169
|
+
let cancelled = false;
|
|
170
|
+
const create = (exts) => {
|
|
171
|
+
if (cancelled) return;
|
|
172
|
+
ed = new Editor({
|
|
173
|
+
element: el,
|
|
174
|
+
extensions: exts,
|
|
175
|
+
content: initialContent,
|
|
176
|
+
editable: initialEditable,
|
|
177
|
+
autofocus: initialAutofocus,
|
|
178
|
+
editorProps: { attributes: initialAttrs },
|
|
179
|
+
onCreate: ({ editor: e }) => syncState(e),
|
|
180
|
+
onUpdate: ({ editor: e }) => {
|
|
181
|
+
syncState(e);
|
|
182
|
+
if (suppressUpdate) return;
|
|
183
|
+
const serialized = serialize(e);
|
|
184
|
+
lastEmitted = serialized;
|
|
185
|
+
value = serialized;
|
|
186
|
+
emit.onInput();
|
|
187
|
+
onValueChange?.(serialized);
|
|
188
|
+
},
|
|
189
|
+
onSelectionUpdate: ({ editor: e }) => syncState(e),
|
|
190
|
+
onFocus: ({ editor: e }) => {
|
|
191
|
+
syncState(e);
|
|
192
|
+
emit.onFocus();
|
|
193
|
+
onFocus?.();
|
|
194
|
+
},
|
|
195
|
+
onBlur: ({ editor: e }) => {
|
|
196
|
+
syncState(e);
|
|
197
|
+
emit.onBlur();
|
|
198
|
+
onBlur?.();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
editor = ed;
|
|
202
|
+
};
|
|
203
|
+
if (result instanceof Promise) {
|
|
204
|
+
result.then(create);
|
|
205
|
+
} else {
|
|
206
|
+
create(result);
|
|
207
|
+
}
|
|
208
|
+
return () => {
|
|
209
|
+
cancelled = true;
|
|
210
|
+
ed?.destroy();
|
|
211
|
+
editor = null;
|
|
212
|
+
};
|
|
213
|
+
});
|
|
214
|
+
$effect(() => {
|
|
215
|
+
if (!editor) return;
|
|
216
|
+
const target = !disabled && !readonly;
|
|
217
|
+
if (editor.isEditable !== target) {
|
|
218
|
+
editor.setEditable(target);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
$effect(() => {
|
|
222
|
+
if (!contentElement) return;
|
|
223
|
+
const pm = contentElement.querySelector(".ProseMirror");
|
|
224
|
+
if (!pm) return;
|
|
225
|
+
if (hasError) pm.setAttribute("aria-invalid", "true");
|
|
226
|
+
else pm.removeAttribute("aria-invalid");
|
|
227
|
+
if (ariaDescribedBy) pm.setAttribute("aria-describedby", ariaDescribedBy);
|
|
228
|
+
else pm.removeAttribute("aria-describedby");
|
|
229
|
+
if (resolvedId) pm.setAttribute("id", resolvedId);
|
|
230
|
+
});
|
|
231
|
+
$effect(() => {
|
|
232
|
+
if (!editor) return;
|
|
233
|
+
if (value === undefined) return;
|
|
234
|
+
if (typeof value === "string" && value === lastEmitted) return;
|
|
235
|
+
const current = serialize(editor);
|
|
236
|
+
if (isContentEqual(current, value)) return;
|
|
237
|
+
suppressUpdate = true;
|
|
238
|
+
editor.commands.setContent(value, { emitUpdate: false });
|
|
239
|
+
suppressUpdate = false;
|
|
240
|
+
syncState(editor);
|
|
241
|
+
});
|
|
242
|
+
const apiInstance = {
|
|
243
|
+
get editor() {
|
|
244
|
+
return editor;
|
|
245
|
+
},
|
|
246
|
+
get state() {
|
|
247
|
+
return editorState;
|
|
248
|
+
},
|
|
249
|
+
focus(position) {
|
|
250
|
+
editor?.commands.focus(position);
|
|
251
|
+
},
|
|
252
|
+
run(action) {
|
|
253
|
+
if (!editor) return;
|
|
254
|
+
TOOLBAR_ACTIONS[action].run(editor);
|
|
255
|
+
},
|
|
256
|
+
getValue(format) {
|
|
257
|
+
if (!editor) return resolvedOutput === "json" ? {} : "";
|
|
258
|
+
const fmt = format ?? resolvedOutput;
|
|
259
|
+
if (fmt === "json") return editor.getJSON();
|
|
260
|
+
if (fmt === "markdown") {
|
|
261
|
+
const md = getMarkdownStorage(editor);
|
|
262
|
+
if (md && typeof md.getMarkdown === "function") return md.getMarkdown();
|
|
263
|
+
return editor.getHTML();
|
|
264
|
+
}
|
|
265
|
+
return editor.getHTML();
|
|
266
|
+
},
|
|
267
|
+
setValue(next) {
|
|
268
|
+
if (!editor) return;
|
|
269
|
+
suppressUpdate = true;
|
|
270
|
+
editor.commands.setContent(next, { emitUpdate: false });
|
|
271
|
+
suppressUpdate = false;
|
|
272
|
+
syncState(editor);
|
|
273
|
+
},
|
|
274
|
+
clear() {
|
|
275
|
+
editor?.chain().focus().clearContent().run();
|
|
276
|
+
},
|
|
277
|
+
insert(content) {
|
|
278
|
+
editor?.chain().focus().insertContent(content).run();
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
api = apiInstance;
|
|
282
|
+
const resolvedToolbar = $derived.by(() => {
|
|
283
|
+
if (toolbar === false) return [];
|
|
284
|
+
if (toolbar === true) return DEFAULT_TOOLBAR;
|
|
285
|
+
return toolbar;
|
|
286
|
+
});
|
|
287
|
+
const classes = $derived.by(() => {
|
|
288
|
+
const slots = editorVariants({
|
|
289
|
+
size,
|
|
290
|
+
color: resolvedColor,
|
|
291
|
+
sticky: stickyToolbar
|
|
292
|
+
});
|
|
293
|
+
const c = config.slots;
|
|
294
|
+
const u = ui ?? {};
|
|
295
|
+
return {
|
|
296
|
+
root: slots.root({ class: [
|
|
297
|
+
c.root,
|
|
298
|
+
className,
|
|
299
|
+
u.root
|
|
300
|
+
] }),
|
|
301
|
+
toolbar: slots.toolbar({ class: [c.toolbar, u.toolbar] }),
|
|
302
|
+
toolbarButton: slots.toolbarButton({ class: [c.toolbarButton, u.toolbarButton] }),
|
|
303
|
+
toolbarSeparator: slots.toolbarSeparator({ class: [c.toolbarSeparator, u.toolbarSeparator] }),
|
|
304
|
+
content: slots.content({ class: [c.content, u.content] }),
|
|
305
|
+
footer: slots.footer({ class: [c.footer, u.footer] }),
|
|
306
|
+
countLabel: slots.countLabel({ class: [c.countLabel, u.countLabel] }),
|
|
307
|
+
bubbleMenu: slots.bubbleMenu({ class: [c.bubbleMenu, u.bubbleMenu] })
|
|
308
|
+
};
|
|
309
|
+
});
|
|
310
|
+
let urlPrompt = $state({
|
|
311
|
+
open: false,
|
|
312
|
+
title: "Enter URL",
|
|
313
|
+
placeholder: "https://",
|
|
314
|
+
initialValue: "",
|
|
315
|
+
confirmLabel: "Insert"
|
|
316
|
+
});
|
|
317
|
+
function openUrlPrompt(opts) {
|
|
318
|
+
urlPrompt = {
|
|
319
|
+
open: true,
|
|
320
|
+
title: opts.title,
|
|
321
|
+
description: opts.description,
|
|
322
|
+
placeholder: opts.placeholder ?? "https://",
|
|
323
|
+
initialValue: opts.initialValue ?? "",
|
|
324
|
+
confirmLabel: opts.confirmLabel ?? "Insert",
|
|
325
|
+
schema: opts.schema,
|
|
326
|
+
onConfirm: opts.onConfirm,
|
|
327
|
+
onCancel: opts.onCancel
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
let fileInput = $state(null);
|
|
331
|
+
async function handleFileSelected(event) {
|
|
332
|
+
if (!editor) return;
|
|
333
|
+
const input = event.currentTarget;
|
|
334
|
+
const file = input.files?.[0];
|
|
335
|
+
input.value = "";
|
|
336
|
+
if (!file) return;
|
|
337
|
+
if (!onImageUpload) return;
|
|
338
|
+
try {
|
|
339
|
+
const url = await onImageUpload(file);
|
|
340
|
+
if (!isSafeImageSrc(url)) {
|
|
341
|
+
console.warn(`[svelora] Blocked unsafe image src returned by onImageUpload: ${url}`);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
editor.chain().focus().setImage({ src: url }).run();
|
|
345
|
+
} catch (err) {
|
|
346
|
+
if (onImageUploadError) {
|
|
347
|
+
onImageUploadError(err);
|
|
348
|
+
} else {}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
function openImagePicker() {
|
|
352
|
+
if (!editor) return;
|
|
353
|
+
if (!onImageUpload) {
|
|
354
|
+
openUrlPrompt({
|
|
355
|
+
title: "Image URL",
|
|
356
|
+
placeholder: "https://example.com/image.png",
|
|
357
|
+
schema: httpUrlSchema,
|
|
358
|
+
onConfirm: (url) => {
|
|
359
|
+
editor?.chain().focus().setImage({ src: url }).run();
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
fileInput?.click();
|
|
365
|
+
}
|
|
366
|
+
function openYoutubePrompt() {
|
|
367
|
+
if (!editor) return;
|
|
368
|
+
openUrlPrompt({
|
|
369
|
+
title: "Embed YouTube video",
|
|
370
|
+
description: "Paste the share link or full URL.",
|
|
371
|
+
placeholder: "https://youtu.be/...",
|
|
372
|
+
confirmLabel: "Embed",
|
|
373
|
+
schema: youtubeUrlSchema,
|
|
374
|
+
onConfirm: (url) => {
|
|
375
|
+
editor?.commands.setYoutubeVideo({ src: url });
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
function openLinkPrompt() {
|
|
380
|
+
if (!editor) return;
|
|
381
|
+
const previous = editor.getAttributes("link").href ?? "";
|
|
382
|
+
openUrlPrompt({
|
|
383
|
+
title: "Insert link",
|
|
384
|
+
placeholder: "https://",
|
|
385
|
+
initialValue: previous,
|
|
386
|
+
schema: httpUrlSchema,
|
|
387
|
+
onConfirm: (url) => {
|
|
388
|
+
editor?.chain().focus().extendMarkRange("link").setLink({ href: url }).run();
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
let tableMenuOpen = $state(false);
|
|
393
|
+
let tablePickerRows = $state(0);
|
|
394
|
+
let tablePickerCols = $state(0);
|
|
395
|
+
let tablePickerEl = $state(null);
|
|
396
|
+
let tableButtonEl = $state(null);
|
|
397
|
+
const TABLE_MAX_ROWS = 8;
|
|
398
|
+
const TABLE_MAX_COLS = 8;
|
|
399
|
+
function insertTable(rows, cols) {
|
|
400
|
+
if (!editor) return;
|
|
401
|
+
editor.chain().focus().insertTable({
|
|
402
|
+
rows,
|
|
403
|
+
cols,
|
|
404
|
+
withHeaderRow: true
|
|
405
|
+
}).run();
|
|
406
|
+
tableMenuOpen = false;
|
|
407
|
+
tablePickerRows = 0;
|
|
408
|
+
tablePickerCols = 0;
|
|
409
|
+
}
|
|
410
|
+
$effect(() => {
|
|
411
|
+
if (!tableMenuOpen) return;
|
|
412
|
+
function onDocPointerDown(event) {
|
|
413
|
+
const target = event.target;
|
|
414
|
+
if (!target) return;
|
|
415
|
+
if (tablePickerEl?.contains(target)) return;
|
|
416
|
+
if (tableButtonEl?.contains(target)) return;
|
|
417
|
+
tableMenuOpen = false;
|
|
418
|
+
tablePickerRows = 0;
|
|
419
|
+
tablePickerCols = 0;
|
|
420
|
+
}
|
|
421
|
+
document.addEventListener("pointerdown", onDocPointerDown, true);
|
|
422
|
+
return () => document.removeEventListener("pointerdown", onDocPointerDown, true);
|
|
423
|
+
});
|
|
424
|
+
function runAction(def, id) {
|
|
425
|
+
if (!editor) return;
|
|
426
|
+
if (id === "image") {
|
|
427
|
+
openImagePicker();
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (id === "youtube") {
|
|
431
|
+
openYoutubePrompt();
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (id === "link") {
|
|
435
|
+
openLinkPrompt();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (id === "table") {
|
|
439
|
+
tableMenuOpen = !tableMenuOpen;
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
def.run(editor);
|
|
443
|
+
}
|
|
579
444
|
</script>
|
|
580
445
|
|
|
581
446
|
<div
|