@kerebron/extension-menu 0.4.27 → 0.4.28

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/esm/icons.js ADDED
@@ -0,0 +1,212 @@
1
+ const SVG = 'http://www.w3.org/2000/svg';
2
+ const XLINK = 'http://www.w3.org/1999/xlink';
3
+ const prefix = 'kb-icon';
4
+ function hashPath(path) {
5
+ let hash = 0;
6
+ for (let i = 0; i < path.length; i++) {
7
+ hash = (((hash << 5) - hash) + path.charCodeAt(i)) | 0;
8
+ }
9
+ return hash;
10
+ }
11
+ export function getIcon(root, icon) {
12
+ let doc = (root.nodeType == 9 ? root : root.ownerDocument) ||
13
+ document;
14
+ let node = doc.createElement('div');
15
+ node.className = prefix;
16
+ if (icon.path) {
17
+ let { path, width, height } = icon;
18
+ let name = 'pm-icon-' + hashPath(path).toString(16);
19
+ if (!doc.getElementById(name)) {
20
+ buildSVG(root, name, icon);
21
+ }
22
+ let svg = node.appendChild(doc.createElementNS(SVG, 'svg'));
23
+ svg.style.width = (width / height) + 'em';
24
+ let use = svg.appendChild(doc.createElementNS(SVG, 'use'));
25
+ use.setAttributeNS(XLINK, 'href', /([^#]*)/.exec(doc.location.toString())[1] + '#' + name);
26
+ }
27
+ else if (icon.dom) {
28
+ node.appendChild(icon.dom.cloneNode(true));
29
+ }
30
+ else {
31
+ let { text, css } = icon;
32
+ node.appendChild(doc.createElement('span')).textContent = text || '';
33
+ if (css)
34
+ node.firstChild.style.cssText = css;
35
+ }
36
+ return node;
37
+ }
38
+ function buildSVG(root, name, data) {
39
+ let [doc, top] = root.nodeType == 9
40
+ ? [root, root.body]
41
+ : [root.ownerDocument || document, root];
42
+ let collection = doc.getElementById(prefix + '-collection');
43
+ if (!collection) {
44
+ collection = doc.createElementNS(SVG, 'svg');
45
+ collection.id = prefix + '-collection';
46
+ collection.style.display = 'none';
47
+ top.insertBefore(collection, top.firstChild);
48
+ }
49
+ let sym = doc.createElementNS(SVG, 'symbol');
50
+ sym.id = name;
51
+ sym.setAttribute('viewBox', '0 0 ' + data.width + ' ' + data.height);
52
+ let path = sym.appendChild(doc.createElementNS(SVG, 'path'));
53
+ path.setAttribute('d', data.path);
54
+ collection.appendChild(sym);
55
+ }
56
+ /// A set of basic editor-related icons. Contains the properties
57
+ /// `join`, `lift`, `selectParentNode`, `undo`, `redo`, `strong`, `em`,
58
+ /// `code`, `link`, `bulletList`, `orderedList`, and `blockquote`, each
59
+ /// holding an object that can be used as the `icon` option to
60
+ /// `MenuItem`.
61
+ export const icons = {
62
+ join: {
63
+ width: 800,
64
+ height: 900,
65
+ path: 'M0 75h800v125h-800z M0 825h800v-125h-800z M250 400h100v-100h100v100h100v100h-100v100h-100v-100h-100z',
66
+ },
67
+ lift: {
68
+ width: 1024,
69
+ height: 1024,
70
+ path: 'M219 310v329q0 7-5 12t-12 5q-8 0-13-5l-164-164q-5-5-5-13t5-13l164-164q5-5 13-5 7 0 12 5t5 12zM1024 749v109q0 7-5 12t-12 5h-987q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h987q7 0 12 5t5 12zM1024 530v109q0 7-5 12t-12 5h-621q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h621q7 0 12 5t5 12zM1024 310v109q0 7-5 12t-12 5h-621q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h621q7 0 12 5t5 12zM1024 91v109q0 7-5 12t-12 5h-987q-7 0-12-5t-5-12v-109q0-7 5-12t12-5h987q7 0 12 5t5 12z',
71
+ },
72
+ selectParentNode: {
73
+ width: 24,
74
+ height: 24,
75
+ path: 'M3 5h2V3c-1.1 0-2 .9-2 2zm0 8h2v-2H3v2zm4 8h2v-2H7v2zM3 9h2V7H3v2zm10-6h-2v2h2V3zm6 0v2h2c0-1.1-.9-2-2-2zM5 21v-2H3c0 1.1.9 2 2 2zm-2-4h2v-2H3v2zM9 3H7v2h2V3zm2 18h2v-2h-2v2zm8-8h2v-2h-2v2zm0 8c1.1 0 2-.9 2-2h-2v2zm0-12h2V7h-2v2zm0 8h2v-2h-2v2zm-4 4h2v-2h-2v2zm0-16h2V3h-2v2z',
76
+ },
77
+ undo: {
78
+ width: 24,
79
+ height: 24,
80
+ path: 'M12.5 8c-2.65 0-5.05 1-6.9 2.6L2 7v9h9l-3.62-3.62c1.39-1.16 3.16-1.88 5.12-1.88 3.54 0 6.55 2.31 7.6 5.5l2.37-.78C21.08 11.03 17.15 8 12.5 8z',
81
+ },
82
+ redo: {
83
+ width: 24,
84
+ height: 24,
85
+ path: 'M18.4 10.6C16.55 8.99 14.15 8 11.5 8c-4.65 0-8.58 3.03-9.96 7.22L3.9 16c1.05-3.19 4.05-5.5 7.6-5.5 1.95 0 3.73.72 5.12 1.88L13 16h9V7l-3.6 3.6z',
86
+ },
87
+ strong: {
88
+ width: 805,
89
+ height: 1024,
90
+ path: 'M317 869q42 18 80 18 214 0 214-191 0-65-23-102-15-25-35-42t-38-26-46-14-48-6-54-1q-41 0-57 5 0 30-0 90t-0 90q0 4-0 38t-0 55 2 47 6 38zM309 442q24 4 62 4 46 0 81-7t62-25 42-51 14-81q0-40-16-70t-45-46-61-24-70-8q-28 0-74 7 0 28 2 86t2 86q0 15-0 45t-0 45q0 26 0 39zM0 950l1-53q8-2 48-9t60-15q4-6 7-15t4-19 3-18 1-21 0-19v-37q0-561-12-585-2-4-12-8t-25-6-28-4-27-2-17-1l-2-47q56-1 194-6t213-5q13 0 39 0t38 0q40 0 78 7t73 24 61 40 42 59 16 78q0 29-9 54t-22 41-36 32-41 25-48 22q88 20 146 76t58 141q0 57-20 102t-53 74-78 48-93 27-100 8q-25 0-75-1t-75-1q-60 0-175 6t-132 6z',
91
+ },
92
+ em: {
93
+ width: 585,
94
+ height: 1024,
95
+ path: 'M0 949l9-48q3-1 46-12t63-21q16-20 23-57 0-4 35-165t65-310 29-169v-14q-13-7-31-10t-39-4-33-3l10-58q18 1 68 3t85 4 68 1q27 0 56-1t69-4 56-3q-2 22-10 50-17 5-58 16t-62 19q-4 10-8 24t-5 22-4 26-3 24q-15 84-50 239t-44 203q-1 5-7 33t-11 51-9 47-3 32l0 10q9 2 105 17-1 25-9 56-6 0-18 0t-18 0q-16 0-49-5t-49-5q-78-1-117-1-29 0-81 5t-69 6z',
96
+ },
97
+ underline: {
98
+ width: 585,
99
+ height: 1024,
100
+ path: 'M0 908h1024v64H0Z',
101
+ },
102
+ code: {
103
+ width: 896,
104
+ height: 1024,
105
+ path: 'M608 192l-96 96 224 224-224 224 96 96 288-320-288-320zM288 192l-288 320 288 320 96-96-224-224 224-224-96-96z',
106
+ },
107
+ link: {
108
+ width: 951,
109
+ height: 1024,
110
+ path: 'M832 694q0-22-16-38l-118-118q-16-16-38-16-24 0-41 18 1 1 10 10t12 12 8 10 7 14 2 15q0 22-16 38t-38 16q-8 0-15-2t-14-7-10-8-12-12-10-10q-18 17-18 41 0 22 16 38l117 118q15 15 38 15 22 0 38-14l84-83q16-16 16-38zM430 292q0-22-16-38l-117-118q-16-16-38-16-22 0-38 15l-84 83q-16 16-16 38 0 22 16 38l118 118q15 15 38 15 24 0 41-17-1-1-10-10t-12-12-8-10-7-14-2-15q0-22 16-38t38-16q8 0 15 2t14 7 10 8 12 12 10 10q18-17 18-41zM941 694q0 68-48 116l-84 83q-47 47-116 47-69 0-116-48l-117-118q-47-47-47-116 0-70 50-119l-50-50q-49 50-118 50-68 0-116-48l-118-118q-48-48-48-116t48-116l84-83q47-47 116-47 69 0 116 48l117 118q47 47 47 116 0 70-50 119l50 50q49-50 118-50 68 0 116 48l118 118q48 48 48 116z',
111
+ },
112
+ bulletList: {
113
+ width: 768,
114
+ height: 896,
115
+ path: 'M0 512h128v-128h-128v128zM0 256h128v-128h-128v128zM0 768h128v-128h-128v128zM256 512h512v-128h-512v128zM256 256h512v-128h-512v128zM256 768h512v-128h-512v128z',
116
+ },
117
+ orderedList: {
118
+ width: 768,
119
+ height: 896,
120
+ path: 'M320 512h448v-128h-448v128zM320 768h448v-128h-448v128zM320 128v128h448v-128h-448zM79 384h78v-256h-36l-85 23v50l43-2v185zM189 590c0-36-12-78-96-78-33 0-64 6-83 16l1 66c21-10 42-15 67-15s32 11 32 28c0 26-30 58-110 112v50h192v-67l-91 2c49-30 87-66 87-113l1-1z',
121
+ },
122
+ taskList: {
123
+ width: 24,
124
+ height: 24,
125
+ path: 'M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-9 14l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z',
126
+ },
127
+ blockquote: {
128
+ width: 640,
129
+ height: 896,
130
+ path: 'M0 448v256h256v-256h-128c0 0 0-128 128-128v-128c0 0-256 0-256 256zM640 320v-128c0 0-256 0-256 256v256h256v-256h-128c0 0 0-128 128-128z',
131
+ },
132
+ insert: {
133
+ width: 24,
134
+ height: 24,
135
+ path: 'M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z',
136
+ },
137
+ type: {
138
+ width: 20,
139
+ height: 22,
140
+ path: 'M2.5 3v3h5.5v13h4V6H17.5V3H2.5z',
141
+ },
142
+ table: {
143
+ width: 24,
144
+ height: 24,
145
+ path: 'M3 3v18h18V3H3zm8 16H5v-6h6v6zm0-8H5V5h6v6zm8 8h-6v-6h6v6zm0-8h-6V5h6v6z',
146
+ },
147
+ image: {
148
+ width: 24,
149
+ height: 24,
150
+ path: 'M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z',
151
+ },
152
+ horizontalRule: {
153
+ width: 24,
154
+ height: 24,
155
+ path: 'M4 11h16v2H4z',
156
+ },
157
+ heading: {
158
+ width: 18,
159
+ height: 23,
160
+ path: 'M2.5 3v17h3v-7h7v7h3V3h-3v8h-7V3h-3z',
161
+ },
162
+ strike: {
163
+ width: 24,
164
+ height: 24,
165
+ path: 'M10 19h4v-3h-4v3zM5 4v3h5v3h4V7h5V4H5zM3 14h18v-2H3v2z',
166
+ },
167
+ highlight: {
168
+ width: 24,
169
+ height: 24,
170
+ path: 'M17.75 7L14 3.25l-10 10V17h3.75l10-10zm2.96-2.96a.996.996 0 0 0 0-1.41L18.37.29a.996.996 0 0 0-1.41 0L15 2.25 18.75 6l1.96-1.96zM2 20h20v4H2z',
171
+ },
172
+ indent: {
173
+ width: 24,
174
+ height: 24,
175
+ path: 'M3 21h18v-2H3v2zM3 8v8l4-4-4-4zm8 9h10v-2H11v2zM3 3v2h18V3H3zm8 6h10V7H11v2zm0 4h10v-2H11v2z',
176
+ },
177
+ outdent: {
178
+ width: 24,
179
+ height: 24,
180
+ path: 'M3 21h18v-2H3v2zM7 8v8l-4-4 4-4zm4 9h10v-2H11v2zM3 3v2h18V3H3zm8 6h10V7H11v2zm0 4h10v-2H11v2z',
181
+ },
182
+ superscript: {
183
+ width: 24,
184
+ height: 24,
185
+ path: 'M22 7h-2v1h3v1h-4V7c0-.55.45-1 1-1h2V5h-3V4h3c.55 0 1 .45 1 1v1c0 .55-.45 1-1 1zM5.88 20h2.66l3.4-5.42h.12l3.4 5.42h2.66l-4.65-7.27L17.81 6h-2.68l-3.07 4.99h-.12L8.87 6H6.19l4.32 6.73L5.88 20z',
186
+ },
187
+ subscript: {
188
+ width: 24,
189
+ height: 24,
190
+ path: 'M22 18h-2v1h3v1h-4v-2c0-.55.45-1 1-1h2v-1h-3v-1h3c.55 0 1 .45 1 1v1c0 .55-.45 1-1 1zM5.88 18h2.66l3.4-5.42h.12l3.4 5.42h2.66l-4.65-7.27L17.81 4h-2.68l-3.07 4.99h-.12L8.87 4H6.19l4.32 6.73L5.88 18z',
191
+ },
192
+ alignLeft: {
193
+ width: 24,
194
+ height: 24,
195
+ path: 'M15 15H3v2h12v-2zm0-8H3v2h12V7zM3 13h18v-2H3v2zm0 8h18v-2H3v2zM3 3v2h18V3H3z',
196
+ },
197
+ alignCenter: {
198
+ width: 24,
199
+ height: 24,
200
+ path: 'M7 15v2h10v-2H7zm-4 6h18v-2H3v2zm0-8h18v-2H3v2zm4-6v2h10V7H7zM3 3v2h18V3H3z',
201
+ },
202
+ alignRight: {
203
+ width: 24,
204
+ height: 24,
205
+ path: 'M3 21h18v-2H3v2zm6-4h12v-2H9v2zm-6-4h18v-2H3v2zm6-4h12V7H9v2zM3 3v2h18V3H3z',
206
+ },
207
+ alignJustify: {
208
+ width: 24,
209
+ height: 24,
210
+ path: 'M3 21h18v-2H3v2zm0-4h18v-2H3v2zm0-4h18v-2H3v2zm0-4h18V7H3v2zm0-6v2h18V3H3z',
211
+ },
212
+ };
package/esm/menu.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { EditorView } from 'prosemirror-view';
2
+ import { EditorState, Transaction } from 'prosemirror-state';
3
+ export interface MenuElement {
4
+ render(pm: EditorView): {
5
+ dom: HTMLElement;
6
+ update: (state: EditorState) => boolean;
7
+ };
8
+ }
9
+ export declare class MenuItem implements MenuElement {
10
+ readonly spec: MenuItemSpec;
11
+ CSS_PREFIX: string;
12
+ constructor(spec: MenuItemSpec);
13
+ render(view: EditorView): {
14
+ dom: HTMLElement | null;
15
+ update: (state: EditorState) => boolean;
16
+ };
17
+ }
18
+ export type IconSpec = {
19
+ path: string;
20
+ width: number;
21
+ height: number;
22
+ } | {
23
+ text: string;
24
+ css?: string;
25
+ } | {
26
+ dom: Node;
27
+ };
28
+ export interface MenuItemSpec {
29
+ run: (state: EditorState, dispatch: (tr: Transaction) => void) => boolean | Promise<boolean>;
30
+ select?: (state: EditorState) => boolean;
31
+ enable?: (state: EditorState) => boolean;
32
+ active?: (state: EditorState) => boolean;
33
+ render?: (view: EditorView) => HTMLElement;
34
+ icon?: IconSpec;
35
+ label?: string;
36
+ title?: string | ((state: EditorState) => string);
37
+ class?: string;
38
+ css?: string;
39
+ }
40
+ export declare class Dropdown implements MenuElement {
41
+ readonly options: {
42
+ label?: string;
43
+ icon?: IconSpec;
44
+ title?: string;
45
+ class?: string;
46
+ css?: string;
47
+ };
48
+ CSS_PREFIX: string;
49
+ content: readonly MenuElement[];
50
+ constructor(content: readonly MenuElement[] | MenuElement, options?: {
51
+ label?: string;
52
+ icon?: IconSpec;
53
+ title?: string;
54
+ class?: string;
55
+ css?: string;
56
+ });
57
+ render(view: EditorView): {
58
+ dom: any;
59
+ update: (state: EditorState) => boolean;
60
+ };
61
+ expand(dom: HTMLElement, items: readonly Node[]): {
62
+ close: () => boolean;
63
+ node: any;
64
+ };
65
+ }
66
+ export declare class DropdownSubmenu implements MenuElement {
67
+ readonly options: {
68
+ label?: string;
69
+ };
70
+ content: readonly MenuElement[];
71
+ constructor(content: readonly MenuElement[] | MenuElement, options?: {
72
+ label?: string;
73
+ });
74
+ render(view: EditorView): {
75
+ dom: any;
76
+ update: (state: EditorState) => boolean;
77
+ };
78
+ }
79
+ export declare function renderGrouped(view: EditorView, content: readonly (readonly MenuElement[])[]): {
80
+ dom: any;
81
+ update: (state: EditorState) => boolean;
82
+ };
83
+ //# sourceMappingURL=menu.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"menu.d.ts","sourceRoot":"","sources":["../src/menu.ts"],"names":[],"mappings":"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"}
package/esm/menu.js ADDED
@@ -0,0 +1,333 @@
1
+ import { getIcon } from './icons.js';
2
+ /// An icon or label that, when clicked, executes a command.
3
+ export class MenuItem {
4
+ spec;
5
+ CSS_PREFIX = 'kb-menu__button';
6
+ /// Create a menu item.
7
+ constructor(
8
+ /// The spec used to create this item.
9
+ spec) {
10
+ this.spec = spec;
11
+ }
12
+ /// Renders the icon according to its [display
13
+ /// spec](#menu.MenuItemSpec.display), and adds an event handler which
14
+ /// executes the command when the representation is clicked.
15
+ render(view) {
16
+ let spec = this.spec;
17
+ let dom = spec.render ? spec.render(view) : null;
18
+ if (!dom) {
19
+ // Create a proper button element for better accessibility
20
+ dom = document.createElement('button');
21
+ dom.setAttribute('type', 'button');
22
+ // Add our new CSS classes while maintaining backward compatibility
23
+ dom.classList.add(this.CSS_PREFIX);
24
+ if (spec.icon) {
25
+ const icon = getIcon(view.root, spec.icon);
26
+ dom.appendChild(icon);
27
+ dom.classList.add(this.CSS_PREFIX + '--icon-only');
28
+ }
29
+ if (spec.label) {
30
+ const labelSpan = document.createElement('span');
31
+ labelSpan.appendChild(document.createTextNode(translate(view, spec.label)));
32
+ dom.appendChild(labelSpan);
33
+ if (spec.icon) {
34
+ dom.classList.remove(this.CSS_PREFIX + '--icon-only');
35
+ }
36
+ }
37
+ if (!spec.icon && !spec.label) {
38
+ throw new RangeError('MenuItem without icon or label property');
39
+ }
40
+ }
41
+ if (spec.title) {
42
+ const title = typeof spec.title === 'function'
43
+ ? spec.title(view.state)
44
+ : spec.title;
45
+ dom.setAttribute('title', translate(view, title));
46
+ dom.setAttribute('aria-label', translate(view, title));
47
+ }
48
+ if (spec.class)
49
+ dom.classList.add(spec.class);
50
+ if (spec.css)
51
+ dom.style.cssText += spec.css;
52
+ dom.addEventListener('mousedown', (e) => {
53
+ e.preventDefault();
54
+ if (!dom.classList.contains(this.CSS_PREFIX + '--disabled')) {
55
+ spec.run(view.state, view.dispatch);
56
+ }
57
+ });
58
+ const update = (state) => {
59
+ if (spec.select) {
60
+ let selected = spec.select(state);
61
+ dom.style.display = selected ? '' : 'none';
62
+ if (!selected)
63
+ return false;
64
+ }
65
+ let enabled = true;
66
+ if (spec.enable) {
67
+ enabled = spec.enable(state) || false;
68
+ setClass(dom, this.CSS_PREFIX + '--disabled', !enabled);
69
+ dom.setAttribute('aria-disabled', (!enabled).toString());
70
+ }
71
+ if (spec.active) {
72
+ let active = enabled && spec.active(state) || false;
73
+ setClass(dom, this.CSS_PREFIX + '--active', active);
74
+ dom.setAttribute('aria-pressed', active.toString());
75
+ }
76
+ return true;
77
+ };
78
+ return { dom, update };
79
+ }
80
+ }
81
+ function translate(view, text) {
82
+ return view._props.translate
83
+ ? view._props.translate(text)
84
+ : text;
85
+ }
86
+ let lastMenuEvent = {
87
+ time: 0,
88
+ node: null,
89
+ };
90
+ function markMenuEvent(e) {
91
+ lastMenuEvent.time = Date.now();
92
+ lastMenuEvent.node = e.target;
93
+ }
94
+ function isMenuEvent(wrapper) {
95
+ return Date.now() - 100 < lastMenuEvent.time &&
96
+ lastMenuEvent.node && wrapper.contains(lastMenuEvent.node);
97
+ }
98
+ /// A drop-down menu, displayed as a label with a downwards-pointing
99
+ /// triangle to the right of it.
100
+ export class Dropdown {
101
+ options;
102
+ CSS_PREFIX = 'kb-dropdown';
103
+ /// @internal
104
+ content;
105
+ /// Create a dropdown wrapping the elements.
106
+ constructor(content,
107
+ /// @internal
108
+ options = {}) {
109
+ this.options = options;
110
+ this.options = options || {};
111
+ this.content = Array.isArray(content) ? content : [content];
112
+ }
113
+ /// Render the dropdown menu and sub-items.
114
+ render(view) {
115
+ let content = renderDropdownItems(this.content, view);
116
+ let win = view.dom.ownerDocument.defaultView;
117
+ // Create a button element instead of div for better accessibility
118
+ let label = document.createElement('button');
119
+ label.setAttribute('type', 'button');
120
+ label.classList.add(this.CSS_PREFIX + '__label');
121
+ if (this.options.class) {
122
+ label.classList.add(this.options.class);
123
+ }
124
+ if (this.options.css)
125
+ label.setAttribute('style', this.options.css);
126
+ // Add icon if specified
127
+ if (this.options.icon) {
128
+ const icon = getIcon(view.root, this.options.icon);
129
+ label.appendChild(icon);
130
+ }
131
+ // Add text label (hidden by CSS in toolbar, visible in title/aria-label)
132
+ const labelText = translate(view, this.options.label || '');
133
+ if (labelText) {
134
+ const textSpan = document.createElement('span');
135
+ textSpan.classList.add(this.CSS_PREFIX + '__label-text');
136
+ textSpan.textContent = labelText;
137
+ label.appendChild(textSpan);
138
+ }
139
+ // Set title from explicit title option, or use label as fallback
140
+ const titleText = this.options.title
141
+ ? translate(view, this.options.title)
142
+ : labelText;
143
+ if (titleText) {
144
+ label.setAttribute('title', titleText);
145
+ label.setAttribute('aria-label', titleText);
146
+ }
147
+ // Set ARIA attributes for accessibility
148
+ label.setAttribute('aria-haspopup', 'true');
149
+ label.setAttribute('aria-expanded', 'false');
150
+ let wrap = document.createElement('div');
151
+ wrap.classList.add(this.CSS_PREFIX);
152
+ wrap.appendChild(label);
153
+ let open = null;
154
+ let listeningOnClose = null;
155
+ let close = () => {
156
+ if (open && open.close()) {
157
+ open = null;
158
+ win?.removeEventListener('mousedown', listeningOnClose);
159
+ }
160
+ };
161
+ label.addEventListener('mousedown', (e) => {
162
+ e.preventDefault();
163
+ markMenuEvent(e);
164
+ if (open) {
165
+ close();
166
+ wrap.classList.remove('kb-dropdown--open');
167
+ }
168
+ else {
169
+ open = this.expand(wrap, content.dom);
170
+ wrap.classList.add('kb-dropdown--open');
171
+ win?.addEventListener('mousedown', listeningOnClose = () => {
172
+ if (!isMenuEvent(wrap))
173
+ close();
174
+ });
175
+ }
176
+ });
177
+ function update(state) {
178
+ let inner = content.update(state);
179
+ wrap.style.display = inner ? '' : 'none';
180
+ return inner;
181
+ }
182
+ return { dom: wrap, update };
183
+ }
184
+ /// @internal
185
+ expand(dom, items) {
186
+ const menuDOM = document.createElement('div');
187
+ menuDOM.classList.add('kb-dropdown__menu');
188
+ menuDOM.setAttribute('role', 'menu');
189
+ if (this.options.class) {
190
+ menuDOM.classList.add(this.options.class);
191
+ }
192
+ items.forEach((item) => menuDOM.appendChild(item));
193
+ let done = false;
194
+ function close() {
195
+ if (done)
196
+ return false;
197
+ done = true;
198
+ dom.removeChild(menuDOM);
199
+ return true;
200
+ }
201
+ dom.appendChild(menuDOM);
202
+ return { close, node: menuDOM };
203
+ }
204
+ }
205
+ function renderDropdownItems(items, view) {
206
+ let rendered = [], updates = [];
207
+ for (let i = 0; i < items.length; i++) {
208
+ let { dom, update } = items[i].render(view);
209
+ const item = document.createElement('div');
210
+ item.classList.add('kb-dropdown__item');
211
+ item.appendChild(dom);
212
+ rendered.push(item);
213
+ updates.push(update);
214
+ }
215
+ return { dom: rendered, update: combineUpdates(updates, rendered) };
216
+ }
217
+ function combineUpdates(updates, nodes) {
218
+ return (state) => {
219
+ let something = false;
220
+ for (let i = 0; i < updates.length; i++) {
221
+ let up = updates[i](state);
222
+ nodes[i].style.display = up ? '' : 'none';
223
+ if (up)
224
+ something = true;
225
+ }
226
+ return something;
227
+ };
228
+ }
229
+ /// Represents a submenu wrapping a group of elements that start
230
+ /// hidden and expand to the right when hovered over or tapped.
231
+ export class DropdownSubmenu {
232
+ options;
233
+ /// @internal
234
+ content;
235
+ /// Creates a submenu for the given group of menu elements. The
236
+ /// following options are recognized:
237
+ constructor(content,
238
+ /// @internal
239
+ options = {}) {
240
+ this.options = options;
241
+ this.content = Array.isArray(content) ? content : [content];
242
+ }
243
+ /// Renders the submenu.
244
+ render(view) {
245
+ const CSS_PREFIX = 'kb-submenu';
246
+ const items = renderDropdownItems(this.content, view);
247
+ const win = view.dom.ownerDocument.defaultView;
248
+ const wrap = document.createElement('div');
249
+ wrap.classList.add(CSS_PREFIX);
250
+ const label = document.createElement('div');
251
+ label.classList.add(CSS_PREFIX + '__label');
252
+ label.appendChild(document.createTextNode(translate(view, this.options.label || '')));
253
+ wrap.appendChild(label);
254
+ const submenu = document.createElement('div');
255
+ submenu.classList.add(CSS_PREFIX + '__content');
256
+ items.dom.forEach((item) => submenu.appendChild(item));
257
+ wrap.appendChild(submenu);
258
+ let listeningOnClose = null;
259
+ label.addEventListener('mousedown', (e) => {
260
+ e.preventDefault();
261
+ markMenuEvent(e);
262
+ const isOpen = wrap.classList.contains(CSS_PREFIX + '--open');
263
+ setClass(wrap, CSS_PREFIX + '--open', !isOpen);
264
+ if (!isOpen && !listeningOnClose) {
265
+ win?.addEventListener('mousedown', listeningOnClose = () => {
266
+ if (!isMenuEvent(wrap)) {
267
+ wrap.classList.remove(CSS_PREFIX + '--open');
268
+ win?.removeEventListener('mousedown', listeningOnClose);
269
+ listeningOnClose = null;
270
+ }
271
+ });
272
+ }
273
+ });
274
+ function update(state) {
275
+ let inner = items.update(state);
276
+ wrap.style.display = inner ? '' : 'none';
277
+ return inner;
278
+ }
279
+ return { dom: wrap, update };
280
+ }
281
+ }
282
+ /// Render the given, possibly nested, array of menu elements into a
283
+ /// document fragment, placing separators between them (and ensuring no
284
+ /// superfluous separators appear when some of the groups turn out to
285
+ /// be empty).
286
+ export function renderGrouped(view, content) {
287
+ let result = document.createDocumentFragment();
288
+ let updates = [], separators = [];
289
+ for (let i = 0; i < content.length; i++) {
290
+ let items = content[i], localUpdates = [], localNodes = [];
291
+ for (let j = 0; j < items.length; j++) {
292
+ let { dom, update } = items[j].render(view);
293
+ let span = document.createElement('span');
294
+ span.classList.add('kb-menu__item');
295
+ span.appendChild(dom);
296
+ result.appendChild(span);
297
+ localNodes.push(span);
298
+ localUpdates.push(update);
299
+ }
300
+ if (localUpdates.length) {
301
+ updates.push(combineUpdates(localUpdates, localNodes));
302
+ if (i < content.length - 1) {
303
+ separators.push(result.appendChild(separator()));
304
+ }
305
+ }
306
+ }
307
+ function update(state) {
308
+ let something = false, needSep = false;
309
+ for (let i = 0; i < updates.length; i++) {
310
+ let hasContent = updates[i](state);
311
+ if (i) {
312
+ separators[i - 1].style.display = needSep && hasContent ? '' : 'none';
313
+ }
314
+ needSep = hasContent;
315
+ if (hasContent)
316
+ something = true;
317
+ }
318
+ return something;
319
+ }
320
+ return { dom: result, update };
321
+ }
322
+ function separator() {
323
+ const elem = document.createElement('div');
324
+ elem.classList.add('kb-menu__separator');
325
+ return elem;
326
+ }
327
+ // Work around classList.toggle being broken in IE11
328
+ function setClass(dom, cls, on) {
329
+ if (on)
330
+ dom.classList.add(cls);
331
+ else
332
+ dom.classList.remove(cls);
333
+ }
package/esm/mod.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { Dropdown, DropdownSubmenu, type MenuElement, MenuItem, type MenuItemSpec, } from './menu.js';
2
+ export * from './ExtensionCustomMenu.js';
3
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,eAAe,EACf,KAAK,WAAW,EAChB,QAAQ,EACR,KAAK,YAAY,GAClB,MAAM,WAAW,CAAC;AAEnB,cAAc,0BAA0B,CAAC"}
package/esm/mod.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Dropdown, DropdownSubmenu, MenuItem, } from './menu.js';
2
+ export * from './ExtensionCustomMenu.js';
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,36 @@
1
+ import { Attrs } from 'prosemirror-model';
2
+ export declare function openPrompt(options: {
3
+ title: string;
4
+ fields: {
5
+ [name: string]: Field;
6
+ };
7
+ callback: (attrs: Attrs) => void;
8
+ }): void;
9
+ export declare abstract class Field {
10
+ readonly options: {
11
+ value?: any;
12
+ label: string;
13
+ required?: boolean;
14
+ validate?: (value: any) => string | null;
15
+ clean?: (value: any) => any;
16
+ };
17
+ constructor(options: {
18
+ value?: any;
19
+ label: string;
20
+ required?: boolean;
21
+ validate?: (value: any) => string | null;
22
+ clean?: (value: any) => any;
23
+ });
24
+ abstract render(): HTMLElement;
25
+ read(dom: HTMLElement): any;
26
+ validateType(value: any): string | null;
27
+ validate(value: any): string | null;
28
+ clean(value: any): any;
29
+ }
30
+ export declare class TextField extends Field {
31
+ render(): any;
32
+ }
33
+ export declare class SelectField extends Field {
34
+ render(): any;
35
+ }
36
+ //# sourceMappingURL=prompt.d.ts.map