From 1e37768b358450a37271ee19fa8bae2bd6cca723 Mon Sep 17 00:00:00 2001 From: Takagi <1103069291@qq.com> Date: Tue, 18 Jun 2024 14:00:52 +0800 Subject: [PATCH] pref: improve code block styling in editor (#6089) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area editor #### What this PR does / why we need it: 优化编辑器代码块样式。 before: after: #### How to test it? 测试复制功能是否正常。 #### Does this PR introduce a user-facing change? ```release-note 优化默认编辑器代码块样式 ``` --- .../code-block/CodeBlockViewRenderer.vue | 63 ++++++++++++++----- .../src/extensions/code-block/code-block.ts | 6 +- ui/packages/editor/src/index.ts | 2 +- ui/packages/editor/src/locales/en.yaml | 5 +- ui/packages/editor/src/locales/zh-CN.yaml | 5 +- ui/packages/editor/src/styles/base.scss | 2 +- ui/packages/editor/src/styles/index.scss | 1 + .../editor/src/styles/node-select.scss | 9 +++ 8 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 ui/packages/editor/src/styles/node-select.scss diff --git a/ui/packages/editor/src/extensions/code-block/CodeBlockViewRenderer.vue b/ui/packages/editor/src/extensions/code-block/CodeBlockViewRenderer.vue index d6ba2639f..e6f675c84 100644 --- a/ui/packages/editor/src/extensions/code-block/CodeBlockViewRenderer.vue +++ b/ui/packages/editor/src/extensions/code-block/CodeBlockViewRenderer.vue @@ -4,6 +4,10 @@ import type { Editor, Node } from "@/tiptap/vue-3"; import { NodeViewContent, NodeViewWrapper } from "@/tiptap/vue-3"; import lowlight from "./lowlight"; import { computed } from "vue"; +import BxBxsCopy from "~icons/bx/bxs-copy"; +import IconCheckboxCircle from "~icons/ri/checkbox-circle-line"; +import { useTimeout } from "@vueuse/core"; +import { i18n } from "@/locales"; const props = defineProps<{ editor: Editor; @@ -28,24 +32,53 @@ const selectedLanguage = computed({ props.updateAttributes({ language: language }); }, }); + +const { ready, start } = useTimeout(2000, { controls: true, immediate: false }); + +const handleCopyCode = () => { + if (!ready.value) return; + const code = props.node.textContent; + navigator.clipboard.writeText(code).then(() => { + start(); + }); +}; - - - - auto - + + + - {{ language }} - - + auto + + {{ language }} + + + + + + + + + diff --git a/ui/packages/editor/src/extensions/code-block/code-block.ts b/ui/packages/editor/src/extensions/code-block/code-block.ts index e69aa69e0..2155a3b8c 100644 --- a/ui/packages/editor/src/extensions/code-block/code-block.ts +++ b/ui/packages/editor/src/extensions/code-block/code-block.ts @@ -205,7 +205,7 @@ export default CodeBlockLowlight.extend< editor, isActive: editor.isActive("codeBlock"), icon: markRaw(MdiCodeBracesBox), - title: i18n.global.t("editor.common.codeblock"), + title: i18n.global.t("editor.common.codeblock.title"), action: () => editor.chain().focus().toggleCodeBlock().run(), }, }; @@ -214,7 +214,7 @@ export default CodeBlockLowlight.extend< return { priority: 80, icon: markRaw(MdiCodeBracesBox), - title: "editor.common.codeblock", + title: "editor.common.codeblock.title", keywords: ["codeblock", "daimakuai"], command: ({ editor, range }: { editor: Editor; range: Range }) => { editor.chain().focus().deleteRange(range).setCodeBlock().run(); @@ -229,7 +229,7 @@ export default CodeBlockLowlight.extend< props: { editor, icon: markRaw(MdiCodeBracesBox), - title: i18n.global.t("editor.common.codeblock"), + title: i18n.global.t("editor.common.codeblock.title"), action: () => { editor.chain().focus().setCodeBlock().run(); }, diff --git a/ui/packages/editor/src/index.ts b/ui/packages/editor/src/index.ts index b711ce8e2..965540617 100644 --- a/ui/packages/editor/src/index.ts +++ b/ui/packages/editor/src/index.ts @@ -4,7 +4,7 @@ import "./styles/index.scss"; import "./styles/tailwind.css"; import "floating-vue/dist/style.css"; import "github-markdown-css/github-markdown-light.css"; -import "highlight.js/styles/github-dark.css"; +import "highlight.js/styles/github.css"; const plugin: Plugin = { install(app: App) { diff --git a/ui/packages/editor/src/locales/en.yaml b/ui/packages/editor/src/locales/en.yaml index 8434939c3..500e62c77 100644 --- a/ui/packages/editor/src/locales/en.yaml +++ b/ui/packages/editor/src/locales/en.yaml @@ -104,7 +104,10 @@ editor: code: Code superscript: Super Script subscript: Sub Script - codeblock: Code block + codeblock: + title: Code block + copy_code: Copy code + copy_code_success: Copy success image: Image heading: title: Text type diff --git a/ui/packages/editor/src/locales/zh-CN.yaml b/ui/packages/editor/src/locales/zh-CN.yaml index 25bf92d44..ef573fba6 100644 --- a/ui/packages/editor/src/locales/zh-CN.yaml +++ b/ui/packages/editor/src/locales/zh-CN.yaml @@ -104,7 +104,10 @@ editor: code: 行内代码 superscript: 上角标 subscript: 下角标 - codeblock: 代码块 + codeblock: + title: 代码块 + copy_code: 复制代码 + copy_code_success: 复制成功 image: 图片 heading: title: 文本类型 diff --git a/ui/packages/editor/src/styles/base.scss b/ui/packages/editor/src/styles/base.scss index 3e671da45..69ad677f7 100644 --- a/ui/packages/editor/src/styles/base.scss +++ b/ui/packages/editor/src/styles/base.scss @@ -37,7 +37,7 @@ } pre { - background: #0d0d0d; + background-color: transparent; padding: 0.75rem 1rem; margin: 0; diff --git a/ui/packages/editor/src/styles/index.scss b/ui/packages/editor/src/styles/index.scss index 0b61ebf52..bbe66c4eb 100644 --- a/ui/packages/editor/src/styles/index.scss +++ b/ui/packages/editor/src/styles/index.scss @@ -4,3 +4,4 @@ @import "./columns.scss"; @import "./search.scss"; @import "./format-brush.scss"; +@import "./node-select.scss"; diff --git a/ui/packages/editor/src/styles/node-select.scss b/ui/packages/editor/src/styles/node-select.scss new file mode 100644 index 000000000..b283320b1 --- /dev/null +++ b/ui/packages/editor/src/styles/node-select.scss @@ -0,0 +1,9 @@ +.halo-rich-text-editor { + $editorNodeCardBorderSelected: rgba(47, 142, 244); + + .has-node-selected { + &.code-node { + border-color: $editorNodeCardBorderSelected; + } + } +}