@sveltia/ui 0.89.3 → 0.91.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/dist/components/dialog/dialog.svelte +149 -9
- package/dist/components/text-editor/constants.d.ts +40 -6
- package/dist/components/text-editor/constants.js +81 -7
- package/dist/components/text-editor/conversion/html/components.d.ts +16 -0
- package/dist/components/text-editor/conversion/html/components.js +87 -0
- package/dist/components/text-editor/conversion/html/export.d.ts +10 -0
- package/dist/components/text-editor/conversion/html/export.js +360 -0
- package/dist/components/text-editor/conversion/html/import-rules.d.ts +12 -0
- package/dist/components/text-editor/conversion/html/import-rules.js +234 -0
- package/dist/components/text-editor/conversion/html/index.d.ts +10 -0
- package/dist/components/text-editor/conversion/html/index.js +33 -0
- package/dist/components/text-editor/conversion/html/paragraph.d.ts +1 -0
- package/dist/components/text-editor/conversion/html/paragraph.js +123 -0
- package/dist/components/text-editor/conversion/html/task-list.d.ts +13 -0
- package/dist/components/text-editor/conversion/html/task-list.js +82 -0
- package/dist/components/text-editor/conversion/html/unsupported.d.ts +2 -0
- package/dist/components/text-editor/conversion/html/unsupported.js +122 -0
- package/dist/components/text-editor/conversion/html/utils.d.ts +6 -0
- package/dist/components/text-editor/conversion/html/utils.js +34 -0
- package/dist/components/text-editor/conversion/import.js +1 -1
- package/dist/components/text-editor/conversion/transformers/task-list.d.ts +24 -0
- package/dist/components/text-editor/conversion/transformers/task-list.js +53 -0
- package/dist/components/text-editor/conversion/update.js +1 -1
- package/dist/components/text-editor/core.js +4 -12
- package/dist/components/text-editor/editing/commands.js +9 -18
- package/dist/components/text-editor/editing/guards.d.ts +7 -0
- package/dist/components/text-editor/editing/guards.js +123 -0
- package/dist/components/text-editor/editing/raw-markdown.d.ts +4 -2
- package/dist/components/text-editor/editing/raw-markdown.js +22 -6
- package/dist/components/text-editor/editing/selection.js +6 -2
- package/dist/components/text-editor/lexical-root.svelte +40 -0
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/text-editor.svelte +7 -4
- package/dist/components/text-editor/text-editor.svelte.d.ts +4 -2
- package/dist/components/text-editor/toolbar/format-text-button.svelte +7 -1
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +19 -8
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +7 -14
- package/dist/components/util/modal.svelte +41 -12
- package/dist/locales/ar.yaml +2 -0
- package/dist/locales/bg.yaml +2 -0
- package/dist/locales/ca.yaml +2 -0
- package/dist/locales/cs.yaml +2 -0
- package/dist/locales/da.yaml +2 -0
- package/dist/locales/de.yaml +2 -0
- package/dist/locales/el.yaml +2 -0
- package/dist/locales/en-CA.yaml +2 -0
- package/dist/locales/en-GB.yaml +2 -0
- package/dist/locales/en-US.yaml +2 -0
- package/dist/locales/en-ZA.yaml +2 -0
- package/dist/locales/es-CO.yaml +2 -0
- package/dist/locales/es-ES.yaml +176 -0
- package/dist/locales/fa.yaml +2 -0
- package/dist/locales/fi.yaml +2 -0
- package/dist/locales/fr.yaml +2 -0
- package/dist/locales/he.yaml +2 -0
- package/dist/locales/hr.yaml +2 -0
- package/dist/locales/it.yaml +2 -0
- package/dist/locales/ja.yaml +2 -0
- package/dist/locales/ko.yaml +2 -0
- package/dist/locales/nl.yaml +2 -0
- package/dist/locales/pl.yaml +2 -0
- package/dist/locales/pt-BR.yaml +2 -0
- package/dist/locales/pt-PT.yaml +2 -0
- package/dist/locales/ru.yaml +2 -0
- package/dist/locales/sk.yaml +2 -0
- package/dist/locales/sv.yaml +2 -0
- package/dist/locales/tr.yaml +2 -0
- package/dist/locales/uk.yaml +2 -0
- package/dist/locales/vi.yaml +2 -0
- package/dist/locales/zh-CN.yaml +2 -0
- package/dist/locales/zh-TW.yaml +2 -0
- package/dist/typedefs.d.ts +33 -3
- package/dist/typedefs.js +19 -3
- package/package.json +13 -13
- package/dist/components/text-editor/conversion/html.d.ts +0 -30
- package/dist/components/text-editor/conversion/html.js +0 -719
|
@@ -4,6 +4,18 @@
|
|
|
4
4
|
@see https://w3c.github.io/aria/#dialog
|
|
5
5
|
@see https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/
|
|
6
6
|
-->
|
|
7
|
+
<script module>
|
|
8
|
+
/**
|
|
9
|
+
* Keep the distance a dialog is dragged within the room it has on either side.
|
|
10
|
+
* @param {number} distance Distance moved.
|
|
11
|
+
* @param {number} before Room on the left or top.
|
|
12
|
+
* @param {number} after Room on the right or bottom.
|
|
13
|
+
* @returns {number} Clamped distance.
|
|
14
|
+
*/
|
|
15
|
+
const clamp = (distance, before, after) =>
|
|
16
|
+
Math.min(Math.max(distance, -before), Math.max(after, 0));
|
|
17
|
+
</script>
|
|
18
|
+
|
|
7
19
|
<script>
|
|
8
20
|
import { _ } from '@sveltia/i18n';
|
|
9
21
|
import { sleep } from '@sveltia/utils/misc';
|
|
@@ -27,6 +39,9 @@
|
|
|
27
39
|
ariaLabelledby = undefined,
|
|
28
40
|
role = 'dialog',
|
|
29
41
|
size = 'medium',
|
|
42
|
+
modal = true,
|
|
43
|
+
showBackdrop = undefined,
|
|
44
|
+
movable = false,
|
|
30
45
|
class: className,
|
|
31
46
|
showClose = false,
|
|
32
47
|
showOk = true,
|
|
@@ -66,12 +81,128 @@
|
|
|
66
81
|
* A reference to the modal component.
|
|
67
82
|
* @type {Modal | undefined}
|
|
68
83
|
*/
|
|
69
|
-
let
|
|
84
|
+
let modalComponent = $state();
|
|
70
85
|
/**
|
|
71
86
|
* @type {HTMLElement | undefined}
|
|
72
87
|
*/
|
|
73
88
|
let content = $state();
|
|
74
89
|
|
|
90
|
+
/**
|
|
91
|
+
* How far the user has moved the dialog from the center of the viewport by dragging its header.
|
|
92
|
+
* @type {{ x: number, y: number }}
|
|
93
|
+
*/
|
|
94
|
+
let offset = $state({ x: 0, y: 0 });
|
|
95
|
+
/**
|
|
96
|
+
* Where the current drag started, if the user is dragging the header.
|
|
97
|
+
* @type {{ pointerId: number, x: number, y: number, offset: { x: number, y: number }, rect:
|
|
98
|
+
* DOMRect } | undefined}
|
|
99
|
+
*/
|
|
100
|
+
let dragStart;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Move the dialog back to the center of the viewport, ending a drag if any.
|
|
104
|
+
*/
|
|
105
|
+
const recenter = () => {
|
|
106
|
+
offset = { x: 0, y: 0 };
|
|
107
|
+
dragStart = undefined;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
$effect(() => {
|
|
111
|
+
if (!open) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Start from the center whenever the dialog is opened again
|
|
116
|
+
recenter();
|
|
117
|
+
|
|
118
|
+
if (!movable) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A moved dialog could end up outside a smaller viewport, so bring it back to the center
|
|
123
|
+
window.addEventListener('resize', recenter);
|
|
124
|
+
|
|
125
|
+
return () => {
|
|
126
|
+
window.removeEventListener('resize', recenter);
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Start moving the dialog when the header is pressed, unless a button or another control in the
|
|
132
|
+
* header is. Only bound when the dialog is {@link movable}.
|
|
133
|
+
* @param {PointerEvent} event `pointerdown` event.
|
|
134
|
+
*/
|
|
135
|
+
const onHeaderPointerDown = (event) => {
|
|
136
|
+
const { button, pointerId, clientX, clientY, currentTarget, target } = event;
|
|
137
|
+
const headerElement = /** @type {HTMLElement} */ (currentTarget);
|
|
138
|
+
|
|
139
|
+
// Look for the control within the header only: the `<dialog>` around it has `tabindex` as well
|
|
140
|
+
const control = /** @type {HTMLElement} */ (target).closest(
|
|
141
|
+
'button, input, select, textarea, a, [tabindex]',
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
if (button !== 0 || (control && headerElement.contains(control))) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
headerElement.setPointerCapture(pointerId);
|
|
150
|
+
dragStart = {
|
|
151
|
+
pointerId,
|
|
152
|
+
x: clientX,
|
|
153
|
+
y: clientY,
|
|
154
|
+
offset: { ...offset },
|
|
155
|
+
// The header is rendered within the content
|
|
156
|
+
rect: /** @type {HTMLElement} */ (content).getBoundingClientRect(),
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Move the dialog along with the pointer, keeping it within the viewport.
|
|
162
|
+
* @param {PointerEvent} event `pointermove` event.
|
|
163
|
+
*/
|
|
164
|
+
const onHeaderPointerMove = ({ pointerId, buttons, clientX, clientY }) => {
|
|
165
|
+
if (!dragStart || dragStart.pointerId !== pointerId) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The drag can end without a `pointerup` reaching the header, e.g. when the dialog is closed
|
|
170
|
+
// halfway, so a move without the primary button pressed ends it. The primary button is the
|
|
171
|
+
// lowest bit of `buttons`
|
|
172
|
+
if (buttons % 2 === 0) {
|
|
173
|
+
dragStart = undefined;
|
|
174
|
+
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const { x, y, offset: startOffset, rect } = dragStart;
|
|
179
|
+
|
|
180
|
+
offset = {
|
|
181
|
+
x: startOffset.x + clamp(clientX - x, rect.left, window.innerWidth - rect.right),
|
|
182
|
+
y: startOffset.y + clamp(clientY - y, rect.top, window.innerHeight - rect.bottom),
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Stop moving the dialog.
|
|
188
|
+
* @param {PointerEvent} event `pointerup` or `pointercancel` event.
|
|
189
|
+
*/
|
|
190
|
+
const onHeaderPointerUp = ({ pointerId }) => {
|
|
191
|
+
if (dragStart?.pointerId === pointerId) {
|
|
192
|
+
dragStart = undefined;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Event handlers that let the user move the dialog by dragging its header.
|
|
198
|
+
*/
|
|
199
|
+
const dragHandlers = {
|
|
200
|
+
onpointerdown: onHeaderPointerDown,
|
|
201
|
+
onpointermove: onHeaderPointerMove,
|
|
202
|
+
onpointerup: onHeaderPointerUp,
|
|
203
|
+
onpointercancel: onHeaderPointerUp,
|
|
204
|
+
};
|
|
205
|
+
|
|
75
206
|
$effect(() => {
|
|
76
207
|
if (open && content) {
|
|
77
208
|
let cancelled = false;
|
|
@@ -102,7 +233,7 @@
|
|
|
102
233
|
} else {
|
|
103
234
|
// Fall back to the `<dialog>` element itself, so the focus is never left outside the
|
|
104
235
|
// modal, e.g. when the dialog has no input field or primary button
|
|
105
|
-
|
|
236
|
+
modalComponent?.focus();
|
|
106
237
|
}
|
|
107
238
|
})();
|
|
108
239
|
|
|
@@ -116,7 +247,7 @@
|
|
|
116
247
|
</script>
|
|
117
248
|
|
|
118
249
|
<Modal
|
|
119
|
-
bind:this={
|
|
250
|
+
bind:this={modalComponent}
|
|
120
251
|
{...restProps}
|
|
121
252
|
{role}
|
|
122
253
|
{id}
|
|
@@ -125,11 +256,17 @@
|
|
|
125
256
|
aria-labelledby={labelledby}
|
|
126
257
|
aria-describedby="{id}-body"
|
|
127
258
|
bind:open
|
|
128
|
-
|
|
259
|
+
{modal}
|
|
260
|
+
showBackdrop={showBackdrop ?? modal}
|
|
129
261
|
>
|
|
130
|
-
<div
|
|
262
|
+
<div
|
|
263
|
+
bind:this={content}
|
|
264
|
+
role="none"
|
|
265
|
+
class={['content', className, size]}
|
|
266
|
+
style:translate={movable ? `${offset.x}px ${offset.y}px` : undefined}
|
|
267
|
+
>
|
|
131
268
|
{#if title || showClose || header || headerExtra}
|
|
132
|
-
<div role="none" class="header">
|
|
269
|
+
<div role="none" class="header" class:movable {...movable ? dragHandlers : {}}>
|
|
133
270
|
{#if header}
|
|
134
271
|
{@render header()}
|
|
135
272
|
{:else}
|
|
@@ -143,7 +280,7 @@
|
|
|
143
280
|
controls={id}
|
|
144
281
|
{closeIcon}
|
|
145
282
|
onClose={() => {
|
|
146
|
-
|
|
283
|
+
modalComponent?.close('close');
|
|
147
284
|
}}
|
|
148
285
|
/>
|
|
149
286
|
{/if}
|
|
@@ -167,7 +304,7 @@
|
|
|
167
304
|
keyShortcuts={okShortcuts}
|
|
168
305
|
disabled={okDisabled}
|
|
169
306
|
onclick={() => {
|
|
170
|
-
|
|
307
|
+
modalComponent?.close('ok');
|
|
171
308
|
}}
|
|
172
309
|
/>
|
|
173
310
|
{/if}
|
|
@@ -178,7 +315,7 @@
|
|
|
178
315
|
keyShortcuts={cancelShortcuts}
|
|
179
316
|
disabled={cancelDisabled}
|
|
180
317
|
onclick={() => {
|
|
181
|
-
|
|
318
|
+
modalComponent?.close('cancel');
|
|
182
319
|
}}
|
|
183
320
|
/>
|
|
184
321
|
{/if}
|
|
@@ -273,6 +410,9 @@
|
|
|
273
410
|
padding: var(--sui-dialog-header-padding, 16px 8px);
|
|
274
411
|
height: var(--sui-dialog-header-height, 32px);
|
|
275
412
|
}
|
|
413
|
+
.header.movable {
|
|
414
|
+
cursor: move;
|
|
415
|
+
}
|
|
276
416
|
.header .title {
|
|
277
417
|
font-size: var(--sui-font-size-large);
|
|
278
418
|
font-weight: var(--sui-font-weight-bold);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @import { EditorThemeClasses, LexicalCommand } from 'lexical';
|
|
3
|
+
* @import { ListType } from '@lexical/list';
|
|
3
4
|
* @import { Transformer } from '@lexical/markdown';
|
|
4
5
|
* @import {
|
|
5
6
|
* TextEditorBlockType,
|
|
@@ -35,6 +36,38 @@ export const INLINE_BUTTON_TYPES: TextEditorInlineType[];
|
|
|
35
36
|
* @type {TextEditorBlockType[]}
|
|
36
37
|
*/
|
|
37
38
|
export const BLOCK_BUTTON_TYPES: TextEditorBlockType[];
|
|
39
|
+
/**
|
|
40
|
+
* Buttons only available for HTML, as Markdown has no syntax for them: the underline format and the
|
|
41
|
+
* alignment menu. They’re hidden for Markdown and in the plain text mode, which shows the HTML
|
|
42
|
+
* source, and they’re not enabled by default.
|
|
43
|
+
* @type {('underline' | 'align')[]}
|
|
44
|
+
*/
|
|
45
|
+
export const HTML_ONLY_BUTTON_TYPES: ("underline" | "align")[];
|
|
46
|
+
export function isHtmlOnlyButton(type: string): type is "underline" | "align";
|
|
47
|
+
/**
|
|
48
|
+
* Text formats only available for HTML, like underline. See {@link HTML_ONLY_BUTTON_TYPES}.
|
|
49
|
+
*/
|
|
50
|
+
export const HTML_ONLY_FORMAT_TYPES: TextEditorFormatType[];
|
|
51
|
+
/**
|
|
52
|
+
* Buttons not enabled by default: the HTML-only ones and the task list, a GitHub Flavored Markdown
|
|
53
|
+
* extension that not every Markdown renderer supports.
|
|
54
|
+
* @type {TextEditorNodeType[]}
|
|
55
|
+
*/
|
|
56
|
+
export const OPTIONAL_BUTTON_TYPES: TextEditorNodeType[];
|
|
57
|
+
/**
|
|
58
|
+
* Buttons enabled by default: all the inline and block level buttons but the optional ones.
|
|
59
|
+
* @type {TextEditorNodeType[]}
|
|
60
|
+
*/
|
|
61
|
+
export const DEFAULT_BUTTON_TYPES: TextEditorNodeType[];
|
|
62
|
+
/**
|
|
63
|
+
* List block types, with their Lexical list type and the command to insert the list.
|
|
64
|
+
* @type {{ type: TextEditorBlockType, listType: ListType, command: LexicalCommand<void> }[]}
|
|
65
|
+
*/
|
|
66
|
+
export const LIST_TYPES: {
|
|
67
|
+
type: TextEditorBlockType;
|
|
68
|
+
listType: ListType;
|
|
69
|
+
command: LexicalCommand<void>;
|
|
70
|
+
}[];
|
|
38
71
|
/**
|
|
39
72
|
* Map of Lexical nodes for each block type. The `paragraph` block type is excluded because it is
|
|
40
73
|
* the default block type.
|
|
@@ -42,12 +75,12 @@ export const BLOCK_BUTTON_TYPES: TextEditorBlockType[];
|
|
|
42
75
|
*/
|
|
43
76
|
export const NODE_MAP: Record<Exclude<TextEditorBlockType | "link", "paragraph">, any[]>;
|
|
44
77
|
/**
|
|
45
|
-
* Map of Lexical transformers for each
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* Transformer[]
|
|
78
|
+
* Map of Lexical transformers for each button type. The `paragraph` block type has none because it
|
|
79
|
+
* is the default block type, and neither have the HTML-only buttons. Horizontal rules and tables
|
|
80
|
+
* are always supported in Markdown, so their transformers are added in `core.js`.
|
|
81
|
+
* @type {Partial<Record<TextEditorNodeType, Transformer[]>>}
|
|
49
82
|
*/
|
|
50
|
-
export const TRANSFORMER_MAP: Record<
|
|
83
|
+
export const TRANSFORMER_MAP: Partial<Record<TextEditorNodeType, Transformer[]>>;
|
|
51
84
|
/**
|
|
52
85
|
* List of Markdown tags that should be disabled in the editor when converting Lexical nodes to
|
|
53
86
|
* Markdown (but not when converting Markdown to Lexical nodes because we don’t want to lose any
|
|
@@ -94,5 +127,6 @@ import type { TextEditorFormatType } from '../../typedefs.js';
|
|
|
94
127
|
import type { TextEditorInlineType } from '../../typedefs.js';
|
|
95
128
|
import type { TextEditorBlockType } from '../../typedefs.js';
|
|
96
129
|
import type { TextEditorNodeType } from '../../typedefs.js';
|
|
97
|
-
import type {
|
|
130
|
+
import type { ListType } from '@lexical/list';
|
|
98
131
|
import type { LexicalCommand } from 'lexical';
|
|
132
|
+
import type { Transformer } from '@lexical/markdown';
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { CodeHighlightNode, CodeNode } from '@lexical/code-core';
|
|
2
2
|
import { LinkNode } from '@lexical/link';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
INSERT_CHECK_LIST_COMMAND,
|
|
5
|
+
INSERT_ORDERED_LIST_COMMAND,
|
|
6
|
+
INSERT_UNORDERED_LIST_COMMAND,
|
|
7
|
+
ListItemNode,
|
|
8
|
+
ListNode,
|
|
9
|
+
} from '@lexical/list';
|
|
4
10
|
import {
|
|
5
11
|
BOLD_ITALIC_STAR,
|
|
6
12
|
BOLD_ITALIC_UNDERSCORE,
|
|
@@ -21,9 +27,11 @@ import { createCommand } from 'lexical';
|
|
|
21
27
|
import { CODE } from './conversion/transformers/code.js';
|
|
22
28
|
import { LEADING_INLINE_CODE } from './conversion/transformers/inline-code.js';
|
|
23
29
|
import { LINK } from './conversion/transformers/link.js';
|
|
30
|
+
import { TASK_LIST } from './conversion/transformers/task-list.js';
|
|
24
31
|
|
|
25
32
|
/**
|
|
26
33
|
* @import { EditorThemeClasses, LexicalCommand } from 'lexical';
|
|
34
|
+
* @import { ListType } from '@lexical/list';
|
|
27
35
|
* @import { Transformer } from '@lexical/markdown';
|
|
28
36
|
* @import {
|
|
29
37
|
* TextEditorBlockType,
|
|
@@ -39,16 +47,22 @@ import { LINK } from './conversion/transformers/link.js';
|
|
|
39
47
|
export const EDITOR_THEME = {
|
|
40
48
|
text: {
|
|
41
49
|
/**
|
|
42
|
-
* Enable bold+italic and strikethrough styling.
|
|
50
|
+
* Enable bold+italic, underline and strikethrough styling. Lexical only uses the combined
|
|
51
|
+
* class for text that is both underlined and struck through.
|
|
43
52
|
* @see https://github.com/facebook/lexical/discussions/4381
|
|
44
53
|
*/
|
|
45
54
|
italic: 'italic',
|
|
55
|
+
underline: 'underline',
|
|
46
56
|
strikethrough: 'strikethrough',
|
|
57
|
+
underlineStrikethrough: 'underline strikethrough',
|
|
47
58
|
},
|
|
48
59
|
list: {
|
|
49
60
|
nested: {
|
|
50
61
|
listitem: 'nested',
|
|
51
62
|
},
|
|
63
|
+
// Task list items, drawn with a check box; see `lexical-root.svelte`
|
|
64
|
+
listitemChecked: 'task-list-item checked',
|
|
65
|
+
listitemUnchecked: 'task-list-item',
|
|
52
66
|
},
|
|
53
67
|
code: 'code-block',
|
|
54
68
|
// Lexical wraps a table with an element to scroll it horizontally when the table button is
|
|
@@ -73,6 +87,11 @@ export const AVAILABLE_BUTTONS = {
|
|
|
73
87
|
icon: 'format_italic',
|
|
74
88
|
inline: true,
|
|
75
89
|
},
|
|
90
|
+
underline: {
|
|
91
|
+
labelKey: 'underline',
|
|
92
|
+
icon: 'format_underlined',
|
|
93
|
+
inline: true,
|
|
94
|
+
},
|
|
76
95
|
strikethrough: {
|
|
77
96
|
labelKey: 'strikethrough',
|
|
78
97
|
icon: 'strikethrough_s',
|
|
@@ -133,6 +152,11 @@ export const AVAILABLE_BUTTONS = {
|
|
|
133
152
|
icon: 'format_list_numbered',
|
|
134
153
|
inline: false,
|
|
135
154
|
},
|
|
155
|
+
'task-list': {
|
|
156
|
+
labelKey: 'task_list',
|
|
157
|
+
icon: 'checklist',
|
|
158
|
+
inline: false,
|
|
159
|
+
},
|
|
136
160
|
blockquote: {
|
|
137
161
|
labelKey: 'blockquote',
|
|
138
162
|
icon: 'format_quote',
|
|
@@ -158,7 +182,7 @@ export const AVAILABLE_BUTTONS = {
|
|
|
158
182
|
/**
|
|
159
183
|
* @type {TextEditorFormatType[]}
|
|
160
184
|
*/
|
|
161
|
-
export const TEXT_FORMAT_BUTTON_TYPES = ['bold', 'italic', 'strikethrough', 'code'];
|
|
185
|
+
export const TEXT_FORMAT_BUTTON_TYPES = ['bold', 'italic', 'underline', 'strikethrough', 'code'];
|
|
162
186
|
|
|
163
187
|
/**
|
|
164
188
|
* @type {TextEditorInlineType[]}
|
|
@@ -178,10 +202,57 @@ export const BLOCK_BUTTON_TYPES = [
|
|
|
178
202
|
'heading-6',
|
|
179
203
|
'bulleted-list',
|
|
180
204
|
'numbered-list',
|
|
205
|
+
'task-list',
|
|
181
206
|
'blockquote',
|
|
182
207
|
'code-block',
|
|
183
208
|
];
|
|
184
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Buttons only available for HTML, as Markdown has no syntax for them: the underline format and the
|
|
212
|
+
* alignment menu. They’re hidden for Markdown and in the plain text mode, which shows the HTML
|
|
213
|
+
* source, and they’re not enabled by default.
|
|
214
|
+
* @type {('underline' | 'align')[]}
|
|
215
|
+
*/
|
|
216
|
+
export const HTML_ONLY_BUTTON_TYPES = ['underline', 'align'];
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Check whether the given button is only available for HTML. See {@link HTML_ONLY_BUTTON_TYPES}.
|
|
220
|
+
* @param {string} type Button type.
|
|
221
|
+
* @returns {type is 'underline' | 'align'} Result.
|
|
222
|
+
*/
|
|
223
|
+
export const isHtmlOnlyButton = (type) =>
|
|
224
|
+
/** @type {string[]} */ (HTML_ONLY_BUTTON_TYPES).includes(type);
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Text formats only available for HTML, like underline. See {@link HTML_ONLY_BUTTON_TYPES}.
|
|
228
|
+
*/
|
|
229
|
+
export const HTML_ONLY_FORMAT_TYPES = TEXT_FORMAT_BUTTON_TYPES.filter(isHtmlOnlyButton);
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Buttons not enabled by default: the HTML-only ones and the task list, a GitHub Flavored Markdown
|
|
233
|
+
* extension that not every Markdown renderer supports.
|
|
234
|
+
* @type {TextEditorNodeType[]}
|
|
235
|
+
*/
|
|
236
|
+
export const OPTIONAL_BUTTON_TYPES = [...HTML_ONLY_BUTTON_TYPES, 'task-list'];
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Buttons enabled by default: all the inline and block level buttons but the optional ones.
|
|
240
|
+
* @type {TextEditorNodeType[]}
|
|
241
|
+
*/
|
|
242
|
+
export const DEFAULT_BUTTON_TYPES = [...INLINE_BUTTON_TYPES, ...BLOCK_BUTTON_TYPES].filter(
|
|
243
|
+
(type) => !OPTIONAL_BUTTON_TYPES.includes(type),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* List block types, with their Lexical list type and the command to insert the list.
|
|
248
|
+
* @type {{ type: TextEditorBlockType, listType: ListType, command: LexicalCommand<void> }[]}
|
|
249
|
+
*/
|
|
250
|
+
export const LIST_TYPES = [
|
|
251
|
+
{ type: 'bulleted-list', listType: 'bullet', command: INSERT_UNORDERED_LIST_COMMAND },
|
|
252
|
+
{ type: 'numbered-list', listType: 'number', command: INSERT_ORDERED_LIST_COMMAND },
|
|
253
|
+
{ type: 'task-list', listType: 'check', command: INSERT_CHECK_LIST_COMMAND },
|
|
254
|
+
];
|
|
255
|
+
|
|
185
256
|
/**
|
|
186
257
|
* Map of Lexical nodes for each block type. The `paragraph` block type is excluded because it is
|
|
187
258
|
* the default block type.
|
|
@@ -196,16 +267,17 @@ export const NODE_MAP = {
|
|
|
196
267
|
'heading-6': [HeadingNode],
|
|
197
268
|
'bulleted-list': [ListNode, ListItemNode],
|
|
198
269
|
'numbered-list': [ListNode, ListItemNode],
|
|
270
|
+
'task-list': [ListNode, ListItemNode],
|
|
199
271
|
blockquote: [QuoteNode],
|
|
200
272
|
'code-block': [CodeNode, CodeHighlightNode],
|
|
201
273
|
link: [LinkNode],
|
|
202
274
|
};
|
|
203
275
|
|
|
204
276
|
/**
|
|
205
|
-
* Map of Lexical transformers for each
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* Transformer[]
|
|
277
|
+
* Map of Lexical transformers for each button type. The `paragraph` block type has none because it
|
|
278
|
+
* is the default block type, and neither have the HTML-only buttons. Horizontal rules and tables
|
|
279
|
+
* are always supported in Markdown, so their transformers are added in `core.js`.
|
|
280
|
+
* @type {Partial<Record<TextEditorNodeType, Transformer[]>>}
|
|
209
281
|
*/
|
|
210
282
|
export const TRANSFORMER_MAP = {
|
|
211
283
|
// ELEMENT_TRANSFORMERS
|
|
@@ -215,6 +287,8 @@ export const TRANSFORMER_MAP = {
|
|
|
215
287
|
'heading-4': [HEADING],
|
|
216
288
|
'heading-5': [HEADING],
|
|
217
289
|
'heading-6': [HEADING],
|
|
290
|
+
// Before the bulleted list, as `- [ ] item` matches both
|
|
291
|
+
'task-list': [TASK_LIST],
|
|
218
292
|
'bulleted-list': [UNORDERED_LIST],
|
|
219
293
|
'numbered-list': [ORDERED_LIST],
|
|
220
294
|
blockquote: [QUOTE],
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { DOMConversionMap, LexicalEditor } from 'lexical';
|
|
3
|
+
* @import { TextEditorComponent } from '../../../../typedefs.js';
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Editor components of each editor, used to tell the elements they import, which come with their
|
|
7
|
+
* content, when HTML is imported.
|
|
8
|
+
* @type {WeakMap<LexicalEditor, TextEditorComponent[]>}
|
|
9
|
+
*/
|
|
10
|
+
export const editorComponents: WeakMap<LexicalEditor, TextEditorComponent[]>;
|
|
11
|
+
export function getComponentConversionMap(node: any): DOMConversionMap;
|
|
12
|
+
export function getElementComponent(editor: LexicalEditor, element: Element): TextEditorComponent | undefined;
|
|
13
|
+
export function isInlineComponentElement(element: Element): boolean;
|
|
14
|
+
import type { LexicalEditor } from 'lexical';
|
|
15
|
+
import type { TextEditorComponent } from '../../../../typedefs.js';
|
|
16
|
+
import type { DOMConversionMap } from 'lexical';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor components in HTML: the elements they import, which come with their content.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { $getEditor as getEditor } from 'lexical';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { DOMConversionMap, LexicalEditor } from 'lexical';
|
|
9
|
+
* @import { TextEditorComponent } from '../../../../typedefs.js';
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Editor components of each editor, used to tell the elements they import, which come with their
|
|
14
|
+
* content, when HTML is imported.
|
|
15
|
+
* @type {WeakMap<LexicalEditor, TextEditorComponent[]>}
|
|
16
|
+
*/
|
|
17
|
+
export const editorComponents = new WeakMap();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Conversion maps of the editor component nodes, cached as each `importDOM()` call can create a new
|
|
21
|
+
* map.
|
|
22
|
+
* @type {WeakMap<any, DOMConversionMap>}
|
|
23
|
+
*/
|
|
24
|
+
const componentConversionMaps = new WeakMap();
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get the conversion map of the given editor component node.
|
|
28
|
+
* @param {any} node Lexical node class.
|
|
29
|
+
* @returns {DOMConversionMap} Conversion map.
|
|
30
|
+
*/
|
|
31
|
+
export const getComponentConversionMap = (node) => {
|
|
32
|
+
let map = componentConversionMaps.get(node);
|
|
33
|
+
|
|
34
|
+
if (!map) {
|
|
35
|
+
map = node.importDOM?.() ?? {};
|
|
36
|
+
componentConversionMaps.set(node, /** @type {DOMConversionMap} */ (map));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return /** @type {DOMConversionMap} */ (map);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Editor component that imports each element, or `null` if none does, cached as the same element is
|
|
44
|
+
* checked many times during an import, and a conversion can run developer code. An element is only
|
|
45
|
+
* imported to one editor, as each import parses the HTML anew.
|
|
46
|
+
* @type {WeakMap<Element, TextEditorComponent | null>}
|
|
47
|
+
*/
|
|
48
|
+
const elementComponents = new WeakMap();
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get the editor component that imports the given element, if any. The element’s content is then
|
|
52
|
+
* part of the component.
|
|
53
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
54
|
+
* @param {Element} element Element.
|
|
55
|
+
* @returns {TextEditorComponent | undefined} Component.
|
|
56
|
+
*/
|
|
57
|
+
export const getElementComponent = (editor, element) => {
|
|
58
|
+
let component = elementComponents.get(element);
|
|
59
|
+
|
|
60
|
+
if (component === undefined) {
|
|
61
|
+
component =
|
|
62
|
+
editorComponents
|
|
63
|
+
.get(editor)
|
|
64
|
+
?.find(
|
|
65
|
+
({ node }) =>
|
|
66
|
+
!!getComponentConversionMap(node)[element.localName]?.(
|
|
67
|
+
/** @type {HTMLElement} */ (element),
|
|
68
|
+
),
|
|
69
|
+
) ?? null;
|
|
70
|
+
elementComponents.set(element, component);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return component ?? undefined;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if the given element is imported by an inline editor component, like an image, which is
|
|
78
|
+
* content in the line just like text.
|
|
79
|
+
* @param {Element} element Element.
|
|
80
|
+
* @returns {boolean} Result.
|
|
81
|
+
*/
|
|
82
|
+
export const isInlineComponentElement = (element) => {
|
|
83
|
+
const component = getElementComponent(getEditor(), element);
|
|
84
|
+
|
|
85
|
+
// The `node` is actually a node class
|
|
86
|
+
return !!component && !!(/** @type {any} */ (component.node).prototype.isInline?.());
|
|
87
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map of Lexical node classes and functions to export them to clean HTML. A class only takes effect
|
|
3
|
+
* if the node is registered with the editor. Other nodes, including the ones of editor components,
|
|
4
|
+
* are exported with their own `exportDOM` method.
|
|
5
|
+
* @type {DOMExportOutputMap}
|
|
6
|
+
*/
|
|
7
|
+
export const HTML_EXPORT_MAP: DOMExportOutputMap;
|
|
8
|
+
export function exportHtml(editor: LexicalEditor): string;
|
|
9
|
+
import type { DOMExportOutputMap } from 'lexical';
|
|
10
|
+
import type { LexicalEditor } from 'lexical';
|