From 4edddfbcedcb8e29a2dee3337120aa22863bb369 Mon Sep 17 00:00:00 2001 From: zoodogood Date: Mon, 4 Nov 2024 14:49:46 +0200 Subject: [PATCH] Docs (playground): provide copy button for large text fragments --- .../.vitepress/components/Playground.vue | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/civet.dev/.vitepress/components/Playground.vue b/civet.dev/.vitepress/components/Playground.vue index 6d15b41f..012144b2 100644 --- a/civet.dev/.vitepress/components/Playground.vue +++ b/civet.dev/.vitepress/components/Playground.vue @@ -16,12 +16,14 @@ const props = defineProps<{ showComptime?: boolean; comptime?: boolean; }>(); +const showCopy = ref(false); const userCode = ref(b64.decode(props.b64Code)); const compileError = ref(''); const inputHtml = ref(''); const outputHtml = ref(''); const inputHtmlEl = ref(); +const outputHtmlEl = ref(); const textareaEl = ref(); // Compile on input @@ -97,6 +99,42 @@ function fixTextareaSize() { } } +async function copyOutputToClipboard(pointerEvent) { + let successed = false; + try { + const { textContent } = outputHtmlEl.value!; + await navigator.clipboard.writeText(textContent as string); + successed = true; + } catch (err) { + console.error(err); + } + // feedback + const labelTextEl = pointerEvent.target.parentNode.querySelector("[data-label-text]"); + if (!labelTextEl) return; + + const currentElementLabel = labelTextEl.textContent; + const SUCCESS = "Copied!"; + const FAIL = "Failed to copy"; + const elementLabelIsNotPrimary = [SUCCESS, FAIL].includes(currentElementLabel); + if (elementLabelIsNotPrimary){ + return; + } + const providedMessage = successed ? SUCCESS : FAIL; + labelTextEl.textContent = providedMessage; + setTimeout(() => { + labelTextEl.textContent = currentElementLabel /* primary label */; + }, 2_000 /* DEFAULT */); +} + +const showCopy_condition = async () => { + await nextTick(); + const THRESHOLD = 300 /* symbols */; + showCopy.value = outputHtmlEl.value?.textContent?.length! > THRESHOLD; +}; +watch(outputHtml, showCopy_condition) +watch(outputHtmlEl, showCopy_condition) + + const playgroundUrl = computed(() => { return `/playground?code=${b64.encode(userCode.value)}`; }); @@ -138,7 +176,7 @@ const playgroundUrl = computed(() => {
-
+
@@ -147,6 +185,10 @@ const playgroundUrl = computed(() => { comptime +