@sveltia/ui 0.80.2 → 0.80.4
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/text-editor/core.js +26 -14
- package/dist/components/text-editor/floating-link-editor.svelte +6 -1
- package/dist/components/text-editor/markdown.d.ts +21 -0
- package/dist/components/text-editor/markdown.js +305 -36
- 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/text-editor.svelte +8 -1
- package/dist/components/text-editor/transformers/block-escape.d.ts +8 -4
- package/dist/components/text-editor/transformers/block-escape.js +111 -14
- package/dist/components/text-editor/transformers/table.js +14 -3
- package/dist/components/text-field/number-input.d.ts +2 -0
- package/dist/components/text-field/number-input.js +56 -0
- package/dist/components/text-field/number-input.svelte +12 -9
- package/dist/shiki-engine.js +3 -3
- package/package.json +9 -9
|
@@ -21,16 +21,103 @@ export const escapeBlockSyntax = (text) =>
|
|
|
21
21
|
.replace(/^( {0,3}\d{1,9})([.)])(?=[ \t]|$)/, '$1\\$2');
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
24
|
+
* Check if the given character is a word character, which is neither whitespace nor punctuation
|
|
25
|
+
* (or a symbol). An underscore next to one on both sides can’t open or close emphasis.
|
|
26
|
+
* @param {string | undefined} char Character.
|
|
27
|
+
* @returns {boolean} Result.
|
|
28
|
+
* @see https://spec.commonmark.org/0.31.2/#left-flanking-delimiter-run
|
|
29
|
+
*/
|
|
30
|
+
const isWordChar = (char) => !!char && /[^\s\p{P}\p{S}]/u.test(char);
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Remove the backslashes Lexical adds before underscores and backslashes in text, where they’re not
|
|
34
|
+
* needed, so `snake_case` and `C:\Users` aren’t exported as `snake\_case` and `C:\\Users`. Unlike
|
|
35
|
+
* a global replacement, the escapes that are needed are kept: an underscore that could open or
|
|
36
|
+
* close emphasis, like that in `\_not italic\_`, and a backslash that would otherwise escape the
|
|
37
|
+
* next character or make a hard line break. Also replace the encoded spaces Lexical outputs around
|
|
38
|
+
* formatted text, which can appear with a combination of bold and italic text.
|
|
39
|
+
* @param {string} markdown Markdown exported from a text node with its format, in which every
|
|
40
|
+
* backslash starts an escape.
|
|
41
|
+
* @returns {string} Tidied Markdown.
|
|
42
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/430
|
|
43
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/512
|
|
44
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/511
|
|
45
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/534
|
|
46
|
+
*/
|
|
47
|
+
export const unescapeText = (markdown) =>
|
|
48
|
+
markdown
|
|
49
|
+
.replace(/((?:\\_)+)|\\(.)/gs, (match, underscores, char, offset, string) => {
|
|
50
|
+
if (underscores) {
|
|
51
|
+
return isWordChar(string[offset - 1]) && isWordChar(string[offset + match.length])
|
|
52
|
+
? underscores.replaceAll('\\', '')
|
|
53
|
+
: match;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (char === '\\') {
|
|
57
|
+
const next = string[offset + 2];
|
|
58
|
+
|
|
59
|
+
// The backslash is literal unless it’s followed by ASCII punctuation, which it would
|
|
60
|
+
// escape, or a line break, which it would turn into a hard line break. The end of the node
|
|
61
|
+
// is kept escaped, as the next node may start with either
|
|
62
|
+
return next && !/[\n\r!-/:-@[-`{-~]/.test(next) ? char : match;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return match;
|
|
66
|
+
})
|
|
67
|
+
.replace(/ /g, ' ');
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Check if the given text node is italic and the italic text it’s part of is next to a word
|
|
71
|
+
* character, like `b` in `a*b*c`. The italic text then needs the `*` delimiter instead of `_`,
|
|
72
|
+
* which can’t be used within a word.
|
|
73
|
+
* @param {TextNode} node Text node.
|
|
74
|
+
* @returns {boolean} Result.
|
|
75
|
+
*/
|
|
76
|
+
const isItalicWithinWord = (node) => {
|
|
77
|
+
if (!node.hasFormat('italic')) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if the given node is italic text.
|
|
83
|
+
* @param {LexicalNode | null} sibling Node.
|
|
84
|
+
* @returns {sibling is TextNode} Result.
|
|
85
|
+
*/
|
|
86
|
+
const isItalicText = (sibling) => isTextNode(sibling) && sibling.hasFormat('italic');
|
|
87
|
+
let first = node;
|
|
88
|
+
let last = node;
|
|
89
|
+
|
|
90
|
+
while (isItalicText(first.getPreviousSibling())) {
|
|
91
|
+
first = /** @type {TextNode} */ (first.getPreviousSibling());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
while (isItalicText(last.getNextSibling())) {
|
|
95
|
+
last = /** @type {TextNode} */ (last.getNextSibling());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Lexical puts the delimiters inside the leading and trailing whitespace, if any
|
|
99
|
+
const firstText = first.getTextContent();
|
|
100
|
+
const lastText = last.getTextContent();
|
|
101
|
+
const before = /^\s/.test(firstText) ? ' ' : first.getPreviousSibling()?.getTextContent().at(-1);
|
|
102
|
+
const after = /\s$/.test(lastText) ? ' ' : last.getNextSibling()?.getTextContent()[0];
|
|
103
|
+
|
|
104
|
+
return isWordChar(before) || isWordChar(after);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Export-only transformer that tidies up the Markdown of a text node that isn’t inline code, which
|
|
109
|
+
* Lexical exports verbatim. It removes the unnecessary escapes Lexical adds; see `unescapeText()`.
|
|
110
|
+
* It uses `*` instead of `_` for italic text within a word, like `a*b*c`, which would otherwise be
|
|
111
|
+
* exported as `a_b_c`, plain text. Last, it escapes the Markdown block syntax at the start of a
|
|
112
|
+
* block or of a line after a line break. Without it, a paragraph typed as `## Hours` with the
|
|
113
|
+
* Markdown shortcuts turned off, pasted as text, or imported from the escaped `\## Hours` would be
|
|
114
|
+
* exported as is and become a heading the next time the Markdown is read.
|
|
28
115
|
* @type {TextMatchTransformer}
|
|
29
116
|
*/
|
|
30
117
|
export const BLOCK_SYNTAX_ESCAPE = {
|
|
31
118
|
dependencies: [],
|
|
32
119
|
/**
|
|
33
|
-
* Export a text node
|
|
120
|
+
* Export a text node.
|
|
34
121
|
* @param {LexicalNode} node Node.
|
|
35
122
|
* @param {(node: ElementNode) => string} _exportChildren Function to export the children of an
|
|
36
123
|
* element, unused.
|
|
@@ -39,25 +126,35 @@ export const BLOCK_SYNTAX_ESCAPE = {
|
|
|
39
126
|
* @returns {string | null} Markdown, or `null` to leave the node to the other transformers.
|
|
40
127
|
*/
|
|
41
128
|
export: (node, _exportChildren, exportFormat) => {
|
|
42
|
-
if (!isTextNode(node)) {
|
|
129
|
+
if (!isTextNode(node) || node.hasFormat('code')) {
|
|
43
130
|
return null;
|
|
44
131
|
}
|
|
45
132
|
|
|
133
|
+
let markdown = exportFormat(node, node.getTextContent());
|
|
134
|
+
|
|
135
|
+
if (isItalicWithinWord(node)) {
|
|
136
|
+
// Every underscore in the text is escaped, so an unescaped one is an italic delimiter
|
|
137
|
+
markdown = markdown.replace(/\\.|_/gs, (match) => (match === '_' ? '*' : match));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
markdown = unescapeText(markdown);
|
|
141
|
+
|
|
46
142
|
const parent = node.getParent();
|
|
47
143
|
const previous = node.getPreviousSibling();
|
|
48
144
|
|
|
49
145
|
// 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 (
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
146
|
+
// heading, whose content is only parsed for inline syntax, or text after the start of a line
|
|
147
|
+
if (
|
|
148
|
+
!parent ||
|
|
149
|
+
parent.isInline() ||
|
|
150
|
+
['code', 'heading'].includes(parent.getType()) ||
|
|
151
|
+
(previous && !isLineBreakNode(previous))
|
|
152
|
+
) {
|
|
153
|
+
return markdown;
|
|
57
154
|
}
|
|
58
155
|
|
|
59
156
|
// A formatted node starts with its own syntax, such as `**`, which is left as is
|
|
60
|
-
return escapeBlockSyntax(
|
|
157
|
+
return escapeBlockSyntax(markdown);
|
|
61
158
|
},
|
|
62
159
|
// Never used to import Markdown or as a shortcut
|
|
63
160
|
regExp: /(?!)/,
|
|
@@ -23,6 +23,8 @@ import {
|
|
|
23
23
|
} from '@lexical/table';
|
|
24
24
|
import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from 'lexical';
|
|
25
25
|
|
|
26
|
+
import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
|
|
27
|
+
|
|
26
28
|
/**
|
|
27
29
|
* @import { ElementTransformer } from '@lexical/markdown';
|
|
28
30
|
*/
|
|
@@ -48,7 +50,8 @@ export const getTableColumnsSize = (table) => {
|
|
|
48
50
|
* @returns {TableCellNode}
|
|
49
51
|
*/
|
|
50
52
|
const createTableCell = (textContent) => {
|
|
51
|
-
|
|
53
|
+
// An escaped pipe is part of the cell, even within inline code, like GitHub Flavored Markdown
|
|
54
|
+
textContent = textContent.replace(/\\n/g, '\n').replace(/\\\|/g, '|');
|
|
52
55
|
|
|
53
56
|
const cell = createTableCellNode(TableCellHeaderStates.NO_STATUS);
|
|
54
57
|
|
|
@@ -70,7 +73,9 @@ export const mapToTableCells = (textContent) => {
|
|
|
70
73
|
return null;
|
|
71
74
|
}
|
|
72
75
|
|
|
73
|
-
|
|
76
|
+
// Split the row at the pipes that are not escaped, which are preceded by an even number of
|
|
77
|
+
// backslashes
|
|
78
|
+
return match.split(/(?<=(?<!\\)(?:\\\\)*)\|/).map((text) => createTableCell(text));
|
|
74
79
|
};
|
|
75
80
|
|
|
76
81
|
/**
|
|
@@ -99,7 +104,13 @@ export const TABLE = {
|
|
|
99
104
|
row.getChildren().forEach((cell) => {
|
|
100
105
|
// It’s TableCellNode so it’s just to make flow happy
|
|
101
106
|
if (isTableCellNode(cell)) {
|
|
102
|
-
rowOutput.push(
|
|
107
|
+
rowOutput.push(
|
|
108
|
+
convertToMarkdownString([...TRANSFORMERS, BLOCK_SYNTAX_ESCAPE], cell)
|
|
109
|
+
// Escape a pipe, which would otherwise end the cell, even within inline code
|
|
110
|
+
.replace(/\|/g, '\\|')
|
|
111
|
+
.replace(/\n/g, '\\n')
|
|
112
|
+
.trim(),
|
|
113
|
+
);
|
|
103
114
|
|
|
104
115
|
if (cell.__headerState === TableCellHeaderStates.ROW) {
|
|
105
116
|
isHeaderRow = true;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export function getMaximumFractionDigits(step: number): number;
|
|
2
|
+
export function getDecimalSeparator(locale: string | undefined): string;
|
|
3
|
+
export function normalizeNumber(inputValue: string, locale: string | undefined): string;
|
|
2
4
|
export function parseNumber(inputValue: string): number | undefined;
|
|
3
5
|
export function isAtMin(inputValue: string, min: number | undefined): boolean;
|
|
4
6
|
export function isAtMax(inputValue: string, max: number | undefined): boolean;
|
|
@@ -17,6 +17,62 @@ export const getMaximumFractionDigits = (step) => {
|
|
|
17
17
|
return Math.max(digits, 0);
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Decimal separators of the locales looked up, keyed by locale. The separator is read on every
|
|
22
|
+
* keystroke, while creating an `Intl.NumberFormat` is comparatively expensive.
|
|
23
|
+
* @type {Map<string, string>}
|
|
24
|
+
*/
|
|
25
|
+
const decimalSeparatorCache = new Map();
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get the decimal separator of the given locale, like `,` in German.
|
|
29
|
+
* @param {string | undefined} locale Locale, like `de`. If omitted, the browser’s default locale is
|
|
30
|
+
* used.
|
|
31
|
+
* @returns {string} Decimal separator. `.` if the locale is invalid.
|
|
32
|
+
*/
|
|
33
|
+
export const getDecimalSeparator = (locale) => {
|
|
34
|
+
const key = locale || '';
|
|
35
|
+
let separator = decimalSeparatorCache.get(key);
|
|
36
|
+
|
|
37
|
+
if (separator === undefined) {
|
|
38
|
+
try {
|
|
39
|
+
separator = /** @type {Intl.NumberFormatPart} */ (
|
|
40
|
+
new Intl.NumberFormat(locale || undefined)
|
|
41
|
+
.formatToParts(1.5)
|
|
42
|
+
.find(({ type }) => type === 'decimal')
|
|
43
|
+
).value;
|
|
44
|
+
} catch {
|
|
45
|
+
separator = '.';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
decimalSeparatorCache.set(key, separator);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return separator;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Normalize the text entered in the field, so a number typed with the locale’s decimal separator,
|
|
56
|
+
* like `1,5` in German, can be parsed as `1.5`. A period is always accepted as the decimal
|
|
57
|
+
* separator, as a value set from outside is shown with one, so the text is left as is if it has
|
|
58
|
+
* one, or more than one decimal separator of the locale. Thousands separators are not supported:
|
|
59
|
+
* `1,500` in English, whose decimal separator is a period, remains no number.
|
|
60
|
+
* @param {string} inputValue Text in the field.
|
|
61
|
+
* @param {string | undefined} locale Locale of the field.
|
|
62
|
+
* @returns {string} Normalized text.
|
|
63
|
+
*/
|
|
64
|
+
export const normalizeNumber = (inputValue, locale) => {
|
|
65
|
+
const separator = getDecimalSeparator(locale);
|
|
66
|
+
|
|
67
|
+
if (!inputValue || separator === '.' || inputValue.includes('.')) {
|
|
68
|
+
return inputValue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const parts = inputValue.split(separator);
|
|
72
|
+
|
|
73
|
+
return parts.length === 2 ? parts.join('.') : inputValue;
|
|
74
|
+
};
|
|
75
|
+
|
|
20
76
|
/**
|
|
21
77
|
* Parse the text entered in the field.
|
|
22
78
|
* @param {string} inputValue Text in the field.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
@see https://w3c.github.io/aria/#textbox
|
|
6
6
|
-->
|
|
7
7
|
<script>
|
|
8
|
-
import { _ } from '@sveltia/i18n';
|
|
8
|
+
import { _, locale } from '@sveltia/i18n';
|
|
9
9
|
import { untrack } from 'svelte';
|
|
10
10
|
import Button from '../button/button.svelte';
|
|
11
11
|
import Icon from '../icon/icon.svelte';
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
isAtMax,
|
|
15
15
|
isAtMin,
|
|
16
16
|
isInvalidNumber,
|
|
17
|
+
normalizeNumber,
|
|
17
18
|
parseNumber,
|
|
18
19
|
stepNumber,
|
|
19
20
|
} from './number-input.js';
|
|
@@ -67,8 +68,10 @@
|
|
|
67
68
|
let inputValue = $state('');
|
|
68
69
|
|
|
69
70
|
const maximumFractionDigits = $derived(getMaximumFractionDigits(step));
|
|
70
|
-
|
|
71
|
-
const
|
|
71
|
+
// Accept the decimal separator of the field’s language, or that of the UI, like `,` in German
|
|
72
|
+
const numberValue = $derived(normalizeNumber(inputValue, restProps.lang ?? locale.current));
|
|
73
|
+
const isMin = $derived(isAtMin(numberValue, min));
|
|
74
|
+
const isMax = $derived(isAtMax(numberValue, max));
|
|
72
75
|
|
|
73
76
|
$effect(() => {
|
|
74
77
|
const newValue = value;
|
|
@@ -76,19 +79,19 @@
|
|
|
76
79
|
// Only overwrite the field when it doesn’t already hold the value, so text that is still being
|
|
77
80
|
// typed, like `1.` or `-`, isn’t wiped out when it parses to the same number or no number
|
|
78
81
|
untrack(() => {
|
|
79
|
-
if (parseNumber(
|
|
82
|
+
if (parseNumber(numberValue) !== newValue) {
|
|
80
83
|
inputValue = String(newValue ?? '');
|
|
81
84
|
}
|
|
82
85
|
});
|
|
83
86
|
});
|
|
84
87
|
|
|
85
88
|
$effect(() => {
|
|
86
|
-
value = parseNumber(
|
|
89
|
+
value = parseNumber(numberValue);
|
|
87
90
|
});
|
|
88
91
|
|
|
89
92
|
$effect(() => {
|
|
90
93
|
if (edited) {
|
|
91
|
-
invalid = isInvalidNumber(
|
|
94
|
+
invalid = isInvalidNumber(numberValue, { required, min, max });
|
|
92
95
|
}
|
|
93
96
|
});
|
|
94
97
|
|
|
@@ -97,7 +100,7 @@
|
|
|
97
100
|
* @param {1 | -1} direction `1` to increase, `-1` to decrease.
|
|
98
101
|
*/
|
|
99
102
|
const stepValue = (direction) => {
|
|
100
|
-
const newInputValue = stepNumber(
|
|
103
|
+
const newInputValue = stepNumber(numberValue, { step, direction, min, max });
|
|
101
104
|
|
|
102
105
|
if (newInputValue !== undefined) {
|
|
103
106
|
inputValue = newInputValue;
|
|
@@ -130,7 +133,7 @@
|
|
|
130
133
|
<div role="none" class="buttons">
|
|
131
134
|
<Button
|
|
132
135
|
iconic
|
|
133
|
-
disabled={disabled || readonly || Number.isNaN(Number(
|
|
136
|
+
disabled={disabled || readonly || Number.isNaN(Number(numberValue)) || isMax}
|
|
134
137
|
aria-label={_('_sui.number_input.increase')}
|
|
135
138
|
aria-controls={id}
|
|
136
139
|
onclick={() => {
|
|
@@ -147,7 +150,7 @@
|
|
|
147
150
|
</Button>
|
|
148
151
|
<Button
|
|
149
152
|
iconic
|
|
150
|
-
disabled={disabled || readonly || Number.isNaN(Number(
|
|
153
|
+
disabled={disabled || readonly || Number.isNaN(Number(numberValue)) || isMin}
|
|
151
154
|
aria-label={_('_sui.number_input.decrease')}
|
|
152
155
|
aria-controls={id}
|
|
153
156
|
onclick={() => {
|