From defbf7894afababdf686a3a09148a721b889bf7c Mon Sep 17 00:00:00 2001 From: LexLuthr <88259624+LexLuthr@users.noreply.github.com> Date: Thu, 10 Oct 2024 00:38:49 +0400 Subject: [PATCH] copy block cid (#255) --- web/static/win-stats.mjs | 76 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/web/static/win-stats.mjs b/web/static/win-stats.mjs index ad23acd43..b17b99255 100644 --- a/web/static/win-stats.mjs +++ b/web/static/win-stats.mjs @@ -14,6 +14,71 @@ class WinStats extends LitElement { this.requestUpdate(); } + async copyToClipboard(block) { + if (navigator.clipboard && navigator.clipboard.writeText) { + try { + await navigator.clipboard.writeText(block); + // Show notification + this.showNotification('Block copied to clipboard'); + } catch (err) { + console.error('Failed to copy using Clipboard API', err); + this.fallbackCopyTextToClipboard(block); + } + } else { + // Fallback method + this.fallbackCopyTextToClipboard(block); + } + } + + fallbackCopyTextToClipboard(text) { + const textArea = document.createElement('textarea'); + textArea.value = text; + + // Avoid scrolling to bottom + textArea.style.position = 'fixed'; + textArea.style.top = '0'; + textArea.style.left = '0'; + textArea.style.width = '1px'; + textArea.style.height = '1px'; + textArea.style.opacity = '0'; + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + const successful = document.execCommand('copy'); + if (successful) { + this.showNotification('Block copied to clipboard'); + } else { + this.showNotification('Failed to copy block'); + } + } catch (err) { + console.error('Fallback: Oops, unable to copy', err); + this.showNotification('Failed to copy block'); + } + + document.body.removeChild(textArea); + } + + showNotification(message) { + // Simple notification logic; you might want to customize this + const notification = document.createElement('div'); + notification.textContent = message; + notification.style.position = 'fixed'; + notification.style.bottom = '20px'; + notification.style.right = '20px'; + notification.style.background = 'rgba(0,0,0,0.7)'; + notification.style.color = 'white'; + notification.style.padding = '10px'; + notification.style.borderRadius = '5px'; + notification.style.zIndex = '1000'; + document.body.appendChild(notification); + setTimeout(() => { + document.body.removeChild(notification); + }, 2000); + } + render() { return html` @@ -35,7 +100,14 @@ class WinStats extends LitElement {