@vitral/editor 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/block.d.ts +3 -1
- package/dist/block.js +59 -5
- package/dist/block.js.map +1 -1
- package/dist/chips.d.ts +38 -0
- package/dist/chips.js +178 -0
- package/dist/chips.js.map +1 -0
- package/dist/editor.js +24 -3
- package/dist/editor.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/render/menus.d.ts +7 -0
- package/dist/render/menus.js +4 -3
- package/dist/render/menus.js.map +1 -1
- package/dist/types.d.ts +36 -2
- package/package.json +6 -6
package/dist/block.d.ts
CHANGED
|
@@ -11,12 +11,14 @@ export interface BlockHandleOptions {
|
|
|
11
11
|
editor: EditorInstance;
|
|
12
12
|
/** Where the handle is put: the editor's own box, which it is positioned against. */
|
|
13
13
|
host: () => HTMLElement | null;
|
|
14
|
-
/** The rectangle of the caret, from the editor's view. */
|
|
14
|
+
/** The rectangle of the caret, from the editor's view: where the handle goes when the block cannot be found. */
|
|
15
15
|
caretRect: () => {
|
|
16
16
|
top: number;
|
|
17
17
|
left: number;
|
|
18
18
|
height: number;
|
|
19
19
|
} | null;
|
|
20
|
+
/** The element the document is drawn in; its children are the top-level blocks. */
|
|
21
|
+
content?: () => HTMLElement | null;
|
|
20
22
|
actions: () => BlockAction[];
|
|
21
23
|
part: (name: string, state?: unknown) => Props;
|
|
22
24
|
locale?: () => Locale;
|
package/dist/block.js
CHANGED
|
@@ -64,16 +64,56 @@ function createBlockHandle(options) {
|
|
|
64
64
|
button?.remove();
|
|
65
65
|
button = null;
|
|
66
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* The middle of the block's first line, in viewport pixels: the handle
|
|
69
|
+
* sits level with the line a reader sees the block start on, whichever
|
|
70
|
+
* line of it the caret is in. A block with no line of text (a rule, a
|
|
71
|
+
* picture) is centred on as a whole.
|
|
72
|
+
*/
|
|
73
|
+
function lineMiddle(index) {
|
|
74
|
+
const element = options.content?.()?.children[index];
|
|
75
|
+
if (element?.getBoundingClientRect) {
|
|
76
|
+
const box = element.getBoundingClientRect();
|
|
77
|
+
const style = getComputedStyle(element);
|
|
78
|
+
const px = (value) => parseFloat(value) || 0;
|
|
79
|
+
const border = (width, kind) => !kind || kind === "none" || kind === "hidden" ? 0 : px(width);
|
|
80
|
+
const above = border(style.borderTopWidth, style.borderTopStyle) + px(style.paddingTop);
|
|
81
|
+
const below = border(style.borderBottomWidth, style.borderBottomStyle) + px(style.paddingBottom);
|
|
82
|
+
const top = box.top + above;
|
|
83
|
+
const inner = box.height - above - below;
|
|
84
|
+
const line = px(style.lineHeight) || px(style.fontSize) * 1.2;
|
|
85
|
+
const height = line > 0 && inner > 0 ? Math.min(line, inner) : inner;
|
|
86
|
+
if (box.height > 0) return top + height / 2;
|
|
87
|
+
}
|
|
88
|
+
const caret = options.caretRect();
|
|
89
|
+
return caret ? caret.top + caret.height / 2 : null;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Puts the handle level with its block. Done now and again on the next
|
|
93
|
+
* frame: a change can reflow the line it sits on — an empty line takes
|
|
94
|
+
* its placeholder — after the change itself has been reported.
|
|
95
|
+
*/
|
|
96
|
+
let frame = 0;
|
|
97
|
+
function place() {
|
|
98
|
+
const handle = button;
|
|
99
|
+
const current = block();
|
|
100
|
+
const hostEl = options.host();
|
|
101
|
+
if (!handle || !hostEl) return;
|
|
102
|
+
const middle = current ? lineMiddle(current.index) : null;
|
|
103
|
+
const box = hostEl.getBoundingClientRect();
|
|
104
|
+
const size = handle.offsetHeight || handle.getBoundingClientRect().height || 0;
|
|
105
|
+
const view = options.content?.()?.getBoundingClientRect();
|
|
106
|
+
const hidden = middle === null || !!view && view.height > 0 && (middle < view.top || middle > view.bottom);
|
|
107
|
+
if (middle !== null) handle.style.top = `${Math.round((middle - box.top - hostEl.clientTop - size / 2) * 100) / 100}px`;
|
|
108
|
+
handle.style.visibility = hidden ? "hidden" : "";
|
|
109
|
+
}
|
|
67
110
|
function sync() {
|
|
68
111
|
if (!enabled()) {
|
|
69
112
|
remove();
|
|
70
113
|
overlay.close();
|
|
71
114
|
return;
|
|
72
115
|
}
|
|
73
|
-
|
|
74
|
-
const rect = options.caretRect();
|
|
75
|
-
const box = options.host()?.getBoundingClientRect();
|
|
76
|
-
if (!handle || !root) return;
|
|
116
|
+
if (!ensure() || !root) return;
|
|
77
117
|
root.attrs({
|
|
78
118
|
...options.part("blockHandle"),
|
|
79
119
|
type: "button",
|
|
@@ -81,7 +121,6 @@ function createBlockHandle(options) {
|
|
|
81
121
|
"aria-label": locale().editor.blockMenu,
|
|
82
122
|
"aria-haspopup": "menu",
|
|
83
123
|
"aria-expanded": overlay.isOpen ? "true" : "false",
|
|
84
|
-
style: rect && box ? { top: `${rect.top - box.top}px` } : void 0,
|
|
85
124
|
onMousedown: (event) => event.preventDefault(),
|
|
86
125
|
onClick: () => {
|
|
87
126
|
if (overlay.isOpen) overlay.close();
|
|
@@ -93,14 +132,29 @@ function createBlockHandle(options) {
|
|
|
93
132
|
}
|
|
94
133
|
});
|
|
95
134
|
root.render([iconView("grip", options.part("blockHandleIcon"))]);
|
|
135
|
+
place();
|
|
136
|
+
if (typeof requestAnimationFrame === "function") {
|
|
137
|
+
cancelAnimationFrame(frame);
|
|
138
|
+
frame = requestAnimationFrame(place);
|
|
139
|
+
}
|
|
96
140
|
}
|
|
97
141
|
const stop = options.editor.subscribe(sync);
|
|
142
|
+
const follow = () => sync();
|
|
143
|
+
const listening = options.host();
|
|
144
|
+
listening?.addEventListener("scroll", follow, {
|
|
145
|
+
capture: true,
|
|
146
|
+
passive: true
|
|
147
|
+
});
|
|
148
|
+
if (typeof window !== "undefined") window.addEventListener("resize", follow);
|
|
98
149
|
sync();
|
|
99
150
|
return {
|
|
100
151
|
sync,
|
|
101
152
|
close: () => overlay.close(),
|
|
102
153
|
destroy() {
|
|
103
154
|
stop();
|
|
155
|
+
if (typeof cancelAnimationFrame === "function") cancelAnimationFrame(frame);
|
|
156
|
+
listening?.removeEventListener("scroll", follow, { capture: true });
|
|
157
|
+
if (typeof window !== "undefined") window.removeEventListener("resize", follow);
|
|
104
158
|
overlay.destroy();
|
|
105
159
|
remove();
|
|
106
160
|
host = null;
|
package/dist/block.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"block.js","names":[],"sources":["../src/block.ts"],"sourcesContent":["import { editorSelectionRange, en, type EditorInstance, type EditorNode, type Locale } from '@vitral/core';\nimport { createOverlay, type OverlayTarget } from '@vitral/controls';\nimport { createRoot, type Props } from '@vitral/dom';\nimport { blockMenuView } from './render/menus';\nimport { iconView } from './render/toolbar';\nimport type { BlockAction } from './types';\n\n/**\n * The handle beside the block the caret is in, and the menu it opens: a piece\n * that attaches to any editor, so the framework-free editor and a framework\n * component that draws its own toolbar both get it from here.\n */\n\nexport interface BlockHandleOptions {\n editor: EditorInstance;\n /** Where the handle is put: the editor's own box, which it is positioned against. */\n host: () => HTMLElement | null;\n /** The rectangle of the caret, from the editor's view. */\n caretRect: () => { top: number; left: number; height: number } | null;\n actions: () => BlockAction[];\n part: (name: string, state?: unknown) => Props;\n locale?: () => Locale;\n enabled?: () => boolean;\n ids?: { slash: string; block: string };\n overlayTarget?: OverlayTarget;\n zIndex?: number;\n}\n\nexport interface BlockHandle {\n /** Reads the document and the caret again: called on every change. */\n sync(): void;\n close(): void;\n destroy(): void;\n}\n\nexport function createBlockHandle(options: BlockHandleOptions): BlockHandle {\n let button: HTMLButtonElement | null = null;\n let root: ReturnType<typeof createRoot> | null = null;\n let host: HTMLElement | null = null;\n\n const locale = () => options.locale?.() ?? en;\n const ids = options.ids ?? { slash: 'vt-slash', block: 'vt-block' };\n const enabled = () => options.enabled?.() ?? true;\n\n /** The top-level block the caret is in. */\n function block(): { node: EditorNode; index: number } | null {\n const state = options.editor.state;\n const index = editorSelectionRange(state.selection).from.path[0];\n const node = index === undefined ? undefined : (state.doc.content?.[index] as EditorNode | undefined);\n return node ? { node, index } : null;\n }\n\n const overlay = createOverlay({\n anchor: () => button,\n render: () => {\n const current = block();\n return current ? (blockMenuView({ locale: locale(), part: options.part, ids }, current.node, options.actions(), { choose: run }) as never) : null;\n },\n placement: 'bottom-start',\n target: () => options.overlayTarget,\n zIndex: options.zIndex\n });\n\n function run(id: string) {\n const current = block();\n const action = options.actions().find((entry) => entry.id === id);\n overlay.close();\n if (!current || !action) return;\n action.run(options.editor, current.node, current.index);\n }\n\n /** The handle is the editor's own element, not the document's: it is never edited. */\n function ensure(): HTMLButtonElement | null {\n const next = options.host();\n if (!next) return null;\n if (host !== next) {\n remove();\n host = next;\n }\n if (button) return button;\n button = host.ownerDocument.createElement('button');\n host.appendChild(button);\n root = createRoot(button);\n return button;\n }\n\n function remove() {\n root = null;\n button?.remove();\n button = null;\n }\n\n function sync() {\n if (!enabled()) {\n remove();\n overlay.close();\n return;\n }\n const handle = ensure();\n const rect = options.caretRect();\n const box = options.host()?.getBoundingClientRect();\n if (!handle || !root) return;\n root.attrs({\n ...options.part('blockHandle'),\n type: 'button',\n contenteditable: 'false',\n 'aria-label': locale().editor.blockMenu,\n 'aria-haspopup': 'menu',\n 'aria-expanded': overlay.isOpen ? 'true' : 'false',\n style: rect && box ? { top: `${rect.top - box.top}px` } : undefined,\n // A press on the handle must not take the caret out of the text.\n onMousedown: (event: MouseEvent) => event.preventDefault(),\n onClick: () => {\n if (overlay.isOpen) overlay.close();\n else {\n overlay.open();\n overlay.update();\n }\n sync();\n }\n });\n root.render([iconView('grip', options.part('blockHandleIcon'))]);\n }\n\n const stop = options.editor.subscribe(sync);\n sync();\n\n return {\n sync,\n close: () => overlay.close(),\n destroy() {\n stop();\n overlay.destroy();\n remove();\n host = null;\n }\n };\n}\n\n/** What the handle offers when an application names nothing. */\nexport function defaultBlockActions(locale: Locale): BlockAction[] {\n const words = locale.editor.blockActions;\n const command = (name: string) => (editor: { run: (name: string, ...args: unknown[]) => boolean }) => void editor.run(name);\n return [\n { id: 'duplicate', label: words.duplicate ?? 'Duplicate', icon: 'copy', run: command('duplicateBlock') },\n { id: 'moveUp', label: words.moveUp ?? 'Move up', icon: 'arrowUp', run: command('moveBlockUp') },\n { id: 'moveDown', label: words.moveDown ?? 'Move down', icon: 'arrowDown', run: command('moveBlockDown') },\n { id: 'turnIntoParagraph', label: words.turnIntoParagraph ?? 'Turn into text', icon: 'pilcrow', run: command('setParagraph') },\n { id: 'delete', label: words.delete ?? 'Delete', icon: 'trash', run: command('deleteBlock') }\n ];\n}\n"],"mappings":";;;;;;AAmCA,SAAgB,kBAAkB,SAA0C;CACxE,IAAI,SAAmC;CACvC,IAAI,OAA6C;CACjD,IAAI,OAA2B;CAE/B,MAAM,eAAe,QAAQ,SAAS,KAAK;CAC3C,MAAM,MAAM,QAAQ,OAAO;EAAE,OAAO;EAAY,OAAO;CAAW;CAClE,MAAM,gBAAgB,QAAQ,UAAU,KAAK;;CAG7C,SAAS,QAAoD;EACzD,MAAM,QAAQ,QAAQ,OAAO;EAC7B,MAAM,QAAQ,qBAAqB,MAAM,SAAS,CAAC,CAAC,KAAK,KAAK;EAC9D,MAAM,OAAO,UAAU,KAAA,IAAY,KAAA,IAAa,MAAM,IAAI,UAAU;EACpE,OAAO,OAAO;GAAE;GAAM;EAAM,IAAI;CACpC;CAEA,MAAM,UAAU,cAAc;EAC1B,cAAc;EACd,cAAc;GACV,MAAM,UAAU,MAAM;GACtB,OAAO,UAAW,cAAc;IAAE,QAAQ,OAAO;IAAG,MAAM,QAAQ;IAAM;GAAI,GAAG,QAAQ,MAAM,QAAQ,QAAQ,GAAG,EAAE,QAAQ,IAAI,CAAC,IAAc;EACjJ;EACA,WAAW;EACX,cAAc,QAAQ;EACtB,QAAQ,QAAQ;CACpB,CAAC;CAED,SAAS,IAAI,IAAY;EACrB,MAAM,UAAU,MAAM;EACtB,MAAM,SAAS,QAAQ,QAAQ,CAAC,CAAC,MAAM,UAAU,MAAM,OAAO,EAAE;EACh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ACH;EACA,aAAa,QAAQ,MAAM;EAC3B,UAAU;GACN,KAAK;GACL,QAAQ,QAAQ;GAChB,OAAO;GACP,OAAO;EACX;CACJ;AACJ;;AAGA,SAAgB,oBAAoB,QAA+B;CAC/D,MAAM,QAAQ,OAAO,OAAO;CAC5B,MAAM,WAAW,UAAkB,WAAmE,KAAK,OAAO,IAAI,IAAI;CAC1H,OAAO;EACH;GAAE,IAAI;GAAa,OAAO,MAAM,aAAa;GAAa,MAAM;GAAQ,KAAK,QAAQ,gBAAgB;EAAE;EACvG;GAAE,IAAI;GAAU,OAAO,MAAM,UAAU;GAAW,MAAM;GAAW,KAAK,QAAQ,aAAa;EAAE;EAC/F;GAAE,IAAI;GAAY,OAAO,MAAM,YAAY;GAAa,MAAM;GAAa,KAAK,QAAQ,eAAe;EAAE;EACzG;GAAE,IAAI;GAAqB,OAAO,MAAM,qBAAqB;GAAkB,MAAM;GAAW,KAAK,QAAQ,cAAc;EAAE;EAC7H;GAAE,IAAI;GAAU,OAAO,MAAM,UAAU;GAAU,MAAM;GAAS,KAAK,QAAQ,aAAa;EAAE;CAChG;AACJ"}
|
|
1
|
+
{"version":3,"file":"block.js","names":[],"sources":["../src/block.ts"],"sourcesContent":["import { editorSelectionRange, en, type EditorInstance, type EditorNode, type Locale } from '@vitral/core';\nimport { createOverlay, type OverlayTarget } from '@vitral/controls';\nimport { createRoot, type Props } from '@vitral/dom';\nimport { blockMenuView } from './render/menus';\nimport { iconView } from './render/toolbar';\nimport type { BlockAction } from './types';\n\n/**\n * The handle beside the block the caret is in, and the menu it opens: a piece\n * that attaches to any editor, so the framework-free editor and a framework\n * component that draws its own toolbar both get it from here.\n */\n\nexport interface BlockHandleOptions {\n editor: EditorInstance;\n /** Where the handle is put: the editor's own box, which it is positioned against. */\n host: () => HTMLElement | null;\n /** The rectangle of the caret, from the editor's view: where the handle goes when the block cannot be found. */\n caretRect: () => { top: number; left: number; height: number } | null;\n /** The element the document is drawn in; its children are the top-level blocks. */\n content?: () => HTMLElement | null;\n actions: () => BlockAction[];\n part: (name: string, state?: unknown) => Props;\n locale?: () => Locale;\n enabled?: () => boolean;\n ids?: { slash: string; block: string };\n overlayTarget?: OverlayTarget;\n zIndex?: number;\n}\n\nexport interface BlockHandle {\n /** Reads the document and the caret again: called on every change. */\n sync(): void;\n close(): void;\n destroy(): void;\n}\n\nexport function createBlockHandle(options: BlockHandleOptions): BlockHandle {\n let button: HTMLButtonElement | null = null;\n let root: ReturnType<typeof createRoot> | null = null;\n let host: HTMLElement | null = null;\n\n const locale = () => options.locale?.() ?? en;\n const ids = options.ids ?? { slash: 'vt-slash', block: 'vt-block' };\n const enabled = () => options.enabled?.() ?? true;\n\n /** The top-level block the caret is in. */\n function block(): { node: EditorNode; index: number } | null {\n const state = options.editor.state;\n const index = editorSelectionRange(state.selection).from.path[0];\n const node = index === undefined ? undefined : (state.doc.content?.[index] as EditorNode | undefined);\n return node ? { node, index } : null;\n }\n\n const overlay = createOverlay({\n anchor: () => button,\n render: () => {\n const current = block();\n return current ? (blockMenuView({ locale: locale(), part: options.part, ids }, current.node, options.actions(), { choose: run }) as never) : null;\n },\n placement: 'bottom-start',\n target: () => options.overlayTarget,\n zIndex: options.zIndex\n });\n\n function run(id: string) {\n const current = block();\n const action = options.actions().find((entry) => entry.id === id);\n overlay.close();\n if (!current || !action) return;\n action.run(options.editor, current.node, current.index);\n }\n\n /** The handle is the editor's own element, not the document's: it is never edited. */\n function ensure(): HTMLButtonElement | null {\n const next = options.host();\n if (!next) return null;\n if (host !== next) {\n remove();\n host = next;\n }\n if (button) return button;\n button = host.ownerDocument.createElement('button');\n host.appendChild(button);\n root = createRoot(button);\n return button;\n }\n\n function remove() {\n root = null;\n button?.remove();\n button = null;\n }\n\n /**\n * The middle of the block's first line, in viewport pixels: the handle\n * sits level with the line a reader sees the block start on, whichever\n * line of it the caret is in. A block with no line of text (a rule, a\n * picture) is centred on as a whole.\n */\n function lineMiddle(index: number): number | null {\n const element = options.content?.()?.children[index] as HTMLElement | undefined;\n if (element?.getBoundingClientRect) {\n const box = element.getBoundingClientRect();\n const style = getComputedStyle(element);\n const px = (value: string) => parseFloat(value) || 0;\n // A border with no style has no width, whatever the width says.\n const border = (width: string, kind: string) => (!kind || kind === 'none' || kind === 'hidden' ? 0 : px(width));\n const above = border(style.borderTopWidth, style.borderTopStyle) + px(style.paddingTop);\n const below = border(style.borderBottomWidth, style.borderBottomStyle) + px(style.paddingBottom);\n const top = box.top + above;\n const inner = box.height - above - below;\n // `normal` has no number to read; 1.2 of the font is what browsers draw it as.\n const line = px(style.lineHeight) || px(style.fontSize) * 1.2;\n const height = line > 0 && inner > 0 ? Math.min(line, inner) : inner;\n if (box.height > 0) return top + height / 2;\n }\n const caret = options.caretRect();\n return caret ? caret.top + caret.height / 2 : null;\n }\n\n /**\n * Puts the handle level with its block. Done now and again on the next\n * frame: a change can reflow the line it sits on — an empty line takes\n * its placeholder — after the change itself has been reported.\n */\n let frame = 0;\n function place() {\n const handle = button;\n const current = block();\n const hostEl = options.host();\n if (!handle || !hostEl) return;\n const middle = current ? lineMiddle(current.index) : null;\n const box = hostEl.getBoundingClientRect();\n // Positioned against the host's padding box, which starts inside its border.\n const size = handle.offsetHeight || handle.getBoundingClientRect().height || 0;\n // A block scrolled out of the content's view takes its handle with it.\n const view = options.content?.()?.getBoundingClientRect();\n const hidden = middle === null || (!!view && view.height > 0 && (middle < view.top || middle > view.bottom));\n if (middle !== null) handle.style.top = `${Math.round((middle - box.top - hostEl.clientTop - size / 2) * 100) / 100}px`;\n handle.style.visibility = hidden ? 'hidden' : '';\n }\n\n function sync() {\n if (!enabled()) {\n remove();\n overlay.close();\n return;\n }\n const handle = ensure();\n if (!handle || !root) return;\n root.attrs({\n ...options.part('blockHandle'),\n type: 'button',\n contenteditable: 'false',\n 'aria-label': locale().editor.blockMenu,\n 'aria-haspopup': 'menu',\n 'aria-expanded': overlay.isOpen ? 'true' : 'false',\n // A press on the handle must not take the caret out of the text.\n onMousedown: (event: MouseEvent) => event.preventDefault(),\n onClick: () => {\n if (overlay.isOpen) overlay.close();\n else {\n overlay.open();\n overlay.update();\n }\n sync();\n }\n });\n root.render([iconView('grip', options.part('blockHandleIcon'))]);\n place();\n if (typeof requestAnimationFrame === 'function') {\n cancelAnimationFrame(frame);\n frame = requestAnimationFrame(place);\n }\n }\n\n const stop = options.editor.subscribe(sync);\n // The content scrolls under the handle, and a resize reflows the lines.\n const follow = () => sync();\n const listening = options.host();\n listening?.addEventListener('scroll', follow, { capture: true, passive: true });\n if (typeof window !== 'undefined') window.addEventListener('resize', follow);\n sync();\n\n return {\n sync,\n close: () => overlay.close(),\n destroy() {\n stop();\n if (typeof cancelAnimationFrame === 'function') cancelAnimationFrame(frame);\n listening?.removeEventListener('scroll', follow, { capture: true });\n if (typeof window !== 'undefined') window.removeEventListener('resize', follow);\n overlay.destroy();\n remove();\n host = null;\n }\n };\n}\n\n/** What the handle offers when an application names nothing. */\nexport function defaultBlockActions(locale: Locale): BlockAction[] {\n const words = locale.editor.blockActions;\n const command = (name: string) => (editor: { run: (name: string, ...args: unknown[]) => boolean }) => void editor.run(name);\n return [\n { id: 'duplicate', label: words.duplicate ?? 'Duplicate', icon: 'copy', run: command('duplicateBlock') },\n { id: 'moveUp', label: words.moveUp ?? 'Move up', icon: 'arrowUp', run: command('moveBlockUp') },\n { id: 'moveDown', label: words.moveDown ?? 'Move down', icon: 'arrowDown', run: command('moveBlockDown') },\n { id: 'turnIntoParagraph', label: words.turnIntoParagraph ?? 'Turn into text', icon: 'pilcrow', run: command('setParagraph') },\n { id: 'delete', label: words.delete ?? 'Delete', icon: 'trash', run: command('deleteBlock') }\n ];\n}\n"],"mappings":";;;;;;AAqCA,SAAgB,kBAAkB,SAA0C;CACxE,IAAI,SAAmC;CACvC,IAAI,OAA6C;CACjD,IAAI,OAA2B;CAE/B,MAAM,eAAe,QAAQ,SAAS,KAAK;CAC3C,MAAM,MAAM,QAAQ,OAAO;EAAE,OAAO;EAAY,OAAO;CAAW;CA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ACP,OAAO;EACX;CACJ;AACJ;;AAGA,SAAgB,oBAAoB,QAA+B;CAC/D,MAAM,QAAQ,OAAO,OAAO;CAC5B,MAAM,WAAW,UAAkB,WAAmE,KAAK,OAAO,IAAI,IAAI;CAC1H,OAAO;EACH;GAAE,IAAI;GAAa,OAAO,MAAM,aAAa;GAAa,MAAM;GAAQ,KAAK,QAAQ,gBAAgB;EAAE;EACvG;GAAE,IAAI;GAAU,OAAO,MAAM,UAAU;GAAW,MAAM;GAAW,KAAK,QAAQ,aAAa;EAAE;EAC/F;GAAE,IAAI;GAAY,OAAO,MAAM,YAAY;GAAa,MAAM;GAAa,KAAK,QAAQ,eAAe;EAAE;EACzG;GAAE,IAAI;GAAqB,OAAO,MAAM,qBAAqB;GAAkB,MAAM;GAAW,KAAK,QAAQ,cAAc;EAAE;EAC7H;GAAE,IAAI;GAAU,OAAO,MAAM,UAAU;GAAU,MAAM;GAAS,KAAK,QAAQ,aAAa;EAAE;CAChG;AACJ"}
|
package/dist/chips.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 { ChipSuggestion, ChipTrigger } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* The suggestions a chip trigger opens — `@` for people, `#` for tags — as a
|
|
7
|
+
* piece that attaches to any editor, the way the slash menu does. It opens
|
|
8
|
+
* when a trigger starts a word, asks for suggestions as the word is typed
|
|
9
|
+
* (and throws away an answer that arrives after a newer one), and puts the
|
|
10
|
+
* chosen one in as a chip in place of the trigger and the words after it.
|
|
11
|
+
*/
|
|
12
|
+
export interface ChipMenuOptions {
|
|
13
|
+
editor: EditorInstance;
|
|
14
|
+
/** The `contenteditable` element, where the keys are heard. */
|
|
15
|
+
content: () => HTMLElement | null;
|
|
16
|
+
/** What the list hangs from: an element over the caret. */
|
|
17
|
+
anchor: () => HTMLElement | null;
|
|
18
|
+
/** The triggers; read every time the document changes. */
|
|
19
|
+
triggers: () => ChipTrigger[];
|
|
20
|
+
part: (name: string, state?: unknown) => Props;
|
|
21
|
+
locale?: () => Locale;
|
|
22
|
+
enabled?: () => boolean;
|
|
23
|
+
/** Puts the anchor where the caret is, before the list opens. */
|
|
24
|
+
place?: () => void;
|
|
25
|
+
/** The list's id, for the options' ids and `aria-controls`. */
|
|
26
|
+
id?: string;
|
|
27
|
+
overlayTarget?: OverlayTarget;
|
|
28
|
+
zIndex?: number;
|
|
29
|
+
/** A chip was put in. */
|
|
30
|
+
onInsert?: (suggestion: ChipSuggestion) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface ChipMenu {
|
|
33
|
+
readonly isOpen: boolean;
|
|
34
|
+
sync(): void;
|
|
35
|
+
close(): void;
|
|
36
|
+
destroy(): void;
|
|
37
|
+
}
|
|
38
|
+
export declare function createChipMenu(options: ChipMenuOptions): ChipMenu;
|
package/dist/chips.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { slashMenuView } from "./render/menus.js";
|
|
2
|
+
import { EDITOR_CHIP_CHAR, editorSelectionRange, editorTextblocks, en } from "@vitral/core";
|
|
3
|
+
import { createOverlay } from "@vitral/controls";
|
|
4
|
+
//#region src/chips.ts
|
|
5
|
+
function createChipMenu(options) {
|
|
6
|
+
let open = null;
|
|
7
|
+
let element = null;
|
|
8
|
+
/** Bumped by every query, so a slow answer to an old one is dropped. */
|
|
9
|
+
let asked = 0;
|
|
10
|
+
const locale = () => options.locale?.() ?? en;
|
|
11
|
+
const enabled = () => options.enabled?.() ?? true;
|
|
12
|
+
const id = options.id ?? "vt-chips";
|
|
13
|
+
const overlay = createOverlay({
|
|
14
|
+
anchor: options.anchor,
|
|
15
|
+
render: () => open ? slashMenuView({
|
|
16
|
+
locale: locale(),
|
|
17
|
+
part: options.part,
|
|
18
|
+
ids: {
|
|
19
|
+
slash: id,
|
|
20
|
+
block: `${id}-block`
|
|
21
|
+
}
|
|
22
|
+
}, {
|
|
23
|
+
id,
|
|
24
|
+
query: open.query,
|
|
25
|
+
items: open.items.map((s) => ({
|
|
26
|
+
id: s.id,
|
|
27
|
+
label: s.label,
|
|
28
|
+
description: s.description,
|
|
29
|
+
icon: s.icon
|
|
30
|
+
})),
|
|
31
|
+
active: open.active,
|
|
32
|
+
labels: {
|
|
33
|
+
list: locale().editor.chipMenu,
|
|
34
|
+
empty: locale().editor.chipEmpty
|
|
35
|
+
}
|
|
36
|
+
}, { choose: insert }) : null,
|
|
37
|
+
placement: "bottom-start",
|
|
38
|
+
target: () => options.overlayTarget,
|
|
39
|
+
zIndex: options.zIndex,
|
|
40
|
+
restoreFocus: false,
|
|
41
|
+
onClose: () => open = null
|
|
42
|
+
});
|
|
43
|
+
/** The text of the caret's block up to the caret; a chip already in it reads as one character. */
|
|
44
|
+
function textBeforeCaret() {
|
|
45
|
+
const state = options.editor.state;
|
|
46
|
+
if (!editorSelectionRange(state.selection).empty) return null;
|
|
47
|
+
const path = state.selection.head.path.join(".");
|
|
48
|
+
const block = editorTextblocks(state.doc).find((entry) => entry.path.join(".") === path)?.node;
|
|
49
|
+
if (!block || block.type === "codeBlock") return null;
|
|
50
|
+
return (block.content ?? []).map((child) => child.type === "text" ? child.text ?? "" : child.type === "chip" ? EDITOR_CHIP_CHAR : "\n").join("").slice(0, state.selection.head.offset);
|
|
51
|
+
}
|
|
52
|
+
/** The trigger that starts the word the caret is in, and the words after it. */
|
|
53
|
+
function find() {
|
|
54
|
+
const before = textBeforeCaret();
|
|
55
|
+
if (before === null) return null;
|
|
56
|
+
let best = null;
|
|
57
|
+
for (const trigger of options.triggers()) {
|
|
58
|
+
const char = trigger.char ?? "@";
|
|
59
|
+
const at = before.lastIndexOf(char);
|
|
60
|
+
if (at < 0) continue;
|
|
61
|
+
const starts = at === 0 || /\s/.test(before[at - 1] ?? "");
|
|
62
|
+
const query = before.slice(at + char.length);
|
|
63
|
+
if (!starts || /\n|\s{2}/.test(query) || query.includes(EDITOR_CHIP_CHAR) || query.length > 40) continue;
|
|
64
|
+
if (!best || at > best.at) best = {
|
|
65
|
+
trigger,
|
|
66
|
+
query,
|
|
67
|
+
at
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return best ? {
|
|
71
|
+
trigger: best.trigger,
|
|
72
|
+
query: best.query
|
|
73
|
+
} : null;
|
|
74
|
+
}
|
|
75
|
+
function sync() {
|
|
76
|
+
if (!enabled()) return close();
|
|
77
|
+
const found = find();
|
|
78
|
+
if (!found) return close();
|
|
79
|
+
if (open && open.trigger === found.trigger && open.query === found.query) return;
|
|
80
|
+
const ask = ++asked;
|
|
81
|
+
const answer = found.trigger.items(found.query);
|
|
82
|
+
const show = (items) => {
|
|
83
|
+
if (ask !== asked) return;
|
|
84
|
+
open = {
|
|
85
|
+
trigger: found.trigger,
|
|
86
|
+
query: found.query,
|
|
87
|
+
items,
|
|
88
|
+
active: open && open.query === found.query ? open.active : 0
|
|
89
|
+
};
|
|
90
|
+
options.place?.();
|
|
91
|
+
overlay.open();
|
|
92
|
+
overlay.update();
|
|
93
|
+
};
|
|
94
|
+
if (answer instanceof Promise) answer.then(show, () => show([]));
|
|
95
|
+
else show(answer);
|
|
96
|
+
}
|
|
97
|
+
function close() {
|
|
98
|
+
asked++;
|
|
99
|
+
if (!overlay.isOpen) {
|
|
100
|
+
open = null;
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
overlay.close();
|
|
104
|
+
}
|
|
105
|
+
/** Takes the trigger and its words out, and puts the chip in their place. */
|
|
106
|
+
function insert(index) {
|
|
107
|
+
if (!open) return;
|
|
108
|
+
const suggestion = open.items[index];
|
|
109
|
+
if (!suggestion) return;
|
|
110
|
+
const remove = open.query.length + (open.trigger.char ?? "@").length;
|
|
111
|
+
const kind = suggestion.kind ?? open.trigger.kind ?? null;
|
|
112
|
+
close();
|
|
113
|
+
for (let n = 0; n < remove; n++) options.editor.backspace("char");
|
|
114
|
+
options.editor.run("insertChip", {
|
|
115
|
+
id: suggestion.id,
|
|
116
|
+
label: suggestion.label,
|
|
117
|
+
kind
|
|
118
|
+
});
|
|
119
|
+
options.onInsert?.(suggestion);
|
|
120
|
+
}
|
|
121
|
+
/** While the list is open those keys are the list's, heard before the editor hears them. */
|
|
122
|
+
function onKeydown(event) {
|
|
123
|
+
if (!open) return;
|
|
124
|
+
const count = open.items.length;
|
|
125
|
+
const take = () => {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
event.stopImmediatePropagation();
|
|
128
|
+
};
|
|
129
|
+
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
130
|
+
if (!count) return;
|
|
131
|
+
take();
|
|
132
|
+
open = {
|
|
133
|
+
...open,
|
|
134
|
+
active: (open.active + (event.key === "ArrowDown" ? 1 : -1) + count) % count
|
|
135
|
+
};
|
|
136
|
+
overlay.update();
|
|
137
|
+
} else if (event.key === "Enter" || event.key === "Tab") {
|
|
138
|
+
if (!count) return;
|
|
139
|
+
take();
|
|
140
|
+
insert(open.active);
|
|
141
|
+
} else if (event.key === "Escape") {
|
|
142
|
+
take();
|
|
143
|
+
close();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function listen() {
|
|
147
|
+
const next = options.content();
|
|
148
|
+
if (next === element) return;
|
|
149
|
+
element?.removeEventListener("keydown", onKeydown, true);
|
|
150
|
+
element = next;
|
|
151
|
+
element?.addEventListener("keydown", onKeydown, true);
|
|
152
|
+
}
|
|
153
|
+
const stop = options.editor.subscribe(() => {
|
|
154
|
+
listen();
|
|
155
|
+
sync();
|
|
156
|
+
});
|
|
157
|
+
listen();
|
|
158
|
+
return {
|
|
159
|
+
get isOpen() {
|
|
160
|
+
return overlay.isOpen;
|
|
161
|
+
},
|
|
162
|
+
sync() {
|
|
163
|
+
listen();
|
|
164
|
+
sync();
|
|
165
|
+
},
|
|
166
|
+
close,
|
|
167
|
+
destroy() {
|
|
168
|
+
stop();
|
|
169
|
+
element?.removeEventListener("keydown", onKeydown, true);
|
|
170
|
+
element = null;
|
|
171
|
+
overlay.destroy();
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
export { createChipMenu };
|
|
177
|
+
|
|
178
|
+
//# sourceMappingURL=chips.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chips.js","names":[],"sources":["../src/chips.ts"],"sourcesContent":["import { editorSelectionRange, editorTextblocks, en, EDITOR_CHIP_CHAR, type EditorInstance, type EditorNode, type Locale } from '@vitral/core';\nimport { createOverlay, type OverlayTarget } from '@vitral/controls';\nimport type { Props } from '@vitral/dom';\nimport { slashMenuView } from './render/menus';\nimport type { ChipSuggestion, ChipTrigger } from './types';\n\n/**\n * The suggestions a chip trigger opens — `@` for people, `#` for tags — as a\n * piece that attaches to any editor, the way the slash menu does. It opens\n * when a trigger starts a word, asks for suggestions as the word is typed\n * (and throws away an answer that arrives after a newer one), and puts the\n * chosen one in as a chip in place of the trigger and the words after it.\n */\n\nexport interface ChipMenuOptions {\n editor: EditorInstance;\n /** The `contenteditable` element, where the keys are heard. */\n content: () => HTMLElement | null;\n /** What the list hangs from: an element over the caret. */\n anchor: () => HTMLElement | null;\n /** The triggers; read every time the document changes. */\n triggers: () => ChipTrigger[];\n part: (name: string, state?: unknown) => Props;\n locale?: () => Locale;\n enabled?: () => boolean;\n /** Puts the anchor where the caret is, before the list opens. */\n place?: () => void;\n /** The list's id, for the options' ids and `aria-controls`. */\n id?: string;\n overlayTarget?: OverlayTarget;\n zIndex?: number;\n /** A chip was put in. */\n onInsert?: (suggestion: ChipSuggestion) => void;\n}\n\nexport interface ChipMenu {\n readonly isOpen: boolean;\n sync(): void;\n close(): void;\n destroy(): void;\n}\n\ninterface OpenState {\n trigger: ChipTrigger;\n query: string;\n items: ChipSuggestion[];\n active: number;\n}\n\nexport function createChipMenu(options: ChipMenuOptions): ChipMenu {\n let open: OpenState | null = null;\n let element: HTMLElement | null = null;\n /** Bumped by every query, so a slow answer to an old one is dropped. */\n let asked = 0;\n\n const locale = () => options.locale?.() ?? en;\n const enabled = () => options.enabled?.() ?? true;\n const id = options.id ?? 'vt-chips';\n\n const overlay = createOverlay({\n anchor: options.anchor,\n render: () =>\n open\n ? (slashMenuView(\n { locale: locale(), part: options.part, ids: { slash: id, block: `${id}-block` } },\n {\n id,\n query: open.query,\n items: open.items.map((s) => ({ id: s.id, label: s.label, description: s.description, icon: s.icon })),\n active: open.active,\n labels: { list: locale().editor.chipMenu, empty: locale().editor.chipEmpty }\n },\n { choose: insert }\n ) as never)\n : null,\n placement: 'bottom-start',\n target: () => options.overlayTarget,\n zIndex: options.zIndex,\n restoreFocus: false,\n onClose: () => (open = null)\n });\n\n /** The text of the caret's block up to the caret; a chip already in it reads as one character. */\n function textBeforeCaret(): string | null {\n const state = options.editor.state;\n if (!editorSelectionRange(state.selection).empty) return null;\n const path = state.selection.head.path.join('.');\n const block = editorTextblocks(state.doc).find((entry) => entry.path.join('.') === path)?.node as EditorNode | undefined;\n if (!block || block.type === 'codeBlock') return null;\n const text = (block.content ?? []).map((child) => (child.type === 'text' ? (child.text ?? '') : child.type === 'chip' ? EDITOR_CHIP_CHAR : '\\n')).join('');\n return text.slice(0, state.selection.head.offset);\n }\n\n /** The trigger that starts the word the caret is in, and the words after it. */\n function find(): { trigger: ChipTrigger; query: string } | null {\n const before = textBeforeCaret();\n if (before === null) return null;\n let best: { trigger: ChipTrigger; query: string; at: number } | null = null;\n for (const trigger of options.triggers()) {\n const char = trigger.char ?? '@';\n const at = before.lastIndexOf(char);\n if (at < 0) continue;\n const starts = at === 0 || /\\s/.test(before[at - 1] ?? '');\n const query = before.slice(at + char.length);\n // A mention is a word or two: a new line, or a second space, ends the search.\n if (!starts || /\\n|\\s{2}/.test(query) || query.includes(EDITOR_CHIP_CHAR) || query.length > 40) continue;\n if (!best || at > best.at) best = { trigger, query, at };\n }\n return best ? { trigger: best.trigger, query: best.query } : null;\n }\n\n function sync() {\n if (!enabled()) return close();\n const found = find();\n if (!found) return close();\n const same = open && open.trigger === found.trigger && open.query === found.query;\n if (same) return;\n const ask = ++asked;\n const answer = found.trigger.items(found.query);\n const show = (items: ChipSuggestion[]) => {\n if (ask !== asked) return;\n open = { trigger: found.trigger, query: found.query, items, active: open && open.query === found.query ? open.active : 0 };\n options.place?.();\n overlay.open();\n overlay.update();\n };\n if (answer instanceof Promise) answer.then(show, () => show([]));\n else show(answer);\n }\n\n function close() {\n asked++;\n if (!overlay.isOpen) {\n open = null;\n return;\n }\n overlay.close();\n }\n\n /** Takes the trigger and its words out, and puts the chip in their place. */\n function insert(index: number) {\n if (!open) return;\n const suggestion = open.items[index];\n if (!suggestion) return;\n const remove = open.query.length + (open.trigger.char ?? '@').length;\n const kind = suggestion.kind ?? open.trigger.kind ?? null;\n close();\n for (let n = 0; n < remove; n++) options.editor.backspace('char');\n (options.editor.run as (name: string, ...args: unknown[]) => boolean)('insertChip', { id: suggestion.id, label: suggestion.label, kind });\n options.onInsert?.(suggestion);\n }\n\n /** While the list is open those keys are the list's, heard before the editor hears them. */\n function onKeydown(event: KeyboardEvent) {\n if (!open) return;\n const count = open.items.length;\n const take = () => {\n event.preventDefault();\n event.stopImmediatePropagation();\n };\n if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {\n if (!count) return;\n take();\n open = { ...open, active: (open.active + (event.key === 'ArrowDown' ? 1 : -1) + count) % count };\n overlay.update();\n } else if (event.key === 'Enter' || event.key === 'Tab') {\n if (!count) return;\n take();\n insert(open.active);\n } else if (event.key === 'Escape') {\n take();\n close();\n }\n }\n\n function listen() {\n const next = options.content();\n if (next === element) return;\n element?.removeEventListener('keydown', onKeydown, true);\n element = next;\n element?.addEventListener('keydown', onKeydown, true);\n }\n\n const stop = options.editor.subscribe(() => {\n listen();\n sync();\n });\n listen();\n\n return {\n get isOpen() {\n return overlay.isOpen;\n },\n sync() {\n listen();\n sync();\n },\n close,\n destroy() {\n stop();\n element?.removeEventListener('keydown', onK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WAAW,IAAI;EACvD,UAAU;EACV,SAAS,iBAAiB,WAAW,WAAW,IAAI;CACxD;CAEA,MAAM,OAAO,QAAQ,OAAO,gBAAgB;EACxC,OAAO;EACP,KAAK;CACT,CAAC;CACD,OAAO;CAEP,OAAO;EACH,IAAI,SAAS;GACT,OAAO,QAAQ;EACnB;EACA,OAAO;GACH,OAAO;GACP,KAAK;EACT;EACA;EACA,UAAU;GACN,KAAK;GACL,SAAS,oBAAoB,WAAW,WAAW,IAAI;GACvD,UAAU;GACV,QAAQ,QAAQ;EACpB;CACJ;AACJ"}
|
package/dist/editor.js
CHANGED
|
@@ -2,6 +2,7 @@ import { defaultBubbleMenu, defaultToolbar, editorIcons } from "./buttons.js";
|
|
|
2
2
|
import { bubbleView, toolbarView } from "./render/toolbar.js";
|
|
3
3
|
import { colorPanelView, imagePanelView, linkPanelView, tablePanelView } from "./render/menus.js";
|
|
4
4
|
import { createBlockHandle, defaultBlockActions } from "./block.js";
|
|
5
|
+
import { createChipMenu } from "./chips.js";
|
|
5
6
|
import { createSlashMenu, defaultSlashCommands } from "./slash.js";
|
|
6
7
|
import { activeEditorBlockType, activeEditorMark, ariaEditorShortcut, createEditor, createEditorView, editorLinkAt, editorLinkHint, editorPalette, editorSelectionRange, editorShortcutFor, en, followEditorLink, formatEditorShortcut, formatMessage, isEmptyEditorDoc, loadStyle, matchEditorPaletteColor, visuallyHidden } from "@vitral/core";
|
|
7
8
|
import { createOverlay, createSelect, createTooltips } from "@vitral/controls";
|
|
@@ -212,6 +213,19 @@ function createTextEditor(element, config = {}) {
|
|
|
212
213
|
bubble.update();
|
|
213
214
|
} else bubble.close();
|
|
214
215
|
}
|
|
216
|
+
const chipMenu = createChipMenu({
|
|
217
|
+
editor,
|
|
218
|
+
content: () => contentEl,
|
|
219
|
+
anchor: () => caretEl,
|
|
220
|
+
triggers: () => current.chips ? Array.isArray(current.chips) ? current.chips : [current.chips] : [],
|
|
221
|
+
part,
|
|
222
|
+
locale,
|
|
223
|
+
enabled: () => !!current.chips && editable(),
|
|
224
|
+
place: placeCaret,
|
|
225
|
+
id: `${ids.slash}-chips`,
|
|
226
|
+
overlayTarget: current.overlayTarget,
|
|
227
|
+
zIndex: current.zIndex
|
|
228
|
+
});
|
|
215
229
|
const slashMenu = createSlashMenu({
|
|
216
230
|
editor,
|
|
217
231
|
content: () => contentEl,
|
|
@@ -234,6 +248,7 @@ function createTextEditor(element, config = {}) {
|
|
|
234
248
|
editor,
|
|
235
249
|
host: () => element,
|
|
236
250
|
caretRect: () => view?.selectionRect() ?? null,
|
|
251
|
+
content: () => contentEl,
|
|
237
252
|
actions: () => Array.isArray(current.blockMenu) ? current.blockMenu : defaultBlockActions(locale()),
|
|
238
253
|
part,
|
|
239
254
|
locale,
|
|
@@ -364,11 +379,15 @@ function createTextEditor(element, config = {}) {
|
|
|
364
379
|
function countText() {
|
|
365
380
|
const words = locale().editor;
|
|
366
381
|
const count = editor.characterCount();
|
|
367
|
-
|
|
382
|
+
const characters = current.maxLength ? formatMessage(words.charactersLimit, {
|
|
368
383
|
count,
|
|
369
384
|
limit: current.maxLength
|
|
370
|
-
});
|
|
371
|
-
|
|
385
|
+
}) : formatMessage(count === 1 ? words.character : words.characters, { count });
|
|
386
|
+
const mode = current.showCount === true ? "characters" : current.showCount;
|
|
387
|
+
if (mode === "characters" || !mode) return characters;
|
|
388
|
+
const wordCount = editor.wordCount();
|
|
389
|
+
const wordText = formatMessage(wordCount === 1 ? words.word : words.words, { count: wordCount });
|
|
390
|
+
return mode === "words" ? wordText : `${wordText} · ${characters}`;
|
|
372
391
|
}
|
|
373
392
|
function contentView() {
|
|
374
393
|
return h("div", mergeAttrs({
|
|
@@ -435,6 +454,7 @@ function createTextEditor(element, config = {}) {
|
|
|
435
454
|
taskLabel: locale().editor.taskDone,
|
|
436
455
|
selectedClass: "vt-editor-node-selected",
|
|
437
456
|
emptyClass: "vt-editor-content-empty",
|
|
457
|
+
chipClass: (kind) => current.unstyled ? void 0 : ["vt-editor-chip", kind ? `vt-editor-chip-${kind}` : ""].filter(Boolean).join(" "),
|
|
438
458
|
handleKey: (_name, binding) => {
|
|
439
459
|
if (binding?.[0] !== "link" || !editable()) return false;
|
|
440
460
|
openLinkPanel();
|
|
@@ -486,6 +506,7 @@ function createTextEditor(element, config = {}) {
|
|
|
486
506
|
destroy() {
|
|
487
507
|
stop();
|
|
488
508
|
slashMenu.destroy();
|
|
509
|
+
chipMenu.destroy();
|
|
489
510
|
blockHandle.destroy();
|
|
490
511
|
[...panels(), bubble].forEach((overlay) => overlay.destroy());
|
|
491
512
|
blockSelect?.destroy();
|
package/dist/editor.js.map
CHANGED
|
@@ -1 +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 editorLinkAt,\n editorLinkHint,\n editorPalette,\n editorShortcutFor,\n editorSelectionRange,\n editorTextblocks,\n en,\n formatEditorShortcut,\n followEditorLink,\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 open: () => {\n followEditorLink(link.href.trim());\n linkPanel.close();\n },\n cancel: () => {\n linkPanel.close();\n focus();\n }\n };\n\n /** The link editor, filled in with the link under the caret when there is one. */\n function openLinkPanel(event?: MouseEvent) {\n if (!editable()) return;\n const at = editorLinkAt(editor.state);\n link = { href: at?.href ?? '', text: at?.text ?? '', newTab: at?.target === '_blank', existing: !!at };\n if (!event) placeCaret();\n openPanel(linkPanel, 'link', event);\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 else anchors.delete(name);\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) => openLinkPanel(event),\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 // Ctrl/⌘+K opens the link editor, over the caret.\n handleKey: (_name, binding) => {\n if (binding?.[0] !== 'link' || !editable()) return false;\n openLinkPanel();\n return true;\n },\n linkHint: (href) => ({ ...tooltips.options(editorLinkHint(href, locale().editor.followLink)), placement: 'top' })\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":";;;;;;;;;;;;;;;;;;AA6CA,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,0BAAU,IAAI,IAAyB;CAE7C,MAAM,KAAK,OAAO,MAAM,iBAAiB,EAAE;CAC3C,MAAM,MAAM;EAAE,SAAS,GAAG,GAAG;EAAW,MAAM,GAAG,GAAG;EAAQ,OAAO,GAAG,GAAG;EAAS,OAAO,GAAG,GAAG;CAAQ;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,SAAS;GAChB,MAAM,QAAQ,OAAO,SAAS,IAAI;GAClC,OAAO;GACP,OAAO;EACX;EACA,aAAa,UAAU;GACnB,OAAO,WAAW,KAAK;GACvB,OAAO;EACX;EACA,MAAM,MAAM,GAAG,SAAS;GACpB,MAAM,UAAU,IAAI,MAAM,GAAG,IAAI;GACjC,OAAO;GACP,OAAO;EACX;EACA;EACA,WAAW,MAAM,UAAU,OAAO,SAAS,MAAM,KAAc;EAC/D;EACA,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"}
|
|
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 editorLinkAt,\n editorLinkHint,\n editorPalette,\n editorShortcutFor,\n editorSelectionRange,\n editorTextblocks,\n en,\n formatEditorShortcut,\n followEditorLink,\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 { createChipMenu } from './chips';\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 open: () => {\n followEditorLink(link.href.trim());\n linkPanel.close();\n },\n cancel: () => {\n linkPanel.close();\n focus();\n }\n };\n\n /** The link editor, filled in with the link under the caret when there is one. */\n function openLinkPanel(event?: MouseEvent) {\n if (!editable()) return;\n const at = editorLinkAt(editor.state);\n link = { href: at?.href ?? '', text: at?.text ?? '', newTab: at?.target === '_blank', existing: !!at };\n if (!event) placeCaret();\n openPanel(linkPanel, 'link', event);\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 chipMenu = createChipMenu({\n editor,\n content: () => contentEl,\n anchor: () => caretEl,\n triggers: () => (current.chips ? (Array.isArray(current.chips) ? current.chips : [current.chips]) : []),\n part,\n locale,\n enabled: () => !!current.chips && editable(),\n place: placeCaret,\n id: `${ids.slash}-chips`,\n overlayTarget: current.overlayTarget,\n zIndex: current.zIndex\n });\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 content: () => contentEl,\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 else anchors.delete(name);\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) => openLinkPanel(event),\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 const characters = current.maxLength\n ? formatMessage(words.charactersLimit, { count, limit: current.maxLength })\n : formatMessage(count === 1 ? words.character : words.characters, { count });\n const mode = current.showCount === true ? 'characters' : current.showCount;\n if (mode === 'characters' || !mode) return characters;\n const wordCount = editor.wordCount();\n const wordText = formatMessage(wordCount === 1 ? words.word : words.words, { count: wordCount });\n return mode === 'words' ? wordText : `${wordText} · ${characters}`;\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 chipClass: (kind) => (current.unstyled ? undefined : ['vt-editor-chip', kind ? `vt-editor-chip-${kind}` : ''].filter(Boolean).join(' ')),\n // Ctrl/⌘+K opens the link editor, over the caret.\n handleKey: (_name, binding) => {\n if (binding?.[0] !== 'link' || !editable()) return false;\n openLinkPanel();\n return true;\n },\n linkHint: (href) => ({ ...tooltips.options(editorLinkHint(href, locale().editor.followLink)), placement: 'top' })\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 chipMenu.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":";;;;;;;;;;;;;;;;;;;AA8CA,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,0BAAU,IAAI,IAAyB;CAE7C,MAAM,KAAK,OAAO,MAAM,iBAAiB,EAAE;CAC3C,MAAM,MAAM;EAAE,SAAS,GAAG,GAAG;EAAW,MAAM,GAAG,GAAG;EAAQ,OAAO,GAAG,GAAG;EAAS,OAAO,GAAG,GAAG;CAAQ;CACvG,MAAM,OAAO,WAAW,OAAO;CAC/B,MAAM,eAAuB,QAAQ,UAAU;CAC/C,MAAM,gBAAwC,QAAQ,WAAW;CACjE,MAAM,OAAO,aAAa;EACtB,OAAO;EACP,gBAAgB,CAAC,CAAC,QAAQ;EAC1B,eAAe,QAAQ;EACvB,UAAU,QAAQ;EAClB,aAAa;CACjB,CAAC;CAED,IAAI,CAAC,OAAO,UAAU;EAClB,MAAM,UAAU;GAAE,OAAO,OAAO;GAAO,UAAU,OAAO;EAAS;EACjE,UAAU,UAAU,MAAM,UAAU,KAAK,OAAO;EAChD,UAAU,YAAY,MAAM,YAAY,KAAK,OAAO;EACpD,UAAU,YAAY,MAAM,YAAY,KAAK,OAAO;CACxD;CAMA,MAAM,WAAW,sBAAsB;EACnC,WAAW;EACX,UAAU,QAAQ;EAClB,OAAO,QAAQ;EACf,UAAU,QAAQ;EAClB,QAAQ,QAAQ;EAChB,IAAI,QAA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EACX;EACA,eAAe,OAAO,QAAQ;EAC9B,eAAe,OAAO,QAAQ;EAC9B,eAAe,OAAO,QAAQ;EAC9B,mBAAmB,OAAO,YAAY;EACtC,WAAW,SAAS;GAChB,MAAM,QAAQ,OAAO,SAAS,IAAI;GAClC,OAAO;GACP,OAAO;EACX;EACA,aAAa,UAAU;GACnB,OAAO,WAAW,KAAK;GACvB,OAAO;EACX;EACA,MAAM,MAAM,GAAG,SAAS;GACpB,MAAM,UAAU,IAAI,MAAM,GAAG,IAAI;GACjC,OAAO;GACP,OAAO;EACX;EACA;EACA,WAAW,MAAM,UAAU,OAAO,SAAS,MAAM,KAAc;EAC/D;EACA,SAAS;EACT,UAAU;GACN,KAAK;GACL,UAAU,QAAQ;GAClB,SAAS,QAAQ;GACjB,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
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export { createTextEditor } from './editor';
|
|
8
8
|
export { createSlashMenu, defaultSlashCommands, type SlashMenu, type SlashMenuOptions } from './slash';
|
|
9
|
+
export { createChipMenu, type ChipMenu, type ChipMenuOptions } from './chips';
|
|
9
10
|
export { createBlockHandle, defaultBlockActions, type BlockHandle, type BlockHandleOptions } from './block';
|
|
10
11
|
export { editorButtons, editorIcons, shortcutCommands, defaultToolbar, defaultBubbleMenu, type EditorButtonSpec } from './buttons';
|
|
11
12
|
export * from './types';
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { defaultBubbleMenu, defaultToolbar, editorButtons, editorIcons, shortcut
|
|
|
2
2
|
import { bubbleView, buttonView, iconView, toolbarView } from "./render/toolbar.js";
|
|
3
3
|
import { blockHandleView, blockMenuView, colorPanelView, imagePanelView, linkPanelView, slashMenuView, tablePanelView } from "./render/menus.js";
|
|
4
4
|
import { createBlockHandle, defaultBlockActions } from "./block.js";
|
|
5
|
+
import { createChipMenu } from "./chips.js";
|
|
5
6
|
import { createSlashMenu, defaultSlashCommands } from "./slash.js";
|
|
6
7
|
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 };
|
|
8
|
+
export { blockHandleView, blockMenuView, bubbleView, buttonView, colorPanelView, createBlockHandle, createChipMenu, createSlashMenu, createTextEditor, defaultBlockActions, defaultBubbleMenu, defaultSlashCommands, defaultToolbar, editorButtons, editorIcons, iconView, imagePanelView, linkPanelView, shortcutCommands, slashMenuView, tablePanelView, toolbarView };
|
package/dist/render/menus.d.ts
CHANGED
|
@@ -48,6 +48,13 @@ export interface SlashMenuState {
|
|
|
48
48
|
query: string;
|
|
49
49
|
items: SlashCommand[];
|
|
50
50
|
active: number;
|
|
51
|
+
/** The list's name and the words when nothing matches; the slash menu's otherwise. */
|
|
52
|
+
labels?: {
|
|
53
|
+
list: string;
|
|
54
|
+
empty: string;
|
|
55
|
+
};
|
|
56
|
+
/** Where the options' ids start; the slash menu's id otherwise. */
|
|
57
|
+
id?: string;
|
|
51
58
|
}
|
|
52
59
|
/**
|
|
53
60
|
* What a `/` opens: a listbox of commands, filtered as it is typed. The text
|
package/dist/render/menus.js
CHANGED
|
@@ -127,8 +127,9 @@ function imagePanelView(context, on) {
|
|
|
127
127
|
*/
|
|
128
128
|
function slashMenuView(context, state, on) {
|
|
129
129
|
const { part, locale } = context;
|
|
130
|
-
const
|
|
131
|
-
|
|
130
|
+
const menuId = state.id ?? context.ids.slash;
|
|
131
|
+
const optionId = (index) => `${menuId}-option-${index}`;
|
|
132
|
+
return h("div", mergeAttrs({ id: menuId }, part("slashMenu")), state.items.length ? h("ul", mergeAttrs({ role: "listbox" }, part("slashList"), { "aria-label": state.labels?.list ?? locale.editor.slashMenu }), state.items.map((item, index) => h("li", mergeAttrs({
|
|
132
133
|
key: item.id,
|
|
133
134
|
id: optionId(index),
|
|
134
135
|
role: "option"
|
|
@@ -140,7 +141,7 @@ function slashMenuView(context, state, on) {
|
|
|
140
141
|
if (state.active === index) return;
|
|
141
142
|
state.active = index;
|
|
142
143
|
}
|
|
143
|
-
}), 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 })));
|
|
144
|
+
}), 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(state.labels?.empty ?? locale.editor.slashEmpty, { query: state.query })));
|
|
144
145
|
}
|
|
145
146
|
function blockMenuView(context, block, actions, on) {
|
|
146
147
|
const { part, locale } = context;
|
package/dist/render/menus.js.map
CHANGED
|
@@ -1 +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 /** Follows the link, in a new tab. */\n open: () => 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 ? [\n h('button', mergeAttrs({ key: 'open', type: 'button', class: 'vt-button vt-button-text vt-button-secondary vt-button-sm' }, { onClick: on.open }), words.openLink),\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 ]\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":";;;;AAqCA,SAAgB,cAAc,SAAuB,OAAuB,IAAgC;CACxG,MAAM,EAAE,MAAM,WAAW;CACzB,MAAM,QAAQ,OAAO;CACrB,MAAM,SAAS,OAAe,OAAe,KAAsB,OAAO,WACtE,EACI,SACA,WAAW,EAAE,IAAI,GAAG,KAAK,OAAO,CAAC,GACjC,EAAE,QAAQ,KAAK,YAAY,GAAG,KAAK,GACnC,EACI,QACA,EAAE,OAAO,sCAAsC,GAC/C,EACI,SACA,WAAW;EAAE;EAAM,OAAO;CAAiB,GAAG;EAC1C;EACA,UAAU,UAAiB,GAAG,OAAO,GAAG,MAAO,MAAM,OAA4B,MAAM,CAA4B;EACnH,YAAY,UAAyB;GACjC,IAAI,MAAM,QAAQ,SAAS;GAC3B,MAAM,eAAe;GACrB,GAAG,MAAM;EACb;CACJ,CAAC,CACL,CACJ,CACJ;CACJ,OAAO,EACH,OACA,WAAW,EAAE,MAAM,SAAS,GAAG,KAAK,OAAO,GAAG,EAAE,cAAc,MAAM,KAAK,CAAC,GAC1E,MAAM,MAAM,SAAS,MAAM,MAAM,QAAQ,KAAK,GAC9C,MAAM,MAAM,UAAU,MAAM,MAAM,MAAM,GACxC,EACI,SACA,WAAW,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":[],"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 /** Follows the link, in a new tab. */\n open: () => 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 ? [\n h('button', mergeAttrs({ key: 'open', type: 'button', class: 'vt-button vt-button-text vt-button-secondary vt-button-sm' }, { onClick: on.open }), words.openLink),\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 ]\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 /** The list's name and the words when nothing matches; the slash menu's otherwise. */\n labels?: { list: string; empty: string };\n /** Where the options' ids start; the slash menu's id otherwise. */\n id?: string;\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 menuId = state.id ?? context.ids.slash;\n const optionId = (index: number) => `${menuId}-option-${index}`;\n return h(\n 'div',\n mergeAttrs({ id: menuId }, part('slashMenu')),\n state.items.length\n ? h(\n 'ul',\n mergeAttrs({ role: 'listbox' }, part('slashList'), { 'aria-label': state.labels?.list ?? 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(state.labels?.empty ?? 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":";;;;AAqCA,SAAgB,cAAc,SAAuB,OAAuB,IAAgC;CACxG,MAAM,EAAE,MAAM,WAAW;CACzB,MAAM,QAAQ,OAAO;CACrB,MAAM,SAAS,OAAe,OAAe,KAAsB,OAAO,WACtE,EACI,SACA,WAAW,EAAE,IAAI,GAAG,KAAK,OAAO,CAAC,GACjC,EAAE,QAAQ,KAAK,YAAY,GAAG,KAAK,GACnC,EACI,Q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AC,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"}
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,31 @@ export type EditorToolbarItem = 'blockType' | 'paragraph' | 'heading1' | 'headin
|
|
|
11
11
|
/** The commands a plain button can run. */
|
|
12
12
|
export type EditorButtonCommand = Exclude<EditorToolbarItem, 'blockType' | 'color' | 'highlight' | 'image' | 'table'>;
|
|
13
13
|
/** One entry of the menu a slash opens. */
|
|
14
|
+
/** Something a chip trigger offers: who or what the chip will stand for. */
|
|
15
|
+
export interface ChipSuggestion {
|
|
16
|
+
/** What the chip stands for, for the application. */
|
|
17
|
+
id: string;
|
|
18
|
+
/** The words on the chip, and in the list. */
|
|
19
|
+
label: string;
|
|
20
|
+
/** A category the chip's look follows: `'mention'`, `'tag'`, `'variable'`. */
|
|
21
|
+
kind?: string;
|
|
22
|
+
/** A line under the label in the list: an email, a count. */
|
|
23
|
+
description?: string;
|
|
24
|
+
icon?: string;
|
|
25
|
+
}
|
|
26
|
+
/** A character that, typed at the start of a word, offers chips to put in its place. */
|
|
27
|
+
export interface ChipTrigger {
|
|
28
|
+
/** One character: `'@'` for people, `'#'` for tags. Defaults to `'@'`. */
|
|
29
|
+
char?: string;
|
|
30
|
+
/**
|
|
31
|
+
* What to offer for the words typed after the trigger: a list, or a
|
|
32
|
+
* promise of one — a search on the server. A late answer to an older
|
|
33
|
+
* query is thrown away.
|
|
34
|
+
*/
|
|
35
|
+
items: (query: string) => ChipSuggestion[] | Promise<ChipSuggestion[]>;
|
|
36
|
+
/** Given to every chip this trigger makes, unless its suggestion says otherwise. */
|
|
37
|
+
kind?: string;
|
|
38
|
+
}
|
|
14
39
|
export interface SlashCommand {
|
|
15
40
|
/** Identifies it, and is what a host matches on. */
|
|
16
41
|
id: string;
|
|
@@ -61,12 +86,21 @@ export interface TextEditorConfig {
|
|
|
61
86
|
/** The handle beside the block the caret is in, and what it offers. */
|
|
62
87
|
blockMenu?: boolean | BlockAction[];
|
|
63
88
|
placeholder?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Inline chips the reader puts in by typing a trigger: `{ char: '@',
|
|
91
|
+
* items: (q) => people.filter(…) }` for mentions, or several triggers at
|
|
92
|
+
* once. Chips can also be put in with `run('insertChip', { id, label, kind })`.
|
|
93
|
+
*/
|
|
94
|
+
chips?: ChipTrigger | ChipTrigger[];
|
|
64
95
|
/** The most characters the document may hold. */
|
|
65
96
|
maxLength?: number | null;
|
|
66
97
|
/** Milliseconds of quiet before an edit starts a new undo step. */
|
|
67
98
|
historyDelay?: number;
|
|
68
|
-
/**
|
|
69
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Shows a count under the text: `true` or `'characters'` for characters
|
|
101
|
+
* (and the limit, where there is one), `'words'` for words, `'both'` for both.
|
|
102
|
+
*/
|
|
103
|
+
showCount?: boolean | 'characters' | 'words' | 'both';
|
|
70
104
|
readonly?: boolean;
|
|
71
105
|
disabled?: boolean;
|
|
72
106
|
invalid?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitral/editor",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Framework-free rich text editor for Vitral: the toolbar, the panels and the content area over @vitral/core's editor engine, which the Vue, React and Angular components wrap.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vitral",
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@vitral/controls": "0.2.
|
|
48
|
-
"@vitral/
|
|
49
|
-
"@vitral/
|
|
50
|
-
"@vitral/
|
|
51
|
-
"@vitral/
|
|
47
|
+
"@vitral/controls": "0.2.6",
|
|
48
|
+
"@vitral/icons": "0.2.6",
|
|
49
|
+
"@vitral/core": "0.2.6",
|
|
50
|
+
"@vitral/dom": "0.2.6",
|
|
51
|
+
"@vitral/styles": "0.2.6"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "vite build && tsc -p tsconfig.build.json"
|