@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
|
@@ -7,7 +7,11 @@ import {
|
|
|
7
7
|
$isCodeNode as isCodeNode,
|
|
8
8
|
} from '@lexical/code-core';
|
|
9
9
|
import { registerDragonSupport } from '@lexical/dragon';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
buildEditorFromExtensions,
|
|
12
|
+
HorizontalRuleExtension,
|
|
13
|
+
HorizontalRuleNode,
|
|
14
|
+
} from '@lexical/extension';
|
|
11
15
|
import { createEmptyHistoryState, registerHistory } from '@lexical/history';
|
|
12
16
|
import { $generateNodesFromDOM as generateNodesFromDOM } from '@lexical/html';
|
|
13
17
|
import {
|
|
@@ -37,7 +41,7 @@ import {
|
|
|
37
41
|
$isQuoteNode as isQuoteNode,
|
|
38
42
|
registerRichText,
|
|
39
43
|
} from '@lexical/rich-text';
|
|
40
|
-
import { TableCellNode, TableNode, TableRowNode } from '@lexical/table';
|
|
44
|
+
import { TableCellNode, TableExtension, TableNode, TableRowNode } from '@lexical/table';
|
|
41
45
|
import {
|
|
42
46
|
$getNearestNodeOfType as getNearestNodeOfType,
|
|
43
47
|
objectKlassEquals,
|
|
@@ -48,6 +52,7 @@ import { isURL } from '@sveltia/utils/string';
|
|
|
48
52
|
import {
|
|
49
53
|
COMMAND_PRIORITY_LOW,
|
|
50
54
|
COMMAND_PRIORITY_NORMAL,
|
|
55
|
+
configExtension,
|
|
51
56
|
createEditor,
|
|
52
57
|
defineExtension,
|
|
53
58
|
DELETE_CHARACTER_COMMAND,
|
|
@@ -107,10 +112,17 @@ import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js
|
|
|
107
112
|
import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
|
|
108
113
|
import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
|
|
109
114
|
import { HR } from './transformers/hr.js';
|
|
115
|
+
import { $getSelectedAlignment } from './alignment.js';
|
|
116
|
+
import {
|
|
117
|
+
$ensureParagraphAfterTable,
|
|
118
|
+
$getSelectedTableCellKey,
|
|
119
|
+
$normalizeTableHeaders,
|
|
120
|
+
} from './table.js';
|
|
110
121
|
import { TABLE } from './transformers/table.js';
|
|
111
122
|
|
|
112
123
|
/**
|
|
113
124
|
* @import { LexicalEditor, LexicalNode } from 'lexical';
|
|
125
|
+
* @import { LinkAttributes } from '@lexical/link';
|
|
114
126
|
* @import { ListType } from '@lexical/list';
|
|
115
127
|
* @import { Transformer } from '@lexical/markdown';
|
|
116
128
|
* @import {
|
|
@@ -131,6 +143,11 @@ import { TABLE } from './transformers/table.js';
|
|
|
131
143
|
* @property {() => void} dispose Remove all registered Lexical listeners.
|
|
132
144
|
*/
|
|
133
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Payload of `TOGGLE_LINK_COMMAND` with the URL and the link attributes, like `target`.
|
|
148
|
+
* @typedef {LinkAttributes & { url: string }} LinkAttributesWithURL
|
|
149
|
+
*/
|
|
150
|
+
|
|
134
151
|
/**
|
|
135
152
|
* Maximum number of entries in the undo stack.
|
|
136
153
|
*/
|
|
@@ -406,6 +423,9 @@ export const getSelectionTypes = () => {
|
|
|
406
423
|
blockNodeKey: null,
|
|
407
424
|
blockType: 'paragraph',
|
|
408
425
|
inlineTypes: [],
|
|
426
|
+
// Several cells can be selected at once
|
|
427
|
+
tableCellKey: $getSelectedTableCellKey(),
|
|
428
|
+
alignment: '',
|
|
409
429
|
};
|
|
410
430
|
}
|
|
411
431
|
|
|
@@ -466,6 +486,8 @@ export const getSelectionTypes = () => {
|
|
|
466
486
|
blockNodeKey: parent?.getKey() ?? null,
|
|
467
487
|
blockType,
|
|
468
488
|
inlineTypes,
|
|
489
|
+
tableCellKey: $getSelectedTableCellKey(),
|
|
490
|
+
alignment: $getSelectedAlignment(),
|
|
469
491
|
};
|
|
470
492
|
};
|
|
471
493
|
|
|
@@ -678,12 +700,15 @@ const registerLinkCommands = (editor) =>
|
|
|
678
700
|
editor.registerCommand(
|
|
679
701
|
TOGGLE_LINK_COMMAND,
|
|
680
702
|
(payload) => {
|
|
703
|
+
// The payload is the URL, or an object with the URL and attributes like `target`
|
|
704
|
+
const url = typeof payload === 'string' ? payload : (payload?.url ?? null);
|
|
705
|
+
|
|
681
706
|
// Ignore an unsafe URL rather than linking to it
|
|
682
|
-
if (
|
|
707
|
+
if (url !== null && !isSafeLinkURL(url)) {
|
|
683
708
|
return true;
|
|
684
709
|
}
|
|
685
710
|
|
|
686
|
-
toggleLink(
|
|
711
|
+
toggleLink(url === null ? null : /** @type {string | LinkAttributesWithURL} */ (payload));
|
|
687
712
|
|
|
688
713
|
return true;
|
|
689
714
|
},
|
|
@@ -890,13 +915,31 @@ export const initEditor = ({
|
|
|
890
915
|
format = 'markdown',
|
|
891
916
|
}) => {
|
|
892
917
|
const hasCodeBlock = enabledButtons.includes('code-block') || isCodeEditor;
|
|
918
|
+
const hasTable = enabledButtons.includes('table') && !isCodeEditor;
|
|
919
|
+
const hasHorizontalRule = enabledButtons.includes('horizontal-rule') && !isCodeEditor;
|
|
893
920
|
|
|
894
921
|
const editorExtension = defineExtension({
|
|
895
922
|
name: '@sveltia/ui/editor',
|
|
896
923
|
namespace: 'editor',
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
924
|
+
dependencies: [
|
|
925
|
+
// `CodeNode` expects the editor to be built with `CodeExtension`, which also registers the
|
|
926
|
+
// `CodeNode` and `CodeHighlightNode` nodes
|
|
927
|
+
...(hasCodeBlock ? [CodeExtension] : []),
|
|
928
|
+
// Table editing: selecting cells, moving between them with the Tab key, and scrolling a wide
|
|
929
|
+
// table. Markdown and the HTML export support neither merged cells nor cell colors
|
|
930
|
+
...(hasTable
|
|
931
|
+
? [
|
|
932
|
+
configExtension(TableExtension, {
|
|
933
|
+
hasCellMerge: false,
|
|
934
|
+
hasCellBackgroundColor: false,
|
|
935
|
+
hasHorizontalScroll: true,
|
|
936
|
+
hasTabHandler: true,
|
|
937
|
+
}),
|
|
938
|
+
]
|
|
939
|
+
: []),
|
|
940
|
+
// Selecting a horizontal rule with a click, so it can be deleted
|
|
941
|
+
...(hasHorizontalRule ? [HorizontalRuleExtension] : []),
|
|
942
|
+
],
|
|
900
943
|
// Start with an empty editor state like `createEditor` does, instead of a blank paragraph. The
|
|
901
944
|
// Markdown import then becomes the first state in the history, so undoing right after the
|
|
902
945
|
// editor loads doesn’t revert it to the blank paragraph and clear the value
|
|
@@ -977,6 +1020,17 @@ export const initEditor = ({
|
|
|
977
1020
|
: undefined,
|
|
978
1021
|
hasCodeBlock ? observeCodeTheme(editor) : undefined,
|
|
979
1022
|
enabledButtons.includes('link') ? registerLinkCommands(editor) : undefined,
|
|
1023
|
+
hasTable
|
|
1024
|
+
? editor.registerNodeTransform(TableNode, (table) => {
|
|
1025
|
+
$ensureParagraphAfterTable(table);
|
|
1026
|
+
|
|
1027
|
+
// A Markdown table always has a single header row on top, while an HTML table can have
|
|
1028
|
+
// any number of them, as well as a header column
|
|
1029
|
+
if (format === 'markdown') {
|
|
1030
|
+
$normalizeTableHeaders(table);
|
|
1031
|
+
}
|
|
1032
|
+
})
|
|
1033
|
+
: undefined,
|
|
980
1034
|
registerListCommands(editor, enabledButtons),
|
|
981
1035
|
registerUpdateEvent(editor, enabledTransformers, format),
|
|
982
1036
|
registerTabIndentation(editor),
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* The link at the selection, if any.
|
|
68
|
-
* @type {{ key: string, url: string } | undefined}
|
|
68
|
+
* @type {{ key: string, url: string, target: string | null } | undefined}
|
|
69
69
|
*/
|
|
70
70
|
let currentLink = $state();
|
|
71
71
|
/**
|
|
@@ -86,6 +86,15 @@
|
|
|
86
86
|
let editTarget = $state.raw();
|
|
87
87
|
/** URL entered in the edit mode. */
|
|
88
88
|
let url = $state('');
|
|
89
|
+
/** Whether the link opens in a new tab, chosen in the edit mode. */
|
|
90
|
+
let openInNewTab = $state(false);
|
|
91
|
+
/** Whether the link opened in a new tab when the edit mode started. */
|
|
92
|
+
let initialOpenInNewTab = false;
|
|
93
|
+
/**
|
|
94
|
+
* Whether the link can be set to open in a new tab. Markdown has no syntax for the link target,
|
|
95
|
+
* so it’s only available for HTML.
|
|
96
|
+
*/
|
|
97
|
+
const canSetTarget = $derived(editorStore.config.format === 'html');
|
|
89
98
|
/**
|
|
90
99
|
* Viewport-relative bounds of the link, or the selected text, the popup is anchored to.
|
|
91
100
|
* @type {EmojiAnchorRect | undefined}
|
|
@@ -208,10 +217,13 @@
|
|
|
208
217
|
* Switch the popup to the edit mode, and move the focus to the URL field.
|
|
209
218
|
* @param {LinkEditTarget} target What to edit.
|
|
210
219
|
* @param {string} initialURL URL to fill the field with.
|
|
220
|
+
* @param {boolean} [newTab] Whether the link opens in a new tab.
|
|
211
221
|
*/
|
|
212
|
-
const startEditing = async (target, initialURL) => {
|
|
222
|
+
const startEditing = async (target, initialURL, newTab = false) => {
|
|
213
223
|
editTarget = target;
|
|
214
224
|
url = initialURL;
|
|
225
|
+
openInNewTab = newTab;
|
|
226
|
+
initialOpenInNewTab = newTab;
|
|
215
227
|
dismissedLinkKey = undefined;
|
|
216
228
|
await tick();
|
|
217
229
|
reposition();
|
|
@@ -226,6 +238,7 @@
|
|
|
226
238
|
const stopEditing = () => {
|
|
227
239
|
editTarget = undefined;
|
|
228
240
|
url = '';
|
|
241
|
+
openInNewTab = false;
|
|
229
242
|
};
|
|
230
243
|
|
|
231
244
|
/**
|
|
@@ -249,6 +262,7 @@
|
|
|
249
262
|
/** @type {LinkEditTarget | undefined} */
|
|
250
263
|
let target;
|
|
251
264
|
let initialURL = '';
|
|
265
|
+
let newTab = false;
|
|
252
266
|
|
|
253
267
|
editorStore.editor?.getEditorState().read(() => {
|
|
254
268
|
const link = findLink();
|
|
@@ -256,6 +270,7 @@
|
|
|
256
270
|
if (link) {
|
|
257
271
|
target = { linkKey: link.getKey() };
|
|
258
272
|
initialURL = link.getURL();
|
|
273
|
+
newTab = link.getTarget() === '_blank';
|
|
259
274
|
|
|
260
275
|
return;
|
|
261
276
|
}
|
|
@@ -279,7 +294,7 @@
|
|
|
279
294
|
return false;
|
|
280
295
|
}
|
|
281
296
|
|
|
282
|
-
startEditing(target, initialURL);
|
|
297
|
+
startEditing(target, initialURL, newTab);
|
|
283
298
|
|
|
284
299
|
return true;
|
|
285
300
|
};
|
|
@@ -293,6 +308,15 @@
|
|
|
293
308
|
const target = editTarget;
|
|
294
309
|
const newURL = url.trim();
|
|
295
310
|
|
|
311
|
+
// Only set the target when the toggle has been changed, so another target, like `_top`, is
|
|
312
|
+
// kept. The toggle isn’t available for Markdown, which can’t hold the target
|
|
313
|
+
const newTarget =
|
|
314
|
+
canSetTarget && openInNewTab !== initialOpenInNewTab
|
|
315
|
+
? openInNewTab
|
|
316
|
+
? '_blank'
|
|
317
|
+
: null
|
|
318
|
+
: undefined;
|
|
319
|
+
|
|
296
320
|
// The Apply button is disabled and the Enter key is ignored otherwise
|
|
297
321
|
/* v8 ignore next */
|
|
298
322
|
if (!editor || !target || !isValidURL) {
|
|
@@ -309,13 +333,20 @@
|
|
|
309
333
|
/* v8 ignore else */
|
|
310
334
|
if (isLinkNode(node)) {
|
|
311
335
|
node.setURL(newURL);
|
|
336
|
+
|
|
337
|
+
if (newTarget !== undefined) {
|
|
338
|
+
node.setTarget(newTarget);
|
|
339
|
+
}
|
|
312
340
|
}
|
|
313
341
|
});
|
|
314
342
|
} else {
|
|
315
343
|
editor.update(() => {
|
|
316
344
|
setSelection(target.selection.clone());
|
|
317
345
|
});
|
|
318
|
-
editor.dispatchCommand(
|
|
346
|
+
editor.dispatchCommand(
|
|
347
|
+
TOGGLE_LINK_COMMAND,
|
|
348
|
+
newTarget ? { url: newURL, target: newTarget } : newURL,
|
|
349
|
+
);
|
|
319
350
|
}
|
|
320
351
|
|
|
321
352
|
await refocusEditor();
|
|
@@ -417,7 +448,9 @@
|
|
|
417
448
|
editorState.read(() => {
|
|
418
449
|
const link = findLink();
|
|
419
450
|
|
|
420
|
-
currentLink = link
|
|
451
|
+
currentLink = link
|
|
452
|
+
? { key: link.getKey(), url: link.getURL(), target: link.getTarget() }
|
|
453
|
+
: undefined;
|
|
421
454
|
|
|
422
455
|
// What is being edited may be gone, e.g. when the value is replaced from outside, in which
|
|
423
456
|
// case there is nothing to apply the URL to anymore
|
|
@@ -580,6 +613,21 @@
|
|
|
580
613
|
onInputKeyDown(event);
|
|
581
614
|
}}
|
|
582
615
|
/>
|
|
616
|
+
{#if canSetTarget}
|
|
617
|
+
<Button
|
|
618
|
+
iconic
|
|
619
|
+
variant="ghost"
|
|
620
|
+
pressed={openInNewTab}
|
|
621
|
+
aria-label={_('_sui.text_editor.open_in_new_tab')}
|
|
622
|
+
onclick={() => {
|
|
623
|
+
openInNewTab = !openInNewTab;
|
|
624
|
+
}}
|
|
625
|
+
>
|
|
626
|
+
{#snippet startIcon()}
|
|
627
|
+
<Icon name="open_in_new" />
|
|
628
|
+
{/snippet}
|
|
629
|
+
</Button>
|
|
630
|
+
{/if}
|
|
583
631
|
<Button
|
|
584
632
|
iconic
|
|
585
633
|
variant="ghost"
|
|
@@ -623,7 +671,11 @@
|
|
|
623
671
|
// The link is still at the selection, since the focus is within the popup
|
|
624
672
|
/* v8 ignore else */
|
|
625
673
|
if (currentLink) {
|
|
626
|
-
startEditing(
|
|
674
|
+
startEditing(
|
|
675
|
+
{ linkKey: currentLink.key },
|
|
676
|
+
currentLink.url,
|
|
677
|
+
currentLink.target === '_blank',
|
|
678
|
+
);
|
|
627
679
|
}
|
|
628
680
|
}}
|
|
629
681
|
>
|
|
@@ -34,7 +34,8 @@ import { isPlainLanguage } from './shiki/facade.js';
|
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Attributes to be kept on the exported elements, by tag name. Anything else Lexical adds, like
|
|
37
|
-
* `class`, `dir`, `style`, `value` or `spellcheck`, is only meant for the editor itself
|
|
37
|
+
* `class`, `dir`, `style`, `value` or `spellcheck`, is only meant for the editor itself, except for
|
|
38
|
+
* the text alignment of a block; see {@link ALIGNABLE_TAGS}.
|
|
38
39
|
* @type {Record<string, string[]>}
|
|
39
40
|
*/
|
|
40
41
|
const ALLOWED_ATTRIBUTES = {
|
|
@@ -79,17 +80,32 @@ export const getCodeLanguage = (code) =>
|
|
|
79
80
|
code.className.match(/(?:^|\s)(?:lang|language)-(\S+)/)?.[1];
|
|
80
81
|
|
|
81
82
|
/**
|
|
82
|
-
*
|
|
83
|
+
* Elements that keep their text alignment, which is the only style kept in the exported HTML.
|
|
84
|
+
* Lexical writes it as `style="text-align: …"` for the blocks it can be set on.
|
|
85
|
+
*/
|
|
86
|
+
const ALIGNABLE_TAGS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li'];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Remove the attributes not listed in {@link ALLOWED_ATTRIBUTES} from the given element, as well as
|
|
90
|
+
* any style but the text alignment of a block listed in {@link ALIGNABLE_TAGS}.
|
|
83
91
|
* @param {Element} element Element.
|
|
84
92
|
*/
|
|
85
93
|
const removeExtraAttributes = (element) => {
|
|
86
94
|
const allowedAttributes = ALLOWED_ATTRIBUTES[element.localName] ?? [];
|
|
87
95
|
|
|
96
|
+
const textAlign = ALIGNABLE_TAGS.includes(element.localName)
|
|
97
|
+
? /** @type {HTMLElement} */ (element).style.textAlign
|
|
98
|
+
: '';
|
|
99
|
+
|
|
88
100
|
[...element.attributes].forEach(({ name }) => {
|
|
89
101
|
if (!allowedAttributes.includes(name)) {
|
|
90
102
|
element.removeAttribute(name);
|
|
91
103
|
}
|
|
92
104
|
});
|
|
105
|
+
|
|
106
|
+
if (textAlign) {
|
|
107
|
+
/** @type {HTMLElement} */ (element).style.textAlign = textAlign;
|
|
108
|
+
}
|
|
93
109
|
};
|
|
94
110
|
|
|
95
111
|
/**
|
|
@@ -134,7 +150,7 @@ const exportElementNode = (editor, node) => {
|
|
|
134
150
|
/**
|
|
135
151
|
* Export a paragraph node. Empty paragraphs at the end are left out, as they have no content; one
|
|
136
152
|
* may be added just by clicking below a block decorator. A paragraph that is the only child of a
|
|
137
|
-
* table cell is exported without the `<p>` element.
|
|
153
|
+
* table cell is exported without the `<p>` element, unless it’s aligned.
|
|
138
154
|
* @param {LexicalEditor} editor Editor instance.
|
|
139
155
|
* @param {ParagraphNode} node Paragraph node.
|
|
140
156
|
* @returns {DOMExportOutput} Export output.
|
|
@@ -142,7 +158,8 @@ const exportElementNode = (editor, node) => {
|
|
|
142
158
|
const exportParagraphNode = (editor, node) => {
|
|
143
159
|
const parent = node.getParent();
|
|
144
160
|
|
|
145
|
-
|
|
161
|
+
// Keep the `<p>` of an aligned paragraph, which holds the alignment
|
|
162
|
+
if (parent instanceof TableCellNode && parent.getChildrenSize() === 1 && !node.getFormatType()) {
|
|
146
163
|
return { element: document.createDocumentFragment() };
|
|
147
164
|
}
|
|
148
165
|
|
|
@@ -277,11 +277,38 @@
|
|
|
277
277
|
white-space: normal;
|
|
278
278
|
word-break: normal;
|
|
279
279
|
}
|
|
280
|
+
.lexical-root :global(.table-wrapper) {
|
|
281
|
+
overflow-x: auto;
|
|
282
|
+
}
|
|
283
|
+
.lexical-root :global(.table-wrapper:not(:first-child)) {
|
|
284
|
+
margin-top: 1em;
|
|
285
|
+
}
|
|
286
|
+
.lexical-root :global(.table-wrapper:not(:last-child)) {
|
|
287
|
+
margin-bottom: 1em;
|
|
288
|
+
}
|
|
289
|
+
.lexical-root :global(:is(th, td)) {
|
|
290
|
+
min-width: 80px;
|
|
291
|
+
vertical-align: top;
|
|
292
|
+
text-align: start;
|
|
293
|
+
}
|
|
294
|
+
.lexical-root :global(th) {
|
|
295
|
+
background-color: var(--sui-secondary-background-color);
|
|
296
|
+
}
|
|
297
|
+
.lexical-root :global(:is(th, td).selected) {
|
|
298
|
+
background-color: var(--sui-selected-background-color);
|
|
299
|
+
}
|
|
300
|
+
.lexical-root :global(.table-selection) :global(::selection) {
|
|
301
|
+
background-color: transparent;
|
|
302
|
+
}
|
|
280
303
|
.lexical-root :global(hr) {
|
|
281
304
|
margin: var(--sui-paragraph-margin) 0;
|
|
282
305
|
border: none;
|
|
283
306
|
padding: 0;
|
|
284
307
|
}
|
|
308
|
+
.lexical-root :global(hr.selected) {
|
|
309
|
+
outline: 2px solid var(--sui-primary-accent-color);
|
|
310
|
+
outline-offset: 4px;
|
|
311
|
+
}
|
|
285
312
|
.lexical-root :global(hr::after) {
|
|
286
313
|
display: block;
|
|
287
314
|
height: 2px;
|
|
@@ -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';
|