@use-kona/editor 0.1.16 → 0.1.17
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/plugins/CommandsPlugin/CommandsPlugin.d.ts +2 -3
- package/dist/plugins/CommandsPlugin/CommandsPlugin.js +5 -15
- package/dist/plugins/CommandsPlugin/Menu.d.ts +3 -3
- package/dist/plugins/CommandsPlugin/Menu.js +240 -69
- package/dist/plugins/CommandsPlugin/index.d.ts +1 -1
- package/dist/plugins/CommandsPlugin/resolveCommands.d.ts +34 -0
- package/dist/plugins/CommandsPlugin/resolveCommands.js +150 -0
- package/dist/plugins/CommandsPlugin/resolveCommands.spec.d.ts +1 -0
- package/dist/plugins/CommandsPlugin/resolveCommands.spec.js +277 -0
- package/dist/plugins/CommandsPlugin/styles.module.js +5 -0
- package/dist/plugins/CommandsPlugin/styles_module.css +47 -7
- package/dist/plugins/CommandsPlugin/types.d.ts +14 -2
- package/dist/plugins/CommandsPlugin/useResolvedCommands.d.ts +12 -0
- package/dist/plugins/CommandsPlugin/useResolvedCommands.js +46 -0
- package/dist/plugins/DnDPlugin/DnDPlugin.d.ts +7 -0
- package/dist/plugins/DnDPlugin/DnDPlugin.js +34 -7
- package/dist/plugins/NodeIdPlugin/NodeIdPlugin.js +6 -1
- package/dist/plugins/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/plugins/CommandsPlugin/CommandsPlugin.tsx +5 -25
- package/src/plugins/CommandsPlugin/Menu.tsx +260 -86
- package/src/plugins/CommandsPlugin/index.ts +1 -1
- package/src/plugins/CommandsPlugin/resolveCommands.spec.ts +261 -0
- package/src/plugins/CommandsPlugin/resolveCommands.ts +275 -0
- package/src/plugins/CommandsPlugin/styles.module.css +49 -7
- package/src/plugins/CommandsPlugin/types.ts +17 -3
- package/src/plugins/CommandsPlugin/useResolvedCommands.ts +67 -0
- package/src/plugins/DnDPlugin/DnDPlugin.tsx +64 -9
- package/src/plugins/NodeIdPlugin/NodeIdPlugin.ts +10 -2
- package/src/plugins/index.ts +6 -1
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { type MapStore
|
|
1
|
+
import { type MapStore } from 'nanostores';
|
|
2
2
|
import type { IPlugin, UiParams } from '../../types';
|
|
3
3
|
import type { CommandsStore, Options } from './types';
|
|
4
4
|
export declare class CommandsPlugin implements IPlugin {
|
|
5
5
|
options: Options;
|
|
6
6
|
$store: MapStore<CommandsStore>;
|
|
7
|
-
$commands: ReadableAtom<CommandsStore['commands']>;
|
|
8
7
|
constructor(options: Options);
|
|
9
8
|
init(editor: any): any;
|
|
10
|
-
ui(
|
|
9
|
+
ui(_params: UiParams): import("react/jsx-runtime").JSX.Element | null;
|
|
11
10
|
}
|
|
@@ -1,34 +1,25 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useStore } from "@nanostores/react";
|
|
3
|
-
import {
|
|
3
|
+
import { map } from "nanostores";
|
|
4
4
|
import { Editor, Node } from "slate";
|
|
5
5
|
import { Menu } from "./Menu.js";
|
|
6
6
|
class CommandsPlugin {
|
|
7
7
|
options;
|
|
8
8
|
$store;
|
|
9
|
-
$commands;
|
|
10
9
|
constructor(options){
|
|
11
10
|
this.options = options;
|
|
12
11
|
this.$store = map({
|
|
13
12
|
isOpen: false,
|
|
14
13
|
filter: false,
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
this.$commands = computed(this.$store, (store)=>{
|
|
18
|
-
if (false === store.filter) return [];
|
|
19
|
-
return store.commands.filter((command)=>{
|
|
20
|
-
const isCommandMatches = command.commandName.toLocaleLowerCase().includes(store.filter.toLocaleLowerCase());
|
|
21
|
-
const isTitleMatches = command.title.toLocaleLowerCase().includes(store.filter.toLocaleLowerCase());
|
|
22
|
-
return isCommandMatches || isTitleMatches;
|
|
23
|
-
});
|
|
14
|
+
openId: 0
|
|
24
15
|
});
|
|
25
16
|
}
|
|
26
17
|
init(editor) {
|
|
27
18
|
const { onChange, insertText } = editor;
|
|
28
19
|
editor.insertText = (text)=>{
|
|
29
20
|
if ('/' === text) {
|
|
21
|
+
this.$store.setKey('openId', this.$store.get().openId + 1);
|
|
30
22
|
this.$store.setKey('isOpen', true);
|
|
31
|
-
this.$store.setKey('commands', this.options.commands);
|
|
32
23
|
this.$store.setKey('filter', '');
|
|
33
24
|
}
|
|
34
25
|
insertText(text);
|
|
@@ -43,14 +34,13 @@ class CommandsPlugin {
|
|
|
43
34
|
};
|
|
44
35
|
return editor;
|
|
45
36
|
}
|
|
46
|
-
ui(
|
|
47
|
-
const commands = useStore(this.$commands);
|
|
37
|
+
ui(_params) {
|
|
48
38
|
const { isOpen } = useStore(this.$store);
|
|
49
39
|
if (!isOpen) return null;
|
|
50
40
|
return /*#__PURE__*/ jsx(Menu, {
|
|
51
41
|
renderMenu: this.options.renderMenu,
|
|
52
42
|
$store: this.$store,
|
|
53
|
-
|
|
43
|
+
rootCommands: this.options.commands,
|
|
54
44
|
ignoreNodes: this.options.ignoreNodes
|
|
55
45
|
});
|
|
56
46
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { MapStore } from 'nanostores';
|
|
2
|
-
import
|
|
2
|
+
import { type ReactNode } from 'react';
|
|
3
3
|
import type { Command, CommandsStore } from './types';
|
|
4
4
|
type Props = {
|
|
5
5
|
$store: MapStore<CommandsStore>;
|
|
6
|
-
|
|
6
|
+
rootCommands: Command[];
|
|
7
7
|
ignoreNodes?: string[];
|
|
8
8
|
renderMenu: (children: ReactNode) => ReactNode;
|
|
9
9
|
};
|
|
10
|
-
export declare const Menu: (props: Props) =>
|
|
10
|
+
export declare const Menu: (props: Props) => import("react").ReactPortal | null;
|
|
11
11
|
export {};
|
|
@@ -7,11 +7,13 @@ import { Editor } from "slate";
|
|
|
7
7
|
import { useFocused, useSlate, useSlateSelection } from "slate-react";
|
|
8
8
|
import { insert, insertText, removeCommand, set, wrap } from "./actions.js";
|
|
9
9
|
import styles_module from "./styles.module.js";
|
|
10
|
+
import { useResolvedCommands } from "./useResolvedCommands.js";
|
|
10
11
|
const Menu = (props)=>{
|
|
11
|
-
const {
|
|
12
|
+
const { rootCommands, $store, renderMenu, ignoreNodes = [] } = props;
|
|
12
13
|
const store = useStore($store);
|
|
13
14
|
const [style, setStyle] = useState({});
|
|
14
15
|
const [active, setActive] = useState(0);
|
|
16
|
+
const [path, setPath] = useState([]);
|
|
15
17
|
const refs = useRef({});
|
|
16
18
|
const selection = useSlateSelection();
|
|
17
19
|
const editor = useSlate();
|
|
@@ -20,6 +22,32 @@ const Menu = (props)=>{
|
|
|
20
22
|
const entry = Editor.above(editor, {
|
|
21
23
|
match: (n)=>Editor.isBlock(editor, n)
|
|
22
24
|
});
|
|
25
|
+
const isBrowseMode = 'string' == typeof store.filter && '' === store.filter;
|
|
26
|
+
const isSearchMode = 'string' == typeof store.filter && '' !== store.filter;
|
|
27
|
+
const { commands, isLoading, isError } = useResolvedCommands({
|
|
28
|
+
rootCommands,
|
|
29
|
+
filter: store.filter,
|
|
30
|
+
path,
|
|
31
|
+
editor,
|
|
32
|
+
isOpen: store.isOpen
|
|
33
|
+
});
|
|
34
|
+
const entries = useMemo(()=>{
|
|
35
|
+
const commandEntries = commands.map((command)=>({
|
|
36
|
+
type: 'command',
|
|
37
|
+
command
|
|
38
|
+
}));
|
|
39
|
+
if (isBrowseMode && path.length > 0) return [
|
|
40
|
+
{
|
|
41
|
+
type: 'back'
|
|
42
|
+
},
|
|
43
|
+
...commandEntries
|
|
44
|
+
];
|
|
45
|
+
return commandEntries;
|
|
46
|
+
}, [
|
|
47
|
+
commands,
|
|
48
|
+
isBrowseMode,
|
|
49
|
+
path.length
|
|
50
|
+
]);
|
|
23
51
|
const actions = useMemo(()=>({
|
|
24
52
|
removeCommand: removeCommand(editor, selection, store.filter),
|
|
25
53
|
set: set(editor, selection, store.filter),
|
|
@@ -27,90 +55,151 @@ const Menu = (props)=>{
|
|
|
27
55
|
wrap: wrap(editor, selection, store.filter),
|
|
28
56
|
insertText: insertText(editor)
|
|
29
57
|
}), [
|
|
30
|
-
|
|
58
|
+
selection,
|
|
31
59
|
store.filter
|
|
32
60
|
]);
|
|
61
|
+
useEffect(()=>{
|
|
62
|
+
if (!store.isOpen) return;
|
|
63
|
+
setPath([]);
|
|
64
|
+
setActive(0);
|
|
65
|
+
}, [
|
|
66
|
+
store.isOpen,
|
|
67
|
+
store.openId
|
|
68
|
+
]);
|
|
69
|
+
useEffect(()=>{
|
|
70
|
+
if (false === store.filter || isSearchMode) setActive(0);
|
|
71
|
+
}, [
|
|
72
|
+
isSearchMode,
|
|
73
|
+
store.filter
|
|
74
|
+
]);
|
|
75
|
+
useEffect(()=>{
|
|
76
|
+
if (!entries.length) return void setActive(0);
|
|
77
|
+
if (active > entries.length - 1) setActive(entries.length - 1);
|
|
78
|
+
}, [
|
|
79
|
+
active,
|
|
80
|
+
entries
|
|
81
|
+
]);
|
|
33
82
|
useLayoutEffect(()=>{
|
|
34
83
|
const { selection } = editor;
|
|
35
84
|
if (!selection || !isFocused) return void setStyle(void 0);
|
|
36
|
-
|
|
85
|
+
const frame = window.requestAnimationFrame(()=>{
|
|
37
86
|
const domSelection = window.getSelection();
|
|
38
|
-
const domRange = domSelection
|
|
87
|
+
const domRange = domSelection && domSelection.rangeCount > 0 ? domSelection.getRangeAt(0) : null;
|
|
39
88
|
const rect = domRange?.getBoundingClientRect();
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
opacity: 0,
|
|
47
|
-
transform
|
|
89
|
+
const x = (rect?.left || 0) + (rect?.width || 0) / 2;
|
|
90
|
+
const menuHeight = ref.current?.offsetHeight || 0;
|
|
91
|
+
let y = (rect?.top || 0) + (rect?.height || 0) + 2;
|
|
92
|
+
if (menuHeight > 0 && y + menuHeight >= window.innerHeight) y = Math.max(8, (rect?.top || 0) - menuHeight - 2);
|
|
93
|
+
const transform = `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0) ${store.isOpen ? 'scale(1)' : 'scale(0.95)'}`;
|
|
94
|
+
setStyle({
|
|
95
|
+
opacity: store.isOpen ? 1 : 0,
|
|
96
|
+
transform
|
|
48
97
|
});
|
|
49
|
-
}
|
|
98
|
+
});
|
|
99
|
+
return ()=>{
|
|
100
|
+
window.cancelAnimationFrame(frame);
|
|
101
|
+
};
|
|
102
|
+
}, [
|
|
103
|
+
selection,
|
|
104
|
+
commands.length,
|
|
105
|
+
isLoading,
|
|
106
|
+
isError,
|
|
107
|
+
store.isOpen,
|
|
108
|
+
isFocused
|
|
109
|
+
]);
|
|
110
|
+
useEffect(()=>{
|
|
50
111
|
const handleKeyDown = (event)=>{
|
|
112
|
+
if (!store.isOpen) return;
|
|
51
113
|
switch(event.key){
|
|
52
114
|
case 'ArrowDown':
|
|
115
|
+
if (!entries.length) return;
|
|
53
116
|
event.preventDefault();
|
|
117
|
+
event.stopPropagation();
|
|
54
118
|
setActive((active)=>{
|
|
55
|
-
const
|
|
56
|
-
refs.current[
|
|
119
|
+
const nextActive = active >= entries.length - 1 ? 0 : active + 1;
|
|
120
|
+
refs.current[nextActive]?.scrollIntoView({
|
|
57
121
|
block: 'nearest'
|
|
58
122
|
});
|
|
59
|
-
return
|
|
123
|
+
return nextActive;
|
|
60
124
|
});
|
|
61
125
|
break;
|
|
62
126
|
case 'ArrowUp':
|
|
127
|
+
if (!entries.length) return;
|
|
63
128
|
event.preventDefault();
|
|
129
|
+
event.stopPropagation();
|
|
64
130
|
setActive((active)=>{
|
|
65
|
-
const
|
|
66
|
-
refs.current[
|
|
131
|
+
const nextActive = active <= 0 ? entries.length - 1 : active - 1;
|
|
132
|
+
refs.current[nextActive]?.scrollIntoView({
|
|
67
133
|
block: 'nearest'
|
|
68
134
|
});
|
|
69
|
-
return
|
|
135
|
+
return nextActive;
|
|
70
136
|
});
|
|
71
137
|
break;
|
|
72
|
-
case '
|
|
138
|
+
case 'ArrowRight':
|
|
139
|
+
{
|
|
140
|
+
const entry = entries[active];
|
|
141
|
+
if (!entry || 'command' !== entry.type || !entry.command.isSubmenu) return;
|
|
142
|
+
event.preventDefault();
|
|
143
|
+
event.stopPropagation();
|
|
144
|
+
setPath(entry.command.path);
|
|
145
|
+
if (!isBrowseMode) $store.setKey('filter', '');
|
|
146
|
+
setActive(0);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case 'ArrowLeft':
|
|
150
|
+
if (!isBrowseMode || !path.length) return;
|
|
73
151
|
event.preventDefault();
|
|
74
|
-
|
|
75
|
-
|
|
152
|
+
event.stopPropagation();
|
|
153
|
+
setPath((path)=>path.slice(0, path.length - 1));
|
|
154
|
+
setActive(0);
|
|
76
155
|
break;
|
|
156
|
+
case 'Enter':
|
|
157
|
+
{
|
|
158
|
+
const entry = entries[active];
|
|
159
|
+
if (!entry) return;
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
event.stopPropagation();
|
|
162
|
+
if ('back' === entry.type && isBrowseMode) {
|
|
163
|
+
setPath((path)=>path.slice(0, path.length - 1));
|
|
164
|
+
setActive(0);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
if ('command' !== entry.type) break;
|
|
168
|
+
if (entry.command.isSubmenu) {
|
|
169
|
+
setPath(entry.command.path);
|
|
170
|
+
if (!isBrowseMode) $store.setKey('filter', '');
|
|
171
|
+
setActive(0);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
entry.command.command.action?.(actions, editor);
|
|
175
|
+
$store.setKey('isOpen', false);
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
77
178
|
case 'Escape':
|
|
179
|
+
event.preventDefault();
|
|
78
180
|
event.stopPropagation();
|
|
79
181
|
$store.setKey('isOpen', false);
|
|
80
182
|
break;
|
|
81
183
|
}
|
|
82
184
|
};
|
|
83
|
-
|
|
185
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
84
186
|
return ()=>{
|
|
85
187
|
document.removeEventListener('keydown', handleKeyDown);
|
|
86
188
|
};
|
|
87
189
|
}, [
|
|
88
|
-
selection,
|
|
89
|
-
active,
|
|
90
190
|
actions,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
store
|
|
191
|
+
active,
|
|
192
|
+
entries,
|
|
193
|
+
editor,
|
|
194
|
+
isBrowseMode,
|
|
195
|
+
path.length,
|
|
196
|
+
store.isOpen,
|
|
197
|
+
$store
|
|
98
198
|
]);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (element) {
|
|
102
|
-
const { height, top } = element.getBoundingClientRect();
|
|
103
|
-
const domSelection = window.getSelection();
|
|
104
|
-
const domRange = domSelection?.getRangeAt(0);
|
|
105
|
-
const rect = domRange?.getBoundingClientRect();
|
|
106
|
-
if (top + height >= window.innerHeight) setStyle((style)=>({
|
|
107
|
-
...style,
|
|
108
|
-
top: `${top - height - (rect?.height ?? 22)}px`
|
|
109
|
-
}));
|
|
110
|
-
}
|
|
111
|
-
}, []);
|
|
112
|
-
if (!commands.length) return null;
|
|
199
|
+
const hasRows = entries.length > 0 || isLoading || isError;
|
|
200
|
+
if (false === store.filter || !hasRows) return null;
|
|
113
201
|
if (entry && ignoreNodes.includes(entry[0].type)) return null;
|
|
202
|
+
const pathLabel = path.map((item)=>item.title).join(' / ');
|
|
114
203
|
return /*#__PURE__*/ createPortal(renderMenu(/*#__PURE__*/ jsxs(Fragment, {
|
|
115
204
|
children: [
|
|
116
205
|
store.isOpen && /*#__PURE__*/ jsx("div", {
|
|
@@ -119,36 +208,118 @@ const Menu = (props)=>{
|
|
|
119
208
|
$store.setKey('isOpen', false);
|
|
120
209
|
}
|
|
121
210
|
}),
|
|
122
|
-
/*#__PURE__*/
|
|
211
|
+
/*#__PURE__*/ jsxs("div", {
|
|
123
212
|
ref: ref,
|
|
124
213
|
style: style,
|
|
125
214
|
className: styles_module.menu,
|
|
126
215
|
onMouseDown: (event)=>{
|
|
127
216
|
event.preventDefault();
|
|
128
217
|
},
|
|
129
|
-
children:
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
218
|
+
children: [
|
|
219
|
+
isBrowseMode && pathLabel && /*#__PURE__*/ jsx("div", {
|
|
220
|
+
className: styles_module.path,
|
|
221
|
+
children: pathLabel
|
|
222
|
+
}),
|
|
223
|
+
entries.map((entry, index)=>{
|
|
224
|
+
if ('back' === entry.type) return /*#__PURE__*/ jsxs("button", {
|
|
225
|
+
type: "button",
|
|
226
|
+
ref: (element)=>{
|
|
227
|
+
refs.current[index] = element;
|
|
228
|
+
},
|
|
229
|
+
className: clsx(styles_module.button, {
|
|
230
|
+
[styles_module.active]: index === active
|
|
231
|
+
}),
|
|
232
|
+
onMouseDown: (event)=>{
|
|
233
|
+
event.preventDefault();
|
|
234
|
+
setPath((path)=>path.slice(0, path.length - 1));
|
|
235
|
+
setActive(0);
|
|
236
|
+
},
|
|
237
|
+
children: [
|
|
238
|
+
/*#__PURE__*/ jsx("span", {
|
|
239
|
+
className: styles_module.icon,
|
|
240
|
+
children: "..."
|
|
241
|
+
}),
|
|
242
|
+
/*#__PURE__*/ jsx("span", {
|
|
243
|
+
className: styles_module.content,
|
|
244
|
+
children: /*#__PURE__*/ jsx("span", {
|
|
245
|
+
children: "..."
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
]
|
|
249
|
+
}, "back");
|
|
250
|
+
return /*#__PURE__*/ jsxs("button", {
|
|
251
|
+
type: "button",
|
|
252
|
+
ref: (element)=>{
|
|
253
|
+
refs.current[index] = element;
|
|
254
|
+
},
|
|
255
|
+
className: clsx(styles_module.button, {
|
|
256
|
+
[styles_module.active]: index === active
|
|
146
257
|
}),
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
258
|
+
onMouseDown: (event)=>{
|
|
259
|
+
event.preventDefault();
|
|
260
|
+
if (entry.command.isSubmenu) {
|
|
261
|
+
setPath(entry.command.path);
|
|
262
|
+
if (!isBrowseMode) $store.setKey('filter', '');
|
|
263
|
+
setActive(0);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
entry.command.command.action?.(actions, editor);
|
|
267
|
+
$store.setKey('isOpen', false);
|
|
268
|
+
},
|
|
269
|
+
children: [
|
|
270
|
+
/*#__PURE__*/ jsx("span", {
|
|
271
|
+
className: styles_module.icon,
|
|
272
|
+
children: entry.command.command.icon
|
|
273
|
+
}),
|
|
274
|
+
/*#__PURE__*/ jsxs("span", {
|
|
275
|
+
className: styles_module.content,
|
|
276
|
+
children: [
|
|
277
|
+
/*#__PURE__*/ jsx("span", {
|
|
278
|
+
children: entry.command.command.title
|
|
279
|
+
}),
|
|
280
|
+
isSearchMode && entry.command.breadcrumb && /*#__PURE__*/ jsx("span", {
|
|
281
|
+
className: styles_module.breadcrumb,
|
|
282
|
+
children: entry.command.breadcrumb
|
|
283
|
+
})
|
|
284
|
+
]
|
|
285
|
+
}),
|
|
286
|
+
entry.command.isSubmenu && isBrowseMode && /*#__PURE__*/ jsx("span", {
|
|
287
|
+
className: styles_module.submenu,
|
|
288
|
+
"aria-hidden": "true",
|
|
289
|
+
children: /*#__PURE__*/ jsxs("svg", {
|
|
290
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
291
|
+
width: "12",
|
|
292
|
+
height: "12",
|
|
293
|
+
viewBox: "0 0 24 24",
|
|
294
|
+
fill: "none",
|
|
295
|
+
stroke: "currentColor",
|
|
296
|
+
strokeWidth: "2",
|
|
297
|
+
strokeLinecap: "round",
|
|
298
|
+
strokeLinejoin: "round",
|
|
299
|
+
children: [
|
|
300
|
+
/*#__PURE__*/ jsx("path", {
|
|
301
|
+
stroke: "none",
|
|
302
|
+
d: "M0 0h24v24H0z",
|
|
303
|
+
fill: "none"
|
|
304
|
+
}),
|
|
305
|
+
/*#__PURE__*/ jsx("path", {
|
|
306
|
+
d: "M9 6l6 6l-6 6"
|
|
307
|
+
})
|
|
308
|
+
]
|
|
309
|
+
})
|
|
310
|
+
})
|
|
311
|
+
]
|
|
312
|
+
}, entry.command.key);
|
|
313
|
+
}),
|
|
314
|
+
isLoading && /*#__PURE__*/ jsx("div", {
|
|
315
|
+
className: styles_module.systemRow,
|
|
316
|
+
children: "Loading..."
|
|
317
|
+
}),
|
|
318
|
+
isError && /*#__PURE__*/ jsx("div", {
|
|
319
|
+
className: styles_module.systemRow,
|
|
320
|
+
children: "Could not load commands"
|
|
321
|
+
})
|
|
322
|
+
]
|
|
152
323
|
})
|
|
153
324
|
]
|
|
154
325
|
})), document.body);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { CommandsPlugin } from './CommandsPlugin';
|
|
2
|
-
export type { Command } from './types';
|
|
2
|
+
export type { Command, CommandPathEntry, GetCommandsContext } from './types';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Editor } from 'slate';
|
|
2
|
+
import type { Command, CommandPathEntry } from './types';
|
|
3
|
+
export type ResolvedCommand = {
|
|
4
|
+
command: Command;
|
|
5
|
+
key: string;
|
|
6
|
+
path: CommandPathEntry[];
|
|
7
|
+
breadcrumb: string;
|
|
8
|
+
isSubmenu: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type ResolveCommandsParams = {
|
|
11
|
+
rootCommands: Command[];
|
|
12
|
+
filter: string;
|
|
13
|
+
path: CommandPathEntry[];
|
|
14
|
+
editor: Editor;
|
|
15
|
+
};
|
|
16
|
+
export type ResolvedCommandsState = {
|
|
17
|
+
commands: ResolvedCommand[];
|
|
18
|
+
isLoading: boolean;
|
|
19
|
+
isError: boolean;
|
|
20
|
+
};
|
|
21
|
+
type ResolveRequest = {
|
|
22
|
+
state: ResolvedCommandsState;
|
|
23
|
+
promise: Promise<ResolvedCommandsState>;
|
|
24
|
+
};
|
|
25
|
+
export declare class CommandsResolver {
|
|
26
|
+
private cache;
|
|
27
|
+
private requestId;
|
|
28
|
+
private state;
|
|
29
|
+
getState(): ResolvedCommandsState;
|
|
30
|
+
resolve(params: ResolveCommandsParams): ResolveRequest;
|
|
31
|
+
private resolveChildCommands;
|
|
32
|
+
private resolveInternal;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const toPathEntry = (command)=>({
|
|
2
|
+
name: command.name,
|
|
3
|
+
title: command.title,
|
|
4
|
+
commandName: command.commandName
|
|
5
|
+
});
|
|
6
|
+
const pathToKey = (path)=>path.map((item)=>item.name).join('/');
|
|
7
|
+
const normalizeCommands = (commands)=>Array.isArray(commands) ? commands : [];
|
|
8
|
+
const isCommandMatchesQuery = (command, query)=>{
|
|
9
|
+
const normalized = query.toLocaleLowerCase();
|
|
10
|
+
return command.commandName.toLocaleLowerCase().includes(normalized) || command.title.toLocaleLowerCase().includes(normalized);
|
|
11
|
+
};
|
|
12
|
+
const toResolvedCommand = (command, parentPath)=>{
|
|
13
|
+
const path = [
|
|
14
|
+
...parentPath,
|
|
15
|
+
toPathEntry(command)
|
|
16
|
+
];
|
|
17
|
+
return {
|
|
18
|
+
command,
|
|
19
|
+
path,
|
|
20
|
+
key: pathToKey(path),
|
|
21
|
+
breadcrumb: parentPath.map((item)=>item.title).join(' / '),
|
|
22
|
+
isSubmenu: Boolean(command.getCommands)
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const resolveBrowseCommands = async (params, resolveChildCommands)=>{
|
|
26
|
+
const { rootCommands, path, editor } = params;
|
|
27
|
+
let currentCommands = rootCommands;
|
|
28
|
+
let currentPath = [];
|
|
29
|
+
for (const item of path){
|
|
30
|
+
const command = currentCommands.find((entry)=>entry.name === item.name);
|
|
31
|
+
if (!command?.getCommands) return [];
|
|
32
|
+
currentPath = [
|
|
33
|
+
...currentPath,
|
|
34
|
+
toPathEntry(command)
|
|
35
|
+
];
|
|
36
|
+
currentCommands = await resolveChildCommands({
|
|
37
|
+
command,
|
|
38
|
+
path: currentPath,
|
|
39
|
+
query: '',
|
|
40
|
+
editor
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return currentCommands.map((command)=>toResolvedCommand(command, currentPath));
|
|
44
|
+
};
|
|
45
|
+
const resolveSearchCommands = async (params, resolveChildCommands)=>{
|
|
46
|
+
const { rootCommands, filter, editor } = params;
|
|
47
|
+
const commands = [];
|
|
48
|
+
const walk = async (levelCommands, parentPath)=>{
|
|
49
|
+
for (const command of levelCommands){
|
|
50
|
+
if (command.getCommands) {
|
|
51
|
+
if (isCommandMatchesQuery(command, filter)) commands.push(toResolvedCommand(command, parentPath));
|
|
52
|
+
const currentPath = [
|
|
53
|
+
...parentPath,
|
|
54
|
+
toPathEntry(command)
|
|
55
|
+
];
|
|
56
|
+
const children = await resolveChildCommands({
|
|
57
|
+
command,
|
|
58
|
+
path: currentPath,
|
|
59
|
+
query: filter,
|
|
60
|
+
editor
|
|
61
|
+
});
|
|
62
|
+
await walk(children, currentPath);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (command.action && isCommandMatchesQuery(command, filter)) commands.push(toResolvedCommand(command, parentPath));
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
await walk(rootCommands, []);
|
|
69
|
+
return commands;
|
|
70
|
+
};
|
|
71
|
+
class CommandsResolver {
|
|
72
|
+
cache = new Map();
|
|
73
|
+
requestId = 0;
|
|
74
|
+
state = {
|
|
75
|
+
commands: [],
|
|
76
|
+
isLoading: false,
|
|
77
|
+
isError: false
|
|
78
|
+
};
|
|
79
|
+
getState() {
|
|
80
|
+
return this.state;
|
|
81
|
+
}
|
|
82
|
+
resolve(params) {
|
|
83
|
+
const currentRequestId = ++this.requestId;
|
|
84
|
+
this.state = {
|
|
85
|
+
commands: [],
|
|
86
|
+
isLoading: true,
|
|
87
|
+
isError: false
|
|
88
|
+
};
|
|
89
|
+
const promise = this.resolveInternal(params).then((commands)=>{
|
|
90
|
+
if (currentRequestId !== this.requestId) return this.state;
|
|
91
|
+
this.state = {
|
|
92
|
+
commands,
|
|
93
|
+
isLoading: false,
|
|
94
|
+
isError: false
|
|
95
|
+
};
|
|
96
|
+
return this.state;
|
|
97
|
+
}).catch(()=>{
|
|
98
|
+
if (currentRequestId !== this.requestId) return this.state;
|
|
99
|
+
this.state = {
|
|
100
|
+
...this.state,
|
|
101
|
+
isLoading: false,
|
|
102
|
+
isError: true
|
|
103
|
+
};
|
|
104
|
+
return this.state;
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
state: this.state,
|
|
108
|
+
promise
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
resolveChildCommands = async (params)=>{
|
|
112
|
+
const { command, path, query, editor } = params;
|
|
113
|
+
if (!command.getCommands) return [];
|
|
114
|
+
const cacheKey = `${query}::${pathToKey(path)}`;
|
|
115
|
+
const cached = this.cache.get(cacheKey);
|
|
116
|
+
if (cached?.status === 'resolved') return cached.value;
|
|
117
|
+
if (cached?.status === 'rejected') throw cached.error;
|
|
118
|
+
if (cached?.status === 'pending') return cached.promise;
|
|
119
|
+
const promise = Promise.resolve(command.getCommands({
|
|
120
|
+
query,
|
|
121
|
+
editor,
|
|
122
|
+
path,
|
|
123
|
+
parent: command
|
|
124
|
+
})).then((commands)=>normalizeCommands(commands));
|
|
125
|
+
this.cache.set(cacheKey, {
|
|
126
|
+
status: 'pending',
|
|
127
|
+
promise
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
const commands = await promise;
|
|
131
|
+
this.cache.set(cacheKey, {
|
|
132
|
+
status: 'resolved',
|
|
133
|
+
value: commands
|
|
134
|
+
});
|
|
135
|
+
return commands;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
this.cache.set(cacheKey, {
|
|
138
|
+
status: 'rejected',
|
|
139
|
+
error
|
|
140
|
+
});
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
async resolveInternal(params) {
|
|
145
|
+
const { filter } = params;
|
|
146
|
+
if (!filter) return resolveBrowseCommands(params, this.resolveChildCommands);
|
|
147
|
+
return resolveSearchCommands(params, this.resolveChildCommands);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
export { CommandsResolver };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|