update
This commit is contained in:
@@ -1,119 +1,159 @@
|
||||
<script setup lang="ts">
|
||||
const props = defineProps({
|
||||
code: {
|
||||
type: String,
|
||||
default: "",
|
||||
},
|
||||
language: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
filename: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
highlights: {
|
||||
type: Array as () => number[],
|
||||
default: () => [],
|
||||
},
|
||||
meta: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
/**
|
||||
* 代码块组件 Props 定义
|
||||
*/
|
||||
interface Props {
|
||||
code?: string; // 代码内容
|
||||
language?: string; // 编程语言
|
||||
filename?: string; // 文件名
|
||||
highlights?: number[]; // 需要高亮的行号
|
||||
meta?: string; // 元数据
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
code: "",
|
||||
language: undefined,
|
||||
filename: undefined,
|
||||
highlights: () => [],
|
||||
meta: undefined,
|
||||
});
|
||||
|
||||
// 剪贴板复制功能
|
||||
const { copy, copied } = useClipboard({ source: props.code });
|
||||
|
||||
// macOS 窗口控制按钮颜色配置
|
||||
const WINDOW_CONTROLS = [
|
||||
{ color: "#ff5f56", label: "close" },
|
||||
{ color: "#ffbd2e", label: "minimize" },
|
||||
{ color: "#27c93f", label: "maximize" },
|
||||
] as const;
|
||||
|
||||
// 是否显示头部(当有文件名或语言信息时显示)
|
||||
const showHeader = computed(() => props.filename || props.language);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="group relative my-6 overflow-hidden rounded-2xl border border-white/10 bg-[#282a36] shadow-2xl line-numbers">
|
||||
class="relative my-6 overflow-hidden rounded-2xl border border-zinc-200 bg-zinc-50/50 shadow-sm dark:border-white/5 dark:bg-[#282a36] dark:shadow-2xl">
|
||||
<!-- 代码块头部 -->
|
||||
<div
|
||||
v-if="filename || language"
|
||||
class="flex items-center justify-between px-4 py-2 bg-black/20 border-b border-white/5">
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="flex gap-1.5">
|
||||
<div class="w-3 h-3 rounded-full bg-[#ff5f56] shadow-[0_0_8px_rgba(255,95,86,0.2)]"></div>
|
||||
<div
|
||||
class="w-3 h-3 rounded-full bg-[#ffbd2e] shadow-[0_0_8px_rgba(255,189,46,0.2)]"></div>
|
||||
<div class="w-3 h-3 rounded-full bg-[#27c93f] shadow-[0_0_8px_rgba(39,201,63,0.2)]"></div>
|
||||
<header
|
||||
v-if="showHeader"
|
||||
class="flex items-center justify-between border-b border-zinc-200 bg-zinc-100/60 px-4 py-2.5 dark:border-white/5 dark:bg-white/5">
|
||||
<!-- 左侧:窗口控制按钮 + 文件名 -->
|
||||
<div class="flex min-w-0 items-center gap-2">
|
||||
<!-- macOS 风格的窗口控制按钮 -->
|
||||
<div class="flex shrink-0 gap-1.5">
|
||||
<span
|
||||
v-for="control in WINDOW_CONTROLS"
|
||||
:key="control.label"
|
||||
:style="{ backgroundColor: control.color }"
|
||||
class="h-3 w-3 rounded-full shadow-[0_0_8px_rgba(0,0,0,0.2)]"
|
||||
:aria-label="control.label" />
|
||||
</div>
|
||||
<span v-if="filename" class="ml-2 text-xs font-mono text-zinc-400 truncate max-w-[200px]">{{
|
||||
filename
|
||||
}}</span>
|
||||
|
||||
<!-- 文件名显示 -->
|
||||
<span
|
||||
v-if="filename"
|
||||
class="ml-2 max-w-50 truncate font-mono text-xs text-zinc-600 dark:text-zinc-400">
|
||||
{{ filename }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
|
||||
<!-- 右侧:语言标签 + 复制按钮 -->
|
||||
<div class="flex shrink-0 items-center gap-3">
|
||||
<!-- 编程语言标签 -->
|
||||
<span
|
||||
v-if="language"
|
||||
class="text-[10px] font-bold uppercase tracking-widest text-zinc-500"
|
||||
>{{ language }}</span
|
||||
>
|
||||
class="text-[0.625rem] font-bold uppercase tracking-widest text-zinc-500 dark:text-zinc-500">
|
||||
{{ language }}
|
||||
</span>
|
||||
|
||||
<!-- 复制按钮 -->
|
||||
<button
|
||||
class="p-1.5 rounded-md hover:bg-white/10 text-zinc-400 hover:text-white transition-all duration-200 active:scale-95"
|
||||
:class="{ 'text-emerald-400': copied }"
|
||||
aria-label="复制代码"
|
||||
class="rounded-md p-1.5 text-zinc-700 transition-all duration-200 hover:bg-gray-200/40 hover:text-zinc-900 active:scale-95 dark:text-zinc-300 dark:hover:bg-white/10 dark:hover:text-white"
|
||||
:class="{ 'text-emerald-500 dark:text-emerald-400': copied }"
|
||||
:title="copied ? '已复制' : '复制代码'"
|
||||
@click="copy()">
|
||||
<Icon :name="copied ? 'heroicons:check' : 'heroicons:clipboard'" class="w-3.5 h-3.5" />
|
||||
<Icon :name="copied ? 'heroicons:check' : 'heroicons:clipboard'" class="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- 代码内容 -->
|
||||
<div class="relative group/code">
|
||||
<!-- 代码内容区域 -->
|
||||
<div class="relative">
|
||||
<pre
|
||||
class="!m-0 !bg-transparent !p-4 overflow-x-auto custom-scrollbar font-mono text-sm leading-relaxed selection:bg-violet-500/30"><slot /></pre>
|
||||
class="custom-scrollbar m-0! text-zinc-700 dark:text-white! overflow-x-auto bg-transparent! p-4! font-mono text-sm leading-relaxed"><slot /></pre>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
/* 自定义滚动条样式 */
|
||||
.custom-scrollbar::-webkit-scrollbar {
|
||||
height: 4px;
|
||||
}
|
||||
|
||||
.custom-scrollbar::-webkit-scrollbar-track {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.custom-scrollbar::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
background: rgba(0, 0, 0, 0.08);
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.custom-scrollbar:hover::-webkit-scrollbar-thumb {
|
||||
background: rgba(0, 0, 0, 0.15);
|
||||
}
|
||||
|
||||
:global(.dark) .custom-scrollbar::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .custom-scrollbar:hover::-webkit-scrollbar-thumb {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
}
|
||||
|
||||
/* 行号实现 */
|
||||
.line-numbers :deep(code) {
|
||||
counter-reset: step;
|
||||
counter-increment: step 0;
|
||||
/* 行号功能 */
|
||||
:deep(code) {
|
||||
counter-reset: line-number;
|
||||
display: grid;
|
||||
min-width: 100%;
|
||||
}
|
||||
|
||||
.line-numbers :deep(.line) {
|
||||
:deep(.line) {
|
||||
display: inline-flex;
|
||||
min-height: 1.5rem;
|
||||
}
|
||||
|
||||
.line-numbers :deep(.line::before) {
|
||||
content: counter(step);
|
||||
counter-increment: step;
|
||||
:deep(.line::before) {
|
||||
content: counter(line-number);
|
||||
counter-increment: line-number;
|
||||
width: 2rem;
|
||||
margin-right: 1.5rem;
|
||||
display: inline-block;
|
||||
text-align: right;
|
||||
color: rgba(255, 255, 255, 0.15);
|
||||
color: rgba(40, 42, 54, 0.3);
|
||||
user-select: none;
|
||||
font-size: 0.75rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
/* 高亮行样式 */
|
||||
.line-numbers :deep(.line.highlight) {
|
||||
background-color: rgba(139, 92, 246, 0.1);
|
||||
:global(.dark) :deep(.line::before) {
|
||||
color: #6272a4 !important;
|
||||
}
|
||||
|
||||
/* 代码行高亮 */
|
||||
:deep(.line.highlight) {
|
||||
background-color: rgba(var(--site-primary-rgb), 0.12);
|
||||
margin: 0 -1rem;
|
||||
padding: 0 1rem;
|
||||
border-left: 2px solid #bd93f9;
|
||||
border-left: 2px solid var(--site-primary);
|
||||
width: calc(100% + 2rem);
|
||||
}
|
||||
|
||||
:global(.dark) :deep(.line.highlight) {
|
||||
background-color: rgba(var(--site-primary-rgb), 0.08);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user