Skip to content

Commit 2c38f70

Browse files
export file dl to utils
1 parent bb79d99 commit 2c38f70

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

editor/components/code-editor/monaco.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MonacoEmptyMock } from "./monaco-mock-empty";
55
import { register } from "./monaco-utils";
66
import { __dangerous__lastFormattedValue__global } from "@code-editor/prettier-services";
77
import { debounce } from "utils/debounce";
8+
import { downloadFile } from "utils/download";
89

910
type ICodeEditor = monaco.editor.IStandaloneCodeEditor;
1011

@@ -57,13 +58,7 @@ export function MonacoEditor(props: MonacoEditorProps) {
5758
contextMenuGroupId: "navigation",
5859
contextMenuOrder: 1.5,
5960
run: function (ed) {
60-
var data = new Blob([ed.getModel().getValue()], { type: "text/txt" });
61-
var csvURL = window.URL.createObjectURL(data);
62-
const tempLink = document.createElement("a");
63-
tempLink.href = csvURL;
64-
tempLink.setAttribute("download", path);
65-
tempLink.click();
66-
tempLink.remove();
61+
downloadFile({ data: ed.getModel().getValue(), filename: path });
6762
},
6863
});
6964

editor/utils/download/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function downloadFile({
2+
data,
3+
filename,
4+
}: {
5+
data: string;
6+
filename: string;
7+
}) {
8+
var blob = new Blob([data], { type: "text/txt" });
9+
var csvURL = window.URL.createObjectURL(blob);
10+
const tempLink = document.createElement("a");
11+
tempLink.href = csvURL;
12+
tempLink.setAttribute("download", filename);
13+
tempLink.click();
14+
tempLink.remove();
15+
}

0 commit comments

Comments
 (0)