From b231aa7fd2eaf8c11bfc052fd5fea70699d2bb54 Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 6 Sep 2023 21:13:05 -0400 Subject: copy link --- index.html | 10 +++++++--- pugl.js | 34 +++++++++++++++++++++++++++++++--- style.css | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 5e3fc53..16a7c50 100644 --- a/index.html +++ b/index.html @@ -7,8 +7,8 @@ @@ -33,6 +33,10 @@ + @@ -131,8 +135,8 @@

delete creation ""?


- +
diff --git a/pugl.js b/pugl.js index 76796db..ac0c72e 100644 --- a/pugl.js +++ b/pugl.js @@ -2349,6 +2349,8 @@ function import_widgets(string) { function export_widgets_to_local_storage() { const widget_str = export_widgets(); code_input.value = widget_str; + // re-read metadata so that having multiple tabs with pugl open isn't an issue + creation_metadata = JSON.parse(localStorage.getItem(`${APP_ID}-metadata`)); localStorage.setItem(`${APP_ID}-${creation_id}-description`, widget_str); creation_metadata[creation_id] = { lastViewed: Date.now(), @@ -2371,9 +2373,9 @@ function load_creation(id) { } } -function new_creation() { +function new_creation(string) { creation_id = generate_creation_id(); - const result = import_widgets(null); + const result = import_widgets(string); if (result.error) { show_error(result.error); } @@ -2576,6 +2578,23 @@ function startup() { new_creation(); }); + document.getElementById('link-creation').addEventListener('click', () => { + // copy link + const string = code_input.value; + const url = new URL(location.href); + url.search = ''; + url.hash = ''; + url.searchParams.set('import', string); + navigator.clipboard.writeText(url.toString()).then(() => { + // display "Copied!" notice + const copied = document.getElementById('copied-notice'); + copied.style.visibility = 'visible'; + copied.style.animationName = ''; + copied.offsetWidth; // force reflow to restart animation + copied.style.animationName = 'copied-notice-animation'; + }); + }); + document.getElementById('resolution-form').addEventListener('submit', () => { render_width = resolution_x_element.value; render_height = resolution_y_element.value; @@ -2729,7 +2748,16 @@ void main() { }); creation_metadata = parse_json(localStorage.getItem(`${APP_ID}-metadata`)); - load_most_recent_or_create_new(); + const url = new URL(location.href); + const import_string = url.searchParams.get('import'); + if (import_string) { + // import from URL parameter + new_creation(import_string); + url.searchParams.delete('import'); + history.replaceState(null, '', url.toString()); + } else { + load_most_recent_or_create_new(); + } frame(0.0); diff --git a/style.css b/style.css index 12f5986..78debd7 100644 --- a/style.css +++ b/style.css @@ -1,6 +1,7 @@ :root { --color-text: #ddd; --color-link: #8fc; + --color-bg: #000; } body { @@ -10,7 +11,7 @@ body { body, dialog { - background-color: black; + background-color: var(--color-bg); color: var(--color-text); } @@ -223,14 +224,42 @@ summary { #top-area { vertical-align: middle; font-weight: bold; - font-size: 0.5cm; + font-size: 1.2em; } #title-icon { - height: 1cm; + height: 1.5em; } #creation-buttons { margin-top: 0.2em; } +#link-creation { + position: relative; +} + +@keyframes copied-notice-animation { + from { + opacity: 1; + } + to { + opacity: 0; + visibility: hidden; + } +} + +#link-creation #copied-notice { + position: absolute; + top: 50%; + left: 50%; + width: 7em; + color: #0f0; + border: 1px solid #0f0; + pointer-events: none; + padding: 3px; + background: var(--color-bg); + visibility: hidden; + animation-duration: 3s; + animation-fill-mode: forwards; +} #resolution-form input[type='number'] { width: 4em; } -- cgit v1.2.3