@use-kona/editor 0.1.15 → 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.
Files changed (31) hide show
  1. package/LICENSE +21 -0
  2. package/dist/plugins/CommandsPlugin/CommandsPlugin.d.ts +2 -3
  3. package/dist/plugins/CommandsPlugin/CommandsPlugin.js +5 -15
  4. package/dist/plugins/CommandsPlugin/Menu.d.ts +3 -3
  5. package/dist/plugins/CommandsPlugin/Menu.js +240 -69
  6. package/dist/plugins/CommandsPlugin/index.d.ts +1 -1
  7. package/dist/plugins/CommandsPlugin/resolveCommands.d.ts +34 -0
  8. package/dist/plugins/CommandsPlugin/resolveCommands.js +150 -0
  9. package/dist/plugins/CommandsPlugin/resolveCommands.spec.d.ts +1 -0
  10. package/dist/plugins/CommandsPlugin/resolveCommands.spec.js +277 -0
  11. package/dist/plugins/CommandsPlugin/styles.module.js +5 -0
  12. package/dist/plugins/CommandsPlugin/styles_module.css +47 -7
  13. package/dist/plugins/CommandsPlugin/types.d.ts +14 -2
  14. package/dist/plugins/CommandsPlugin/useResolvedCommands.d.ts +12 -0
  15. package/dist/plugins/CommandsPlugin/useResolvedCommands.js +46 -0
  16. package/dist/plugins/DnDPlugin/DnDPlugin.d.ts +8 -0
  17. package/dist/plugins/DnDPlugin/DnDPlugin.js +35 -7
  18. package/dist/plugins/NodeIdPlugin/NodeIdPlugin.js +6 -1
  19. package/dist/plugins/index.d.ts +1 -1
  20. package/package.json +11 -11
  21. package/src/plugins/CommandsPlugin/CommandsPlugin.tsx +5 -25
  22. package/src/plugins/CommandsPlugin/Menu.tsx +260 -86
  23. package/src/plugins/CommandsPlugin/index.ts +1 -1
  24. package/src/plugins/CommandsPlugin/resolveCommands.spec.ts +261 -0
  25. package/src/plugins/CommandsPlugin/resolveCommands.ts +275 -0
  26. package/src/plugins/CommandsPlugin/styles.module.css +49 -7
  27. package/src/plugins/CommandsPlugin/types.ts +17 -3
  28. package/src/plugins/CommandsPlugin/useResolvedCommands.ts +67 -0
  29. package/src/plugins/DnDPlugin/DnDPlugin.tsx +66 -9
  30. package/src/plugins/NodeIdPlugin/NodeIdPlugin.ts +10 -2
  31. package/src/plugins/index.ts +6 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kona
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,11 +1,10 @@
1
- import { type MapStore, type ReadableAtom } from 'nanostores';
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(params: UiParams): import("react/jsx-runtime").JSX.Element | null;
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 { computed, map } from "nanostores";
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
- commands: []
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(params) {
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
- commands: commands,
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 React, { type ReactNode } from 'react';
2
+ import { type ReactNode } from 'react';
3
3
  import type { Command, CommandsStore } from './types';
4
4
  type Props = {
5
5
  $store: MapStore<CommandsStore>;
6
- commands: Command[];
6
+ rootCommands: Command[];
7
7
  ignoreNodes?: string[];
8
8
  renderMenu: (children: ReactNode) => ReactNode;
9
9
  };
10
- export declare const Menu: (props: Props) => React.ReactPortal | null;
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 { commands, $store, renderMenu, ignoreNodes = [] } = props;
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
- commands,
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
- setTimeout(()=>{
85
+ const frame = window.requestAnimationFrame(()=>{
37
86
  const domSelection = window.getSelection();
38
- const domRange = domSelection?.getRangeAt(0);
87
+ const domRange = domSelection && domSelection.rangeCount > 0 ? domSelection.getRangeAt(0) : null;
39
88
  const rect = domRange?.getBoundingClientRect();
40
- store.isOpen ? setStyle({
41
- opacity: 1,
42
- transform: 'scale(1)',
43
- top: `${(rect?.top || 0) + window.scrollY + (rect?.height || 0) + 2}px`,
44
- left: `${(rect?.left || 0) + window.scrollX + (rect?.width || 0) / 2}px`
45
- }) : setStyle({
46
- opacity: 0,
47
- transform: 'scale(0.9)'
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
- }, 0);
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 newActive = active >= commands.length - 1 ? 0 : active + 1;
56
- refs.current[newActive]?.scrollIntoView({
119
+ const nextActive = active >= entries.length - 1 ? 0 : active + 1;
120
+ refs.current[nextActive]?.scrollIntoView({
57
121
  block: 'nearest'
58
122
  });
59
- return newActive;
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 newActive = active <= 0 ? commands.length - 1 : active - 1;
66
- refs.current[newActive]?.scrollIntoView({
131
+ const nextActive = active <= 0 ? entries.length - 1 : active - 1;
132
+ refs.current[nextActive]?.scrollIntoView({
67
133
  block: 'nearest'
68
134
  });
69
- return newActive;
135
+ return nextActive;
70
136
  });
71
137
  break;
72
- case 'Enter':
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
- commands[active]?.action(actions, editor);
75
- $store.setKey('isOpen', false);
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
- if (store.isOpen && commands.length > 0) document.addEventListener('keydown', handleKeyDown);
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
- store.isOpen
92
- ]);
93
- useEffect(()=>{
94
- if (active < 0) return void setActive(0);
95
- if (active > commands.length - 1) setActive(commands.length - 1);
96
- }, [
97
- store.filter
191
+ active,
192
+ entries,
193
+ editor,
194
+ isBrowseMode,
195
+ path.length,
196
+ store.isOpen,
197
+ $store
98
198
  ]);
99
- useLayoutEffect(()=>{
100
- const element = ref.current;
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__*/ jsx("div", {
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: commands.map((command, index)=>/*#__PURE__*/ jsxs("button", {
130
- type: "button",
131
- ref: (element)=>{
132
- if (element) refs.current[index] = element;
133
- },
134
- className: clsx(styles_module.button, {
135
- [styles_module.active]: index === active
136
- }),
137
- onMouseDown: (event)=>{
138
- event.preventDefault();
139
- command.action(actions, editor);
140
- $store.setKey('isOpen', false);
141
- },
142
- children: [
143
- /*#__PURE__*/ jsx("span", {
144
- className: styles_module.icon,
145
- children: command.icon
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
- /*#__PURE__*/ jsx("span", {
148
- children: command.title
149
- })
150
- ]
151
- }, index))
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 {};