@use-kona/editor 0.1.20 → 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.
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> {
@@ -50,6 +50,7 @@ export type UiParams = {
50
50
  export type EditorRef = {
51
51
  serialize: (node: CustomElement | CustomText) => string;
52
52
  deserialize: (element: HTMLElement) => (Descendant | string)[] | string | Descendant | null;
53
+ insertNodes: (nodes: Descendant[], at?: Location) => boolean;
53
54
  isEmpty: () => boolean;
54
55
  deleteNode: (match: NodeMatch<CustomElement>) => void;
55
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.20",
3
+ "version": "0.1.21",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
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';
@@ -82,6 +83,7 @@ export type EditorRef = {
82
83
  deserialize: (
83
84
  element: HTMLElement,
84
85
  ) => (Descendant | string)[] | string | Descendant | null;
86
+ insertNodes: (nodes: Descendant[], at?: Location) => boolean;
85
87
  isEmpty: () => boolean;
86
88
  deleteNode: (match: NodeMatch<CustomElement>) => void;
87
89
  focus: (mode?: 'end') => void;