@vitral/editor 0.2.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 +165 -0
- package/LICENSE.GPL +674 -0
- package/README.md +34 -0
- package/dist/block.d.ts +39 -0
- package/dist/block.js +150 -0
- package/dist/block.js.map +1 -0
- package/dist/buttons.d.ts +32 -0
- package/dist/buttons.js +193 -0
- package/dist/buttons.js.map +1 -0
- package/dist/editor.d.ts +2 -0
- package/dist/editor.js +483 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/dist/render/menus.d.ts +67 -0
- package/dist/render/menus.js +172 -0
- package/dist/render/menus.js.map +1 -0
- package/dist/render/toolbar.d.ts +48 -0
- package/dist/render/toolbar.js +114 -0
- package/dist/render/toolbar.js.map +1 -0
- package/dist/slash.d.ts +44 -0
- package/dist/slash.js +196 -0
- package/dist/slash.js.map +1 -0
- package/dist/types.d.ts +128 -0
- package/package.json +56 -0
package/dist/editor.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { defaultBubbleMenu, defaultToolbar, editorIcons } from "./buttons.js";
|
|
2
|
+
import { bubbleView, toolbarView } from "./render/toolbar.js";
|
|
3
|
+
import { colorPanelView, imagePanelView, linkPanelView, tablePanelView } from "./render/menus.js";
|
|
4
|
+
import { createBlockHandle, defaultBlockActions } from "./block.js";
|
|
5
|
+
import { createSlashMenu, defaultSlashCommands } from "./slash.js";
|
|
6
|
+
import { activeEditorBlockType, activeEditorMark, ariaEditorShortcut, createEditor, createEditorView, editorPalette, editorSelectionRange, editorShortcutFor, en, formatEditorShortcut, formatMessage, isEmptyEditorDoc, loadStyle, matchEditorPaletteColor, visuallyHidden } from "@vitral/core";
|
|
7
|
+
import { createOverlay, createSelect, createTooltips } from "@vitral/controls";
|
|
8
|
+
import { createRoot, h, mergeAttrs, partResolver } from "@vitral/dom";
|
|
9
|
+
import { registerIcons } from "@vitral/icons";
|
|
10
|
+
import { baseStyle, buttonStyle, editorStyle } from "@vitral/styles";
|
|
11
|
+
//#region src/editor.ts
|
|
12
|
+
/**
|
|
13
|
+
* A rich text editor with no framework in it: the document, the commands and
|
|
14
|
+
* the history are `@vitral/core`'s, the text itself is its view over a
|
|
15
|
+
* `contenteditable` element, and everything around them — the toolbar, the
|
|
16
|
+
* panels, the floating toolbar over a selection, the menu a `/` opens and the
|
|
17
|
+
* handle beside a block — is drawn here.
|
|
18
|
+
*/
|
|
19
|
+
var counter = 0;
|
|
20
|
+
function createTextEditor(element, config = {}) {
|
|
21
|
+
let current = { ...config };
|
|
22
|
+
let view = null;
|
|
23
|
+
let contentEl = null;
|
|
24
|
+
let caretEl = null;
|
|
25
|
+
let blockSelect = null;
|
|
26
|
+
let blockSelectHost = null;
|
|
27
|
+
let link = {
|
|
28
|
+
href: "",
|
|
29
|
+
text: "",
|
|
30
|
+
newTab: false,
|
|
31
|
+
existing: false
|
|
32
|
+
};
|
|
33
|
+
const anchors = /* @__PURE__ */ new Map();
|
|
34
|
+
const id = config.id ?? `vt-texteditor-${++counter}`;
|
|
35
|
+
const ids = {
|
|
36
|
+
content: `${id}-content`,
|
|
37
|
+
help: `${id}-help`,
|
|
38
|
+
slash: `${id}-slash`,
|
|
39
|
+
block: `${id}-block`
|
|
40
|
+
};
|
|
41
|
+
const root = createRoot(element);
|
|
42
|
+
const locale = () => current.locale ?? en;
|
|
43
|
+
const palette = () => current.palette ?? editorPalette;
|
|
44
|
+
const part = partResolver({
|
|
45
|
+
style: editorStyle,
|
|
46
|
+
unstyled: () => !!current.unstyled,
|
|
47
|
+
classes: () => current.classes,
|
|
48
|
+
pt: () => current.pt,
|
|
49
|
+
props: () => current
|
|
50
|
+
});
|
|
51
|
+
if (!config.unstyled) {
|
|
52
|
+
const options = {
|
|
53
|
+
nonce: config.nonce,
|
|
54
|
+
cssLayer: config.cssLayer
|
|
55
|
+
};
|
|
56
|
+
loadStyle(baseStyle.name, baseStyle.css, options);
|
|
57
|
+
loadStyle(buttonStyle.name, buttonStyle.css, options);
|
|
58
|
+
loadStyle(editorStyle.name, editorStyle.css, options);
|
|
59
|
+
}
|
|
60
|
+
const tooltips = createTooltips(() => ({
|
|
61
|
+
placement: "bottom",
|
|
62
|
+
unstyled: current.unstyled,
|
|
63
|
+
nonce: current.nonce,
|
|
64
|
+
cssLayer: current.cssLayer,
|
|
65
|
+
zIndex: current.zIndex,
|
|
66
|
+
pt: current.pt
|
|
67
|
+
}));
|
|
68
|
+
registerIcons(editorIcons);
|
|
69
|
+
const editor = createEditor({
|
|
70
|
+
content: current.content ?? "",
|
|
71
|
+
maxLength: current.maxLength ?? null,
|
|
72
|
+
historyDelay: current.historyDelay
|
|
73
|
+
});
|
|
74
|
+
const editable = () => !current.readonly && !current.disabled;
|
|
75
|
+
const collapsed = () => editorSelectionRange(editor.state.selection).empty;
|
|
76
|
+
const run = (name, ...args) => editor.run(name, ...args);
|
|
77
|
+
const can = (name, ...args) => editor.can(name, ...args);
|
|
78
|
+
const stop = editor.subscribe((update) => {
|
|
79
|
+
if (update.docChanged) current.on?.change?.({
|
|
80
|
+
html: editor.getHTML(),
|
|
81
|
+
json: editor.getJSON(),
|
|
82
|
+
text: editor.getText()
|
|
83
|
+
});
|
|
84
|
+
if (update.selectionChanged) current.on?.["selection-change"]?.({
|
|
85
|
+
empty: collapsed(),
|
|
86
|
+
source: update.origin === "user" ? "user" : update.origin === "history" ? "history" : "api"
|
|
87
|
+
});
|
|
88
|
+
render();
|
|
89
|
+
});
|
|
90
|
+
const panelContext = () => ({
|
|
91
|
+
locale: locale(),
|
|
92
|
+
part,
|
|
93
|
+
ids
|
|
94
|
+
});
|
|
95
|
+
const anchorFor = (name) => () => anchors.get(name) ?? caretEl;
|
|
96
|
+
function panel(name, render, placement = "bottom-start") {
|
|
97
|
+
return createOverlay({
|
|
98
|
+
anchor: anchorFor(name),
|
|
99
|
+
render,
|
|
100
|
+
placement,
|
|
101
|
+
target: () => current.overlayTarget,
|
|
102
|
+
zIndex: current.zIndex,
|
|
103
|
+
onClose: () => renderPanels()
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const linkPanel = panel("link", () => linkPanelView(panelContext(), link, linkActions));
|
|
107
|
+
const imagePanel = panel("image", () => imagePanelView(panelContext(), imageActions));
|
|
108
|
+
const tablePanel = panel("table", () => tablePanelView(panelContext(), { insert: insertTable }));
|
|
109
|
+
const colorPanel = panel("color", () => colorPanelView(panelContext(), {
|
|
110
|
+
kind: colorKind,
|
|
111
|
+
palette: palette(),
|
|
112
|
+
chosen: colorOf(colorKind),
|
|
113
|
+
onChoose: chooseColor
|
|
114
|
+
}));
|
|
115
|
+
let colorKind = "color";
|
|
116
|
+
const linkActions = {
|
|
117
|
+
change: (patch) => {
|
|
118
|
+
link = {
|
|
119
|
+
...link,
|
|
120
|
+
...patch,
|
|
121
|
+
error: void 0
|
|
122
|
+
};
|
|
123
|
+
linkPanel.update();
|
|
124
|
+
},
|
|
125
|
+
apply: () => {
|
|
126
|
+
const href = link.href.trim();
|
|
127
|
+
if (!href) {
|
|
128
|
+
link = {
|
|
129
|
+
...link,
|
|
130
|
+
error: locale().editor.invalidUrl
|
|
131
|
+
};
|
|
132
|
+
linkPanel.update();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
run("setLink", href, {
|
|
136
|
+
target: link.newTab ? "_blank" : null,
|
|
137
|
+
text: link.text.trim() || void 0
|
|
138
|
+
});
|
|
139
|
+
linkPanel.close();
|
|
140
|
+
focus();
|
|
141
|
+
},
|
|
142
|
+
remove: () => {
|
|
143
|
+
run("unsetLink");
|
|
144
|
+
linkPanel.close();
|
|
145
|
+
focus();
|
|
146
|
+
},
|
|
147
|
+
cancel: () => {
|
|
148
|
+
linkPanel.close();
|
|
149
|
+
focus();
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const imageActions = {
|
|
153
|
+
insert: (src, alt) => {
|
|
154
|
+
if (src.trim()) run("insertImage", {
|
|
155
|
+
src: src.trim(),
|
|
156
|
+
alt: alt.trim()
|
|
157
|
+
});
|
|
158
|
+
imagePanel.close();
|
|
159
|
+
focus();
|
|
160
|
+
},
|
|
161
|
+
cancel: () => {
|
|
162
|
+
imagePanel.close();
|
|
163
|
+
focus();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
function insertTable(rows, columns) {
|
|
167
|
+
run("insertTable", rows, columns, true);
|
|
168
|
+
tablePanel.close();
|
|
169
|
+
focus();
|
|
170
|
+
}
|
|
171
|
+
const colorOf = (kind) => {
|
|
172
|
+
const value = activeEditorMark(editor.state, kind)?.attrs?.color;
|
|
173
|
+
if (typeof value !== "string") return void 0;
|
|
174
|
+
const name = matchEditorPaletteColor(value, kind, palette());
|
|
175
|
+
return palette().find((color) => color.name === name);
|
|
176
|
+
};
|
|
177
|
+
function chooseColor(color) {
|
|
178
|
+
run(colorKind === "color" ? "setColor" : "setHighlight", color?.name ?? null);
|
|
179
|
+
colorPanel.close();
|
|
180
|
+
focus();
|
|
181
|
+
}
|
|
182
|
+
const bubble = createOverlay({
|
|
183
|
+
anchor: () => caretEl,
|
|
184
|
+
render: () => bubbleView(toolbarContext(), bubbleItems()),
|
|
185
|
+
placement: "top",
|
|
186
|
+
target: () => current.overlayTarget,
|
|
187
|
+
zIndex: current.zIndex,
|
|
188
|
+
restoreFocus: false
|
|
189
|
+
});
|
|
190
|
+
const bubbleItems = () => Array.isArray(current.bubbleMenu) ? current.bubbleMenu : defaultBubbleMenu;
|
|
191
|
+
function syncBubble() {
|
|
192
|
+
if (current.bubbleMenu !== false && editable() && !collapsed() && !editor.isEmpty()) {
|
|
193
|
+
placeCaret();
|
|
194
|
+
bubble.open();
|
|
195
|
+
bubble.update();
|
|
196
|
+
} else bubble.close();
|
|
197
|
+
}
|
|
198
|
+
const slashMenu = createSlashMenu({
|
|
199
|
+
editor,
|
|
200
|
+
content: () => contentEl,
|
|
201
|
+
anchor: () => caretEl,
|
|
202
|
+
commands: () => Array.isArray(current.slashMenu) ? current.slashMenu : defaultSlashCommands(locale()),
|
|
203
|
+
part,
|
|
204
|
+
locale,
|
|
205
|
+
enabled: () => current.slashMenu !== false && editable(),
|
|
206
|
+
place: placeCaret,
|
|
207
|
+
ids,
|
|
208
|
+
overlayTarget: current.overlayTarget,
|
|
209
|
+
zIndex: current.zIndex,
|
|
210
|
+
onRun: (command) => {
|
|
211
|
+
if (command.run) command.run(handle);
|
|
212
|
+
else if (command.command) run(command.command[0], ...command.command.slice(1));
|
|
213
|
+
focus();
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
const blockHandle = createBlockHandle({
|
|
217
|
+
editor,
|
|
218
|
+
host: () => element,
|
|
219
|
+
caretRect: () => view?.selectionRect() ?? null,
|
|
220
|
+
actions: () => Array.isArray(current.blockMenu) ? current.blockMenu : defaultBlockActions(locale()),
|
|
221
|
+
part,
|
|
222
|
+
locale,
|
|
223
|
+
enabled: () => current.blockMenu !== false && editable(),
|
|
224
|
+
ids,
|
|
225
|
+
overlayTarget: current.overlayTarget,
|
|
226
|
+
zIndex: current.zIndex
|
|
227
|
+
});
|
|
228
|
+
/** The caret's own rectangle, as an element popups can be anchored to. */
|
|
229
|
+
function placeCaret() {
|
|
230
|
+
if (!caretEl || !contentEl) return;
|
|
231
|
+
const rect = view?.selectionRect();
|
|
232
|
+
if (!rect) return;
|
|
233
|
+
const host = element.getBoundingClientRect();
|
|
234
|
+
caretEl.style.top = `${rect.top - host.top}px`;
|
|
235
|
+
caretEl.style.left = `${rect.left - host.left}px`;
|
|
236
|
+
caretEl.style.width = `${Math.max(1, rect.width)}px`;
|
|
237
|
+
caretEl.style.height = `${rect.height}px`;
|
|
238
|
+
}
|
|
239
|
+
function openPanel(overlay, name, event) {
|
|
240
|
+
if (event) anchors.set(name, event.currentTarget);
|
|
241
|
+
closePanels(overlay);
|
|
242
|
+
overlay.open();
|
|
243
|
+
overlay.update();
|
|
244
|
+
}
|
|
245
|
+
const panels = () => [
|
|
246
|
+
linkPanel,
|
|
247
|
+
imagePanel,
|
|
248
|
+
tablePanel,
|
|
249
|
+
colorPanel
|
|
250
|
+
];
|
|
251
|
+
const closePanels = (except) => panels().forEach((overlay) => overlay !== except && overlay.close());
|
|
252
|
+
const renderPanels = () => panels().forEach((overlay) => overlay.update());
|
|
253
|
+
function toolbarContext() {
|
|
254
|
+
return {
|
|
255
|
+
locale: locale(),
|
|
256
|
+
part,
|
|
257
|
+
editable: editable(),
|
|
258
|
+
can,
|
|
259
|
+
isActive: (name, attrs) => editor.isActive(name, attrs),
|
|
260
|
+
shortcut: (name, args) => {
|
|
261
|
+
const binding = editorShortcutFor(name, args);
|
|
262
|
+
return binding ? {
|
|
263
|
+
label: formatEditorShortcut(binding),
|
|
264
|
+
aria: ariaEditorShortcut(binding)
|
|
265
|
+
} : void 0;
|
|
266
|
+
},
|
|
267
|
+
colors: {
|
|
268
|
+
text: colorOf("color"),
|
|
269
|
+
highlight: colorOf("highlight")
|
|
270
|
+
},
|
|
271
|
+
ref: (name) => (el) => {
|
|
272
|
+
if (el) anchors.set(name, el);
|
|
273
|
+
else anchors.delete(name);
|
|
274
|
+
},
|
|
275
|
+
on: {
|
|
276
|
+
tip: (element, text) => tooltips.attach(element, text),
|
|
277
|
+
command: (name, args, event) => {
|
|
278
|
+
run(name, ...args);
|
|
279
|
+
if (event.detail > 0) focus();
|
|
280
|
+
},
|
|
281
|
+
openLink: (event) => {
|
|
282
|
+
link = {
|
|
283
|
+
href: "",
|
|
284
|
+
text: "",
|
|
285
|
+
newTab: false,
|
|
286
|
+
existing: editor.isActive("link")
|
|
287
|
+
};
|
|
288
|
+
openPanel(linkPanel, "link", event);
|
|
289
|
+
},
|
|
290
|
+
openImage: (event) => openPanel(imagePanel, "image", event),
|
|
291
|
+
openTable: (event) => openPanel(tablePanel, "table", event),
|
|
292
|
+
openColor: (kind, event) => {
|
|
293
|
+
colorKind = kind;
|
|
294
|
+
openPanel(colorPanel, kind, event);
|
|
295
|
+
},
|
|
296
|
+
blockSelect: (el) => {
|
|
297
|
+
blockSelectHost = el ?? null;
|
|
298
|
+
syncBlockSelect();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
/** The block type, drawn by the control kit's select. */
|
|
304
|
+
function syncBlockSelect() {
|
|
305
|
+
if (!blockSelectHost) return;
|
|
306
|
+
const words = locale().editor;
|
|
307
|
+
const settings = {
|
|
308
|
+
options: [
|
|
309
|
+
{
|
|
310
|
+
label: words.paragraph,
|
|
311
|
+
value: "paragraph"
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
label: words.heading1,
|
|
315
|
+
value: "heading1"
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
label: words.heading2,
|
|
319
|
+
value: "heading2"
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
label: words.heading3,
|
|
323
|
+
value: "heading3"
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
label: words.codeBlock,
|
|
327
|
+
value: "codeBlock"
|
|
328
|
+
}
|
|
329
|
+
],
|
|
330
|
+
optionValue: "value",
|
|
331
|
+
value: activeEditorBlockType(editor.state),
|
|
332
|
+
size: "small",
|
|
333
|
+
ariaLabel: words.blockType,
|
|
334
|
+
disabled: !editable(),
|
|
335
|
+
locale: locale(),
|
|
336
|
+
unstyled: current.unstyled,
|
|
337
|
+
overlayTarget: current.overlayTarget,
|
|
338
|
+
zIndex: current.zIndex,
|
|
339
|
+
nonce: current.nonce,
|
|
340
|
+
cssLayer: current.cssLayer
|
|
341
|
+
};
|
|
342
|
+
if (blockSelect) blockSelect.update(settings);
|
|
343
|
+
else blockSelect = createSelect(blockSelectHost, {
|
|
344
|
+
...settings,
|
|
345
|
+
onChange: (value) => {
|
|
346
|
+
if (value === "paragraph") run("setParagraph");
|
|
347
|
+
else if (value === "codeBlock") run("toggleCodeBlock");
|
|
348
|
+
else run("toggleHeading", Number(String(value).slice(-1)));
|
|
349
|
+
focus();
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
const toolbarGroups = () => Array.isArray(current.toolbar) ? current.toolbar : defaultToolbar;
|
|
354
|
+
function countText() {
|
|
355
|
+
const words = locale().editor;
|
|
356
|
+
const count = editor.characterCount();
|
|
357
|
+
if (current.maxLength) return formatMessage(words.charactersLimit, {
|
|
358
|
+
count,
|
|
359
|
+
limit: current.maxLength
|
|
360
|
+
});
|
|
361
|
+
return formatMessage(count === 1 ? words.character : words.characters, { count });
|
|
362
|
+
}
|
|
363
|
+
function contentView() {
|
|
364
|
+
return h("div", mergeAttrs({
|
|
365
|
+
key: "content",
|
|
366
|
+
id: ids.content
|
|
367
|
+
}, part("content"), {
|
|
368
|
+
class: isEmptyEditorDoc(editor.state.doc) ? "vt-editor-content-empty" : void 0,
|
|
369
|
+
role: "textbox",
|
|
370
|
+
"aria-multiline": "true",
|
|
371
|
+
"aria-label": current.ariaLabelledby ? void 0 : current.ariaLabel,
|
|
372
|
+
"aria-labelledby": current.ariaLabelledby,
|
|
373
|
+
"aria-describedby": ids.help,
|
|
374
|
+
"aria-readonly": current.readonly ? "true" : void 0,
|
|
375
|
+
"aria-disabled": current.disabled ? "true" : void 0,
|
|
376
|
+
"aria-invalid": current.invalid ? "true" : void 0,
|
|
377
|
+
"aria-placeholder": current.placeholder || void 0,
|
|
378
|
+
"data-placeholder": current.placeholder || void 0,
|
|
379
|
+
contenteditable: editable() ? "true" : "false",
|
|
380
|
+
tabindex: current.disabled ? "-1" : "0",
|
|
381
|
+
spellcheck: "true",
|
|
382
|
+
translate: "no",
|
|
383
|
+
ref: (el) => {
|
|
384
|
+
const next = el;
|
|
385
|
+
if (next === contentEl) return;
|
|
386
|
+
contentEl = next;
|
|
387
|
+
attachView();
|
|
388
|
+
},
|
|
389
|
+
onFocus: (event) => current.on?.focus?.(event),
|
|
390
|
+
onBlur: (event) => current.on?.blur?.(event)
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
function render() {
|
|
394
|
+
const drawn = toolbarContext();
|
|
395
|
+
root.attrs(part("root", {
|
|
396
|
+
readonly: current.readonly,
|
|
397
|
+
disabled: current.disabled,
|
|
398
|
+
invalid: current.invalid
|
|
399
|
+
}));
|
|
400
|
+
root.render([
|
|
401
|
+
current.hooks?.toolbar ? current.hooks.toolbar() : current.toolbar === false ? null : toolbarView(drawn, toolbarGroups()),
|
|
402
|
+
contentView(),
|
|
403
|
+
h("span", mergeAttrs({
|
|
404
|
+
key: "caret",
|
|
405
|
+
"aria-hidden": "true"
|
|
406
|
+
}, part("caret"), { ref: (el) => caretEl = el })),
|
|
407
|
+
h("span", {
|
|
408
|
+
key: "help",
|
|
409
|
+
id: ids.help,
|
|
410
|
+
style: visuallyHidden
|
|
411
|
+
}, locale().editor.keyboardHelp),
|
|
412
|
+
current.hooks?.footer ? current.hooks.footer() : current.showCount ? h("div", mergeAttrs({ key: "footer" }, part("footer")), h("span", part("count", { limit: !!current.maxLength }), countText())) : null
|
|
413
|
+
]);
|
|
414
|
+
syncBlockSelect();
|
|
415
|
+
syncBubble();
|
|
416
|
+
renderPanels();
|
|
417
|
+
blockHandle.sync();
|
|
418
|
+
}
|
|
419
|
+
function attachView() {
|
|
420
|
+
view?.destroy();
|
|
421
|
+
view = null;
|
|
422
|
+
if (!contentEl) return;
|
|
423
|
+
view = createEditorView(contentEl, editor, {
|
|
424
|
+
editable,
|
|
425
|
+
taskLabel: locale().editor.taskDone,
|
|
426
|
+
selectedClass: "vt-editor-node-selected",
|
|
427
|
+
emptyClass: "vt-editor-content-empty"
|
|
428
|
+
});
|
|
429
|
+
if (current.autofocus) view.focus();
|
|
430
|
+
}
|
|
431
|
+
const focus = () => view?.focus();
|
|
432
|
+
render();
|
|
433
|
+
const handle = {
|
|
434
|
+
element,
|
|
435
|
+
update(next) {
|
|
436
|
+
const content = "content" in next;
|
|
437
|
+
current = {
|
|
438
|
+
...current,
|
|
439
|
+
...next
|
|
440
|
+
};
|
|
441
|
+
if (content) editor.setContent(current.content ?? "", { emit: false });
|
|
442
|
+
if ("maxLength" in next) editor.maxLength = current.maxLength ?? null;
|
|
443
|
+
render();
|
|
444
|
+
},
|
|
445
|
+
getHTML: () => editor.getHTML(),
|
|
446
|
+
getJSON: () => editor.getJSON(),
|
|
447
|
+
getText: () => editor.getText(),
|
|
448
|
+
getMarkdown: () => editor.getMarkdown(),
|
|
449
|
+
typeText: (text) => {
|
|
450
|
+
const typed = editor.typeText(text);
|
|
451
|
+
render();
|
|
452
|
+
return typed;
|
|
453
|
+
},
|
|
454
|
+
setContent: (value) => {
|
|
455
|
+
editor.setContent(value);
|
|
456
|
+
render();
|
|
457
|
+
},
|
|
458
|
+
run: (name, ...args) => {
|
|
459
|
+
const applied = run(name, ...args);
|
|
460
|
+
render();
|
|
461
|
+
return applied;
|
|
462
|
+
},
|
|
463
|
+
can,
|
|
464
|
+
isActive: (name, attrs) => editor.isActive(name, attrs),
|
|
465
|
+
focus,
|
|
466
|
+
refresh: render,
|
|
467
|
+
destroy() {
|
|
468
|
+
stop();
|
|
469
|
+
slashMenu.destroy();
|
|
470
|
+
blockHandle.destroy();
|
|
471
|
+
[...panels(), bubble].forEach((overlay) => overlay.destroy());
|
|
472
|
+
blockSelect?.destroy();
|
|
473
|
+
tooltips.destroy();
|
|
474
|
+
view?.destroy();
|
|
475
|
+
root.clear();
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
return handle;
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
export { createTextEditor };
|
|
482
|
+
|
|
483
|
+
//# sourceMappingURL=editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"editor.js","names":[],"sources":["../src/editor.ts"],"sourcesContent":["import {\n activeEditorBlockType,\n activeEditorMark,\n ariaEditorShortcut,\n createEditor as createEditorInstance,\n createEditorView,\n editorPalette,\n editorShortcutFor,\n editorSelectionRange,\n editorTextblocks,\n en,\n formatEditorShortcut,\n formatMessage,\n isEmptyEditorDoc,\n loadStyle,\n matchEditorPaletteColor,\n visuallyHidden,\n type EditorColor,\n type EditorInstance,\n type EditorNode,\n type EditorView,\n type Locale\n} from '@vitral/core';\nimport { createOverlay, createSelect, createTooltips, type Overlay, type SelectHandle } from '@vitral/controls';\nimport { createRoot, h, mergeAttrs, partResolver, type Child, type Props } from '@vitral/dom';\nimport { registerIcons } from '@vitral/icons';\nimport { baseStyle, buttonStyle, editorStyle } from '@vitral/styles';\nimport { defaultBubbleMenu, defaultToolbar, editorIcons } from './buttons';\nimport { createBlockHandle, defaultBlockActions } from './block';\nimport { colorPanelView, imagePanelView, linkPanelView, tablePanelView, type LinkPanelState, type PanelContext } from './render/menus';\nimport { createSlashMenu, defaultSlashCommands } from './slash';\nimport { bubbleView, toolbarView, type ToolbarContext } from './render/toolbar';\nimport type { TextEditorConfig, TextEditorHandle, EditorToolbarItem } from './types';\n\n/**\n * A rich text editor with no framework in it: the document, the commands and\n * the history are `@vitral/core`'s, the text itself is its view over a\n * `contenteditable` element, and everything around them — the toolbar, the\n * panels, the floating toolbar over a selection, the menu a `/` opens and the\n * handle beside a block — is drawn here.\n */\n\nlet counter = 0;\n\nexport function createTextEditor(element: HTMLElement, config: TextEditorConfig = {}): TextEditorHandle {\n let current: TextEditorConfig = { ...config };\n let view: EditorView | null = null;\n let contentEl: HTMLElement | null = null;\n let caretEl: HTMLElement | null = null;\n let blockSelect: SelectHandle | null = null;\n let blockSelectHost: HTMLElement | null = null;\n let link: LinkPanelState = { href: '', text: '', newTab: false, existing: false };\n const anchors = new Map<string, HTMLElement>();\n\n const id = config.id ?? `vt-texteditor-${++counter}`;\n const ids = { content: `${id}-content`, help: `${id}-help`, slash: `${id}-slash`, block: `${id}-block` };\n const root = createRoot(element);\n const locale = (): Locale => current.locale ?? en;\n const palette = (): readonly EditorColor[] => current.palette ?? editorPalette;\n const part = partResolver({\n style: editorStyle,\n unstyled: () => !!current.unstyled,\n classes: () => current.classes,\n pt: () => current.pt,\n props: () => current as Record<string, unknown>\n });\n\n if (!config.unstyled) {\n const options = { nonce: config.nonce, cssLayer: config.cssLayer };\n loadStyle(baseStyle.name, baseStyle.css, options);\n loadStyle(buttonStyle.name, buttonStyle.css, options);\n loadStyle(editorStyle.name, editorStyle.css, options);\n }\n // The toolbar holds its own definitions, but the slash menu, the block\n // handle and any command an application names its own icon for are looked\n // up by name: the editor puts its set in the registry rather than drawing\n // a bar of empty buttons in an application that never registered them.\n // What each button is, shown the way every other Vitral control shows it.\n const tooltips = createTooltips(() => ({\n placement: 'bottom',\n unstyled: current.unstyled,\n nonce: current.nonce,\n cssLayer: current.cssLayer,\n zIndex: current.zIndex,\n pt: current.pt\n }));\n\n registerIcons(editorIcons);\n\n const editor: EditorInstance = createEditorInstance({\n content: current.content ?? '',\n maxLength: current.maxLength ?? null,\n historyDelay: current.historyDelay\n });\n\n const editable = () => !current.readonly && !current.disabled;\n const collapsed = () => editorSelectionRange(editor.state.selection).empty;\n const run = (name: string, ...args: unknown[]) => (editor.run as (n: string, ...a: unknown[]) => boolean)(name, ...args);\n const can = (name: string, ...args: unknown[]) => (editor.can as (n: string, ...a: unknown[]) => boolean)(name, ...args);\n\n // ---- what the outside hears ---------------------------------------------------------\n\n const stop = editor.subscribe((update) => {\n if (update.docChanged) {\n current.on?.change?.({ html: editor.getHTML(), json: editor.getJSON(), text: editor.getText() });\n }\n if (update.selectionChanged) {\n current.on?.['selection-change']?.({ empty: collapsed(), source: update.origin === 'user' ? 'user' : update.origin === 'history' ? 'history' : 'api' });\n }\n render();\n });\n\n // ---- the panels ------------------------------------------------------------------------\n\n const panelContext = (): PanelContext => ({ locale: locale(), part, ids });\n\n const anchorFor = (name: string) => () => anchors.get(name) ?? caretEl;\n\n function panel(name: string, render: () => ReturnType<typeof linkPanelView> | null, placement: 'bottom-start' | 'bottom-end' = 'bottom-start'): Overlay {\n return createOverlay({\n anchor: anchorFor(name),\n render,\n placement,\n target: () => current.overlayTarget,\n zIndex: current.zIndex,\n onClose: () => renderPanels()\n });\n }\n\n const linkPanel = panel('link', () => linkPanelView(panelContext(), link, linkActions));\n const imagePanel = panel('image', () => imagePanelView(panelContext(), imageActions));\n const tablePanel = panel('table', () => tablePanelView(panelContext(), { insert: insertTable }));\n const colorPanel = panel('color', () => colorPanelView(panelContext(), { kind: colorKind, palette: palette(), chosen: colorOf(colorKind), onChoose: chooseColor }));\n let colorKind: 'color' | 'highlight' = 'color';\n\n const linkActions = {\n change: (patch: Partial<LinkPanelState>) => {\n link = { ...link, ...patch, error: undefined };\n linkPanel.update();\n },\n apply: () => {\n const href = link.href.trim();\n if (!href) {\n link = { ...link, error: locale().editor.invalidUrl };\n linkPanel.update();\n return;\n }\n run('setLink', href, { target: link.newTab ? '_blank' : null, text: link.text.trim() || undefined });\n linkPanel.close();\n focus();\n },\n remove: () => {\n run('unsetLink');\n linkPanel.close();\n focus();\n },\n cancel: () => {\n linkPanel.close();\n focus();\n }\n };\n\n const imageActions = {\n insert: (src: string, alt: string) => {\n if (src.trim()) run('insertImage', { src: src.trim(), alt: alt.trim() });\n imagePanel.close();\n focus();\n },\n cancel: () => {\n imagePanel.close();\n focus();\n }\n };\n\n function insertTable(rows: number, columns: number) {\n run('insertTable', rows, columns, true);\n tablePanel.close();\n focus();\n }\n\n const colorOf = (kind: 'color' | 'highlight'): EditorColor | undefined => {\n const mark = activeEditorMark(editor.state, kind);\n const value = mark?.attrs?.color;\n if (typeof value !== 'string') return undefined;\n const name = matchEditorPaletteColor(value, kind, palette());\n return palette().find((color) => color.name === name);\n };\n\n function chooseColor(color: EditorColor | null) {\n run(colorKind === 'color' ? 'setColor' : 'setHighlight', color?.name ?? null);\n colorPanel.close();\n focus();\n }\n\n // ---- the floating toolbar over a selection -----------------------------------------------\n\n const bubble = createOverlay({\n anchor: () => caretEl,\n render: () => bubbleView(toolbarContext(), bubbleItems()) as never,\n placement: 'top',\n target: () => current.overlayTarget,\n zIndex: current.zIndex,\n restoreFocus: false\n });\n\n const bubbleItems = (): EditorToolbarItem[] => (Array.isArray(current.bubbleMenu) ? current.bubbleMenu : defaultBubbleMenu);\n\n function syncBubble() {\n const wanted = current.bubbleMenu !== false && editable() && !collapsed() && !editor.isEmpty();\n if (wanted) {\n placeCaret();\n bubble.open();\n bubble.update();\n } else bubble.close();\n }\n\n // ---- the menu a slash opens, and the handle beside a block ---------------------------------\n\n const slashMenu = createSlashMenu({\n editor,\n content: () => contentEl,\n anchor: () => caretEl,\n commands: () => (Array.isArray(current.slashMenu) ? current.slashMenu : defaultSlashCommands(locale())),\n part,\n locale,\n enabled: () => current.slashMenu !== false && editable(),\n place: placeCaret,\n ids,\n overlayTarget: current.overlayTarget,\n zIndex: current.zIndex,\n onRun: (command) => {\n if (command.run) command.run(handle);\n else if (command.command) run(command.command[0], ...command.command.slice(1));\n focus();\n }\n });\n\n const blockHandle = createBlockHandle({\n editor,\n host: () => element,\n caretRect: () => view?.selectionRect() ?? null,\n actions: () => (Array.isArray(current.blockMenu) ? current.blockMenu : defaultBlockActions(locale())),\n part,\n locale,\n enabled: () => current.blockMenu !== false && editable(),\n ids,\n overlayTarget: current.overlayTarget,\n zIndex: current.zIndex\n });\n\n // ---- where a popup hangs from ---------------------------------------------------------------\n\n /** The caret's own rectangle, as an element popups can be anchored to. */\n function placeCaret() {\n if (!caretEl || !contentEl) return;\n const rect = view?.selectionRect();\n if (!rect) return;\n const host = element.getBoundingClientRect();\n caretEl.style.top = `${rect.top - host.top}px`;\n caretEl.style.left = `${rect.left - host.left}px`;\n caretEl.style.width = `${Math.max(1, rect.width)}px`;\n caretEl.style.height = `${rect.height}px`;\n }\n\n function openPanel(overlay: Overlay, name: string, event?: MouseEvent) {\n if (event) anchors.set(name, event.currentTarget as HTMLElement);\n closePanels(overlay);\n overlay.open();\n overlay.update();\n }\n\n const panels = () => [linkPanel, imagePanel, tablePanel, colorPanel];\n const closePanels = (except?: Overlay) => panels().forEach((overlay) => overlay !== except && overlay.close());\n const renderPanels = () => panels().forEach((overlay) => overlay.update());\n\n // ---- the toolbar -----------------------------------------------------------------------------\n\n function toolbarContext(): ToolbarContext {\n return {\n locale: locale(),\n part,\n editable: editable(),\n can,\n isActive: (name, attrs) => editor.isActive(name, attrs as never),\n shortcut: (name, args) => {\n const binding = editorShortcutFor(name, args);\n return binding ? { label: formatEditorShortcut(binding), aria: ariaEditorShortcut(binding) } : undefined;\n },\n colors: { text: colorOf('color'), highlight: colorOf('highlight') },\n ref: (name) => (el) => {\n if (el) anchors.set(name, el as HTMLElement);\n else anchors.delete(name);\n },\n on: {\n tip: (element, text) => tooltips.attach(element, text),\n command: (name, args, event) => {\n run(name, ...args);\n if (event.detail > 0) focus();\n },\n openLink: (event) => {\n link = { href: '', text: '', newTab: false, existing: editor.isActive('link') };\n openPanel(linkPanel, 'link', event);\n },\n openImage: (event) => openPanel(imagePanel, 'image', event),\n openTable: (event) => openPanel(tablePanel, 'table', event),\n openColor: (kind, event) => {\n colorKind = kind;\n openPanel(colorPanel, kind, event);\n },\n blockSelect: (el) => {\n blockSelectHost = (el as HTMLElement | null) ?? null;\n syncBlockSelect();\n }\n }\n };\n }\n\n /** The block type, drawn by the control kit's select. */\n function syncBlockSelect() {\n if (!blockSelectHost) return;\n const words = locale().editor;\n const options = [\n { label: words.paragraph, value: 'paragraph' },\n { label: words.heading1, value: 'heading1' },\n { label: words.heading2, value: 'heading2' },\n { label: words.heading3, value: 'heading3' },\n { label: words.codeBlock, value: 'codeBlock' }\n ];\n const active = activeEditorBlockType(editor.state);\n const settings = {\n options,\n optionValue: 'value',\n value: active,\n size: 'small' as const,\n ariaLabel: words.blockType,\n disabled: !editable(),\n locale: locale(),\n unstyled: current.unstyled,\n overlayTarget: current.overlayTarget,\n zIndex: current.zIndex,\n nonce: current.nonce,\n cssLayer: current.cssLayer\n };\n if (blockSelect) blockSelect.update(settings);\n else\n blockSelect = createSelect(blockSelectHost, {\n ...settings,\n onChange: (value) => {\n if (value === 'paragraph') run('setParagraph');\n else if (value === 'codeBlock') run('toggleCodeBlock');\n else run('toggleHeading', Number(String(value).slice(-1)));\n focus();\n }\n });\n }\n\n // ---- drawing --------------------------------------------------------------------------------\n\n const toolbarGroups = (): EditorToolbarItem[][] => (Array.isArray(current.toolbar) ? current.toolbar : defaultToolbar);\n\n function countText(): string {\n const words = locale().editor;\n const count = editor.characterCount();\n if (current.maxLength) return formatMessage(words.charactersLimit, { count, limit: current.maxLength });\n return formatMessage(count === 1 ? words.character : words.characters, { count });\n }\n\n function contentView(): Child {\n return h(\n 'div',\n mergeAttrs({ key: 'content', id: ids.content }, part('content'), {\n class: isEmptyEditorDoc(editor.state.doc) ? 'vt-editor-content-empty' : undefined,\n role: 'textbox',\n 'aria-multiline': 'true',\n 'aria-label': current.ariaLabelledby ? undefined : current.ariaLabel,\n 'aria-labelledby': current.ariaLabelledby,\n 'aria-describedby': ids.help,\n 'aria-readonly': current.readonly ? 'true' : undefined,\n 'aria-disabled': current.disabled ? 'true' : undefined,\n 'aria-invalid': current.invalid ? 'true' : undefined,\n 'aria-placeholder': current.placeholder || undefined,\n 'data-placeholder': current.placeholder || undefined,\n contenteditable: editable() ? 'true' : 'false',\n tabindex: current.disabled ? '-1' : '0',\n spellcheck: 'true',\n translate: 'no',\n ref: (el: Element | null) => {\n const next = el as HTMLElement | null;\n if (next === contentEl) return;\n contentEl = next;\n attachView();\n },\n onFocus: (event: FocusEvent) => current.on?.focus?.(event),\n onBlur: (event: FocusEvent) => current.on?.blur?.(event)\n })\n );\n }\n\n function render() {\n const drawn = toolbarContext();\n root.attrs(part('root', { readonly: current.readonly, disabled: current.disabled, invalid: current.invalid }));\n root.render([\n current.hooks?.toolbar ? (current.hooks.toolbar() as Child) : current.toolbar === false ? null : toolbarView(drawn, toolbarGroups()),\n contentView(),\n h('span', mergeAttrs({ key: 'caret', 'aria-hidden': 'true' }, part('caret'), { ref: (el: Element | null) => (caretEl = el as HTMLElement | null) })),\n h('span', { key: 'help', id: ids.help, style: visuallyHidden }, locale().editor.keyboardHelp),\n current.hooks?.footer\n ? (current.hooks.footer() as Child)\n : current.showCount\n ? h('div', mergeAttrs({ key: 'footer' }, part('footer')), h('span', part('count', { limit: !!current.maxLength }), countText()))\n : null\n ]);\n syncBlockSelect();\n syncBubble();\n renderPanels();\n blockHandle.sync();\n }\n\n function attachView() {\n view?.destroy();\n view = null;\n if (!contentEl) return;\n view = createEditorView(contentEl, editor, {\n editable,\n taskLabel: locale().editor.taskDone,\n selectedClass: 'vt-editor-node-selected',\n emptyClass: 'vt-editor-content-empty'\n });\n if (current.autofocus) view.focus();\n }\n\n const focus = () => view?.focus();\n\n render();\n\n const handle: TextEditorHandle = {\n element,\n update(next) {\n const content = 'content' in next;\n current = { ...current, ...next };\n if (content) editor.setContent(current.content ?? '', { emit: false });\n if ('maxLength' in next) editor.maxLength = current.maxLength ?? null;\n render();\n },\n getHTML: () => editor.getHTML(),\n getJSON: () => editor.getJSON(),\n getText: () => editor.getText(),\n getMarkdown: () => editor.getMarkdown(),\n typeText: (text) => {\n const typed = editor.typeText(text);\n render();\n return typed;\n },\n setContent: (value) => {\n editor.setContent(value);\n render();\n },\n run: (name, ...args) => {\n const applied = run(name, ...args);\n render();\n return applied;\n },\n can,\n isActive: (name, attrs) => editor.isActive(name, attrs as never),\n focus,\n refresh: render,\n destroy() {\n stop();\n slashMenu.destroy();\n blockHandle.destroy();\n [...panels(), bubble].forEach((overlay) => overlay.destroy());\n blockSelect?.destroy();\n tooltips.destroy();\n view?.destroy();\n root.clear();\n }\n };\n\n return handle;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0CA,IAAI,UAAU;AAEd,SAAgB,iBAAiB,SAAsB,SAA2B,CAAC,GAAqB;CACpG,IAAI,UAA4B,EAAE,GAAG,OAAO;CAC5C,IAAI,OAA0B;CAC9B,IAAI,YAAgC;CACpC,IAAI,UAA8B;CAClC,IAAI,cAAmC;CACvC,IAAI,kBAAsC;CAC1C,IAAI,OAAuB;EAAE,MAAM;EAAI,MAAM;EAAI,QAAQ;EAAO,UAAU;CAAM;CAChF,MAAM,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,SAAS;EACT,UAAU;GACN,KAAK;GACL,UAAU,QAAQ;GAClB,YAAY,QAAQ;GACpB,CAAC,GAAG,OAAO,GAAG,MAAM,CAAC,CAAC,SAAS,YAAY,QAAQ,QAAQ,CAAC;GAC5D,aAAa,QAAQ;GACrB,SAAS,QAAQ;GACjB,MAAM,QAAQ;GACd,KAAK,MAAM;EACf;CACJ;CAEA,OAAO;AACX"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@vitral/editor`: a rich text editor with no framework in it. The document,
|
|
3
|
+
* the commands and the history are `@vitral/core`'s; this package is the
|
|
4
|
+
* interface around them — the toolbar, the panels, the floating toolbar over a
|
|
5
|
+
* selection, the menu a `/` opens and the handle beside a block.
|
|
6
|
+
*/
|
|
7
|
+
export { createTextEditor } from './editor';
|
|
8
|
+
export { createSlashMenu, defaultSlashCommands, type SlashMenu, type SlashMenuOptions } from './slash';
|
|
9
|
+
export { createBlockHandle, defaultBlockActions, type BlockHandle, type BlockHandleOptions } from './block';
|
|
10
|
+
export { editorButtons, editorIcons, shortcutCommands, defaultToolbar, defaultBubbleMenu, type EditorButtonSpec } from './buttons';
|
|
11
|
+
export * from './types';
|
|
12
|
+
export { toolbarView, bubbleView, buttonView, iconView, type ToolbarActions, type ToolbarContext } from './render/toolbar';
|
|
13
|
+
export { blockHandleView, blockMenuView, colorPanelView, imagePanelView, linkPanelView, slashMenuView, tablePanelView, type LinkPanelActions, type LinkPanelState, type PanelContext, type SlashMenuState } from './render/menus';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { defaultBubbleMenu, defaultToolbar, editorButtons, editorIcons, shortcutCommands } from "./buttons.js";
|
|
2
|
+
import { bubbleView, buttonView, iconView, toolbarView } from "./render/toolbar.js";
|
|
3
|
+
import { blockHandleView, blockMenuView, colorPanelView, imagePanelView, linkPanelView, slashMenuView, tablePanelView } from "./render/menus.js";
|
|
4
|
+
import { createBlockHandle, defaultBlockActions } from "./block.js";
|
|
5
|
+
import { createSlashMenu, defaultSlashCommands } from "./slash.js";
|
|
6
|
+
import { createTextEditor } from "./editor.js";
|
|
7
|
+
export { blockHandleView, blockMenuView, bubbleView, buttonView, colorPanelView, createBlockHandle, createSlashMenu, createTextEditor, defaultBlockActions, defaultBubbleMenu, defaultSlashCommands, defaultToolbar, editorButtons, editorIcons, iconView, imagePanelView, linkPanelView, shortcutCommands, slashMenuView, tablePanelView, toolbarView };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type EditorColor, type EditorNode, type Locale } from '@vitral/core';
|
|
2
|
+
import { type Child, type Props, type VElement } from '@vitral/dom';
|
|
3
|
+
import type { BlockAction, SlashCommand } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* The panels the toolbar opens, and the two menus the text itself opens: the
|
|
6
|
+
* one a `/` brings up in an empty block, and the one the handle beside a block
|
|
7
|
+
* offers. All of them are listboxes or dialogs the handle puts in an overlay.
|
|
8
|
+
*/
|
|
9
|
+
export interface PanelContext {
|
|
10
|
+
locale: Locale;
|
|
11
|
+
part: (name: string, state?: unknown) => Props;
|
|
12
|
+
ids: {
|
|
13
|
+
slash: string;
|
|
14
|
+
block: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface LinkPanelState {
|
|
18
|
+
href: string;
|
|
19
|
+
text: string;
|
|
20
|
+
newTab: boolean;
|
|
21
|
+
/** Editing a link that is already there, rather than making one. */
|
|
22
|
+
existing: boolean;
|
|
23
|
+
error?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface LinkPanelActions {
|
|
26
|
+
change: (patch: Partial<LinkPanelState>) => void;
|
|
27
|
+
apply: () => void;
|
|
28
|
+
remove: () => void;
|
|
29
|
+
cancel: () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function linkPanelView(context: PanelContext, state: LinkPanelState, on: LinkPanelActions): VElement;
|
|
32
|
+
export declare function colorPanelView(context: PanelContext, options: {
|
|
33
|
+
kind: 'color' | 'highlight';
|
|
34
|
+
palette: readonly EditorColor[];
|
|
35
|
+
chosen?: EditorColor;
|
|
36
|
+
onChoose: (color: EditorColor | null) => void;
|
|
37
|
+
}): VElement;
|
|
38
|
+
export declare function tablePanelView(context: PanelContext, on: {
|
|
39
|
+
insert: (rows: number, columns: number) => void;
|
|
40
|
+
}): VElement;
|
|
41
|
+
export declare function imagePanelView(context: PanelContext, on: {
|
|
42
|
+
insert: (src: string, alt: string) => void;
|
|
43
|
+
cancel: () => void;
|
|
44
|
+
}): VElement;
|
|
45
|
+
export interface SlashMenuState {
|
|
46
|
+
query: string;
|
|
47
|
+
items: SlashCommand[];
|
|
48
|
+
active: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* What a `/` opens: a listbox of commands, filtered as it is typed. The text
|
|
52
|
+
* keeps the keyboard — the caret is still in the document — so the list is
|
|
53
|
+
* pointed at through `aria-activedescendant` rather than focused.
|
|
54
|
+
*/
|
|
55
|
+
export declare function slashMenuView(context: PanelContext, state: SlashMenuState, on: {
|
|
56
|
+
choose: (index: number) => void;
|
|
57
|
+
}): VElement;
|
|
58
|
+
export declare function blockMenuView(context: PanelContext, block: EditorNode, actions: BlockAction[], on: {
|
|
59
|
+
choose: (id: string) => void;
|
|
60
|
+
}): VElement;
|
|
61
|
+
/** The handle beside the block the caret is in: it drags nothing yet, it opens the menu. */
|
|
62
|
+
export declare function blockHandleView(context: PanelContext, on: {
|
|
63
|
+
open: (event: MouseEvent) => void;
|
|
64
|
+
}, position: {
|
|
65
|
+
top: number;
|
|
66
|
+
left: number;
|
|
67
|
+
}): Child;
|