jodit-react 5.4.72 → 5.4.80

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.
@@ -1,5 +1,6 @@
1
1
  import React, { useEffect, useRef, forwardRef } from 'react';
2
2
  import { Jodit } from './include.jodit';
3
+ const MAX_TRACKED_EMITTED_VALUES = 20;
3
4
  function usePrevious(value) {
4
5
  const ref = useRef('');
5
6
  useEffect(() => {
@@ -11,6 +12,12 @@ function usePrevious(value) {
11
12
  const JoditEditor = forwardRef(({ JoditConstructor = Jodit, className, config, id, name, onBlur, onChange, tabIndex, value, editorRef }, ref) => {
12
13
  const textAreaRef = useRef(null);
13
14
  const joditRef = useRef(null);
15
+ // Values the editor itself reported through `change` and that the
16
+ // parent may echo back through the `value` prop. When a render
17
+ // lags behind typing, the echoed prop is an older snapshot than
18
+ // the editor content; writing it back would replace the DOM and
19
+ // throw the caret to the start of the editor (#217)
20
+ const emittedValuesRef = useRef([]);
14
21
  useEffect(() => {
15
22
  const element = textAreaRef.current;
16
23
  const jodit = JoditConstructor.make(element, config);
@@ -69,11 +76,20 @@ const JoditEditor = forwardRef(({ JoditConstructor = Jodit, className, config, i
69
76
  }, [tabIndex]);
70
77
  useEffect(() => {
71
78
  const jodit = joditRef.current;
72
- if (!jodit?.events || !(onBlur || onChange)) {
79
+ if (!jodit?.events) {
73
80
  return;
74
81
  }
75
82
  const onBlurHandler = (event) => onBlur && onBlur(joditRef?.current?.value ?? '', event);
76
- const onChangeHandler = (value) => onChange && onChange(value);
83
+ const onChangeHandler = (value) => {
84
+ const emitted = emittedValuesRef.current;
85
+ emitted.push(value);
86
+ if (emitted.length > MAX_TRACKED_EMITTED_VALUES) {
87
+ emitted.splice(0, emitted.length - MAX_TRACKED_EMITTED_VALUES);
88
+ }
89
+ if (onChange) {
90
+ onChange(value);
91
+ }
92
+ };
77
93
  // adding event handlers
78
94
  jodit.events
79
95
  .on('blur', onBlurHandler)
@@ -88,7 +104,20 @@ const JoditEditor = forwardRef(({ JoditConstructor = Jodit, className, config, i
88
104
  useEffect(() => {
89
105
  const jodit = joditRef.current;
90
106
  const updateValue = () => {
91
- if (jodit && value !== undefined && jodit.value !== value) {
107
+ if (!jodit || value === undefined) {
108
+ return;
109
+ }
110
+ const emitted = emittedValuesRef.current;
111
+ const echoIndex = emitted.indexOf(value);
112
+ if (echoIndex !== -1) {
113
+ // The parent handed back something the editor reported
114
+ // itself: either the current content or a stale
115
+ // snapshot. Neither must be written back into the DOM
116
+ emitted.splice(0, echoIndex + 1);
117
+ return;
118
+ }
119
+ if (jodit.value !== value) {
120
+ emitted.length = 0;
92
121
  jodit.value = value;
93
122
  }
94
123
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jodit-react",
3
- "version": "5.4.72",
3
+ "version": "5.4.80",
4
4
  "description": "Jodit is awesome and usefully wysiwyg editor with filebrowser",
5
5
  "main": "build/react-jodit.js",
6
6
  "module": "./build/esm/index.mjs",
@@ -43,7 +43,7 @@
43
43
  "rte"
44
44
  ],
45
45
  "dependencies": {
46
- "jodit": "^4.13.18"
46
+ "jodit": "^4.13.26"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "react": "~0.14 || ^15 || ^16 || ^17 || ^18 || ^19",