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

feat: Add copy button to code blocks #441

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

beast-pro
Copy link

No description provided.

@caixiaoxi
Copy link

如果显示行号的代码querySelector会查找到行号code元素 复制出来的只有行号,应该取codeElement.length-1

修改之后如下:

// 添加点击事件以复制代码
  svgContainer.addEventListener("click", async () => {
    // 选择当前 .chromaDiv 内的 code 元素
    const codeElement = chromaDiv.querySelectorAll("code");
    if (codeElement.length === 0) {
      console.error("未找到要复制的代码块");
      return;
    }
    const codeToCopy = codeElement[codeElement.length - 1].innerText;
    try {
      await navigator.clipboard.writeText(codeToCopy);
      handleCopySuccess(svgContainer, copiedSVGContent, svgContent); // 显示复制成功的反馈
    } catch (error) {
      console.error("复制失败: ", error);
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants