@contentful/field-editor-json 4.3.1 → 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.
@@ -15,19 +15,6 @@ const _JsonEditorField = require("./JsonEditorField");
15
15
  const _JsonEditorToolbar = require("./JsonEditorToolbar");
16
16
  const _JsonInvalidStatus = require("./JsonInvalidStatus");
17
17
  const _utils = require("./utils");
18
- function _define_property(obj, key, value) {
19
- if (key in obj) {
20
- Object.defineProperty(obj, key, {
21
- value: value,
22
- enumerable: true,
23
- configurable: true,
24
- writable: true
25
- });
26
- } else {
27
- obj[key] = value;
28
- }
29
- return obj;
30
- }
31
18
  function _interop_require_default(obj) {
32
19
  return obj && obj.__esModule ? obj : {
33
20
  default: obj
@@ -74,104 +61,109 @@ function _interop_require_wildcard(obj, nodeInterop) {
74
61
  }
75
62
  return newObj;
76
63
  }
77
- var _React_Component;
78
- class ConnectedJsonEditor extends (_React_Component = _react.Component) {
79
- render() {
80
- return /*#__PURE__*/ _react.createElement("div", {
81
- "data-test-id": "json-editor"
82
- }, /*#__PURE__*/ _react.createElement(_JsonEditorToolbar.JsonEditorToolbar, {
83
- isRedoDisabled: this.props.disabled || this.state.redoStack.length === 0,
84
- isUndoDisabled: this.props.disabled || this.state.undoStack.length === 0,
85
- onUndo: this.onUndo,
86
- onRedo: this.onRedo
87
- }), /*#__PURE__*/ _react.createElement(_JsonEditorField.JsonEditorField, {
88
- value: this.state.value,
89
- onChange: this.onChange,
90
- isDisabled: this.props.disabled
91
- }), !this.state.isValidJson && /*#__PURE__*/ _react.createElement(_JsonInvalidStatus.JsonInvalidStatus, null));
92
- }
93
- constructor(props){
94
- super(props), _define_property(this, "setValidJson", (value)=>{
95
- this.setState({
96
- isValidJson: value
97
- });
98
- }), _define_property(this, "pushUndo", (0, _throttle.default)((value)=>{
99
- this.setState((state)=>({
64
+ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
65
+ const [state, setState] = _react.useState(()=>{
66
+ return {
67
+ value: (0, _utils.stringifyJSON)(initialValue),
68
+ isValidJson: true,
69
+ undoStack: [],
70
+ redoStack: [],
71
+ lastUndo: ''
72
+ };
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
+ });
83
+ const pushUndo = _react.useMemo(()=>(0, _throttle.default)((value)=>{
84
+ setState((currentState)=>({
85
+ ...currentState,
100
86
  undoStack: [
101
- ...state.undoStack,
87
+ ...currentState.undoStack,
102
88
  value
103
89
  ]
104
90
  }));
105
- }, 400)), _define_property(this, "onChange", (value)=>{
106
- const parsed = (0, _utils.parseJSON)(value);
107
- if (value !== this.state.lastUndo) {
108
- this.pushUndo(this.state.value);
109
- }
110
- this.setState({
91
+ }, 400), []);
92
+ const onChange = _react.useCallback((value)=>{
93
+ const currentState = stateRef.current;
94
+ const parsed = (0, _utils.parseJSON)(value);
95
+ if (value !== currentState.lastUndo) {
96
+ pushUndo(currentState.value);
97
+ }
98
+ setState((latestState)=>({
99
+ ...latestState,
111
100
  value,
112
101
  isValidJson: parsed.valid
113
- });
114
- if (parsed.valid) {
115
- this.props.setValue(parsed.value);
116
- }
117
- }), _define_property(this, "onUndo", ()=>{
118
- const undoStack = this.state.undoStack;
119
- if (undoStack.length === 0) {
120
- return;
121
- }
122
- const value = undoStack.pop() || '';
123
- const parsedValue = (0, _utils.parseJSON)(value);
124
- this.setState((state)=>({
125
- ...state,
126
- value,
127
- isValidJson: parsedValue.valid,
128
- undoStack,
129
- redoStack: [
130
- ...state.redoStack,
131
- state.value
132
- ],
133
- lastUndo: value
134
- }), ()=>{
135
- if (parsedValue.valid) {
136
- this.props.setValue(parsedValue.value);
137
- }
138
- });
139
- }), _define_property(this, "onRedo", ()=>{
140
- const redoStack = [
141
- ...this.state.redoStack
142
- ];
143
- if (redoStack.length === 0) {
144
- return;
145
- }
146
- const value = redoStack.pop() || '';
147
- const parsedValue = (0, _utils.parseJSON)(value);
148
- this.setState((state)=>({
149
- ...state,
150
- value,
151
- isValidJson: parsedValue.valid,
152
- redoStack,
153
- undoStack: [
154
- ...state.undoStack,
155
- state.value
156
- ]
157
- }), ()=>{
158
- if (parsedValue.valid) {
159
- this.props.setValue(parsedValue.value);
160
- }
161
- });
162
- });
163
- this.state = {
164
- value: (0, _utils.stringifyJSON)(props.initialValue),
165
- isValidJson: true,
166
- undoStack: [],
167
- redoStack: [],
168
- lastUndo: ''
169
- };
170
- }
102
+ }));
103
+ if (parsed.valid) {
104
+ setValueRef.current(parsed.value);
105
+ }
106
+ }, [
107
+ pushUndo
108
+ ]);
109
+ const onUndo = _react.useCallback(()=>{
110
+ const undoStack = stateRef.current.undoStack;
111
+ if (undoStack.length === 0) {
112
+ return;
113
+ }
114
+ const value = undoStack.pop() || '';
115
+ const parsedValue = (0, _utils.parseJSON)(value);
116
+ if (parsedValue.valid) {
117
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
118
+ }
119
+ setState((currentState)=>({
120
+ ...currentState,
121
+ value,
122
+ isValidJson: parsedValue.valid,
123
+ undoStack,
124
+ redoStack: [
125
+ ...currentState.redoStack,
126
+ currentState.value
127
+ ],
128
+ lastUndo: value
129
+ }));
130
+ }, []);
131
+ const onRedo = _react.useCallback(()=>{
132
+ const redoStack = [
133
+ ...stateRef.current.redoStack
134
+ ];
135
+ if (redoStack.length === 0) {
136
+ return;
137
+ }
138
+ const value = redoStack.pop() || '';
139
+ const parsedValue = (0, _utils.parseJSON)(value);
140
+ if (parsedValue.valid) {
141
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
142
+ }
143
+ setState((currentState)=>({
144
+ ...currentState,
145
+ value,
146
+ isValidJson: parsedValue.valid,
147
+ redoStack,
148
+ undoStack: [
149
+ ...currentState.undoStack,
150
+ currentState.value
151
+ ]
152
+ }));
153
+ }, []);
154
+ return /*#__PURE__*/ _react.createElement("div", {
155
+ "data-test-id": "json-editor"
156
+ }, /*#__PURE__*/ _react.createElement(_JsonEditorToolbar.JsonEditorToolbar, {
157
+ isRedoDisabled: disabled || state.redoStack.length === 0,
158
+ isUndoDisabled: disabled || state.undoStack.length === 0,
159
+ onUndo: onUndo,
160
+ onRedo: onRedo
161
+ }), /*#__PURE__*/ _react.createElement(_JsonEditorField.JsonEditorField, {
162
+ value: state.value,
163
+ onChange: onChange,
164
+ isDisabled: disabled
165
+ }), !state.isValidJson && /*#__PURE__*/ _react.createElement(_JsonInvalidStatus.JsonInvalidStatus, null));
171
166
  }
172
- _define_property(ConnectedJsonEditor, "defaultProps", {
173
- isInitiallyDisabled: true
174
- });
175
167
  function JsonEditor(props) {
176
168
  return /*#__PURE__*/ _react.createElement(_fieldeditorshared.FieldConnector, {
177
169
  field: props.field,
@@ -1,16 +1,3 @@
1
- function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
1
  import * as React from 'react';
15
2
  import { FieldConnector } from '@contentful/field-editor-shared';
16
3
  import throttle from 'lodash/throttle';
@@ -18,104 +5,109 @@ import { JsonEditorField } from './JsonEditorField';
18
5
  import { JsonEditorToolbar } from './JsonEditorToolbar';
19
6
  import { JsonInvalidStatus } from './JsonInvalidStatus';
20
7
  import { stringifyJSON, parseJSON, SPACE_INDENT_COUNT } from './utils';
21
- var _React_Component;
22
- class ConnectedJsonEditor extends (_React_Component = React.Component) {
23
- render() {
24
- return /*#__PURE__*/ React.createElement("div", {
25
- "data-test-id": "json-editor"
26
- }, /*#__PURE__*/ React.createElement(JsonEditorToolbar, {
27
- isRedoDisabled: this.props.disabled || this.state.redoStack.length === 0,
28
- isUndoDisabled: this.props.disabled || this.state.undoStack.length === 0,
29
- onUndo: this.onUndo,
30
- onRedo: this.onRedo
31
- }), /*#__PURE__*/ React.createElement(JsonEditorField, {
32
- value: this.state.value,
33
- onChange: this.onChange,
34
- isDisabled: this.props.disabled
35
- }), !this.state.isValidJson && /*#__PURE__*/ React.createElement(JsonInvalidStatus, null));
36
- }
37
- constructor(props){
38
- super(props), _define_property(this, "setValidJson", (value)=>{
39
- this.setState({
40
- isValidJson: value
41
- });
42
- }), _define_property(this, "pushUndo", throttle((value)=>{
43
- this.setState((state)=>({
8
+ function ConnectedJsonEditor({ initialValue, setValue, disabled }) {
9
+ const [state, setState] = React.useState(()=>{
10
+ return {
11
+ value: stringifyJSON(initialValue),
12
+ isValidJson: true,
13
+ undoStack: [],
14
+ redoStack: [],
15
+ lastUndo: ''
16
+ };
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
+ });
27
+ const pushUndo = React.useMemo(()=>throttle((value)=>{
28
+ setState((currentState)=>({
29
+ ...currentState,
44
30
  undoStack: [
45
- ...state.undoStack,
31
+ ...currentState.undoStack,
46
32
  value
47
33
  ]
48
34
  }));
49
- }, 400)), _define_property(this, "onChange", (value)=>{
50
- const parsed = parseJSON(value);
51
- if (value !== this.state.lastUndo) {
52
- this.pushUndo(this.state.value);
53
- }
54
- this.setState({
35
+ }, 400), []);
36
+ const onChange = React.useCallback((value)=>{
37
+ const currentState = stateRef.current;
38
+ const parsed = parseJSON(value);
39
+ if (value !== currentState.lastUndo) {
40
+ pushUndo(currentState.value);
41
+ }
42
+ setState((latestState)=>({
43
+ ...latestState,
55
44
  value,
56
45
  isValidJson: parsed.valid
57
- });
58
- if (parsed.valid) {
59
- this.props.setValue(parsed.value);
60
- }
61
- }), _define_property(this, "onUndo", ()=>{
62
- const undoStack = this.state.undoStack;
63
- if (undoStack.length === 0) {
64
- return;
65
- }
66
- const value = undoStack.pop() || '';
67
- const parsedValue = parseJSON(value);
68
- this.setState((state)=>({
69
- ...state,
70
- value,
71
- isValidJson: parsedValue.valid,
72
- undoStack,
73
- redoStack: [
74
- ...state.redoStack,
75
- state.value
76
- ],
77
- lastUndo: value
78
- }), ()=>{
79
- if (parsedValue.valid) {
80
- this.props.setValue(parsedValue.value);
81
- }
82
- });
83
- }), _define_property(this, "onRedo", ()=>{
84
- const redoStack = [
85
- ...this.state.redoStack
86
- ];
87
- if (redoStack.length === 0) {
88
- return;
89
- }
90
- const value = redoStack.pop() || '';
91
- const parsedValue = parseJSON(value);
92
- this.setState((state)=>({
93
- ...state,
94
- value,
95
- isValidJson: parsedValue.valid,
96
- redoStack,
97
- undoStack: [
98
- ...state.undoStack,
99
- state.value
100
- ]
101
- }), ()=>{
102
- if (parsedValue.valid) {
103
- this.props.setValue(parsedValue.value);
104
- }
105
- });
106
- });
107
- this.state = {
108
- value: stringifyJSON(props.initialValue),
109
- isValidJson: true,
110
- undoStack: [],
111
- redoStack: [],
112
- lastUndo: ''
113
- };
114
- }
46
+ }));
47
+ if (parsed.valid) {
48
+ setValueRef.current(parsed.value);
49
+ }
50
+ }, [
51
+ pushUndo
52
+ ]);
53
+ const onUndo = React.useCallback(()=>{
54
+ const undoStack = stateRef.current.undoStack;
55
+ if (undoStack.length === 0) {
56
+ return;
57
+ }
58
+ const value = undoStack.pop() || '';
59
+ const parsedValue = parseJSON(value);
60
+ if (parsedValue.valid) {
61
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
62
+ }
63
+ setState((currentState)=>({
64
+ ...currentState,
65
+ value,
66
+ isValidJson: parsedValue.valid,
67
+ undoStack,
68
+ redoStack: [
69
+ ...currentState.redoStack,
70
+ currentState.value
71
+ ],
72
+ lastUndo: value
73
+ }));
74
+ }, []);
75
+ const onRedo = React.useCallback(()=>{
76
+ const redoStack = [
77
+ ...stateRef.current.redoStack
78
+ ];
79
+ if (redoStack.length === 0) {
80
+ return;
81
+ }
82
+ const value = redoStack.pop() || '';
83
+ const parsedValue = parseJSON(value);
84
+ if (parsedValue.valid) {
85
+ valuesToPersistAfterCommitRef.current.push(parsedValue.value);
86
+ }
87
+ setState((currentState)=>({
88
+ ...currentState,
89
+ value,
90
+ isValidJson: parsedValue.valid,
91
+ redoStack,
92
+ undoStack: [
93
+ ...currentState.undoStack,
94
+ currentState.value
95
+ ]
96
+ }));
97
+ }, []);
98
+ return /*#__PURE__*/ React.createElement("div", {
99
+ "data-test-id": "json-editor"
100
+ }, /*#__PURE__*/ React.createElement(JsonEditorToolbar, {
101
+ isRedoDisabled: disabled || state.redoStack.length === 0,
102
+ isUndoDisabled: disabled || state.undoStack.length === 0,
103
+ onUndo: onUndo,
104
+ onRedo: onRedo
105
+ }), /*#__PURE__*/ React.createElement(JsonEditorField, {
106
+ value: state.value,
107
+ onChange: onChange,
108
+ isDisabled: disabled
109
+ }), !state.isValidJson && /*#__PURE__*/ React.createElement(JsonInvalidStatus, null));
115
110
  }
116
- _define_property(ConnectedJsonEditor, "defaultProps", {
117
- isInitiallyDisabled: true
118
- });
119
111
  export default function JsonEditor(props) {
120
112
  return /*#__PURE__*/ React.createElement(FieldConnector, {
121
113
  field: props.field,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-json",
3
- "version": "4.3.1",
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.1",
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": "78ed76ca4a3207924294971766a40bb46e196203"
61
+ "gitHead": "69033935571ab64c941e30158340242c5a449c64"
62
62
  }