@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.
Files changed (70) hide show
  1. package/dist/components/drawer/drawer.svelte.d.ts +2 -2
  2. package/dist/components/scroll/infinite-scroll.svelte +4 -2
  3. package/dist/components/text-editor/alignment.d.ts +2 -0
  4. package/dist/components/text-editor/alignment.js +40 -0
  5. package/dist/components/text-editor/block.d.ts +4 -0
  6. package/dist/components/text-editor/block.js +65 -0
  7. package/dist/components/text-editor/constants.d.ts +22 -2
  8. package/dist/components/text-editor/constants.js +40 -1
  9. package/dist/components/text-editor/core.d.ts +7 -0
  10. package/dist/components/text-editor/core.js +61 -7
  11. package/dist/components/text-editor/floating-link-editor.svelte +58 -6
  12. package/dist/components/text-editor/html.js +21 -4
  13. package/dist/components/text-editor/lexical-root.svelte +27 -0
  14. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  15. package/dist/components/text-editor/shiki/version.js +1 -1
  16. package/dist/components/text-editor/table-cell-menu-button.svelte +228 -0
  17. package/dist/components/text-editor/table-cell-menu-button.svelte.d.ts +12 -0
  18. package/dist/components/text-editor/table.d.ts +18 -0
  19. package/dist/components/text-editor/table.js +277 -0
  20. package/dist/components/text-editor/text-editor.svelte +5 -0
  21. package/dist/components/text-editor/toolbar/align-menu-button.svelte +84 -0
  22. package/dist/components/text-editor/toolbar/align-menu-button.svelte.d.ts +10 -0
  23. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +65 -0
  24. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte.d.ts +30 -0
  25. package/dist/components/text-editor/toolbar/insert-link-button.svelte +17 -1
  26. package/dist/components/text-editor/toolbar/insert-table-button.svelte +209 -0
  27. package/dist/components/text-editor/toolbar/insert-table-button.svelte.d.ts +24 -0
  28. package/dist/components/text-editor/toolbar/menu.d.ts +1 -0
  29. package/dist/components/text-editor/toolbar/menu.js +32 -0
  30. package/dist/components/text-editor/toolbar/table-action-menu.svelte +96 -0
  31. package/dist/components/text-editor/toolbar/table-action-menu.svelte.d.ts +21 -0
  32. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +75 -7
  33. package/dist/components/text-editor/transformers/hr.d.ts +5 -0
  34. package/dist/components/text-editor/transformers/hr.js +7 -1
  35. package/dist/components/text-editor/transformers/table.js +3 -2
  36. package/dist/locales/ar.yaml +28 -0
  37. package/dist/locales/bg.yaml +28 -0
  38. package/dist/locales/ca.yaml +28 -0
  39. package/dist/locales/cs.yaml +28 -0
  40. package/dist/locales/da.yaml +28 -0
  41. package/dist/locales/de.yaml +28 -0
  42. package/dist/locales/el.yaml +28 -0
  43. package/dist/locales/en-CA.yaml +28 -0
  44. package/dist/locales/en-GB.yaml +28 -0
  45. package/dist/locales/en-US.yaml +28 -0
  46. package/dist/locales/es-CO.yaml +28 -0
  47. package/dist/locales/fa.yaml +28 -0
  48. package/dist/locales/fi.yaml +28 -0
  49. package/dist/locales/fr.yaml +28 -0
  50. package/dist/locales/he.yaml +28 -0
  51. package/dist/locales/hr.yaml +28 -0
  52. package/dist/locales/it.yaml +28 -0
  53. package/dist/locales/ja.yaml +28 -0
  54. package/dist/locales/ko.yaml +28 -0
  55. package/dist/locales/nl.yaml +28 -0
  56. package/dist/locales/pl.yaml +28 -0
  57. package/dist/locales/pt-BR.yaml +28 -0
  58. package/dist/locales/pt-PT.yaml +28 -0
  59. package/dist/locales/ru.yaml +28 -0
  60. package/dist/locales/sk.yaml +28 -0
  61. package/dist/locales/sv.yaml +28 -0
  62. package/dist/locales/tr.yaml +28 -0
  63. package/dist/locales/uk.yaml +28 -0
  64. package/dist/locales/vi.yaml +28 -0
  65. package/dist/locales/zh-CN.yaml +28 -0
  66. package/dist/locales/zh-TW.yaml +28 -0
  67. package/dist/services/popup.svelte.js +4 -2
  68. package/dist/typedefs.d.ts +10 -1
  69. package/dist/typedefs.js +5 -1
  70. package/package.json +6 -6
