@sveltia/ui 0.81.3 → 0.83.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/grid/grid-col-header.svelte +121 -2
- package/dist/components/grid/grid-col-header.svelte.d.ts +35 -1
- package/dist/components/grid/grid-foot.svelte +5 -0
- package/dist/components/grid/grid-head.svelte +4 -0
- package/dist/components/grid/grid-row.svelte +55 -2
- package/dist/components/grid/grid-row.svelte.d.ts +4 -2
- package/dist/components/grid/grid.svelte +5 -0
- 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/group.svelte.js +6 -1
- package/dist/typedefs.d.ts +10 -1
- package/dist/typedefs.js +5 -1
- package/package.json +4 -4
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Table editing helpers for the rich text editor: inserting a table, and the row and column
|
|
3
|
+
* operations offered by the table menu. Functions starting with `$` must be called within an editor
|
|
4
|
+
* update.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
$createTableNodeWithDimensions as createTableNodeWithDimensions,
|
|
9
|
+
$deleteTableColumnAtSelection as deleteTableColumnAtSelection,
|
|
10
|
+
$deleteTableRowAtSelection as deleteTableRowAtSelection,
|
|
11
|
+
$findTableNode as findTableNode,
|
|
12
|
+
$insertTableColumnAtSelection as insertTableColumnAtSelection,
|
|
13
|
+
$insertTableRowAtSelection as insertTableRowAtSelection,
|
|
14
|
+
$isTableCellNode as isTableCellNode,
|
|
15
|
+
$isTableRowNode as isTableRowNode,
|
|
16
|
+
$isTableSelection as isTableSelection,
|
|
17
|
+
TableCellHeaderStates,
|
|
18
|
+
} from '@lexical/table';
|
|
19
|
+
import { $findMatchingParent as findMatchingParent } from '@lexical/utils';
|
|
20
|
+
import {
|
|
21
|
+
$createParagraphNode as createParagraphNode,
|
|
22
|
+
$getNodeByKey as getNodeByKey,
|
|
23
|
+
$getPreviousSelection as getPreviousSelection,
|
|
24
|
+
$getSelection as getSelection,
|
|
25
|
+
$isElementNode as isElementNode,
|
|
26
|
+
$isRangeSelection as isRangeSelection,
|
|
27
|
+
$isRootNode as isRootNode,
|
|
28
|
+
$setSelection as setSelection,
|
|
29
|
+
} from 'lexical';
|
|
30
|
+
import { $insertBlockNode } from './block.js';
|
|
31
|
+
import { MAX_TABLE_SIZE } from './constants.js';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @import { LexicalNode, RangeSelection } from 'lexical';
|
|
35
|
+
* @import { TableCellNode, TableNode, TableRowNode, TableSelection } from '@lexical/table';
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Operation offered by the table menu.
|
|
40
|
+
* @typedef {'insert-row-above' | 'insert-row-below' | 'insert-column-left' |
|
|
41
|
+
* 'insert-column-right' | 'delete-row' | 'delete-column' | 'delete-table'} TableAction
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the number of rows or columns for a new table from the given input, which can be anything
|
|
46
|
+
* the user has entered: a whole number from 1 to {@link MAX_TABLE_SIZE}.
|
|
47
|
+
* @param {unknown} value Input value.
|
|
48
|
+
* @returns {number | undefined} Number, or `undefined` if the value is not a valid size.
|
|
49
|
+
*/
|
|
50
|
+
export const parseTableSize = (value) => {
|
|
51
|
+
const number = Number(value);
|
|
52
|
+
|
|
53
|
+
return Number.isInteger(number) && number >= 1 && number <= MAX_TABLE_SIZE ? number : undefined;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create the Markdown of a new empty table with a header row, to insert it in the plain text mode.
|
|
58
|
+
* The cells are written the same way the rich text editor exports them, so switching the mode
|
|
59
|
+
* doesn’t change the value.
|
|
60
|
+
* @param {number} rows Number of rows, including the header row.
|
|
61
|
+
* @param {number} columns Number of columns.
|
|
62
|
+
* @returns {string} Markdown.
|
|
63
|
+
*/
|
|
64
|
+
export const createTableMarkdown = (rows, columns) => {
|
|
65
|
+
const row = `| ${Array(columns).fill('').join(' | ')} |`;
|
|
66
|
+
const divider = `| ${Array(columns).fill('---').join(' | ')} |`;
|
|
67
|
+
|
|
68
|
+
return [row, divider, ...Array(rows - 1).fill(row)].join('\n');
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check if the table the given cell element is in is laid out from right to left. The columns
|
|
73
|
+
* follow the direction of the table, while each cell can have a direction of its own.
|
|
74
|
+
* @param {HTMLElement} cell Cell element.
|
|
75
|
+
* @returns {boolean} Result.
|
|
76
|
+
*/
|
|
77
|
+
export const isRightToLeftTable = (cell) => {
|
|
78
|
+
const table = cell.closest('table');
|
|
79
|
+
|
|
80
|
+
return !!table && getComputedStyle(table).direction === 'rtl';
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get the table cell that contains the given node.
|
|
85
|
+
* @param {LexicalNode | null | undefined} node Node.
|
|
86
|
+
* @returns {TableCellNode | null} Cell, if any.
|
|
87
|
+
*/
|
|
88
|
+
export const $getTableCell = (node) =>
|
|
89
|
+
node ? /** @type {TableCellNode | null} */ (findMatchingParent(node, isTableCellNode)) : null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get the key of the table cell the selection’s focus is in. The focus is where the user has moved
|
|
93
|
+
* the caret last, including when they have selected several cells.
|
|
94
|
+
* @returns {string | null} Key, or `null` if the selection is not in a table.
|
|
95
|
+
*/
|
|
96
|
+
export const $getSelectedTableCellKey = () => {
|
|
97
|
+
const selection = getSelection();
|
|
98
|
+
|
|
99
|
+
if (!isRangeSelection(selection) && !isTableSelection(selection)) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return $getTableCell(selection.focus.getNode())?.getKey() ?? null;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Insert a new empty table with a header row at the selection, and move the caret to its first
|
|
108
|
+
* cell. A table can’t be inserted into another table, as neither Markdown nor the editor supports
|
|
109
|
+
* nested tables.
|
|
110
|
+
* @param {number} rows Number of rows, including the header row.
|
|
111
|
+
* @param {number} columns Number of columns.
|
|
112
|
+
* @returns {boolean} Whether the table has been inserted.
|
|
113
|
+
*/
|
|
114
|
+
export const $insertTable = (rows, columns) => {
|
|
115
|
+
const selection = getSelection() ?? getPreviousSelection();
|
|
116
|
+
|
|
117
|
+
if (
|
|
118
|
+
(isRangeSelection(selection) || isTableSelection(selection)) &&
|
|
119
|
+
findTableNode(selection.anchor.getNode())
|
|
120
|
+
) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const table = $insertBlockNode(
|
|
125
|
+
createTableNodeWithDimensions(rows, columns, { rows: true, columns: false }),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// The table has at least one row and one column
|
|
129
|
+
/** @type {TableRowNode} */ (table.getFirstChildOrThrow()).getFirstChildOrThrow().selectStart();
|
|
130
|
+
|
|
131
|
+
return true;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Add an empty paragraph after the given table if it’s the last block of the document. The caret
|
|
136
|
+
* can’t otherwise be moved below the table, so nothing could be added after it. An empty paragraph
|
|
137
|
+
* at the end is left out of the Markdown and HTML values, so this doesn’t change them.
|
|
138
|
+
* @param {TableNode} table Table.
|
|
139
|
+
*/
|
|
140
|
+
export const $ensureParagraphAfterTable = (table) => {
|
|
141
|
+
if (!table.getNextSibling() && isRootNode(table.getParent())) {
|
|
142
|
+
table.insertAfter(createParagraphNode());
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Make the first row of the given table the header row, and the other rows plain rows. A Markdown
|
|
148
|
+
* table always has exactly one header row on top, so this keeps the table in the editor in line
|
|
149
|
+
* with its Markdown, even after a row has been inserted above the header or the header removed.
|
|
150
|
+
* @param {TableNode} table Table.
|
|
151
|
+
*/
|
|
152
|
+
export const $normalizeTableHeaders = (table) => {
|
|
153
|
+
table.getChildren().forEach((row, rowIndex) => {
|
|
154
|
+
// The table transform of Lexical removes anything but rows and cells
|
|
155
|
+
/* v8 ignore next 3 */
|
|
156
|
+
if (!isTableRowNode(row)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const headerState =
|
|
161
|
+
rowIndex === 0 ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS;
|
|
162
|
+
|
|
163
|
+
row.getChildren().forEach((cell) => {
|
|
164
|
+
if (isTableCellNode(cell) && cell.getHeaderStyles() !== headerState) {
|
|
165
|
+
cell.setHeaderStyles(headerState);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Move the caret out of the given table, which is about to be removed: to the end of the block
|
|
173
|
+
* before it, the start of the block after it, or a new paragraph if there’s neither to type in.
|
|
174
|
+
* @param {TableNode} table Table.
|
|
175
|
+
*/
|
|
176
|
+
const $moveCaretOutOfTable = (table) => {
|
|
177
|
+
const previous = table.getPreviousSibling();
|
|
178
|
+
const next = table.getNextSibling();
|
|
179
|
+
|
|
180
|
+
if (isElementNode(previous)) {
|
|
181
|
+
previous.selectEnd();
|
|
182
|
+
} else if (isElementNode(next)) {
|
|
183
|
+
next.selectStart();
|
|
184
|
+
} else {
|
|
185
|
+
table.insertAfter(createParagraphNode()).selectStart();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Check if the given operation deletes rows or columns.
|
|
191
|
+
* @param {TableAction} action Operation.
|
|
192
|
+
* @returns {action is 'delete-row' | 'delete-column'} Result.
|
|
193
|
+
*/
|
|
194
|
+
const isDeleteAction = (action) => action === 'delete-row' || action === 'delete-column';
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Check if the selection spans all the rows or all the columns of the given table, depending on the
|
|
198
|
+
* action, in which case deleting them deletes the whole table. Merged cells are disabled, so the
|
|
199
|
+
* table is a plain grid, where the index of a row or cell tells its position.
|
|
200
|
+
* @param {TableNode} table Table the selection is in.
|
|
201
|
+
* @param {'delete-row' | 'delete-column'} action Operation.
|
|
202
|
+
* @returns {boolean} Result.
|
|
203
|
+
*/
|
|
204
|
+
const $selectionSpansTable = (table, action) => {
|
|
205
|
+
const selection = /** @type {RangeSelection | TableSelection} */ (getSelection());
|
|
206
|
+
|
|
207
|
+
const cells = /** @type {TableCellNode[]} */ (
|
|
208
|
+
[selection.anchor, selection.focus].map((point) => $getTableCell(point.getNode()))
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const [start, end] = cells
|
|
212
|
+
.map((cell) =>
|
|
213
|
+
action === 'delete-row'
|
|
214
|
+
? cell.getParentOrThrow().getIndexWithinParent()
|
|
215
|
+
: cell.getIndexWithinParent(),
|
|
216
|
+
)
|
|
217
|
+
.sort((a, b) => a - b);
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
end - start + 1 === (action === 'delete-row' ? table.getChildrenSize() : table.getColumnCount())
|
|
221
|
+
);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Run the given table menu operation on the cell with the given key. If the selection is already
|
|
226
|
+
* in the same table, it’s kept, so an operation applies to all the selected rows or columns.
|
|
227
|
+
* Otherwise, the caret is moved to the cell first.
|
|
228
|
+
* @param {TableAction} action Operation.
|
|
229
|
+
* @param {string} cellKey Key of the cell the menu has been opened for.
|
|
230
|
+
* @param {object} [options] Options.
|
|
231
|
+
* @param {boolean} [options.rtl] Whether the table is laid out from right to left, in which case
|
|
232
|
+
* the left column comes after the cell rather than before it.
|
|
233
|
+
* @returns {boolean} Whether the operation has been run. It’s not if the cell is gone.
|
|
234
|
+
*/
|
|
235
|
+
export const $runTableAction = (action, cellKey, { rtl = false } = {}) => {
|
|
236
|
+
const cell = getNodeByKey(cellKey);
|
|
237
|
+
|
|
238
|
+
if (!isTableCellNode(cell)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const table = /** @type {TableNode} */ (findTableNode(cell));
|
|
243
|
+
const selection = getSelection() ?? getPreviousSelection();
|
|
244
|
+
|
|
245
|
+
if (
|
|
246
|
+
(isRangeSelection(selection) || isTableSelection(selection)) &&
|
|
247
|
+
findTableNode(selection.anchor.getNode())?.is(table) &&
|
|
248
|
+
findTableNode(selection.focus.getNode())?.is(table)
|
|
249
|
+
) {
|
|
250
|
+
if (!getSelection()) {
|
|
251
|
+
setSelection(selection.clone());
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
cell.selectStart();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (
|
|
258
|
+
action === 'delete-table' ||
|
|
259
|
+
(isDeleteAction(action) && $selectionSpansTable(table, action))
|
|
260
|
+
) {
|
|
261
|
+
// Deleting every row or column deletes the table, which Lexical does by moving the caret to
|
|
262
|
+
// the previous node, even if that’s a decorator or there’s none, where the user can’t type
|
|
263
|
+
$moveCaretOutOfTable(table);
|
|
264
|
+
table.remove();
|
|
265
|
+
} else if (action === 'delete-row') {
|
|
266
|
+
deleteTableRowAtSelection();
|
|
267
|
+
} else if (action === 'delete-column') {
|
|
268
|
+
deleteTableColumnAtSelection();
|
|
269
|
+
} else if (action === 'insert-row-above' || action === 'insert-row-below') {
|
|
270
|
+
insertTableRowAtSelection(action === 'insert-row-below');
|
|
271
|
+
} else {
|
|
272
|
+
// In a right-to-left table, the left column comes after the cell
|
|
273
|
+
insertTableColumnAtSelection((action === 'insert-column-right') !== rtl);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return true;
|
|
277
|
+
};
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import EmojiAutocomplete from './emoji-autocomplete.svelte';
|
|
14
14
|
import FloatingLinkEditor from './floating-link-editor.svelte';
|
|
15
15
|
import LexicalRoot from './lexical-root.svelte';
|
|
16
|
+
import TableCellMenuButton from './table-cell-menu-button.svelte';
|
|
16
17
|
import { getBlockType, getRawTextState } from './raw-markdown.js';
|
|
17
18
|
import { highlightCodeToTokens } from './shiki/facade.js';
|
|
18
19
|
import { getCodeTheme, onCodeThemeChange } from './shiki/theme.js';
|
|
@@ -336,6 +337,9 @@
|
|
|
336
337
|
{#if editorStore.config.enabledButtons.includes('link') && !disabled && !readonly}
|
|
337
338
|
<FloatingLinkEditor />
|
|
338
339
|
{/if}
|
|
340
|
+
{#if editorStore.config.enabledButtons.includes('table') && !disabled && !readonly}
|
|
341
|
+
<TableCellMenuButton />
|
|
342
|
+
{/if}
|
|
339
343
|
</div>
|
|
340
344
|
|
|
341
345
|
{#if editorStore.showConverterError}
|
|
@@ -345,6 +349,7 @@
|
|
|
345
349
|
{/if}
|
|
346
350
|
|
|
347
351
|
<style>.text-editor {
|
|
352
|
+
position: relative;
|
|
348
353
|
margin: var(--sui-focus-ring-width);
|
|
349
354
|
border-radius: var(--sui-textbox-border-radius);
|
|
350
355
|
width: calc(100% - var(--sui-focus-ring-width) * 2);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Toolbar menu button to change the text alignment of the selected blocks, showing the current
|
|
4
|
+
alignment. It’s only available for HTML, as Markdown has no syntax for alignment.
|
|
5
|
+
-->
|
|
6
|
+
<script>
|
|
7
|
+
import { _ } from '@sveltia/i18n';
|
|
8
|
+
import { FORMAT_ELEMENT_COMMAND } from 'lexical';
|
|
9
|
+
import { getContext } from 'svelte';
|
|
10
|
+
import Icon from '../../icon/icon.svelte';
|
|
11
|
+
import MenuButton from '../../menu/menu-button.svelte';
|
|
12
|
+
import MenuItemRadio from '../../menu/menu-item-radio.svelte';
|
|
13
|
+
import Menu from '../../menu/menu.svelte';
|
|
14
|
+
import { ALIGNMENT_OPTIONS } from '../constants.js';
|
|
15
|
+
import { focusEditor } from '../core.js';
|
|
16
|
+
import { waitForMenuToClose } from './menu.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @import { TextEditorStore } from '../../../typedefs.js';
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** @type {TextEditorStore} */
|
|
23
|
+
const editorStore = getContext('editorStore');
|
|
24
|
+
|
|
25
|
+
const alignment = $derived(editorStore.selection.alignment ?? '');
|
|
26
|
+
/**
|
|
27
|
+
* Icon of the current alignment. The default alignment, as well as one the menu doesn’t offer,
|
|
28
|
+
* like `start`, is shown as left aligned.
|
|
29
|
+
*/
|
|
30
|
+
const icon = $derived(
|
|
31
|
+
ALIGNMENT_OPTIONS.find(({ type }) => type === alignment)?.icon ?? 'format_align_left',
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Align the selected blocks, then move the focus back to the editor.
|
|
36
|
+
* @param {(typeof ALIGNMENT_OPTIONS)[number]['type']} type Alignment.
|
|
37
|
+
* @param {HTMLElement} item Menu item chosen.
|
|
38
|
+
*/
|
|
39
|
+
const align = async (type, item) => {
|
|
40
|
+
const { editor } = editorStore;
|
|
41
|
+
|
|
42
|
+
// The menu is only shown in the rich text mode, where the editor is there
|
|
43
|
+
/* v8 ignore next 3 */
|
|
44
|
+
if (!editor) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, type);
|
|
49
|
+
await waitForMenuToClose(item);
|
|
50
|
+
await focusEditor(editor);
|
|
51
|
+
};
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<MenuButton
|
|
55
|
+
aria-label={_('_sui.text_editor.show_alignment_options')}
|
|
56
|
+
aria-controls={editorStore.controlId}
|
|
57
|
+
>
|
|
58
|
+
{#snippet startIcon()}
|
|
59
|
+
<Icon name={icon} />
|
|
60
|
+
{/snippet}
|
|
61
|
+
{#snippet popup()}
|
|
62
|
+
<Menu ariaLabel={_('_sui.text_editor.alignment_options')}>
|
|
63
|
+
<!--
|
|
64
|
+
The menu checks the item chosen on its own, which is wrong if nothing could be aligned, like
|
|
65
|
+
when a horizontal rule is selected, so start over whenever the editor reports the selection
|
|
66
|
+
-->
|
|
67
|
+
{#key editorStore.selection}
|
|
68
|
+
{#each ALIGNMENT_OPTIONS as option (option.type)}
|
|
69
|
+
<MenuItemRadio
|
|
70
|
+
label={_(`_sui.text_editor.${option.labelKey}`)}
|
|
71
|
+
checked={alignment === option.type}
|
|
72
|
+
onclick={(/** @type {MouseEvent} */ event) => {
|
|
73
|
+
align(option.type, /** @type {HTMLElement} */ (event.currentTarget));
|
|
74
|
+
}}
|
|
75
|
+
>
|
|
76
|
+
{#snippet startIcon()}
|
|
77
|
+
<Icon name={option.icon} />
|
|
78
|
+
{/snippet}
|
|
79
|
+
</MenuItemRadio>
|
|
80
|
+
{/each}
|
|
81
|
+
{/key}
|
|
82
|
+
</Menu>
|
|
83
|
+
{/snippet}
|
|
84
|
+
</MenuButton>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default AlignMenuButton;
|
|
2
|
+
type AlignMenuButton = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Record<string, never>>): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Toolbar menu button to change the text alignment of the selected blocks, showing the current
|
|
8
|
+
* alignment. It’s only available for HTML, as Markdown has no syntax for alignment.
|
|
9
|
+
*/
|
|
10
|
+
declare const AlignMenuButton: import("svelte").Component<Record<string, never>, {}, "">;
|
|
@@ -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>
|