@sveltia/ui 0.90.0 → 0.91.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 (75) hide show
  1. package/dist/components/text-editor/constants.d.ts +40 -6
  2. package/dist/components/text-editor/constants.js +81 -7
  3. package/dist/components/text-editor/conversion/html/components.d.ts +16 -0
  4. package/dist/components/text-editor/conversion/html/components.js +87 -0
  5. package/dist/components/text-editor/conversion/html/export.d.ts +10 -0
  6. package/dist/components/text-editor/conversion/html/export.js +360 -0
  7. package/dist/components/text-editor/conversion/html/import-rules.d.ts +12 -0
  8. package/dist/components/text-editor/conversion/html/import-rules.js +234 -0
  9. package/dist/components/text-editor/conversion/html/index.d.ts +10 -0
  10. package/dist/components/text-editor/conversion/html/index.js +33 -0
  11. package/dist/components/text-editor/conversion/html/paragraph.d.ts +1 -0
  12. package/dist/components/text-editor/conversion/html/paragraph.js +123 -0
  13. package/dist/components/text-editor/conversion/html/task-list.d.ts +13 -0
  14. package/dist/components/text-editor/conversion/html/task-list.js +82 -0
  15. package/dist/components/text-editor/conversion/html/unsupported.d.ts +2 -0
  16. package/dist/components/text-editor/conversion/html/unsupported.js +122 -0
  17. package/dist/components/text-editor/conversion/html/utils.d.ts +6 -0
  18. package/dist/components/text-editor/conversion/html/utils.js +34 -0
  19. package/dist/components/text-editor/conversion/import.js +1 -1
  20. package/dist/components/text-editor/conversion/transformers/task-list.d.ts +24 -0
  21. package/dist/components/text-editor/conversion/transformers/task-list.js +53 -0
  22. package/dist/components/text-editor/conversion/update.js +1 -1
  23. package/dist/components/text-editor/core.js +4 -12
  24. package/dist/components/text-editor/editing/commands.js +9 -18
  25. package/dist/components/text-editor/editing/guards.d.ts +7 -0
  26. package/dist/components/text-editor/editing/guards.js +123 -0
  27. package/dist/components/text-editor/editing/raw-markdown.d.ts +4 -2
  28. package/dist/components/text-editor/editing/raw-markdown.js +22 -6
  29. package/dist/components/text-editor/editing/selection.js +6 -2
  30. package/dist/components/text-editor/lexical-root.svelte +40 -0
  31. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  32. package/dist/components/text-editor/shiki/version.js +1 -1
  33. package/dist/components/text-editor/text-editor.svelte +7 -4
  34. package/dist/components/text-editor/text-editor.svelte.d.ts +4 -2
  35. package/dist/components/text-editor/toolbar/format-text-button.svelte +7 -1
  36. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +19 -8
  37. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +7 -14
  38. package/dist/locales/ar.yaml +2 -0
  39. package/dist/locales/bg.yaml +2 -0
  40. package/dist/locales/ca.yaml +2 -0
  41. package/dist/locales/cs.yaml +2 -0
  42. package/dist/locales/da.yaml +2 -0
  43. package/dist/locales/de.yaml +2 -0
  44. package/dist/locales/el.yaml +2 -0
  45. package/dist/locales/en-CA.yaml +2 -0
  46. package/dist/locales/en-GB.yaml +2 -0
  47. package/dist/locales/en-US.yaml +2 -0
  48. package/dist/locales/en-ZA.yaml +2 -0
  49. package/dist/locales/es-CO.yaml +2 -0
  50. package/dist/locales/es-ES.yaml +2 -0
  51. package/dist/locales/fa.yaml +2 -0
  52. package/dist/locales/fi.yaml +2 -0
  53. package/dist/locales/fr.yaml +2 -0
  54. package/dist/locales/he.yaml +2 -0
  55. package/dist/locales/hr.yaml +2 -0
  56. package/dist/locales/it.yaml +2 -0
  57. package/dist/locales/ja.yaml +2 -0
  58. package/dist/locales/ko.yaml +2 -0
  59. package/dist/locales/nl.yaml +2 -0
  60. package/dist/locales/pl.yaml +2 -0
  61. package/dist/locales/pt-BR.yaml +2 -0
  62. package/dist/locales/pt-PT.yaml +2 -0
  63. package/dist/locales/ru.yaml +2 -0
  64. package/dist/locales/sk.yaml +2 -0
  65. package/dist/locales/sv.yaml +2 -0
  66. package/dist/locales/tr.yaml +2 -0
  67. package/dist/locales/uk.yaml +2 -0
  68. package/dist/locales/vi.yaml +2 -0
  69. package/dist/locales/zh-CN.yaml +2 -0
  70. package/dist/locales/zh-TW.yaml +2 -0
  71. package/dist/typedefs.d.ts +5 -2
  72. package/dist/typedefs.js +3 -2
  73. package/package.json +5 -5
  74. package/dist/components/text-editor/conversion/html.d.ts +0 -30
  75. package/dist/components/text-editor/conversion/html.js +0 -857
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Splitting of an exported paragraph around the elements a paragraph can’t hold, like a figure,
3
+ * which the HTML parser would otherwise move out of it.
4
+ */
5
+
6
+ import { isDOMTextNode, isHTMLElement } from 'lexical';
7
+ import { isPreformatted, TEXT_FORMAT_TAGS } from './utils.js';
8
+
9
+ /**
10
+ * Elements a paragraph can’t hold: the HTML parser ends an open `<p>` before any of them.
11
+ * @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
12
+ */
13
+ const PARAGRAPH_ENDING_TAGS = [
14
+ ...['address', 'article', 'aside', 'blockquote', 'center', 'dd', 'details', 'dialog', 'dir'],
15
+ ...['div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3'],
16
+ ...['h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'listing', 'main', 'menu', 'nav', 'ol'],
17
+ ...['p', 'plaintext', 'pre', 'search', 'section', 'summary', 'table', 'ul', 'xmp'],
18
+ ];
19
+
20
+ /**
21
+ * Check whether the given node is an element a paragraph can’t hold, like a `<figure>`.
22
+ * @param {Node} node Node.
23
+ * @returns {boolean} Result.
24
+ */
25
+ const isParagraphEndingElement = (node) =>
26
+ isHTMLElement(node) && PARAGRAPH_ENDING_TAGS.includes(node.localName);
27
+
28
+ /**
29
+ * Find the first or last text node within the given node, or the node itself if it’s a text node.
30
+ * @param {Node | undefined} node Node.
31
+ * @param {boolean} forward Whether to look for the first text node rather than the last.
32
+ * @returns {Text | undefined} Text node, if any.
33
+ */
34
+ const findEdgeTextNode = (node, forward) => {
35
+ let current = node ?? null;
36
+
37
+ while (current && !isDOMTextNode(current)) {
38
+ current = forward ? current.firstChild : current.lastChild;
39
+ }
40
+
41
+ return current ?? undefined;
42
+ };
43
+
44
+ /**
45
+ * Names of the elements of the text formats, which hold nothing but text. See
46
+ * {@link TEXT_FORMAT_TAGS}.
47
+ */
48
+ const TEXT_FORMAT_TAG_NAMES = TEXT_FORMAT_TAGS.map(([, tagName]) => tagName);
49
+
50
+ /**
51
+ * Check whether the given node is blank: text with nothing but whitespace, or the element of a text
52
+ * format, like `<strong>`, holding only such text. Any other element, like an image, a line break
53
+ * or a link, is content, even without any text.
54
+ * @param {Node} node Node.
55
+ * @returns {boolean} Result.
56
+ */
57
+ const isBlank = (node) =>
58
+ isDOMTextNode(node)
59
+ ? !node.data.trim()
60
+ : isHTMLElement(node) &&
61
+ TEXT_FORMAT_TAG_NAMES.includes(node.localName) &&
62
+ [...node.childNodes].every(isBlank);
63
+
64
+ /**
65
+ * Split the given exported paragraph around the elements it can’t hold, like the `<figure>` an
66
+ * inline editor component exports for an image with a caption: the HTML parser would end the `<p>`
67
+ * before the first one, leaving the content after it in an empty paragraph. The rest of the content
68
+ * stays in copies of the paragraph, which keep its alignment, while the whitespace next to such an
69
+ * element is dropped, as the parser collapses it at the start and end of a paragraph.
70
+ * @param {HTMLElement} paragraph Exported paragraph element.
71
+ * @returns {DocumentFragment | undefined} Paragraphs and elements to replace the paragraph with,
72
+ * or `undefined` if it holds no such element.
73
+ */
74
+ export const splitParagraphAroundBlocks = (paragraph) => {
75
+ const children = [...paragraph.childNodes];
76
+
77
+ if (!children.some(isParagraphEndingElement)) {
78
+ return undefined;
79
+ }
80
+
81
+ const fragment = document.createDocumentFragment();
82
+ /** @type {ChildNode[]} */
83
+ let run = [];
84
+
85
+ /**
86
+ * Add the content collected since the last block, if any, to the fragment in a new paragraph.
87
+ */
88
+ const flush = () => {
89
+ const first = findEdgeTextNode(run[0], true);
90
+ const last = findEdgeTextNode(run.at(-1), false);
91
+
92
+ // Preformatted whitespace is imported as is, so it’s kept as well
93
+ if (first && !isPreformatted(first)) {
94
+ first.data = first.data.trimStart();
95
+ }
96
+
97
+ if (last && !isPreformatted(last)) {
98
+ last.data = last.data.trimEnd();
99
+ }
100
+
101
+ if (!run.every(isBlank)) {
102
+ const newParagraph = /** @type {HTMLElement} */ (paragraph.cloneNode(false));
103
+
104
+ newParagraph.append(...run);
105
+ fragment.append(newParagraph);
106
+ }
107
+
108
+ run = [];
109
+ };
110
+
111
+ children.forEach((child) => {
112
+ if (isParagraphEndingElement(child)) {
113
+ flush();
114
+ fragment.append(child);
115
+ } else {
116
+ run.push(child);
117
+ }
118
+ });
119
+
120
+ flush();
121
+
122
+ return fragment;
123
+ };
@@ -0,0 +1,13 @@
1
+ export function decorateTaskList(node: ListNode, element: HTMLElement): void;
2
+ export function decorateTaskListItem(node: ListItemNode, element: HTMLElement): void;
3
+ export function isTaskListCheckbox(element: Element): boolean;
4
+ /**
5
+ * Additional DOM conversions to import HTML with task lists. The check box of a task list item is
6
+ * dropped, as Lexical has already taken its state for the item. Any other `<input>` is still
7
+ * reported by `findUnsupportedNode()` in `unsupported.js`.
8
+ * @type {DOMConversionMap}
9
+ */
10
+ export const HTML_TASK_LIST_IMPORT_MAP: DOMConversionMap;
11
+ import type { ListNode } from '@lexical/list';
12
+ import type { ListItemNode } from '@lexical/list';
13
+ import type { DOMConversionMap } from 'lexical';
@@ -0,0 +1,82 @@
1
+ /**
2
+ * HTML of GitHub Flavored Markdown task lists, which Lexical calls check lists. They’re exported
3
+ * the way GitHub and markdown-it render them, a `<ul class="contains-task-list">` holding
4
+ * `<li class="task-list-item">` items that start with a disabled `<input type="checkbox">`, which
5
+ * Lexical imports back as a check list. The Markdown transformer is in
6
+ * `../transformers/task-list.js`.
7
+ */
8
+
9
+ import { $isListNode as isListNode } from '@lexical/list';
10
+
11
+ /**
12
+ * @import { ListItemNode, ListNode } from '@lexical/list';
13
+ * @import { DOMConversion, DOMConversionMap, DOMConversionOutput } from 'lexical';
14
+ */
15
+
16
+ /**
17
+ * Add the class of a task list to the exported element of a check list.
18
+ * @param {ListNode} node List node.
19
+ * @param {HTMLElement} element Exported element.
20
+ */
21
+ export const decorateTaskList = (node, element) => {
22
+ if (node.getListType() === 'check') {
23
+ element.classList.add('contains-task-list');
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Add the class and a disabled check box to the exported element of a check list item. An item
29
+ * that only holds a nested list has neither, as it’s merged into the previous item.
30
+ * @param {ListItemNode} node List item node.
31
+ * @param {HTMLElement} element Exported element.
32
+ */
33
+ export const decorateTaskListItem = (node, element) => {
34
+ const parent = node.getParent();
35
+
36
+ if (!isListNode(parent) || parent.getListType() !== 'check' || isListNode(node.getFirstChild())) {
37
+ return;
38
+ }
39
+
40
+ const checkbox = element.ownerDocument.createElement('input');
41
+
42
+ checkbox.type = 'checkbox';
43
+ checkbox.disabled = true;
44
+ checkbox.toggleAttribute('checked', !!node.getChecked());
45
+ element.classList.add('task-list-item');
46
+ element.prepend(checkbox, ' ');
47
+ };
48
+
49
+ /**
50
+ * Check whether the given element is the check box of a task list item, like the `<input>` in
51
+ * `<li class="task-list-item"><input type="checkbox" disabled> Item</li>`. Lexical imports the
52
+ * item as a check list item, taking its state from the first `<input>` in it.
53
+ * @param {Element} element Element.
54
+ * @returns {boolean} Result.
55
+ */
56
+ export const isTaskListCheckbox = (element) =>
57
+ element.localName === 'input' &&
58
+ element.getAttribute('type')?.toLowerCase() === 'checkbox' &&
59
+ !!element.parentElement?.matches('li.task-list-item') &&
60
+ element.parentElement.querySelector(':scope > input') === element;
61
+
62
+ /**
63
+ * Drop an element without importing it.
64
+ * @returns {DOMConversionOutput} Conversion output.
65
+ */
66
+ const dropElement = () => ({ node: null });
67
+
68
+ /**
69
+ * Additional DOM conversions to import HTML with task lists. The check box of a task list item is
70
+ * dropped, as Lexical has already taken its state for the item. Any other `<input>` is still
71
+ * reported by `findUnsupportedNode()` in `unsupported.js`.
72
+ * @type {DOMConversionMap}
73
+ */
74
+ export const HTML_TASK_LIST_IMPORT_MAP = {
75
+ /**
76
+ * Get the conversion for an `<input>` element.
77
+ * @param {HTMLElement} element Element.
78
+ * @returns {DOMConversion | null} Conversion.
79
+ */
80
+ input: (element) =>
81
+ isTaskListCheckbox(element) ? { conversion: dropElement, priority: 1 } : null,
82
+ };
@@ -0,0 +1,2 @@
1
+ export function findUnsupportedNode(editor: LexicalEditor, dom: Document): Element | Comment | undefined;
2
+ import type { LexicalEditor } from 'lexical';
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Detection of HTML the rich text editor can’t import, which can then only be edited as source.
3
+ */
4
+
5
+ import { isTaskListCheckbox } from './task-list.js';
6
+ import { editorComponents, getComponentConversionMap, getElementComponent } from './components.js';
7
+
8
+ /**
9
+ * @import { LexicalEditor } from 'lexical';
10
+ */
11
+
12
+ /**
13
+ * Table elements that have no Lexical node of their own. The rows are imported as is, while the
14
+ * column definitions are only used for the column widths.
15
+ */
16
+ const PASSTHROUGH_TAGS = ['tbody', 'thead', 'tfoot', 'colgroup', 'col'];
17
+ /**
18
+ * Elements the HTML parser always creates, which hold the content rather than being part of it.
19
+ */
20
+ const DOCUMENT_TAGS = ['html', 'head', 'body'];
21
+
22
+ /**
23
+ * Get the names of the elements only editor components import, which no other node handles.
24
+ * @param {LexicalEditor} editor Editor instance.
25
+ * @returns {Set<string>} Tag names.
26
+ */
27
+ const getComponentOnlyTagNames = (editor) => {
28
+ const components = editorComponents.get(editor) ?? [];
29
+ // The `node` is actually a node class
30
+ const componentNodes = new Set(components.map(({ node }) => /** @type {any} */ (node)));
31
+
32
+ const otherTagNames = new Set(
33
+ [...editor._nodes.values()]
34
+ .filter(({ klass }) => !componentNodes.has(klass))
35
+ .flatMap(({ klass }) => Object.keys(klass.importDOM?.() ?? {})),
36
+ );
37
+
38
+ return new Set(
39
+ components
40
+ .flatMap(({ node }) => Object.keys(getComponentConversionMap(node)))
41
+ .filter((tagName) => !otherTagNames.has(tagName)),
42
+ );
43
+ };
44
+
45
+ /**
46
+ * Get the first node in the given document that the editor cannot import, which would be silently
47
+ * dropped, possibly along with its content:
48
+ *
49
+ * - An element none of the registered nodes, including the ones of editor components, handle. This
50
+ * includes an element the parser moves to `<head>`, like a `<script>` at the beginning, and an
51
+ * element only editor components handle, but none of them imports, like a `<figure>` that isn’t an
52
+ * instance of a component for `<figure class="photo">`, which would be unwrapped.
53
+ * - A comment, like `<!-- more -->`.
54
+ *
55
+ * The content of an element an editor component imports is part of the component, which is
56
+ * expected to drop the children, and the elements within a `<pre>` element are imported as plain
57
+ * text, so they’re ignored.
58
+ * @param {LexicalEditor} editor Editor instance.
59
+ * @param {Document} dom Parsed document.
60
+ * @returns {Element | Comment | undefined} Unsupported node.
61
+ */
62
+ export const findUnsupportedNode = (editor, dom) => {
63
+ const componentOnlyTagNames = getComponentOnlyTagNames(editor);
64
+
65
+ /**
66
+ * Find an unsupported node among the descendants of the given node.
67
+ * @param {Node} parent Parent node.
68
+ * @returns {Element | Comment | undefined} Unsupported node.
69
+ */
70
+ const findWithin = (parent) => {
71
+ /** @type {Element | Comment | undefined} */
72
+ let found;
73
+
74
+ [...parent.childNodes].some((node) => {
75
+ if (node.nodeType === Node.COMMENT_NODE) {
76
+ found = /** @type {Comment} */ (node);
77
+
78
+ return true;
79
+ }
80
+
81
+ if (node.nodeType !== Node.ELEMENT_NODE) {
82
+ return false;
83
+ }
84
+
85
+ const element = /** @type {Element} */ (node);
86
+ const tagName = element.localName;
87
+
88
+ if (getElementComponent(editor, element)) {
89
+ return false;
90
+ }
91
+
92
+ // `_htmlConversions` holds the conversions of all the registered nodes, along with the ones
93
+ // in the `html.import` editor config. A conversion may still skip an element on purpose,
94
+ // like a `<br>` at the end of a paragraph, which browsers don’t render
95
+ if (
96
+ !DOCUMENT_TAGS.includes(tagName) &&
97
+ !PASSTHROUGH_TAGS.includes(tagName) &&
98
+ (!editor._htmlConversions.has(tagName) ||
99
+ componentOnlyTagNames.has(tagName) ||
100
+ // Only the check box of a task list item can be imported
101
+ (tagName === 'input' && !isTaskListCheckbox(element)))
102
+ ) {
103
+ found = element;
104
+
105
+ return true;
106
+ }
107
+
108
+ found =
109
+ tagName === 'pre'
110
+ ? /** @type {Comment | undefined} */ (
111
+ dom.createTreeWalker(element, NodeFilter.SHOW_COMMENT).nextNode() ?? undefined
112
+ )
113
+ : findWithin(element);
114
+
115
+ return !!found;
116
+ });
117
+
118
+ return found;
119
+ };
120
+
121
+ return findWithin(dom);
122
+ };
@@ -0,0 +1,6 @@
1
+ export function isPreformatted(node: Node): boolean;
2
+ /**
3
+ * Text formats and their HTML tags, from the innermost to the outermost.
4
+ * @type {[import('lexical').TextFormatType, string][]}
5
+ */
6
+ export const TEXT_FORMAT_TAGS: [import("lexical").TextFormatType, string][];
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Helpers and constants shared by the HTML import and export.
3
+ */
4
+
5
+ /**
6
+ * Check if the given node is within a `<pre>` element or an element with preformatted whitespace,
7
+ * whose text is imported by Lexical’s own conversion as is.
8
+ * @param {Node} node Node.
9
+ * @returns {boolean} Result.
10
+ */
11
+ export const isPreformatted = (node) => {
12
+ for (let element = node.parentElement; element; element = element.parentElement) {
13
+ if (element.localName === 'pre' || element.style.whiteSpace.startsWith('pre')) {
14
+ return true;
15
+ }
16
+ }
17
+
18
+ return false;
19
+ };
20
+
21
+ /**
22
+ * Text formats and their HTML tags, from the innermost to the outermost.
23
+ * @type {[import('lexical').TextFormatType, string][]}
24
+ */
25
+ export const TEXT_FORMAT_TAGS = [
26
+ ['code', 'code'],
27
+ ['subscript', 'sub'],
28
+ ['superscript', 'sup'],
29
+ ['highlight', 'mark'],
30
+ ['underline', 'u'],
31
+ ['strikethrough', 's'],
32
+ ['italic', 'em'],
33
+ ['bold', 'strong'],
34
+ ];
@@ -19,7 +19,7 @@ import {
19
19
  $isTextNode as isTextNode,
20
20
  } from 'lexical';
21
21
  import { IMPORT_UPDATE_TAG } from '../constants.js';
22
- import { exportHtml, findUnsupportedNode, getCodeLanguage } from './html.js';
22
+ import { exportHtml, findUnsupportedNode, getCodeLanguage } from './html/index.js';
23
23
  import {
24
24
  addImportPlaceholders,
25
25
  convertSetextHeadings,
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @import { ElementTransformer } from '@lexical/markdown';
3
+ */
4
+ /**
5
+ * Start of a task list item as GitHub Flavored Markdown defines it: a bullet list marker followed
6
+ * by a check box holding a space or `x`, like `- [ ] item`. Like Lexical’s `CHECK_LIST`, it allows
7
+ * one or two spaces between them; with more, the line is a bulleted list item starting with `[ ]`.
8
+ * Like the other block checks in the plain text mode, it doesn’t allow any indentation, which a
9
+ * nested item has.
10
+ * @see https://github.github.com/gfm/#task-list-items-extension-
11
+ */
12
+ export const TASK_LIST_ITEM_REGEX: RegExp;
13
+ /**
14
+ * Transformer for task list items, based on Lexical’s `CHECK_LIST`, which also takes a line without
15
+ * a list marker or with an empty check box, like `[x] Agreed` or `[] Empty`, for a task list item.
16
+ * That suits a Markdown shortcut, like typing `[ ] ` at the start of a paragraph, but would turn
17
+ * such a paragraph into a task list when Markdown is imported, so an import only takes a line that
18
+ * matches {@link TASK_LIST_ITEM_REGEX}, leaving any other line to the other transformers. Lexical
19
+ * has already removed the matched text from the line by then, and doesn’t put it back when a
20
+ * transformer leaves the line, so it’s restored here.
21
+ * @type {ElementTransformer}
22
+ */
23
+ export const TASK_LIST: ElementTransformer;
24
+ import type { ElementTransformer } from '@lexical/markdown';
@@ -0,0 +1,53 @@
1
+ import { CHECK_LIST } from '@lexical/markdown';
2
+ import { $isTextNode as isTextNode } from 'lexical';
3
+
4
+ /**
5
+ * @import { ElementTransformer } from '@lexical/markdown';
6
+ */
7
+
8
+ /**
9
+ * Start of a task list item as GitHub Flavored Markdown defines it: a bullet list marker followed
10
+ * by a check box holding a space or `x`, like `- [ ] item`. Like Lexical’s `CHECK_LIST`, it allows
11
+ * one or two spaces between them; with more, the line is a bulleted list item starting with `[ ]`.
12
+ * Like the other block checks in the plain text mode, it doesn’t allow any indentation, which a
13
+ * nested item has.
14
+ * @see https://github.github.com/gfm/#task-list-items-extension-
15
+ */
16
+ export const TASK_LIST_ITEM_REGEX = /^[-*+][ \t]{1,2}\[[ xX]\][ \t]/;
17
+
18
+ /**
19
+ * Transformer for task list items, based on Lexical’s `CHECK_LIST`, which also takes a line without
20
+ * a list marker or with an empty check box, like `[x] Agreed` or `[] Empty`, for a task list item.
21
+ * That suits a Markdown shortcut, like typing `[ ] ` at the start of a paragraph, but would turn
22
+ * such a paragraph into a task list when Markdown is imported, so an import only takes a line that
23
+ * matches {@link TASK_LIST_ITEM_REGEX}, leaving any other line to the other transformers. Lexical
24
+ * has already removed the matched text from the line by then, and doesn’t put it back when a
25
+ * transformer leaves the line, so it’s restored here.
26
+ * @type {ElementTransformer}
27
+ */
28
+ export const TASK_LIST = {
29
+ ...CHECK_LIST,
30
+ /**
31
+ * Replace the matched line with a task list item, unless it’s imported and isn’t one.
32
+ * @param {Parameters<ElementTransformer['replace']>} args Arguments.
33
+ * @returns {boolean | void} `false` if the line isn’t a task list item.
34
+ */
35
+ replace: (...args) => {
36
+ const [, children, match, isImport] = args;
37
+
38
+ // The match includes the indentation of a nested item
39
+ if (isImport && !TASK_LIST_ITEM_REGEX.test(match[0].trimStart())) {
40
+ const [textNode] = children;
41
+
42
+ if (isTextNode(textNode)) {
43
+ textNode.setTextContent(
44
+ /** @type {string} */ (/** @type {RegExpMatchArray} */ (match).input),
45
+ );
46
+ }
47
+
48
+ return false;
49
+ }
50
+
51
+ return CHECK_LIST.replace(...args);
52
+ },
53
+ };
@@ -5,7 +5,7 @@
5
5
  import { $convertToMarkdownString as convertToMarkdownString } from '@lexical/markdown';
6
6
  import { sleep } from '@sveltia/utils/misc';
7
7
  import { DISABLED_MARKDOWN_TAGS } from '../constants.js';
8
- import { exportHtml } from './html.js';
8
+ import { exportHtml } from './html/index.js';
9
9
  import { trimBlankBlockquoteLines } from './markdown.js';
10
10
  import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
11
11
  import { getSelectionTypes } from '../editing/selection.js';
@@ -11,12 +11,7 @@ import { registerRichText } from '@lexical/rich-text';
11
11
  import { TableCellNode, TableExtension, TableNode, TableRowNode } from '@lexical/table';
12
12
  import { configExtension, defineExtension } from 'lexical';
13
13
  import { EDITOR_THEME, NODE_MAP, TRANSFORMER_MAP } from './constants.js';
14
- import {
15
- editorComponents,
16
- HTML_CODE_IMPORT_MAP,
17
- HTML_EXPORT_MAP,
18
- HTML_IMPORT_MAP,
19
- } from './conversion/html.js';
14
+ import { editorComponents, getHtmlConfig } from './conversion/html/index.js';
20
15
  import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js';
21
16
  import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
22
17
  import { CODE } from './conversion/transformers/code.js';
@@ -31,6 +26,7 @@ import {
31
26
  registerListCommands,
32
27
  registerTabIndentation,
33
28
  } from './editing/commands.js';
29
+ import { registerGuards } from './editing/guards.js';
34
30
  import { mergeUnregisters } from './utils.js';
35
31
  import { registerLinkCommands } from './editing/link.js';
36
32
  import { registerUpdateEvent } from './conversion/update.js';
@@ -174,14 +170,9 @@ const createEditorExtension = ({
174
170
  ...(hasBlockSyntax ? [HorizontalRuleNode, TableNode, TableCellNode, TableRowNode] : []),
175
171
  ],
176
172
  theme: EDITOR_THEME,
177
- // Export clean HTML without the attributes and elements only meant for the editor, and import
178
- // code blocks with their language. The export map also applies to copying
179
173
  html:
180
174
  format === 'html'
181
- ? {
182
- export: HTML_EXPORT_MAP,
183
- import: { ...HTML_IMPORT_MAP, ...(hasCodeBlock ? HTML_CODE_IMPORT_MAP : {}) },
184
- }
175
+ ? getHtmlConfig({ hasCodeBlock, hasTaskList: enabledButtons.includes('task-list') })
185
176
  : undefined,
186
177
  /**
187
178
  * Log an error like `createEditor` does by default, instead of throwing it.
@@ -286,6 +277,7 @@ const registerEditorListeners = (
286
277
  })
287
278
  : undefined,
288
279
  registerListCommands(editor, enabledButtons),
280
+ isCodeEditor ? undefined : registerGuards(editor, { format, enabledButtons }),
289
281
  registerUpdateEvent(editor, enabledTransformers, format),
290
282
  registerTabIndentation(editor),
291
283
  isCodeEditor ? undefined : registerPasteAsMarkdown(editor, enabledTransformers),
@@ -1,6 +1,5 @@
1
1
  /**
2
- * Commands of the code editor, lists and indentation, and the helper to combine the cleanup
3
- * handlers of the registered listeners.
2
+ * Commands of the code editor, lists and indentation.
4
3
  */
5
4
 
6
5
  import {
@@ -10,10 +9,9 @@ import {
10
9
  } from '@lexical/code-core';
11
10
  import {
12
11
  $handleListInsertParagraph as handleListInsertParagraph,
13
- INSERT_ORDERED_LIST_COMMAND,
14
- INSERT_UNORDERED_LIST_COMMAND,
15
12
  $insertList as insertList,
16
13
  $isListItemNode as isListItemNode,
14
+ registerCheckList,
17
15
  } from '@lexical/list';
18
16
  import { $getNearestNodeOfType as getNearestNodeOfType } from '@lexical/utils';
19
17
  import {
@@ -30,12 +28,12 @@ import {
30
28
  RootNode,
31
29
  } from 'lexical';
32
30
  import { getNearestElementNode } from './selection.js';
31
+ import { LIST_TYPES } from '../constants.js';
33
32
  import { mergeUnregisters } from '../utils.js';
34
33
 
35
34
  /**
36
35
  * @import { LexicalEditor } from 'lexical';
37
- * @import { ListType } from '@lexical/list';
38
- * @import { TextEditorBlockType, TextEditorConfig } from '../../../typedefs.js';
36
+ * @import { TextEditorConfig } from '../../../typedefs.js';
39
37
  */
40
38
 
41
39
  /**
@@ -116,25 +114,17 @@ export const registerCodeEditorCommands = (editor, defaultLanguage) =>
116
114
  );
117
115
 
118
116
  /**
119
- * List buttons, along with the command to insert the list and its type.
120
- * @type {[TextEditorBlockType, typeof INSERT_UNORDERED_LIST_COMMAND, ListType][]}
121
- */
122
- const LIST_COMMANDS = [
123
- ['bulleted-list', INSERT_UNORDERED_LIST_COMMAND, 'bullet'],
124
- ['numbered-list', INSERT_ORDERED_LIST_COMMAND, 'number'],
125
- ];
126
-
127
- /**
128
- * Register the commands for the enabled list buttons.
117
+ * Register the commands for the enabled list buttons. With the task list button, a check box can be
118
+ * toggled with a click or the Space key.
129
119
  * @param {LexicalEditor} editor Editor instance.
130
120
  * @param {TextEditorConfig['enabledButtons']} enabledButtons Enabled buttons.
131
121
  * @returns {() => void} Cleanup handler.
132
122
  */
133
123
  export const registerListCommands = (editor, enabledButtons = []) => {
134
- const enabledLists = LIST_COMMANDS.filter(([button]) => enabledButtons.includes(button));
124
+ const enabledLists = LIST_TYPES.filter(({ type }) => enabledButtons.includes(type));
135
125
 
136
126
  return mergeUnregisters(
137
- ...enabledLists.map(([, command, listType]) =>
127
+ ...enabledLists.map(({ command, listType }) =>
138
128
  editor.registerCommand(
139
129
  command,
140
130
  () => {
@@ -153,6 +143,7 @@ export const registerListCommands = (editor, enabledButtons = []) => {
153
143
  COMMAND_PRIORITY_NORMAL,
154
144
  )
155
145
  : undefined,
146
+ enabledButtons.includes('task-list') ? registerCheckList(editor) : undefined,
156
147
  );
157
148
  };
158
149
 
@@ -0,0 +1,7 @@
1
+ export function registerGuards(editor: LexicalEditor, { format, enabledButtons }: {
2
+ format: TextEditorValueFormat;
3
+ enabledButtons: TextEditorConfig["enabledButtons"];
4
+ }): () => void;
5
+ import type { LexicalEditor } from 'lexical';
6
+ import type { TextEditorValueFormat } from '../../../typedefs.js';
7
+ import type { TextEditorConfig } from '../../../typedefs.js';