hero-editor 1.9.11 → 1.9.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hero-editor",
3
- "version": "1.9.11",
3
+ "version": "1.9.13",
4
4
  "description": "",
5
5
  "main": "dist/lib.js",
6
6
  "scripts": {
@@ -28,9 +28,10 @@
28
28
  "webpack-cli": "^3.3.11"
29
29
  },
30
30
  "dependencies": {
31
+ "@juggle/resize-observer": "^3.4.0",
31
32
  "is-url": "^1.2.4",
32
- "slate": "^0.70.0",
33
- "slate-react": "^0.70.0"
33
+ "slate": "^0.71.0",
34
+ "slate-react": "^0.71.0"
34
35
  },
35
36
  "peerDependencies": {
36
37
  "react": "^16.13.1",
package/src/lib.js CHANGED
@@ -1,5 +1,5 @@
1
- import React, { useState, useMemo, useEffect, useRef } from 'react';
2
- import { createEditor } from 'slate';
1
+ import React, { useMemo, useEffect, useRef } from 'react';
2
+ import { createEditor, Editor, Transforms } from 'slate';
3
3
  import { Slate, Editable, withReact } from 'slate-react';
4
4
  import {
5
5
  composeRenderLeaf,
@@ -18,6 +18,12 @@ import {
18
18
  EDITOR_BLUR,
19
19
  EDITOR_LAYOUT,
20
20
  } from './constants';
21
+ import { ResizeObserver } from '@juggle/resize-observer';
22
+ import { isEmptyContent } from './helpers';
23
+
24
+ if (!window.ResizeObserver) {
25
+ window.ResizeObserver = ResizeObserver;
26
+ }
21
27
 
22
28
  const styles = {
23
29
  editorWrapper: {
@@ -84,6 +90,12 @@ const HeroEditor = ({
84
90
 
85
91
  const toolBarButtons = useMemo(() => getToolbarButtons(plugins), [plugins]);
86
92
 
93
+ useEffect(() => {
94
+ if (editor && autoFocus && !isEmptyContent(value)) {
95
+ Transforms.select(editor, Editor.end(editor, []));
96
+ }
97
+ }, []);
98
+
87
99
  useEffect(() => {
88
100
  const removeHandlers = addMessageListeners(editor)(plugins);
89
101
 
@@ -91,18 +103,30 @@ const HeroEditor = ({
91
103
  }, [plugins]);
92
104
 
93
105
  useEffect(() => {
94
- const width = wrapper.current?.offsetWidth;
95
- const height = wrapper.current?.offsetHeight;
96
-
97
- if (
98
- width !== wrapperLayout.current.width ||
99
- height !== wrapperLayout.current.height
100
- ) {
101
- wrapperLayout.current = { width, height };
102
- onLayout(wrapperLayout.current);
103
- postMessage(EDITOR_LAYOUT, wrapperLayout.current, editor);
104
- }
105
- }, [value]);
106
+ const observer = new ResizeObserver((entries) => {
107
+ const entry = entries[0];
108
+ if (!entry) {
109
+ return;
110
+ }
111
+
112
+ const { width, height } = entry.contentRect;
113
+ if (
114
+ width !== wrapperLayout.current.width ||
115
+ height !== wrapperLayout.current.height
116
+ ) {
117
+ wrapperLayout.current = { width, height };
118
+ onLayout(wrapperLayout.current);
119
+ postMessage(EDITOR_LAYOUT, wrapperLayout.current, editor);
120
+ }
121
+ });
122
+ observer.observe(wrapper.current);
123
+
124
+ return () => {
125
+ if (wrapper.current) {
126
+ observer.unobserve(wrapper.current);
127
+ }
128
+ };
129
+ }, []);
106
130
 
107
131
  return (
108
132
  <div