@sveltia/ui 0.79.0 → 0.79.2

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.
@@ -86,6 +86,7 @@ import {
86
86
  } from './shiki/facade.js';
87
87
  import { registerCodeHighlighting, shikiTokenizer } from './shiki/highlighter.js';
88
88
  import { getCodeTheme, observeCodeTheme } from './shiki/theme.js';
89
+ import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
89
90
  import { HR } from './transformers/hr.js';
90
91
  import { TABLE } from './transformers/table.js';
91
92
 
@@ -455,9 +456,13 @@ export const getSelectionTypes = () => {
455
456
  * @returns {string} Markdown value.
456
457
  */
457
458
  export const exportMarkdown = (enabledTransformers) => {
458
- const transformers = enabledTransformers.filter(
459
- (/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
460
- );
459
+ const transformers = [
460
+ ...enabledTransformers.filter(
461
+ (/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
462
+ ),
463
+ // Last, so a transformer of the editor’s own, such as a link, comes first
464
+ BLOCK_SYNTAX_ESCAPE,
465
+ ];
461
466
 
462
467
  return trimBlankBlockquoteLines(
463
468
  convertToMarkdownString(transformers)
@@ -88,7 +88,9 @@
88
88
  // The content has been converted, if it has changed at all
89
89
  editorStore.pending = false;
90
90
 
91
- if (hasConverterError || !useRichText) {
91
+ // Ignore the content while a value is being imported, like the empty code block the editor
92
+ // starts with: it would overwrite the value, which then replaces the content once imported
93
+ if (hasConverterError || !useRichText || editorStore.importing) {
92
94
  return;
93
95
  }
94
96
 
@@ -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.79.0";
4
+ export const UI_VERSION: "0.79.2";
@@ -3,4 +3,4 @@
3
3
  /**
4
4
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
5
5
  */
6
- export const UI_VERSION = '0.79.0';
6
+ export const UI_VERSION = '0.79.2';
@@ -50,6 +50,16 @@ export const createEditorStore = () => {
50
50
  * @type {{ source: string, exported: string } | undefined}
51
51
  */
52
52
  let lastImport = undefined;
53
+ /**
54
+ * Number of imports started, to tell the latest one.
55
+ * @type {number}
56
+ */
57
+ let importCount = 0;
58
+ /**
59
+ * Whether the latest import is still in progress.
60
+ * @type {boolean}
61
+ */
62
+ let importing = false;
53
63
 
54
64
  /**
55
65
  * Flag a conversion error, which takes the editor out of the rich text mode, and shows the error
@@ -76,6 +86,12 @@ export const createEditorStore = () => {
76
86
 
77
87
  const originalValue = inputValue;
78
88
 
89
+ importCount += 1;
90
+
91
+ const importId = importCount;
92
+
93
+ importing = true;
94
+
79
95
  try {
80
96
  // We should avoid an empty editor; there should be at least one `<p>`, so give it an empty
81
97
  // string if the `value` is `undefined`
@@ -96,6 +112,11 @@ export const createEditorStore = () => {
96
112
  inputValue = originalValue;
97
113
  // eslint-disable-next-line no-console
98
114
  console.error(ex);
115
+ } finally {
116
+ // An earlier import finishing last doesn’t end the latest one
117
+ if (importId === importCount) {
118
+ importing = false;
119
+ }
99
120
  }
100
121
  };
101
122
 
@@ -179,6 +200,9 @@ export const createEditorStore = () => {
179
200
  set pending(newValue) {
180
201
  pending = newValue;
181
202
  },
203
+ get importing() {
204
+ return importing;
205
+ },
182
206
  editorId,
183
207
  convertMarkdown,
184
208
  /**
@@ -0,0 +1,10 @@
1
+ export function escapeBlockSyntax(text: string): string;
2
+ /**
3
+ * Export-only transformer that escapes the Markdown block syntax at the start of a block or of a
4
+ * line after a line break. Without it, a paragraph typed as `## Hours` with the Markdown shortcuts
5
+ * turned off, pasted as text, or imported from the escaped `\## Hours` would be exported as is and
6
+ * become a heading the next time the Markdown is read.
7
+ * @type {TextMatchTransformer}
8
+ */
9
+ export const BLOCK_SYNTAX_ESCAPE: TextMatchTransformer;
10
+ import type { TextMatchTransformer } from '@lexical/markdown';
@@ -0,0 +1,65 @@
1
+ import { $isLineBreakNode as isLineBreakNode, $isTextNode as isTextNode } from 'lexical';
2
+
3
+ /**
4
+ * @import { TextMatchTransformer } from '@lexical/markdown';
5
+ * @import { ElementNode, LexicalNode, TextNode } from 'lexical';
6
+ */
7
+
8
+ /**
9
+ * Escape the Markdown block syntax at the start of a line, so text that only looks like a heading,
10
+ * a list item, a blockquote, a setext heading underline or a thematic break stays text when the
11
+ * Markdown is read again. Lexical already escapes the inline syntax characters, including `*`, `_`,
12
+ * `` ` `` and `~`.
13
+ * @param {string} text Exported Markdown of a line start.
14
+ * @returns {string} Escaped Markdown.
15
+ */
16
+ export const escapeBlockSyntax = (text) =>
17
+ text
18
+ // ATX heading, bullet list item, blockquote, setext heading underline or thematic break
19
+ .replace(/^( {0,3})(#{1,6}(?=[ \t]|$)|[-+](?=[ \t]|$)|>|=+[ \t]*$|-{2,}[ \t]*$)/, '$1\\$2')
20
+ // Ordered list item, whose number can’t be escaped, unlike the delimiter after it
21
+ .replace(/^( {0,3}\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2');
22
+
23
+ /**
24
+ * Export-only transformer that escapes the Markdown block syntax at the start of a block or of a
25
+ * line after a line break. Without it, a paragraph typed as `## Hours` with the Markdown shortcuts
26
+ * turned off, pasted as text, or imported from the escaped `\## Hours` would be exported as is and
27
+ * become a heading the next time the Markdown is read.
28
+ * @type {TextMatchTransformer}
29
+ */
30
+ export const BLOCK_SYNTAX_ESCAPE = {
31
+ dependencies: [],
32
+ /**
33
+ * Export a text node at the start of a line, escaping the block syntax it starts with.
34
+ * @param {LexicalNode} node Node.
35
+ * @param {(node: ElementNode) => string} _exportChildren Function to export the children of an
36
+ * element, unused.
37
+ * @param {(node: TextNode, textContent: string) => string} exportFormat Function to export a text
38
+ * node with its format.
39
+ * @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
40
+ */
41
+ export: (node, _exportChildren, exportFormat) => {
42
+ if (!isTextNode(node)) {
43
+ return null;
44
+ }
45
+
46
+ const parent = node.getParent();
47
+ const previous = node.getPreviousSibling();
48
+
49
+ // Text in a link or other inline element, in a code block, which is exported verbatim, or in a
50
+ // heading, whose content is only parsed for inline syntax
51
+ if (!parent || parent.isInline() || ['code', 'heading'].includes(parent.getType())) {
52
+ return null;
53
+ }
54
+
55
+ if (previous && !isLineBreakNode(previous)) {
56
+ return null;
57
+ }
58
+
59
+ // A formatted node starts with its own syntax, such as `**`, which is left as is
60
+ return escapeBlockSyntax(exportFormat(node, node.getTextContent()));
61
+ },
62
+ // Never used to import Markdown or as a shortcut
63
+ regExp: /(?!)/,
64
+ type: 'text-match',
65
+ };
@@ -1048,6 +1048,11 @@ export type TextEditorStore = {
1048
1048
  * has yet to convert it to Markdown and update {@link TextEditorStore.inputValue}.
1049
1049
  */
1050
1050
  pending: boolean;
1051
+ /**
1052
+ * Whether the latest {@link TextEditorStore.inputValue} is still
1053
+ * being imported to the Lexical editor.
1054
+ */
1055
+ importing: boolean;
1051
1056
  /**
1052
1057
  * Function to trigger the Lexical converter.
1053
1058
  */
package/dist/typedefs.js CHANGED
@@ -448,6 +448,8 @@
448
448
  * @property {boolean} showConverterError Whether to show a converter error in the UI.
449
449
  * @property {boolean} pending Whether the user has changed the rich text content, and the editor
450
450
  * has yet to convert it to Markdown and update {@link TextEditorStore.inputValue}.
451
+ * @property {boolean} importing Whether the latest {@link TextEditorStore.inputValue} is still
452
+ * being imported to the Lexical editor.
451
453
  * @property {() => Promise<void>} convertMarkdown Function to trigger the Lexical converter.
452
454
  * @property {(value: string) => string | undefined} getImportedValue Function to get the
453
455
  * {@link TextEditorStore.inputValue} last imported if the given value, exported by the editor, is
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltia/ui",
3
- "version": "0.79.0",
3
+ "version": "0.79.2",
4
4
  "description": "A collection of Svelte components and utilities for building user interfaces.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -80,7 +80,7 @@
80
80
  "eslint-config-airbnb-extended": "^3.3.0",
81
81
  "eslint-config-prettier": "^10.1.8",
82
82
  "eslint-plugin-import": "^2.32.0",
83
- "eslint-plugin-jsdoc": "^65.0.2",
83
+ "eslint-plugin-jsdoc": "^65.1.0",
84
84
  "eslint-plugin-package-json": "^1.10.1",
85
85
  "eslint-plugin-svelte": "^3.23.0",
86
86
  "globals": "^17.13.0",