Selaa lähdekoodia

fix: prevent double parsing

AykutSarac 2 vuotta sitten
vanhempi
commit
1840496620
2 muutettua tiedostoa jossa 11 lisäystä ja 2 poistoa
  1. 10 1
      src/components/MonacoEditor/index.tsx
  2. 1 1
      src/store/useJson.tsx

+ 10 - 1
src/components/MonacoEditor/index.tsx

@@ -69,6 +69,15 @@ export const MonacoEditor = () => {
     // eslint-disable-next-line react-hooks/exhaustive-deps
   }, [debouncedSetJson, hasError, value]);
 
+  const handleChange = React.useCallback((value?: string) => {
+    try {
+      const parsedJson = JSON.stringify(JSON.parse(value!), null, 2);
+      setValue(parsedJson);
+    } catch (error) {
+      setValue(value);
+    }
+  }, []);
+
   React.useEffect(() => {
     const beforeunload = (e: BeforeUnloadEvent) => {
       if (getHasChanges()) {
@@ -93,7 +102,7 @@ export const MonacoEditor = () => {
         value={json}
         theme={lightmode}
         options={editorOptions}
-        onChange={setValue}
+        onChange={handleChange}
         loading={<Loading message="Loading Editor..." />}
         beforeMount={handleEditorWillMount}
         defaultLanguage="json"

+ 1 - 1
src/store/useJson.tsx

@@ -85,7 +85,7 @@ const useJson = create<JsonStates & JsonActions>()((set, get) => ({
   },
   setJson: json => {
     useGraph.getState().setGraph(json);
-    set({ json: JSON.stringify(JSON.parse(json), null, 2), hasChanges: true });
+    set({ json, hasChanges: true });
   },
   saveJson: async (isNew = true) => {
     try {