diff --git a/web/src/components/Memo.tsx b/web/src/components/Memo.tsx index 2817c396..bd2468d3 100644 --- a/web/src/components/Memo.tsx +++ b/web/src/components/Memo.tsx @@ -143,6 +143,9 @@ const Memo: React.FC = (props: Props) => { if (imgUrl) { showPreviewImageDialog([imgUrl], 0); } + } else if (targetEl.tagName === "BUTTON" && targetEl.className === "codeblock-copy-btn") { + copy(targetEl.parentElement?.children[1].textContent ?? ""); + toastHelper.success(t("message.succeed-copy-code")); } }; diff --git a/web/src/labs/marked/marked.test.ts b/web/src/labs/marked/marked.test.ts index 16d1c1de..8ef893da 100644 --- a/web/src/labs/marked/marked.test.ts +++ b/web/src/labs/marked/marked.test.ts @@ -26,7 +26,7 @@ This is some text after the horizontal rule.`, markdown: `\`\`\` hello world! \`\`\``, - want: `
hello world!
+        want: `
hello world!
 
`, }, { @@ -35,7 +35,7 @@ hello world! \`\`\`js console.log("hello world!") \`\`\``, - want: `

test code block


console.log("hello world!")
+        want: `

test code block


console.log("hello world!")
 
`, }, ]; diff --git a/web/src/labs/marked/parser/CodeBlock.ts b/web/src/labs/marked/parser/CodeBlock.ts index 3275bbec..cb55617d 100644 --- a/web/src/labs/marked/parser/CodeBlock.ts +++ b/web/src/labs/marked/parser/CodeBlock.ts @@ -26,7 +26,7 @@ const renderer = (rawStr: string): string => { // do nth } - return `
${highlightedCode}
`; + return `
${highlightedCode}
`; }; export default { diff --git a/web/src/less/memo-content.less b/web/src/less/memo-content.less index c689d2b4..db44e58a 100644 --- a/web/src/less/memo-content.less +++ b/web/src/less/memo-content.less @@ -64,11 +64,21 @@ } pre { - @apply w-full my-1 p-3 rounded bg-gray-100 dark:bg-zinc-600 whitespace-pre-wrap; + @apply w-full my-1 p-3 rounded bg-gray-100 dark:bg-zinc-600 whitespace-pre-wrap relative; code { @apply block; } + + &:hover{ + .codeblock-copy-btn{ + @apply flex; + } + } + + .codeblock-copy-btn{ + @apply btn-normal absolute hidden top-2 right-2 border-solid border-2; + } } code { diff --git a/web/src/locales/en.json b/web/src/locales/en.json index 29f3415e..1b7fbd7e 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -219,6 +219,7 @@ "private-only": "This memo is private only.", "copied": "Copied", "succeed-copy-content": "Succeed to copy content to clipboard.", + "succeed-copy-code": "Succeed to copy code to clipboard.", "succeed-copy-link": "Succeed to copy link to clipboard.", "change-resource-filename": "Change resource filename", "resource-filename-updated": "Resource filename changed.", diff --git a/web/src/locales/zh.json b/web/src/locales/zh.json index 541b7cae..546dcb4a 100644 --- a/web/src/locales/zh.json +++ b/web/src/locales/zh.json @@ -219,6 +219,7 @@ "private-only": "此 Memo 仅自己可见", "copied": "已复制", "succeed-copy-content": "复制内容到剪贴板成功。", + "succeed-copy-code": "复制代码到剪贴板成功。", "succeed-copy-link": "复制链接到剪贴板成功。", "change-resource-filename": "更改资源文件名", "resource-filename-updated": "资源文件名更改成功。",