@@ -31,7 +31,7 @@ declare const Drawer: import("svelte").Component<ModalProps & {
31
31
  /**
32
32
  * Position of the drawer.
33
33
  */
34
- position?: "top" | "left" | "right" | "bottom" | undefined;
34
+ position?: "left" | "right" | "top" | "bottom" | undefined;
35
35
  /**
36
36
  * Width or height of the
37
37
  * drawer.
@@ -97,7 +97,7 @@ type Props = {
97
97
  /**
98
98
  * Position of the drawer.
99
99
  */
100
- position?: "top" | "left" | "right" | "bottom" | undefined;
100
+ position?: "left" | "right" | "top" | "bottom" | undefined;
101
101
  /**
102
102
  * Width or height of the
103
103
  * drawer.
@@ -73,8 +73,10 @@
73
73
  }
74
74
 
75
75
  const observer = new IntersectionObserver(
76
- ([{ isIntersecting }]) => {
77
- if (isIntersecting && loading) {
76
+ (entries) => {
77
+ // A busy browser can report several changes at once, oldest first, so only the last entry
78
+ // tells whether the spinner is near the view now
79
+ if (entries.at(-1)?.isIntersecting && loading) {
78
80
  loadedItemSize += itemChunkSize;
79
81
  }
80
82
  },
@@ -0,0 +1,2 @@
1
+ export function $getSelectedAlignment(): ElementFormatType;
2
+ import type { ElementFormatType } from 'lexical';
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Text alignment helpers for the rich text editor. Alignment is only available for HTML, as
3
+ * Markdown has no syntax for it. Functions starting with `$` must be called within an editor update
4
+ * or read.
5
+ */
6
+
7
+ import { $findMatchingParent as findMatchingParent } from '@lexical/utils';
8
+ import {
9
+ $getSelection as getSelection,
10
+ $isElementNode as isElementNode,
11
+ $isRangeSelection as isRangeSelection,
12
+ } from 'lexical';
13
+
14
+ /**
15
+ * @import { ElementFormatType, ElementNode } from 'lexical';
16
+ */
17
+
18
+ /**
19
+ * Get the text alignment of the block the selection’s anchor is in, like a paragraph, a heading or
20
+ * a list item.
21
+ * @returns {ElementFormatType} Alignment. An empty string means the default alignment, as well as
22
+ * no selection.
23
+ */
24
+ export const $getSelectedAlignment = () => {
25
+ const selection = getSelection();
26
+
27
+ if (!isRangeSelection(selection)) {
28
+ return '';
29
+ }
30
+
31
+ const block = /** @type {ElementNode | null} */ (
32
+ findMatchingParent(
33
+ selection.anchor.getNode(),
34
+ (node) => isElementNode(node) && !node.isInline(),
35
+ )
36
+ );
37
+
38
+ // The caret can be on the root itself, between blocks, like next to a horizontal rule
39
+ return block?.getFormatType() ?? '';
40
+ };
@@ -0,0 +1,4 @@
1
+ export function $insertBlockNode<T extends LexicalNode>(node: T): T;
2
+ export function $insertHorizontalRule(): HorizontalRuleNode;
3
+ import type { LexicalNode } from 'lexical';
4
+ import type { HorizontalRuleNode } from '@lexical/extension';
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Helpers for inserting block nodes, like a table or a horizontal rule, into the rich text editor.
3
+ * Functions starting with `$` must be called within an editor update.
4
+ */
5
+
6
+ import { $createHorizontalRuleNode as createHorizontalRuleNode } from '@lexical/extension';
7
+ import { $insertNodeToNearestRoot as insertNodeToNearestRoot } from '@lexical/utils';
8
+ import {
9
+ $createParagraphNode as createParagraphNode,
10
+ $getPreviousSelection as getPreviousSelection,
11
+ $getSelection as getSelection,
12
+ $isElementNode as isElementNode,
13
+ $isParagraphNode as isParagraphNode,
14
+ $isRangeSelection as isRangeSelection,
15
+ $isRootNode as isRootNode,
16
+ $setSelection as setSelection,
17
+ } from 'lexical';
18
+
19
+ /**
20
+ * @import { LexicalNode } from 'lexical';
21
+ * @import { HorizontalRuleNode } from '@lexical/extension';
22
+ */
23
+
24
+ /**
25
+ * Insert the given block node at the selection, which is restored if the focus has moved to the
26
+ * toolbar or a dialog in the meantime. The node takes the place of the empty paragraph the caret is
27
+ * in, rather than leaving the paragraph above it. Otherwise, the block the caret is in is split at
28
+ * the caret, and the node is inserted in between.
29
+ * @template {LexicalNode} T
30
+ * @param {T} node Node.
31
+ * @returns {T} The node, once inserted.
32
+ */
33
+ export const $insertBlockNode = (node) => {
34
+ const selection = getSelection() ?? getPreviousSelection();
35
+
36
+ if (selection && !getSelection()) {
37
+ setSelection(selection.clone());
38
+ }
39
+
40
+ const anchor = isRangeSelection(selection) ? selection.anchor.getNode() : null;
41
+
42
+ if (isParagraphNode(anchor) && anchor.isEmpty() && isRootNode(anchor.getParent())) {
43
+ return anchor.replace(node);
44
+ }
45
+
46
+ return insertNodeToNearestRoot(node);
47
+ };
48
+
49
+ /**
50
+ * Insert a horizontal rule at the selection, and move the caret to the start of the block after it,
51
+ * or a new paragraph if there’s none to type in.
52
+ * @returns {HorizontalRuleNode} The rule.
53
+ */
54
+ export const $insertHorizontalRule = () => {
55
+ const rule = $insertBlockNode(createHorizontalRuleNode());
56
+ const next = rule.getNextSibling();
57
+
58
+ if (isElementNode(next)) {
59
+ next.selectStart();
60
+ } else {
61
+ rule.insertAfter(createParagraphNode()).selectStart();
62
+ }
63
+
64
+ return rule;
65
+ };
@@ -44,9 +44,10 @@ export const NODE_MAP: Record<Exclude<TextEditorBlockType | "link", "paragraph">
44
44
  /**
45
45
  * Map of Lexical transformers for each block type. The `paragraph` block type is excluded because
46
46
  * it is the default block type.
47
- * @type {Record<Exclude<TextEditorNodeType, 'paragraph'>, Transformer[]>}
47
+ * @type {Record<Exclude<TextEditorNodeType, 'paragraph' | 'table' | 'horizontal-rule' | 'align'>,
48
+ * Transformer[]>}
48
49
  */
49
- export const TRANSFORMER_MAP: Record<Exclude<TextEditorNodeType, "paragraph">, Transformer[]>;
50
+ export const TRANSFORMER_MAP: Record<Exclude<TextEditorNodeType, "paragraph" | "table" | "horizontal-rule" | "align">, Transformer[]>;
50
51
  /**
51
52
  * List of Markdown tags that should be disabled in the editor when converting Lexical nodes to
52
53
  * Markdown (but not when converting Markdown to Lexical nodes because we don’t want to lose any
@@ -69,6 +70,25 @@ export const IMPORT_UPDATE_TAG: "sui-import";
69
70
  * @type {LexicalCommand<void>}
70
71
  */
71
72
  export const OPEN_LINK_EDITOR_COMMAND: LexicalCommand<void>;
73
+ /**
74
+ * Text alignments offered by the alignment menu, which is only available for HTML, as Markdown
75
+ * has no syntax for alignment. Like most editors, it offers the physical sides rather than the
76
+ * logical `start` and `end`, which are still kept as is when the HTML has them.
77
+ * @type {{ type: 'left' | 'center' | 'right' | 'justify', labelKey: string, icon: string }[]}
78
+ */
79
+ export const ALIGNMENT_OPTIONS: {
80
+ type: "left" | "center" | "right" | "justify";
81
+ labelKey: string;
82
+ icon: string;
83
+ }[];
84
+ /**
85
+ * Default number of rows, including the header row, and columns of a new table.
86
+ */
87
+ export const DEFAULT_TABLE_SIZE: 3;
88
+ /**
89
+ * Maximum number of rows and columns of a new table.
90
+ */
91
+ export const MAX_TABLE_SIZE: 50;
72
92
  import type { EditorThemeClasses } from 'lexical';
73
93
  import type { TextEditorFormatType } from '../../typedefs.js';
74
94
  import type { TextEditorInlineType } from '../../typedefs.js';
@@ -49,6 +49,11 @@ export const EDITOR_THEME = {
49
49
  },
50
50
  },
