From 5d5e9f47741a939e1ad4eb773c2e1a11876425a1 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Fri, 8 Mar 2024 23:17:59 -0800 Subject: [PATCH] Add buttons to easily download and copy table --- index.html | 75 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index f364fdd..0b04c03 100644 --- a/index.html +++ b/index.html @@ -125,6 +125,15 @@ overflow: auto; } + .button-container { + visibility: hidden; + opacity: 0; + display: flex; + justify-content: center; + gap: 16px; + transition: opacity 300ms; + } + table { visibility: hidden; opacity: 0; @@ -171,7 +180,7 @@
-
+
+ + +
+

No events found. Try another code block.

@@ -309,10 +335,13 @@ const noEventsMessage = document.querySelector('.no-events'); const table = document.querySelector('table'); const tableBody = document.querySelector('table tbody'); + const buttonContainer = document.querySelector('.button-container'); textarea.addEventListener('input', debounce(() => { noEventsMessage.style.setProperty('display', 'none'); table.style.setProperty('visibility', 'hidden'); table.style.setProperty('opacity', 0); + buttonContainer.style.setProperty('visibility', 'hidden'); + buttonContainer.style.setProperty('opacity', 0); tableBody.innerHTML = ''; const events = getEvents(textarea.value); @@ -328,26 +357,54 @@ } table.style.setProperty('visibility', 'visible'); table.style.setProperty('opacity', 1); + buttonContainer.style.setProperty('visibility', 'visible'); + buttonContainer.style.setProperty('opacity', 1); }, 250)); - // Set up "Copy permalink to clipboard" button + // Set up buttons const checkSVG = ''; const permalinkSVG = ''; - document.querySelector('button').addEventListener('click', async e => { - const b64 = await textToGzipBase64(textarea.value); - const url = `${location.origin}#${b64}`; - navigator.clipboard.writeText(url); + const downloadSVG = ''; + const clipboardSVG = ''; - const button = e.target; + function debounceButton(button, icon, message) { button.setAttribute('aria-label', 'Copied!'); const svg = button.querySelector('svg'); svg.style.setProperty('stroke', 'green'); svg.innerHTML = checkSVG; setTimeout(() => { - svg.innerHTML = permalinkSVG; + svg.innerHTML = icon; svg.style.setProperty('stroke', 'currentColor'); - button.setAttribute('aria-label', 'Copy permalink to clipboard'); + button.setAttribute('aria-label', message); }, 2000); + } + + document.querySelector('#copy-permalink').addEventListener('click', async e => { + const b64 = await textToGzipBase64(textarea.value); + const url = `${location.origin}#${b64}`; + navigator.clipboard.writeText(url); + debounceButton(e.target, permalinkSVG, 'Copy permalink to clipboard'); + }); + + document.querySelector('#download-table').addEventListener('click', async e => { + const table = document.querySelector('table'); + const csv = '"' + table.innerText.replaceAll('"', '""').replaceAll('\t', '","').replaceAll('\n', '"\n"') + '"'; + const file = new File([csv], `Event_Hashes_${Date.now()}`, { type: 'text/csv' }); + const a = document.createElement('a'); + a.href = URL.createObjectURL(file); + a.download = file.name; + a.click(); + debounceButton(e.target, downloadSVG, 'Download table to CSV'); + }); + + document.querySelector('#copy-table').addEventListener('click', async e => { + const table = document.querySelector('table'); + const data = [new ClipboardItem({ + 'text/plain': new Blob([table.innerText], { type: 'text/plain' }), + 'text/html': new Blob([table.outerHTML], { type: 'text/html' }) + })]; + navigator.clipboard.write(data); + debounceButton(e.target, clipboardSVG, 'Copy table to clipboard'); }); if (location.hash !== '') { -- 2.47.3