Added a download button (#2312)
Some checks failed
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (16, ubuntu, 3.8) (push) Failing after 0s
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (18, ubuntu, 3.10) (push) Failing after 0s
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (20, ubuntu, 3.11) (push) Failing after 0s
CodeQL / Analyze (javascript) (push) Failing after 0s
CodeQL / Analyze (python) (push) Failing after 0s
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (16, macos, 3.8) (push) Has been cancelled
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (16, windows, 3.8) (push) Has been cancelled
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (18, macos, 3.10) (push) Has been cancelled
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (18, windows, 3.10) (push) Has been cancelled
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (20, macos, 3.11) (push) Has been cancelled
CI / Build ${{ matrix.os }} (Node ${{ matrix.node-version }}, Python ${{ matrix.python-version }} ) (20, windows, 3.11) (push) Has been cancelled

* Added a Download button next to the Copy to Clipboard button in the UI

* fixed download button and passed all test cases

* Update js/bin/js-beautify.js

* Update index.html

* Update index.html

* Update web/common-function.js

Co-authored-by: Liam Newman <bitwiseman@gmail.com>

* Update web/common-function.js

---------

Co-authored-by: Liam Newman <bitwiseman@gmail.com>
This commit is contained in:
ozearkhan 2024-09-15 13:48:32 +05:30 committed by GitHub
parent eddf69db58
commit 1ff8cce65c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 0 deletions

View File

@ -97,6 +97,7 @@
<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)">

View File

@ -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 = "beautified." + fileExtension; // Dynamic file name based on extension
// Triggering the download
link.click();
} finally {
// Cleanup
URL.revokeObjectURL(link.href);
}
}
function beautify() {
if (the.beautify_in_progress) {

View File

@ -55,4 +55,6 @@ $(function() {
$('#additional-options').change(beautify);
});