51
51
  code: 'code-block',
52
+ // Lexical wraps a table with an element to scroll it horizontally when the table button is
53
+ // enabled; see `initEditor()`
54
+ tableScrollableWrapper: 'table-wrapper',
55
+ tableCellSelected: 'selected',
56
+ tableSelection: 'table-selection',
52
57
  };
53
58
 
54
59
  /**
@@ -136,6 +141,16 @@ export const AVAILABLE_BUTTONS = {
136
141
  icon: 'code_blocks',
137
142
  inline: false,
138
143
  },
144
+ table: {
145
+ labelKey: 'insert_table',
146
+ icon: 'table',
147
+ inline: false,
148
+ },
149
+ 'horizontal-rule': {
150
+ labelKey: 'insert_horizontal_rule',
151
+ icon: 'horizontal_rule',
152
+ inline: false,
153
+ },
139
154
  };
140
155
 
141
156
  /**
@@ -187,7 +202,8 @@ export const NODE_MAP = {
187
202
  /**
188
203
  * Map of Lexical transformers for each block type. The `paragraph` block type is excluded because
189
204
  * it is the default block type.
190
- * @type {Record<Exclude<TextEditorNodeType, 'paragraph'>, Transformer[]>}
205
+ * @type {Record<Exclude<TextEditorNodeType, 'paragraph' | 'table' | 'horizontal-rule' | 'align'>,
206
+ * Transformer[]>}
191
207
  */
