mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-11-23 04:40:06 +00:00
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
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:
parent
eddf69db58
commit
1ff8cce65c
@ -97,6 +97,7 @@
|
||||
<div class="buttons-box">
|
||||
<button class="submit"><strong>Beautify Code</strong> <em>(ctrl‑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)">
|
||||
|
@ -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) {
|
||||
|
@ -55,4 +55,6 @@ $(function() {
|
||||
$('#additional-options').change(beautify);
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user