@use-kona/editor 0.1.28 → 0.1.29

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.
@@ -196,9 +196,9 @@ const Menu = (props)=>{
196
196
  break;
197
197
  }
198
198
  };
199
- document.addEventListener('keydown', handleKeyDown);
199
+ document.addEventListener('keydown', handleKeyDown, true);
200
200
  return ()=>{
201
- document.removeEventListener('keydown', handleKeyDown);
201
+ document.removeEventListener('keydown', handleKeyDown, true);
202
202
  };
203
203
  }, [
204
204
  actions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@use-kona/editor",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -0,0 +1,87 @@
1
+ // @vitest-environment jsdom
2
+ import '@testing-library/jest-dom/vitest';
3
+ import {
4
+ act,
5
+ fireEvent,
6
+ render,
7
+ screen,
8
+ waitFor,
9
+ } from '@testing-library/react';
10
+ import React from 'react';
11
+ import type { Editor } from 'slate';
12
+ import { describe, expect, it, vi } from 'vitest';
13
+ import { KonaEditor } from '../../editor';
14
+ import type { IPlugin } from '../../types';
15
+ import { CommandsPlugin } from './CommandsPlugin';
16
+
17
+ Object.assign(globalThis, { React });
18
+
19
+ const commands = [
20
+ {
21
+ name: 'one',
22
+ commandName: 'one',
23
+ title: 'One',
24
+ icon: null,
25
+ action: () => {},
26
+ },
27
+ {
28
+ name: 'two',
29
+ commandName: 'two',
30
+ title: 'Two',
31
+ icon: null,
32
+ action: () => {},
33
+ },
34
+ ];
35
+
36
+ describe('CommandsPlugin keyboard navigation', () => {
37
+ it('keeps the slash menu open when ArrowDown would select the void block below', async () => {
38
+ HTMLElement.prototype.scrollIntoView = vi.fn();
39
+ let editor: Editor;
40
+ const onKeyDown = vi.fn();
41
+ const plugin = new CommandsPlugin({
42
+ commands,
43
+ renderMenu: (children) => <div>{children}</div>,
44
+ });
45
+ const voidPlugin: IPlugin = {
46
+ init(value) {
47
+ editor = value;
48
+ return value;
49
+ },
50
+ blocks: [
51
+ {
52
+ type: 'void',
53
+ isVoid: true,
54
+ render: (props) => <div {...props.attributes}>{props.children}</div>,
55
+ },
56
+ ],
57
+ handlers: { onKeyDown },
58
+ };
59
+
60
+ render(
61
+ <KonaEditor
62
+ initialValue={[
63
+ { type: 'paragraph', children: [{ text: '' }] },
64
+ { type: 'void', children: [{ text: '' }] },
65
+ ]}
66
+ plugins={[plugin, voidPlugin]}
67
+ onChange={() => {}}
68
+ />,
69
+ );
70
+
71
+ act(() => editor.insertText('/'));
72
+ await screen.findByRole('button', { name: 'One' });
73
+ const before = screen
74
+ .getAllByRole('button')
75
+ .map((button) => button.className);
76
+
77
+ fireEvent.keyDown(screen.getByRole('textbox'), { key: 'ArrowDown' });
78
+
79
+ await waitFor(() =>
80
+ expect(screen.getByRole('button', { name: 'Two' })).toBeInTheDocument(),
81
+ );
82
+ expect(
83
+ screen.getAllByRole('button').map((button) => button.className),
84
+ ).toEqual([before[1], before[0]]);
85
+ expect(onKeyDown).not.toHaveBeenCalled();
86
+ });
87
+ });
@@ -260,10 +260,10 @@ export const Menu = (props: Props) => {
260
260
  }
261
261
  };
262
262
 
263
- document.addEventListener('keydown', handleKeyDown);
263
+ document.addEventListener('keydown', handleKeyDown, true);
264
264
 
265
265
  return () => {
266
- document.removeEventListener('keydown', handleKeyDown);
266
+ document.removeEventListener('keydown', handleKeyDown, true);
267
267
  };
268
268
  }, [
269
269
  actions,