Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playground copy button for large text fragments #1569

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions civet.dev/.vitepress/components/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>('');
const inputHtml = ref('');
const outputHtml = ref('');
const inputHtmlEl = ref<HTMLDivElement>();
const outputHtmlEl = ref<HTMLDivElement>();
const textareaEl = ref<HTMLTextAreaElement>();

// Compile on input
Expand Down Expand Up @@ -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)}`;
});
Expand Down Expand Up @@ -138,7 +176,7 @@ const playgroundUrl = computed(() => {
</div>

<div class="col" :class="{ 'col--error': compileError }">
<div class="code code--output">
<div class="code code--output" ref="outputHtmlEl">
<div v-if="outputHtml" v-html="outputHtml" />
<slot v-else name="output" />
</div>
Expand All @@ -147,6 +185,10 @@ const playgroundUrl = computed(() => {
<input type="checkbox" v-model="comptime"/>
comptime
</label>
<label v-if = "showCopy">
<input type="button" @click="copyOutputToClipboard" />
<span data-label-text>Copy</span>
</label>
<label>
<input type="checkbox" v-model="ligatures"/>
Ligatures
Expand Down Expand Up @@ -271,5 +313,12 @@ const playgroundUrl = computed(() => {
overflow: visible;
}

input { vertical-align: middle; }
input {
vertical-align: middle;
}

label:has(> input),
label>input {
cursor: pointer;
}
</style>
Loading