@contentful/field-editor-json 4.3.2-canary.29 → 4.3.2-canary.30

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.
@@ -71,6 +71,15 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
71
71
  lastUndo: ''
72
72
  };
73
73
  });
74
+ const stateRef = _react.useRef(state);
75
+ stateRef.current = state;
76
+ const setValueRef = _react.useRef(setValue);
77
+ setValueRef.current = setValue;
78
+ const valuesToPersistAfterCommitRef = _react.useRef([]);
79
+ _react.useLayoutEffect(()=>{
80
+ const values = valuesToPersistAfterCommitRef.current.splice(0);
81
+ values.forEach((value)=>setValueRef.current(value));
82
+ });
74
83
  const pushUndo = _react.useMemo(()=>(0, _throttle.default)((value)=>{
75
84
  setState((currentState)=>({
76
85
  ...currentState,
@@ -80,32 +89,33 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
80
89
  ]
81
90
  }));
82
91
  }, 400), []);
83
- _react.useEffect(()=>()=>pushUndo.cancel(), [
84
- pushUndo
85
- ]);
86
- const onChange = (value)=>{
92
+ const onChange = _react.useCallback((value)=>{
93
+ const currentState = stateRef.current;
87
94
  const parsed = (0, _utils.parseJSON)(value);
88
- if (value !== state.lastUndo) {
89
- pushUndo(state.value);
95
+ if (value !== currentState.lastUndo) {
96
+ pushUndo(currentState.value);
90
97
  }
91
- setState((currentState)=>({
92
- ...currentState,
98
+ setState((latestState)=>({
99
+ ...latestState,
93
100
  value,
94
101
  isValidJson: parsed.valid
95
102
  }));
96
103
  if (parsed.valid) {
97
- setValue(parsed.value);
104
+ setValueRef.current(parsed.value);
98
105
  }
99
- };
100
- const onUndo = ()=>{
101
- const undoStack = [
102
- ...state.undoStack
103
- ];
106
+ }, [
107
+ pushUndo
108
+ ]);
109
+ const onUndo = _react.useCallback(()=>{
110
+ const undoStack = stateRef.current.undoStack;
104
111
  if (undoStack.length === 0) {
105
112
  return;
106
113
  }
107
114
  const value = undoStack.pop() || '';
108
115
  const parsedValue = (0, _utils.parseJSON)(value);
116
+ if (parsedValue.valid) {
117
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
118
+ }
109
119
  setState((currentState)=>({
110
120
  ...currentState,
111
121
  value,
@@ -117,19 +127,19 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
117
127
  ],
118
128
  lastUndo: value
119
129
  }));
120
- if (parsedValue.valid) {
121
- setValue(parsedValue.value);
122
- }
123
- };
124
- const onRedo = ()=>{
130
+ }, []);
131
+ const onRedo = _react.useCallback(()=>{
125
132
  const redoStack = [
126
- ...state.redoStack
133
+ ...stateRef.current.redoStack
127
134
  ];
128
135
  if (redoStack.length === 0) {
129
136
  return;
130
137
  }
131
138
  const value = redoStack.pop() || '';
132
139
  const parsedValue = (0, _utils.parseJSON)(value);
140
+ if (parsedValue.valid) {
141
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
142
+ }
133
143
  setState((currentState)=>({
134
144
  ...currentState,
135
145
  value,
@@ -140,10 +150,7 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
140
150
  currentState.value
141
151
  ]
142
152
  }));
143
- if (parsedValue.valid) {
144
- setValue(parsedValue.value);
145
- }
146
- };
153
+ }, []);
147
154
  return /*#__PURE__*/ _react.createElement("div", {
148
155
  "data-test-id": "json-editor"
149
156
  }, /*#__PURE__*/ _react.createElement(_JsonEditorToolbar.JsonEditorToolbar, {
@@ -15,6 +15,15 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
15
15
  lastUndo: ''
16
16
  };
17
17
  });
18
+ const stateRef = React.useRef(state);
19
+ stateRef.current = state;
20
+ const setValueRef = React.useRef(setValue);
21
+ setValueRef.current = setValue;
22
+ const valuesToPersistAfterCommitRef = React.useRef([]);
23
+ React.useLayoutEffect(()=>{
24
+ const values = valuesToPersistAfterCommitRef.current.splice(0);
25
+ values.forEach((value)=>setValueRef.current(value));
26
+ });
18
27
  const pushUndo = React.useMemo(()=>throttle((value)=>{
19
28
  setState((currentState)=>({
20
29
  ...currentState,
@@ -24,32 +33,33 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
24
33
  ]
25
34
  }));
26
35
  }, 400), []);
27
- React.useEffect(()=>()=>pushUndo.cancel(), [
28
- pushUndo
29
- ]);
30
- const onChange = (value)=>{
36
+ const onChange = React.useCallback((value)=>{
37
+ const currentState = stateRef.current;
31
38
  const parsed = parseJSON(value);
32
- if (value !== state.lastUndo) {
33
- pushUndo(state.value);
39
+ if (value !== currentState.lastUndo) {
40
+ pushUndo(currentState.value);
34
41
  }
35
- setState((currentState)=>({
36
- ...currentState,
42
+ setState((latestState)=>({
43
+ ...latestState,
37
44
  value,
38
45
  isValidJson: parsed.valid
39
46
  }));
40
47
  if (parsed.valid) {
41
- setValue(parsed.value);
48
+ setValueRef.current(parsed.value);
42
49
  }
43
- };
44
- const onUndo = ()=>{
45
- const undoStack = [
46
- ...state.undoStack
47
- ];
50
+ }, [
51
+ pushUndo
52
+ ]);
53
+ const onUndo = React.useCallback(()=>{
54
+ const undoStack = stateRef.current.undoStack;
48
55
  if (undoStack.length === 0) {
49
56
  return;
50
57
  }
51
58
  const value = undoStack.pop() || '';
52
59
  const parsedValue = parseJSON(value);
60
+ if (parsedValue.valid) {
61
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
62
+ }
53
63
  setState((currentState)=>({
54
64
  ...currentState,
55
65
  value,
@@ -61,19 +71,19 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
61
71
  ],
62
72
  lastUndo: value
63
73
  }));
64
- if (parsedValue.valid) {
65
- setValue(parsedValue.value);
66
- }
67
- };
68
- const onRedo = ()=>{
74
+ }, []);
75
+ const onRedo = React.useCallback(()=>{
69
76
  const redoStack = [
70
- ...state.redoStack
77
+ ...stateRef.current.redoStack
71
78
  ];
72
79
  if (redoStack.length === 0) {
73
80
  return;
74
81
  }
75
82
  const value = redoStack.pop() || '';
76
83
  const parsedValue = parseJSON(value);
84
+ if (parsedValue.valid) {
85
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
86
+ }
77
87
  setState((currentState)=>({
78
88
  ...currentState,
79
89
  value,
@@ -84,10 +94,7 @@ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
84
94
  currentState.value
85
95
  ]
86
96
  }));
87
- if (parsedValue.valid) {
88
- setValue(parsedValue.value);
89
- }
90
- };
97
+ }, []);
91
98
  return /*#__PURE__*/ React.createElement("div", {
92
99
  "data-test-id": "json-editor"
93
100
  }, /*#__PURE__*/ React.createElement(JsonEditorToolbar, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-json",
3
- "version": "4.3.2-canary.29+3ee64435",
3
+ "version": "4.3.2-canary.30+69033935",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "@contentful/f36-components": "^6.7.1",
41
41
  "@contentful/f36-icons": "^6.7.1",
42
42
  "@contentful/f36-tokens": "^6.1.2",
43
- "@contentful/field-editor-shared": "^4.5.2-canary.29+3ee64435",
43
+ "@contentful/field-editor-shared": "^4.5.2-canary.30+69033935",
44
44
  "@emotion/css": "^11.13.5",
45
45
  "@types/react-codemirror": "1.0.11",
46
46
  "@uiw/react-codemirror": "^4.11.4",
@@ -58,5 +58,5 @@
58
58
  "publishConfig": {
59
59
  "registry": "https://npm.pkg.github.com/"
60
60
  },
61
- "gitHead": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
61
+ "gitHead": "69033935571ab64c941e30158340242c5a449c64"
62
62
  }