hero-editor 1.16.0 → 2.0.0
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/app.js +1 -1
- package/dist/app.js.map +1 -1
- package/dist/lib.js +1 -1
- package/dist/lib.js.map +1 -1
- package/package.json +12 -8
- package/src/constants.js +2 -0
- package/src/helpers/apply.js +4 -1
- package/src/helpers/compose.js +4 -2
- package/src/helpers/flow.js +4 -2
- package/src/helpers/why.js +9 -1
- package/src/lib.js +20 -2
- package/src/plugins/bold.js +1 -0
- package/src/plugins/bulletedList.js +1 -0
- package/src/plugins/cursorTracker.js +21 -3
- package/src/plugins/headingOne.js +1 -0
- package/src/plugins/headingTwo.js +1 -0
- package/src/plugins/italic.js +1 -0
- package/src/plugins/link.js +3 -1
- package/src/plugins/numberedList.js +1 -0
- package/src/plugins/underline.js +1 -0
- package/src/transformers/react.js +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hero-editor",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/lib.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,21 +13,22 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@babel/core": "^7.
|
|
17
|
-
"@babel/preset-env": "^7.
|
|
18
|
-
"@babel/preset-react": "^7.
|
|
16
|
+
"@babel/core": "^7.26.0",
|
|
17
|
+
"@babel/preset-env": "^7.26.0",
|
|
18
|
+
"@babel/preset-react": "^7.25.9",
|
|
19
19
|
"@testing-library/react": "^10.2.1",
|
|
20
20
|
"@testing-library/react-hooks": "^3.3.0",
|
|
21
21
|
"@webpack-cli/serve": "^2.0.5",
|
|
22
|
-
"babel-jest": "^
|
|
23
|
-
"babel-loader": "^
|
|
24
|
-
"jest": "^
|
|
22
|
+
"babel-jest": "^29.7.0",
|
|
23
|
+
"babel-loader": "^9.2.1",
|
|
24
|
+
"jest": "^29.7.0",
|
|
25
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
25
26
|
"react": "^16.13.1",
|
|
26
27
|
"react-dom": "^16.13.1",
|
|
27
28
|
"react-test-renderer": "^16.13.1",
|
|
28
29
|
"webpack": "^5.95.0",
|
|
29
30
|
"webpack-cli": "^5.1.4",
|
|
30
|
-
"webpack-dev-server": "^5.1
|
|
31
|
+
"webpack-dev-server": "^5.2.1"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"@juggle/resize-observer": "^3.4.0",
|
|
@@ -40,5 +41,8 @@
|
|
|
40
41
|
"peerDependencies": {
|
|
41
42
|
"react": "^16.13.1",
|
|
42
43
|
"react-dom": "^16.13.1"
|
|
44
|
+
},
|
|
45
|
+
"jest": {
|
|
46
|
+
"testEnvironment": "jsdom"
|
|
43
47
|
}
|
|
44
48
|
}
|
package/src/constants.js
CHANGED
|
@@ -37,6 +37,7 @@ const EMPTY_VALUE = [
|
|
|
37
37
|
|
|
38
38
|
// Link actions
|
|
39
39
|
const ADD_LINK = 'add-link';
|
|
40
|
+
const REQUEST_UPSERT_LINK = 'request-upsert-link';
|
|
40
41
|
|
|
41
42
|
export {
|
|
42
43
|
EMPTY_VALUE,
|
|
@@ -61,4 +62,5 @@ export {
|
|
|
61
62
|
EDITOR_DELETE_BACKWARD,
|
|
62
63
|
EDITOR_BUILT_IN_METHOD,
|
|
63
64
|
EDITOR_READ_ONLY,
|
|
65
|
+
REQUEST_UPSERT_LINK,
|
|
64
66
|
};
|
package/src/helpers/apply.js
CHANGED
package/src/helpers/compose.js
CHANGED
package/src/helpers/flow.js
CHANGED
package/src/helpers/why.js
CHANGED
package/src/lib.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
useMemo,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
useCallback,
|
|
7
|
+
} from 'react';
|
|
2
8
|
import { createEditor, Editor, Transforms } from 'slate';
|
|
3
9
|
import { Slate, Editable, withReact } from 'slate-react';
|
|
4
10
|
import { withHistory } from 'slate-history';
|
|
@@ -81,6 +87,14 @@ const HeroEditor = ({
|
|
|
81
87
|
)(),
|
|
82
88
|
);
|
|
83
89
|
|
|
90
|
+
const getToolbarState = useCallback(() => {
|
|
91
|
+
return plugins.reduce((acc, plugin) => {
|
|
92
|
+
if (plugin.isActive) {
|
|
93
|
+
acc[plugin.name] = plugin.isActive(editor);
|
|
94
|
+
}
|
|
95
|
+
return acc;
|
|
96
|
+
}, {});
|
|
97
|
+
}, [editor, plugins]);
|
|
84
98
|
const renderLeaf = useMemo(() => composeRenderLeaf(plugins), [plugins]);
|
|
85
99
|
|
|
86
100
|
const renderElement = useMemo(() => composeRenderElement(plugins), [plugins]);
|
|
@@ -142,7 +156,11 @@ const HeroEditor = ({
|
|
|
142
156
|
initialValue={initialValue || value}
|
|
143
157
|
onChange={(value) => {
|
|
144
158
|
onChange(value);
|
|
145
|
-
postMessage(
|
|
159
|
+
postMessage(
|
|
160
|
+
EDITOR_CHANGE,
|
|
161
|
+
{ value, toolbarState: getToolbarState() },
|
|
162
|
+
editor,
|
|
163
|
+
);
|
|
146
164
|
}}
|
|
147
165
|
>
|
|
148
166
|
{showToolbar ? <Toolbar>{toolBarButtons}</Toolbar> : null}
|
package/src/plugins/bold.js
CHANGED
|
@@ -2,20 +2,38 @@ import { ReactEditor } from 'slate-react';
|
|
|
2
2
|
import { createPlugin, addMessageListener, postMessage } from '../helpers';
|
|
3
3
|
import { EDITOR_CHANGE } from '../constants';
|
|
4
4
|
|
|
5
|
+
const getCursorPosition = (domRange) => {
|
|
6
|
+
const rangeRect = domRange.getBoundingClientRect();
|
|
7
|
+
/**
|
|
8
|
+
* On mobile, slate insert a <br> node when adding a new line which causing the DOM range after <br /> not have a bounding client rect
|
|
9
|
+
* so we need to check if the start container is a <br> node and return the bounding client rect of it
|
|
10
|
+
*/
|
|
11
|
+
if (domRange.collapsed && domRange.startContainer.nodeName === 'BR') {
|
|
12
|
+
return domRange.startContainer?.getBoundingClientRect();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return rangeRect;
|
|
16
|
+
};
|
|
17
|
+
|
|
5
18
|
const handleMessage = addMessageListener(EDITOR_CHANGE, ({ editor }) => {
|
|
6
19
|
const { selection } = editor;
|
|
7
20
|
|
|
8
21
|
if (selection) {
|
|
9
|
-
const domRange = ReactEditor.toDOMRange(editor,
|
|
10
|
-
|
|
22
|
+
const domRange = ReactEditor.toDOMRange(editor, {
|
|
23
|
+
anchor: selection.focus,
|
|
24
|
+
focus: selection.focus,
|
|
25
|
+
});
|
|
26
|
+
const rangeRect = getCursorPosition(domRange);
|
|
27
|
+
if (!rangeRect) return;
|
|
11
28
|
|
|
12
29
|
postMessage(
|
|
13
30
|
'cursor-change',
|
|
14
31
|
{
|
|
15
|
-
offset: undefined,
|
|
16
32
|
position: {
|
|
17
33
|
top: rangeRect.top,
|
|
18
34
|
left: rangeRect.left,
|
|
35
|
+
bottom: rangeRect.bottom,
|
|
36
|
+
right: rangeRect.right,
|
|
19
37
|
},
|
|
20
38
|
},
|
|
21
39
|
editor,
|
package/src/plugins/italic.js
CHANGED
package/src/plugins/link.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
isLink,
|
|
12
12
|
getUrlFromNode,
|
|
13
13
|
} from '../helpers';
|
|
14
|
-
import { LINK, ADD_LINK } from '../constants';
|
|
14
|
+
import { LINK, ADD_LINK, REQUEST_UPSERT_LINK } from '../constants';
|
|
15
15
|
import Toolbar from '../components/Toolbar';
|
|
16
16
|
import Icon from '../components/Icon';
|
|
17
17
|
|
|
@@ -132,6 +132,7 @@ const LinkCustomWrapper = ({ renderLinkCustom }) => {
|
|
|
132
132
|
useEffect(() => {
|
|
133
133
|
const removeLinkCustomListener = addMessageListener(ADD_LINK, () => {
|
|
134
134
|
setShowLinkCustom(true);
|
|
135
|
+
postMessage(REQUEST_UPSERT_LINK, getSelectedData(editor), editor);
|
|
135
136
|
})(editor);
|
|
136
137
|
|
|
137
138
|
return () => removeLinkCustomListener();
|
|
@@ -184,5 +185,6 @@ export default ({ renderLinkCustom, showToolbarButton } = {}) =>
|
|
|
184
185
|
ToolbarButton: () => (
|
|
185
186
|
<ToolbarButton showToolbarButton={showToolbarButton} />
|
|
186
187
|
),
|
|
188
|
+
isActive: (editor) => isBlockActive(editor, LINK),
|
|
187
189
|
enhanceEditor,
|
|
188
190
|
});
|
package/src/plugins/underline.js
CHANGED