@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
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { iconView } from "./toolbar.js";
|
|
2
|
+
import { formatMessage } from "@vitral/core";
|
|
3
|
+
import { h, mergeAttrs } from "@vitral/dom";
|
|
4
|
+
//#region src/render/menus.ts
|
|
5
|
+
function linkPanelView(context, state, on) {
|
|
6
|
+
const { part, locale } = context;
|
|
7
|
+
const words = locale.editor;
|
|
8
|
+
const field = (label, value, key, type = "text") => h("label", mergeAttrs({ key }, part("field")), h("span", part("fieldLabel"), label), h("span", { class: "vt-field vt-field-sm vt-field-fluid" }, h("input", mergeAttrs({
|
|
9
|
+
type,
|
|
10
|
+
class: "vt-field-input"
|
|
11
|
+
}, {
|
|
12
|
+
value,
|
|
13
|
+
onInput: (event) => on.change({ [key]: event.target.value }),
|
|
14
|
+
onKeydown: (event) => {
|
|
15
|
+
if (event.key !== "Enter") return;
|
|
16
|
+
event.preventDefault();
|
|
17
|
+
on.apply();
|
|
18
|
+
}
|
|
19
|
+
}))));
|
|
20
|
+
return h("div", mergeAttrs({ role: "dialog" }, part("panel"), { "aria-label": words.link }), field(words.linkUrl, state.href, "href", "url"), field(words.linkText, state.text, "text"), h("label", mergeAttrs({ key: "newtab" }, part("check")), h("input", {
|
|
21
|
+
type: "checkbox",
|
|
22
|
+
checked: state.newTab,
|
|
23
|
+
onChange: (event) => on.change({ newTab: event.target.checked })
|
|
24
|
+
}), h("span", null, words.openInNewTab)), state.error ? h("span", mergeAttrs({
|
|
25
|
+
key: "error",
|
|
26
|
+
role: "alert"
|
|
27
|
+
}, part("fieldError")), state.error) : null, h("div", mergeAttrs({ key: "actions" }, part("actions")), state.existing ? h("button", mergeAttrs({
|
|
28
|
+
key: "remove",
|
|
29
|
+
type: "button",
|
|
30
|
+
class: "vt-button vt-button-text vt-button-secondary vt-button-sm"
|
|
31
|
+
}, { onClick: on.remove }), words.removeLink) : null, h("button", mergeAttrs({
|
|
32
|
+
key: "cancel",
|
|
33
|
+
type: "button",
|
|
34
|
+
class: "vt-button vt-button-text vt-button-secondary vt-button-sm"
|
|
35
|
+
}, { onClick: on.cancel }), locale.cancel), h("button", mergeAttrs({
|
|
36
|
+
key: "apply",
|
|
37
|
+
type: "button",
|
|
38
|
+
class: "vt-button vt-button-sm"
|
|
39
|
+
}, { onClick: on.apply }), words.apply)));
|
|
40
|
+
}
|
|
41
|
+
function colorPanelView(context, options) {
|
|
42
|
+
const { part, locale } = context;
|
|
43
|
+
const words = locale.editor;
|
|
44
|
+
const none = options.kind === "color" ? words.defaultColor : words.noHighlight;
|
|
45
|
+
const nameOf = (color) => words.colors[color.name] ?? color.name;
|
|
46
|
+
return h("div", mergeAttrs({ role: "dialog" }, part("panel"), { "aria-label": options.kind === "color" ? words.textColor : words.highlight }), h("div", mergeAttrs({
|
|
47
|
+
key: "swatches",
|
|
48
|
+
role: "group"
|
|
49
|
+
}, part("swatches")), [h("button", mergeAttrs({
|
|
50
|
+
key: "none",
|
|
51
|
+
type: "button"
|
|
52
|
+
}, part("swatch", {
|
|
53
|
+
none: true,
|
|
54
|
+
kind: options.kind,
|
|
55
|
+
selected: !options.chosen
|
|
56
|
+
}), {
|
|
57
|
+
"aria-label": none,
|
|
58
|
+
title: none,
|
|
59
|
+
onClick: () => options.onChoose(null)
|
|
60
|
+
})), ...options.palette.map((color) => h("button", mergeAttrs({
|
|
61
|
+
key: color.name,
|
|
62
|
+
type: "button"
|
|
63
|
+
}, part("swatch", {
|
|
64
|
+
kind: options.kind,
|
|
65
|
+
selected: options.chosen?.name === color.name
|
|
66
|
+
}), {
|
|
67
|
+
"aria-label": nameOf(color),
|
|
68
|
+
title: nameOf(color),
|
|
69
|
+
style: { background: options.kind === "color" ? color.text : color.highlight },
|
|
70
|
+
onClick: () => options.onChoose(color)
|
|
71
|
+
})))]));
|
|
72
|
+
}
|
|
73
|
+
function tablePanelView(context, on) {
|
|
74
|
+
const { part, locale } = context;
|
|
75
|
+
const words = locale.editor;
|
|
76
|
+
const number = (label, name, value) => h("label", mergeAttrs({ key: name }, part("field")), h("span", part("fieldLabel"), label), h("span", { class: "vt-field vt-field-sm" }, h("input", {
|
|
77
|
+
type: "number",
|
|
78
|
+
min: "1",
|
|
79
|
+
max: "20",
|
|
80
|
+
value: String(value),
|
|
81
|
+
class: "vt-field-input",
|
|
82
|
+
"data-vt-table": name
|
|
83
|
+
})));
|
|
84
|
+
const read = (element, name, fallback) => {
|
|
85
|
+
const input = element?.querySelector(`[data-vt-table="${name}"]`);
|
|
86
|
+
const value = Number(input?.value);
|
|
87
|
+
return Number.isFinite(value) && value > 0 ? Math.min(20, Math.round(value)) : fallback;
|
|
88
|
+
};
|
|
89
|
+
return h("div", mergeAttrs({ role: "dialog" }, part("panel"), { "aria-label": words.table }), number(words.tableRows, "rows", 3), number(words.tableColumns, "columns", 3), h("div", mergeAttrs({ key: "actions" }, part("actions")), h("button", mergeAttrs({
|
|
90
|
+
type: "button",
|
|
91
|
+
class: "vt-button vt-button-sm"
|
|
92
|
+
}, { onClick: (event) => {
|
|
93
|
+
const panel = event.currentTarget.closest("[role=\"dialog\"]");
|
|
94
|
+
on.insert(read(panel, "rows", 3), read(panel, "columns", 3));
|
|
95
|
+
} }), words.insertTable)));
|
|
96
|
+
}
|
|
97
|
+
function imagePanelView(context, on) {
|
|
98
|
+
const { part, locale } = context;
|
|
99
|
+
const words = locale.editor;
|
|
100
|
+
const field = (label, name, type = "text") => h("label", mergeAttrs({ key: name }, part("field")), h("span", part("fieldLabel"), label), h("span", { class: "vt-field vt-field-sm vt-field-fluid" }, h("input", {
|
|
101
|
+
type,
|
|
102
|
+
class: "vt-field-input",
|
|
103
|
+
"data-vt-image": name
|
|
104
|
+
})));
|
|
105
|
+
const read = (element, name) => element?.querySelector(`[data-vt-image="${name}"]`)?.value ?? "";
|
|
106
|
+
return h("div", mergeAttrs({ role: "dialog" }, part("panel"), { "aria-label": words.image }), field(words.imageUrl, "src", "url"), field(words.imageAlt, "alt"), h("div", mergeAttrs({ key: "actions" }, part("actions")), h("button", mergeAttrs({
|
|
107
|
+
key: "cancel",
|
|
108
|
+
type: "button",
|
|
109
|
+
class: "vt-button vt-button-text vt-button-secondary vt-button-sm"
|
|
110
|
+
}, { onClick: on.cancel }), locale.cancel), h("button", mergeAttrs({
|
|
111
|
+
key: "insert",
|
|
112
|
+
type: "button",
|
|
113
|
+
class: "vt-button vt-button-sm"
|
|
114
|
+
}, { onClick: (event) => {
|
|
115
|
+
const panel = event.currentTarget.closest("[role=\"dialog\"]");
|
|
116
|
+
on.insert(read(panel, "src"), read(panel, "alt"));
|
|
117
|
+
} }), words.insert)));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* What a `/` opens: a listbox of commands, filtered as it is typed. The text
|
|
121
|
+
* keeps the keyboard — the caret is still in the document — so the list is
|
|
122
|
+
* pointed at through `aria-activedescendant` rather than focused.
|
|
123
|
+
*/
|
|
124
|
+
function slashMenuView(context, state, on) {
|
|
125
|
+
const { part, locale } = context;
|
|
126
|
+
const optionId = (index) => `${context.ids.slash}-option-${index}`;
|
|
127
|
+
return h("div", mergeAttrs({ id: context.ids.slash }, part("slashMenu")), state.items.length ? h("ul", mergeAttrs({ role: "listbox" }, part("slashList"), { "aria-label": locale.editor.slashMenu }), state.items.map((item, index) => h("li", mergeAttrs({
|
|
128
|
+
key: item.id,
|
|
129
|
+
id: optionId(index),
|
|
130
|
+
role: "option"
|
|
131
|
+
}, part("slashItem", { focused: index === state.active }), {
|
|
132
|
+
"aria-selected": index === state.active ? "true" : "false",
|
|
133
|
+
onMousedown: (event) => event.preventDefault(),
|
|
134
|
+
onClick: () => on.choose(index),
|
|
135
|
+
onMousemove: () => {
|
|
136
|
+
if (state.active === index) return;
|
|
137
|
+
state.active = index;
|
|
138
|
+
}
|
|
139
|
+
}), item.icon ? iconView(item.icon, part("slashIcon")) : null, h("span", part("slashText"), h("span", part("slashLabel"), item.label), item.description ? h("span", part("slashDescription"), item.description) : null)))) : h("div", mergeAttrs({ role: "status" }, part("slashEmpty")), formatMessage(locale.editor.slashEmpty, { query: state.query })));
|
|
140
|
+
}
|
|
141
|
+
function blockMenuView(context, block, actions, on) {
|
|
142
|
+
const { part, locale } = context;
|
|
143
|
+
return h("div", mergeAttrs({
|
|
144
|
+
id: context.ids.block,
|
|
145
|
+
role: "menu"
|
|
146
|
+
}, part("blockMenu"), { "aria-label": locale.editor.blockMenu }), actions.filter((action) => !action.when || action.when(block)).map((action) => h("button", mergeAttrs({
|
|
147
|
+
key: action.id,
|
|
148
|
+
type: "button",
|
|
149
|
+
role: "menuitem"
|
|
150
|
+
}, part("blockItem"), { onClick: () => on.choose(action.id) }), action.icon ? iconView(action.icon, part("blockIcon")) : null, h("span", null, action.label))));
|
|
151
|
+
}
|
|
152
|
+
/** The handle beside the block the caret is in: it drags nothing yet, it opens the menu. */
|
|
153
|
+
function blockHandleView(context, on, position) {
|
|
154
|
+
const { part, locale } = context;
|
|
155
|
+
return h("button", mergeAttrs({
|
|
156
|
+
key: "block-handle",
|
|
157
|
+
type: "button"
|
|
158
|
+
}, part("blockHandle"), {
|
|
159
|
+
"aria-label": locale.editor.blockMenu,
|
|
160
|
+
"aria-haspopup": "menu",
|
|
161
|
+
style: {
|
|
162
|
+
top: `${position.top}px`,
|
|
163
|
+
left: `${position.left}px`
|
|
164
|
+
},
|
|
165
|
+
onMousedown: (event) => event.preventDefault(),
|
|
166
|
+
onClick: on.open
|
|
167
|
+
}), iconView("grip", part("blockHandleIcon")));
|
|
168
|
+
}
|
|
169
|
+
//#endregion
|
|
170
|
+
export { blockHandleView, blockMenuView, colorPanelView, imagePanelView, linkPanelView, slashMenuView, tablePanelView };
|
|
171
|
+
|
|
172
|
+
//# sourceMappingURL=menus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"menus.js","names":[],"sources":["../../src/render/menus.ts"],"sourcesContent":["import { formatMessage, type EditorColor, type EditorNode, type Locale } from '@vitral/core';\nimport { h, mergeAttrs, type Child, type Props, type VElement } from '@vitral/dom';\nimport type { BlockAction, SlashCommand } from '../types';\nimport { iconView } from './toolbar';\n\n/**\n * The panels the toolbar opens, and the two menus the text itself opens: the\n * one a `/` brings up in an empty block, and the one the handle beside a block\n * offers. All of them are listboxes or dialogs the handle puts in an overlay.\n */\n\nexport interface PanelContext {\n locale: Locale;\n part: (name: string, state?: unknown) => Props;\n ids: { slash: string; block: string };\n}\n\n// ---- the link panel ---------------------------------------------------------------\n\nexport interface LinkPanelState {\n href: string;\n text: string;\n newTab: boolean;\n /** Editing a link that is already there, rather than making one. */\n existing: boolean;\n error?: string;\n}\n\nexport interface LinkPanelActions {\n change: (patch: Partial<LinkPanelState>) => void;\n apply: () => void;\n remove: () => void;\n cancel: () => void;\n}\n\nexport function linkPanelView(context: PanelContext, state: LinkPanelState, on: LinkPanelActions): VElement {\n const { part, locale } = context;\n const words = locale.editor;\n const field = (label: string, value: string, key: 'href' | 'text', type = 'text') =>\n h(\n 'label',\n mergeAttrs({ key }, part('field')),\n h('span', part('fieldLabel'), label),\n h(\n 'span',\n { class: 'vt-field vt-field-sm vt-field-fluid' },\n h(\n 'input',\n mergeAttrs({ type, class: 'vt-field-input' }, {\n value,\n onInput: (event: Event) => on.change({ [key]: (event.target as HTMLInputElement).value } as Partial<LinkPanelState>),\n onKeydown: (event: KeyboardEvent) => {\n if (event.key !== 'Enter') return;\n event.preventDefault();\n on.apply();\n }\n })\n )\n )\n );\n return h(\n 'div',\n mergeAttrs({ role: 'dialog' }, part('panel'), { 'aria-label': words.link }),\n field(words.linkUrl, state.href, 'href', 'url'),\n field(words.linkText, state.text, 'text'),\n h(\n 'label',\n mergeAttrs({ key: 'newtab' }, part('check')),\n h('input', { type: 'checkbox', checked: state.newTab, onChange: (event: Event) => on.change({ newTab: (event.target as HTMLInputElement).checked }) }),\n h('span', null, words.openInNewTab)\n ),\n state.error ? h('span', mergeAttrs({ key: 'error', role: 'alert' }, part('fieldError')), state.error) : null,\n h(\n 'div',\n mergeAttrs({ key: 'actions' }, part('actions')),\n state.existing\n ? h('button', mergeAttrs({ key: 'remove', type: 'button', class: 'vt-button vt-button-text vt-button-secondary vt-button-sm' }, { onClick: on.remove }), words.removeLink)\n : null,\n h('button', mergeAttrs({ key: 'cancel', type: 'button', class: 'vt-button vt-button-text vt-button-secondary vt-button-sm' }, { onClick: on.cancel }), locale.cancel),\n h('button', mergeAttrs({ key: 'apply', type: 'button', class: 'vt-button vt-button-sm' }, { onClick: on.apply }), words.apply)\n )\n ) as VElement;\n}\n\n// ---- the colour swatches ------------------------------------------------------------\n\nexport function colorPanelView(\n context: PanelContext,\n options: { kind: 'color' | 'highlight'; palette: readonly EditorColor[]; chosen?: EditorColor; onChoose: (color: EditorColor | null) => void }\n): VElement {\n const { part, locale } = context;\n const words = locale.editor;\n const none = options.kind === 'color' ? words.defaultColor : words.noHighlight;\n const nameOf = (color: EditorColor) => words.colors[color.name] ?? color.name;\n return h(\n 'div',\n mergeAttrs({ role: 'dialog' }, part('panel'), { 'aria-label': options.kind === 'color' ? words.textColor : words.highlight }),\n h(\n 'div',\n mergeAttrs({ key: 'swatches', role: 'group' }, part('swatches')),\n [\n h(\n 'button',\n mergeAttrs({ key: 'none', type: 'button' }, part('swatch', { none: true, kind: options.kind, selected: !options.chosen }), {\n 'aria-label': none,\n title: none,\n onClick: () => options.onChoose(null)\n })\n ),\n ...options.palette.map((color) =>\n h(\n 'button',\n mergeAttrs({ key: color.name, type: 'button' }, part('swatch', { kind: options.kind, selected: options.chosen?.name === color.name }), {\n 'aria-label': nameOf(color),\n title: nameOf(color),\n style: { background: options.kind === 'color' ? color.text : color.highlight },\n onClick: () => options.onChoose(color)\n })\n )\n )\n ]\n )\n ) as VElement;\n}\n\n// ---- the table picker ----------------------------------------------------------------\n\nexport function tablePanelView(context: PanelContext, on: { insert: (rows: number, columns: number) => void }): VElement {\n const { part, locale } = context;\n const words = locale.editor;\n const number = (label: string, name: 'rows' | 'columns', value: number) =>\n h(\n 'label',\n mergeAttrs({ key: name }, part('field')),\n h('span', part('fieldLabel'), label),\n h(\n 'span',\n { class: 'vt-field vt-field-sm' },\n h('input', { type: 'number', min: '1', max: '20', value: String(value), class: 'vt-field-input', 'data-vt-table': name })\n )\n );\n const read = (element: Element | null, name: string, fallback: number) => {\n const input = element?.querySelector<HTMLInputElement>(`[data-vt-table=\"${name}\"]`);\n const value = Number(input?.value);\n return Number.isFinite(value) && value > 0 ? Math.min(20, Math.round(value)) : fallback;\n };\n return h(\n 'div',\n mergeAttrs({ role: 'dialog' }, part('panel'), { 'aria-label': words.table }),\n number(words.tableRows, 'rows', 3),\n number(words.tableColumns, 'columns', 3),\n h(\n 'div',\n mergeAttrs({ key: 'actions' }, part('actions')),\n h(\n 'button',\n mergeAttrs({ type: 'button', class: 'vt-button vt-button-sm' }, {\n onClick: (event: MouseEvent) => {\n const panel = (event.currentTarget as HTMLElement).closest('[role=\"dialog\"]');\n on.insert(read(panel, 'rows', 3), read(panel, 'columns', 3));\n }\n }),\n words.insertTable\n )\n )\n ) as VElement;\n}\n\n// ---- the image panel -------------------------------------------------------------------\n\nexport function imagePanelView(context: PanelContext, on: { insert: (src: string, alt: string) => void; cancel: () => void }): VElement {\n const { part, locale } = context;\n const words = locale.editor;\n const field = (label: string, name: string, type = 'text') =>\n h(\n 'label',\n mergeAttrs({ key: name }, part('field')),\n h('span', part('fieldLabel'), label),\n h('span', { class: 'vt-field vt-field-sm vt-field-fluid' }, h('input', { type, class: 'vt-field-input', 'data-vt-image': name }))\n );\n const read = (element: Element | null, name: string) => element?.querySelector<HTMLInputElement>(`[data-vt-image=\"${name}\"]`)?.value ?? '';\n return h(\n 'div',\n mergeAttrs({ role: 'dialog' }, part('panel'), { 'aria-label': words.image }),\n field(words.imageUrl, 'src', 'url'),\n field(words.imageAlt, 'alt'),\n h(\n 'div',\n mergeAttrs({ key: 'actions' }, part('actions')),\n h('button', mergeAttrs({ key: 'cancel', type: 'button', class: 'vt-button vt-button-text vt-button-secondary vt-button-sm' }, { onClick: on.cancel }), locale.cancel),\n h(\n 'button',\n mergeAttrs({ key: 'insert', type: 'button', class: 'vt-button vt-button-sm' }, {\n onClick: (event: MouseEvent) => {\n const panel = (event.currentTarget as HTMLElement).closest('[role=\"dialog\"]');\n on.insert(read(panel, 'src'), read(panel, 'alt'));\n }\n }),\n words.insert\n )\n )\n ) as VElement;\n}\n\n// ---- the slash menu ----------------------------------------------------------------------\n\nexport interface SlashMenuState {\n query: string;\n items: SlashCommand[];\n active: number;\n}\n\n/**\n * What a `/` opens: a listbox of commands, filtered as it is typed. The text\n * keeps the keyboard — the caret is still in the document — so the list is\n * pointed at through `aria-activedescendant` rather than focused.\n */\nexport function slashMenuView(context: PanelContext, state: SlashMenuState, on: { choose: (index: number) => void }): VElement {\n const { part, locale } = context;\n const optionId = (index: number) => `${context.ids.slash}-option-${index}`;\n return h(\n 'div',\n mergeAttrs({ id: context.ids.slash }, part('slashMenu')),\n state.items.length\n ? h(\n 'ul',\n mergeAttrs({ role: 'listbox' }, part('slashList'), { 'aria-label': locale.editor.slashMenu }),\n state.items.map((item, index) =>\n h(\n 'li',\n mergeAttrs({ key: item.id, id: optionId(index), role: 'option' }, part('slashItem', { focused: index === state.active }), {\n 'aria-selected': index === state.active ? 'true' : 'false',\n onMousedown: (event: MouseEvent) => event.preventDefault(),\n onClick: () => on.choose(index),\n onMousemove: () => {\n if (state.active === index) return;\n state.active = index;\n }\n }),\n item.icon ? iconView(item.icon, part('slashIcon')) : null,\n h(\n 'span',\n part('slashText'),\n h('span', part('slashLabel'), item.label),\n item.description ? h('span', part('slashDescription'), item.description) : null\n )\n )\n )\n )\n : h('div', mergeAttrs({ role: 'status' }, part('slashEmpty')), formatMessage(locale.editor.slashEmpty, { query: state.query }))\n ) as VElement;\n}\n\n// ---- the block handle's menu ----------------------------------------------------------------\n\nexport function blockMenuView(context: PanelContext, block: EditorNode, actions: BlockAction[], on: { choose: (id: string) => void }): VElement {\n const { part, locale } = context;\n return h(\n 'div',\n mergeAttrs({ id: context.ids.block, role: 'menu' }, part('blockMenu'), { 'aria-label': locale.editor.blockMenu }),\n actions\n .filter((action) => !action.when || action.when(block))\n .map((action) =>\n h(\n 'button',\n mergeAttrs({ key: action.id, type: 'button', role: 'menuitem' }, part('blockItem'), { onClick: () => on.choose(action.id) }),\n action.icon ? iconView(action.icon, part('blockIcon')) : null,\n h('span', null, action.label)\n )\n )\n ) as VElement;\n}\n\n/** The handle beside the block the caret is in: it drags nothing yet, it opens the menu. */\nexport function blockHandleView(context: PanelContext, on: { open: (event: MouseEvent) => void }, position: { top: number; left: number }): Child {\n const { part, locale } = context;\n return h(\n 'button',\n mergeAttrs({ key: 'block-handle', type: 'button' }, part('blockHandle'), {\n 'aria-label': locale.editor.blockMenu,\n 'aria-haspopup': 'menu',\n style: { top: `${position.top}px`, left: `${position.left}px` },\n onMousedown: (event: MouseEvent) => event.preventDefault(),\n onClick: on.open\n }),\n iconView('grip', part('blockHandleIcon'))\n );\n}\n"],"mappings":";;;;AAmCA,SAAgB,cAAc,SAAu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e,GAAG,OAAO,OAAO,EAAE,EAAE,CAAC,GAC3H,OAAO,OAAO,SAAS,OAAO,MAAM,KAAK,WAAW,CAAC,IAAI,MACzD,EAAE,QAAQ,MAAM,OAAO,KAAK,CAChC,CACJ,CACR;AACJ;;AAGA,SAAgB,gBAAgB,SAAuB,IAA2C,UAAgD;CAC9I,MAAM,EAAE,MAAM,WAAW;CACzB,OAAO,EACH,UACA,WAAW;EAAE,KAAK;EAAgB,MAAM;CAAS,GAAG,KAAK,aAAa,GAAG;EACrE,cAAc,OAAO,OAAO;EAC5B,iBAAiB;EACjB,OAAO;GAAE,KAAK,GAAG,SAAS,IAAI;GAAK,MAAM,GAAG,SAAS,KAAK;EAAI;EAC9D,cAAc,UAAsB,MAAM,eAAe;EACzD,SAAS,GAAG;CAChB,CAAC,GACD,SAAS,QAAQ,KAAK,iBAAiB,CAAC,CAC5C;AACJ"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type EditorColor, type Locale } from '@vitral/core';
|
|
2
|
+
import { type Child, type Props } from '@vitral/dom';
|
|
3
|
+
import type { EditorButtonCommand, EditorToolbarItem } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* The toolbar as plain objects: a group of buttons a command each, the block
|
|
6
|
+
* type as a select, the two colour pickers, and the buttons that open a panel.
|
|
7
|
+
* What is drawn is decided here; what happens when it is pressed is the
|
|
8
|
+
* handle's.
|
|
9
|
+
*/
|
|
10
|
+
export interface ToolbarContext {
|
|
11
|
+
locale: Locale;
|
|
12
|
+
part: (name: string, state?: unknown) => Props;
|
|
13
|
+
editable: boolean;
|
|
14
|
+
/** Whether a command applies right now. */
|
|
15
|
+
can: (name: string, ...args: unknown[]) => boolean;
|
|
16
|
+
isActive: (name: string, attrs?: Record<string, unknown>) => boolean;
|
|
17
|
+
/** The shortcut for a command, as a label and as `aria-keyshortcuts`. */
|
|
18
|
+
shortcut: (name: string, args?: readonly unknown[]) => {
|
|
19
|
+
label: string;
|
|
20
|
+
aria: string;
|
|
21
|
+
} | undefined;
|
|
22
|
+
on: ToolbarActions;
|
|
23
|
+
/** The element each opener is drawn into, so a panel can hang from it. */
|
|
24
|
+
ref: (name: string) => (element: Element | null) => void;
|
|
25
|
+
colors: {
|
|
26
|
+
text?: EditorColor;
|
|
27
|
+
highlight?: EditorColor;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export interface ToolbarActions {
|
|
31
|
+
/** Gives a button the tooltip that says what it is. */
|
|
32
|
+
tip: (element: Element | null, text: string) => void;
|
|
33
|
+
command: (name: string, args: readonly unknown[], event: MouseEvent) => void;
|
|
34
|
+
openLink: (event: MouseEvent) => void;
|
|
35
|
+
openImage: (event: MouseEvent) => void;
|
|
36
|
+
openTable: (event: MouseEvent) => void;
|
|
37
|
+
openColor: (kind: 'color' | 'highlight', event: MouseEvent) => void;
|
|
38
|
+
blockSelect: (element: Element | null) => void;
|
|
39
|
+
}
|
|
40
|
+
export declare const iconView: (name: string, props?: Props) => Child;
|
|
41
|
+
/** One command's button: a toggle where the command has a pressed state. */
|
|
42
|
+
export declare function buttonView(context: ToolbarContext, item: EditorButtonCommand): Child;
|
|
43
|
+
export declare function toolbarView(context: ToolbarContext, groups: EditorToolbarItem[][]): Child;
|
|
44
|
+
/** The floating toolbar over a selection: the same buttons, in one group. */
|
|
45
|
+
export declare function bubbleView(context: ToolbarContext, items: EditorToolbarItem[]): Child;
|
|
46
|
+
export declare const shortcutLabel: (binding: {
|
|
47
|
+
key: string;
|
|
48
|
+
} | undefined) => string | undefined;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { editorButtons, shortcutCommands } from "../buttons.js";
|
|
2
|
+
import "@vitral/core";
|
|
3
|
+
import { h, iconNode, mergeAttrs } from "@vitral/dom";
|
|
4
|
+
import { getIcon } from "@vitral/icons";
|
|
5
|
+
//#region src/render/toolbar.ts
|
|
6
|
+
var iconView = (name, props) => iconNode(getIcon(name), props);
|
|
7
|
+
var OPENERS = {
|
|
8
|
+
image: {
|
|
9
|
+
icon: "image",
|
|
10
|
+
label: (words) => words.image,
|
|
11
|
+
action: "openImage"
|
|
12
|
+
},
|
|
13
|
+
table: {
|
|
14
|
+
icon: "table",
|
|
15
|
+
label: (words) => words.table,
|
|
16
|
+
action: "openTable"
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
/** One command's button: a toggle where the command has a pressed state. */
|
|
20
|
+
function buttonView(context, item) {
|
|
21
|
+
const { part, locale } = context;
|
|
22
|
+
const spec = editorButtons[item];
|
|
23
|
+
if (!spec) return null;
|
|
24
|
+
const name = spec.label(locale.editor);
|
|
25
|
+
const isLink = item === "link";
|
|
26
|
+
const pressed = spec.active ? context.isActive(spec.active[0], spec.active[1]) : void 0;
|
|
27
|
+
const [command, ...args] = spec.command;
|
|
28
|
+
const disabled = !context.editable || (isLink ? context.isActive("codeBlock") : !context.can(command, ...args));
|
|
29
|
+
const [shortcutName, ...shortcutArgs] = shortcutCommands[item] ?? spec.command;
|
|
30
|
+
const shortcut = context.shortcut(shortcutName, shortcutArgs);
|
|
31
|
+
return h("button", mergeAttrs({
|
|
32
|
+
key: item,
|
|
33
|
+
type: "button"
|
|
34
|
+
}, part("button", {
|
|
35
|
+
active: pressed,
|
|
36
|
+
disabled
|
|
37
|
+
}), {
|
|
38
|
+
"aria-label": name,
|
|
39
|
+
"aria-pressed": isLink || pressed === void 0 ? void 0 : pressed ? "true" : "false",
|
|
40
|
+
"aria-haspopup": isLink ? "dialog" : void 0,
|
|
41
|
+
"aria-keyshortcuts": shortcut?.aria,
|
|
42
|
+
disabled,
|
|
43
|
+
ref: (element) => {
|
|
44
|
+
if (isLink) context.ref("link")(element);
|
|
45
|
+
context.on.tip(element, shortcut ? `${name} (${shortcut.label})` : name);
|
|
46
|
+
},
|
|
47
|
+
onClick: (event) => isLink ? context.on.openLink(event) : context.on.command(command, args, event)
|
|
48
|
+
}), iconView(spec.icon.name, part("buttonIcon")));
|
|
49
|
+
}
|
|
50
|
+
/** A button that opens a panel: the image and table pickers. */
|
|
51
|
+
function openerView(context, item) {
|
|
52
|
+
const spec = OPENERS[item];
|
|
53
|
+
const name = spec.label(context.locale.editor);
|
|
54
|
+
return h("button", mergeAttrs({
|
|
55
|
+
key: item,
|
|
56
|
+
type: "button"
|
|
57
|
+
}, context.part("button", { disabled: !context.editable }), {
|
|
58
|
+
"aria-label": name,
|
|
59
|
+
"aria-haspopup": "dialog",
|
|
60
|
+
disabled: !context.editable,
|
|
61
|
+
ref: (element) => {
|
|
62
|
+
context.ref(item)(element);
|
|
63
|
+
context.on.tip(element, name);
|
|
64
|
+
},
|
|
65
|
+
onClick: (event) => spec.action === "openImage" ? context.on.openImage(event) : context.on.openTable(event)
|
|
66
|
+
}), iconView(spec.icon, context.part("buttonIcon")));
|
|
67
|
+
}
|
|
68
|
+
/** The text and highlight pickers: a button showing the colour in hand. */
|
|
69
|
+
function colorView(context, kind) {
|
|
70
|
+
const { part, locale } = context;
|
|
71
|
+
const name = kind === "color" ? locale.editor.textColor : locale.editor.highlight;
|
|
72
|
+
const chosen = context.colors[kind === "color" ? "text" : "highlight"];
|
|
73
|
+
return h("button", mergeAttrs({
|
|
74
|
+
key: kind,
|
|
75
|
+
type: "button"
|
|
76
|
+
}, part("button", { disabled: !context.editable }), {
|
|
77
|
+
"aria-label": name,
|
|
78
|
+
"aria-haspopup": "dialog",
|
|
79
|
+
disabled: !context.editable,
|
|
80
|
+
ref: (element) => {
|
|
81
|
+
context.ref(kind)(element);
|
|
82
|
+
context.on.tip(element, name);
|
|
83
|
+
},
|
|
84
|
+
onClick: (event) => context.on.openColor(kind, event)
|
|
85
|
+
}), iconView(kind === "color" ? "textColor" : "highlighter", part("buttonIcon")), h("span", mergeAttrs({
|
|
86
|
+
key: "bar",
|
|
87
|
+
"aria-hidden": "true"
|
|
88
|
+
}, part("colorBar"), { style: { background: chosen ? kind === "color" ? chosen.text : chosen.highlight : "transparent" } })));
|
|
89
|
+
}
|
|
90
|
+
/** The block type, as the select the control kit draws; the handle mounts it. */
|
|
91
|
+
function blockSelectView(context) {
|
|
92
|
+
return h("span", mergeAttrs({ key: "blockType" }, { ref: context.on.blockSelect }));
|
|
93
|
+
}
|
|
94
|
+
function toolbarView(context, groups) {
|
|
95
|
+
const { part, locale } = context;
|
|
96
|
+
return h("div", mergeAttrs({
|
|
97
|
+
key: "toolbar",
|
|
98
|
+
role: "toolbar"
|
|
99
|
+
}, part("toolbar"), {
|
|
100
|
+
"aria-label": locale.editor.toolbar,
|
|
101
|
+
"aria-orientation": "horizontal"
|
|
102
|
+
}), groups.map((group, index) => h("div", mergeAttrs({
|
|
103
|
+
key: index,
|
|
104
|
+
role: "group"
|
|
105
|
+
}, part("toolbarGroup")), group.map((item) => item === "blockType" ? blockSelectView(context) : item === "color" || item === "highlight" ? colorView(context, item) : item === "image" || item === "table" ? openerView(context, item) : buttonView(context, item)))));
|
|
106
|
+
}
|
|
107
|
+
/** The floating toolbar over a selection: the same buttons, in one group. */
|
|
108
|
+
function bubbleView(context, items) {
|
|
109
|
+
return h("div", mergeAttrs({ role: "toolbar" }, context.part("bubble"), { "aria-label": context.locale.editor.toolbar }), items.map((item) => item === "color" || item === "highlight" ? colorView(context, item) : item === "image" || item === "table" ? openerView(context, item) : buttonView(context, item)));
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { bubbleView, buttonView, iconView, toolbarView };
|
|
113
|
+
|
|
114
|
+
//# sourceMappingURL=toolbar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolbar.js","names":[],"sources":["../../src/render/toolbar.ts"],"sourcesContent":["import { formatEditorShortcut, type EditorColor, type Locale } from '@vitral/core';\nimport { h, iconNode, mergeAttrs, type Child, type Props } from '@vitral/dom';\nimport { getIcon } from '@vitral/icons';\nimport { editorButtons, shortcutCommands } from '../buttons';\nimport type { EditorButtonCommand, EditorToolbarItem } from '../types';\n\n/**\n * The toolbar as plain objects: a group of buttons a command each, the block\n * type as a select, the two colour pickers, and the buttons that open a panel.\n * What is drawn is decided here; what happens when it is pressed is the\n * handle's.\n */\n\nexport interface ToolbarContext {\n locale: Locale;\n part: (name: string, state?: unknown) => Props;\n editable: boolean;\n /** Whether a command applies right now. */\n can: (name: string, ...args: unknown[]) => boolean;\n isActive: (name: string, attrs?: Record<string, unknown>) => boolean;\n /** The shortcut for a command, as a label and as `aria-keyshortcuts`. */\n shortcut: (name: string, args?: readonly unknown[]) => { label: string; aria: string } | undefined;\n on: ToolbarActions;\n /** The element each opener is drawn into, so a panel can hang from it. */\n ref: (name: string) => (element: Element | null) => void;\n colors: { text?: EditorColor; highlight?: EditorColor };\n}\n\nexport interface ToolbarActions {\n /** Gives a button the tooltip that says what it is. */\n tip: (element: Element | null, text: string) => void;\n command: (name: string, args: readonly unknown[], event: MouseEvent) => void;\n openLink: (event: MouseEvent) => void;\n openImage: (event: MouseEvent) => void;\n openTable: (event: MouseEvent) => void;\n openColor: (kind: 'color' | 'highlight', event: MouseEvent) => void;\n blockSelect: (element: Element | null) => void;\n}\n\nexport const iconView = (name: string, props?: Props): Child => iconNode(getIcon(name), props);\n\nconst OPENERS: Partial<Record<EditorToolbarItem, { icon: string; label: (words: Locale['editor']) => string; action: keyof ToolbarActions }>> = {\n image: { icon: 'image', label: (words) => words.image, action: 'openImage' },\n table: { icon: 'table', label: (words) => words.table, action: 'openTable' }\n};\n\n/** One command's button: a toggle where the command has a pressed state. */\nexport function buttonView(context: ToolbarContext, item: EditorButtonCommand): Child {\n const { part, locale } = context;\n const spec = editorButtons[item];\n if (!spec) return null;\n const name = spec.label(locale.editor);\n const isLink = item === 'link';\n const pressed = spec.active ? context.isActive(spec.active[0], spec.active[1]) : undefined;\n const [command, ...args] = spec.command;\n const disabled = !context.editable || (isLink ? context.isActive('codeBlock') : !context.can(command, ...args));\n const [shortcutName, ...shortcutArgs] = shortcutCommands[item] ?? spec.command;\n const shortcut = context.shortcut(shortcutName, shortcutArgs);\n return h(\n 'button',\n mergeAttrs({ key: item, type: 'button' }, part('button', { active: pressed, disabled }), {\n 'aria-label': name,\n 'aria-pressed': isLink || pressed === undefined ? undefined : pressed ? 'true' : 'false',\n 'aria-haspopup': isLink ? 'dialog' : undefined,\n 'aria-keyshortcuts': shortcut?.aria,\n disabled,\n ref: (element: Element | null) => {\n if (isLink) context.ref('link')(element);\n context.on.tip(element, shortcut ? `${name} (${shortcut.label})` : name);\n },\n onClick: (event: MouseEvent) => (isLink ? context.on.openLink(event) : context.on.command(command, args, event))\n }),\n iconView(spec.icon.name, part('buttonIcon'))\n );\n}\n\n/** A button that opens a panel: the image and table pickers. */\nfunction openerView(context: ToolbarContext, item: 'image' | 'table'): Child {\n const spec = OPENERS[item]!;\n const name = spec.label(context.locale.editor);\n return h(\n 'button',\n mergeAttrs({ key: item, type: 'button' }, context.part('button', { disabled: !context.editable }), {\n 'aria-label': name,\n 'aria-haspopup': 'dialog',\n disabled: !context.editable,\n ref: (element: Element | null) => {\n context.ref(item)(element);\n context.on.tip(element, name);\n },\n onClick: (event: MouseEvent) => (spec.action === 'openImage' ? context.on.openImage(event) : context.on.openTable(event))\n }),\n iconView(spec.icon, context.part('buttonIcon'))\n );\n}\n\n/** The text and highlight pickers: a button showing the colour in hand. */\nfunction colorView(context: ToolbarContext, kind: 'color' | 'highlight'): Child {\n const { part, locale } = context;\n const name = kind === 'color' ? locale.editor.textColor : locale.editor.highlight;\n const chosen = context.colors[kind === 'color' ? 'text' : 'highlight'];\n return h(\n 'button',\n mergeAttrs({ key: kind, type: 'button' }, part('button', { disabled: !context.editable }), {\n 'aria-label': name,\n 'aria-haspopup': 'dialog',\n disabled: !context.editable,\n ref: (element: Element | null) => {\n context.ref(kind)(element);\n context.on.tip(element, name);\n },\n onClick: (event: MouseEvent) => context.on.openColor(kind, event)\n }),\n iconView(kind === 'color' ? 'textColor' : 'highlighter', part('buttonIcon')),\n h('span', mergeAttrs({ key: 'bar', 'aria-hidden': 'true' }, part('colorBar'), { style: { background: chosen ? (kind === 'color' ? chosen.text : chosen.highlight) : 'transparent' } }))\n );\n}\n\n/** The block type, as the select the control kit draws; the handle mounts it. */\nfunction blockSelectView(context: ToolbarContext): Child {\n return h('span', mergeAttrs({ key: 'blockType' }, { ref: context.on.blockSelect }));\n}\n\nexport function toolbarView(context: ToolbarContext, groups: EditorToolbarItem[][]): Child {\n const { part, locale } = context;\n return h(\n 'div',\n mergeAttrs({ key: 'toolbar', role: 'toolbar' }, part('toolbar'), { 'aria-label': locale.editor.toolbar, 'aria-orientation': 'horizontal' }),\n groups.map((group, index) =>\n h(\n 'div',\n mergeAttrs({ key: index, role: 'group' }, part('toolbarGroup')),\n group.map((item) =>\n item === 'blockType'\n ? blockSelectView(context)\n : item === 'color' || item === 'highlight'\n ? colorView(context, item)\n : item === 'image' || item === 'table'\n ? openerView(context, item)\n : buttonView(context, item as EditorButtonCommand)\n )\n )\n )\n );\n}\n\n/** The floating toolbar over a selection: the same buttons, in one group. */\nexport function bubbleView(context: ToolbarContext, items: EditorToolbarItem[]): Child {\n return h(\n 'div',\n mergeAttrs({ role: 'toolbar' }, context.part('bubble'), { 'aria-label': context.locale.editor.toolbar }),\n items.map((item) =>\n item === 'color' || item === 'highlight' ? colorView(context, item) : item === 'image' || item === 'table' ? openerView(context, item) : buttonView(context, item as EditorButtonCommand)\n )\n );\n}\n\nexport const shortcutLabel = (binding: { key: string } | undefined): string | undefined => (binding ? formatEditorShortcut(binding.key) : undefined);\n"],"mappings":";;;;;AAuCA,IAAa,YAAY,MAAc,UAAyB,SAAS,QAAQ,IAAI,GAAG,KAAK;AAE7F,IAAM,UAA0I;CAC5I,OAAO;EAAE,MAAM;EAAS,QAAQ,UAAU,MAAM;EAAO,QAAQ;CAAY;CAC3E,OAAO;EAAE,MAAM;EAAS,QAAQ,UAAU,MAAM;EAAO,QAAQ;CAAY;AAC/E;;AAGA,SAAgB,WAAW,SAAyB,MAAkC;CAClF,MAAM,EAAE,MAAM,WAAW;CACzB,MAAM,OAAO,cAAc;CAC3B,IAAI,CAAC,MAAM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,QAAsC;CACvF,MAAM,EAAE,MAAM,WAAW;CACzB,OAAO,EACH,OACA,WAAW;EAAE,KAAK;EAAW,MAAM;CAAU,GAAG,KAAK,SAAS,GAAG;EAAE,cAAc,OAAO,OAAO;EAAS,oBAAoB;CAAa,CAAC,GAC1I,OAAO,KAAK,OAAO,UACf,EACI,OACA,WAAW;EAAE,KAAK;EAAO,MAAM;CAAQ,GAAG,KAAK,cAAc,CAAC,GAC9D,MAAM,KAAK,SACP,SAAS,cACH,gBAAgB,OAAO,IACvB,SAAS,WAAW,SAAS,cAC3B,UAAU,SAAS,IAAI,IACvB,SAAS,WAAW,SAAS,UAC3B,WAAW,SAAS,IAAI,IACxB,WAAW,SAAS,IAA2B,CAC7D,CACJ,CACJ,CACJ;AACJ;;AAGA,SAAgB,WAAW,SAAyB,OAAmC;CACnF,OAAO,EACH,OACA,WAAW,EAAE,MAAM,UAAU,GAAG,QAAQ,KAAK,QAAQ,GAAG,EAAE,cAAc,QAAQ,OAAO,OAAO,QAAQ,CAAC,GACvG,MAAM,KAAK,SACP,SAAS,WAAW,SAAS,cAAc,UAAU,SAAS,IAAI,IAAI,SAAS,WAAW,SAAS,UAAU,WAAW,SAAS,IAAI,IAAI,WAAW,SAAS,IAA2B,CAC5L,CACJ;AACJ"}
|
package/dist/slash.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type EditorInstance, type Locale } from '@vitral/core';
|
|
2
|
+
import { type OverlayTarget } from '@vitral/controls';
|
|
3
|
+
import type { Props } from '@vitral/dom';
|
|
4
|
+
import type { SlashCommand } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* The menu a `/` opens, as a piece that attaches to any editor: the
|
|
7
|
+
* framework-free one uses it, and so does a framework component that draws its
|
|
8
|
+
* own toolbar. It watches the document, opens when a slash starts a word,
|
|
9
|
+
* filters as it is typed, and takes the keys it needs while it is open.
|
|
10
|
+
*/
|
|
11
|
+
export interface SlashMenuOptions {
|
|
12
|
+
editor: EditorInstance;
|
|
13
|
+
/** The `contenteditable` element, where the keys are heard. */
|
|
14
|
+
content: () => HTMLElement | null;
|
|
15
|
+
/** What the menu hangs from: an element over the caret. */
|
|
16
|
+
anchor: () => HTMLElement | null;
|
|
17
|
+
/** The commands on offer; a function is read every time it opens. */
|
|
18
|
+
commands: () => SlashCommand[];
|
|
19
|
+
/** The classes and pass-through of the menu's parts. */
|
|
20
|
+
part: (name: string, state?: unknown) => Props;
|
|
21
|
+
locale?: () => Locale;
|
|
22
|
+
/** Off while the editor is read-only. */
|
|
23
|
+
enabled?: () => boolean;
|
|
24
|
+
/** Puts the anchor where the caret is, before the menu opens. */
|
|
25
|
+
place?: () => void;
|
|
26
|
+
ids?: {
|
|
27
|
+
slash: string;
|
|
28
|
+
block: string;
|
|
29
|
+
};
|
|
30
|
+
overlayTarget?: OverlayTarget;
|
|
31
|
+
zIndex?: number;
|
|
32
|
+
/** What to do with the command the reader chose; it runs itself by default. */
|
|
33
|
+
onRun?: (command: SlashCommand) => void;
|
|
34
|
+
}
|
|
35
|
+
export interface SlashMenu {
|
|
36
|
+
readonly isOpen: boolean;
|
|
37
|
+
/** Reads the document again: called on every change. */
|
|
38
|
+
sync(): void;
|
|
39
|
+
close(): void;
|
|
40
|
+
destroy(): void;
|
|
41
|
+
}
|
|
42
|
+
export declare function createSlashMenu(options: SlashMenuOptions): SlashMenu;
|
|
43
|
+
/** The blocks a slash offers when an application names none. */
|
|
44
|
+
export declare function defaultSlashCommands(locale: Locale): SlashCommand[];
|
package/dist/slash.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { slashMenuView } from "./render/menus.js";
|
|
2
|
+
import { editorSelectionRange, editorTextblocks, en } from "@vitral/core";
|
|
3
|
+
import { createOverlay } from "@vitral/controls";
|
|
4
|
+
//#region src/slash.ts
|
|
5
|
+
function createSlashMenu(options) {
|
|
6
|
+
let open = null;
|
|
7
|
+
let element = null;
|
|
8
|
+
const locale = () => options.locale?.() ?? en;
|
|
9
|
+
const ids = options.ids ?? {
|
|
10
|
+
slash: "vt-slash",
|
|
11
|
+
block: "vt-block"
|
|
12
|
+
};
|
|
13
|
+
const enabled = () => options.enabled?.() ?? true;
|
|
14
|
+
/** What the query after the slash matches, by label, id and keyword. */
|
|
15
|
+
function matches(query) {
|
|
16
|
+
const needle = query.trim().toLocaleLowerCase(locale().code);
|
|
17
|
+
const all = options.commands();
|
|
18
|
+
if (!needle) return all;
|
|
19
|
+
return all.filter((item) => [
|
|
20
|
+
item.label,
|
|
21
|
+
item.id,
|
|
22
|
+
...item.keywords ?? []
|
|
23
|
+
].some((word) => word.toLocaleLowerCase(locale().code).includes(needle)));
|
|
24
|
+
}
|
|
25
|
+
const overlay = createOverlay({
|
|
26
|
+
anchor: options.anchor,
|
|
27
|
+
render: () => open ? slashMenuView({
|
|
28
|
+
locale: locale(),
|
|
29
|
+
part: options.part,
|
|
30
|
+
ids
|
|
31
|
+
}, {
|
|
32
|
+
query: open.query,
|
|
33
|
+
items: matches(open.query),
|
|
34
|
+
active: open.active
|
|
35
|
+
}, { choose: run }) : null,
|
|
36
|
+
placement: "bottom-start",
|
|
37
|
+
target: () => options.overlayTarget,
|
|
38
|
+
zIndex: options.zIndex,
|
|
39
|
+
restoreFocus: false,
|
|
40
|
+
onClose: () => open = null
|
|
41
|
+
});
|
|
42
|
+
/** The text of the block the caret is in, and where the caret is in it. */
|
|
43
|
+
function caretText() {
|
|
44
|
+
const state = options.editor.state;
|
|
45
|
+
if (!editorSelectionRange(state.selection).empty) return null;
|
|
46
|
+
const blocks = editorTextblocks(state.doc);
|
|
47
|
+
const path = state.selection.head.path;
|
|
48
|
+
const block = blocks.find((entry) => entry.path.join(".") === path.join("."))?.node ?? (path[0] === void 0 ? void 0 : state.doc.content?.[path[0]]);
|
|
49
|
+
if (!block) return null;
|
|
50
|
+
return {
|
|
51
|
+
text: (block.content ?? []).map((child) => typeof child === "string" ? child : child.text ?? "").join(""),
|
|
52
|
+
offset: state.selection.head.offset
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function sync() {
|
|
56
|
+
if (!enabled()) return close();
|
|
57
|
+
const caret = caretText();
|
|
58
|
+
if (!caret) return close();
|
|
59
|
+
const before = caret.text.slice(0, caret.offset);
|
|
60
|
+
const at = before.lastIndexOf("/");
|
|
61
|
+
const starts = at === 0 || at > 0 && /\s/.test(before[at - 1] ?? "");
|
|
62
|
+
if (at < 0 || !starts) return close();
|
|
63
|
+
const query = before.slice(at + 1);
|
|
64
|
+
if (/\s/.test(query)) return close();
|
|
65
|
+
open = {
|
|
66
|
+
query,
|
|
67
|
+
active: open && open.query === query ? open.active : 0
|
|
68
|
+
};
|
|
69
|
+
options.place?.();
|
|
70
|
+
overlay.open();
|
|
71
|
+
overlay.update();
|
|
72
|
+
}
|
|
73
|
+
function close() {
|
|
74
|
+
if (!overlay.isOpen) {
|
|
75
|
+
open = null;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
overlay.close();
|
|
79
|
+
}
|
|
80
|
+
/** Runs what the menu was pointing at, after taking the slash and its query out. */
|
|
81
|
+
function run(index) {
|
|
82
|
+
const item = matches(open?.query ?? "")[index];
|
|
83
|
+
if (!item || !open) return;
|
|
84
|
+
const remove = open.query.length + 1;
|
|
85
|
+
close();
|
|
86
|
+
for (let n = 0; n < remove; n++) options.editor.backspace("char");
|
|
87
|
+
if (options.onRun) options.onRun(item);
|
|
88
|
+
else if (item.run) item.run(options.editor);
|
|
89
|
+
else if (item.command) options.editor.run(item.command[0], ...item.command.slice(1));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* While the menu is open those keys are the menu's. The listener is a
|
|
93
|
+
* capturing one: the editor's own view listens on the same element, and an
|
|
94
|
+
* Enter it heard would split the block the command is about to change.
|
|
95
|
+
*/
|
|
96
|
+
function onKeydown(event) {
|
|
97
|
+
if (!open) return;
|
|
98
|
+
const items = matches(open.query);
|
|
99
|
+
const take = () => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
event.stopImmediatePropagation();
|
|
102
|
+
};
|
|
103
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
104
|
+
take();
|
|
105
|
+
const step = event.key === "ArrowDown" ? 1 : -1;
|
|
106
|
+
open = {
|
|
107
|
+
...open,
|
|
108
|
+
active: (open.active + step + items.length) % Math.max(1, items.length)
|
|
109
|
+
};
|
|
110
|
+
overlay.update();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (event.key === "Home" || event.key === "End") {
|
|
114
|
+
take();
|
|
115
|
+
open = {
|
|
116
|
+
...open,
|
|
117
|
+
active: event.key === "Home" ? 0 : Math.max(0, items.length - 1)
|
|
118
|
+
};
|
|
119
|
+
overlay.update();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (event.key === "Enter" || event.key === "Tab") {
|
|
123
|
+
if (!items.length) return;
|
|
124
|
+
take();
|
|
125
|
+
run(open.active);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (event.key === "Escape") {
|
|
129
|
+
take();
|
|
130
|
+
close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function listen() {
|
|
134
|
+
const next = options.content();
|
|
135
|
+
if (next === element) return;
|
|
136
|
+
element?.removeEventListener("keydown", onKeydown, true);
|
|
137
|
+
element = next;
|
|
138
|
+
element?.addEventListener("keydown", onKeydown, true);
|
|
139
|
+
}
|
|
140
|
+
const stop = options.editor.subscribe(() => {
|
|
141
|
+
listen();
|
|
142
|
+
sync();
|
|
143
|
+
});
|
|
144
|
+
listen();
|
|
145
|
+
sync();
|
|
146
|
+
return {
|
|
147
|
+
get isOpen() {
|
|
148
|
+
return overlay.isOpen;
|
|
149
|
+
},
|
|
150
|
+
sync() {
|
|
151
|
+
listen();
|
|
152
|
+
sync();
|
|
153
|
+
},
|
|
154
|
+
close,
|
|
155
|
+
destroy() {
|
|
156
|
+
stop();
|
|
157
|
+
element?.removeEventListener("keydown", onKeydown, true);
|
|
158
|
+
element = null;
|
|
159
|
+
overlay.destroy();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/** The blocks a slash offers when an application names none. */
|
|
164
|
+
function defaultSlashCommands(locale) {
|
|
165
|
+
const words = locale.editor;
|
|
166
|
+
const entry = (id, icon, command) => ({
|
|
167
|
+
id,
|
|
168
|
+
icon,
|
|
169
|
+
label: words.slashCommands[id] ?? id,
|
|
170
|
+
description: words.slashHints[id],
|
|
171
|
+
command
|
|
172
|
+
});
|
|
173
|
+
return [
|
|
174
|
+
entry("paragraph", "pilcrow", ["setParagraph"]),
|
|
175
|
+
entry("heading1", "heading1", ["toggleHeading", 1]),
|
|
176
|
+
entry("heading2", "heading2", ["toggleHeading", 2]),
|
|
177
|
+
entry("heading3", "heading3", ["toggleHeading", 3]),
|
|
178
|
+
entry("bulletList", "list", ["toggleBulletList"]),
|
|
179
|
+
entry("orderedList", "listOrdered", ["toggleOrderedList"]),
|
|
180
|
+
entry("taskList", "listChecks", ["toggleTaskList"]),
|
|
181
|
+
entry("blockquote", "quote", ["toggleBlockquote"]),
|
|
182
|
+
entry("codeBlock", "codeBlock", ["toggleCodeBlock"]),
|
|
183
|
+
entry("horizontalRule", "horizontalRule", ["insertHorizontalRule"]),
|
|
184
|
+
entry("table", "table", [
|
|
185
|
+
"insertTable",
|
|
186
|
+
3,
|
|
187
|
+
3,
|
|
188
|
+
true
|
|
189
|
+
]),
|
|
190
|
+
entry("image", "image", ["insertImage", { src: "" }])
|
|
191
|
+
];
|
|
192
|
+
}
|
|
193
|
+
//#endregion
|
|
194
|
+
export { createSlashMenu, defaultSlashCommands };
|
|
195
|
+
|
|
196
|
+
//# sourceMappingURL=slash.js.map
|