@use-kona/editor 0.1.19 → 0.1.21

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.
@@ -8,6 +8,7 @@ import { deserialize } from "./deserialize.js";
8
8
  import styles_module from "./styles.module.js";
9
9
  const SUPPORTED_HANDLERS = [
10
10
  'onDrop',
11
+ 'onDragOver',
11
12
  'onKeyDown',
12
13
  'onPaste'
13
14
  ];
package/dist/editor.js CHANGED
@@ -18,9 +18,24 @@ const KonaEditor = /*#__PURE__*/ forwardRef((props, ref)=>{
18
18
  match
19
19
  });
20
20
  };
21
+ const insertNodes = (nodes, at)=>{
22
+ if (!editor || !Array.isArray(nodes) || !nodes.length) return false;
23
+ const target = at ?? editor.selection ?? [
24
+ editor.children.length
25
+ ];
26
+ try {
27
+ Transforms.insertNodes(editor, nodes, {
28
+ at: target
29
+ });
30
+ return true;
31
+ } catch {
32
+ return false;
33
+ }
34
+ };
21
35
  return {
22
36
  serialize: serialize(plugins),
23
37
  deserialize: deserialize(plugins),
38
+ insertNodes,
24
39
  deleteNode,
25
40
  isEmpty: ()=>isEmpty(editor.children),
26
41
  focus: (mode)=>{
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { KeyboardEvent, ReactElement, ReactNode } from 'react';
2
- import type { DecoratedRange, Descendant, Editor, NodeEntry, NodeMatch } from 'slate';
2
+ import type { DecoratedRange, Descendant, Editor, Location, NodeEntry, NodeMatch } from 'slate';
3
3
  import type { RenderElementProps, RenderLeafProps } from 'slate-react';
4
4
  import type { CustomElement, CustomText } from '../types';
5
5
  export interface IPlugin<TEditor extends Editor = Editor, TBlock extends CustomElement = CustomElement, TLeaf extends CustomText = CustomText> {
@@ -11,6 +11,7 @@ export interface IPlugin<TEditor extends Editor = Editor, TBlock extends CustomE
11
11
  renderLeaf?: (props: RenderLeafProps) => ReactElement | null;
12
12
  handlers?: {
13
13
  onDrop?: (event: DragEvent, editor: Editor) => void;
14
+ onDragOver?: (event: DragEvent, editor: Editor) => void;
14
15
  onKeyDown?: (event: KeyboardEvent, editor: Editor) => void;
15
16
  onPaste?: (event: ClipboardEvent, editor: Editor) => void;
16
17
  };
@@ -49,6 +50,7 @@ export type UiParams = {
49
50
  export type EditorRef = {
50
51
  serialize: (node: CustomElement | CustomText) => string;
51
52
  deserialize: (element: HTMLElement) => (Descendant | string)[] | string | Descendant | null;
53
+ insertNodes: (nodes: Descendant[], at?: Location) => boolean;
52
54
  isEmpty: () => boolean;
53
55
  deleteNode: (match: NodeMatch<CustomElement>) => void;
54
56
  focus: (mode?: 'end') => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@use-kona/editor",
3
- "version": "0.1.19",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -26,7 +26,7 @@ import type { IPlugin } from '../types';
26
26
  import { deserialize } from './deserialize';
27
27
  import styles from './styles.module.css';
28
28
 
29
- const SUPPORTED_HANDLERS = ['onDrop', 'onKeyDown', 'onPaste'];
29
+ const SUPPORTED_HANDLERS = ['onDrop', 'onDragOver', 'onKeyDown', 'onPaste'];
30
30
 
31
31
  type Props = {
32
32
  readOnly?: boolean;
package/src/editor.tsx CHANGED
@@ -29,9 +29,25 @@ export const KonaEditor = forwardRef<EditorRef, KonaEditorProps>(
29
29
  Transforms.removeNodes(editor, { at: [], match });
30
30
  };
31
31
 
32
+ const insertNodes: EditorRef['insertNodes'] = (nodes, at) => {
33
+ if (!editor || !Array.isArray(nodes) || !nodes.length) {
34
+ return false;
35
+ }
36
+
37
+ const target = at ?? editor.selection ?? [editor.children.length];
38
+
39
+ try {
40
+ Transforms.insertNodes(editor, nodes, { at: target });
41
+ return true;
42
+ } catch {
43
+ return false;
44
+ }
45
+ };
46
+
32
47
  return {
33
48
  serialize: serialize(plugins),
34
49
  deserialize: deserialize(plugins),
50
+ insertNodes,
35
51
  deleteNode,
36
52
  isEmpty: () => {
37
53
  return isEmpty(editor.children);
package/src/types.ts CHANGED
@@ -3,6 +3,7 @@ import type {
3
3
  DecoratedRange,
4
4
  Descendant,
5
5
  Editor,
6
+ Location,
6
7
  NodeEntry,
7
8
  NodeMatch,
8
9
  } from 'slate';
@@ -24,6 +25,7 @@ export interface IPlugin<
24
25
 
25
26
  handlers?: {
26
27
  onDrop?: (event: DragEvent, editor: Editor) => void;
28
+ onDragOver?: (event: DragEvent, editor: Editor) => void;
27
29
  onKeyDown?: (event: KeyboardEvent, editor: Editor) => void;
28
30
  onPaste?: (event: ClipboardEvent, editor: Editor) => void;
29
31
  };
@@ -81,6 +83,7 @@ export type EditorRef = {
81
83
  deserialize: (
82
84
  element: HTMLElement,
83
85
  ) => (Descendant | string)[] | string | Descendant | null;
86
+ insertNodes: (nodes: Descendant[], at?: Location) => boolean;
84
87
  isEmpty: () => boolean;
85
88
  deleteNode: (match: NodeMatch<CustomElement>) => void;
86
89
  focus: (mode?: 'end') => void;