-- Reusable Hyperscript behaviours and helper functions for the site. -- Loaded from base.html before the Hyperscript runtime, so every behaviour -- defined here can be applied with _="install Name(...)" on any page. -- See HYPERSCRIPT_CONVENTIONS.md, section 4, before adding to this file. -- Copies text to the clipboard on click and confirms in a status element. -- Parameters are copied to element-scoped variables in init: a default set on -- a parameter there is local to the init block and invisible to the handlers. -- The write API is called directly: Hyperscript's own `clipboard` reads the -- clipboard while resolving the target, which makes the browser ask permission. behavior CopyToClipboard(text, status, doneLabel, resetAfter) init set :text to text default :text to my @data-copy set :status to status default :status to me set :doneLabel to doneLabel default :doneLabel to "Copied" set :resetAfter to resetAfter default :resetAfter to 2s set :idleLabel to the textContent of :status end on click queue none call navigator.clipboard.writeText(:text) put :doneLabel into :status add .is-done to me wait :resetAfter put :idleLabel into :status remove .is-done from me end on exception(error) put "Copy failed" into :status end end -- Scales a fixed-width frame to fit its box by setting the ratio the CSS reads. def ui.fitFrame(box) set frame to the first