diff --git a/src/app/components/generator/SourcePanel.tsx b/src/app/components/generator/SourcePanel.tsx index 5a9bf511..667a6c68 100644 --- a/src/app/components/generator/SourcePanel.tsx +++ b/src/app/components/generator/SourcePanel.tsx @@ -35,6 +35,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc const download = useRef(null) const retransform = useRef(() => {}) const onImport = useRef<() => Promise>(async () => {}) + const outputRef = useRef() const textarea = useRef(null) const editor = useRef() @@ -56,6 +57,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc } try { const output = getSerializedOutput(text) + outputRef.current = output editor.current.setValue(output) } catch (e) { if (e instanceof Error) { @@ -163,22 +165,22 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc }, [indent, format, sort, highlighting, braceLoaded]) useEffect(() => { - if (doCopy && text !== undefined) { - navigator.clipboard.writeText(getSerializedOutput(text)).then(() => { + if (doCopy && outputRef.current) { + navigator.clipboard.writeText(outputRef.current).then(() => { copySuccess() }) } - }, [doCopy, text]) + }, [doCopy, textarea]) useEffect(() => { - if (doDownload && docAndNode && text !== undefined && download.current) { - const content = encodeURIComponent(getSerializedOutput(text)) + if (doDownload && docAndNode && outputRef.current && download.current) { + const content = encodeURIComponent(outputRef.current) download.current.setAttribute('href', `data:text/json;charset=utf-8,${content}`) const fileName = fileUtil.basename(docAndNode.doc.uri) download.current.setAttribute('download', fileName) download.current.click() } - }, [doDownload]) + }, [doDownload, outputRef]) useEffect(() => { if (doImport && editor.current) {