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

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,102 @@ 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 pushUndo = _react.useMemo(()=>(0, _throttle.default)((value)=>{
75
+ setState((currentState)=>({
76
+ ...currentState,
100
77
  undoStack: [
101
- ...state.undoStack,
78
+ ...currentState.undoStack,
102
79
  value
103
80
  ]
104
81
  }));
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({
82
+ }, 400), []);
83
+ _react.useEffect(()=>()=>pushUndo.cancel(), [
84
+ pushUndo
85
+ ]);
86
+ const onChange = (value)=>{
87
+ const parsed = (0, _utils.parseJSON)(value);
88
+ if (value !== state.lastUndo) {
89
+ pushUndo(state.value);
90
+ }
91
+ setState((currentState)=>({
92
+ ...currentState,
111
93
  value,
112
94
  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
- }
95
+ }));
96
+ if (parsed.valid) {
97
+ setValue(parsed.value);
98
+ }
99
+ };
100
+ const onUndo = ()=>{
101
+ const undoStack = [
102
+ ...state.undoStack
103
+ ];
104
+ if (undoStack.length === 0) {
105
+ return;
106
+ }
107
+ const value = undoStack.pop() || '';
108
+ const parsedValue = (0, _utils.parseJSON)(value);
109
+ setState((currentState)=>({
110
+ ...currentState,
111
+ value,
112
+ isValidJson: parsedValue.valid,
113
+ undoStack,
114
+ redoStack: [
115
+ ...currentState.redoStack,
116
+ currentState.value
117
+ ],
118
+ lastUndo: value
119
+ }));
120
+ if (parsedValue.valid) {
121
+ setValue(parsedValue.value);
122
+ }
123
+ };
124
+ const onRedo = ()=>{
125
+ const redoStack = [
126
+ ...state.redoStack
127
+ ];
128
+ if (redoStack.length === 0) {
129
+ return;
130
+ }
131
+ const value = redoStack.pop() || '';
132
+ const parsedValue = (0, _utils.parseJSON)(value);
133
+ setState((currentState)=>({
134
+ ...currentState,
135
+ value,
136
+ isValidJson: parsedValue.valid,
137
+ redoStack,
138
+ undoStack: [
139
+ ...currentState.undoStack,
140
+ currentState.value
141
+ ]
142
+ }));
143
+ if (parsedValue.valid) {
144
+ setValue(parsedValue.value);
145
+ }
146
+ };
147
+ return /*#__PURE__*/ _react.createElement("div", {
148
+ "data-test-id": "json-editor"
149
+ }, /*#__PURE__*/ _react.createElement(_JsonEditorToolbar.JsonEditorToolbar, {
150
+ isRedoDisabled: disabled || state.redoStack.length === 0,
151
+ isUndoDisabled: disabled || state.undoStack.length === 0,
152
+ onUndo: onUndo,
153
+ onRedo: onRedo
154
+ }), /*#__PURE__*/ _react.createElement(_JsonEditorField.JsonEditorField, {
155
+ value: state.value,
156
+ onChange: onChange,
157
+ isDisabled: disabled
158
+ }), !state.isValidJson && /*#__PURE__*/ _react.createElement(_JsonInvalidStatus.JsonInvalidStatus, null));
171
159
  }
172
- _define_property(ConnectedJsonEditor, "defaultProps", {
173
- isInitiallyDisabled: true
174
- });
175
160
  function JsonEditor(props) {
176
161
  return /*#__PURE__*/ _react.createElement(_fieldeditorshared.FieldConnector, {
177
162
  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,102 @@ 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 pushUndo = React.useMemo(()=>throttle((value)=>{
19
+ setState((currentState)=>({
20
+ ...currentState,
44
21
  undoStack: [
45
- ...state.undoStack,
22
+ ...currentState.undoStack,
46
23
  value
47
24
  ]
48
25
  }));
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({
26
+ }, 400), []);
27
+ React.useEffect(()=>()=>pushUndo.cancel(), [
28
+ pushUndo
29
+ ]);
30
+ const onChange = (value)=>{
31
+ const parsed = parseJSON(value);
32
+ if (value !== state.lastUndo) {
33
+ pushUndo(state.value);
34
+ }
35
+ setState((currentState)=>({
36
+ ...currentState,
55
37
  value,
56
38
  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
- }
39
+ }));
40
+ if (parsed.valid) {
41
+ setValue(parsed.value);
42
+ }
43
+ };
44
+ const onUndo = ()=>{
45
+ const undoStack = [
46
+ ...state.undoStack
47
+ ];
48
+ if (undoStack.length === 0) {
49
+ return;
50
+ }
51
+ const value = undoStack.pop() || '';
52
+ const parsedValue = parseJSON(value);
53
+ setState((currentState)=>({
54
+ ...currentState,
55
+ value,
56
+ isValidJson: parsedValue.valid,
57
+ undoStack,
58
+ redoStack: [
59
+ ...currentState.redoStack,
60
+ currentState.value
61
+ ],
62
+ lastUndo: value
63
+ }));
64
+ if (parsedValue.valid) {
65
+ setValue(parsedValue.value);
66
+ }
67
+ };
68
+ const onRedo = ()=>{
69
+ const redoStack = [
70
+ ...state.redoStack
71
+ ];
72
+ if (redoStack.length === 0) {
73
+ return;
74
+ }
75
+ const value = redoStack.pop() || '';
76
+ const parsedValue = parseJSON(value);
77
+ setState((currentState)=>({
78
+ ...currentState,
79
+ value,
80
+ isValidJson: parsedValue.valid,
81
+ redoStack,
82
+ undoStack: [
83
+ ...currentState.undoStack,
84
+ currentState.value
85
+ ]
86
+ }));
87
+ if (parsedValue.valid) {
88
+ setValue(parsedValue.value);
89
+ }
90
+ };
91
+ return /*#__PURE__*/ React.createElement("div", {
92
+ "data-test-id": "json-editor"
93
+ }, /*#__PURE__*/ React.createElement(JsonEditorToolbar, {
94
+ isRedoDisabled: disabled || state.redoStack.length === 0,
95
+ isUndoDisabled: disabled || state.undoStack.length === 0,
96
+ onUndo: onUndo,
97
+ onRedo: onRedo
98
+ }), /*#__PURE__*/ React.createElement(JsonEditorField, {
99
+ value: state.value,
100
+ onChange: onChange,
101
+ isDisabled: disabled
102
+ }), !state.isValidJson && /*#__PURE__*/ React.createElement(JsonInvalidStatus, null));
115
103
  }
116
- _define_property(ConnectedJsonEditor, "defaultProps", {
117
- isInitiallyDisabled: true
118
- });
119
104
  export default function JsonEditor(props) {
120
105
  return /*#__PURE__*/ React.createElement(FieldConnector, {
121
106
  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-canary.2+060cdf6a",
3
+ "version": "4.3.2-canary.29+3ee64435",
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-canary.2+060cdf6a",
43
+ "@contentful/field-editor-shared": "^4.5.2-canary.29+3ee64435",
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": "060cdf6a8238b1e17edb97c387686eed72ae7141"
61
+ "gitHead": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
62
62
  }