@sveltia/ui 0.81.2 → 0.82.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/drawer/drawer.svelte.d.ts +2 -2
- package/dist/components/scroll/infinite-scroll.svelte +4 -2
- package/dist/components/text-editor/alignment.d.ts +2 -0
- package/dist/components/text-editor/alignment.js +40 -0
- package/dist/components/text-editor/block.d.ts +4 -0
- package/dist/components/text-editor/block.js +65 -0
- package/dist/components/text-editor/constants.d.ts +22 -2
- package/dist/components/text-editor/constants.js +40 -1
- package/dist/components/text-editor/core.d.ts +7 -0
- package/dist/components/text-editor/core.js +61 -7
- package/dist/components/text-editor/floating-link-editor.svelte +58 -6
- package/dist/components/text-editor/html.js +21 -4
- package/dist/components/text-editor/lexical-root.svelte +27 -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/table-cell-menu-button.svelte +228 -0
- package/dist/components/text-editor/table-cell-menu-button.svelte.d.ts +12 -0
- package/dist/components/text-editor/table.d.ts +18 -0
- package/dist/components/text-editor/table.js +277 -0
- package/dist/components/text-editor/text-editor.svelte +5 -0
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +84 -0
- package/dist/components/text-editor/toolbar/align-menu-button.svelte.d.ts +10 -0
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +65 -0
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte.d.ts +30 -0
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +17 -1
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +209 -0
- package/dist/components/text-editor/toolbar/insert-table-button.svelte.d.ts +24 -0
- package/dist/components/text-editor/toolbar/menu.d.ts +1 -0
- package/dist/components/text-editor/toolbar/menu.js +32 -0
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +96 -0
- package/dist/components/text-editor/toolbar/table-action-menu.svelte.d.ts +21 -0
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +75 -7
- package/dist/components/text-editor/transformers/hr.d.ts +5 -0
- package/dist/components/text-editor/transformers/hr.js +7 -1
- package/dist/components/text-editor/transformers/table.js +3 -2
- package/dist/locales/ar.yaml +28 -0
- package/dist/locales/bg.yaml +28 -0
- package/dist/locales/ca.yaml +28 -0
- package/dist/locales/cs.yaml +28 -0
- package/dist/locales/da.yaml +28 -0
- package/dist/locales/de.yaml +28 -0
- package/dist/locales/el.yaml +28 -0
- package/dist/locales/en-CA.yaml +28 -0
- package/dist/locales/en-GB.yaml +28 -0
- package/dist/locales/en-US.yaml +28 -0
- package/dist/locales/es-CO.yaml +28 -0
- package/dist/locales/fa.yaml +28 -0
- package/dist/locales/fi.yaml +28 -0
- package/dist/locales/fr.yaml +28 -0
- package/dist/locales/he.yaml +28 -0
- package/dist/locales/hr.yaml +28 -0
- package/dist/locales/it.yaml +28 -0
- package/dist/locales/ja.yaml +28 -0
- package/dist/locales/ko.yaml +28 -0
- package/dist/locales/nl.yaml +28 -0
- package/dist/locales/pl.yaml +28 -0
- package/dist/locales/pt-BR.yaml +28 -0
- package/dist/locales/pt-PT.yaml +28 -0
- package/dist/locales/ru.yaml +28 -0
- package/dist/locales/sk.yaml +28 -0
- package/dist/locales/sv.yaml +28 -0
- package/dist/locales/tr.yaml +28 -0
- package/dist/locales/uk.yaml +28 -0
- package/dist/locales/vi.yaml +28 -0
- package/dist/locales/zh-CN.yaml +28 -0
- package/dist/locales/zh-TW.yaml +28 -0
- package/dist/services/popup.svelte.js +4 -2
- package/dist/typedefs.d.ts +10 -1
- package/dist/typedefs.js +5 -1
- package/package.json +6 -6
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Toolbar button to insert a horizontal rule: a node in the rich text mode, with the caret moved
|
|
4
|
+
below it, or its Markdown in the plain text mode.
|
|
5
|
+
-->
|
|
6
|
+
<script>
|
|
7
|
+
import { _ } from '@sveltia/i18n';
|
|
8
|
+
import { getContext } from 'svelte';
|
|
9
|
+
import Button from '../../button/button.svelte';
|
|
10
|
+
import Icon from '../../icon/icon.svelte';
|
|
11
|
+
import { $insertHorizontalRule as insertHorizontalRule } from '../block.js';
|
|
12
|
+
import { AVAILABLE_BUTTONS } from '../constants.js';
|
|
13
|
+
import { focusEditor } from '../core.js';
|
|
14
|
+
import { editRawText, insertMarkdown } from '../raw-markdown.js';
|
|
15
|
+
import { HORIZONTAL_RULE_MARKDOWN } from '../transformers/hr.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @import { TextEditorStore } from '../../../typedefs.js';
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** @type {TextEditorStore} */
|
|
22
|
+
const editorStore = getContext('editorStore');
|
|
23
|
+
|
|
24
|
+
const { labelKey, icon } = AVAILABLE_BUTTONS['horizontal-rule'];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Insert a horizontal rule, then move the focus back to the editor.
|
|
28
|
+
*/
|
|
29
|
+
const insert = async () => {
|
|
30
|
+
const { editor, textArea, useRichText } = editorStore;
|
|
31
|
+
|
|
32
|
+
if (!useRichText) {
|
|
33
|
+
await editRawText(textArea, (state) =>
|
|
34
|
+
insertMarkdown(state, HORIZONTAL_RULE_MARKDOWN, { block: true }),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The button is only enabled while the editor is there
|
|
41
|
+
/* v8 ignore next 3 */
|
|
42
|
+
if (!editor) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
editor.update(() => {
|
|
47
|
+
insertHorizontalRule();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await focusEditor(editor);
|
|
51
|
+
};
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<Button
|
|
55
|
+
iconic
|
|
56
|
+
aria-label={_(`_sui.text_editor.${labelKey}`)}
|
|
57
|
+
aria-controls={editorStore.controlId}
|
|
58
|
+
onclick={() => {
|
|
59
|
+
insert();
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{#snippet startIcon()}
|
|
63
|
+
<Icon name={icon} />
|
|
64
|
+
{/snippet}
|
|
65
|
+
</Button>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export default InsertHorizontalRuleButton;
|
|
2
|
+
type InsertHorizontalRuleButton = SvelteComponent<{
|
|
3
|
+
[x: string]: never;
|
|
4
|
+
}, {
|
|
5
|
+
[evt: string]: CustomEvent<any>;
|
|
6
|
+
}, {}> & {
|
|
7
|
+
$$bindings?: string | undefined;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Toolbar button to insert a horizontal rule: a node in the rich text mode, with the caret moved
|
|
11
|
+
* below it, or its Markdown in the plain text mode.
|
|
12
|
+
*/
|
|
13
|
+
declare const InsertHorizontalRuleButton: $$__sveltets_2_IsomorphicComponent<{
|
|
14
|
+
[x: string]: never;
|
|
15
|
+
}, {
|
|
16
|
+
[evt: string]: CustomEvent<any>;
|
|
17
|
+
}, {}, {}, string>;
|
|
18
|
+
interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
|
|
19
|
+
new (options: import("svelte").ComponentConstructorOptions<Props>): import("svelte").SvelteComponent<Props, Events, Slots> & {
|
|
20
|
+
$$bindings?: Bindings;
|
|
21
|
+
} & Exports;
|
|
22
|
+
(internal: unknown, props: {
|
|
23
|
+
$$events?: Events;
|
|
24
|
+
$$slots?: Slots;
|
|
25
|
+
}): Exports & {
|
|
26
|
+
$set?: any;
|
|
27
|
+
$on?: any;
|
|
28
|
+
};
|
|
29
|
+
z_$$bindings?: Bindings;
|
|
30
|
+
}
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
} from 'lexical';
|
|
18
18
|
import { getContext } from 'svelte';
|
|
19
19
|
import Button from '../../button/button.svelte';
|
|
20
|
+
import Checkbox from '../../checkbox/checkbox.svelte';
|
|
20
21
|
import Dialog from '../../dialog/dialog.svelte';
|
|
21
22
|
import Icon from '../../icon/icon.svelte';
|
|
22
23
|
import TextInput from '../../text-field/text-input.svelte';
|
|
@@ -48,6 +49,12 @@
|
|
|
48
49
|
let hasAnchor = $state(false);
|
|
49
50
|
let anchorURL = $state('');
|
|
50
51
|
let anchorText = $state('');
|
|
52
|
+
let openInNewTab = $state(false);
|
|
53
|
+
/**
|
|
54
|
+
* Whether the link can be set to open in a new tab. Markdown has no syntax for the link target,
|
|
55
|
+
* so it’s only available for HTML, whose source isn’t edited with the toolbar.
|
|
56
|
+
*/
|
|
57
|
+
const canSetTarget = $derived(editorStore.config.format === 'html' && editorStore.useRichText);
|
|
51
58
|
/**
|
|
52
59
|
* Whether the URL can be inserted: not empty, and safe to link to.
|
|
53
60
|
*/
|
|
@@ -168,7 +175,10 @@
|
|
|
168
175
|
});
|
|
169
176
|
|
|
170
177
|
await focusEditor(editorStore.editor);
|
|
171
|
-
editorStore.editor.dispatchCommand(
|
|
178
|
+
editorStore.editor.dispatchCommand(
|
|
179
|
+
TOGGLE_LINK_COMMAND,
|
|
180
|
+
canSetTarget && openInNewTab ? { url: anchorURL, target: '_blank' } : anchorURL,
|
|
181
|
+
);
|
|
172
182
|
} else /* v8 ignore else */ if (editorStore.editor) {
|
|
173
183
|
// The dialog leaves the focus alone (see `restoreFocus` below), so bring it back here
|
|
174
184
|
await focusEditor(editorStore.editor);
|
|
@@ -176,6 +186,7 @@
|
|
|
176
186
|
|
|
177
187
|
anchorURL = '';
|
|
178
188
|
anchorText = '';
|
|
189
|
+
openInNewTab = false;
|
|
179
190
|
};
|
|
180
191
|
|
|
181
192
|
/**
|
|
@@ -273,4 +284,9 @@
|
|
|
273
284
|
/>
|
|
274
285
|
</div>
|
|
275
286
|
{/if}
|
|
287
|
+
{#if canSetTarget}
|
|
288
|
+
<div role="none">
|
|
289
|
+
<Checkbox bind:checked={openInNewTab} label={_('_sui.text_editor.open_in_new_tab')} />
|
|
290
|
+
</div>
|
|
291
|
+
{/if}
|
|
276
292
|
</Dialog>
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Toolbar button to insert a table. It opens a dialog to choose the number of rows and columns, then
|
|
4
|
+
inserts an empty table with a header row: a table node in the rich text mode, or its Markdown in
|
|
5
|
+
the plain text mode.
|
|
6
|
+
-->
|
|
7
|
+
<script>
|
|
8
|
+
import { _ } from '@sveltia/i18n';
|
|
9
|
+
import { matchesShortcuts } from '@sveltia/utils/events';
|
|
10
|
+
import { getContext } from 'svelte';
|
|
11
|
+
import Button from '../../button/button.svelte';
|
|
12
|
+
import Dialog from '../../dialog/dialog.svelte';
|
|
13
|
+
import Icon from '../../icon/icon.svelte';
|
|
14
|
+
import NumberInput from '../../text-field/number-input.svelte';
|
|
15
|
+
import { AVAILABLE_BUTTONS, DEFAULT_TABLE_SIZE, MAX_TABLE_SIZE } from '../constants.js';
|
|
16
|
+
import { focusEditor } from '../core.js';
|
|
17
|
+
import { editRawText, insertMarkdown } from '../raw-markdown.js';
|
|
18
|
+
import { $insertTable as insertTable, createTableMarkdown, parseTableSize } from '../table.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @import { TextEditorStore } from '../../../typedefs.js';
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Number of rows, including the header row, and columns of a new table.
|
|
26
|
+
* @typedef {{ rows: number, columns: number }} TableSize
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {object} Props
|
|
31
|
+
* @property {boolean} [disabled] Whether to disable the button, like when the caret is already in
|
|
32
|
+
* a table, as tables can’t be nested.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/** @type {Props} */
|
|
36
|
+
let {
|
|
37
|
+
/* eslint-disable prefer-const */
|
|
38
|
+
disabled = false,
|
|
39
|
+
/* eslint-enable prefer-const */
|
|
40
|
+
} = $props();
|
|
41
|
+
|
|
42
|
+
const id = $props.id();
|
|
43
|
+
|
|
44
|
+
/** @type {TextEditorStore} */
|
|
45
|
+
const editorStore = getContext('editorStore');
|
|
46
|
+
|
|
47
|
+
let openDialog = $state(false);
|
|
48
|
+
/** @type {number | undefined} */
|
|
49
|
+
let rows = $state(DEFAULT_TABLE_SIZE);
|
|
50
|
+
/** @type {number | undefined} */
|
|
51
|
+
let columns = $state(DEFAULT_TABLE_SIZE);
|
|
52
|
+
|
|
53
|
+
const rowCount = $derived(parseTableSize(rows));
|
|
54
|
+
const columnCount = $derived(parseTableSize(columns));
|
|
55
|
+
const isValid = $derived(!!rowCount && !!columnCount);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Open the dialog. The size last chosen is kept, unless it was left invalid when the dialog was
|
|
59
|
+
* canceled.
|
|
60
|
+
*/
|
|
61
|
+
const onButtonClick = () => {
|
|
62
|
+
if (!rowCount) {
|
|
63
|
+
rows = DEFAULT_TABLE_SIZE;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!columnCount) {
|
|
67
|
+
columns = DEFAULT_TABLE_SIZE;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
openDialog = true;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Handle `keydown` event fired on the input fields on the dialog: insert the table with Enter.
|
|
75
|
+
* @param {KeyboardEvent} event `keydown` event.
|
|
76
|
+
*/
|
|
77
|
+
const onInputKeyDown = (event) => {
|
|
78
|
+
if (matchesShortcuts(event, 'Enter') && isValid) {
|
|
79
|
+
openDialog = false;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Insert the table into the rich text editor, then move the focus back to it, where the caret is
|
|
85
|
+
* in the first cell.
|
|
86
|
+
* @param {TableSize} size Table size.
|
|
87
|
+
*/
|
|
88
|
+
const insertRichText = async ({ rows: rowSize, columns: columnSize }) => {
|
|
89
|
+
const { editor } = editorStore;
|
|
90
|
+
|
|
91
|
+
// The button is only enabled while the editor is there
|
|
92
|
+
/* v8 ignore next 3 */
|
|
93
|
+
if (!editor) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
editor.update(() => {
|
|
98
|
+
insertTable(rowSize, columnSize);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
await focusEditor(editor);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Insert the table’s Markdown into the `<textarea>`, with the caret in the first cell.
|
|
106
|
+
* @param {TableSize} size Table size.
|
|
107
|
+
*/
|
|
108
|
+
const insertPlainText = async ({ rows: rowSize, columns: columnSize }) => {
|
|
109
|
+
const markdown = createTableMarkdown(rowSize, columnSize);
|
|
110
|
+
|
|
111
|
+
await editRawText(editorStore.textArea, (state) => {
|
|
112
|
+
const edit = insertMarkdown(state, markdown, { block: true });
|
|
113
|
+
// Right after the leading pipe and space of the first cell
|
|
114
|
+
const caret = edit.start + edit.text.indexOf(markdown) + 2;
|
|
115
|
+
|
|
116
|
+
return { ...edit, selectionStart: caret, selectionEnd: caret };
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Handle `close` event fired on the dialog. Insert the table unless the dialog has been canceled.
|
|
122
|
+
* @param {CustomEvent} event `close` event.
|
|
123
|
+
*/
|
|
124
|
+
const onDialogClose = async (event) => {
|
|
125
|
+
const canceled = event.detail.returnValue === 'cancel';
|
|
126
|
+
|
|
127
|
+
if (!canceled && rowCount && columnCount) {
|
|
128
|
+
const size = { rows: rowCount, columns: columnCount };
|
|
129
|
+
|
|
130
|
+
if (editorStore.useRichText) {
|
|
131
|
+
await insertRichText(size);
|
|
132
|
+
} else {
|
|
133
|
+
await insertPlainText(size);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The dialog leaves the focus alone (see `restoreFocus` below), so bring it back here
|
|
140
|
+
if (editorStore.useRichText) {
|
|
141
|
+
// The button is only enabled while the editor is there
|
|
142
|
+
/* v8 ignore else */
|
|
143
|
+
if (editorStore.editor) {
|
|
144
|
+
await focusEditor(editorStore.editor);
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
editorStore.textArea?.focus();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
</script>
|
|
151
|
+
|
|
152
|
+
<Button
|
|
153
|
+
iconic
|
|
154
|
+
{disabled}
|
|
155
|
+
aria-label={_(`_sui.text_editor.${AVAILABLE_BUTTONS.table.labelKey}`)}
|
|
156
|
+
aria-controls={editorStore.controlId}
|
|
157
|
+
onclick={() => {
|
|
158
|
+
onButtonClick();
|
|
159
|
+
}}
|
|
160
|
+
>
|
|
161
|
+
{#snippet startIcon()}
|
|
162
|
+
<Icon name={AVAILABLE_BUTTONS.table.icon} />
|
|
163
|
+
{/snippet}
|
|
164
|
+
</Button>
|
|
165
|
+
|
|
166
|
+
<Dialog
|
|
167
|
+
title={_('_sui.text_editor.insert_table')}
|
|
168
|
+
bind:open={openDialog}
|
|
169
|
+
okDisabled={!isValid}
|
|
170
|
+
okLabel={_('_sui.insert')}
|
|
171
|
+
restoreFocus={false}
|
|
172
|
+
onClose={(event) => {
|
|
173
|
+
onDialogClose(event);
|
|
174
|
+
}}
|
|
175
|
+
>
|
|
176
|
+
<div role="none" class="table-size">
|
|
177
|
+
<label for="{id}-rows">{_('_sui.text_editor.rows')}</label>
|
|
178
|
+
<NumberInput
|
|
179
|
+
id="{id}-rows"
|
|
180
|
+
bind:value={rows}
|
|
181
|
+
min={1}
|
|
182
|
+
max={MAX_TABLE_SIZE}
|
|
183
|
+
invalid={!rowCount}
|
|
184
|
+
aria-label={_('_sui.text_editor.rows')}
|
|
185
|
+
onkeydown={(event) => {
|
|
186
|
+
onInputKeyDown(event);
|
|
187
|
+
}}
|
|
188
|
+
/>
|
|
189
|
+
<label for="{id}-columns">{_('_sui.text_editor.columns')}</label>
|
|
190
|
+
<NumberInput
|
|
191
|
+
id="{id}-columns"
|
|
192
|
+
bind:value={columns}
|
|
193
|
+
min={1}
|
|
194
|
+
max={MAX_TABLE_SIZE}
|
|
195
|
+
invalid={!columnCount}
|
|
196
|
+
aria-label={_('_sui.text_editor.columns')}
|
|
197
|
+
onkeydown={(event) => {
|
|
198
|
+
onInputKeyDown(event);
|
|
199
|
+
}}
|
|
200
|
+
/>
|
|
201
|
+
</div>
|
|
202
|
+
</Dialog>
|
|
203
|
+
|
|
204
|
+
<style>.table-size {
|
|
205
|
+
display: grid;
|
|
206
|
+
grid-template-columns: auto 1fr;
|
|
207
|
+
align-items: center;
|
|
208
|
+
gap: 8px 16px;
|
|
209
|
+
}</style>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default InsertTableButton;
|
|
2
|
+
type InsertTableButton = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Props>): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Toolbar button to insert a table. It opens a dialog to choose the number of rows and columns, then
|
|
8
|
+
* inserts an empty table with a header row: a table node in the rich text mode, or its Markdown in
|
|
9
|
+
* the plain text mode.
|
|
10
|
+
*/
|
|
11
|
+
declare const InsertTableButton: import("svelte").Component<{
|
|
12
|
+
/**
|
|
13
|
+
* Whether to disable the button, like when the caret is already in
|
|
14
|
+
* a table, as tables can’t be nested.
|
|
15
|
+
*/
|
|
16
|
+
disabled?: boolean | undefined;
|
|
17
|
+
}, {}, "">;
|
|
18
|
+
type Props = {
|
|
19
|
+
/**
|
|
20
|
+
* Whether to disable the button, like when the caret is already in
|
|
21
|
+
* a table, as tables can’t be nested.
|
|
22
|
+
*/
|
|
23
|
+
disabled?: boolean | undefined;
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function waitForMenuToClose(item: HTMLElement, frames?: number): Promise<void>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for the menus on the text editor toolbar.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Maximum number of frames to wait for a menu to close.
|
|
7
|
+
*/
|
|
8
|
+
const MAX_CLOSE_FRAMES = 60;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wait for the menu the given item is in to close. A menu is a modal dialog, which makes the rest
|
|
12
|
+
* of the page inert while it’s open, so the editor can’t take the focus yet. Once closed, the
|
|
13
|
+
* dialog moves the focus back to the button that opened it on the next task, so wait for that as
|
|
14
|
+
* well.
|
|
15
|
+
* Call this before moving the focus back to the editor after an item is chosen.
|
|
16
|
+
* @param {HTMLElement} item Menu item chosen.
|
|
17
|
+
* @param {number} [frames] Number of frames waited so far.
|
|
18
|
+
* @returns {Promise<void>} Nothing.
|
|
19
|
+
*/
|
|
20
|
+
export const waitForMenuToClose = async (item, frames = 0) => {
|
|
21
|
+
if (item.closest('dialog')?.open && frames < MAX_CLOSE_FRAMES) {
|
|
22
|
+
await new Promise((resolve) => {
|
|
23
|
+
window.requestAnimationFrame(resolve);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return waitForMenuToClose(item, frames + 1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
window.setTimeout(resolve);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Menu of the operations on a table: inserting and deleting rows and columns, and deleting the whole
|
|
4
|
+
table. It’s opened from the toolbar or the button on the selected table cell.
|
|
5
|
+
-->
|
|
6
|
+
<script>
|
|
7
|
+
import { _ } from '@sveltia/i18n';
|
|
8
|
+
import { getContext } from 'svelte';
|
|
9
|
+
import Divider from '../../divider/divider.svelte';
|
|
10
|
+
import MenuItem from '../../menu/menu-item.svelte';
|
|
11
|
+
import Menu from '../../menu/menu.svelte';
|
|
12
|
+
import { focusEditor } from '../core.js';
|
|
13
|
+
import { isRightToLeftTable, $runTableAction as runTableAction } from '../table.js';
|
|
14
|
+
import { waitForMenuToClose } from './menu.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @import { TextEditorStore } from '../../../typedefs.js';
|
|
18
|
+
* @import { TableAction } from '../table.js';
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @typedef {object} Props
|
|
23
|
+
* @property {string} cellKey Key of the table cell to run the operations on.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** @type {Props} */
|
|
27
|
+
let {
|
|
28
|
+
/* eslint-disable prefer-const */
|
|
29
|
+
cellKey,
|
|
30
|
+
/* eslint-enable prefer-const */
|
|
31
|
+
} = $props();
|
|
32
|
+
|
|
33
|
+
/** @type {TextEditorStore} */
|
|
34
|
+
const editorStore = getContext('editorStore');
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Menu items, grouped by what they do. Each item has the operation and the key of its label.
|
|
38
|
+
* @type {[TableAction, string][][]}
|
|
39
|
+
*/
|
|
40
|
+
const ITEM_GROUPS = [
|
|
41
|
+
[
|
|
42
|
+
['insert-row-above', 'insert_row_above'],
|
|
43
|
+
['insert-row-below', 'insert_row_below'],
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
['insert-column-left', 'insert_column_left'],
|
|
47
|
+
['insert-column-right', 'insert_column_right'],
|
|
48
|
+
],
|
|
49
|
+
[
|
|
50
|
+
['delete-row', 'delete_row'],
|
|
51
|
+
['delete-column', 'delete_column'],
|
|
52
|
+
['delete-table', 'delete_table'],
|
|
53
|
+
],
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Run the given operation on the cell, then move the focus back to the editor.
|
|
58
|
+
* @param {TableAction} action Operation.
|
|
59
|
+
* @param {HTMLElement} item Menu item chosen.
|
|
60
|
+
*/
|
|
61
|
+
const runAction = async (action, item) => {
|
|
62
|
+
const { editor } = editorStore;
|
|
63
|
+
|
|
64
|
+
// The menu is only shown for a cell in the editor
|
|
65
|
+
/* v8 ignore next 3 */
|
|
66
|
+
if (!editor) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const cell = editor.getElementByKey(cellKey);
|
|
71
|
+
const rtl = !!cell && isRightToLeftTable(cell);
|
|
72
|
+
|
|
73
|
+
editor.update(() => {
|
|
74
|
+
runTableAction(action, cellKey, { rtl });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await waitForMenuToClose(item);
|
|
78
|
+
await focusEditor(editor);
|
|
79
|
+
};
|
|
80
|
+
</script>
|
|
81
|
+
|
|
82
|
+
<Menu ariaLabel={_('_sui.text_editor.table_options')}>
|
|
83
|
+
{#each ITEM_GROUPS as items, index (index)}
|
|
84
|
+
{#if index > 0}
|
|
85
|
+
<Divider />
|
|
86
|
+
{/if}
|
|
87
|
+
{#each items as [action, labelKey] (action)}
|
|
88
|
+
<MenuItem
|
|
89
|
+
label={_(`_sui.text_editor.${labelKey}`)}
|
|
90
|
+
onclick={(/** @type {MouseEvent} */ event) => {
|
|
91
|
+
runAction(action, /** @type {HTMLElement} */ (event.currentTarget));
|
|
92
|
+
}}
|
|
93
|
+
/>
|
|
94
|
+
{/each}
|
|
95
|
+
{/each}
|
|
96
|
+
</Menu>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default TableActionMenu;
|
|
2
|
+
type TableActionMenu = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Props>): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Menu of the operations on a table: inserting and deleting rows and columns, and deleting the whole
|
|
8
|
+
* table. It’s opened from the toolbar or the button on the selected table cell.
|
|
9
|
+
*/
|
|
10
|
+
declare const TableActionMenu: import("svelte").Component<{
|
|
11
|
+
/**
|
|
12
|
+
* Key of the table cell to run the operations on.
|
|
13
|
+
*/
|
|
14
|
+
cellKey: string;
|
|
15
|
+
}, {}, "">;
|
|
16
|
+
type Props = {
|
|
17
|
+
/**
|
|
18
|
+
* Key of the table cell to run the operations on.
|
|
19
|
+
*/
|
|
20
|
+
cellKey: string;
|
|
21
|
+
};
|
|
@@ -11,10 +11,14 @@
|
|
|
11
11
|
import Menu from '../../menu/menu.svelte';
|
|
12
12
|
import { AVAILABLE_BUTTONS, BLOCK_BUTTON_TYPES, INLINE_BUTTON_TYPES } from '../constants.js';
|
|
13
13
|
import CodeLanguageSwitcher from './code-language-switcher.svelte';
|
|
14
|
+
import AlignMenuButton from './align-menu-button.svelte';
|
|
14
15
|
import FormatTextButton from './format-text-button.svelte';
|
|
16
|
+
import InsertHorizontalRuleButton from './insert-horizontal-rule-button.svelte';
|
|
15
17
|
import InsertItemButton from './insert-item-button.svelte';
|
|
16
18
|
import InsertLinkButton from './insert-link-button.svelte';
|
|
17
19
|
import InsertMenuButton from './insert-menu-button.svelte';
|
|
20
|
+
import InsertTableButton from './insert-table-button.svelte';
|
|
21
|
+
import TableActionMenu from './table-action-menu.svelte';
|
|
18
22
|
import ToggleBlockMenuItem from './toggle-block-menu-item.svelte';
|
|
19
23
|
import ToolbarWrapper from './toolbar-wrapper.svelte';
|
|
20
24
|
|
|
@@ -71,6 +75,41 @@
|
|
|
71
75
|
*/
|
|
72
76
|
const canFormat = $derived(editorStore.useRichText || !isHtml);
|
|
73
77
|
|
|
78
|
+
const hasTable = $derived(editorStore.config.enabledButtons.includes('table'));
|
|
79
|
+
/**
|
|
80
|
+
* Whether the alignment menu is available. Markdown has no syntax for alignment, so it’s only
|
|
81
|
+
* available for HTML, and only in the rich text mode, as the plain text mode shows the source.
|
|
82
|
+
*/
|
|
83
|
+
const canAlign = $derived(
|
|
84
|
+
isHtml && editorStore.useRichText && editorStore.config.enabledButtons.includes('align'),
|
|
85
|
+
);
|
|
86
|
+
const hasHorizontalRule = $derived(editorStore.config.enabledButtons.includes('horizontal-rule'));
|
|
87
|
+
/**
|
|
88
|
+
* Key of the table cell the caret is in, if any. The table menu only works with the rich text
|
|
89
|
+
* editor.
|
|
90
|
+
*/
|
|
91
|
+
const tableCellKey = $derived(
|
|
92
|
+
editorStore.useRichText ? (editorStore.selection.tableCellKey ?? null) : null,
|
|
93
|
+
);
|
|
94
|
+
/**
|
|
95
|
+
* Whether the caret is in a Markdown table cell, which can only have inline content, so neither
|
|
96
|
+
* a horizontal rule nor an editor component can be inserted. A cell is also exported with the
|
|
97
|
+
* standard Markdown syntax only, so an editor component would be lost.
|
|
98
|
+
*/
|
|
99
|
+
const inMarkdownTable = $derived(!!tableCellKey && !isHtml);
|
|
100
|
+
/**
|
|
101
|
+
* Whether any of the buttons to insert a table, a horizontal rule or an editor component is
|
|
102
|
+
* shown.
|
|
103
|
+
*/
|
|
104
|
+
const hasInsertButtons = $derived(
|
|
105
|
+
hasTable ||
|
|
106
|
+
(!inMarkdownTable && (hasHorizontalRule || editorStore.config.components.length > 0)),
|
|
107
|
+
);
|
|
108
|
+
/**
|
|
109
|
+
* Whether the block level styles are available.
|
|
110
|
+
*/
|
|
111
|
+
const canChangeBlock = $derived(canFormat && !inMarkdownTable);
|
|
112
|
+
|
|
74
113
|
/**
|
|
75
114
|
* Enabled inline level buttons.
|
|
76
115
|
* @type {TextEditorInlineType[]}
|
|
@@ -85,7 +124,7 @@
|
|
|
85
124
|
</script>
|
|
86
125
|
|
|
87
126
|
<ToolbarWrapper disabled={disabled || readonly} ariaLabel={_('_sui.text_editor.text_editor')}>
|
|
88
|
-
{#if
|
|
127
|
+
{#if canChangeBlock && blockLevelButtons.length > 1}
|
|
89
128
|
<MenuButton
|
|
90
129
|
aria-label={_('_sui.text_editor.show_text_style_options')}
|
|
91
130
|
aria-controls={editorStore.controlId}
|
|
@@ -109,6 +148,22 @@
|
|
|
109
148
|
<Divider orientation="vertical" />
|
|
110
149
|
<CodeLanguageSwitcher />
|
|
111
150
|
{:else if canFormat}
|
|
151
|
+
{#if hasTable && tableCellKey}
|
|
152
|
+
{#if canChangeBlock && blockLevelButtons.length > 1}
|
|
153
|
+
<Divider orientation="vertical" />
|
|
154
|
+
{/if}
|
|
155
|
+
<MenuButton
|
|
156
|
+
aria-label={_('_sui.text_editor.show_table_options')}
|
|
157
|
+
aria-controls={editorStore.controlId}
|
|
158
|
+
>
|
|
159
|
+
{#snippet startIcon()}
|
|
160
|
+
<Icon name="table_edit" />
|
|
161
|
+
{/snippet}
|
|
162
|
+
{#snippet popup()}
|
|
163
|
+
<TableActionMenu cellKey={tableCellKey} />
|
|
164
|
+
{/snippet}
|
|
165
|
+
</MenuButton>
|
|
166
|
+
{/if}
|
|
112
167
|
{#if inlineLevelButtons.length}
|
|
113
168
|
<Divider orientation="vertical" />
|
|
114
169
|
<ButtonGroup>
|
|
@@ -121,13 +176,26 @@
|
|
|
121
176
|
{/each}
|
|
122
177
|
</ButtonGroup>
|
|
123
178
|
{/if}
|
|
124
|
-
{#if
|
|
179
|
+
{#if canAlign}
|
|
180
|
+
<Divider orientation="vertical" />
|
|
181
|
+
<AlignMenuButton />
|
|
182
|
+
{/if}
|
|
183
|
+
{#if hasInsertButtons}
|
|
125
184
|
<Divider orientation="vertical" />
|
|
126
|
-
{#
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
{
|
|
130
|
-
|
|
185
|
+
{#if hasTable}
|
|
186
|
+
<!-- Tables can’t be nested -->
|
|
187
|
+
<InsertTableButton disabled={!!tableCellKey} />
|
|
188
|
+
{/if}
|
|
189
|
+
{#if !inMarkdownTable}
|
|
190
|
+
{#if hasHorizontalRule}
|
|
191
|
+
<InsertHorizontalRuleButton />
|
|
192
|
+
{/if}
|
|
193
|
+
{#each buttons as button (button.id)}
|
|
194
|
+
<InsertItemButton component={button} />
|
|
195
|
+
{/each}
|
|
196
|
+
{#if menuitems.length}
|
|
197
|
+
<InsertMenuButton components={menuitems} />
|
|
198
|
+
{/if}
|
|
131
199
|
{/if}
|
|
132
200
|
{/if}
|
|
133
201
|
{/if}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @import { ElementTransformer } from '@lexical/markdown';
|
|
3
3
|
*/
|
|
4
|
+
/**
|
|
5
|
+
* Markdown of a horizontal rule. Unlike `---`, it can’t be taken for the underline of a setext
|
|
6
|
+
* heading or the delimiter of front matter.
|
|
7
|
+
*/
|
|
8
|
+
export const HORIZONTAL_RULE_MARKDOWN: "***";
|
|
4
9
|
/**
|
|
5
10
|
* @type {ElementTransformer}
|
|
6
11
|
*/
|
|
@@ -12,12 +12,18 @@ import {
|
|
|
12
12
|
* @import { ElementTransformer } from '@lexical/markdown';
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Markdown of a horizontal rule. Unlike `---`, it can’t be taken for the underline of a setext
|
|
17
|
+
* heading or the delimiter of front matter.
|
|
18
|
+
*/
|
|
19
|
+
export const HORIZONTAL_RULE_MARKDOWN = '***';
|
|
20
|
+
|
|
15
21
|
/**
|
|
16
22
|
* @type {ElementTransformer}
|
|
17
23
|
*/
|
|
18
24
|
export const HR = {
|
|
19
25
|
dependencies: [HorizontalRuleNode],
|
|
20
|
-
export: (node) => (isHorizontalRuleNode(node) ?
|
|
26
|
+
export: (node) => (isHorizontalRuleNode(node) ? HORIZONTAL_RULE_MARKDOWN : null),
|
|
21
27
|
regExp: /^(---|\*\*\*|___)\s?$/,
|
|
22
28
|
replace: (parentNode, _1, _2, isImport) => {
|
|
23
29
|
const line = createHorizontalRuleNode();
|