add copy code button
This commit is contained in:
@@ -112,6 +112,11 @@ export function babycodeEditorCharCount(evOrPayload, sender, el) {
|
||||
}
|
||||
}
|
||||
|
||||
export function clearThreadDraft(_, __, ___) {
|
||||
const threadId = getThreadId();
|
||||
localStorage.removeItem(`thread-${threadId}`);
|
||||
}
|
||||
|
||||
export function babycodeEditorCharCountInit(_, __, el) {
|
||||
if (el === undefined) { // no editors on page
|
||||
return;
|
||||
@@ -119,7 +124,7 @@ export function babycodeEditorCharCountInit(_, __, el) {
|
||||
|
||||
const threadId = getThreadId();
|
||||
if (threadId !== -1) {
|
||||
el.value = localStorage.getItem(`thread-${threadId}`);
|
||||
el.value = localStorage.getItem(`thread-${threadId}`) || '';
|
||||
}
|
||||
b.send({ sender: el }, 'babycodeEditorCharCount');
|
||||
}
|
||||
@@ -192,3 +197,21 @@ export function babycodeEditorQuote(ev, sender, el) {
|
||||
b.send({ sender: el }, 'babycodeEditorCharCount');
|
||||
el.focus();
|
||||
}
|
||||
|
||||
export async function copyCode(ev, sender, el) {
|
||||
if (!el.contains(sender)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const originalText = sender.textContent;
|
||||
const doneText = 'Copied!';
|
||||
const code = el.dataset.code;
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(code);
|
||||
sender.textContent = doneText;
|
||||
setTimeout(() => { sender.textContent = originalText }, 2000);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user