Skip to content

Commit 8f37b45

Browse files
committed
Fix copy and download triggers re-activating when document changes
1 parent fabdb1a commit 8f37b45

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/app/components/generator/SourcePanel.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
3535
const download = useRef<HTMLAnchorElement>(null)
3636
const retransform = useRef<Function>(() => {})
3737
const onImport = useRef<() => Promise<void>>(async () => {})
38+
const outputRef = useRef<string>()
3839

3940
const textarea = useRef<HTMLTextAreaElement>(null)
4041
const editor = useRef<Editor>()
@@ -56,6 +57,7 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
5657
}
5758
try {
5859
const output = getSerializedOutput(text)
60+
outputRef.current = output
5961
editor.current.setValue(output)
6062
} catch (e) {
6163
if (e instanceof Error) {
@@ -163,22 +165,22 @@ export function SourcePanel({ docAndNode, doCopy, doDownload, doImport, copySucc
163165
}, [indent, format, sort, highlighting, braceLoaded])
164166

165167
useEffect(() => {
166-
if (doCopy && text !== undefined) {
167-
navigator.clipboard.writeText(getSerializedOutput(text)).then(() => {
168+
if (doCopy && outputRef.current) {
169+
navigator.clipboard.writeText(outputRef.current).then(() => {
168170
copySuccess()
169171
})
170172
}
171-
}, [doCopy, text])
173+
}, [doCopy, textarea])
172174

173175
useEffect(() => {
174-
if (doDownload && docAndNode && text !== undefined && download.current) {
175-
const content = encodeURIComponent(getSerializedOutput(text))
176+
if (doDownload && docAndNode && outputRef.current && download.current) {
177+
const content = encodeURIComponent(outputRef.current)
176178
download.current.setAttribute('href', `data:text/json;charset=utf-8,${content}`)
177179
const fileName = fileUtil.basename(docAndNode.doc.uri)
178180
download.current.setAttribute('download', fileName)
179181
download.current.click()
180182
}
181-
}, [doDownload])
183+
}, [doDownload, outputRef])
182184

183185
useEffect(() => {
184186
if (doImport && editor.current) {

0 commit comments

Comments
 (0)