@use-kona/editor 0.1.5 → 0.1.6
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.d.ts +1 -1
- package/dist/core/createEditable.js +12 -1
- package/dist/core/createEditor.js +8 -8
- package/dist/plugins/CodeBlockPlugin/CodeBlockPlugin.d.ts +8 -2
- package/dist/plugins/CodeBlockPlugin/CodeBlockPlugin.js +12 -0
- package/dist/plugins/ShortcutsPlugin/ShortcutsPlugin.js +4 -4
- package/package.json +1 -1
- package/src/core/createEditable.tsx +21 -1
- package/src/core/createEditor.ts +8 -8
- package/src/core/deserialize.ts +5 -3
- package/src/plugins/CodeBlockPlugin/CodeBlockPlugin.tsx +16 -0
- package/src/plugins/LinksPlugin/LinksPlugin.tsx +0 -1
- package/src/plugins/ShortcutsPlugin/ShortcutsPlugin.tsx +4 -7
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import is_hotkey from "is-hotkey";
|
|
3
3
|
import react, { isValidElement, useCallback } from "react";
|
|
4
|
+
import { Transforms } from "slate";
|
|
4
5
|
import { Editable, useReadOnly } from "slate-react";
|
|
5
6
|
import { BaseElement } from "../elements/BaseElement.js";
|
|
7
|
+
import { deserialize } from "./deserialize.js";
|
|
6
8
|
import styles_module from "./styles.module.js";
|
|
7
9
|
const SUPPORTED_HANDLERS = [
|
|
8
10
|
'onDrop',
|
|
@@ -44,6 +46,14 @@ const createEditable = (editor, plugins)=>({ readOnly })=>{
|
|
|
44
46
|
}
|
|
45
47
|
}
|
|
46
48
|
};
|
|
49
|
+
const handlePaste = (event)=>{
|
|
50
|
+
const html = event.clipboardData?.getData('text/html');
|
|
51
|
+
if (!html) return;
|
|
52
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
53
|
+
const value = deserialize(plugins)(parsed.body);
|
|
54
|
+
event.preventDefault();
|
|
55
|
+
Transforms.insertNodes(editor, value);
|
|
56
|
+
};
|
|
47
57
|
const decorate = (entry)=>{
|
|
48
58
|
const decorators = plugins.filter((plugin)=>!!plugin.decorate);
|
|
49
59
|
return decorators.reduce((decorations, plugin)=>{
|
|
@@ -66,7 +76,8 @@ const createEditable = (editor, plugins)=>({ readOnly })=>{
|
|
|
66
76
|
});
|
|
67
77
|
return handlers;
|
|
68
78
|
}, {
|
|
69
|
-
onKeyDown: handleHotkey
|
|
79
|
+
onKeyDown: handleHotkey,
|
|
80
|
+
onPaste: handlePaste
|
|
70
81
|
});
|
|
71
82
|
const Ui = useCallback(({ children })=>{
|
|
72
83
|
const ui = plugins.filter((p)=>Boolean(p.ui));
|
|
@@ -74,7 +74,7 @@ const createEditor_createEditor = (plugins)=>()=>{
|
|
|
74
74
|
const result = matchedBlock.onBeforeDelete ? await matchedBlock?.onBeforeDelete?.([
|
|
75
75
|
node
|
|
76
76
|
]) : true;
|
|
77
|
-
if (result) {
|
|
77
|
+
if (result && Editor.isVoid(editorWithPlugins, node)) {
|
|
78
78
|
Transforms.removeNodes(editorWithPlugins, {
|
|
79
79
|
at: path
|
|
80
80
|
});
|
|
@@ -92,19 +92,19 @@ const createEditor_createEditor = (plugins)=>()=>{
|
|
|
92
92
|
const { selection } = editorWithPlugins;
|
|
93
93
|
if (!selection) return;
|
|
94
94
|
if (Range.isCollapsed(selection)) {
|
|
95
|
-
const
|
|
95
|
+
const currentEntry = Editor.above(editorWithPlugins, {
|
|
96
96
|
at: selection.anchor,
|
|
97
97
|
match: (n)=>Editor.isBlock(editorWithPlugins, n),
|
|
98
98
|
mode: 'lowest'
|
|
99
99
|
});
|
|
100
|
-
const
|
|
100
|
+
const previousEntry = Editor.above(editorWithPlugins, {
|
|
101
101
|
at: Editor.before(editorWithPlugins, selection.anchor),
|
|
102
102
|
match: (n)=>Editor.isBlock(editorWithPlugins, n),
|
|
103
103
|
mode: 'lowest'
|
|
104
104
|
});
|
|
105
|
-
if (!
|
|
106
|
-
const [_, currentPath] =
|
|
107
|
-
const [node, path] =
|
|
105
|
+
if (!currentEntry || !previousEntry) return;
|
|
106
|
+
const [_, currentPath] = currentEntry;
|
|
107
|
+
const [node, path] = previousEntry;
|
|
108
108
|
if (node && Editor.isStart(editorWithPlugins, selection.anchor, currentPath)) {
|
|
109
109
|
const matchedBlock = plugins.reduce((block, plugin)=>{
|
|
110
110
|
const match = plugin.blocks?.find((b)=>b.type === node.type);
|
|
@@ -114,15 +114,15 @@ const createEditor_createEditor = (plugins)=>()=>{
|
|
|
114
114
|
const result = matchedBlock.onBeforeDelete ? await matchedBlock.onBeforeDelete([
|
|
115
115
|
node
|
|
116
116
|
]) : true;
|
|
117
|
-
if (result) {
|
|
117
|
+
if (result && Editor.isVoid(editorWithPlugins, node)) {
|
|
118
118
|
Transforms.removeNodes(editorWithPlugins, {
|
|
119
119
|
at: path
|
|
120
120
|
});
|
|
121
121
|
matchedBlock.onDelete?.([
|
|
122
122
|
node
|
|
123
123
|
]);
|
|
124
|
+
return;
|
|
124
125
|
}
|
|
125
|
-
return;
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -4,6 +4,7 @@ import { type RenderElementProps, type RenderLeafProps } from 'slate-react';
|
|
|
4
4
|
import type { IPlugin } from '../../types';
|
|
5
5
|
import type { CodeElement } from './types';
|
|
6
6
|
import 'prismjs/themes/prism.css';
|
|
7
|
+
import type { CustomElement } from '../../../types';
|
|
7
8
|
import 'prismjs/components/prism-javascript';
|
|
8
9
|
import 'prismjs/components/prism-typescript';
|
|
9
10
|
import 'prismjs/components/prism-json';
|
|
@@ -43,10 +44,15 @@ export declare class CodeBlockPlugin implements IPlugin {
|
|
|
43
44
|
} & {
|
|
44
45
|
token: boolean;
|
|
45
46
|
})[];
|
|
46
|
-
blocks: {
|
|
47
|
+
blocks: ({
|
|
47
48
|
type: string;
|
|
48
49
|
render: (props: RenderElementProps, editor: Editor) => import("react/jsx-runtime").JSX.Element;
|
|
49
|
-
|
|
50
|
+
deserialize: (el: HTMLElement, children: any) => CustomElement | undefined;
|
|
51
|
+
} | {
|
|
52
|
+
type: string;
|
|
53
|
+
render: (props: RenderElementProps) => import("react/jsx-runtime").JSX.Element;
|
|
54
|
+
deserialize?: undefined;
|
|
55
|
+
})[];
|
|
50
56
|
leafs: {
|
|
51
57
|
render: (props: RenderLeafProps) => import("react/jsx-runtime").JSX.Element;
|
|
52
58
|
}[];
|
|
@@ -26,6 +26,7 @@ import "prismjs/components/prism-bash";
|
|
|
26
26
|
import "prismjs/components/prism-sql";
|
|
27
27
|
import "prismjs/components/prism-yaml";
|
|
28
28
|
import "prismjs/components/prism-markdown";
|
|
29
|
+
import { jsx as external_slate_hyperscript_jsx } from "slate-hyperscript";
|
|
29
30
|
class CodeBlockPlugin {
|
|
30
31
|
options;
|
|
31
32
|
constructor(options){
|
|
@@ -155,6 +156,17 @@ class CodeBlockPlugin {
|
|
|
155
156
|
Content
|
|
156
157
|
})
|
|
157
158
|
});
|
|
159
|
+
},
|
|
160
|
+
deserialize: (el, children)=>{
|
|
161
|
+
const { nodeName } = el;
|
|
162
|
+
if ('PRE' === nodeName) {
|
|
163
|
+
const lines = children.join('').split('\n');
|
|
164
|
+
return external_slate_hyperscript_jsx('element', {
|
|
165
|
+
type: CodeBlockPlugin.CODE_ELEMENT
|
|
166
|
+
}, lines.map((line)=>external_slate_hyperscript_jsx('element', {
|
|
167
|
+
type: CodeBlockPlugin.CODE_LINE_ELEMENT
|
|
168
|
+
}, line)));
|
|
169
|
+
}
|
|
158
170
|
}
|
|
159
171
|
},
|
|
160
172
|
{
|
|
@@ -25,12 +25,12 @@ class ShortcutsPlugin {
|
|
|
25
25
|
const currentPoint = Range.start(selection);
|
|
26
26
|
const [currentNode] = Editor.node(editor, currentPoint.path);
|
|
27
27
|
const textBeforeCursor = Node.string(currentNode).slice(0, currentPoint.offset);
|
|
28
|
-
matchBefore = new RegExp(shortcut.before.source
|
|
28
|
+
matchBefore = new RegExp(`${shortcut.before.source}`, 'g').exec(textBeforeCursor);
|
|
29
29
|
}
|
|
30
30
|
if (shortcut.after && matchBefore) {
|
|
31
31
|
const currentPoint = Range.start(selection);
|
|
32
32
|
const [currentNode] = Editor.node(editor, currentPoint.path);
|
|
33
|
-
const textBetweenMatches = Node.string(currentNode).slice(matchBefore.index, currentPoint.offset);
|
|
33
|
+
const textBetweenMatches = Node.string(currentNode).slice(matchBefore.index + matchBefore[0].length, currentPoint.offset);
|
|
34
34
|
matchAfter = new RegExp(shortcut.after.source + '$', 'g').exec(textBetweenMatches);
|
|
35
35
|
}
|
|
36
36
|
if (matchBefore && !shortcut.after) return void shortcut.change(editor, {
|
|
@@ -42,8 +42,8 @@ class ShortcutsPlugin {
|
|
|
42
42
|
if (matchBefore && matchAfter && shortcut.before && shortcut.after) return void shortcut.change(editor, {
|
|
43
43
|
before: matchBefore,
|
|
44
44
|
after: matchAfter,
|
|
45
|
-
text:
|
|
46
|
-
cleanText: matchBefore.input.slice(matchBefore.index + matchBefore[0].length, matchBefore.
|
|
45
|
+
text: matchBefore.input.slice(matchBefore.index),
|
|
46
|
+
cleanText: matchBefore.input.slice(matchBefore.index + matchBefore[0].length, matchBefore.input.length - matchAfter[0].length)
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
49
|
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import isHotkey from 'is-hotkey';
|
|
2
2
|
import React, {
|
|
3
|
+
type ClipboardEvent,
|
|
3
4
|
isValidElement,
|
|
4
5
|
type JSX,
|
|
5
6
|
type KeyboardEvent,
|
|
@@ -7,7 +8,13 @@ import React, {
|
|
|
7
8
|
type ReactNode,
|
|
8
9
|
useCallback,
|
|
9
10
|
} from 'react';
|
|
10
|
-
import
|
|
11
|
+
import {
|
|
12
|
+
DecoratedRange,
|
|
13
|
+
Descendant,
|
|
14
|
+
Editor,
|
|
15
|
+
NodeEntry,
|
|
16
|
+
Transforms,
|
|
17
|
+
} from 'slate';
|
|
11
18
|
import {
|
|
12
19
|
Editable,
|
|
13
20
|
type RenderElementProps,
|
|
@@ -16,6 +23,7 @@ import {
|
|
|
16
23
|
} from 'slate-react';
|
|
17
24
|
import { BaseElement } from '../elements/BaseElement';
|
|
18
25
|
import type { IPlugin } from '../types';
|
|
26
|
+
import { deserialize } from './deserialize';
|
|
19
27
|
import styles from './styles.module.css';
|
|
20
28
|
|
|
21
29
|
const SUPPORTED_HANDLERS = ['onDrop', 'onKeyDown', 'onPaste'];
|
|
@@ -84,6 +92,17 @@ export const createEditable =
|
|
|
84
92
|
}
|
|
85
93
|
};
|
|
86
94
|
|
|
95
|
+
const handlePaste = (event: ClipboardEvent<HTMLDivElement>) => {
|
|
96
|
+
const html = event.clipboardData?.getData('text/html');
|
|
97
|
+
if (!html) return;
|
|
98
|
+
|
|
99
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
100
|
+
const value = deserialize(plugins)(parsed.body);
|
|
101
|
+
event.preventDefault();
|
|
102
|
+
|
|
103
|
+
Transforms.insertNodes(editor, value as Descendant);
|
|
104
|
+
};
|
|
105
|
+
|
|
87
106
|
const decorate = (entry: NodeEntry) => {
|
|
88
107
|
const decorators: IPlugin[] = plugins.filter(
|
|
89
108
|
(plugin) => !!plugin.decorate,
|
|
@@ -117,6 +136,7 @@ export const createEditable =
|
|
|
117
136
|
},
|
|
118
137
|
{
|
|
119
138
|
onKeyDown: handleHotkey,
|
|
139
|
+
onPaste: handlePaste,
|
|
120
140
|
},
|
|
121
141
|
);
|
|
122
142
|
|
package/src/core/createEditor.ts
CHANGED
|
@@ -119,7 +119,7 @@ export const createEditor = (plugins: IPlugin[]) => () => {
|
|
|
119
119
|
const result = matchedBlock.onBeforeDelete
|
|
120
120
|
? await matchedBlock?.onBeforeDelete?.([node])
|
|
121
121
|
: true;
|
|
122
|
-
if (result) {
|
|
122
|
+
if (result && Editor.isVoid(editorWithPlugins, node)) {
|
|
123
123
|
Transforms.removeNodes(editorWithPlugins, { at: path });
|
|
124
124
|
matchedBlock.onDelete?.([node]);
|
|
125
125
|
}
|
|
@@ -139,23 +139,23 @@ export const createEditor = (plugins: IPlugin[]) => () => {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
if (Range.isCollapsed(selection)) {
|
|
142
|
-
const
|
|
142
|
+
const currentEntry = Editor.above<CustomElement>(editorWithPlugins, {
|
|
143
143
|
at: selection.anchor,
|
|
144
144
|
match: (n) => Editor.isBlock(editorWithPlugins, n as CustomElement),
|
|
145
145
|
mode: 'lowest',
|
|
146
146
|
});
|
|
147
|
-
const
|
|
147
|
+
const previousEntry = Editor.above<CustomElement>(editorWithPlugins, {
|
|
148
148
|
at: Editor.before(editorWithPlugins, selection.anchor),
|
|
149
149
|
match: (n) => Editor.isBlock(editorWithPlugins, n as CustomElement),
|
|
150
150
|
mode: 'lowest',
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
-
if (!
|
|
153
|
+
if (!currentEntry || !previousEntry) {
|
|
154
154
|
return;
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
const [_, currentPath] =
|
|
158
|
-
const [node, path] =
|
|
157
|
+
const [_, currentPath] = currentEntry;
|
|
158
|
+
const [node, path] = previousEntry;
|
|
159
159
|
|
|
160
160
|
if (
|
|
161
161
|
node &&
|
|
@@ -170,11 +170,11 @@ export const createEditor = (plugins: IPlugin[]) => () => {
|
|
|
170
170
|
const result = matchedBlock.onBeforeDelete
|
|
171
171
|
? await matchedBlock.onBeforeDelete([node])
|
|
172
172
|
: true;
|
|
173
|
-
if (result) {
|
|
173
|
+
if (result && Editor.isVoid(editorWithPlugins, node)) {
|
|
174
174
|
Transforms.removeNodes(editorWithPlugins, { at: path });
|
|
175
175
|
matchedBlock.onDelete?.([node]);
|
|
176
|
+
return;
|
|
176
177
|
}
|
|
177
|
-
return;
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
}
|
package/src/core/deserialize.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { type Descendant, Element
|
|
1
|
+
import { type Descendant, Element } from 'slate';
|
|
2
2
|
import { jsx } from 'slate-hyperscript';
|
|
3
|
-
import { CustomElement, CustomText } from '../../types';
|
|
4
3
|
import type { IPlugin } from '../types';
|
|
5
4
|
|
|
6
5
|
export const deserialize =
|
|
@@ -36,7 +35,8 @@ export const deserialize =
|
|
|
36
35
|
return jsx('element', { type: 'paragraph' }, children);
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
let result:
|
|
38
|
+
let result: (Descendant | string)[] | string | Descendant | null = null;
|
|
39
|
+
|
|
40
40
|
for (const plugin of plugins) {
|
|
41
41
|
if (plugin.blocks?.some((b) => b.deserialize)) {
|
|
42
42
|
plugin.blocks.forEach((e) => {
|
|
@@ -61,7 +61,9 @@ export const deserialize =
|
|
|
61
61
|
| string
|
|
62
62
|
| Descendant
|
|
63
63
|
)[];
|
|
64
|
+
|
|
64
65
|
const newResult = e.deserialize(element, childrenAsDescendants);
|
|
66
|
+
|
|
65
67
|
if (newResult) {
|
|
66
68
|
result = newResult;
|
|
67
69
|
}
|
|
@@ -42,6 +42,7 @@ import 'prismjs/components/prism-bash';
|
|
|
42
42
|
import 'prismjs/components/prism-sql';
|
|
43
43
|
import 'prismjs/components/prism-yaml';
|
|
44
44
|
import 'prismjs/components/prism-markdown';
|
|
45
|
+
import { jsx } from 'slate-hyperscript';
|
|
45
46
|
|
|
46
47
|
type Options = {
|
|
47
48
|
renderCodeBlock: (
|
|
@@ -211,6 +212,21 @@ export class CodeBlockPlugin implements IPlugin {
|
|
|
211
212
|
</>
|
|
212
213
|
);
|
|
213
214
|
},
|
|
215
|
+
deserialize: (el: HTMLElement, children) => {
|
|
216
|
+
const { nodeName } = el;
|
|
217
|
+
|
|
218
|
+
if (nodeName === 'PRE') {
|
|
219
|
+
const lines = children.join('').split('\n');
|
|
220
|
+
|
|
221
|
+
return jsx(
|
|
222
|
+
'element',
|
|
223
|
+
{ type: CodeBlockPlugin.CODE_ELEMENT },
|
|
224
|
+
lines.map((line) =>
|
|
225
|
+
jsx('element', { type: CodeBlockPlugin.CODE_LINE_ELEMENT }, line),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
},
|
|
214
230
|
},
|
|
215
231
|
{
|
|
216
232
|
type: CodeBlockPlugin.CODE_LINE_ELEMENT,
|
|
@@ -2,7 +2,6 @@ import isUrl from 'is-url';
|
|
|
2
2
|
import { Editor, Element, Range, Transforms } from 'slate';
|
|
3
3
|
import { jsx } from 'slate-hyperscript';
|
|
4
4
|
import type { RenderElementProps } from 'slate-react';
|
|
5
|
-
import { deserialize } from '../../core/deserialize';
|
|
6
5
|
import type { IPlugin } from '../../types';
|
|
7
6
|
import { LINK_ELEMENT } from './constants';
|
|
8
7
|
import { Link } from './Link';
|
|
@@ -67,7 +67,7 @@ export class ShortcutsPlugin implements IPlugin {
|
|
|
67
67
|
);
|
|
68
68
|
|
|
69
69
|
// Check if the text before cursor matches the pattern
|
|
70
|
-
matchBefore = new RegExp(shortcut.before.source
|
|
70
|
+
matchBefore = new RegExp(`${shortcut.before.source}`, 'g').exec(
|
|
71
71
|
textBeforeCursor,
|
|
72
72
|
);
|
|
73
73
|
}
|
|
@@ -77,7 +77,7 @@ export class ShortcutsPlugin implements IPlugin {
|
|
|
77
77
|
const currentPoint = Range.start(selection);
|
|
78
78
|
const [currentNode] = Editor.node(editor, currentPoint.path);
|
|
79
79
|
const textBetweenMatches = Node.string(currentNode).slice(
|
|
80
|
-
matchBefore.index,
|
|
80
|
+
matchBefore.index + matchBefore[0].length,
|
|
81
81
|
currentPoint.offset,
|
|
82
82
|
);
|
|
83
83
|
|
|
@@ -110,13 +110,10 @@ export class ShortcutsPlugin implements IPlugin {
|
|
|
110
110
|
shortcut.change(editor, {
|
|
111
111
|
before: matchBefore,
|
|
112
112
|
after: matchAfter,
|
|
113
|
-
text:
|
|
113
|
+
text: matchBefore.input.slice(matchBefore.index),
|
|
114
114
|
cleanText: matchBefore.input.slice(
|
|
115
115
|
matchBefore.index + matchBefore[0].length,
|
|
116
|
-
matchBefore.
|
|
117
|
-
matchBefore[0].length +
|
|
118
|
-
matchAfter.index -
|
|
119
|
-
matchAfter[0].length,
|
|
116
|
+
matchBefore.input.length - matchAfter[0].length,
|
|
120
117
|
),
|
|
121
118
|
});
|
|
122
119
|
return;
|