Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a download button #2312

Merged
merged 7 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ <h1 class="logo">
<div class="buttons-box">
<button class="submit"><strong>Beautify Code</strong> <em>(ctrl&#8209;enter)</em></button>
<button class="control" type="button" onclick="copyText()"><strong>Copy to Clipboard</strong></button>
<button class="control" type="button" onclick="downloadBeautifiedCode()"><strong>Download</strong></button>
<button class="control" type="button" onclick="selectAll()"><strong>Select All</strong></button>
<button class="control" type="button" onclick="clearAll()"><strong>Clear</strong></button>
<input type="file" onchange="changeToFileContent(this)">
Expand Down
34 changes: 34 additions & 0 deletions web/common-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,40 @@ function unpacker_filter(source) {
return leading_comments + source;
}

/* exported downloadBeautifiedCode */
function downloadBeautifiedCode() {
var content = the.editor ? the.editor.getValue() : $('#source').val();

// Getting the selected language to determine the file extension
var language = $('#language').val();
var fileExtension = "txt"; // Default extension

// Setting the file extension based on the selected language
if (language === 'html') {
fileExtension = 'html';
} else if (language === 'css') {
fileExtension = 'css';
} else if (language === 'js') {
fileExtension = 'js';
}

// Creating a Blob object with the content
var blob = new Blob([content], { type: "text/plain;charset=utf-8" });

// Creating a temporary anchor element to trigger the download
var link = document.createElement("a");
link.href = URL.createObjectURL(blob);
try {
link.download = "js-beautify." + fileExtension; // Dynamic file name based on extension
bitwiseman marked this conversation as resolved.
Show resolved Hide resolved

// Triggering the download
link.click();
} finally {
// Cleanup
URL.revokeObjectURL(link.href);
}
}


function beautify() {
if (the.beautify_in_progress) {
Expand Down
2 changes: 2 additions & 0 deletions web/onload.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ $(function() {
$('#additional-options').change(beautify);




});
Loading