From e71fd1a14ed539b7c8d328e5df13df65ae939189 Mon Sep 17 00:00:00 2001 From: elioat Date: Mon, 15 Jan 2024 21:15:17 -0500 Subject: * --- js/inknswitch/ink.js | 68 +++++++++++++++++++++------------------------------- js/inknswitch/sw.js | 15 ++++++++++-- 2 files changed, 40 insertions(+), 43 deletions(-) (limited to 'js') diff --git a/js/inknswitch/ink.js b/js/inknswitch/ink.js index dbfa0e4..3ac1892 100644 --- a/js/inknswitch/ink.js +++ b/js/inknswitch/ink.js @@ -1,12 +1,12 @@ if ('serviceWorker' in navigator) { navigator.serviceWorker.register('./sw.js') .then(function(registration) { - console.log('Service Worker registered with scope:', registration.scope); + console.log('Service Worker registered with scope:', registration.scope); }) .catch(function(err) { - console.log('Service Worker registration failed:', err); + console.error('Service Worker registration failed:', err); }); - } +} document.getElementById('noteArea').focus(); // ensures that textarea is focused when the page loads @@ -125,6 +125,25 @@ if (savedImage) { img.src = savedImage; } +const saveCanvasAsImage = () => { + const confirmDownload = confirm('Do you really want to save the canvas as an image?'); + if (confirmDownload) { + const dataUrl = canvas.toDataURL(); + const a = document.createElement('a'); + a.href = dataUrl; + a.download = 'canvas.png'; + a.click(); + } +} + +const clearCanvas = () => { + const confirmClear = confirm('Do you really want to clear the canvas?'); + if (confirmClear) { + ctx.clearRect(0, 0, canvas.width, canvas.height); + localStorage.removeItem('canvasImage'); + } +} + // Toggle between drawing and note mode const toggle = () => { if (canvas.style.pointerEvents === 'none') { @@ -155,37 +174,11 @@ toggleButton.addEventListener('click', toggle); const helpText = "This is a modal note taking tool. With it, you can type text that'll be saved to the browser, and you can draw pictures that'll also be saved to the browser. You are in typing mode right now. To toggle between modes tap either cmd + d, ctrl + d or cmd + 1, or ctrl + 1. When in drawing mode, tap c to clear the canvas, tap s to save the contents of the canvas to a png." -// Currently unused -// const saveTextAsFile() => { -// const textToWrite = document.getElementById('noteArea').value; -// const textFileAsBlob = new Blob([textToWrite], { type: 'text/plain' }); -// const fileNameToSaveAs = 'notes.txt'; - -// const downloadLink = document.createElement('a'); -// downloadLink.download = fileNameToSaveAs; -// downloadLink.href = window.URL.createObjectURL(textFileAsBlob); -// downloadLink.style.display = 'none'; -// document.body.appendChild(downloadLink); -// downloadLink.click(); -// document.body.removeChild(downloadLink); -// } - const handleSelectChange = () => { if (selectElement.value === 'clear') { - const confirmClear = confirm('Do you really want to clear the canvas?'); - if (confirmClear) { - ctx.clearRect(0, 0, canvas.width, canvas.height); - localStorage.removeItem('canvasImage'); - } + clearCanvas(); } else if (selectElement.value === 'save') { - const confirmDownload = confirm('Do you really want to save the canvas as an image?'); - if (confirmDownload) { - const dataUrl = canvas.toDataURL(); - const a = document.createElement('a'); - a.href = dataUrl; - a.download = 'canvas.png'; - a.click(); - } + saveCanvasAsImage(); } else if (selectElement.value === 'help') { alert(helpText); } @@ -206,23 +199,16 @@ const handleKeyDown = (e) => { // C key to clear the canvas if (e.key === 'c' && canvas.style.pointerEvents === 'auto') { - const confirmClear = confirm('Do you really want to clear the canvas?'); - if (confirmClear) { - ctx.clearRect(0, 0, canvas.width, canvas.height); - localStorage.removeItem('canvasImage'); - } + clearCanvas(); } // S key to save the canvas as an image if (e.key === 's' && canvas.style.pointerEvents === 'auto') { - const dataUrl = canvas.toDataURL(); - const a = document.createElement('a'); - a.href = dataUrl; - a.download = 'canvas.png'; - a.click(); + saveCanvasAsImage(); } localStorage.setItem('noteContent', noteArea.value); }; document.addEventListener('keydown', handleKeyDown); + diff --git a/js/inknswitch/sw.js b/js/inknswitch/sw.js index 81e6bf4..4a63871 100644 --- a/js/inknswitch/sw.js +++ b/js/inknswitch/sw.js @@ -4,12 +4,23 @@ self.addEventListener('install', function(event) { return cache.addAll([ './index.html', './ink.js', - './icon.png', './android-icon-36x36.png', './android-icon-48x48.png', './android-icon-72x72.png', './android-icon-96x96.png', - './android-icon-144x144.png' + './android-icon-144x144.png', + './android-icon-192x192.png', + './apple-icon-57x57.png', + './apple-icon-60x60.png', + './apple-icon-72x72.png', + './apple-icon-76x76.png', + './apple-icon-114x114.png', + './apple-icon-120x120.png', + './apple-icon-144x144.png', + './apple-icon-152x152.png', + './apple-icon-180x180.png', + './apple-icon-precomposed.png', + './apple-icon.png', ]); }) ); -- cgit 1.4.1-2-gfad0