diff --git a/js/preview-link.js b/js/preview-link.js new file mode 100644 index 0000000000000000000000000000000000000000..b2c0959f8da3631b7f77bdc684089211837732ef --- /dev/null +++ b/js/preview-link.js @@ -0,0 +1,26 @@ +/** + * @file JS file for the preview-link component. + */ + +(function previewLinkScript(Drupal) { + Drupal.behaviors.previewLink = { + attach(context) { + const previewLink = context.querySelector(".preview-link__link a"); + const copyLinkToClipboardButton = context.querySelector( + ".preview-link__copy button" + ); + + copyLinkToClipboardButton.addEventListener("click", function () { + navigator.clipboard.writeText(previewLink.href); + copyLinkToClipboardButton.setAttribute("disabled", true); + copyLinkToClipboardButton.textContent = Drupal.t("Copied!"); + setTimeout(function () { + copyLinkToClipboardButton.textContent = Drupal.t( + "Copy link to clipboard" + ); + copyLinkToClipboardButton.removeAttribute("disabled"); + }, 2000); + }); + }, + }; +})(Drupal); diff --git a/preview_link.libraries.yml b/preview_link.libraries.yml new file mode 100644 index 0000000000000000000000000000000000000000..21cb09c0e9991abaa2d28409561abe2fd4206111 --- /dev/null +++ b/preview_link.libraries.yml @@ -0,0 +1,3 @@ +preview-link: + js: + js/preview-link.js: {} \ No newline at end of file diff --git a/templates/preview-link.html.twig b/templates/preview-link.html.twig index d8660c32b9de9e3a2b779d941560801fc184ab79..941b1108077b678494e3587c87f58900f0633c4a 100644 --- a/templates/preview-link.html.twig +++ b/templates/preview-link.html.twig @@ -1,6 +1,9 @@ +{{ attach_library('preview_link/preview-link') }} +
{{ description }}
{{ 'Link:'|t }} {{ link }}
{{ 'Expiry:'|t }} {{ remaining_lifetime }}
+