-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
189 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.