@sveltia/ui 0.87.3 → 0.88.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/text-editor/code-editor.svelte +0 -5
- package/dist/components/text-editor/core.d.ts +8 -1
- package/dist/components/text-editor/core.js +358 -26
- package/dist/components/text-editor/lexical-root.svelte +7 -3
- package/dist/components/text-editor/markdown.js +149 -8
- package/dist/components/text-editor/raw-markdown.d.ts +8 -0
- package/dist/components/text-editor/raw-markdown.js +85 -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/store.svelte.js +3 -3
- package/dist/components/text-editor/text-editor.svelte +44 -6
- package/dist/components/text-editor/text-editor.svelte.d.ts +28 -0
- package/dist/components/text-editor/toolbar/insert-component.d.ts +1 -2
- package/dist/components/text-editor/toolbar/insert-component.js +0 -9
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
- package/dist/components/text-editor/transformers/block-escape.js +19 -24
- package/dist/components/text-editor/transformers/raw-syntax.d.ts +33 -0
- package/dist/components/text-editor/transformers/raw-syntax.js +85 -1
- package/dist/components/text-editor/transformers/table.js +8 -2
- package/dist/components/text-field/text-area.svelte +4 -4
- package/dist/shiki-engine.js +5 -5
- package/dist/typedefs.d.ts +5 -0
- package/dist/typedefs.js +2 -0
- package/package.json +8 -8
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { INFO_BACKTICK } from './transformers/code.js';
|
|
2
|
+
import {
|
|
3
|
+
HTML_BLOCK_PLACEHOLDER,
|
|
4
|
+
HTML_TAG_SOURCE,
|
|
5
|
+
INLINE_HTML_REGEX,
|
|
6
|
+
} from './transformers/raw-syntax.js';
|
|
2
7
|
|
|
3
8
|
/**
|
|
4
9
|
* @typedef {object} CodeBlock
|
|
@@ -187,14 +192,52 @@ const HTML_BLOCK_TAG_NAMES = [
|
|
|
187
192
|
];
|
|
188
193
|
|
|
189
194
|
/**
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
195
|
+
* Start and end conditions of the HTML blocks that can interrupt a paragraph: a raw text element, a
|
|
196
|
+
* comment, a processing instruction, a declaration, a CDATA section or a block element. The start
|
|
197
|
+
* is what follows the `<` at the start of the line. The end of the block is the line that matches
|
|
198
|
+
* the end condition, or the line before a blank line if there’s none.
|
|
199
|
+
* @type {[string, RegExp | null][]}
|
|
193
200
|
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
194
201
|
*/
|
|
202
|
+
const HTML_BLOCK_CONDITION_SOURCES = [
|
|
203
|
+
['(?:script|pre|style|textarea)(?:[ \\t>]|$)', /<\/(?:pre|script|style|textarea)>/i],
|
|
204
|
+
['!--', /-->/],
|
|
205
|
+
['\\?', /\?>/],
|
|
206
|
+
['![A-Za-z]', />/],
|
|
207
|
+
['!\\[CDATA\\[', /\]\]>/],
|
|
208
|
+
[`/?(?:${HTML_BLOCK_TAG_NAMES.join('|')})(?:[ \\t>]|/>|$)`, null],
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Regular expression to match the start of an HTML block that can interrupt a paragraph; see
|
|
213
|
+
* {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
214
|
+
*/
|
|
195
215
|
const HTML_BLOCK_START_REGEX = new RegExp(
|
|
196
|
-
`<(
|
|
197
|
-
|
|
216
|
+
`<(?:${HTML_BLOCK_CONDITION_SOURCES.map(([start]) => start).join('|')})`,
|
|
217
|
+
'i',
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Start and end conditions of the HTML blocks that can interrupt a paragraph, with the start as a
|
|
222
|
+
* regular expression to match a line; see {@link HTML_BLOCK_CONDITION_SOURCES}.
|
|
223
|
+
* @type {[RegExp, RegExp | null][]}
|
|
224
|
+
*/
|
|
225
|
+
const HTML_BLOCK_CONDITIONS = HTML_BLOCK_CONDITION_SOURCES.map(([start, end]) => [
|
|
226
|
+
new RegExp(`^ {0,3}<(?:${start})`, 'i'),
|
|
227
|
+
end,
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Global version of `INLINE_HTML_REGEX`.
|
|
232
|
+
*/
|
|
233
|
+
const GLOBAL_INLINE_HTML_REGEX = new RegExp(INLINE_HTML_REGEX, 'g');
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Regular expression to match a line that starts an HTML block that can’t interrupt a paragraph.
|
|
237
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
238
|
+
*/
|
|
239
|
+
const STANDALONE_HTML_TAG_REGEX = new RegExp(
|
|
240
|
+
`^ {0,3}(?!</?(?:pre|script|style|textarea)\\b)(?:${HTML_TAG_SOURCE})[ \\t]*$`,
|
|
198
241
|
'i',
|
|
199
242
|
);
|
|
200
243
|
|
|
@@ -360,7 +403,11 @@ export const splitMultilineFormatting = (value) =>
|
|
|
360
403
|
|
|
361
404
|
const lines = splitFormattedLines(text);
|
|
362
405
|
|
|
406
|
+
// A line with only markers, like `**` in `_**\n**_` of `**_(a)_**\n**_(b)_**`, means
|
|
407
|
+
// the match runs from the closing marker of one line to the opening marker of the next,
|
|
408
|
+
// which isn’t formatting. Splitting it would add markers that match again, without end
|
|
363
409
|
if (
|
|
410
|
+
lines.some(({ text: lineText }) => /^[*_~]+$/.test(lineText)) ||
|
|
364
411
|
lines
|
|
365
412
|
.slice(1)
|
|
366
413
|
.some(({ indent, text: lineText }) =>
|
|
@@ -536,6 +583,35 @@ const markMarkersNextToBackslashes = (line) =>
|
|
|
536
583
|
)
|
|
537
584
|
: line;
|
|
538
585
|
|
|
586
|
+
/**
|
|
587
|
+
* Regular expression to match the place between a formatting marker and a punctuation character or
|
|
588
|
+
* a symbol other than ASCII, like an en dash or a curly quote.
|
|
589
|
+
*/
|
|
590
|
+
const MARKER_UNICODE_PUNCTUATION_REGEX =
|
|
591
|
+
/(?<=[*_~])(?=(?![\0-\x7F])[\p{P}\p{S}])|(?<=(?![\0-\x7F])[\p{P}\p{S}])(?=[*_~])/gu;
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Put a period, which Lexical takes as punctuation, between a formatting marker and a punctuation
|
|
595
|
+
* character or a symbol other than ASCII. Unlike CommonMark, Lexical only takes ASCII characters as
|
|
596
|
+
* punctuation, so it reads the marker as being next to a letter: it doesn’t read `**[a](u)**` in
|
|
597
|
+
* `–**[a](u)**` or `“**[a](u)**”` as bold, as the marker can’t open or close the formatting there.
|
|
598
|
+
* The period gives the marker the same neighbor as in CommonMark. A placeholder on the other side
|
|
599
|
+
* lets it be removed.
|
|
600
|
+
* @param {string} line Line.
|
|
601
|
+
* @returns {string} Processed line.
|
|
602
|
+
* @see https://spec.commonmark.org/0.31.2/#unicode-punctuation-character
|
|
603
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1107
|
|
604
|
+
*/
|
|
605
|
+
const markMarkersNextToUnicodePunctuation = (line) =>
|
|
606
|
+
/[^\0-\x7F]/.test(line)
|
|
607
|
+
? line.replace(MARKER_UNICODE_PUNCTUATION_REGEX, (_match, index) =>
|
|
608
|
+
// The period goes next to the marker
|
|
609
|
+
'*_~'.includes(line[index - 1])
|
|
610
|
+
? `.${PUNCTUATION_PLACEHOLDER}`
|
|
611
|
+
: `${PUNCTUATION_PLACEHOLDER}.`,
|
|
612
|
+
)
|
|
613
|
+
: line;
|
|
614
|
+
|
|
539
615
|
/**
|
|
540
616
|
* Replace the tabs in the code spans of a line, and the spaces in a code span made only of spaces,
|
|
541
617
|
* with placeholders. Lexical turns a tab into a tab node, which has no format, so a tab in a code
|
|
@@ -569,17 +645,81 @@ const markCodeSpanWhitespace = (line) => {
|
|
|
569
645
|
return result + line.slice(index);
|
|
570
646
|
};
|
|
571
647
|
|
|
648
|
+
/**
|
|
649
|
+
* Mark the lines of the HTML blocks in a part of a Markdown string with a placeholder at the start,
|
|
650
|
+
* so they’re imported as raw HTML, without the Markdown syntax being read; see `RAW_HTML`. Their
|
|
651
|
+
* tabs, and those in the inline HTML of the other lines, are replaced with a placeholder, as
|
|
652
|
+
* Lexical would otherwise split the raw HTML into separate nodes around a tab node.
|
|
653
|
+
* @param {string} part Part of a Markdown string outside fenced code blocks.
|
|
654
|
+
* @returns {string} Processed part.
|
|
655
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
656
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
657
|
+
*/
|
|
658
|
+
const markHtml = (part) => {
|
|
659
|
+
/**
|
|
660
|
+
* End condition of the current HTML block: a regular expression, `null` for a blank line, or
|
|
661
|
+
* `undefined` if the line is not in an HTML block.
|
|
662
|
+
* @type {RegExp | null | undefined}
|
|
663
|
+
*/
|
|
664
|
+
let end;
|
|
665
|
+
let isInParagraph = false;
|
|
666
|
+
|
|
667
|
+
return part
|
|
668
|
+
.split('\n')
|
|
669
|
+
.map((line) => {
|
|
670
|
+
const isBlankLine = !line.trim();
|
|
671
|
+
|
|
672
|
+
if (end === null && isBlankLine) {
|
|
673
|
+
end = undefined;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (end === undefined) {
|
|
677
|
+
const condition = HTML_BLOCK_CONDITIONS.find(([start]) => start.test(line));
|
|
678
|
+
|
|
679
|
+
if (condition) {
|
|
680
|
+
[, end] = condition;
|
|
681
|
+
} else if (!isInParagraph && STANDALONE_HTML_TAG_REGEX.test(line)) {
|
|
682
|
+
end = null;
|
|
683
|
+
} else {
|
|
684
|
+
isInParagraph = !isBlankLine;
|
|
685
|
+
|
|
686
|
+
return line.includes('\t')
|
|
687
|
+
? line.replace(GLOBAL_INLINE_HTML_REGEX, (tag) =>
|
|
688
|
+
tag.replaceAll('\t', CODE_TAB_PLACEHOLDER),
|
|
689
|
+
)
|
|
690
|
+
: line;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (end?.test(line)) {
|
|
695
|
+
end = undefined;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
isInParagraph = false;
|
|
699
|
+
|
|
700
|
+
return `${HTML_BLOCK_PLACEHOLDER}${line.replaceAll('\t', CODE_TAB_PLACEHOLDER)}`;
|
|
701
|
+
})
|
|
702
|
+
.join('\n');
|
|
703
|
+
};
|
|
704
|
+
|
|
572
705
|
/**
|
|
573
706
|
* Add placeholders to a Markdown string, so Lexical reads it as CommonMark does; see
|
|
574
|
-
* `markLineEndBackslashes()`, `markMarkersNextToBackslashes()
|
|
707
|
+
* `markLineEndBackslashes()`, `markMarkersNextToBackslashes()`,
|
|
708
|
+
* `markMarkersNextToUnicodePunctuation()` and `markCodeSpanWhitespace()`.
|
|
575
709
|
* Lines in fenced code blocks are left untouched. The placeholders are removed from the imported
|
|
576
710
|
* text with `removeImportPlaceholders()`.
|
|
577
711
|
* @param {string} value Markdown string to process.
|
|
578
712
|
* @returns {string} Processed Markdown string.
|
|
579
713
|
*/
|
|
580
714
|
export const addImportPlaceholders = (value) =>
|
|
581
|
-
mapLinesOutsideCodeBlocks(
|
|
582
|
-
|
|
715
|
+
mapLinesOutsideCodeBlocks(
|
|
716
|
+
value.includes('<') ? mapOutsideCodeBlocks(value, markHtml) : value,
|
|
717
|
+
(line) =>
|
|
718
|
+
markCodeSpanWhitespace(
|
|
719
|
+
markMarkersNextToUnicodePunctuation(
|
|
720
|
+
markMarkersNextToBackslashes(markLineEndBackslashes(line)),
|
|
721
|
+
),
|
|
722
|
+
),
|
|
583
723
|
);
|
|
584
724
|
|
|
585
725
|
/**
|
|
@@ -591,6 +731,7 @@ export const removeImportPlaceholders = (text) =>
|
|
|
591
731
|
text
|
|
592
732
|
.replace(new RegExp(`\\.${PUNCTUATION_PLACEHOLDER}|${PUNCTUATION_PLACEHOLDER}\\.`, 'g'), '')
|
|
593
733
|
.replaceAll(ESCAPED_BACKSLASH_PLACEHOLDER, '')
|
|
734
|
+
.replaceAll(HTML_BLOCK_PLACEHOLDER, '')
|
|
594
735
|
.replaceAll(CODE_TAB_PLACEHOLDER, '\t')
|
|
595
736
|
.replaceAll(CODE_SPACE_PLACEHOLDER, ' ');
|
|
596
737
|
|
|
@@ -15,6 +15,14 @@ export function applyRawTextEdit(textArea: HTMLTextAreaElement, { start, end, te
|
|
|
15
15
|
export function editRawText(textArea: HTMLTextAreaElement | undefined, getEdit: (state: RawTextState) => RawTextEdit): Promise<void>;
|
|
16
16
|
export function registerRawTextShortcut(textArea: HTMLTextAreaElement, key: string, handler: () => void): () => void;
|
|
17
17
|
export function insertComponent(textArea: HTMLTextAreaElement, component: TextEditorComponent): Promise<void>;
|
|
18
|
+
export function joinLines({ value, caret }: {
|
|
19
|
+
value: string;
|
|
20
|
+
caret: number;
|
|
21
|
+
}): {
|
|
22
|
+
value: string;
|
|
23
|
+
caret: number;
|
|
24
|
+
};
|
|
25
|
+
export function registerRawTextSingleLine(textArea: HTMLTextAreaElement): () => void;
|
|
18
26
|
/**
|
|
19
27
|
* Current value and selection of the `<textarea>`.
|
|
20
28
|
*/
|
|
@@ -553,3 +553,88 @@ export const insertComponent = async (textArea, component) => {
|
|
|
553
553
|
|
|
554
554
|
await applyRawTextEdit(textArea, insertMarkdown(getRawTextState(textArea), markdown, { block }));
|
|
555
555
|
};
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Line breaks, along with the spaces and tabs around them, to replace with a single space.
|
|
559
|
+
*/
|
|
560
|
+
const LINE_BREAK_REGEX = /[ \t]*(?:\r?\n[ \t]*)+/g;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Join the lines of the given value with spaces, for a single-line editor, keeping the caret where
|
|
564
|
+
* it was in the text.
|
|
565
|
+
* @param {object} state Value and caret position.
|
|
566
|
+
* @param {string} state.value Value.
|
|
567
|
+
* @param {number} state.caret Caret position.
|
|
568
|
+
* @returns {{ value: string, caret: number }} Joined value and the new caret position.
|
|
569
|
+
*/
|
|
570
|
+
export const joinLines = ({ value, caret }) => {
|
|
571
|
+
const before = value.slice(0, caret).replace(LINE_BREAK_REGEX, ' ');
|
|
572
|
+
|
|
573
|
+
return {
|
|
574
|
+
value: before + value.slice(caret).replace(LINE_BREAK_REGEX, ' '),
|
|
575
|
+
caret: before.length,
|
|
576
|
+
};
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Keep the `<textarea>` on a single line: the Enter key doesn’t insert a line break, and the lines
|
|
581
|
+
* of pasted or dropped text are joined with spaces before the value is read.
|
|
582
|
+
* @param {HTMLTextAreaElement} textArea `<textarea>` element.
|
|
583
|
+
* @returns {() => void} Function to stop handling the events.
|
|
584
|
+
*/
|
|
585
|
+
export const registerRawTextSingleLine = (textArea) => {
|
|
586
|
+
/**
|
|
587
|
+
* Handle the `beforeinput` event. The emoji autocomplete, which selects an emoji with the Enter
|
|
588
|
+
* key, cancels the `keydown` event first, so there’s no `beforeinput` event then. Pasted text is
|
|
589
|
+
* inserted with its lines joined instead, which keeps the edit in the native undo history.
|
|
590
|
+
* @param {InputEvent} event `beforeinput` event.
|
|
591
|
+
*/
|
|
592
|
+
const onBeforeInput = (event) => {
|
|
593
|
+
const { inputType } = event;
|
|
594
|
+
|
|
595
|
+
if (inputType === 'insertLineBreak' || inputType === 'insertParagraph') {
|
|
596
|
+
event.preventDefault();
|
|
597
|
+
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const text = event.data || event.dataTransfer?.getData('text/plain') || '';
|
|
602
|
+
|
|
603
|
+
if (
|
|
604
|
+
(inputType === 'insertFromPaste' || inputType === 'insertReplacementText') &&
|
|
605
|
+
text.includes('\n') &&
|
|
606
|
+
// `execCommand()` is deprecated, but it’s still the only way to keep the edit in the native
|
|
607
|
+
// undo history. If it’s unavailable, the `input` listener below joins the lines instead
|
|
608
|
+
typeof document.execCommand === 'function'
|
|
609
|
+
) {
|
|
610
|
+
event.preventDefault();
|
|
611
|
+
document.execCommand('insertText', false, text.replace(LINE_BREAK_REGEX, ' '));
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Handle the `input` event, in the capture phase, so the value is joined before the bound value
|
|
617
|
+
* is updated. It catches the line breaks the `beforeinput` listener can’t, like those of dropped
|
|
618
|
+
* text, but the edit then leaves the native undo history.
|
|
619
|
+
*/
|
|
620
|
+
const onInput = () => {
|
|
621
|
+
const { value, selectionStart } = textArea;
|
|
622
|
+
|
|
623
|
+
if (!value.includes('\n')) {
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const joined = joinLines({ value, caret: selectionStart });
|
|
628
|
+
|
|
629
|
+
textArea.value = joined.value;
|
|
630
|
+
textArea.setSelectionRange(joined.caret, joined.caret);
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
textArea.addEventListener('beforeinput', onBeforeInput);
|
|
634
|
+
textArea.addEventListener('input', onInput, { capture: true });
|
|
635
|
+
|
|
636
|
+
return () => {
|
|
637
|
+
textArea.removeEventListener('beforeinput', onBeforeInput);
|
|
638
|
+
textArea.removeEventListener('input', onInput, { capture: true });
|
|
639
|
+
};
|
|
640
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { generateElementId } from '@sveltia/utils/element';
|
|
2
|
-
import { convertHtmlToLexical, convertMarkdownToLexical } from './core.js';
|
|
2
|
+
import { convertHtmlToLexical, convertMarkdownToLexical, limitToSingleLine } from './core.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @import { LexicalEditor } from 'lexical';
|
|
@@ -152,8 +152,8 @@ export const createEditorStore = () => {
|
|
|
152
152
|
return config;
|
|
153
153
|
},
|
|
154
154
|
set config(newValue) {
|
|
155
|
-
config = newValue;
|
|
156
|
-
useRichText =
|
|
155
|
+
config = limitToSingleLine(newValue);
|
|
156
|
+
useRichText = config.modes[0] === 'rich-text' || config.isCodeEditor;
|
|
157
157
|
},
|
|
158
158
|
get inputValue() {
|
|
159
159
|
return inputValue;
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
import FloatingLinkEditor from './floating-link-editor.svelte';
|
|
15
15
|
import LexicalRoot from './lexical-root.svelte';
|
|
16
16
|
import TableCellMenuButton from './table-cell-menu-button.svelte';
|
|
17
|
-
import { getBlockType, getRawTextState } from './raw-markdown.js';
|
|
17
|
+
import { getBlockType, getRawTextState, registerRawTextSingleLine } from './raw-markdown.js';
|
|
18
18
|
import { highlightCodeToTokens } from './shiki/facade.js';
|
|
19
19
|
import { getCodeTheme, onCodeThemeChange } from './shiki/theme.js';
|
|
20
20
|
import { createEditorStore } from './store.svelte.js';
|
|
@@ -50,6 +50,14 @@
|
|
|
50
50
|
* @property {TextEditorMode[]} [modes] Enabled modes.
|
|
51
51
|
* @property {TextEditorNodeType[]} [buttons] Enabled buttons.
|
|
52
52
|
* @property {TextEditorComponent[]} [components] Editor components.
|
|
53
|
+
* @property {boolean} [multiline] Whether the content can span multiple paragraphs and lines.
|
|
54
|
+
* With `false`, the content is kept in a single paragraph: the Enter key does nothing, and the
|
|
55
|
+
* paragraphs and lines of pasted or imported content are joined with spaces. Only the inline
|
|
56
|
+
* buttons and the editor components with a text match transformer are enabled then, horizontal
|
|
57
|
+
* rules and tables in Markdown are kept as text, and the HTML format has no plain text mode.
|
|
58
|
+
* @property {boolean} [minimal] Whether to make the editor compact: it starts with the height of
|
|
59
|
+
* a single line, like a single-line editor, and stops growing at 240 pixels, scrolling the
|
|
60
|
+
* content beyond that.
|
|
53
61
|
* @property {boolean} [useMarkdownShortcuts] Whether to enable Markdown keyboard shortcuts in the
|
|
54
62
|
* rich text editor.
|
|
55
63
|
* @property {boolean} [useEmojiAutocomplete] Whether to autocomplete emojis when the user
|
|
@@ -85,6 +93,8 @@
|
|
|
85
93
|
modes = ['rich-text', 'plain-text'],
|
|
86
94
|
buttons = [...INLINE_BUTTON_TYPES, ...BLOCK_BUTTON_TYPES],
|
|
87
95
|
components = [],
|
|
96
|
+
multiline = true,
|
|
97
|
+
minimal = false,
|
|
88
98
|
useMarkdownShortcuts = true,
|
|
89
99
|
useEmojiAutocomplete = true,
|
|
90
100
|
hidden = false,
|
|
@@ -121,6 +131,7 @@
|
|
|
121
131
|
modes,
|
|
122
132
|
enabledButtons: buttons,
|
|
123
133
|
components,
|
|
134
|
+
multiline,
|
|
124
135
|
useMarkdownShortcuts,
|
|
125
136
|
useEmojiAutocomplete,
|
|
126
137
|
format,
|
|
@@ -263,6 +274,16 @@
|
|
|
263
274
|
};
|
|
264
275
|
});
|
|
265
276
|
|
|
277
|
+
$effect(() => {
|
|
278
|
+
const { textArea } = editorStore;
|
|
279
|
+
|
|
280
|
+
if (!textArea || multiline) {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return registerRawTextSingleLine(textArea);
|
|
285
|
+
});
|
|
286
|
+
|
|
266
287
|
$effect(() => {
|
|
267
288
|
pending = editorStore.pending;
|
|
268
289
|
});
|
|
@@ -309,6 +330,8 @@
|
|
|
309
330
|
role="none"
|
|
310
331
|
class="sui text-editor"
|
|
311
332
|
class:flex
|
|
333
|
+
class:single-line={!multiline}
|
|
334
|
+
class:minimal
|
|
312
335
|
{hidden}
|
|
313
336
|
style:--sui-text-editor-toolbar-height="{editorStore.toolbarHeight}px"
|
|
314
337
|
>
|
|
@@ -327,6 +350,8 @@
|
|
|
327
350
|
bind:element={editorStore.textArea}
|
|
328
351
|
id="{editorStore.editorId}-plain-text"
|
|
329
352
|
autoResize={true}
|
|
353
|
+
rows={multiline ? undefined : 1}
|
|
354
|
+
aria-multiline={multiline ? undefined : false}
|
|
330
355
|
bind:value={editorStore.inputValue}
|
|
331
356
|
{useEmojiAutocomplete}
|
|
332
357
|
highlight={highlightSource}
|
|
@@ -358,16 +383,28 @@
|
|
|
358
383
|
<style>.text-editor {
|
|
359
384
|
position: relative;
|
|
360
385
|
margin: var(--sui-focus-ring-width);
|
|
361
|
-
border-radius: var(--sui-textbox-border-radius);
|
|
362
386
|
width: calc(100% - var(--sui-focus-ring-width) * 2);
|
|
363
|
-
transition: all 200ms;
|
|
364
|
-
}
|
|
365
|
-
.text-editor:focus-within {
|
|
366
|
-
outline: var(--sui-focus-ring-width) solid var(--sui-focus-ring-color);
|
|
367
387
|
}
|
|
368
388
|
.text-editor.flex:not([hidden]) {
|
|
369
389
|
display: block;
|
|
370
390
|
}
|
|
391
|
+
.text-editor:is(.single-line, .minimal) > :global(.lexical-root), .text-editor:is(.single-line, .minimal) > :global(.text-area) > :global(:is(textarea, .clone)) {
|
|
392
|
+
min-height: 0;
|
|
393
|
+
}
|
|
394
|
+
.text-editor.minimal > :global(.lexical-root),
|
|
395
|
+
.text-editor.minimal > :global(.text-area) > :global(:is(textarea, .clone)) {
|
|
396
|
+
max-height: 240px;
|
|
397
|
+
}
|
|
398
|
+
.text-editor.minimal > :global(.lexical-root),
|
|
399
|
+
.text-editor.minimal > :global(.text-area) > :global(textarea) {
|
|
400
|
+
overflow: auto;
|
|
401
|
+
}
|
|
402
|
+
.text-editor.minimal > :global(.lexical-root) {
|
|
403
|
+
--sui-text-editor-inner-toolbar-top: 0px;
|
|
404
|
+
}
|
|
405
|
+
.text-editor.minimal > :global(.text-area) > :global(:is(textarea, .clone)) {
|
|
406
|
+
scrollbar-gutter: stable;
|
|
407
|
+
}
|
|
371
408
|
.text-editor :global(.sui.text-area) {
|
|
372
409
|
margin: 0 !important;
|
|
373
410
|
width: 100% !important;
|
|
@@ -378,6 +415,7 @@
|
|
|
378
415
|
border-start-end-radius: 0 !important;
|
|
379
416
|
border-end-start-radius: var(--sui-textbox-border-radius) !important;
|
|
380
417
|
border-end-end-radius: var(--sui-textbox-border-radius) !important;
|
|
418
|
+
outline-offset: calc(var(--sui-focus-ring-width) * -1);
|
|
381
419
|
}
|
|
382
420
|
.text-editor {
|
|
383
421
|
--sui-text-editor-toolbar-top: var(--sui-text-editor-inner-toolbar-top, 0px);
|
|
@@ -46,6 +46,20 @@ declare const TextEditor: import("svelte").Component<{
|
|
|
46
46
|
* Editor components.
|
|
47
47
|
*/
|
|
48
48
|
components?: TextEditorComponent[] | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Whether the content can span multiple paragraphs and lines.
|
|
51
|
+
* With `false`, the content is kept in a single paragraph: the Enter key does nothing, and the
|
|
52
|
+
* paragraphs and lines of pasted or imported content are joined with spaces. Only the inline
|
|
53
|
+
* buttons and the editor components with a text match transformer are enabled then, horizontal
|
|
54
|
+
* rules and tables in Markdown are kept as text, and the HTML format has no plain text mode.
|
|
55
|
+
*/
|
|
56
|
+
multiline?: boolean | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Whether to make the editor compact: it starts with the height of
|
|
59
|
+
* a single line, like a single-line editor, and stops growing at 240 pixels, scrolling the
|
|
60
|
+
* content beyond that.
|
|
61
|
+
*/
|
|
62
|
+
minimal?: boolean | undefined;
|
|
49
63
|
/**
|
|
50
64
|
* Whether to enable Markdown keyboard shortcuts in the
|
|
51
65
|
* rich text editor.
|
|
@@ -145,6 +159,20 @@ type Props = {
|
|
|
145
159
|
* Editor components.
|
|
146
160
|
*/
|
|
147
161
|
components?: TextEditorComponent[] | undefined;
|
|
162
|
+
/**
|
|
163
|
+
* Whether the content can span multiple paragraphs and lines.
|
|
164
|
+
* With `false`, the content is kept in a single paragraph: the Enter key does nothing, and the
|
|
165
|
+
* paragraphs and lines of pasted or imported content are joined with spaces. Only the inline
|
|
166
|
+
* buttons and the editor components with a text match transformer are enabled then, horizontal
|
|
167
|
+
* rules and tables in Markdown are kept as text, and the HTML format has no plain text mode.
|
|
168
|
+
*/
|
|
169
|
+
multiline?: boolean | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Whether to make the editor compact: it starts with the height of
|
|
172
|
+
* a single line, like a single-line editor, and stops growing at 240 pixels, scrolling the
|
|
173
|
+
* content beyond that.
|
|
174
|
+
*/
|
|
175
|
+
minimal?: boolean | undefined;
|
|
148
176
|
/**
|
|
149
177
|
* Whether to enable Markdown keyboard shortcuts in the
|
|
150
178
|
* rich text editor.
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export function isInlineComponent({ transformer }: TextEditorComponent): boolean;
|
|
2
1
|
export function insertEditorComponent(editorStore: TextEditorStore, component: TextEditorComponent): void;
|
|
3
|
-
import type { TextEditorComponent } from '../../../typedefs.js';
|
|
4
2
|
import type { TextEditorStore } from '../../../typedefs.js';
|
|
3
|
+
import type { TextEditorComponent } from '../../../typedefs.js';
|
|
@@ -5,15 +5,6 @@ import { insertComponent } from '../raw-markdown.js';
|
|
|
5
5
|
* @import { TextEditorComponent, TextEditorStore } from '../../../typedefs.js';
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* Check if the given editor component is inline, which means its Markdown is matched within a line
|
|
10
|
-
* of text by a text match transformer. Only an inline component can be inserted into a Markdown
|
|
11
|
-
* table cell.
|
|
12
|
-
* @param {TextEditorComponent} component Editor component.
|
|
13
|
-
* @returns {boolean} Result.
|
|
14
|
-
*/
|
|
15
|
-
export const isInlineComponent = ({ transformer }) => transformer.type === 'text-match';
|
|
16
|
-
|
|
17
8
|
/**
|
|
18
9
|
* Insert a new instance of the given editor component into the editor: its Markdown in the plain
|
|
19
10
|
* text mode, or its node in the rich text mode.
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import MenuButton from '../../menu/menu-button.svelte';
|
|
11
11
|
import Menu from '../../menu/menu.svelte';
|
|
12
12
|
import { AVAILABLE_BUTTONS, BLOCK_BUTTON_TYPES, INLINE_BUTTON_TYPES } from '../constants.js';
|
|
13
|
+
import { isInlineComponent } from '../core.js';
|
|
13
14
|
import CodeLanguageSwitcher from './code-language-switcher.svelte';
|
|
14
15
|
import AlignMenuButton from './align-menu-button.svelte';
|
|
15
16
|
import FormatTextButton from './format-text-button.svelte';
|
|
@@ -17,7 +18,6 @@
|
|
|
17
18
|
import InsertItemButton from './insert-item-button.svelte';
|
|
18
19
|
import InsertLinkButton from './insert-link-button.svelte';
|
|
19
20
|
import InsertMenuButton from './insert-menu-button.svelte';
|
|
20
|
-
import { isInlineComponent } from './insert-component.js';
|
|
21
21
|
import InsertTableButton from './insert-table-button.svelte';
|
|
22
22
|
import TableActionMenu from './table-action-menu.svelte';
|
|
23
23
|
import ToggleBlockMenuItem from './toggle-block-menu-item.svelte';
|
|
@@ -37,37 +37,32 @@ const isWordChar = (char) => !!char && /[^\s\p{P}\p{S}]/u.test(char);
|
|
|
37
37
|
* needed, so `snake_case` and `C:\Users` aren’t exported as `snake\_case` and `C:\\Users`. Unlike
|
|
38
38
|
* a global replacement, the escapes that are needed are kept: an underscore that could open or
|
|
39
39
|
* close emphasis, like that in `\_not italic\_`, and a backslash that would otherwise escape the
|
|
40
|
-
* next character or make a hard line break.
|
|
41
|
-
* formatted text, which can appear with a combination of bold and italic text.
|
|
40
|
+
* next character or make a hard line break.
|
|
42
41
|
* @param {string} markdown Markdown exported from a text node with its format, in which every
|
|
43
42
|
* backslash starts an escape.
|
|
44
43
|
* @returns {string} Tidied Markdown.
|
|
45
44
|
* @see https://github.com/sveltia/sveltia-cms/issues/430
|
|
46
45
|
* @see https://github.com/sveltia/sveltia-cms/issues/512
|
|
47
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/511
|
|
48
|
-
* @see https://github.com/sveltia/sveltia-cms/issues/534
|
|
49
46
|
*/
|
|
50
47
|
export const unescapeText = (markdown) =>
|
|
51
|
-
markdown
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
70
|
-
.replace(/ /g, ' ');
|
|
48
|
+
markdown.replace(/((?:\\_)+)|\\(.)/gs, (match, underscores, char, offset, string) => {
|
|
49
|
+
if (underscores) {
|
|
50
|
+
return isWordChar(string[offset - 1]) && isWordChar(string[offset + match.length])
|
|
51
|
+
? underscores.replaceAll('\\', '')
|
|
52
|
+
: match;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (char === '\\') {
|
|
56
|
+
const next = string[offset + 2];
|
|
57
|
+
|
|
58
|
+
// The backslash is literal unless it’s followed by ASCII punctuation, which it would
|
|
59
|
+
// escape, or a line break, which it would turn into a hard line break. The end of the node
|
|
60
|
+
// is kept escaped, as the next node may start with either
|
|
61
|
+
return next && !/[\n\r!-/:-@[-`{-~]/.test(next) ? char : match;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return match;
|
|
65
|
+
});
|
|
71
66
|
|
|
72
67
|
/**
|
|
73
68
|
* Check if the given text node is italic and the italic text it’s part of is next to a word
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { TextMatchTransformer } from '@lexical/markdown';
|
|
3
|
+
* @import { TextNode } from 'lexical';
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Placeholder put at the start of a line of an HTML block; see `addImportPlaceholders()`.
|
|
7
|
+
*/
|
|
8
|
+
export const HTML_BLOCK_PLACEHOLDER: "\uE007";
|
|
9
|
+
/**
|
|
10
|
+
* Regular expression source to match an HTML open tag or closing tag.
|
|
11
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
12
|
+
*/
|
|
13
|
+
export const HTML_TAG_SOURCE: string;
|
|
14
|
+
/**
|
|
15
|
+
* Regular expression to match inline raw HTML on a line: an open tag, a closing tag, a comment, a
|
|
16
|
+
* processing instruction, a declaration or a CDATA section, which isn’t escaped.
|
|
17
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
18
|
+
*/
|
|
19
|
+
export const INLINE_HTML_REGEX: RegExp;
|
|
20
|
+
export function getImportedRawSyntax(node: TextNode): string | undefined;
|
|
1
21
|
export function getRawSyntax(node: TextNode): string | undefined;
|
|
2
22
|
/**
|
|
3
23
|
* Import-only transformer that marks the text imported from the Markdown syntax the editor doesn’t
|
|
@@ -8,5 +28,18 @@ export function getRawSyntax(node: TextNode): string | undefined;
|
|
|
8
28
|
* @type {TextMatchTransformer}
|
|
9
29
|
*/
|
|
10
30
|
export const RAW_SYNTAX: TextMatchTransformer;
|
|
31
|
+
/**
|
|
32
|
+
* Import-only transformer that marks raw HTML, so it’s exported as is. Neither the Markdown syntax
|
|
33
|
+
* nor the escapes are read in raw HTML, so an escape added to it on export, like `\_` in an
|
|
34
|
+
* attribute value or `\*` in the text of an HTML block, would show up as a backslash. Lexical
|
|
35
|
+
* removes the escapes from the text it has imported, so the text is restored with
|
|
36
|
+
* `getImportedRawSyntax()` once the import is done.
|
|
37
|
+
* @type {TextMatchTransformer}
|
|
38
|
+
* @see https://spec.commonmark.org/0.31.2/#raw-html
|
|
39
|
+
* @see https://spec.commonmark.org/0.31.2/#html-blocks
|
|
40
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1105
|
|
41
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/1106
|
|
42
|
+
*/
|
|
43
|
+
export const RAW_HTML: TextMatchTransformer;
|
|
11
44
|
import type { TextNode } from 'lexical';
|
|
12
45
|
import type { TextMatchTransformer } from '@lexical/markdown';
|