@rathkit/rte-core 0.1.0-alpha.4 → 0.1.0-alpha.5
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/README.md +1 -1
- package/dist/autoformat/autoformat.js +18 -3
- package/dist/base64.js +5 -1
- package/dist/dom/index.d.ts +1 -1
- package/dist/dom/render.js +4 -4
- package/dist/dom/view.d.ts +54 -3
- package/dist/dom/view.js +163 -24
- package/dist/edit.d.ts +6 -0
- package/dist/edit.js +96 -9
- package/dist/engine.d.ts +19 -3
- package/dist/engine.js +96 -22
- package/dist/export.js +1 -1
- package/dist/html/parse.d.ts +8 -2
- package/dist/html/parse.js +6 -2
- package/dist/images/plugin.js +2 -1
- package/dist/images/view.d.ts +1 -1
- package/dist/images/view.js +4 -4
- package/dist/import.d.ts +18 -2
- package/dist/import.js +35 -27
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/lists/lists.js +14 -4
- package/dist/markdown/export.js +1 -1
- package/dist/markdown/inline.d.ts +2 -0
- package/dist/markdown/inline.js +1 -1
- package/dist/markdown/plugin.js +4 -1
- package/dist/model.d.ts +5 -0
- package/dist/paste.js +9 -2
- package/dist/path.d.ts +7 -0
- package/dist/path.js +20 -7
- package/dist/plugin.d.ts +1 -1
- package/dist/policy.d.ts +41 -0
- package/dist/policy.js +40 -0
- package/dist/standard/index.d.ts +0 -1
- package/dist/standard/index.js +0 -1
- package/dist/stats.js +1 -1
- package/dist/svg/svg.js +9 -1
- package/dist/tables/cells.d.ts +1 -1
- package/dist/tables/cells.js +3 -3
- package/dist/tables/grips.js +2 -2
- package/dist/warning.d.ts +16 -0
- package/dist/warning.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @rathkit/rte-core
|
|
2
2
|
|
|
3
|
-
> **Alpha
|
|
3
|
+
> **Alpha.** Not for production: the APIs will change between releases. Published to the `next` tag only.
|
|
4
4
|
|
|
5
5
|
The editor itself: the document model, HTML import and export, editing operations, commands and keys, the plugin host, the browser view, and the free plugins (lists, tables, find and replace, word-processor lists, SVG, images). The model does not touch the DOM, so it also runs in Node.
|
|
6
6
|
|
|
@@ -73,7 +73,19 @@ export function spanShortcut(doc, sel, typed) {
|
|
|
73
73
|
}
|
|
74
74
|
return undefined;
|
|
75
75
|
}
|
|
76
|
-
const
|
|
76
|
+
const ADDRESS_START = /(?:https?:\/\/|www\.)/gi;
|
|
77
|
+
const ADDRESS_BREAK = /[\s<>"]/;
|
|
78
|
+
function addressAtEnd(text) {
|
|
79
|
+
let from = text.length;
|
|
80
|
+
while (from > 0 && !ADDRESS_BREAK.test(text[from - 1]))
|
|
81
|
+
from--;
|
|
82
|
+
ADDRESS_START.lastIndex = from;
|
|
83
|
+
for (let m = ADDRESS_START.exec(text); m; m = ADDRESS_START.exec(text)) {
|
|
84
|
+
if (m.index + m[0].length < text.length)
|
|
85
|
+
return { index: m.index, text: text.slice(m.index) };
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
77
89
|
export function addressShortcut(doc, sel, typed) {
|
|
78
90
|
if (typed !== " " || !isCollapsed(sel))
|
|
79
91
|
return undefined;
|
|
@@ -83,10 +95,13 @@ export function addressShortcut(doc, sel, typed) {
|
|
|
83
95
|
const content = getContent(doc, path);
|
|
84
96
|
const offset = sel.anchor.offset;
|
|
85
97
|
const before = inlineText(sliceInline(content, 0, offset));
|
|
86
|
-
const m =
|
|
98
|
+
const m = addressAtEnd(before);
|
|
87
99
|
if (!m)
|
|
88
100
|
return undefined;
|
|
89
|
-
|
|
101
|
+
let stop = m.text.length;
|
|
102
|
+
while (stop > 0 && ".,;:!?)]'\"".includes(m.text[stop - 1]))
|
|
103
|
+
stop--;
|
|
104
|
+
const word = m.text.slice(0, stop);
|
|
90
105
|
if (word.replace(/^(?:https?:\/\/|www\.)/i, "").length < 3 || !/[a-z0-9]/i.test(word.replace(/^(?:https?:\/\/|www\.)/i, "")))
|
|
91
106
|
return undefined;
|
|
92
107
|
const start = m.index;
|
package/dist/base64.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
2
2
|
export function decodeBase64(input) {
|
|
3
|
-
const
|
|
3
|
+
const spaced = input.replace(/\s+/g, "");
|
|
4
|
+
let end = spaced.length;
|
|
5
|
+
while (end > 0 && spaced[end - 1] === "=")
|
|
6
|
+
end--;
|
|
7
|
+
const text = spaced.slice(0, end);
|
|
4
8
|
if (/[^A-Za-z0-9+/]/.test(text))
|
|
5
9
|
return undefined;
|
|
6
10
|
const out = new Uint8Array(Math.floor((text.length * 3) / 4));
|
package/dist/dom/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Editor, type EditorOptions } from "./view.ts";
|
|
1
|
+
export { Editor, type EditorEvents, type EditorOptions } from "./view.ts";
|
|
2
2
|
export { renderDoc, posFromDom, domFromPos } from "./render.ts";
|
package/dist/dom/render.js
CHANGED
|
@@ -112,7 +112,7 @@ function renderList(list, path, options) {
|
|
|
112
112
|
if (list.style !== undefined)
|
|
113
113
|
el.style.listStyleType = list.style;
|
|
114
114
|
if (list.notes) {
|
|
115
|
-
el.setAttribute("data-
|
|
115
|
+
el.setAttribute("data-rte-notes", "");
|
|
116
116
|
el.setAttribute("role", "doc-endnotes");
|
|
117
117
|
el.setAttribute("aria-label", "Notes");
|
|
118
118
|
}
|
|
@@ -120,12 +120,12 @@ function renderList(list, path, options) {
|
|
|
120
120
|
const itemPath = [...path, i];
|
|
121
121
|
const li = tag("li", itemPath);
|
|
122
122
|
if (list.notes && item.id)
|
|
123
|
-
li.setAttribute("data-
|
|
124
|
-
if (item.content.length === 0 && (item.children[0]?.type === "ul" || item.children[0]?.type === "ol"))
|
|
123
|
+
li.setAttribute("data-rte-note-id", item.id);
|
|
124
|
+
if (item.bare === true && item.content.length === 0 && (item.children[0]?.type === "ul" || item.children[0]?.type === "ol"))
|
|
125
125
|
li.style.listStyleType = "none";
|
|
126
126
|
if (item.checked !== undefined) {
|
|
127
127
|
li.style.listStyleType = "none";
|
|
128
|
-
li.setAttribute("data-
|
|
128
|
+
li.setAttribute("data-rte-task", item.checked ? "done" : "open");
|
|
129
129
|
const box = document.createElement("span");
|
|
130
130
|
box.setAttribute("data-rte-check", "");
|
|
131
131
|
box.setAttribute("role", "checkbox");
|
package/dist/dom/view.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Engine } from "../engine.ts";
|
|
2
2
|
import type { Plugin } from "../plugin.ts";
|
|
3
|
+
import type { ContentOptions } from "../policy.ts";
|
|
4
|
+
import type { Warning } from "../warning.ts";
|
|
3
5
|
import { type Doc, type MarkType } from "../model.ts";
|
|
4
6
|
import { type ClipboardData, type PasteHandler } from "../paste.ts";
|
|
5
7
|
import { type Pos, type Selection } from "../path.ts";
|
|
@@ -7,7 +9,10 @@ export interface EditorOptions {
|
|
|
7
9
|
readonly element: HTMLElement;
|
|
8
10
|
readonly html?: string;
|
|
9
11
|
readonly pasteHandlers?: readonly PasteHandler[];
|
|
12
|
+
/** No edits; the page can still be focused, selected and copied from. Default false. */
|
|
10
13
|
readonly readOnly?: boolean;
|
|
14
|
+
/** Read-only, and also out of the tab order and not focusable: for a field that does not apply now. Default false. */
|
|
15
|
+
readonly disabled?: boolean;
|
|
11
16
|
/** Draw remote (`http`, `https`) images. Off by default, so pasting never makes the browser fetch anything. */
|
|
12
17
|
readonly loadRemoteImages?: boolean;
|
|
13
18
|
/** Called with the `inputType` of input the editor does not handle. That input is blocked, not applied. */
|
|
@@ -15,12 +20,16 @@ export interface EditorOptions {
|
|
|
15
20
|
/** Plugins to add: lists, for one (`@rathkit/rte-core/lists`). With none, the editor has only what is built in. */
|
|
16
21
|
readonly plugins?: readonly Plugin[];
|
|
17
22
|
/**
|
|
18
|
-
* A hint shown while the document is empty. The editor marks itself `data-
|
|
19
|
-
* technology (`aria-placeholder`) and puts it in the custom property `--
|
|
23
|
+
* A hint shown while the document is empty. The editor marks itself `data-rte-empty` when empty, says the hint to assistive
|
|
24
|
+
* technology (`aria-placeholder`) and puts it in the custom property `--rte-placeholder`; the styled interface draws it.
|
|
20
25
|
*/
|
|
21
26
|
readonly placeholder?: string;
|
|
22
27
|
/** The most text the document may hold, in characters. Typing and pasting past it are refused (see `onMaxLength`). */
|
|
23
28
|
readonly maxLength?: number;
|
|
29
|
+
/** Told about problems the editor carried on from (a plugin that threw, content that was refused): see docs/ERRORS.md. Default: the console. */
|
|
30
|
+
readonly onWarning?: (warning: Warning) => void;
|
|
31
|
+
/** What is kept and dropped when HTML is read: a profile and single choices on top of it. See docs/CONTENT-POLICY.md. */
|
|
32
|
+
readonly content?: ContentOptions;
|
|
24
33
|
/** Called when an edit is refused for being past `maxLength`, with the limit and what the length would have been. */
|
|
25
34
|
readonly onMaxLength?: (info: {
|
|
26
35
|
readonly limit: number;
|
|
@@ -32,12 +41,29 @@ export interface EditorOptions {
|
|
|
32
41
|
readonly autofocus?: boolean;
|
|
33
42
|
}
|
|
34
43
|
type Listener = (editor: Editor) => void;
|
|
44
|
+
/**
|
|
45
|
+
* What an editor reports, by name (`editor.on(name, listener)`). Every package that wraps the editor reports the same five things
|
|
46
|
+
* with the same timing; see docs/EVENTS.md.
|
|
47
|
+
* - `change`: the document's HTML, after an edit that altered it. Not for `setHtml`, and not for a selection that moved.
|
|
48
|
+
* - `focus` and `blur`: the editor's own focus moved in or out.
|
|
49
|
+
* - `selection`: the selection moved or changed.
|
|
50
|
+
*/
|
|
51
|
+
export interface EditorEvents {
|
|
52
|
+
change: (html: string) => void;
|
|
53
|
+
focus: (editor: Editor) => void;
|
|
54
|
+
blur: (editor: Editor) => void;
|
|
55
|
+
selection: (selection: Selection) => void;
|
|
56
|
+
}
|
|
35
57
|
export declare class Editor {
|
|
36
58
|
readonly element: HTMLElement;
|
|
37
59
|
/** The editor's state, commands, keys and plugins. It has no DOM, so everything it does can be driven without a page. */
|
|
38
60
|
readonly engine: Engine;
|
|
39
61
|
private composing;
|
|
40
62
|
private reading;
|
|
63
|
+
private userReadOnly;
|
|
64
|
+
private isDisabled;
|
|
65
|
+
private ownTabindex;
|
|
66
|
+
private savedTabindex;
|
|
41
67
|
private readonly loadRemoteImages;
|
|
42
68
|
private readonly unhandled;
|
|
43
69
|
private dragSource;
|
|
@@ -46,6 +72,10 @@ export declare class Editor {
|
|
|
46
72
|
private readonly observer;
|
|
47
73
|
private readonly cleanups;
|
|
48
74
|
private readonly changeListeners;
|
|
75
|
+
/** Set while `setHtml` replaces the document, so replacing it is not reported as an edit. */
|
|
76
|
+
private replacing;
|
|
77
|
+
/** Lets each `change` listener forget what it last reported, after the document was replaced. */
|
|
78
|
+
private readonly resyncs;
|
|
49
79
|
private readonly viewHost;
|
|
50
80
|
constructor(options: EditorOptions);
|
|
51
81
|
get doc(): Doc;
|
|
@@ -56,7 +86,10 @@ export declare class Editor {
|
|
|
56
86
|
rangeFor(from: Pos, to: Pos): Range | null;
|
|
57
87
|
get canUndo(): boolean;
|
|
58
88
|
get canRedo(): boolean;
|
|
89
|
+
/** Whether edits are refused: read-only, or disabled. */
|
|
59
90
|
get readOnly(): boolean;
|
|
91
|
+
/** Whether the editor is disabled: read-only and out of the tab order. */
|
|
92
|
+
get disabled(): boolean;
|
|
60
93
|
/** Named commands and their state: `editor.commands.run("format.bold")`, `editor.commands.state("format.bold")`. */
|
|
61
94
|
get commands(): Engine["commands"];
|
|
62
95
|
/** Interface items (toolbar buttons and the like) that the editor and its plugins offer. */
|
|
@@ -76,9 +109,19 @@ export declare class Editor {
|
|
|
76
109
|
remoteImages(): string[];
|
|
77
110
|
/** Sets the hint shown while the document is empty ("" for none). */
|
|
78
111
|
setPlaceholder(text: string): void;
|
|
79
|
-
/** Replaces the whole document and empties the history. */
|
|
112
|
+
/** Replaces the whole document and empties the history. Reported to nobody as a `change`, like setting the value of a text field. */
|
|
80
113
|
setHtml(html: string): void;
|
|
114
|
+
/** Listens for `change`, `focus`, `blur` or `selection` (see `EditorEvents`). Returns a function that stops listening. */
|
|
115
|
+
on<K extends keyof EditorEvents>(event: K, listener: EditorEvents[K]): () => void;
|
|
116
|
+
/** Read-only on or off. While the editor is disabled it stays read-only whatever this says. */
|
|
81
117
|
setReadOnly(value: boolean): void;
|
|
118
|
+
/** Disabled on or off. Disabled is read-only, not focusable and out of the tab order. */
|
|
119
|
+
setDisabled(value: boolean): void;
|
|
120
|
+
/**
|
|
121
|
+
* The page attributes that say what the editor allows: editable, read-only (focusable, `aria-readonly`) or disabled (not
|
|
122
|
+
* focusable, `aria-disabled`). Only the attributes this sets are touched; a `tabindex` the page gave is its own.
|
|
123
|
+
*/
|
|
124
|
+
private applyAccess;
|
|
82
125
|
/** Calls back after the document or selection changed, once per turn. Same as `subscribe`, kept for older callers. */
|
|
83
126
|
onChange(listener: Listener): () => void;
|
|
84
127
|
destroy(): void;
|
|
@@ -104,6 +147,8 @@ export declare class Editor {
|
|
|
104
147
|
* Brings the editor's selection up to date from the page. Only while the editor has the focus: when focus is somewhere else (a dialog,
|
|
105
148
|
* a toolbar button) the page's selection is not the editor's business, and the editor keeps the selection it had.
|
|
106
149
|
*/
|
|
150
|
+
/** Set while a selection is read when some point had to be moved to a text position. */
|
|
151
|
+
private fellBack;
|
|
107
152
|
private readSelection;
|
|
108
153
|
/**
|
|
109
154
|
* A point on the page as a position in the document. Some browsers report a selection that spans whole blocks (select all, in Firefox)
|
|
@@ -137,5 +182,11 @@ export declare class Editor {
|
|
|
137
182
|
private pasteFiles;
|
|
138
183
|
/** Keys go through the key map: a chord bound to a command runs it, and the event is stopped if the command changed something. */
|
|
139
184
|
private onKeyDown;
|
|
185
|
+
/**
|
|
186
|
+
* Backspace at the very start of the document, and Delete at the very end, change something here (a code block or heading becomes a
|
|
187
|
+
* paragraph, a rule goes) but there is nothing for the browser to delete, so WebKit sends no input event for them. They are handled
|
|
188
|
+
* from the key instead.
|
|
189
|
+
*/
|
|
190
|
+
private keyAtDocumentEdge;
|
|
140
191
|
}
|
|
141
192
|
export {};
|
package/dist/dom/view.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createEngine } from "../core.js";
|
|
2
|
-
import { caret, clampPos, deleteBackward, deleteForward, deleteSelection, deleteToEdge, deleteWord, extractSelection, insertBlocks, insertLineBreak, insertText, isCollapsed, moveSelection, selectedText, splitBlock, transposeChars, } from "../edit.js";
|
|
2
|
+
import { caret, clampPos, deleteBackward, deleteForward, deleteSelection, deleteToEdge, deleteWord, extractSelection, insertBlocks, insertLineBreak, endAtBlockEnd, insertText, isCollapsed, moveSelection, selectedText, splitBlock, transposeChars, } from "../edit.js";
|
|
3
3
|
import { exportHtml } from "../export.js";
|
|
4
4
|
import { ops } from "../ops.js";
|
|
5
5
|
import { importBlocks, importHtml } from "../import.js";
|
|
6
6
|
import { inlineLength } from "../model.js";
|
|
7
7
|
import { pasteBlocks } from "../paste.js";
|
|
8
|
-
import { cellChain, getContent, setContent } from "../path.js";
|
|
8
|
+
import { cellChain, containerPaths, getContent, pathKey, setContent } from "../path.js";
|
|
9
9
|
import { tableFromText } from "../tablefill.js";
|
|
10
10
|
import { imageSelection } from "../imageselect.js";
|
|
11
11
|
import { containerAt, createRenderCache, domFromPos, posFromDom, renderDoc } from "./render.js";
|
|
@@ -23,6 +23,10 @@ export class Editor {
|
|
|
23
23
|
engine;
|
|
24
24
|
composing = false;
|
|
25
25
|
reading = false;
|
|
26
|
+
userReadOnly = false;
|
|
27
|
+
isDisabled = false;
|
|
28
|
+
ownTabindex = false;
|
|
29
|
+
savedTabindex = null;
|
|
26
30
|
loadRemoteImages;
|
|
27
31
|
unhandled;
|
|
28
32
|
dragSource = null;
|
|
@@ -42,15 +46,19 @@ export class Editor {
|
|
|
42
46
|
});
|
|
43
47
|
cleanups = [];
|
|
44
48
|
changeListeners = new Set();
|
|
49
|
+
replacing = false;
|
|
50
|
+
resyncs = new Set();
|
|
45
51
|
viewHost;
|
|
46
52
|
constructor(options) {
|
|
47
53
|
this.element = options.element;
|
|
48
54
|
this.loadRemoteImages = options.loadRemoteImages ?? false;
|
|
49
55
|
this.unhandled = options.onUnhandledInput;
|
|
50
56
|
this.engine = createEngine({
|
|
51
|
-
readOnly: options.readOnly ?? false,
|
|
57
|
+
readOnly: (options.readOnly ?? false) || (options.disabled ?? false),
|
|
52
58
|
beforeAccess: () => this.readSelection(),
|
|
53
59
|
focus: () => this.element.focus(),
|
|
60
|
+
...(options.content ? { content: options.content } : {}),
|
|
61
|
+
...(options.onWarning ? { onWarning: options.onWarning } : {}),
|
|
54
62
|
...(options.maxLength !== undefined ? { maxLength: options.maxLength } : {}),
|
|
55
63
|
...(options.onMaxLength ? { onMaxLength: options.onMaxLength } : {}),
|
|
56
64
|
});
|
|
@@ -63,10 +71,18 @@ export class Editor {
|
|
|
63
71
|
domFromPos: (pos) => domFromPos(this.element, pos),
|
|
64
72
|
};
|
|
65
73
|
this.engine.pasteHandlers.push(...(options.pasteHandlers ?? []));
|
|
66
|
-
for (const plugin of options.plugins ?? [])
|
|
67
|
-
|
|
74
|
+
for (const plugin of options.plugins ?? []) {
|
|
75
|
+
try {
|
|
76
|
+
this.use(plugin);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
this.engine.report({ code: "plugin-rejected", plugin: plugin.id, message: `${plugin.id} was not added: ${error instanceof Error ? error.message : String(error)}`, error });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
68
82
|
this.engine.setDocument(importHtml(options.html ?? "", this.engine.importOptions));
|
|
69
|
-
this.
|
|
83
|
+
this.userReadOnly = options.readOnly ?? false;
|
|
84
|
+
this.isDisabled = options.disabled ?? false;
|
|
85
|
+
this.applyAccess();
|
|
70
86
|
this.element.setAttribute("role", "textbox");
|
|
71
87
|
this.element.setAttribute("aria-multiline", "true");
|
|
72
88
|
this.element.style.whiteSpace = "pre-wrap";
|
|
@@ -110,6 +126,15 @@ export class Editor {
|
|
|
110
126
|
this.listen(this.element, "compositionstart", () => { this.composing = true; });
|
|
111
127
|
this.listen(this.element, "compositionend", () => { this.composing = false; this.reconcileFromDom(); });
|
|
112
128
|
this.listen(document, "selectionchange", () => this.readSelection());
|
|
129
|
+
this.listen(this.element, "pointerup", () => this.readSelection());
|
|
130
|
+
this.listen(this.element, "mouseup", (e) => {
|
|
131
|
+
if (e.detail >= 3) {
|
|
132
|
+
this.readSelection();
|
|
133
|
+
const trimmed = endAtBlockEnd(this.engine.document, this.engine.selection);
|
|
134
|
+
if (trimmed !== this.engine.selection)
|
|
135
|
+
this.engine.setSelection(trimmed);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
113
138
|
this.observer.observe(this.element, { subtree: true, childList: true, characterData: true, attributes: true });
|
|
114
139
|
if (options.autofocus)
|
|
115
140
|
this.focus();
|
|
@@ -130,6 +155,7 @@ export class Editor {
|
|
|
130
155
|
get canUndo() { return this.engine.canUndo; }
|
|
131
156
|
get canRedo() { return this.engine.canRedo; }
|
|
132
157
|
get readOnly() { return this.engine.readOnly; }
|
|
158
|
+
get disabled() { return this.isDisabled; }
|
|
133
159
|
get commands() { return this.engine.commands; }
|
|
134
160
|
get ui() { return this.engine.ui; }
|
|
135
161
|
get keymap() { return this.engine.keymap; }
|
|
@@ -138,9 +164,9 @@ export class Editor {
|
|
|
138
164
|
select = (selector, onChange, equals) => this.engine.select(selector, onChange, equals);
|
|
139
165
|
use = (plugin) => {
|
|
140
166
|
const remove = this.engine.use(plugin);
|
|
141
|
-
const stop = plugin.view?.(this.viewHost);
|
|
167
|
+
const stop = this.engine.attempt(plugin.id, "its page part", () => plugin.view?.(this.viewHost), () => undefined);
|
|
142
168
|
return () => { if (stop)
|
|
143
|
-
stop
|
|
169
|
+
this.engine.attempt(plugin.id, "taking its page part away", stop, undefined); remove(); };
|
|
144
170
|
};
|
|
145
171
|
getHtml(separator = "") { return exportHtml(this.engine.document, separator); }
|
|
146
172
|
remoteImages() {
|
|
@@ -161,19 +187,98 @@ export class Editor {
|
|
|
161
187
|
setPlaceholder(text) {
|
|
162
188
|
if (text === "") {
|
|
163
189
|
this.element.removeAttribute("aria-placeholder");
|
|
164
|
-
this.element.style.removeProperty("--
|
|
190
|
+
this.element.style.removeProperty("--rte-placeholder");
|
|
165
191
|
}
|
|
166
192
|
else {
|
|
167
193
|
this.element.setAttribute("aria-placeholder", text);
|
|
168
|
-
this.element.style.setProperty("--
|
|
194
|
+
this.element.style.setProperty("--rte-placeholder", `"${text.replace(/[\\"]/g, "\\$&").replace(/\n/g, "\\A ")}"`);
|
|
169
195
|
}
|
|
170
196
|
}
|
|
171
197
|
setHtml(html) {
|
|
172
|
-
this.
|
|
198
|
+
this.replacing = true;
|
|
199
|
+
try {
|
|
200
|
+
this.engine.setDocument(importHtml(html, this.engine.importOptions));
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
this.replacing = false;
|
|
204
|
+
}
|
|
205
|
+
for (const resync of this.resyncs)
|
|
206
|
+
resync();
|
|
207
|
+
}
|
|
208
|
+
on(event, listener) {
|
|
209
|
+
const given = listener;
|
|
210
|
+
const call = (...args) => this.engine.attempt(undefined, `your ${event} listener`, () => given(...args), undefined, "listener-failed");
|
|
211
|
+
let last = event === "change" ? this.getHtml() : "";
|
|
212
|
+
const resync = () => { if (event === "change")
|
|
213
|
+
last = this.getHtml(); };
|
|
214
|
+
const handler = (change) => {
|
|
215
|
+
if (this.replacing)
|
|
216
|
+
return;
|
|
217
|
+
if (event === "change") {
|
|
218
|
+
if (!change.docChanged)
|
|
219
|
+
return;
|
|
220
|
+
const html = this.getHtml();
|
|
221
|
+
if (html === last)
|
|
222
|
+
return;
|
|
223
|
+
last = html;
|
|
224
|
+
call(html);
|
|
225
|
+
}
|
|
226
|
+
else if (event === "selection") {
|
|
227
|
+
if (change.selectionChanged)
|
|
228
|
+
call(this.selection);
|
|
229
|
+
}
|
|
230
|
+
else if (change.focusMoved && this.engine.snapshot.focused === (event === "focus"))
|
|
231
|
+
call(this);
|
|
232
|
+
};
|
|
233
|
+
this.resyncs.add(resync);
|
|
234
|
+
this.changeListeners.add(handler);
|
|
235
|
+
return () => { this.resyncs.delete(resync); this.changeListeners.delete(handler); };
|
|
173
236
|
}
|
|
174
237
|
setReadOnly(value) {
|
|
175
|
-
this.
|
|
176
|
-
this.
|
|
238
|
+
this.userReadOnly = value;
|
|
239
|
+
this.applyAccess();
|
|
240
|
+
}
|
|
241
|
+
setDisabled(value) {
|
|
242
|
+
this.isDisabled = value;
|
|
243
|
+
this.applyAccess();
|
|
244
|
+
}
|
|
245
|
+
applyAccess() {
|
|
246
|
+
const el = this.element;
|
|
247
|
+
const off = this.userReadOnly || this.isDisabled;
|
|
248
|
+
this.engine.setReadOnly(off);
|
|
249
|
+
el.setAttribute("contenteditable", off ? "false" : "true");
|
|
250
|
+
if (this.userReadOnly && !this.isDisabled)
|
|
251
|
+
el.setAttribute("aria-readonly", "true");
|
|
252
|
+
else
|
|
253
|
+
el.removeAttribute("aria-readonly");
|
|
254
|
+
if (this.isDisabled)
|
|
255
|
+
el.setAttribute("aria-disabled", "true");
|
|
256
|
+
else
|
|
257
|
+
el.removeAttribute("aria-disabled");
|
|
258
|
+
if (off && !this.isDisabled) {
|
|
259
|
+
if (!el.hasAttribute("tabindex")) {
|
|
260
|
+
el.setAttribute("tabindex", "0");
|
|
261
|
+
this.ownTabindex = true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if (this.ownTabindex) {
|
|
265
|
+
el.removeAttribute("tabindex");
|
|
266
|
+
this.ownTabindex = false;
|
|
267
|
+
}
|
|
268
|
+
if (this.isDisabled) {
|
|
269
|
+
if (el.hasAttribute("tabindex")) {
|
|
270
|
+
this.savedTabindex = el.getAttribute("tabindex");
|
|
271
|
+
el.removeAttribute("tabindex");
|
|
272
|
+
}
|
|
273
|
+
if (el.contains(el.ownerDocument.activeElement))
|
|
274
|
+
el.ownerDocument.activeElement?.blur();
|
|
275
|
+
}
|
|
276
|
+
else if (this.savedTabindex !== null) {
|
|
277
|
+
el.setAttribute("tabindex", this.savedTabindex);
|
|
278
|
+
this.savedTabindex = null;
|
|
279
|
+
}
|
|
280
|
+
for (const listener of [...this.changeListeners])
|
|
281
|
+
listener({ docChanged: false, selectionChanged: false, focusMoved: false });
|
|
177
282
|
}
|
|
178
283
|
onChange(listener) {
|
|
179
284
|
return this.engine.subscribe(() => listener(this));
|
|
@@ -250,13 +355,14 @@ export class Editor {
|
|
|
250
355
|
draw() {
|
|
251
356
|
renderDoc(this.engine.document, this.element, { loadRemoteImages: this.loadRemoteImages }, this.renderCache);
|
|
252
357
|
const blocks = this.engine.document.blocks;
|
|
253
|
-
this.element.toggleAttribute("data-
|
|
358
|
+
this.element.toggleAttribute("data-rte-empty", blocks.length === 1 && blocks[0]?.type === "p" && blocks[0].content.length === 0);
|
|
254
359
|
this.observer.takeRecords();
|
|
255
360
|
this.writeSelection();
|
|
256
361
|
}
|
|
257
362
|
setSelection(sel) {
|
|
258
363
|
this.engine.setSelection({ anchor: clampPos(this.engine.document, sel.anchor), focus: clampPos(this.engine.document, sel.focus) });
|
|
259
364
|
}
|
|
365
|
+
fellBack = false;
|
|
260
366
|
readSelection(force = false) {
|
|
261
367
|
if (!force && !this.element.contains(document.activeElement))
|
|
262
368
|
return;
|
|
@@ -267,6 +373,7 @@ export class Editor {
|
|
|
267
373
|
return;
|
|
268
374
|
const first = domSel.rangeCount > 1 ? domSel.getRangeAt(0) : undefined;
|
|
269
375
|
const last = first ? domSel.getRangeAt(domSel.rangeCount - 1) : undefined;
|
|
376
|
+
this.fellBack = false;
|
|
270
377
|
const anchor = first ? this.posOf(first.startContainer, first.startOffset) : this.posOf(domSel.anchorNode, domSel.anchorOffset);
|
|
271
378
|
const focus = last ? this.posOf(last.endContainer, last.endOffset) : this.posOf(domSel.focusNode, domSel.focusOffset);
|
|
272
379
|
if (!anchor || !focus)
|
|
@@ -279,17 +386,33 @@ export class Editor {
|
|
|
279
386
|
finally {
|
|
280
387
|
this.reading = false;
|
|
281
388
|
}
|
|
389
|
+
if (this.fellBack && isCollapsed(this.engine.selection))
|
|
390
|
+
this.writeSelection();
|
|
282
391
|
}
|
|
283
392
|
posOf(node, offset) {
|
|
284
393
|
const direct = node !== this.element ? posFromDom(this.element, node, offset) : null;
|
|
285
394
|
if (direct || !(node instanceof Element))
|
|
286
395
|
return direct;
|
|
287
396
|
const containers = (el) => (el.hasAttribute("data-rte") ? [el, ...el.querySelectorAll("[data-rte]")] : [...el.querySelectorAll("[data-rte]")]);
|
|
288
|
-
|
|
289
|
-
|
|
397
|
+
this.fellBack = true;
|
|
398
|
+
let first;
|
|
399
|
+
for (let i = offset; i < node.children.length && !first; i++)
|
|
400
|
+
first = containers(node.children[i])[0];
|
|
290
401
|
if (first)
|
|
291
402
|
return posFromDom(this.element, first, 0);
|
|
292
403
|
const all = containers(node);
|
|
404
|
+
if (all.length === 0 && node !== this.element) {
|
|
405
|
+
for (let from = node; from && from !== this.element; from = from.parentElement) {
|
|
406
|
+
for (let next = from.nextElementSibling; next; next = next.nextElementSibling) {
|
|
407
|
+
const there = containers(next)[0];
|
|
408
|
+
if (there)
|
|
409
|
+
return posFromDom(this.element, there, 0);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const every = [...this.element.querySelectorAll("[data-rte]")];
|
|
413
|
+
const end = every[every.length - 1];
|
|
414
|
+
return end ? posFromDom(this.element, end, end.childNodes.length) : null;
|
|
415
|
+
}
|
|
293
416
|
const last = all[all.length - 1];
|
|
294
417
|
return last ? posFromDom(this.element, last, last.childNodes.length) : null;
|
|
295
418
|
}
|
|
@@ -320,7 +443,7 @@ export class Editor {
|
|
|
320
443
|
return;
|
|
321
444
|
const domSel = document.getSelection();
|
|
322
445
|
const raw = domSel?.focusNode && this.element.contains(domSel.focusNode) ? posFromDom(this.element, domSel.focusNode, domSel.focusOffset) : null;
|
|
323
|
-
this.apply("
|
|
446
|
+
this.apply("compose", (doc) => {
|
|
324
447
|
const next = setContent(doc, path, first.content);
|
|
325
448
|
return { doc: next, selection: raw ? caret(clampPos(next, raw)) : caret(clampPos(next, this.engine.selection.focus)) };
|
|
326
449
|
});
|
|
@@ -500,12 +623,7 @@ export class Editor {
|
|
|
500
623
|
const formats = { "text/html": exportHtml({ blocks }), "text/plain": selectedText(this.engine.document, this.engine.selection) };
|
|
501
624
|
for (const formatter of this.engine.copyFormatters) {
|
|
502
625
|
let added;
|
|
503
|
-
|
|
504
|
-
added = formatter(blocks);
|
|
505
|
-
}
|
|
506
|
-
catch (error) {
|
|
507
|
-
this.engine.warn(`A copy formatter failed: ${String(error)}`);
|
|
508
|
-
}
|
|
626
|
+
added = formatter(blocks);
|
|
509
627
|
for (const [type, value] of Object.entries(added ?? {}))
|
|
510
628
|
if (/^[a-z]+\/[a-z0-9.+-]+$/i.test(type) && typeof value === "string")
|
|
511
629
|
formats[type] = value;
|
|
@@ -590,7 +708,28 @@ export class Editor {
|
|
|
590
708
|
onKeyDown(event) {
|
|
591
709
|
if (this.composing || event.isComposing)
|
|
592
710
|
return;
|
|
593
|
-
if (this.engine.handleKey(event))
|
|
711
|
+
if (this.engine.handleKey(event)) {
|
|
594
712
|
event.preventDefault();
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
this.keyAtDocumentEdge(event);
|
|
716
|
+
}
|
|
717
|
+
keyAtDocumentEdge(event) {
|
|
718
|
+
if ((event.key !== "Backspace" && event.key !== "Delete") || event.ctrlKey || event.metaKey || event.altKey || event.shiftKey || this.engine.readOnly)
|
|
719
|
+
return;
|
|
720
|
+
this.engine.refresh();
|
|
721
|
+
const sel = this.engine.selection;
|
|
722
|
+
if (!isCollapsed(sel))
|
|
723
|
+
return;
|
|
724
|
+
const paths = containerPaths(this.engine.document);
|
|
725
|
+
const edge = paths[event.key === "Backspace" ? 0 : paths.length - 1];
|
|
726
|
+
const here = sel.focus;
|
|
727
|
+
if (!edge || pathKey(here.path) !== pathKey(edge) || here.offset !== (event.key === "Backspace" ? 0 : inlineLength(getContent(this.engine.document, edge))))
|
|
728
|
+
return;
|
|
729
|
+
event.preventDefault();
|
|
730
|
+
if (event.key === "Backspace")
|
|
731
|
+
this.apply("delete", (doc, s) => deleteBackward(doc, s, this.engine.behaviors));
|
|
732
|
+
else
|
|
733
|
+
this.apply("delete", deleteForward);
|
|
595
734
|
}
|
|
596
735
|
}
|
package/dist/edit.d.ts
CHANGED
|
@@ -20,6 +20,12 @@ export declare function insertLineBreak(doc: Doc, sel: Selection): EditResult;
|
|
|
20
20
|
export declare function splitBlock(doc: Doc, sel: Selection, behaviors?: Behaviors): EditResult;
|
|
21
21
|
export declare function deleteBackward(doc: Doc, sel: Selection, behaviors?: Behaviors): EditResult;
|
|
22
22
|
export declare function deleteForward(doc: Doc, sel: Selection): EditResult;
|
|
23
|
+
/**
|
|
24
|
+
* A selection of whole blocks that a browser made by running its end on to the start of the next block (a triple click in Chrome and
|
|
25
|
+
* Safari does this) ends at the end of the last selected block instead, so what is typed or deleted never reaches into the next one.
|
|
26
|
+
* Anything else is returned as it was.
|
|
27
|
+
*/
|
|
28
|
+
export declare function endAtBlockEnd(doc: Doc, sel: Selection): Selection;
|
|
23
29
|
/** Deletes back to the start of the previous word (or forward to the end of the next). At a block edge it joins blocks like Backspace. */
|
|
24
30
|
export declare function deleteWord(doc: Doc, sel: Selection, direction: "backward" | "forward", behaviors?: Behaviors): EditResult;
|
|
25
31
|
/** Deletes from the caret to the start or end of the block. */
|