@sveltia/ui 0.89.1 → 0.89.3
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/checkbox/checkbox.svelte +5 -8
- package/dist/components/dialog/dialog.svelte +2 -2
- package/dist/components/emoji/emoji-suggestions.svelte +7 -7
- package/dist/components/grid/grid-col-header.svelte +4 -0
- package/dist/components/grid/grid-head.svelte +1 -1
- package/dist/components/grid/grid-head.svelte.d.ts +1 -1
- package/dist/components/radio/radio.svelte +9 -9
- package/dist/components/resizable-pane/resizable-pane-group.svelte +5 -20
- package/dist/components/resizable-pane/sizing.d.ts +6 -0
- package/dist/components/resizable-pane/sizing.js +31 -0
- package/dist/components/text-editor/code-editor.svelte +22 -39
- package/dist/components/text-editor/constants.js +3 -3
- package/dist/components/text-editor/conversion/editor-components.d.ts +5 -0
- package/dist/components/text-editor/conversion/editor-components.js +228 -0
- package/dist/components/text-editor/{html.d.ts → conversion/html.d.ts} +1 -1
- package/dist/components/text-editor/{html.js → conversion/html.js} +2 -2
- package/dist/components/text-editor/conversion/import.d.ts +12 -0
- package/dist/components/text-editor/conversion/import.js +327 -0
- package/dist/components/text-editor/conversion/paste.d.ts +4 -0
- package/dist/components/text-editor/conversion/paste.js +166 -0
- package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.js +4 -4
- package/dist/components/text-editor/conversion/transformers/table.js +342 -0
- package/dist/components/text-editor/conversion/update.d.ts +6 -0
- package/dist/components/text-editor/conversion/update.js +125 -0
- package/dist/components/text-editor/core.d.ts +50 -26
- package/dist/components/text-editor/core.js +133 -1694
- package/dist/components/text-editor/editing/caret.d.ts +4 -0
- package/dist/components/text-editor/editing/caret.js +248 -0
- package/dist/components/text-editor/editing/commands.d.ts +5 -0
- package/dist/components/text-editor/editing/commands.js +205 -0
- package/dist/components/text-editor/editing/link.d.ts +19 -0
- package/dist/components/text-editor/editing/link.js +222 -0
- package/dist/components/text-editor/{raw-markdown.d.ts → editing/raw-markdown.d.ts} +3 -3
- package/dist/components/text-editor/{raw-markdown.js → editing/raw-markdown.js} +2 -2
- package/dist/components/text-editor/editing/selection.d.ts +5 -0
- package/dist/components/text-editor/editing/selection.js +122 -0
- package/dist/components/text-editor/editing/single-line.d.ts +8 -0
- package/dist/components/text-editor/editing/single-line.js +190 -0
- package/dist/components/text-editor/{table.js → editing/table.js} +1 -1
- package/dist/components/text-editor/lexical-root.svelte +61 -10
- package/dist/components/text-editor/shiki/preload.d.ts +1 -0
- package/dist/components/text-editor/shiki/preload.js +30 -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.d.ts +5 -0
- package/dist/components/text-editor/store.svelte.js +55 -1
- package/dist/components/text-editor/text-editor.svelte +29 -41
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/code-language-switcher.svelte +2 -1
- package/dist/components/text-editor/toolbar/format-text-button.svelte +6 -2
- package/dist/components/text-editor/toolbar/insert-component.js +1 -1
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +4 -4
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +3 -2
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +7 -3
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +3 -3
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +2 -2
- package/dist/components/text-editor/utils.d.ts +1 -0
- package/dist/components/text-editor/utils.js +17 -0
- package/dist/components/text-editor/{emoji-autocomplete.svelte → widgets/emoji-autocomplete.svelte} +3 -3
- package/dist/components/text-editor/{floating-link-editor.svelte → widgets/floating-link-editor.svelte} +36 -79
- package/dist/components/text-editor/{table-cell-menu-button.svelte → widgets/table-cell-menu-button.svelte} +15 -5
- package/dist/components/text-field/date-time-input.svelte +8 -8
- package/dist/components/text-field/text-area.svelte +10 -10
- package/dist/components/text-field/text-input.svelte +7 -7
- package/dist/components/util/popup.svelte +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/services/label.d.ts +8 -0
- package/dist/services/label.js +15 -0
- package/dist/services/popup-position.d.ts +9 -0
- package/dist/services/popup-position.js +52 -55
- package/dist/shiki-engine.js +1 -1
- package/dist/styles/mixins.scss +69 -0
- package/package.json +12 -10
- package/dist/components/text-editor/transformers/table.js +0 -296
- /package/dist/components/text-editor/{markdown.d.ts → conversion/markdown.d.ts} +0 -0
- /package/dist/components/text-editor/{markdown.js → conversion/markdown.js} +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/table.d.ts +0 -0
- /package/dist/components/text-editor/{alignment.d.ts → editing/alignment.d.ts} +0 -0
- /package/dist/components/text-editor/{alignment.js → editing/alignment.js} +0 -0
- /package/dist/components/text-editor/{block.d.ts → editing/block.d.ts} +0 -0
- /package/dist/components/text-editor/{block.js → editing/block.js} +0 -0
- /package/dist/components/text-editor/{table.d.ts → editing/table.d.ts} +0 -0
- /package/dist/components/text-editor/{emoji-autocomplete.svelte.d.ts → widgets/emoji-autocomplete.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{floating-link-editor.svelte.d.ts → widgets/floating-link-editor.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{table-cell-menu-button.svelte.d.ts → widgets/table-cell-menu-button.svelte.d.ts} +0 -0
|
@@ -66,6 +66,6 @@ export type RawTextEdit = {
|
|
|
66
66
|
*/
|
|
67
67
|
selectionEnd: number;
|
|
68
68
|
};
|
|
69
|
-
import type { TextEditorBlockType } from '
|
|
70
|
-
import type { TextEditorFormatType } from '
|
|
71
|
-
import type { TextEditorComponent } from '
|
|
69
|
+
import type { TextEditorBlockType } from '../../../typedefs.js';
|
|
70
|
+
import type { TextEditorFormatType } from '../../../typedefs.js';
|
|
71
|
+
import type { TextEditorComponent } from '../../../typedefs.js';
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { isMac, matchesShortcuts } from '@sveltia/utils/events';
|
|
10
|
-
import { getComponentMarkdown } from '
|
|
10
|
+
import { getComponentMarkdown } from '../conversion/editor-components.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @import {
|
|
14
14
|
* TextEditorBlockType,
|
|
15
15
|
* TextEditorComponent,
|
|
16
16
|
* TextEditorFormatType,
|
|
17
|
-
* } from '
|
|
17
|
+
* } from '../../../typedefs.js';
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function getNearestElementNode(node: LexicalNode): ElementNode | null;
|
|
2
|
+
export function getSelectionTypes(): TextEditorSelectionState;
|
|
3
|
+
import type { LexicalNode } from 'lexical';
|
|
4
|
+
import { ElementNode } from 'lexical';
|
|
5
|
+
import type { TextEditorSelectionState } from '../../../typedefs.js';
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of the editor selection, for the toolbar.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
$isCodeHighlightNode as isCodeHighlightNode,
|
|
7
|
+
$isCodeNode as isCodeNode,
|
|
8
|
+
} from '@lexical/code-core';
|
|
9
|
+
import { $isLinkNode as isLinkNode } from '@lexical/link';
|
|
10
|
+
import {
|
|
11
|
+
$isListItemNode as isListItemNode,
|
|
12
|
+
$isListNode as isListNode,
|
|
13
|
+
ListNode,
|
|
14
|
+
} from '@lexical/list';
|
|
15
|
+
import { $isHeadingNode as isHeadingNode, $isQuoteNode as isQuoteNode } from '@lexical/rich-text';
|
|
16
|
+
import { $getNearestNodeOfType as getNearestNodeOfType } from '@lexical/utils';
|
|
17
|
+
import {
|
|
18
|
+
ElementNode,
|
|
19
|
+
$getSelection as getSelection,
|
|
20
|
+
$isRangeSelection as isRangeSelection,
|
|
21
|
+
} from 'lexical';
|
|
22
|
+
import { BLOCK_BUTTON_TYPES, TEXT_FORMAT_BUTTON_TYPES } from '../constants.js';
|
|
23
|
+
import { $getSelectedAlignment } from './alignment.js';
|
|
24
|
+
import { $getSelectedTableCellKey } from './table.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @import { LexicalNode } from 'lexical';
|
|
28
|
+
* @import {
|
|
29
|
+
* TextEditorBlockType,
|
|
30
|
+
* TextEditorInlineType,
|
|
31
|
+
* TextEditorSelectionState,
|
|
32
|
+
* } from '../../../typedefs.js';
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get the given node itself if it’s an element node, or its nearest element node ancestor.
|
|
37
|
+
* @param {LexicalNode} node Node.
|
|
38
|
+
* @returns {ElementNode | null} Element node.
|
|
39
|
+
*/
|
|
40
|
+
export const getNearestElementNode = (node) =>
|
|
41
|
+
node instanceof ElementNode ? node : getNearestNodeOfType(node, ElementNode);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get the current selection’s block node key as well as block and inline level types.
|
|
45
|
+
* @internal
|
|
46
|
+
* @returns {TextEditorSelectionState} Current selection state.
|
|
47
|
+
*/
|
|
48
|
+
export const getSelectionTypes = () => {
|
|
49
|
+
const selection = getSelection();
|
|
50
|
+
|
|
51
|
+
if (!isRangeSelection(selection)) {
|
|
52
|
+
return {
|
|
53
|
+
blockNodeKey: null,
|
|
54
|
+
blockType: 'paragraph',
|
|
55
|
+
inlineTypes: [],
|
|
56
|
+
// Several cells can be selected at once
|
|
57
|
+
tableCellKey: $getSelectedTableCellKey(),
|
|
58
|
+
alignment: '',
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const anchor = selection.anchor.getNode();
|
|
63
|
+
/** @type {ElementNode | null} */
|
|
64
|
+
let parent = null;
|
|
65
|
+
/** @type {TextEditorInlineType[]} */
|
|
66
|
+
const inlineTypes = TEXT_FORMAT_BUTTON_TYPES.filter((type) => selection.hasFormat(type));
|
|
67
|
+
|
|
68
|
+
if (anchor.getType() !== 'root') {
|
|
69
|
+
parent = getNearestElementNode(anchor);
|
|
70
|
+
|
|
71
|
+
if (isLinkNode(parent)) {
|
|
72
|
+
inlineTypes.push('link');
|
|
73
|
+
// `getNearestNodeOfType()` would return the link itself, so start from its parent to find the
|
|
74
|
+
// block the link is in
|
|
75
|
+
parent = parent.getParent();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (isListItemNode(parent)) {
|
|
79
|
+
parent = getNearestNodeOfType(parent, ListNode);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const blockType = /** @type {TextEditorBlockType} */ (
|
|
84
|
+
(() => {
|
|
85
|
+
if (!parent) {
|
|
86
|
+
return 'paragraph';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (isHeadingNode(parent)) {
|
|
90
|
+
return `heading-${parent.getTag().match(/\d/)?.[0]}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (isListNode(parent)) {
|
|
94
|
+
return parent.getListType() === 'bullet' ? 'bulleted-list' : 'numbered-list';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (isQuoteNode(parent)) {
|
|
98
|
+
return 'blockquote';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (isCodeNode(parent) || isCodeHighlightNode(parent)) {
|
|
102
|
+
return 'code-block';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const type = parent.getType();
|
|
106
|
+
|
|
107
|
+
if (BLOCK_BUTTON_TYPES.includes(/** @type {any} */ (type))) {
|
|
108
|
+
return type;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return 'paragraph';
|
|
112
|
+
})()
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
blockNodeKey: parent?.getKey() ?? null,
|
|
117
|
+
blockType,
|
|
118
|
+
inlineTypes,
|
|
119
|
+
tableCellKey: $getSelectedTableCellKey(),
|
|
120
|
+
alignment: $getSelectedAlignment(),
|
|
121
|
+
};
|
|
122
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function limitToSingleLine(config: TextEditorConfig): TextEditorConfig;
|
|
2
|
+
export function $joinIntoSingleParagraph(root: RootNode): void;
|
|
3
|
+
export function $replaceLineBreak(node: LineBreakNode): void;
|
|
4
|
+
export function registerSingleLine(editor: LexicalEditor): () => void;
|
|
5
|
+
import type { TextEditorConfig } from '../../../typedefs.js';
|
|
6
|
+
import { RootNode } from 'lexical';
|
|
7
|
+
import { LineBreakNode } from 'lexical';
|
|
8
|
+
import type { LexicalEditor } from 'lexical';
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping a single-line editor on one line.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { $isLinkNode as isLinkNode } from '@lexical/link';
|
|
6
|
+
import {
|
|
7
|
+
COMMAND_PRIORITY_HIGH,
|
|
8
|
+
$createParagraphNode as createParagraphNode,
|
|
9
|
+
$createTextNode as createTextNode,
|
|
10
|
+
INSERT_LINE_BREAK_COMMAND,
|
|
11
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
12
|
+
KEY_ENTER_COMMAND,
|
|
13
|
+
LineBreakNode,
|
|
14
|
+
$isElementNode as isElementNode,
|
|
15
|
+
$isParagraphNode as isParagraphNode,
|
|
16
|
+
$isTextNode as isTextNode,
|
|
17
|
+
RootNode,
|
|
18
|
+
} from 'lexical';
|
|
19
|
+
import { INLINE_BUTTON_TYPES, TEXT_FORMAT_BUTTON_TYPES } from '../constants.js';
|
|
20
|
+
import { isInlineComponent } from '../conversion/editor-components.js';
|
|
21
|
+
import { mergeUnregisters } from '../utils.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @import { LexicalEditor, LexicalNode } from 'lexical';
|
|
25
|
+
* @import { TextEditorConfig, TextEditorNodeType } from '../../../typedefs.js';
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Limit the given editor configuration to what a single-line editor can hold, if it’s one: only
|
|
30
|
+
* the inline buttons and components, and no plain text mode for HTML, whose source can’t be kept to
|
|
31
|
+
* a single paragraph by keeping it on a single line. Any other configuration is returned as is.
|
|
32
|
+
* @param {TextEditorConfig} config Editor configuration.
|
|
33
|
+
* @returns {TextEditorConfig} Limited configuration.
|
|
34
|
+
*/
|
|
35
|
+
export const limitToSingleLine = (config) => {
|
|
36
|
+
if (config.multiline !== false || config.isCodeEditor) {
|
|
37
|
+
return config;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
...config,
|
|
42
|
+
modes: config.format === 'html' ? ['rich-text'] : config.modes,
|
|
43
|
+
enabledButtons: (config.enabledButtons ?? []).filter((button) =>
|
|
44
|
+
/** @type {TextEditorNodeType[]} */ (INLINE_BUTTON_TYPES).includes(button),
|
|
45
|
+
),
|
|
46
|
+
components: (config.components ?? []).filter(isInlineComponent),
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get the inline content of the given top-level node, looking into any block within it.
|
|
52
|
+
* @param {LexicalNode} node Node.
|
|
53
|
+
* @returns {LexicalNode[]} Inline nodes, or an empty array for a block that has no inline content,
|
|
54
|
+
* like a horizontal rule.
|
|
55
|
+
*/
|
|
56
|
+
const getInlineContent = (node) => {
|
|
57
|
+
if (isElementNode(node) && !node.isInline()) {
|
|
58
|
+
return node.getChildren().flatMap(getInlineContent);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return node.isInline() ? [node] : [];
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Join the top-level nodes of the given root into a single paragraph, with a space between the
|
|
66
|
+
* content of each, for a single-line editor.
|
|
67
|
+
* @param {RootNode} root Root node.
|
|
68
|
+
*/
|
|
69
|
+
export const $joinIntoSingleParagraph = (root) => {
|
|
70
|
+
const children = root.getChildren();
|
|
71
|
+
const [firstChild] = children;
|
|
72
|
+
|
|
73
|
+
if (!firstChild || (children.length === 1 && isParagraphNode(firstChild))) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const paragraph = isParagraphNode(firstChild) ? firstChild : createParagraphNode();
|
|
78
|
+
|
|
79
|
+
if (paragraph !== firstChild) {
|
|
80
|
+
firstChild.insertBefore(paragraph);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Keep track of the last character rather than reading the growing text of the paragraph again
|
|
84
|
+
// for each block, which would take quadratic time on a long pasted document
|
|
85
|
+
let lastChar = paragraph.getTextContent().at(-1) ?? '';
|
|
86
|
+
|
|
87
|
+
children
|
|
88
|
+
.filter((child) => child !== paragraph)
|
|
89
|
+
.forEach((child) => {
|
|
90
|
+
const content = getInlineContent(child);
|
|
91
|
+
const text = content.map((node) => node.getTextContent()).join('');
|
|
92
|
+
|
|
93
|
+
// Add a space between the content of each block, unless there’s one already
|
|
94
|
+
if (content.length && paragraph.getChildrenSize() && !/\s/.test(lastChar + text.charAt(0))) {
|
|
95
|
+
paragraph.append(createTextNode(' '));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
paragraph.append(...content);
|
|
99
|
+
lastChar = text.at(-1) ?? lastChar;
|
|
100
|
+
// Lexical moves the selection out of a removed node on its own
|
|
101
|
+
child.remove();
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Replace the given line break with a space for a single-line editor, with the formats the text on
|
|
107
|
+
* both sides shares, so a line break within bold text doesn’t split it. With a space on either side
|
|
108
|
+
* already, or nothing at all, the line break is just removed.
|
|
109
|
+
* @param {LineBreakNode} node Line break.
|
|
110
|
+
*/
|
|
111
|
+
export const $replaceLineBreak = (node) => {
|
|
112
|
+
const previous = node.getPreviousSibling();
|
|
113
|
+
const next = node.getNextSibling();
|
|
114
|
+
|
|
115
|
+
// A line break at either end of the paragraph separates nothing, so it leaves no space
|
|
116
|
+
if (
|
|
117
|
+
!previous ||
|
|
118
|
+
!next ||
|
|
119
|
+
/\s$/.test(previous.getTextContent()) ||
|
|
120
|
+
/^\s/.test(next.getTextContent())
|
|
121
|
+
) {
|
|
122
|
+
node.remove();
|
|
123
|
+
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const space = createTextNode(' ');
|
|
128
|
+
|
|
129
|
+
// A link split over lines, like the ones `splitMultilineFormatting()` makes from a link within
|
|
130
|
+
// formatting, is joined back into one link
|
|
131
|
+
const isSameLink =
|
|
132
|
+
isLinkNode(previous) &&
|
|
133
|
+
isLinkNode(next) &&
|
|
134
|
+
previous.getURL() === next.getURL() &&
|
|
135
|
+
previous.getTitle() === next.getTitle() &&
|
|
136
|
+
previous.getTarget() === next.getTarget() &&
|
|
137
|
+
previous.getRel() === next.getRel();
|
|
138
|
+
|
|
139
|
+
// The text next to the line break, also within an inline element like a link
|
|
140
|
+
const before = isElementNode(previous) ? previous.getLastDescendant() : previous;
|
|
141
|
+
const after = isElementNode(next) ? next.getFirstDescendant() : next;
|
|
142
|
+
|
|
143
|
+
if (isTextNode(before) && isTextNode(after)) {
|
|
144
|
+
// Keep the formats both sides share, e.g. italic between italic and bold italic text
|
|
145
|
+
TEXT_FORMAT_BUTTON_TYPES.forEach((format) => {
|
|
146
|
+
if (before.hasFormat(format) && after.hasFormat(format)) {
|
|
147
|
+
space.toggleFormat(format);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
if (before.getStyle() === after.getStyle()) {
|
|
152
|
+
space.setStyle(before.getStyle());
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (isSameLink) {
|
|
157
|
+
previous.append(space, ...next.getChildren());
|
|
158
|
+
next.remove();
|
|
159
|
+
node.remove();
|
|
160
|
+
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
node.replace(space);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Register the commands and transforms that keep a single-line editor on one line. Neither a new
|
|
169
|
+
* paragraph nor a line break can be inserted, and any content that brings them in anyway, like
|
|
170
|
+
* pasted text or an imported value, is joined into a single paragraph with spaces.
|
|
171
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
172
|
+
* @returns {() => void} Cleanup handler.
|
|
173
|
+
*/
|
|
174
|
+
export const registerSingleLine = (editor) =>
|
|
175
|
+
mergeUnregisters(
|
|
176
|
+
// The emoji autocomplete, which selects an emoji with the Enter key, takes priority
|
|
177
|
+
editor.registerCommand(
|
|
178
|
+
KEY_ENTER_COMMAND,
|
|
179
|
+
(event) => {
|
|
180
|
+
event?.preventDefault();
|
|
181
|
+
|
|
182
|
+
return true;
|
|
183
|
+
},
|
|
184
|
+
COMMAND_PRIORITY_HIGH,
|
|
185
|
+
),
|
|
186
|
+
editor.registerCommand(INSERT_PARAGRAPH_COMMAND, () => true, COMMAND_PRIORITY_HIGH),
|
|
187
|
+
editor.registerCommand(INSERT_LINE_BREAK_COMMAND, () => true, COMMAND_PRIORITY_HIGH),
|
|
188
|
+
editor.registerNodeTransform(LineBreakNode, $replaceLineBreak),
|
|
189
|
+
editor.registerNodeTransform(RootNode, $joinIntoSingleParagraph),
|
|
190
|
+
);
|
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
$setSelection as setSelection,
|
|
29
29
|
} from 'lexical';
|
|
30
30
|
import { $insertBlockNode } from './block.js';
|
|
31
|
-
import { MAX_TABLE_SIZE } from '
|
|
31
|
+
import { MAX_TABLE_SIZE } from '../constants.js';
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* @import { LexicalNode, RangeSelection } from 'lexical';
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
import { $getRoot as getRoot, HISTORY_MERGE_TAG } from 'lexical';
|
|
3
3
|
import { getContext, onMount, untrack } from 'svelte';
|
|
4
4
|
import { IMPORT_UPDATE_TAG } from './constants.js';
|
|
5
|
-
import { handleEditorMouseDown,
|
|
5
|
+
import { handleEditorMouseDown, isStaticDecoratorContent } from './editing/caret.js';
|
|
6
|
+
import { initEditor } from './core.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @import { Snippet } from 'svelte';
|
|
@@ -146,6 +147,33 @@
|
|
|
146
147
|
*/
|
|
147
148
|
const isOtherDialog = (dialog) => !!dialog && !dialog.contains(lexicalRoot ?? null);
|
|
148
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Interactive element in an editor component the pointer has just been pressed on, if any.
|
|
152
|
+
* @type {Element | undefined}
|
|
153
|
+
*/
|
|
154
|
+
let pressedComponentElement;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Track the pointer being pressed on an interactive element within an editor component. Safari
|
|
158
|
+
* doesn’t focus a button on click, like one on the toolbar of a nested editor, and moves the
|
|
159
|
+
* focus to the closest focusable ancestor, which is the root, or `<body>` instead. Neither is the
|
|
160
|
+
* focus leaving the component. A press on its static content, like its label, does move the
|
|
161
|
+
* focus to the root; see `handleEditorMouseDown()`.
|
|
162
|
+
* @param {MouseEvent} event `mousedown` event.
|
|
163
|
+
*/
|
|
164
|
+
const onComponentMouseDown = ({ target }) => {
|
|
165
|
+
if (
|
|
166
|
+
target instanceof Element &&
|
|
167
|
+
target.closest('[data-lexical-decorator]') &&
|
|
168
|
+
!isStaticDecoratorContent(target)
|
|
169
|
+
) {
|
|
170
|
+
pressedComponentElement = target;
|
|
171
|
+
window.setTimeout(() => {
|
|
172
|
+
pressedComponentElement = undefined;
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
149
177
|
/**
|
|
150
178
|
* Track the focus, to tell whether it’s in an editor component, like a nested editor or a field
|
|
151
179
|
* of the component, rather than on the editor itself. The focus moving to another dialog doesn’t
|
|
@@ -156,6 +184,11 @@
|
|
|
156
184
|
const node = /** @type {Node} */ (target);
|
|
157
185
|
|
|
158
186
|
if (lexicalRoot?.contains(node)) {
|
|
187
|
+
// Safari moves the focus to the root when a button in a component is pressed; see above
|
|
188
|
+
if (node === lexicalRoot && pressedComponentElement?.isConnected) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
159
192
|
untrack(() => {
|
|
160
193
|
editorStore.componentHasFocus = node !== lexicalRoot;
|
|
161
194
|
});
|
|
@@ -166,22 +199,34 @@
|
|
|
166
199
|
}
|
|
167
200
|
};
|
|
168
201
|
|
|
202
|
+
/**
|
|
203
|
+
* Clear the state if the focus has left the editor component, landing on `<body>`, unless the
|
|
204
|
+
* pointer has just been pressed within the component, or another modal dialog is open.
|
|
205
|
+
*/
|
|
206
|
+
const checkComponentFocus = () => {
|
|
207
|
+
// The update listener below may run within an effect, which mustn’t depend on the state
|
|
208
|
+
untrack(() => {
|
|
209
|
+
if (
|
|
210
|
+
editorStore.componentHasFocus &&
|
|
211
|
+
document.activeElement === document.body &&
|
|
212
|
+
!pressedComponentElement?.isConnected &&
|
|
213
|
+
![...document.querySelectorAll(':modal')].some(isOtherDialog)
|
|
214
|
+
) {
|
|
215
|
+
editorStore.componentHasFocus = false;
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
|
|
169
220
|
/**
|
|
170
221
|
* Track the focus leaving a removed element, which doesn’t fire `focusin`. The event also comes
|
|
171
222
|
* without a related target when a dialog opens, which only moves the focus into itself a moment
|
|
172
|
-
* later, so the state is kept while another modal dialog is open.
|
|
223
|
+
* later, so the state is kept while another modal dialog is open. Firefox and Safari don’t fire
|
|
224
|
+
* the event for a removed element at all; see the update listener below.
|
|
173
225
|
* @param {FocusEvent} event `focusout` event.
|
|
174
226
|
*/
|
|
175
227
|
const onFocusOut = ({ relatedTarget }) => {
|
|
176
228
|
if (!relatedTarget) {
|
|
177
|
-
queueMicrotask(
|
|
178
|
-
if (
|
|
179
|
-
document.activeElement === document.body &&
|
|
180
|
-
![...document.querySelectorAll(':modal')].some(isOtherDialog)
|
|
181
|
-
) {
|
|
182
|
-
editorStore.componentHasFocus = false;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
229
|
+
queueMicrotask(checkComponentFocus);
|
|
185
230
|
}
|
|
186
231
|
};
|
|
187
232
|
|
|
@@ -194,6 +239,7 @@
|
|
|
194
239
|
lexicalRoot?.addEventListener('Update', onUpdate);
|
|
195
240
|
lexicalRoot?.addEventListener('click', onClick);
|
|
196
241
|
lexicalRoot?.addEventListener('mousedown', onMouseDown);
|
|
242
|
+
lexicalRoot?.addEventListener('mousedown', onComponentMouseDown);
|
|
197
243
|
document.addEventListener('focusin', onFocusIn);
|
|
198
244
|
document.addEventListener('focusout', onFocusOut);
|
|
199
245
|
|
|
@@ -209,6 +255,10 @@
|
|
|
209
255
|
) {
|
|
210
256
|
editorStore.pending = true;
|
|
211
257
|
}
|
|
258
|
+
|
|
259
|
+
// An editor component with the focus may have been removed, which doesn’t fire `focusout`
|
|
260
|
+
// in Firefox and Safari
|
|
261
|
+
checkComponentFocus();
|
|
212
262
|
},
|
|
213
263
|
);
|
|
214
264
|
|
|
@@ -217,6 +267,7 @@
|
|
|
217
267
|
lexicalRoot?.removeEventListener('Update', onUpdate);
|
|
218
268
|
lexicalRoot?.removeEventListener('click', onClick);
|
|
219
269
|
lexicalRoot?.removeEventListener('mousedown', onMouseDown);
|
|
270
|
+
lexicalRoot?.removeEventListener('mousedown', onComponentMouseDown);
|
|
220
271
|
document.removeEventListener('focusin', onFocusIn);
|
|
221
272
|
document.removeEventListener('focusout', onFocusOut);
|
|
222
273
|
dispose();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function loadCodeHighlighter(lang: string): Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preloading the syntax highlighter.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
isPlainLanguage,
|
|
7
|
+
loadCodeLanguage,
|
|
8
|
+
loadCodeTheme,
|
|
9
|
+
loadEngine,
|
|
10
|
+
normalizeCodeLanguage,
|
|
11
|
+
} from './facade.js';
|
|
12
|
+
import { getCodeTheme } from './theme.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Preload the syntax highlighter for the given programming language.
|
|
16
|
+
*
|
|
17
|
+
* Highlighting also works without this — the transform loads whatever it needs and re-highlights
|
|
18
|
+
* once it arrives — but preloading avoids a visible flash of unhighlighted code.
|
|
19
|
+
* @param {string} lang Language name, like scss.
|
|
20
|
+
*/
|
|
21
|
+
export const loadCodeHighlighter = async (lang) => {
|
|
22
|
+
if (isPlainLanguage(lang)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The grammar and theme loaders are no-ops until the engine is in place
|
|
27
|
+
await loadEngine();
|
|
28
|
+
|
|
29
|
+
await Promise.all([loadCodeLanguage(normalizeCodeLanguage(lang)), loadCodeTheme(getCodeTheme())]);
|
|
30
|
+
};
|
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
export function createEditorStore(): TextEditorStore;
|
|
2
|
+
export function syncEditorProps(editorStore: TextEditorStore, { getInputValue, setValue, setPending }: {
|
|
3
|
+
getInputValue: () => string;
|
|
4
|
+
setValue: (inputValue: string) => void;
|
|
5
|
+
setPending: (pending: boolean) => void;
|
|
6
|
+
}): void;
|
|
2
7
|
import type { TextEditorStore } from '../../typedefs.js';
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { generateElementId } from '@sveltia/utils/element';
|
|
2
|
-
import {
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { convertHtmlToLexical, convertMarkdownToLexical } from './conversion/import.js';
|
|
4
|
+
import { limitToSingleLine } from './editing/single-line.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* @import { LexicalEditor } from 'lexical';
|
|
@@ -256,3 +258,55 @@ export const createEditorStore = () => {
|
|
|
256
258
|
/* eslint-enable jsdoc/require-jsdoc */
|
|
257
259
|
};
|
|
258
260
|
};
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Keep the bindable props of a `<TextEditor>` or `<CodeEditor>` in sync with its editor store: the
|
|
264
|
+
* `pending` flag, and the value going into and coming out of the editor. This must be called
|
|
265
|
+
* during the component initialization, after the store is created.
|
|
266
|
+
* @param {TextEditorStore} editorStore Editor store.
|
|
267
|
+
* @param {object} args Arguments.
|
|
268
|
+
* @param {() => string} args.getInputValue Read the props and return them as the editor input
|
|
269
|
+
* value. The props read here are tracked, so the editor is updated whenever they change.
|
|
270
|
+
* @param {(inputValue: string) => void} args.setValue Update the props with the editor input value
|
|
271
|
+
* when it changes.
|
|
272
|
+
* @param {(pending: boolean) => void} args.setPending Update the `pending` prop.
|
|
273
|
+
*/
|
|
274
|
+
export const syncEditorProps = (editorStore, { getInputValue, setValue, setPending }) => {
|
|
275
|
+
$effect(() => {
|
|
276
|
+
setPending(editorStore.pending);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// An editor removed right after a change never converts it, so don’t leave a bound `pending` set
|
|
280
|
+
// for good, which would hold up anything waiting for the change
|
|
281
|
+
$effect(() => () => {
|
|
282
|
+
setPending(false);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
$effect(() => {
|
|
286
|
+
// The root initializes the editor before these effects first run, and stays initialized
|
|
287
|
+
/* v8 ignore next */
|
|
288
|
+
if (!editorStore.initialized) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const newValue = getInputValue();
|
|
293
|
+
|
|
294
|
+
untrack(() => {
|
|
295
|
+
editorStore.inputValue = newValue;
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
$effect(() => {
|
|
300
|
+
// The root initializes the editor before these effects first run, and stays initialized
|
|
301
|
+
/* v8 ignore next */
|
|
302
|
+
if (!editorStore.initialized) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const newValue = editorStore.inputValue;
|
|
307
|
+
|
|
308
|
+
untrack(() => {
|
|
309
|
+
setValue(newValue);
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
};
|