Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Make download button. Close #31
Browse files Browse the repository at this point in the history
  • Loading branch information
lelinhtinh committed Jul 25, 2020
1 parent eb90784 commit b3e39d1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ <h2>{{ site.description }}</h2>
<button id="copyjs" class="btn" type="button">
<img class="btn-icon" src="{{ 'assets/icon/copy-document.png' | relative_url }}" alt="Copy to clipboard">
</button>
<a id="preview" class="btn" type="button" href="#" target="_blank">
<a id="download" class="btn" type="button" href="#" download="result.js">
<img class="btn-icon" src="{{ 'assets/icon/download.png' | relative_url }}" alt="Download file">
</a>
<a id="preview" class="btn" type="button" href="#">
<img class="btn-icon" src="{{ 'assets/icon/external-link-symbol.png' | relative_url }}" alt="Open in new tab">
</a>
</div>
Expand Down
1 change: 1 addition & 0 deletions assets/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ pre {
}

#preview,
#download,
.tab-content {
display: none;

Expand Down
Binary file added assets/icon/download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 21 additions & 2 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
isAuto = false,

preview = document.getElementById('preview'),
download = document.getElementById('download'),

clipboard = new ClipboardJS('#copyjs', {
target: function () {
Expand Down Expand Up @@ -163,6 +164,19 @@
preview.href = externalUrl;
},

downloadUrl,
downloadResult = function (source) {
if (downloadUrl) URL.revokeObjectURL(downloadUrl);

downloadUrl = new Blob([source], {
type: 'text/javascript'
});
downloadUrl = URL.createObjectURL(downloadUrl);

download.classList.add('show');
download.href = downloadUrl;
},

workerFormat,
workerDecode,

Expand All @@ -172,8 +186,13 @@
if (!workerFormat) {
workerFormat = new Worker('{{ "/assets/js/worker/format.js" | relative_url }}');
workerFormat.addEventListener('message', function (e) {
view.innerHTML = e.data;
externalPreview(e.data);
if (!e.data.highlight) {
downloadResult(e.data.result);
return;
}

view.innerHTML = e.data.result;
externalPreview(e.data.result);

stopEffect();
});
Expand Down
10 changes: 9 additions & 1 deletion assets/js/worker/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ self.addEventListener('message', function (e) {
console.log(err);
}

self.postMessage({
result: source,
highlight: false
});

try {
self.importScripts('{{ "/assets/js/lib/highlight-js/highlight.min.js" | relative_url }}');

Expand All @@ -36,5 +41,8 @@ self.addEventListener('message', function (e) {
console.log(err);
}

self.postMessage(source);
self.postMessage({
result: source,
highlight: true
});
});

0 comments on commit b3e39d1

Please sign in to comment.