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

This commit is contained in:
Samuel Maier 2022-02-19 23:19:47 +01:00
parent aefb7d56db
commit d7c14f1c60

View file

@ -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);