jerryc127 / hexo-theme-butterfly

🦋 A Hexo Theme: Butterfly
https://butterfly.js.org
Apache License 2.0
6.82k stars 1.24k forks source link

[Bug]: Codeblock copy function issues #1403

Closed nova1751 closed 7 months ago

nova1751 commented 7 months ago

使用的 Butterfly 版本? | What version of Butterfly are you use?

4.10.0

是否修改过主题文件? || Has the theme files been modified?

不是 (No)

使用的瀏覽器? || What browse are you using?

Chrome

使用的系統? || What operating system are you using?

Windows

問題描述 | Describe the bug

I found that there seems to be a bug in the copy function when the browser doesn't support execCommand,it just changes the element's text content but doesn't make it show up to remind the user.The code in main.js is below: code Maybe a code like this is more reasonable?I would appreciate it if you could answer soon.Thank you!😊

const copy = async (text, ctx) => {
  const showMsg = (msg) => {
    if (GLOBAL_CONFIG.Snackbar !== undefined) {
      btf.snackbarShow(msg);
    } else {
      const prevEle = ctx.previousElementSibling;
      prevEle.textContent = msg;
      prevEle.style.opacity = 1;
      setTimeout(() => {
        prevEle.style.opacity = 0;
      }, 700);
    }
  };
  if (
    document.queryCommandSupported &&
    document.queryCommandSupported("copy")
  ) {
    document.execCommand("copy");
    showMsg(CODE_CONFIG.copy.success);
  } else if (navigator.clipboard) {
    try {
      await navigator.clipboard.writeText(text);
      showMsg(CODE_CONFIG.copy.success);
    } catch {
      showMsg(CODE_CONFIG.copy.error);
    }
  } else {
    showMsg(CODE_CONFIG.copy.noSupport);
  }
};

出現問題網站 | Website

refrain.cf

jerryc127 commented 7 months ago

image image

If the browser doesn't support execCommand, it also doesn't support navigator.clipboard.writeText

nova1751 commented 7 months ago

image image

If the browser doesn't support execCommand, it also doesn't support navigator.clipboard.writeText

Thanks for your relpy,but what I'm focused is the bug in the code below.

if (GLOBAL_CONFIG.Snackbar !== undefined) {
  btf.snackbarShow(GLOBAL_CONFIG.copy.noSupport)
} else {
  ctx.previousElementSibling.textContent = GLOBAL_CONFIG.copy.noSupport
}

It seems to just change the textcontent but not make the message to show up to remind the user.Is this a bug?

jerryc127 commented 7 months ago

image image If the browser doesn't support execCommand, it also doesn't support navigator.clipboard.writeText

Thanks for your relpy,but what I'm focused is the bug in the code below.

if (GLOBAL_CONFIG.Snackbar !== undefined) {
  btf.snackbarShow(GLOBAL_CONFIG.copy.noSupport)
} else {
  ctx.previousElementSibling.textContent = GLOBAL_CONFIG.copy.noSupport
}

It seems to just change the textcontent but not make the message to show up to remind the user.Is this a bug?

if you use Snackbar, it will pop up and show the 'no support' message. if not, it will show the message before the copy button. image

nova1751 commented 7 months ago

Oh,sorry,maybe I should explain it more specifically.The text show-up logic is like below,because the copy-notice element's opacity is set to 0 initially.

const prevEle = ctx.previousElementSibling;
prevEle.textContent = msg;
prevEle.style.opacity = 1;
setTimeout(() => {
  prevEle.style.opacity = 0;
}, 700);

However,there is only one line for browser-not-support case:

if (GLOBAL_CONFIG.Snackbar !== undefined) {
  btf.snackbarShow(GLOBAL_CONFIG.copy.noSupport)
} else {
  ctx.previousElementSibling.textContent = GLOBAL_CONFIG.copy.noSupport
}

As a result,the textcontent is changed but the the copy-notice element's opacity attribute is still 0,which means copy-notice element' won't show up to remind the user.