@sveltia/ui 0.81.3 → 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.
Files changed (68) hide show
  1. package/dist/components/drawer/drawer.svelte.d.ts +2 -2
  2. package/dist/components/text-editor/alignment.d.ts +2 -0
  3. package/dist/components/text-editor/alignment.js +40 -0
  4. package/dist/components/text-editor/block.d.ts +4 -0
  5. package/dist/components/text-editor/block.js +65 -0
  6. package/dist/components/text-editor/constants.d.ts +22 -2
  7. package/dist/components/text-editor/constants.js +40 -1
  8. package/dist/components/text-editor/core.d.ts +7 -0
  9. package/dist/components/text-editor/core.js +61 -7
  10. package/dist/components/text-editor/floating-link-editor.svelte +58 -6
  11. package/dist/components/text-editor/html.js +21 -4
  12. package/dist/components/text-editor/lexical-root.svelte +27 -0
  13. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  14. package/dist/components/text-editor/shiki/version.js +1 -1
  15. package/dist/components/text-editor/table-cell-menu-button.svelte +228 -0
  16. package/dist/components/text-editor/table-cell-menu-button.svelte.d.ts +12 -0
  17. package/dist/components/text-editor/table.d.ts +18 -0
  18. package/dist/components/text-editor/table.js +277 -0
  19. package/dist/components/text-editor/text-editor.svelte +5 -0
  20. package/dist/components/text-editor/toolbar/align-menu-button.svelte +84 -0
  21. package/dist/components/text-editor/toolbar/align-menu-button.svelte.d.ts +10 -0
  22. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +65 -0
  23. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte.d.ts +30 -0
  24. package/dist/components/text-editor/toolbar/insert-link-button.svelte +17 -1
  25. package/dist/components/text-editor/toolbar/insert-table-button.svelte +209 -0
  26. package/dist/components/text-editor/toolbar/insert-table-button.svelte.d.ts +24 -0
  27. package/dist/components/text-editor/toolbar/menu.d.ts +1 -0
  28. package/dist/components/text-editor/toolbar/menu.js +32 -0
  29. package/dist/components/text-editor/toolbar/table-action-menu.svelte +96 -0
  30. package/dist/components/text-editor/toolbar/table-action-menu.svelte.d.ts +21 -0
  31. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +75 -7
  32. package/dist/components/text-editor/transformers/hr.d.ts +5 -0
  33. package/dist/components/text-editor/transformers/hr.js +7 -1
  34. package/dist/components/text-editor/transformers/table.js +3 -2
  35. package/dist/locales/ar.yaml +28 -0
  36. package/dist/locales/bg.yaml +28 -0
  37. package/dist/locales/ca.yaml +28 -0
  38. package/dist/locales/cs.yaml +28 -0
  39. package/dist/locales/da.yaml +28 -0
  40. package/dist/locales/de.yaml +28 -0
  41. package/dist/locales/el.yaml +28 -0
  42. package/dist/locales/en-CA.yaml +28 -0
  43. package/dist/locales/en-GB.yaml +28 -0
  44. package/dist/locales/en-US.yaml +28 -0
  45. package/dist/locales/es-CO.yaml +28 -0
  46. package/dist/locales/fa.yaml +28 -0
  47. package/dist/locales/fi.yaml +28 -0
  48. package/dist/locales/fr.yaml +28 -0
  49. package/dist/locales/he.yaml +28 -0
  50. package/dist/locales/hr.yaml +28 -0
  51. package/dist/locales/it.yaml +28 -0
  52. package/dist/locales/ja.yaml +28 -0
  53. package/dist/locales/ko.yaml +28 -0
  54. package/dist/locales/nl.yaml +28 -0
  55. package/dist/locales/pl.yaml +28 -0
  56. package/dist/locales/pt-BR.yaml +28 -0
  57. package/dist/locales/pt-PT.yaml +28 -0
  58. package/dist/locales/ru.yaml +28 -0
  59. package/dist/locales/sk.yaml +28 -0
  60. package/dist/locales/sv.yaml +28 -0
  61. package/dist/locales/tr.yaml +28 -0
  62. package/dist/locales/uk.yaml +28 -0
  63. package/dist/locales/vi.yaml +28 -0
  64. package/dist/locales/zh-CN.yaml +28 -0
  65. package/dist/locales/zh-TW.yaml +28 -0
  66. package/dist/typedefs.d.ts +10 -1
  67. package/dist/typedefs.js +5 -1
  68. package/package.json +1 -1
