浏览代码

Create downloadText wrapper for downloadUri and use it, use string interpolation for filename

Samuel Maier 3 年之前
父节点
当前提交
d7c14f1c60
共有 1 个文件被更改,包括 11 次插入7 次删除
  1. 11 7
      src/App.tsx

+ 11 - 7
src/App.tsx

@@ -90,6 +90,13 @@ function downloadUri(uri: string, filename: string) {
   downloadAnchor.click();
 }
 
+function downloadText(text: string, fileName: string) {
+  const file = new File([text], fileName);
+  const url = URL.createObjectURL(file);
+  downloadUri(url, fileName);
+  URL.revokeObjectURL(url)
+}
+
 async function getFileUploadWithDialog() {
   const uploadInput = document.createElement("input");
   uploadInput.type = "file";
@@ -222,13 +229,10 @@ function App() {
   function handleDownloadFile() {
     const model = editor?.getModel();
     if (!model || !editor) return;
-    const text = model.getValue();
-    const languageExtension = getFileExtension(language);
-    const fileName = "unknown." + languageExtension;
-    const file = new File([text], fileName);
-    const url = URL.createObjectURL(file);
-    downloadUri(url, fileName);
-    URL.revokeObjectURL(url);
+    downloadText(
+      model.getValue(),
+      `rustpad.${getFileExtension(language)}`,
+    )
   }
 
   useKeyboardCtrlIntercept("s", handleDownloadFile);