192
208
  export const TRANSFORMER_MAP = {
193
209
  // ELEMENT_TRANSFORMERS
@@ -247,3 +263,26 @@ export const IMPORT_UPDATE_TAG = 'sui-import';
247
263
  * @type {LexicalCommand<void>}
248
264
  */
249
265
  export const OPEN_LINK_EDITOR_COMMAND = createCommand('OPEN_LINK_EDITOR_COMMAND');
266
+
267
+ /**
268
+ * Text alignments offered by the alignment menu, which is only available for HTML, as Markdown
269
+ * has no syntax for alignment. Like most editors, it offers the physical sides rather than the
270
+ * logical `start` and `end`, which are still kept as is when the HTML has them.
271
+ * @type {{ type: 'left' | 'center' | 'right' | 'justify', labelKey: string, icon: string }[]}
272
+ */
273
+ export const ALIGNMENT_OPTIONS = [
274
+ { type: 'left', labelKey: 'align_left', icon: 'format_align_left' },
275
+ { type: 'center', labelKey: 'align_center', icon: 'format_align_center' },
276
+ { type: 'right', labelKey: 'align_right', icon: 'format_align_right' },
277
+ { type: 'justify', labelKey: 'align_justify', icon: 'format_align_justify' },
278
+ ];
279
+
280
+ /**
281
+ * Default number of rows, including the header row, and columns of a new table.
282
+ */
283
+ export const DEFAULT_TABLE_SIZE = 3;
284
+
285
+ /**
286
+ * Maximum number of rows and columns of a new table.
287
+ */
288
+ export const MAX_TABLE_SIZE = 50;
@@ -24,9 +24,16 @@ export type InitEditorResult = {
24
24
  */
25
25
  dispose: () => void;
26
26
  };
27
+ /**
28
+ * Payload of `TOGGLE_LINK_COMMAND` with the URL and the link attributes, like `target`.
29
+ */
30
+ export type LinkAttributesWithURL = LinkAttributes & {
31
+ url: string;
32
+ };
27
33
  import type { LexicalEditor } from 'lexical';
28
34
  import type { TextEditorSelectionState } from '../../typedefs.js';
29
35
  import type { Transformer } from '@lexical/markdown';
30
36
  import type { TextEditorValueFormat } from '../../typedefs.js';
31
37
  import type { TextEditorComponent } from '../../typedefs.js';
32
38
  import type { TextEditorConfig } from '../../typedefs.js';
39
+ import type { LinkAttributes } from '@lexical/link';
@@ -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 { buildEditorFromExtensions, HorizontalRuleNode } from '@lexical/extension';
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 (typeof payload === 'string' && !isSafeLinkURL(payload)) {
707
+ if (url !== null && !isSafeLinkURL(url)) {
683
708
  return true;
684
709
  }
685
710
 
686
- toggleLink(typeof payload === 'string' ? payload : null);
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
- // `CodeNode` expects the editor to be built with `CodeExtension`, which also registers the
898
- // `CodeNode` and `CodeHighlightNode` nodes
899
- dependencies: hasCodeBlock ? [CodeExtension] : [],
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(TOGGLE_LINK_COMMAND, newURL);
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 ? { key: link.getKey(), url: link.getURL() } : undefined;
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({ linkKey: currentLink.key }, currentLink.url);
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
- * Remove the attributes not listed in {@link ALLOWED_ATTRIBUTES} from the given element.
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
- if (parent instanceof TableCellNode && parent.getChildrenSize() === 1) {
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;
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
3
3
  */
4
- export const UI_VERSION: "0.81.2";
4
+ export const UI_VERSION: "0.82.0";