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 3 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
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ <h1 class="logo">
<button class="control" type="button" onclick="copyText()"><strong>Copy to Clipboard</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>
<button class="control" type="button" onclick="downloadBeautifiedCode()"><strong>Download Code</strong></button>
<input type="file" onchange="changeToFileContent(this)">

bitwiseman marked this conversation as resolved.
Show resolved Hide resolved
</div>

<div class="config-options">
Expand Down
32 changes: 32 additions & 0 deletions web/common-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ 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);
link.download = "js-beautify." + fileExtension; // Dynamic file name based on extension

// Triggering the download
link.click();

// Cleanup
URL.revokeObjectURL(link.href);
ozearkhan marked this conversation as resolved.
Show resolved Hide resolved
}


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