@salmonui/rich-text 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/blocks/find.d.ts +4 -0
- package/dist/blocks/find.d.ts.map +1 -0
- package/dist/blocks/find.js +14 -0
- package/dist/blocks/find.js.map +1 -0
- package/dist/blocks/from-html.d.ts +10 -0
- package/dist/blocks/from-html.d.ts.map +1 -0
- package/dist/blocks/from-html.js +459 -0
- package/dist/blocks/from-html.js.map +1 -0
- package/dist/blocks/index.d.ts +5 -0
- package/dist/blocks/index.d.ts.map +1 -0
- package/dist/blocks/index.js +4 -0
- package/dist/blocks/index.js.map +1 -0
- package/dist/blocks/ops.d.ts +27 -0
- package/dist/blocks/ops.d.ts.map +1 -0
- package/dist/blocks/ops.js +172 -0
- package/dist/blocks/ops.js.map +1 -0
- package/dist/blocks/to-html.d.ts +18 -0
- package/dist/blocks/to-html.d.ts.map +1 -0
- package/dist/blocks/to-html.js +207 -0
- package/dist/blocks/to-html.js.map +1 -0
- package/dist/blocks/types.d.ts +76 -0
- package/dist/blocks/types.d.ts.map +1 -0
- package/dist/blocks/types.js +4 -0
- package/dist/blocks/types.js.map +1 -0
- package/dist/collab.d.ts +43 -0
- package/dist/collab.d.ts.map +1 -0
- package/dist/collab.js +45 -0
- package/dist/collab.js.map +1 -0
- package/dist/content/RichTextContent.d.ts +19 -0
- package/dist/content/RichTextContent.d.ts.map +1 -0
- package/dist/content/RichTextContent.js +20 -0
- package/dist/content/RichTextContent.js.map +1 -0
- package/dist/content/index.d.ts +2 -0
- package/dist/content/index.d.ts.map +1 -0
- package/dist/content/index.js +3 -0
- package/dist/content/index.js.map +1 -0
- package/dist/engine/commands.d.ts +19 -0
- package/dist/engine/commands.d.ts.map +1 -0
- package/dist/engine/commands.js +457 -0
- package/dist/engine/commands.js.map +1 -0
- package/dist/engine/core.d.ts +116 -0
- package/dist/engine/core.d.ts.map +1 -0
- package/dist/engine/core.js +460 -0
- package/dist/engine/core.js.map +1 -0
- package/dist/engine/dom-shim.d.ts +47 -0
- package/dist/engine/dom-shim.d.ts.map +1 -0
- package/dist/engine/dom-shim.js +128 -0
- package/dist/engine/dom-shim.js.map +1 -0
- package/dist/engine/html-out.d.ts +12 -0
- package/dist/engine/html-out.d.ts.map +1 -0
- package/dist/engine/html-out.js +24 -0
- package/dist/engine/html-out.js.map +1 -0
- package/dist/engine/index.d.ts +6 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +5 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/keymap.d.ts +18 -0
- package/dist/engine/keymap.d.ts.map +1 -0
- package/dist/engine/keymap.js +85 -0
- package/dist/engine/keymap.js.map +1 -0
- package/dist/engine/load.d.ts +10 -0
- package/dist/engine/load.d.ts.map +1 -0
- package/dist/engine/load.js +11 -0
- package/dist/engine/load.js.map +1 -0
- package/dist/engine/nodeviews.d.ts +19 -0
- package/dist/engine/nodeviews.d.ts.map +1 -0
- package/dist/engine/nodeviews.js +241 -0
- package/dist/engine/nodeviews.js.map +1 -0
- package/dist/engine/plugins.d.ts +77 -0
- package/dist/engine/plugins.d.ts.map +1 -0
- package/dist/engine/plugins.js +312 -0
- package/dist/engine/plugins.js.map +1 -0
- package/dist/engine/schema.d.ts +11 -0
- package/dist/engine/schema.d.ts.map +1 -0
- package/dist/engine/schema.js +350 -0
- package/dist/engine/schema.js.map +1 -0
- package/dist/engine/serialize.d.ts +13 -0
- package/dist/engine/serialize.d.ts.map +1 -0
- package/dist/engine/serialize.js +119 -0
- package/dist/engine/serialize.js.map +1 -0
- package/dist/engine/snapshot.d.ts +4 -0
- package/dist/engine/snapshot.d.ts.map +1 -0
- package/dist/engine/snapshot.js +62 -0
- package/dist/engine/snapshot.js.map +1 -0
- package/dist/engine/types.d.ts +215 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/html/entities.d.ts +7 -0
- package/dist/html/entities.d.ts.map +1 -0
- package/dist/html/entities.js +69 -0
- package/dist/html/entities.js.map +1 -0
- package/dist/html/index.d.ts +7 -0
- package/dist/html/index.d.ts.map +1 -0
- package/dist/html/index.js +9 -0
- package/dist/html/index.js.map +1 -0
- package/dist/html/parse.d.ts +30 -0
- package/dist/html/parse.d.ts.map +1 -0
- package/dist/html/parse.js +394 -0
- package/dist/html/parse.js.map +1 -0
- package/dist/html/paste.d.ts +47 -0
- package/dist/html/paste.d.ts.map +1 -0
- package/dist/html/paste.js +373 -0
- package/dist/html/paste.js.map +1 -0
- package/dist/html/policy.d.ts +56 -0
- package/dist/html/policy.d.ts.map +1 -0
- package/dist/html/policy.js +245 -0
- package/dist/html/policy.js.map +1 -0
- package/dist/html/sanitize.d.ts +60 -0
- package/dist/html/sanitize.d.ts.map +1 -0
- package/dist/html/sanitize.js +699 -0
- package/dist/html/sanitize.js.map +1 -0
- package/dist/html/serialize.d.ts +16 -0
- package/dist/html/serialize.d.ts.map +1 -0
- package/dist/html/serialize.js +68 -0
- package/dist/html/serialize.js.map +1 -0
- package/dist/html/text.d.ts +18 -0
- package/dist/html/text.d.ts.map +1 -0
- package/dist/html/text.js +96 -0
- package/dist/html/text.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/mention/editable.d.ts +15 -0
- package/dist/mention/editable.d.ts.map +1 -0
- package/dist/mention/editable.js +167 -0
- package/dist/mention/editable.js.map +1 -0
- package/dist/mention/index.d.ts +2 -0
- package/dist/mention/index.d.ts.map +1 -0
- package/dist/mention/index.js +3 -0
- package/dist/mention/index.js.map +1 -0
- package/dist/react/RichTextEditor.d.ts +15 -0
- package/dist/react/RichTextEditor.d.ts.map +1 -0
- package/dist/react/RichTextEditor.js +497 -0
- package/dist/react/RichTextEditor.js.map +1 -0
- package/dist/react/block-editor/BlockEditor.d.ts +5 -0
- package/dist/react/block-editor/BlockEditor.d.ts.map +1 -0
- package/dist/react/block-editor/BlockEditor.js +106 -0
- package/dist/react/block-editor/BlockEditor.js.map +1 -0
- package/dist/react/block-editor/controller.d.ts +94 -0
- package/dist/react/block-editor/controller.d.ts.map +1 -0
- package/dist/react/block-editor/controller.js +758 -0
- package/dist/react/block-editor/controller.js.map +1 -0
- package/dist/react/block-editor/index.d.ts +4 -0
- package/dist/react/block-editor/index.d.ts.map +1 -0
- package/dist/react/block-editor/index.js +5 -0
- package/dist/react/block-editor/index.js.map +1 -0
- package/dist/react/block-editor/locale.d.ts +73 -0
- package/dist/react/block-editor/locale.d.ts.map +1 -0
- package/dist/react/block-editor/locale.js +65 -0
- package/dist/react/block-editor/locale.js.map +1 -0
- package/dist/react/block-editor/transforms.d.ts +26 -0
- package/dist/react/block-editor/transforms.d.ts.map +1 -0
- package/dist/react/block-editor/transforms.js +233 -0
- package/dist/react/block-editor/transforms.js.map +1 -0
- package/dist/react/block-editor/types.d.ts +180 -0
- package/dist/react/block-editor/types.d.ts.map +1 -0
- package/dist/react/block-editor/types.js +2 -0
- package/dist/react/block-editor/types.js.map +1 -0
- package/dist/react/block-editor/ui.d.ts +21 -0
- package/dist/react/block-editor/ui.d.ts.map +1 -0
- package/dist/react/block-editor/ui.js +601 -0
- package/dist/react/block-editor/ui.js.map +1 -0
- package/dist/react/client.d.ts +28 -0
- package/dist/react/client.d.ts.map +1 -0
- package/dist/react/client.js +111 -0
- package/dist/react/client.js.map +1 -0
- package/dist/react/dialogs.d.ts +46 -0
- package/dist/react/dialogs.d.ts.map +1 -0
- package/dist/react/dialogs.js +312 -0
- package/dist/react/dialogs.js.map +1 -0
- package/dist/react/emoji-data.d.ts +3 -0
- package/dist/react/emoji-data.d.ts.map +1 -0
- package/dist/react/emoji-data.js +6 -0
- package/dist/react/emoji-data.js.map +1 -0
- package/dist/react/icons.d.ts +5 -0
- package/dist/react/icons.d.ts.map +1 -0
- package/dist/react/icons.js +69 -0
- package/dist/react/icons.js.map +1 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +5 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/items.d.ts +60 -0
- package/dist/react/items.d.ts.map +1 -0
- package/dist/react/items.js +255 -0
- package/dist/react/items.js.map +1 -0
- package/dist/react/locale.d.ts +151 -0
- package/dist/react/locale.d.ts.map +1 -0
- package/dist/react/locale.js +139 -0
- package/dist/react/locale.js.map +1 -0
- package/dist/react/overlay.d.ts +41 -0
- package/dist/react/overlay.d.ts.map +1 -0
- package/dist/react/overlay.js +162 -0
- package/dist/react/overlay.js.map +1 -0
- package/dist/react/store.d.ts +23 -0
- package/dist/react/store.d.ts.map +1 -0
- package/dist/react/store.js +84 -0
- package/dist/react/store.js.map +1 -0
- package/dist/react/types.d.ts +205 -0
- package/dist/react/types.d.ts.map +1 -0
- package/dist/react/types.js +2 -0
- package/dist/react/types.js.map +1 -0
- package/dist/rich-text.css +1555 -0
- package/package.json +61 -4
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
// BlockEditor client controller (lazy chunk, loaded with dynamic import()
|
|
2
|
+
// after hydration like the engine it wraps). It owns the RichTextEngine over
|
|
3
|
+
// the server-rendered content element and keeps the block model in sync:
|
|
4
|
+
// blocks -> blocksToHtml -> engine, engine HTML -> htmlToBlocks -> blocks
|
|
5
|
+
// (the single conversion path). Block ids live in the schema's
|
|
6
|
+
// `blockId` attributes; a plugin keeps them present and unique.
|
|
7
|
+
import { NodeSelection, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
8
|
+
import { createEngine, serializeDoc, toolbarState, DEFAULT_KEY_BINDINGS } from '../../engine/load.js';
|
|
9
|
+
import { blocksToHtml } from '../../blocks/to-html.js';
|
|
10
|
+
import { htmlToBlocks } from '../../blocks/from-html.js';
|
|
11
|
+
import { findBlock, insertBlocks, moveBlock as moveIn, updateBlock as updateIn } from '../../blocks/ops.js';
|
|
12
|
+
import { cleanPastedHtml } from '../../html/paste.js';
|
|
13
|
+
import { diffBlocks, duplicate, locate, remove, stepPosition, turnInto } from './transforms.js';
|
|
14
|
+
/** Nodes that are blocks of the model (list containers and table parts are not). */
|
|
15
|
+
const BLOCK_NODES = new Set([
|
|
16
|
+
'paragraph', 'heading', 'blockquote', 'code_block', 'horizontal_rule', 'list_item', 'task_item', 'figure', 'table', 'callout', 'toggle', 'audio', 'video', 'embed',
|
|
17
|
+
]);
|
|
18
|
+
/** Containers whose first paragraph is the block's own text, not a child block. */
|
|
19
|
+
const OWN_TEXT = new Set(['list_item', 'task_item', 'blockquote', 'callout']);
|
|
20
|
+
export const INITIAL_SNAPSHOT = Object.freeze({
|
|
21
|
+
ready: false,
|
|
22
|
+
focused: false,
|
|
23
|
+
selectionEmpty: true,
|
|
24
|
+
textSelection: false,
|
|
25
|
+
marks: Object.freeze({ bold: false, italic: false, underline: false, strike: false, subscript: false, superscript: false, code: false }),
|
|
26
|
+
link: null,
|
|
27
|
+
inCode: false,
|
|
28
|
+
blockId: null,
|
|
29
|
+
suggest: null,
|
|
30
|
+
empty: false,
|
|
31
|
+
});
|
|
32
|
+
function randomPrefix() {
|
|
33
|
+
const bytes = new Uint8Array(4);
|
|
34
|
+
globalThis.crypto?.getRandomValues?.(bytes);
|
|
35
|
+
return 'k' + [...bytes].map((b) => b.toString(36)).join('').slice(0, 6);
|
|
36
|
+
}
|
|
37
|
+
/** Ids and inline-image normalization (an image inside text becomes its own image block). */
|
|
38
|
+
function blockPlugin(nextId, suggestKey, suggestActive) {
|
|
39
|
+
return new Plugin({
|
|
40
|
+
key: new PluginKey('salmonBlockIds'),
|
|
41
|
+
props: {
|
|
42
|
+
// Slash menu keys before the editing keymap (Enter would split the block).
|
|
43
|
+
handleKeyDown: (_view, event) => suggestActive() && suggestKey(event),
|
|
44
|
+
},
|
|
45
|
+
// Table node views render their own <table>: put the block id on it (an ignored mutation).
|
|
46
|
+
view(view) {
|
|
47
|
+
const sync = () => {
|
|
48
|
+
view.state.doc.forEach((node, offset) => {
|
|
49
|
+
if (node.type.name !== 'table')
|
|
50
|
+
return;
|
|
51
|
+
const dom = view.nodeDOM(offset);
|
|
52
|
+
const table = dom instanceof HTMLElement ? (dom.tagName === 'TABLE' ? dom : dom.querySelector('table')) : null;
|
|
53
|
+
const id = node.attrs['blockId'];
|
|
54
|
+
if (table && id && table.getAttribute('data-block-id') !== id)
|
|
55
|
+
table.setAttribute('data-block-id', id);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
sync();
|
|
59
|
+
return { update: sync };
|
|
60
|
+
},
|
|
61
|
+
appendTransaction(transactions, _old, state) {
|
|
62
|
+
if (!transactions.some((tr) => tr.docChanged))
|
|
63
|
+
return null;
|
|
64
|
+
return normalize(state, nextId);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function normalize(state, nextId) {
|
|
69
|
+
const { schema } = state;
|
|
70
|
+
const figure = schema.nodes['figure'];
|
|
71
|
+
// 1. Inline images (upload, paste) become figure blocks after their paragraph.
|
|
72
|
+
let tr = null;
|
|
73
|
+
const images = [];
|
|
74
|
+
state.doc.descendants((node, pos, parent) => {
|
|
75
|
+
if (node.type.name === 'table')
|
|
76
|
+
return false;
|
|
77
|
+
if (node.type.name === 'image' && parent && parent.type.name === 'paragraph') {
|
|
78
|
+
const $pos = state.doc.resolve(pos);
|
|
79
|
+
images.push({ pos, node, para: $pos.before(), paraNode: parent });
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
});
|
|
83
|
+
if (images.length && figure) {
|
|
84
|
+
tr = state.tr;
|
|
85
|
+
for (let i = images.length - 1; i >= 0; i--) {
|
|
86
|
+
const { pos, node, para, paraNode } = images[i];
|
|
87
|
+
const fig = figure.create({ src: node.attrs['src'], alt: node.attrs['alt'] ?? '', width: node.attrs['width'], height: node.attrs['height'], blockId: null });
|
|
88
|
+
const onlyChild = paraNode.childCount === 1;
|
|
89
|
+
if (onlyChild)
|
|
90
|
+
tr.replaceWith(para, para + paraNode.nodeSize, fig);
|
|
91
|
+
else {
|
|
92
|
+
tr.delete(pos, pos + node.nodeSize);
|
|
93
|
+
const after = tr.mapping.map(para + paraNode.nodeSize);
|
|
94
|
+
tr.insert(after, fig);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return tr.setMeta('addToHistory', true);
|
|
98
|
+
}
|
|
99
|
+
// 2. A container's own-text paragraph carries no id; a container wrapped around
|
|
100
|
+
// a paragraph (Markdown rule, turn into) inherits the paragraph's id.
|
|
101
|
+
const moves = [];
|
|
102
|
+
state.doc.descendants((node, pos, parent, index) => {
|
|
103
|
+
if (parent && OWN_TEXT.has(parent.type.name) && index === 0 && node.type.name === 'paragraph' && node.attrs['blockId']) {
|
|
104
|
+
moves.push({ pos, node, parentPos: state.doc.resolve(pos).before(), parent });
|
|
105
|
+
}
|
|
106
|
+
return !node.isTextblock;
|
|
107
|
+
});
|
|
108
|
+
if (moves.length) {
|
|
109
|
+
tr = state.tr;
|
|
110
|
+
for (const m of moves) {
|
|
111
|
+
tr.setNodeMarkup(m.pos, undefined, { ...m.node.attrs, blockId: null });
|
|
112
|
+
if (!m.parent.attrs['blockId'])
|
|
113
|
+
tr.setNodeMarkup(m.parentPos, undefined, { ...m.parent.attrs, blockId: m.node.attrs['blockId'] });
|
|
114
|
+
}
|
|
115
|
+
return tr.setMeta('addToHistory', false);
|
|
116
|
+
}
|
|
117
|
+
// 3. Every model block has a unique id.
|
|
118
|
+
const seen = new Set();
|
|
119
|
+
const fix = [];
|
|
120
|
+
state.doc.descendants((node, pos, parent, index) => {
|
|
121
|
+
const name = node.type.name;
|
|
122
|
+
if (name === 'table_row' || name === 'table_cell' || name === 'table_header')
|
|
123
|
+
return false;
|
|
124
|
+
if (parent && OWN_TEXT.has(parent.type.name) && index === 0 && name === 'paragraph')
|
|
125
|
+
return false;
|
|
126
|
+
if (parent && parent.type.name === 'table')
|
|
127
|
+
return false;
|
|
128
|
+
if (BLOCK_NODES.has(name)) {
|
|
129
|
+
const id = node.attrs['blockId'];
|
|
130
|
+
if (!id || seen.has(id))
|
|
131
|
+
fix.push({ pos, node });
|
|
132
|
+
else
|
|
133
|
+
seen.add(id);
|
|
134
|
+
return name !== 'table' && !node.isTextblock && !node.isAtom;
|
|
135
|
+
}
|
|
136
|
+
return !node.isTextblock;
|
|
137
|
+
});
|
|
138
|
+
if (!fix.length)
|
|
139
|
+
return null;
|
|
140
|
+
tr = state.tr;
|
|
141
|
+
for (const { pos, node } of fix) {
|
|
142
|
+
let id = nextId();
|
|
143
|
+
while (seen.has(id))
|
|
144
|
+
id = nextId();
|
|
145
|
+
seen.add(id);
|
|
146
|
+
tr.setNodeMarkup(pos, undefined, { ...node.attrs, blockId: id });
|
|
147
|
+
}
|
|
148
|
+
return tr.setMeta('addToHistory', false);
|
|
149
|
+
}
|
|
150
|
+
function plainChord(map, command) {
|
|
151
|
+
return Object.entries(map)
|
|
152
|
+
.filter(([, value]) => value === command)
|
|
153
|
+
.map(([key]) => key);
|
|
154
|
+
}
|
|
155
|
+
const BLOCK_ACTIONS = new Set([
|
|
156
|
+
'moveBlockUp', 'moveBlockDown', 'duplicateBlock', 'deleteBlock', 'blockMenu', 'contextMenu', 'print', 'link', 'image',
|
|
157
|
+
'paragraph', 'heading1', 'heading2', 'heading3', 'bulletList', 'numberedList', 'checklist', 'quote', 'callout', 'code', 'toggle', 'divider', 'table',
|
|
158
|
+
]);
|
|
159
|
+
const BLOCK_KEYS = {
|
|
160
|
+
'Mod-Shift-ArrowUp': 'moveBlockUp',
|
|
161
|
+
'Mod-Shift-ArrowDown': 'moveBlockDown',
|
|
162
|
+
'Mod-d': 'duplicateBlock',
|
|
163
|
+
'Mod-Shift-Backspace': 'deleteBlock',
|
|
164
|
+
'Alt-F10': 'blockMenu',
|
|
165
|
+
'Shift-F10': 'contextMenu',
|
|
166
|
+
'Mod-p': 'print',
|
|
167
|
+
};
|
|
168
|
+
/** Merges the BlockEditor defaults and the `keyBindings` prop (command -> chord) into engine bindings (chord -> command). */
|
|
169
|
+
export function engineKeyBindings(custom) {
|
|
170
|
+
const out = { ...BLOCK_KEYS };
|
|
171
|
+
if (!custom)
|
|
172
|
+
return out;
|
|
173
|
+
const defaults = { ...DEFAULT_KEY_BINDINGS, ...BLOCK_KEYS };
|
|
174
|
+
for (const [command, chord] of Object.entries(custom)) {
|
|
175
|
+
if (chord === undefined)
|
|
176
|
+
continue;
|
|
177
|
+
for (const key of plainChord(defaults, command))
|
|
178
|
+
out[key] = false;
|
|
179
|
+
if (chord !== false)
|
|
180
|
+
out[chord] = command;
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
export class BlockController {
|
|
185
|
+
engine;
|
|
186
|
+
element;
|
|
187
|
+
#hooks;
|
|
188
|
+
#blocks;
|
|
189
|
+
#blocksDoc;
|
|
190
|
+
#snapshot = INITIAL_SNAPSHOT;
|
|
191
|
+
#listeners = new Set();
|
|
192
|
+
#focused = false;
|
|
193
|
+
#lastSelection = null;
|
|
194
|
+
#counter = 0;
|
|
195
|
+
#prefix = randomPrefix();
|
|
196
|
+
#lastPaste = null;
|
|
197
|
+
#mention = null;
|
|
198
|
+
#unsubscribe;
|
|
199
|
+
#cleanups = [];
|
|
200
|
+
constructor(engine, element, blocks, hooks) {
|
|
201
|
+
this.engine = engine;
|
|
202
|
+
this.element = element;
|
|
203
|
+
this.#hooks = hooks;
|
|
204
|
+
this.#blocks = blocks;
|
|
205
|
+
this.#blocksDoc = engine.state.doc;
|
|
206
|
+
engine.mount(element);
|
|
207
|
+
// Ids for blocks that arrived without one (the initial state has no appendTransaction).
|
|
208
|
+
const fix = normalize(engine.state, () => this.nextId());
|
|
209
|
+
if (fix) {
|
|
210
|
+
engine.dispatch(fix.setMeta('addToHistory', false));
|
|
211
|
+
this.#blocks = this.#readBlocks();
|
|
212
|
+
this.#blocksDoc = engine.state.doc;
|
|
213
|
+
}
|
|
214
|
+
this.#unsubscribe = engine.subscribe((tr) => this.#onTransaction(tr));
|
|
215
|
+
const dom = engine.view.dom;
|
|
216
|
+
const owner = element.ownerDocument;
|
|
217
|
+
const root = element.parentElement ?? element;
|
|
218
|
+
// Focus inside the editor root or its own popups (menus may be portaled) counts as focused.
|
|
219
|
+
const inside = (node) => node instanceof Node && (root.contains(node) || (node instanceof Element && node.closest('.salmon-menu, .salmon-block-editor__menu-popover, .salmon-rich-text__dialog') !== null));
|
|
220
|
+
const onFocusIn = (event) => {
|
|
221
|
+
if (event.target === dom || dom.contains(event.target))
|
|
222
|
+
this.#setFocus(true);
|
|
223
|
+
else if (this.#focused && !inside(event.target))
|
|
224
|
+
this.#setFocus(false);
|
|
225
|
+
};
|
|
226
|
+
const onFocusOut = (event) => {
|
|
227
|
+
if (event.relatedTarget === null && this.#focused) {
|
|
228
|
+
// Focus went to the document (or another window); check once it settled.
|
|
229
|
+
queueMicrotask(() => {
|
|
230
|
+
if (!inside(owner.activeElement))
|
|
231
|
+
this.#setFocus(false);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
owner.addEventListener('focusin', onFocusIn, true);
|
|
236
|
+
owner.addEventListener('focusout', onFocusOut, true);
|
|
237
|
+
const onPaste = (event) => this.#onPaste(event);
|
|
238
|
+
root.addEventListener('paste', onPaste, true);
|
|
239
|
+
this.#cleanups.push(() => {
|
|
240
|
+
owner.removeEventListener('focusin', onFocusIn, true);
|
|
241
|
+
owner.removeEventListener('focusout', onFocusOut, true);
|
|
242
|
+
root.removeEventListener('paste', onPaste, true);
|
|
243
|
+
});
|
|
244
|
+
this.#publish();
|
|
245
|
+
}
|
|
246
|
+
nextId() {
|
|
247
|
+
this.#counter += 1;
|
|
248
|
+
return `${this.#prefix}${this.#counter.toString(36)}`;
|
|
249
|
+
}
|
|
250
|
+
// ---- store ---------------------------------------------------------------
|
|
251
|
+
getSnapshot = () => this.#snapshot;
|
|
252
|
+
subscribe = (listener) => {
|
|
253
|
+
this.#listeners.add(listener);
|
|
254
|
+
return () => this.#listeners.delete(listener);
|
|
255
|
+
};
|
|
256
|
+
#publish() {
|
|
257
|
+
const t = toolbarState(this.engine);
|
|
258
|
+
const sel = this.engine.state.selection;
|
|
259
|
+
const suggest = t.suggest && t.suggest.id === 'slash' ? { query: t.suggest.query, from: t.suggest.from } : null;
|
|
260
|
+
const next = {
|
|
261
|
+
ready: true,
|
|
262
|
+
focused: this.#focused,
|
|
263
|
+
selectionEmpty: sel.empty,
|
|
264
|
+
textSelection: sel instanceof TextSelection,
|
|
265
|
+
marks: t.marks,
|
|
266
|
+
link: t.link,
|
|
267
|
+
inCode: t.block === 'code',
|
|
268
|
+
blockId: this.currentBlockId(),
|
|
269
|
+
suggest,
|
|
270
|
+
empty: t.empty,
|
|
271
|
+
};
|
|
272
|
+
const prev = this.#snapshot;
|
|
273
|
+
const same = prev.ready === next.ready &&
|
|
274
|
+
prev.focused === next.focused &&
|
|
275
|
+
prev.selectionEmpty === next.selectionEmpty &&
|
|
276
|
+
prev.textSelection === next.textSelection &&
|
|
277
|
+
prev.link === next.link &&
|
|
278
|
+
prev.inCode === next.inCode &&
|
|
279
|
+
prev.blockId === next.blockId &&
|
|
280
|
+
prev.empty === next.empty &&
|
|
281
|
+
JSON.stringify(prev.marks) === JSON.stringify(next.marks) &&
|
|
282
|
+
JSON.stringify(prev.suggest) === JSON.stringify(next.suggest);
|
|
283
|
+
if (!same)
|
|
284
|
+
this.#snapshot = next;
|
|
285
|
+
// Listeners also reposition popups, so they run after every transaction.
|
|
286
|
+
for (const l of this.#listeners)
|
|
287
|
+
l();
|
|
288
|
+
}
|
|
289
|
+
#setFocus(focused) {
|
|
290
|
+
if (this.#focused === focused)
|
|
291
|
+
return;
|
|
292
|
+
this.#focused = focused;
|
|
293
|
+
const p = this.#hooks.props();
|
|
294
|
+
if (focused)
|
|
295
|
+
p.onFocus?.();
|
|
296
|
+
else
|
|
297
|
+
p.onBlur?.();
|
|
298
|
+
this.#publish();
|
|
299
|
+
}
|
|
300
|
+
// ---- model sync ----------------------------------------------------------
|
|
301
|
+
#readBlocks() {
|
|
302
|
+
return htmlToBlocks(serializeDoc(this.engine.state.doc));
|
|
303
|
+
}
|
|
304
|
+
/** The current document as blocks (cached per ProseMirror document). */
|
|
305
|
+
getBlocks() {
|
|
306
|
+
if (this.#blocksDoc !== this.engine.state.doc) {
|
|
307
|
+
this.#blocks = this.#readBlocks();
|
|
308
|
+
this.#blocksDoc = this.engine.state.doc;
|
|
309
|
+
}
|
|
310
|
+
return this.#blocks;
|
|
311
|
+
}
|
|
312
|
+
#onTransaction(tr) {
|
|
313
|
+
const p = this.#hooks.props();
|
|
314
|
+
if (tr && tr.docChanged) {
|
|
315
|
+
const external = tr.getMeta('salmonExternal') === true;
|
|
316
|
+
const prev = this.#blocks;
|
|
317
|
+
if (p.onChange || p.onBlockChange || p.onPaste) {
|
|
318
|
+
const next = this.getBlocks();
|
|
319
|
+
if (!external) {
|
|
320
|
+
if (p.onBlockChange)
|
|
321
|
+
for (const event of diffBlocks(prev, next))
|
|
322
|
+
p.onBlockChange(event);
|
|
323
|
+
p.onChange?.(next);
|
|
324
|
+
}
|
|
325
|
+
if (tr.getMeta('uiEvent') === 'paste' && this.#lastPaste) {
|
|
326
|
+
p.onPaste?.({ ...this.#lastPaste, blocks: next });
|
|
327
|
+
this.#lastPaste = null;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const sel = this.getSelection();
|
|
332
|
+
const key = sel ? `${sel.blockId}:${sel.offset}` : '';
|
|
333
|
+
if (key !== this.#lastSelection) {
|
|
334
|
+
this.#lastSelection = key;
|
|
335
|
+
p.onSelectionChange?.(sel);
|
|
336
|
+
}
|
|
337
|
+
this.#publish();
|
|
338
|
+
}
|
|
339
|
+
/** Controlled `value`: replaces the document unless it is what the editor last reported. */
|
|
340
|
+
syncValue(value) {
|
|
341
|
+
if (value === this.#blocks)
|
|
342
|
+
return;
|
|
343
|
+
const html = blocksToHtml(value);
|
|
344
|
+
if (html === blocksToHtml(this.getBlocks())) {
|
|
345
|
+
this.#blocks = value;
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const sel = this.getSelection();
|
|
349
|
+
this.engine.setHtml(html);
|
|
350
|
+
this.#blocks = this.#readBlocks();
|
|
351
|
+
this.#blocksDoc = this.engine.state.doc;
|
|
352
|
+
if (sel && this.#focused)
|
|
353
|
+
this.setSelection(sel);
|
|
354
|
+
}
|
|
355
|
+
/** Applies a block-level edit as one undoable step and reports it. */
|
|
356
|
+
apply(next, caret) {
|
|
357
|
+
if (next === this.getBlocks())
|
|
358
|
+
return;
|
|
359
|
+
const doc = this.engine.parseHtml(blocksToHtml(next));
|
|
360
|
+
const state = this.engine.state;
|
|
361
|
+
const tr = state.tr.replaceWith(0, state.doc.content.size, doc.content).setMeta('salmonBlocksApi', true);
|
|
362
|
+
this.engine.dispatch(tr);
|
|
363
|
+
if (caret)
|
|
364
|
+
this.setSelection(caret);
|
|
365
|
+
}
|
|
366
|
+
// ---- selection --------------------------------------------------
|
|
367
|
+
#blockAncestor(state = this.engine.state) {
|
|
368
|
+
const sel = state.selection;
|
|
369
|
+
if (sel instanceof NodeSelection && sel.node.attrs['blockId'])
|
|
370
|
+
return { id: String(sel.node.attrs['blockId']), pos: sel.from, node: sel.node };
|
|
371
|
+
const $from = sel.$from;
|
|
372
|
+
for (let d = $from.depth; d > 0; d--) {
|
|
373
|
+
const node = $from.node(d);
|
|
374
|
+
const id = node.attrs['blockId'];
|
|
375
|
+
const own = d > 1 && node.type.name === 'paragraph' && OWN_TEXT.has($from.node(d - 1).type.name) && $from.index(d - 1) === 0;
|
|
376
|
+
if (id && !own && BLOCK_NODES.has(node.type.name))
|
|
377
|
+
return { id: String(id), pos: $from.before(d), node };
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
currentBlockId() {
|
|
382
|
+
return this.#blockAncestor()?.id ?? null;
|
|
383
|
+
}
|
|
384
|
+
getSelection() {
|
|
385
|
+
const found = this.#blockAncestor();
|
|
386
|
+
if (!found)
|
|
387
|
+
return null;
|
|
388
|
+
const sel = this.engine.state.selection;
|
|
389
|
+
return { blockId: found.id, offset: sel instanceof NodeSelection ? 0 : sel.$from.parentOffset };
|
|
390
|
+
}
|
|
391
|
+
#find(id) {
|
|
392
|
+
let found = null;
|
|
393
|
+
this.engine.state.doc.descendants((node, pos) => {
|
|
394
|
+
if (found)
|
|
395
|
+
return false;
|
|
396
|
+
if (node.attrs['blockId'] === id && BLOCK_NODES.has(node.type.name)) {
|
|
397
|
+
found = { pos, node };
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
return true;
|
|
401
|
+
});
|
|
402
|
+
return found;
|
|
403
|
+
}
|
|
404
|
+
setSelection(selection) {
|
|
405
|
+
const found = this.#find(selection.blockId);
|
|
406
|
+
if (!found)
|
|
407
|
+
return;
|
|
408
|
+
const { pos, node } = found;
|
|
409
|
+
const state = this.engine.state;
|
|
410
|
+
let tr;
|
|
411
|
+
if (node.isTextblock) {
|
|
412
|
+
tr = state.tr.setSelection(TextSelection.create(state.doc, pos + 1 + Math.max(0, Math.min(selection.offset, node.content.size))));
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
let text = null;
|
|
416
|
+
node.descendants((child, childPos) => {
|
|
417
|
+
if (text !== null)
|
|
418
|
+
return false;
|
|
419
|
+
if (child.isTextblock) {
|
|
420
|
+
text = pos + 1 + childPos + 1 + Math.max(0, Math.min(selection.offset, child.content.size));
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
return true;
|
|
424
|
+
});
|
|
425
|
+
tr = text !== null ? state.tr.setSelection(TextSelection.create(state.doc, text)) : state.tr.setSelection(NodeSelection.create(state.doc, pos));
|
|
426
|
+
}
|
|
427
|
+
this.engine.dispatch(tr.scrollIntoView());
|
|
428
|
+
}
|
|
429
|
+
/** The block element for an id (for the handle, menus and drop indicator). */
|
|
430
|
+
blockElement(id) {
|
|
431
|
+
const found = this.#find(id);
|
|
432
|
+
if (!found)
|
|
433
|
+
return null;
|
|
434
|
+
const dom = this.engine.view?.nodeDOM(found.pos);
|
|
435
|
+
return dom instanceof HTMLElement ? dom : null;
|
|
436
|
+
}
|
|
437
|
+
/** Viewport rectangle of the caret (slash menu) or the selection start (inline toolbar). */
|
|
438
|
+
caretRect(at = 'from') {
|
|
439
|
+
const view = this.engine.view;
|
|
440
|
+
if (!view)
|
|
441
|
+
return null;
|
|
442
|
+
const pos = at === 'suggest' ? (this.engine.suggest?.from ?? view.state.selection.from) : view.state.selection.from;
|
|
443
|
+
try {
|
|
444
|
+
const r = view.coordsAtPos(pos);
|
|
445
|
+
return { left: r.left, top: r.top, bottom: r.bottom };
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
focus() {
|
|
452
|
+
this.engine.focus();
|
|
453
|
+
}
|
|
454
|
+
// ---- block operations ---------------------
|
|
455
|
+
getBlock(id) {
|
|
456
|
+
return findBlock(this.getBlocks(), id);
|
|
457
|
+
}
|
|
458
|
+
insertBlocks(blocks, position) {
|
|
459
|
+
const used = new Set();
|
|
460
|
+
const collect = (list) => {
|
|
461
|
+
for (const b of list) {
|
|
462
|
+
used.add(b.id);
|
|
463
|
+
if (b.children)
|
|
464
|
+
collect(b.children);
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
collect(this.getBlocks());
|
|
468
|
+
// Inserted blocks keep their ids unless they collide.
|
|
469
|
+
const fresh = (b) => {
|
|
470
|
+
const copy = { ...b, id: used.has(b.id) || !b.id ? this.nextId() : b.id };
|
|
471
|
+
used.add(copy.id);
|
|
472
|
+
if (b.children)
|
|
473
|
+
copy.children = b.children.map(fresh);
|
|
474
|
+
return copy;
|
|
475
|
+
};
|
|
476
|
+
this.apply(insertBlocks(this.getBlocks(), blocks.map(fresh), position));
|
|
477
|
+
}
|
|
478
|
+
removeBlocks(ids) {
|
|
479
|
+
this.apply(remove(this.getBlocks(), ids, () => this.nextId()));
|
|
480
|
+
}
|
|
481
|
+
updateBlock(id, patch) {
|
|
482
|
+
this.apply(updateIn(this.getBlocks(), id, patch));
|
|
483
|
+
}
|
|
484
|
+
turnInto(id, type) {
|
|
485
|
+
const sel = this.getSelection();
|
|
486
|
+
this.apply(turnInto(this.getBlocks(), id, type), sel && sel.blockId === id ? sel : { blockId: id, offset: 0 });
|
|
487
|
+
}
|
|
488
|
+
duplicate(id) {
|
|
489
|
+
const result = duplicate(this.getBlocks(), id, () => this.nextId());
|
|
490
|
+
if (result.id)
|
|
491
|
+
this.apply(result.blocks, { blockId: result.id, offset: 0 });
|
|
492
|
+
}
|
|
493
|
+
delete(id) {
|
|
494
|
+
const blocks = this.getBlocks();
|
|
495
|
+
const at = locate(blocks, id);
|
|
496
|
+
const next = remove(blocks, [id], () => this.nextId());
|
|
497
|
+
// Caret to the previous block (or the next one).
|
|
498
|
+
const siblings = at ? (at.parentId === null ? next : (findBlock(next, at.parentId)?.children ?? [])) : next;
|
|
499
|
+
const focus = siblings[Math.max(0, (at?.index ?? 0) - 1)] ?? next[0];
|
|
500
|
+
this.apply(next, focus ? { blockId: focus.id, offset: 0 } : null);
|
|
501
|
+
}
|
|
502
|
+
/** Moves a block; false when cancelled by onBeforeBlockMove or impossible. */
|
|
503
|
+
move(id, position, source) {
|
|
504
|
+
const blocks = this.getBlocks();
|
|
505
|
+
const block = findBlock(blocks, id);
|
|
506
|
+
const from = locate(blocks, id);
|
|
507
|
+
if (!block || !from)
|
|
508
|
+
return false;
|
|
509
|
+
const next = moveIn(blocks, id, position);
|
|
510
|
+
if (next === blocks)
|
|
511
|
+
return false;
|
|
512
|
+
const p = this.#hooks.props();
|
|
513
|
+
let prevented = false;
|
|
514
|
+
const event = { block, from, position, source };
|
|
515
|
+
p.onBeforeBlockMove?.({
|
|
516
|
+
...event,
|
|
517
|
+
preventDefault: () => {
|
|
518
|
+
prevented = true;
|
|
519
|
+
},
|
|
520
|
+
get defaultPrevented() {
|
|
521
|
+
return prevented;
|
|
522
|
+
},
|
|
523
|
+
});
|
|
524
|
+
if (prevented)
|
|
525
|
+
return false;
|
|
526
|
+
const sel = this.getSelection();
|
|
527
|
+
this.apply(next, sel && sel.blockId === id ? sel : { blockId: id, offset: 0 });
|
|
528
|
+
p.onBlockMove?.(event);
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
step(direction, id = this.currentBlockId()) {
|
|
532
|
+
if (!id)
|
|
533
|
+
return false;
|
|
534
|
+
const position = stepPosition(this.getBlocks(), id, direction);
|
|
535
|
+
return position ? this.move(id, position, 'keyboard') : false;
|
|
536
|
+
}
|
|
537
|
+
/** Runs a block command, UI action or engine command by id (menus, actionsRef.runCommand). */
|
|
538
|
+
runAction(id, ...args) {
|
|
539
|
+
if (this.runBlockAction(id))
|
|
540
|
+
return true;
|
|
541
|
+
if (BLOCK_ACTIONS.has(id))
|
|
542
|
+
return false;
|
|
543
|
+
const result = this.engine.run(id, ...args);
|
|
544
|
+
return result !== false && result !== 0 && result !== undefined;
|
|
545
|
+
}
|
|
546
|
+
/** Ids the engine does not know (key bindings and `run` fall through to this). */
|
|
547
|
+
runBlockAction(id) {
|
|
548
|
+
const current = this.currentBlockId();
|
|
549
|
+
switch (id) {
|
|
550
|
+
case 'moveBlockUp':
|
|
551
|
+
return this.step(-1);
|
|
552
|
+
case 'moveBlockDown':
|
|
553
|
+
return this.step(1);
|
|
554
|
+
case 'duplicateBlock':
|
|
555
|
+
if (!current)
|
|
556
|
+
return false;
|
|
557
|
+
this.duplicate(current);
|
|
558
|
+
return true;
|
|
559
|
+
case 'deleteBlock':
|
|
560
|
+
if (!current)
|
|
561
|
+
return false;
|
|
562
|
+
this.delete(current);
|
|
563
|
+
return true;
|
|
564
|
+
case 'blockMenu':
|
|
565
|
+
case 'contextMenu':
|
|
566
|
+
case 'print':
|
|
567
|
+
case 'link':
|
|
568
|
+
case 'image':
|
|
569
|
+
return this.#hooks.action(id);
|
|
570
|
+
case 'paragraph':
|
|
571
|
+
case 'heading1':
|
|
572
|
+
case 'heading2':
|
|
573
|
+
case 'heading3':
|
|
574
|
+
case 'bulletList':
|
|
575
|
+
case 'numberedList':
|
|
576
|
+
case 'checklist':
|
|
577
|
+
case 'quote':
|
|
578
|
+
case 'callout':
|
|
579
|
+
case 'code':
|
|
580
|
+
case 'toggle':
|
|
581
|
+
if (!current)
|
|
582
|
+
return false;
|
|
583
|
+
this.turnInto(current, id);
|
|
584
|
+
return true;
|
|
585
|
+
case 'divider':
|
|
586
|
+
return this.#insertAfterCurrent({ id: this.nextId(), type: 'divider' });
|
|
587
|
+
case 'table':
|
|
588
|
+
return this.engine.commands.insertTable({ rows: 3, cols: 3, headerRow: true });
|
|
589
|
+
default:
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
#insertAfterCurrent(block) {
|
|
594
|
+
const current = this.currentBlockId();
|
|
595
|
+
const blocks = this.getBlocks();
|
|
596
|
+
const empty = current ? findBlock(blocks, current) : undefined;
|
|
597
|
+
// An empty paragraph (where `/divider` was typed) is replaced.
|
|
598
|
+
if (empty && empty.type === 'paragraph' && !(empty.content ?? []).length) {
|
|
599
|
+
const next = insertBlocks(blocks, [block], { after: empty.id });
|
|
600
|
+
const after = { id: this.nextId(), type: 'paragraph', content: [] };
|
|
601
|
+
this.apply(insertBlocks(remove(next, [empty.id], () => this.nextId()), [after], { after: block.id }), { blockId: after.id, offset: 0 });
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
this.apply(current ? insertBlocks(blocks, [block], { after: current }) : [...blocks, block]);
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
async insertFiles(files) {
|
|
608
|
+
await this.engine.insertFiles(files);
|
|
609
|
+
}
|
|
610
|
+
/** Puts the caret in a table's first cell (table commands from the block menu). */
|
|
611
|
+
selectInTable(id) {
|
|
612
|
+
const found = this.#find(id);
|
|
613
|
+
if (!found)
|
|
614
|
+
return;
|
|
615
|
+
const sel = this.engine.state.selection;
|
|
616
|
+
if (sel.from > found.pos && sel.from < found.pos + found.node.nodeSize)
|
|
617
|
+
return;
|
|
618
|
+
let text = null;
|
|
619
|
+
found.node.descendants((child, childPos) => {
|
|
620
|
+
if (text !== null)
|
|
621
|
+
return false;
|
|
622
|
+
if (child.isTextblock) {
|
|
623
|
+
text = found.pos + 1 + childPos + 1;
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
return true;
|
|
627
|
+
});
|
|
628
|
+
if (text !== null)
|
|
629
|
+
this.engine.dispatch(this.engine.state.tr.setSelection(TextSelection.create(this.engine.state.doc, text)));
|
|
630
|
+
}
|
|
631
|
+
/** Image block settings (alt text, width). */
|
|
632
|
+
updateImage(id, attrs) {
|
|
633
|
+
const found = this.#find(id);
|
|
634
|
+
if (!found || found.node.type.name !== 'figure')
|
|
635
|
+
return;
|
|
636
|
+
this.engine.dispatch(this.engine.state.tr.setNodeMarkup(found.pos, undefined, { ...found.node.attrs, ...attrs }));
|
|
637
|
+
}
|
|
638
|
+
getHtml() {
|
|
639
|
+
return this.engine.getHtml();
|
|
640
|
+
}
|
|
641
|
+
// ---- paste --------------------------------------------------------
|
|
642
|
+
#onPaste(event) {
|
|
643
|
+
const data = event.clipboardData;
|
|
644
|
+
const html = data?.getData('text/html') ?? '';
|
|
645
|
+
const text = data?.getData('text/plain') ?? '';
|
|
646
|
+
const files = data ? [...data.files] : [];
|
|
647
|
+
let prevented = false;
|
|
648
|
+
this.#hooks.props().onBeforePaste?.({
|
|
649
|
+
html,
|
|
650
|
+
text,
|
|
651
|
+
files,
|
|
652
|
+
preventDefault: () => {
|
|
653
|
+
prevented = true;
|
|
654
|
+
},
|
|
655
|
+
get defaultPrevented() {
|
|
656
|
+
return prevented;
|
|
657
|
+
},
|
|
658
|
+
});
|
|
659
|
+
if (prevented || this.#hooks.props().readOnly) {
|
|
660
|
+
event.preventDefault();
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
this.#lastPaste = { html, text };
|
|
664
|
+
}
|
|
665
|
+
// ---- mentions and labels ----------------------------------------------
|
|
666
|
+
/** The engine's Mention target, with labels inserted as label atoms. */
|
|
667
|
+
mentionTarget() {
|
|
668
|
+
const base = this.engine.mentionTarget;
|
|
669
|
+
if (!base)
|
|
670
|
+
return null;
|
|
671
|
+
if (this.#mention && this.#mention.element === base.element)
|
|
672
|
+
return this.#mention;
|
|
673
|
+
const engine = this.engine;
|
|
674
|
+
const hooks = this.#hooks;
|
|
675
|
+
this.#mention = {
|
|
676
|
+
element: base.element,
|
|
677
|
+
getTextBeforeCaret: () => base.getTextBeforeCaret(),
|
|
678
|
+
getCaretRect: (offset) => base.getCaretRect(offset),
|
|
679
|
+
subscribe: (listener) => base.subscribe(listener),
|
|
680
|
+
insertMention(insertion) {
|
|
681
|
+
const labels = hooks.props().labels;
|
|
682
|
+
if (!labels || insertion.trigger !== (labels.trigger ?? '#')) {
|
|
683
|
+
base.insertMention(insertion);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const $from = engine.state.selection.$from;
|
|
687
|
+
const start = $from.start();
|
|
688
|
+
const tone = insertion.item?.tone;
|
|
689
|
+
const node = engine.schema.nodes['label'].create({ id: String(insertion.value), label: insertion.label, trigger: insertion.trigger, tone: typeof tone === 'string' ? tone : null });
|
|
690
|
+
const suffix = insertion.suffix || ' ';
|
|
691
|
+
const tr = engine.state.tr.replaceWith(start + insertion.start, start + insertion.end, [node, engine.schema.text(suffix)]);
|
|
692
|
+
tr.setSelection(TextSelection.create(tr.doc, start + insertion.start + 1 + suffix.length));
|
|
693
|
+
engine.dispatch(tr.scrollIntoView());
|
|
694
|
+
engine.view?.focus();
|
|
695
|
+
},
|
|
696
|
+
};
|
|
697
|
+
return this.#mention;
|
|
698
|
+
}
|
|
699
|
+
refresh() {
|
|
700
|
+
this.engine.refresh();
|
|
701
|
+
}
|
|
702
|
+
destroy() {
|
|
703
|
+
this.#unsubscribe();
|
|
704
|
+
for (const c of this.#cleanups)
|
|
705
|
+
c();
|
|
706
|
+
this.engine.destroy();
|
|
707
|
+
this.#listeners.clear();
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
/** Creates the engine over the server-rendered element and wraps it. */
|
|
711
|
+
export async function createBlockController(element, blocks, hooks) {
|
|
712
|
+
const p = hooks.props();
|
|
713
|
+
let controller = null;
|
|
714
|
+
const nextId = () => controller?.nextId() ?? `k${Math.random().toString(36).slice(2, 8)}`;
|
|
715
|
+
const extension = {
|
|
716
|
+
name: 'salmonBlockEditor',
|
|
717
|
+
plugins: () => [blockPlugin(nextId, (event) => hooks.suggestKey(event), () => Boolean(controller?.engine.suggest))],
|
|
718
|
+
};
|
|
719
|
+
const cleanup = () => hooks.props().pasteCleanup;
|
|
720
|
+
const engine = await createEngine({
|
|
721
|
+
html: blocksToHtml(blocks),
|
|
722
|
+
extensions: [extension, ...(p.extensions ?? [])],
|
|
723
|
+
embedHosts: p.embedHosts,
|
|
724
|
+
sanitize: p.sanitize,
|
|
725
|
+
undoLimit: p.undoLimit,
|
|
726
|
+
tabKey: 'indent',
|
|
727
|
+
keyBindings: engineKeyBindings(p.keyBindings),
|
|
728
|
+
placeholder: () => hooks.props().placeholder,
|
|
729
|
+
highlight: () => hooks.props().highlight,
|
|
730
|
+
codeLanguages: () => hooks.props().codeLanguages,
|
|
731
|
+
codeLanguageText: () => ({ label: hooks.text().codeLanguage, plainText: hooks.text().codePlainText }),
|
|
732
|
+
suggestTriggers: () => (hooks.props().slashMenu === false || hooks.props().readOnly ? [] : [{ id: 'slash', char: '/' }]),
|
|
733
|
+
onAction: (id) => controller?.runBlockAction(id) ?? false,
|
|
734
|
+
upload: () => {
|
|
735
|
+
const upload = hooks.props().onUpload;
|
|
736
|
+
return upload ? (file) => upload(file) : undefined;
|
|
737
|
+
},
|
|
738
|
+
uploadLabel: () => hooks.text().uploading,
|
|
739
|
+
pasteMode: () => cleanup()?.mode ?? 'clean',
|
|
740
|
+
cleanPaste: (html, mode) => cleanPastedHtml(html, { ...cleanup(), mode }),
|
|
741
|
+
pastePrompt: () => hooks.pastePrompt(),
|
|
742
|
+
editable: () => !hooks.props().readOnly,
|
|
743
|
+
attributes: () => {
|
|
744
|
+
const q = hooks.props();
|
|
745
|
+
const attrs = { role: 'textbox', 'aria-multiline': 'true' };
|
|
746
|
+
if (q['aria-labelledby'])
|
|
747
|
+
attrs['aria-labelledby'] = q['aria-labelledby'];
|
|
748
|
+
else
|
|
749
|
+
attrs['aria-label'] = q['aria-label'] ?? hooks.text().editor;
|
|
750
|
+
if (q.readOnly)
|
|
751
|
+
attrs['aria-readonly'] = 'true';
|
|
752
|
+
return attrs;
|
|
753
|
+
},
|
|
754
|
+
});
|
|
755
|
+
controller = new BlockController(engine, element, [...blocks], hooks);
|
|
756
|
+
return controller;
|
|
757
|
+
}
|
|
758
|
+
//# sourceMappingURL=controller.js.map
|