@use-kona/editor 0.1.4 → 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/CommandsPlugin/Menu.js +5 -3
- package/dist/plugins/EmojiPlugin/Menu.js +6 -4
- 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/CommandsPlugin/Menu.tsx +5 -3
- package/src/plugins/EmojiPlugin/Menu.tsx +7 -4
- 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
|
{
|
|
@@ -16,6 +16,7 @@ const Menu = (props)=>{
|
|
|
16
16
|
const selection = useSlateSelection();
|
|
17
17
|
const editor = useSlate();
|
|
18
18
|
const isFocused = useFocused();
|
|
19
|
+
const ref = useRef(null);
|
|
19
20
|
const entry = Editor.above(editor, {
|
|
20
21
|
match: (n)=>Editor.isBlock(editor, n)
|
|
21
22
|
});
|
|
@@ -95,7 +96,8 @@ const Menu = (props)=>{
|
|
|
95
96
|
}, [
|
|
96
97
|
store.filter
|
|
97
98
|
]);
|
|
98
|
-
|
|
99
|
+
useLayoutEffect(()=>{
|
|
100
|
+
const element = ref.current;
|
|
99
101
|
if (element) {
|
|
100
102
|
const { height, top } = element.getBoundingClientRect();
|
|
101
103
|
const domSelection = window.getSelection();
|
|
@@ -106,7 +108,7 @@ const Menu = (props)=>{
|
|
|
106
108
|
top: `${top - height - (rect?.height ?? 22)}px`
|
|
107
109
|
}));
|
|
108
110
|
}
|
|
109
|
-
};
|
|
111
|
+
}, []);
|
|
110
112
|
if (!commands.length) return null;
|
|
111
113
|
if (entry && ignoreNodes.includes(entry[0].type)) return null;
|
|
112
114
|
return /*#__PURE__*/ createPortal(renderMenu(/*#__PURE__*/ jsxs(Fragment, {
|
|
@@ -118,7 +120,7 @@ const Menu = (props)=>{
|
|
|
118
120
|
}
|
|
119
121
|
}),
|
|
120
122
|
/*#__PURE__*/ jsx("div", {
|
|
121
|
-
ref:
|
|
123
|
+
ref: ref,
|
|
122
124
|
style: style,
|
|
123
125
|
className: styles_module.menu,
|
|
124
126
|
onMouseDown: (event)=>{
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
import { useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { createPortal } from "react-dom";
|
|
4
4
|
import { useFocused, useSlateSelection } from "slate-react";
|
|
5
5
|
import styles_module from "./styles.module.js";
|
|
@@ -7,6 +7,7 @@ const Menu = (props)=>{
|
|
|
7
7
|
const { isOpen, children } = props;
|
|
8
8
|
const isFocused = useFocused();
|
|
9
9
|
const selection = useSlateSelection();
|
|
10
|
+
const ref = useRef(null);
|
|
10
11
|
const [style, setStyle] = useState({});
|
|
11
12
|
useLayoutEffect(()=>{
|
|
12
13
|
if (!selection || !isFocused) return void setStyle(void 0);
|
|
@@ -29,7 +30,8 @@ const Menu = (props)=>{
|
|
|
29
30
|
isFocused,
|
|
30
31
|
isOpen
|
|
31
32
|
]);
|
|
32
|
-
|
|
33
|
+
useLayoutEffect(()=>{
|
|
34
|
+
const element = ref.current;
|
|
33
35
|
if (element) {
|
|
34
36
|
const { height, top } = element.getBoundingClientRect();
|
|
35
37
|
const domSelection = window.getSelection();
|
|
@@ -40,9 +42,9 @@ const Menu = (props)=>{
|
|
|
40
42
|
top: `${top - height - (rect?.height ?? 22)}px`
|
|
41
43
|
}));
|
|
42
44
|
}
|
|
43
|
-
};
|
|
45
|
+
}, []);
|
|
44
46
|
return /*#__PURE__*/ createPortal(/*#__PURE__*/ jsx("div", {
|
|
45
|
-
ref:
|
|
47
|
+
ref: ref,
|
|
46
48
|
style: style,
|
|
47
49
|
className: styles_module.root,
|
|
48
50
|
children: children
|
|
@@ -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,
|
|
@@ -35,6 +35,7 @@ export const Menu = (props: Props) => {
|
|
|
35
35
|
const selection = useSlateSelection();
|
|
36
36
|
const editor = useSlate() as Editor;
|
|
37
37
|
const isFocused = useFocused();
|
|
38
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
38
39
|
|
|
39
40
|
const entry = Editor.above<CustomElement>(editor, {
|
|
40
41
|
match: (n) => Editor.isBlock(editor, n as CustomElement),
|
|
@@ -137,7 +138,8 @@ export const Menu = (props: Props) => {
|
|
|
137
138
|
}
|
|
138
139
|
}, [store.filter]);
|
|
139
140
|
|
|
140
|
-
|
|
141
|
+
useLayoutEffect(() => {
|
|
142
|
+
const element = ref.current;
|
|
141
143
|
if (element) {
|
|
142
144
|
const { height, top } = element.getBoundingClientRect();
|
|
143
145
|
|
|
@@ -152,7 +154,7 @@ export const Menu = (props: Props) => {
|
|
|
152
154
|
}));
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
|
-
};
|
|
157
|
+
}, []);
|
|
156
158
|
|
|
157
159
|
if (!commands.length) {
|
|
158
160
|
return null;
|
|
@@ -174,7 +176,7 @@ export const Menu = (props: Props) => {
|
|
|
174
176
|
/>
|
|
175
177
|
)}
|
|
176
178
|
<div
|
|
177
|
-
ref={
|
|
179
|
+
ref={ref}
|
|
178
180
|
style={style}
|
|
179
181
|
className={styles.menu}
|
|
180
182
|
onMouseDown={(event) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type CSSProperties,
|
|
3
3
|
type ReactNode,
|
|
4
|
-
useLayoutEffect,
|
|
4
|
+
useLayoutEffect, useRef,
|
|
5
5
|
useState,
|
|
6
6
|
} from 'react';
|
|
7
7
|
import { createPortal } from 'react-dom';
|
|
@@ -18,6 +18,7 @@ export const Menu = (props: Props) => {
|
|
|
18
18
|
|
|
19
19
|
const isFocused = useFocused();
|
|
20
20
|
const selection = useSlateSelection();
|
|
21
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
21
22
|
|
|
22
23
|
const [style, setStyle] = useState<CSSProperties | undefined>({});
|
|
23
24
|
|
|
@@ -48,7 +49,8 @@ export const Menu = (props: Props) => {
|
|
|
48
49
|
}, 0);
|
|
49
50
|
}, [selection, isFocused, isOpen]);
|
|
50
51
|
|
|
51
|
-
|
|
52
|
+
useLayoutEffect(() => {
|
|
53
|
+
const element = ref.current;
|
|
52
54
|
if (element) {
|
|
53
55
|
const { height, top } = element.getBoundingClientRect();
|
|
54
56
|
|
|
@@ -63,10 +65,11 @@ export const Menu = (props: Props) => {
|
|
|
63
65
|
}));
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
|
-
};
|
|
68
|
+
}, []);
|
|
69
|
+
|
|
67
70
|
|
|
68
71
|
return createPortal(
|
|
69
|
-
<div ref={
|
|
72
|
+
<div ref={ref} style={style} className={styles.root}>
|
|
70
73
|
{children}
|
|
71
74
|
</div>,
|
|
72
75
|
document.body,
|
|
@@ -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;
|