@@ -0,0 +1,228 @@
1
+ <!--
2
+ @component
3
+ Button on the table cell where the caret is, at its top end corner, that opens the table menu to
4
+ insert and delete rows and columns, the same way it works in the Lexical Playground. The same menu
5
+ is also available on the toolbar, which is easier to reach with the keyboard.
6
+ @see https://playground.lexical.dev/
7
+ -->
8
+ <script>
9
+ import { _ } from '@sveltia/i18n';
10
+ import { getContext, onMount } from 'svelte';
11
+ import Icon from '../icon/icon.svelte';
12
+ import MenuButton from '../menu/menu-button.svelte';
13
+ import {
14
+ $getSelectedTableCellKey as getSelectedTableCellKey,
15
+ isRightToLeftTable,
16
+ } from './table.js';
17
+ import TableActionMenu from './toolbar/table-action-menu.svelte';
18
+
19
+ /**
20
+ * @import { PopupPosition, TextEditorStore } from '../../typedefs.js';
21
+ */
22
+
23
+ /**
24
+ * Space between the button and the cell border.
25
+ */
26
+ const OFFSET = 4;
27
+
28
+ /** @type {TextEditorStore} */
29
+ const editorStore = getContext('editorStore');
30
+
31
+ /**
32
+ * Key of the table cell the selection is in, if any.
33
+ * @type {string | null}
34
+ */
35
+ let cellKey = $state(null);
36
+ /**
37
+ * Whether the focus is within the editor, or on the menu opened from the button. The button is
38
+ * hidden otherwise, so it doesn’t linger once the user has moved on.
39
+ */
40
+ let hasFocus = $state(false);
41
+ /**
42
+ * Position of the button relative to the editor wrapper: the top end corner of the cell.
43
+ * @type {{ top: number, left: number, rtl: boolean, popupPosition: PopupPosition } |
44
+ * undefined}
45
+ */
46
+ let position = $state.raw();
47
+ /**
48
+ * A reference to the element positioning the button.
49
+ * @type {HTMLElement | undefined}
50
+ */
51
+ let container = $state();
52
+
53
+ const show = $derived(!!cellKey && hasFocus && !!position && editorStore.useRichText);
54
+ const style = $derived(position ? `top: ${position.top}px; left: ${position.left}px` : undefined);
55
+
56
+ /**
57
+ * Get the editor wrapper, which the button is positioned against.
58
+ * @returns {HTMLElement | null | undefined} Wrapper.
59
+ */
60
+ const getWrapper = () => container?.parentElement;
61
+
62
+ /**
63
+ * Move the button to the top end corner of the cell, as much of it as is visible: a wide table
64
+ * can be scrolled horizontally within its wrapper, and the editor can be scrolled itself when its
65
+ * height is limited. Nothing is measured while the button is hidden for the lack of focus.
66
+ */
67
+ const reposition = () => {
68
+ const wrapper = getWrapper();
69
+ const cell = cellKey ? editorStore.editor?.getElementByKey(cellKey) : undefined;
70
+
71
+ if (!hasFocus || !wrapper || !cell) {
72
+ position = undefined;
73
+
74
+ return;
75
+ }
76
+
77
+ const cellRect = cell.getBoundingClientRect();
78
+
79
+ // The table is wrapped with an element scrolling it horizontally; see `initEditor()`
80
+ const tableRect = /** @type {HTMLElement} */ (
81
+ cell.closest('.table-wrapper')
82
+ ).getBoundingClientRect();
83
+
84
+ // The cell is in the editor root
85
+ const rootRect = /** @type {HTMLElement} */ (
86
+ cell.closest('.lexical-root')
87
+ ).getBoundingClientRect();
88
+
89
+ const wrapperRect = wrapper.getBoundingClientRect();
90
+ const rtl = isRightToLeftTable(cell);
91
+ const left = Math.max(cellRect.left, tableRect.left);
92
+ const right = Math.min(cellRect.right, tableRect.right);
93
+ const top = Math.max(cellRect.top, rootRect.top);
94
+ const bottom = Math.min(cellRect.bottom, rootRect.bottom);
95
+
96
+ // The cell is not laid out, e.g. while the editor is hidden, or scrolled out of view
97
+ if (right <= left || bottom <= top) {
98
+ position = undefined;
99
+
100
+ return;
101
+ }
102
+
103
+ position = {
104
+ top: top - wrapperRect.top + OFFSET,
105
+ left: (rtl ? left + OFFSET : right - OFFSET) - wrapperRect.left,
106
+ rtl,
107
+ // Open the menu toward the inline end, so it doesn’t cover the cell
108
+ popupPosition: rtl ? 'bottom-right' : 'bottom-left',
109
+ };
110
+ };
111
+
112
+ /**
113
+ * Timer to check the focus once it has settled.
114
+ * @type {number | undefined}
115
+ */
116
+ let focusTimer;
117
+
118
+ /**
119
+ * Check whether the focus is within the editor, or on the menu, once it has settled. While it
120
+ * moves, it’s on `<body>` for a moment.
121
+ */
122
+ const updateFocus = () => {
123
+ window.clearTimeout(focusTimer);
124
+ focusTimer = window.setTimeout(() => {
125
+ const wrapper = getWrapper();
126
+
127
+ hasFocus =
128
+ !!wrapper?.contains(document.activeElement) ||
129
+ !!container?.querySelector('[aria-expanded="true"]');
130
+ reposition();
131
+ });
132
+ };
133
+
134
+ $effect(() => {
135
+ const { editor } = editorStore;
136
+
137
+ // The root initializes the editor before this first runs
138
+ /* v8 ignore next 3 */
139
+ if (!editor) {
140
+ return undefined;
141
+ }
142
+
143
+ const unregisterUpdateListener = editor.registerUpdateListener(({ editorState }) => {
144
+ cellKey = editorState.read(getSelectedTableCellKey);
145
+ reposition();
146
+ });
147
+
148
+ // Follow the cell when a wide table is scrolled, or the editor is resized
149
+ const unregisterRootListener = editor.registerRootListener((root) => {
150
+ if (!root) {
151
+ return undefined;
152
+ }
153
+
154
+ const resizeObserver = new ResizeObserver(reposition);
155
+
156
+ root.addEventListener('scroll', reposition, { capture: true });
157
+ resizeObserver.observe(root);
158
+
159
+ return () => {
160
+ root.removeEventListener('scroll', reposition, { capture: true });
161
+ resizeObserver.disconnect();
162
+ };
163
+ });
164
+
165
+ return () => {
166
+ unregisterUpdateListener();
167
+ unregisterRootListener();
168
+ };
169
+ });
170
+
171
+ onMount(() => {
172
+ window.addEventListener('resize', reposition);
173
+ document.addEventListener('focusin', updateFocus);
174
+ document.addEventListener('focusout', updateFocus);
175
+
176
+ return () => {
177
+ window.clearTimeout(focusTimer);
178
+ window.removeEventListener('resize', reposition);
179
+ document.removeEventListener('focusin', updateFocus);
180
+ document.removeEventListener('focusout', updateFocus);
181
+ };
182
+ });
183
+ </script>
184
+
185
+ <div
186
+ role="none"
187
+ class="table-cell-menu-button"
188
+ class:rtl={position?.rtl}
189
+ hidden={!show}
190
+ {style}
191
+ bind:this={container}
192
+ >
193
+ {#if show && cellKey}
194
+ {@const key = cellKey}
195
+ <MenuButton
196
+ iconic
197
+ size="small"
198
+ variant="tertiary"
199
+ popupPosition={position?.popupPosition}
200
+ aria-label={_('_sui.text_editor.show_table_options')}
201
+ aria-controls={editorStore.controlId}
202
+ >
203
+ {#snippet endIcon()}
204
+ <Icon name="expand_more" />
205
+ {/snippet}
206
+ {#snippet popup()}
207
+ <TableActionMenu cellKey={key} />
208
+ {/snippet}
209
+ </MenuButton>
210
+ {/if}
211
+ </div>
212
+
213
+ <style>.table-cell-menu-button {
214
+ position: absolute;
215
+ z-index: 1;
216
+ translate: -100% 0;
217
+ }
218
+ .table-cell-menu-button.rtl {
219
+ translate: 0 0;
220
+ }
221
+ .table-cell-menu-button[hidden] {
222
+ display: none;
223
+ }
224
+ .table-cell-menu-button :global(.sui.button) {
225
+ margin: 0;
226
+ border-radius: 50%;
227
+ background-color: var(--sui-secondary-background-color);
228
+ }</style>
@@ -0,0 +1,12 @@
1
+ export default TableCellMenuButton;
2
+ type TableCellMenuButton = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<Record<string, never>>): void;
5
+ };
6
+ /**
7
+ * Button on the table cell where the caret is, at its top end corner, that opens the table menu to
8
+ * insert and delete rows and columns, the same way it works in the Lexical Playground. The same menu
9
+ * is also available on the toolbar, which is easier to reach with the keyboard.
10
+ * @see https://playground.lexical.dev/
11
+ */
12
+ declare const TableCellMenuButton: import("svelte").Component<Record<string, never>, {}, "">;
@@ -0,0 +1,18 @@
1
+ export function parseTableSize(value: unknown): number | undefined;
2
+ export function createTableMarkdown(rows: number, columns: number): string;
3
+ export function isRightToLeftTable(cell: HTMLElement): boolean;
4
+ export function $getTableCell(node: LexicalNode | null | undefined): TableCellNode | null;
5
+ export function $getSelectedTableCellKey(): string | null;
6
+ export function $insertTable(rows: number, columns: number): boolean;
7
+ export function $ensureParagraphAfterTable(table: TableNode): void;
8
+ export function $normalizeTableHeaders(table: TableNode): void;
9
+ export function $runTableAction(action: TableAction, cellKey: string, { rtl }?: {
10
+ rtl?: boolean | undefined;
11
+ }): boolean;
12
+ /**
13
+ * Operation offered by the table menu.
14
+ */
15
+ export type TableAction = "insert-row-above" | "insert-row-below" | "insert-column-left" | "insert-column-right" | "delete-row" | "delete-column" | "delete-table";
16
+ import type { LexicalNode } from 'lexical';
17
+ import type { TableCellNode } from '@lexical/table';
18
+ import type { TableNode } from '@lexical/table';
@@ -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>, {}, "">;