@use-kona/editor 0.1.24 → 0.1.26
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/core/createEditable.js +146 -15
- package/dist/core/createEditor.js +1 -1
- package/dist/core/styles.module.js +3 -1
- package/dist/core/styles_module.css +13 -0
- package/dist/elements/Block.js +25 -0
- package/dist/index.js +2 -1
- package/dist/plugins/CodeBlockPlugin/CodeBlockPlugin.js +1 -1
- package/dist/plugins/DnDPlugin/DnDPlugin.js +93 -45
- package/dist/plugins/DnDPlugin/types.js +2 -0
- package/dist/plugins/HeadingsPlugin/HeadingsPlugin.js +1 -1
- package/dist/plugins/ListsPlugin/ListsPlugin.js +5 -3
- package/dist/plugins/NodeIdPlugin/NodeIdPlugin.js +6 -4
- package/dist/provider.js +15 -0
- package/dist/src/core/createEditable.d.ts +1 -1
- package/dist/src/elements/Block.d.ts +2 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/plugins/DnDPlugin/DnDPlugin.d.ts +0 -7
- package/dist/src/plugins/DnDPlugin/types.d.ts +29 -15
- package/dist/src/plugins/ListsPlugin/ListsPlugin.d.ts +1 -0
- package/dist/src/plugins/NodeIdPlugin/NodeIdPlugin.d.ts +1 -0
- package/dist/src/provider.d.ts +8 -0
- package/dist/src/types.d.ts +4 -0
- package/package.json +9 -8
- package/src/core/createEditable.tsx +170 -18
- package/src/core/createEditor.ts +1 -1
- package/src/core/styles.module.css +12 -0
- package/src/editor.tsx +4 -3
- package/src/elements/Block.tsx +31 -0
- package/src/index.ts +1 -0
- package/src/plugins/CodeBlockPlugin/CodeBlockPlugin.tsx +1 -3
- package/src/plugins/DnDPlugin/DnDPlugin.tsx +147 -62
- package/src/plugins/DnDPlugin/types.ts +35 -20
- package/src/plugins/HeadingsPlugin/HeadingsPlugin.tsx +1 -2
- package/src/plugins/ListsPlugin/ListsPlugin.spec.tsx +110 -14
- package/src/plugins/ListsPlugin/ListsPlugin.tsx +17 -6
- package/src/plugins/NodeIdPlugin/NodeIdPlugin.ts +9 -7
- package/src/provider.tsx +28 -0
- package/src/types.ts +5 -0
|
@@ -26,7 +26,11 @@ type Options = {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export class NodeIdPlugin implements IPlugin<Editor, NodeIdBlock> {
|
|
29
|
-
|
|
29
|
+
generateId: () => string;
|
|
30
|
+
|
|
31
|
+
constructor(private options: Options) {
|
|
32
|
+
this.generateId = options.generateId;
|
|
33
|
+
}
|
|
30
34
|
|
|
31
35
|
init(editor: Editor) {
|
|
32
36
|
const { apply, normalizeNode } = editor;
|
|
@@ -39,14 +43,13 @@ export class NodeIdPlugin implements IPlugin<Editor, NodeIdBlock> {
|
|
|
39
43
|
const newType = (operation.newProperties as CustomElement).type;
|
|
40
44
|
|
|
41
45
|
if (!node || (!Text.isText(node) && oldType !== newType)) {
|
|
42
|
-
(operation.newProperties as NodeIdBlock).nodeId =
|
|
43
|
-
this.options.generateId();
|
|
46
|
+
(operation.newProperties as NodeIdBlock).nodeId = this.generateId();
|
|
44
47
|
}
|
|
45
48
|
return apply(operation);
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
if (operation.type === 'insert_node') {
|
|
49
|
-
assignId(operation.node, this.
|
|
52
|
+
assignId(operation.node, this.generateId);
|
|
50
53
|
return apply(operation);
|
|
51
54
|
}
|
|
52
55
|
|
|
@@ -54,8 +57,7 @@ export class NodeIdPlugin implements IPlugin<Editor, NodeIdBlock> {
|
|
|
54
57
|
const node = Node.get(editor, operation.path);
|
|
55
58
|
|
|
56
59
|
if (!node || !Text.isText(node)) {
|
|
57
|
-
(operation.properties as NodeIdBlock).nodeId =
|
|
58
|
-
this.options.generateId();
|
|
60
|
+
(operation.properties as NodeIdBlock).nodeId = this.generateId();
|
|
59
61
|
}
|
|
60
62
|
return apply(operation);
|
|
61
63
|
}
|
|
@@ -66,7 +68,7 @@ export class NodeIdPlugin implements IPlugin<Editor, NodeIdBlock> {
|
|
|
66
68
|
editor.normalizeNode = (entry: NodeEntry) => {
|
|
67
69
|
const [node] = entry;
|
|
68
70
|
|
|
69
|
-
assignId(node, this.
|
|
71
|
+
assignId(node, this.generateId);
|
|
70
72
|
|
|
71
73
|
normalizeNode(entry);
|
|
72
74
|
};
|
package/src/provider.tsx
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { StoreValue } from 'nanostores';
|
|
2
|
+
import type React from 'react';
|
|
3
|
+
import { createContext, useContext } from 'react';
|
|
4
|
+
import type { EditorContext } from './types';
|
|
5
|
+
|
|
6
|
+
const Context = createContext<EditorContext | null>(null);
|
|
7
|
+
|
|
8
|
+
export const EditorProvider = ({
|
|
9
|
+
children,
|
|
10
|
+
selectedNodes,
|
|
11
|
+
}: {
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
selectedNodes: StoreValue<string[]>;
|
|
14
|
+
}) => {
|
|
15
|
+
return (
|
|
16
|
+
<Context.Provider value={{ selectedNodes }}>{children}</Context.Provider>
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const useEditorContext = () => {
|
|
21
|
+
const context = useContext(Context);
|
|
22
|
+
|
|
23
|
+
if (!context) {
|
|
24
|
+
throw new Error('useEditorContext must be used within an EditorProvider');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return context;
|
|
28
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { StoreValue } from 'nanostores';
|
|
1
2
|
import type { KeyboardEvent, ReactElement, ReactNode } from 'react';
|
|
2
3
|
import type {
|
|
3
4
|
DecoratedRange,
|
|
@@ -98,3 +99,7 @@ export type Deserialize = (
|
|
|
98
99
|
element: HTMLElement,
|
|
99
100
|
children?: (string | Descendant)[],
|
|
100
101
|
) => CustomElement | CustomText[] | undefined;
|
|
102
|
+
|
|
103
|
+
export type EditorContext = {
|
|
104
|
+
selectedNodes: StoreValue<string[]>;
|
|
105
|
+
};
|