Skip to content

Commit

Permalink
fixed monaco editor cache issue
Browse files Browse the repository at this point in the history
  • Loading branch information
abelpz committed Nov 9, 2022
1 parent 9eb7092 commit 9540fb3
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 183 deletions.
218 changes: 112 additions & 106 deletions src/components/EditorWrapper.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,129 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from "react";
import Editor, { DiffEditor } from "@monaco-editor/react";

const cleanEditorContent = {
title: "index",
value: "",
inputType: "json"
}
title: "index",
value: "",
inputType: "json",
};

const defaultOptions = { readOnly: true, domReadOnly: true };

function EditorWrapper({ results }) {
const [editorContent, setEditorContent] = useState(cleanEditorContent);
const [editorOptions, setEditorOptions] = useState(defaultOptions);
const [editorContent, setEditorContent] = useState(cleanEditorContent);
const [editorOptions, setEditorOptions] = useState(defaultOptions);
const [cacheKey, setCacheKey] = useState(1);

const editorRef = useRef(null);
const editorRef = useRef(null);

useEffect(() => {
console.log("updated editor");
editorRef.current?.focus();
}, [editorContent?.id]);
useEffect(() => {
editorRef.current?.focus();
}, [editorContent?.id]);

const firstResult = results[0]?.id
const firstResult = results[0]?.id;

useEffect(() => {
if (!editorContent.id && firstResult) setEditorContent(results[results.length - 1])
if (!results.length && editorContent.id) setEditorContent(cleanEditorContent);
}, [firstResult, editorContent, results])
console.log({editorContent})
useEffect(() => {
if (!editorContent.id && firstResult)
setEditorContent(results[results.length - 1]);
if (!results.length && editorContent.id) {
setEditorContent(cleanEditorContent);
setCacheKey((prev) => prev + 1);
}
}, [firstResult, editorContent, results]);

return (
return (
<>
{firstResult && (
<>
{firstResult && (
<>
<span className="add-step-button tooltip">
<span className="tooltiptext ltooltiptext">
Save display to File
</span>
<button
className="result-button"
onClick={() => {
const a = document.createElement("a");
a.download = `${editorContent.title
.toLowerCase()
.replace(" ", "_")}.${typeof editorContent.value === "string" ? "txt" : "json"
}`;
const blob = new Blob(
[
typeof editorContent.value === "string"
? editorContent.value
: JSON.stringify(editorContent.value, null, 2),
],
{ type: "application/json" }
);
a.href = URL.createObjectURL(blob);
a.addEventListener("click", (e) => {
setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000);
});
a.click();
}}
>
{"R>"}
</button>
</span>
<span className="add-step-button tooltip">
<span className="tooltiptext ltooltiptext">Toggle word wrap</span>
<button
className="result-button"
onClick={() =>
setEditorOptions((options) => ({
...options,
wordWrap: !options?.wordWrap,
}))
}
>
{"W"}
</button>
</span>
</>
)}
{results.map((r, n) => {
return (
<button
className="editor-tab"
disabled={editorContent.title === r.title}
onClick={() => setEditorContent(r)}
key={n}
>
{r.title}
</button>
<span className="add-step-button tooltip">
<span className="tooltiptext ltooltiptext">
Save display to File
</span>
<button
className="result-button"
onClick={() => {
const a = document.createElement("a");
a.download = `${editorContent.title
.toLowerCase()
.replace(" ", "_")}.${
typeof editorContent.value === "string" ? "txt" : "json"
}`;
const blob = new Blob(
[
typeof editorContent.value === "string"
? editorContent.value
: JSON.stringify(editorContent.value, null, 2),
],
{ type: "application/json" }
);
})}
{editorContent.value?.type === "diff" ? (
<DiffEditor
height="80vh"
theme="vs-dark"
path={editorContent.title}
original={editorContent.value.original}
modified={editorContent.value.modified}
language={editorContent.inputType}
options={editorOptions}
onMount={(editor) => (editorRef.current = editor)}
/>
) : (
<Editor
height="80vh"
theme="vs-dark"
path={editorContent.title}
defaultLanguage={editorContent.inputType}
defaultValue={
typeof editorContent.value === "object"
? JSON.stringify(editorContent.value, null, 2)
: editorContent.value
}
options={editorOptions}
onMount={(editor) => (editorRef.current = editor)}
/>
)}
a.href = URL.createObjectURL(blob);
a.addEventListener("click", (e) => {
setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000);
});
a.click();
}}
>
{"R>"}
</button>
</span>
<span className="add-step-button tooltip">
<span className="tooltiptext ltooltiptext">Toggle word wrap</span>
<button
className="result-button"
onClick={() =>
setEditorOptions((options) => ({
...options,
wordWrap: !options?.wordWrap,
}))
}
>
{"W"}
</button>
</span>
</>
);
)}
{results.map((r, n) => {
return (
<button
className="editor-tab"
disabled={editorContent.title === r.title}
onClick={() => setEditorContent(r)}
key={n}
>
{r.title}
</button>
);
})}
{editorContent.value?.type === "diff" ? (
<DiffEditor
height="80vh"
theme="vs-dark"
path={editorContent.title}
original={editorContent.value.original}
modified={editorContent.value.modified}
language={editorContent.inputType}
options={editorOptions}
onMount={(editor) => (editorRef.current = editor)}
/>
) : (
<Editor
height="80vh"
theme="vs-dark"
path={`${editorContent.title}--${cacheKey}`}
defaultLanguage={editorContent.inputType}
defaultValue={
typeof editorContent.value === "object"
? JSON.stringify(editorContent.value, null, 2)
: editorContent.value
}
options={editorOptions}
onMount={(editor) => {
editorRef.current = editor;
}}
/>
)}
</>
);
}

export default EditorWrapper
export default EditorWrapper;
Loading

0 comments on commit 9540fb3

Please sign in to comment.