commit a77dab3457b53c988a5d93c922a492e7d4c4c80c
parent d246f70fdd113837cb5754be978d8af6a4b07cf8
Author: oscarbenedito <oscar@obenedito.org>
Date:   Sun, 24 Nov 2019 20:04:07 +0100

Added option to save files in composer

Diffstat:
Massets/js/composer.js | 24+++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/assets/js/composer.js b/assets/js/composer.js @@ -43,9 +43,31 @@ var resetTimer = function() { clearTimeout(typingTimer); typingTimer = setTimeout(updateContents, typingInterval); } +function downloadData(filename, text) { + var text = localStorage.getItem('content'); + if (text != null && text != '') { + var tmpElement = document.createElement('a'); + tmpElement.setAttribute('href', 'data:text/markdown;charset=utf-8,' + encodeURIComponent(text)); + tmpElement.setAttribute('download', 'contents.md'); + tmpElement.style.display = 'none'; + + document.body.appendChild(tmpElement); + tmpElement.click(); + document.body.removeChild(tmpElement); + } +} +var saveOrResetTimer = function(event) { + if (event.keyCode == 83 && (event.metaKey || event.ctrlKey)) { + event.preventDefault(); + updateContents(); + downloadData(); + } + clearTimeout(typingTimer); + typingTimer = setTimeout(updateContents, typingInterval); +} $composer.addEventListener('keyup input', resetTimer); -$composer.addEventListener('keydown', resetTimer); +$composer.addEventListener('keydown', saveOrResetTimer); $composer.addEventListener('input', resetTimer); window.addEventListener('beforeunload', updateContents); loadContents();