1
This commit is contained in:
@@ -49,6 +49,12 @@
|
||||
<span class="nav-text">关于页面</span>
|
||||
</router-link>
|
||||
</li>
|
||||
<li>
|
||||
<router-link to="/admin/snippets" class="nav-link" active-class="active">
|
||||
<span class="nav-icon">💻</span>
|
||||
<span class="nav-text">代码片段</span>
|
||||
</router-link>
|
||||
</li>
|
||||
<li>
|
||||
<router-link to="/admin/settings" class="nav-link" active-class="active">
|
||||
<span class="nav-icon">⚙️</span>
|
||||
|
||||
@@ -2,19 +2,19 @@
|
||||
<section id="blog-detail" class="page-section block animate-slide-down">
|
||||
<div class="max-w-4xl mx-auto pt-32 px-6 pb-20">
|
||||
<!-- 返回按钮 -->
|
||||
<button
|
||||
@click="$router.push('/blog')"
|
||||
class="group flex items-center gap-2 text-art-muted hover:text-white transition-colors mb-12"
|
||||
<button
|
||||
@click="$router.push('/blog')"
|
||||
class="group flex items-center gap-2 text-art-muted hover:text-white transition-colors mb-12"
|
||||
>
|
||||
<i data-lucide="arrow-left" class="w-4 h-4 transform group-hover:-translate-x-1 transition-transform"></i>
|
||||
<span class="text-sm font-medium tracking-wide">返回文章列表</span>
|
||||
</button>
|
||||
|
||||
|
||||
<!-- Loading state -->
|
||||
<div v-if="loading" class="flex justify-center items-center h-64">
|
||||
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-art-accent"></div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Error state -->
|
||||
<div v-else-if="error" class="text-center text-red-500 py-20">
|
||||
<p class="mb-4">{{ error }}</p>
|
||||
@@ -25,7 +25,7 @@
|
||||
返回文章列表
|
||||
</button>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Blog detail content -->
|
||||
<div v-else class="space-y-12">
|
||||
<!-- 文章信息 -->
|
||||
@@ -37,10 +37,10 @@
|
||||
<h1 class="font-serif text-4xl md:text-5xl lg:text-6xl text-white leading-tight mb-8">{{ post.title }}</h1>
|
||||
<div class="flex items-center gap-4">
|
||||
<div class="w-10 h-10 rounded-full overflow-hidden border border-white/20">
|
||||
<img
|
||||
src="https://api.dicebear.com/7.x/avataaars/svg?seed=NianGao"
|
||||
alt="Avatar"
|
||||
class="w-full h-full object-cover"
|
||||
<img
|
||||
src="https://api.dicebear.com/7.x/avataaars/svg?seed=NianGao"
|
||||
alt="Avatar"
|
||||
class="w-full h-full object-cover"
|
||||
>
|
||||
</div>
|
||||
<div>
|
||||
@@ -49,10 +49,11 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 文章内容 -->
|
||||
<article class="blog-content prose prose-invert prose-lg max-w-none text-art-muted leading-relaxed">
|
||||
<div v-html="post.content"></div>
|
||||
|
||||
<!-- 文章内容 - 使用 v-html 渲染 Markdown 解析后的 HTML -->
|
||||
<!-- ref="articleRef" 用于后续绑定复制事件 -->
|
||||
<article ref="articleRef" class="blog-content prose prose-invert prose-lg max-w-none text-art-muted leading-relaxed">
|
||||
<div v-html="renderedContent"></div>
|
||||
</article>
|
||||
</div>
|
||||
</div>
|
||||
@@ -60,14 +61,21 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onBeforeMount } from 'vue'
|
||||
import { ref, onMounted, computed, nextTick, onBeforeUnmount } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { fetchPost, Post } from '../services/api'
|
||||
import { useScrollAnimation } from '../composables/useScrollAnimation'
|
||||
|
||||
// 引入 Markdown 解析依赖
|
||||
import MarkdownIt from 'markdown-it'
|
||||
import hljs from 'highlight.js'
|
||||
// 引入代码高亮样式 (Atom One Dark)
|
||||
import 'highlight.js/styles/atom-one-dark.css'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const postId = route.params.id as string
|
||||
const articleRef = ref<HTMLElement | null>(null)
|
||||
|
||||
const post = ref<Post>({
|
||||
id: postId,
|
||||
@@ -83,6 +91,124 @@ const error = ref('')
|
||||
|
||||
const { initObserver } = useScrollAnimation()
|
||||
|
||||
// 初始化 MarkdownIt 实例
|
||||
const md = new MarkdownIt({
|
||||
html: true, // 允许 HTML 标签
|
||||
linkify: true, // 自动转换 URL 为链接
|
||||
typographer: true, // 优化排版
|
||||
})
|
||||
|
||||
// 自定义 fence 渲染规则以实现 iOS 风格代码块 + 行号 + 复制功能
|
||||
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
|
||||
const token = tokens[idx]
|
||||
const info = token.info ? md.utils.unescapeAll(token.info).trim() : ''
|
||||
let langName = ''
|
||||
let highlighted
|
||||
|
||||
if (info) {
|
||||
langName = info.split(/\s+/g)[0]
|
||||
}
|
||||
|
||||
// 1. 代码高亮处理
|
||||
if (langName && hljs.getLanguage(langName)) {
|
||||
try {
|
||||
highlighted = hljs.highlight(token.content, { language: langName, ignoreIllegals: true }).value
|
||||
} catch (__) {
|
||||
highlighted = md.utils.escapeHtml(token.content)
|
||||
}
|
||||
} else {
|
||||
highlighted = md.utils.escapeHtml(token.content)
|
||||
}
|
||||
|
||||
// 2. 生成行号
|
||||
// 移除末尾多余的换行符以计算准确行数
|
||||
const lines = token.content.trimEnd().split('\n')
|
||||
const lineNumbers = lines.map((_, i) => i + 1).join('\n')
|
||||
|
||||
// 3. 构建 iOS 风格 HTML 结构
|
||||
// - copy-btn: 复制按钮,通过 JS 绑定事件
|
||||
// - code-content-wrapper: Flex 容器,包含行号和代码
|
||||
// - line-numbers-wrapper: 行号区域,右对齐,不可选中
|
||||
// - code-block-wrapper: 代码区域
|
||||
return `
|
||||
<div class="ios-code-container my-10 rounded-xl overflow-hidden shadow-2xl bg-[#1e1e1e] border border-white/5 relative group/code">
|
||||
<!-- Window Header -->
|
||||
<div class="mac-window-header flex items-center justify-between px-4 py-3 bg-[#282828] border-b border-white/5">
|
||||
<div class="flex items-center gap-4">
|
||||
<div class="flex gap-2">
|
||||
<div class="w-3 h-3 rounded-full bg-[#ff5f56]"></div>
|
||||
<div class="w-3 h-3 rounded-full bg-[#ffbd2e]"></div>
|
||||
<div class="w-3 h-3 rounded-full bg-[#27c93f]"></div>
|
||||
</div>
|
||||
<div class="text-xs text-white/30 font-mono select-none uppercase tracking-wider">${langName || 'TEXT'}</div>
|
||||
</div>
|
||||
|
||||
<!-- Copy Button -->
|
||||
<button class="copy-btn flex items-center gap-1.5 px-2 py-1 rounded hover:bg-white/10 transition-all cursor-pointer opacity-0 group-hover/code:opacity-100" aria-label="Copy code">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-white/40"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>
|
||||
<span class="text-xs text-white/40 font-medium">复制</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Code Body -->
|
||||
<div class="code-content-wrapper flex">
|
||||
<!-- Line Numbers -->
|
||||
<div class="line-numbers-wrapper select-none text-right border-r border-white/5 bg-[#1e1e1e] py-4 px-3 min-w-[3rem] text-white/20">
|
||||
<pre class="font-mono text-sm leading-relaxed" style="margin:0;">${lineNumbers}</pre>
|
||||
</div>
|
||||
|
||||
<!-- Code Content -->
|
||||
<div class="code-block-wrapper flex-1 overflow-x-auto py-4 px-4 bg-[#1e1e1e]">
|
||||
<pre class="hljs !bg-transparent !m-0 !p-0 !rounded-none"><code class="!font-mono text-sm leading-relaxed block">${highlighted}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`
|
||||
}
|
||||
|
||||
// 计算属性:将 Markdown 内容转换为 HTML
|
||||
const renderedContent = computed(() => {
|
||||
if (!post.value.content) return ''
|
||||
return md.render(post.value.content)
|
||||
})
|
||||
|
||||
// 处理复制功能的逻辑
|
||||
const addCopyListeners = () => {
|
||||
if (!articleRef.value) return
|
||||
|
||||
const buttons = articleRef.value.querySelectorAll('.copy-btn')
|
||||
|
||||
buttons.forEach(btn => {
|
||||
// 移除旧的监听器(防止重复绑定,尽管这里每次都是新渲染的DOM)
|
||||
const newBtn = btn.cloneNode(true) as HTMLElement
|
||||
btn.parentNode?.replaceChild(newBtn, btn)
|
||||
|
||||
newBtn.addEventListener('click', async () => {
|
||||
// 找到对应的代码块容器
|
||||
const container = newBtn.closest('.ios-code-container')
|
||||
const codeElement = container?.querySelector('code')
|
||||
|
||||
if (codeElement) {
|
||||
try {
|
||||
// 获取纯文本内容
|
||||
const text = codeElement.innerText
|
||||
await navigator.clipboard.writeText(text)
|
||||
|
||||
// 更新按钮状态反馈
|
||||
const originalContent = newBtn.innerHTML
|
||||
newBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-green-400"><polyline points="20 6 9 17 4 12"/></svg><span class="text-green-400 text-xs font-medium">已复制</span>`
|
||||
|
||||
setTimeout(() => {
|
||||
newBtn.innerHTML = originalContent
|
||||
}, 2000)
|
||||
} catch (err) {
|
||||
console.error('Copy failed:', err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const fetchBlogDetail = async () => {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
@@ -90,8 +216,16 @@ const fetchBlogDetail = async () => {
|
||||
const postData = await fetchPost(postId)
|
||||
if (postData) {
|
||||
post.value = postData
|
||||
// 初始化动画 - 在数据加载完成后
|
||||
initObserver()
|
||||
// 初始化动画和事件监听 - 在数据加载完成后
|
||||
// 使用 nextTick 确保 v-html 已经渲染到 DOM
|
||||
nextTick(() => {
|
||||
setTimeout(() => {
|
||||
initObserver()
|
||||
addCopyListeners() // 绑定复制按钮事件
|
||||
// 如果使用了 lucide 图标,重新扫描
|
||||
if (window.lucide) window.lucide.createIcons()
|
||||
}, 100)
|
||||
})
|
||||
} else {
|
||||
error.value = '未找到该文章'
|
||||
}
|
||||
@@ -105,9 +239,109 @@ const fetchBlogDetail = async () => {
|
||||
|
||||
onMounted(() => {
|
||||
fetchBlogDetail()
|
||||
// 初始化Lucide图标
|
||||
if (window.lucide) {
|
||||
window.lucide.createIcons()
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
// 组件卸载时清理(虽然这里主要是DOM事件,组件销毁会自动清理DOM,但为了好习惯)
|
||||
onBeforeUnmount(() => {
|
||||
// 可以在这里移除特定的全局监听器,如果是局部绑定的DOM事件通常不需要手动移除
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* Tailwind Typography (:prose) 覆盖 */
|
||||
|
||||
/* 移除 prose 默认的 pre 样式,因为我们自定义了容器 */
|
||||
:deep(.prose pre) {
|
||||
background-color: transparent !important;
|
||||
margin: 0 !important;
|
||||
padding: 0 !important;
|
||||
border-radius: 0 !important;
|
||||
box-shadow: none !important;
|
||||
border: none !important;
|
||||
}
|
||||
|
||||
/* 代码字体栈 - 优先使用 SF Mono 等 Mac 字体 */
|
||||
:deep(.prose code) {
|
||||
font-family: 'SF Mono', Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
||||
}
|
||||
|
||||
/* 行内代码样式优化 (非代码块中的代码) */
|
||||
:deep(.prose :not(pre) > code) {
|
||||
color: #d4b383;
|
||||
background-color: rgba(212, 179, 131, 0.1);
|
||||
padding: 0.2em 0.4em;
|
||||
border-radius: 0.3em;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* 标题样式 */
|
||||
:deep(.prose h1), :deep(.prose h2), :deep(.prose h3) {
|
||||
color: #fff;
|
||||
font-family: serif;
|
||||
margin-top: 2em;
|
||||
margin-bottom: 0.8em;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
:deep(.prose h2) {
|
||||
font-size: 1.8rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
:deep(.prose h3) {
|
||||
font-size: 1.4rem;
|
||||
color: rgba(255, 255, 255, 0.9);
|
||||
}
|
||||
|
||||
/* 链接样式 */
|
||||
:deep(.prose a) {
|
||||
color: #d4b383;
|
||||
text-decoration: none;
|
||||
border-bottom: 1px dashed rgba(212, 179, 131, 0.5);
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
:deep(.prose a:hover) {
|
||||
color: #fff;
|
||||
border-bottom-style: solid;
|
||||
}
|
||||
|
||||
/* 引用块样式 */
|
||||
:deep(.prose blockquote) {
|
||||
border-left: 3px solid #d4b383;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
padding: 1rem 1.5rem;
|
||||
border-radius: 0 0.5rem 0.5rem 0;
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
:deep(.prose ul li::marker) {
|
||||
color: #d4b383;
|
||||
}
|
||||
|
||||
/* 自定义滚动条 - 针对代码块区域 */
|
||||
:deep(.code-block-wrapper::-webkit-scrollbar) {
|
||||
height: 8px; /* 横向滚动条高度 */
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
:deep(.code-block-wrapper::-webkit-scrollbar-thumb) {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
:deep(.code-block-wrapper::-webkit-scrollbar-thumb:hover) {
|
||||
background-color: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
:deep(.code-block-wrapper::-webkit-scrollbar-corner) {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
/* 强制覆盖 highlight.js 可能的背景色 */
|
||||
:deep(.hljs) {
|
||||
background: transparent !important;
|
||||
}
|
||||
</style>
|
||||
@@ -1,95 +1,97 @@
|
||||
<template>
|
||||
<div class="post-form-container">
|
||||
<h1 class="page-title">{{ isEditing ? '编辑文章' : '新建文章' }}</h1>
|
||||
|
||||
|
||||
<div class="form-container">
|
||||
<form @submit.prevent="handleSubmit" class="post-form">
|
||||
<!-- Title Field -->
|
||||
<div class="form-group">
|
||||
<label for="title">文章标题</label>
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
v-model="form.title"
|
||||
placeholder="请输入文章标题"
|
||||
required
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
v-model="form.title"
|
||||
placeholder="请输入文章标题"
|
||||
required
|
||||
/>
|
||||
<div class="error-message" v-if="errors.title">
|
||||
{{ errors.title }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Category Field -->
|
||||
<div class="form-group">
|
||||
<label for="category">分类</label>
|
||||
<input
|
||||
type="text"
|
||||
id="category"
|
||||
v-model="form.category"
|
||||
placeholder="请输入文章分类"
|
||||
required
|
||||
<input
|
||||
type="text"
|
||||
id="category"
|
||||
v-model="form.category"
|
||||
placeholder="请输入文章分类"
|
||||
required
|
||||
/>
|
||||
<div class="error-message" v-if="errors.category">
|
||||
{{ errors.category }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Date Field -->
|
||||
<div class="form-group">
|
||||
<label for="date">发布日期</label>
|
||||
<input
|
||||
type="date"
|
||||
id="date"
|
||||
v-model="form.date"
|
||||
required
|
||||
<input
|
||||
type="date"
|
||||
id="date"
|
||||
v-model="form.date"
|
||||
required
|
||||
/>
|
||||
<div class="error-message" v-if="errors.date">
|
||||
{{ errors.date }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Excerpt Field -->
|
||||
<div class="form-group">
|
||||
<label for="excerpt">文章摘要</label>
|
||||
<textarea
|
||||
id="excerpt"
|
||||
v-model="form.excerpt"
|
||||
placeholder="请输入文章摘要"
|
||||
rows="3"
|
||||
<textarea
|
||||
id="excerpt"
|
||||
v-model="form.excerpt"
|
||||
placeholder="请输入文章摘要"
|
||||
rows="3"
|
||||
></textarea>
|
||||
<div class="error-message" v-if="errors.excerpt">
|
||||
{{ errors.excerpt }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Content Field -->
|
||||
|
||||
<!-- Content Field - 使用 Markdown 编辑器 -->
|
||||
<div class="form-group">
|
||||
<label for="content">文章内容</label>
|
||||
<textarea
|
||||
id="content"
|
||||
v-model="form.content"
|
||||
placeholder="请输入文章内容"
|
||||
rows="10"
|
||||
required
|
||||
></textarea>
|
||||
<label>文章内容</label>
|
||||
<div class="editor-wrapper">
|
||||
<MdEditor
|
||||
v-model="form.content"
|
||||
theme="dark"
|
||||
:preview="false"
|
||||
placeholder="请输入 Markdown 内容..."
|
||||
class="custom-md-editor"
|
||||
/>
|
||||
</div>
|
||||
<div class="error-message" v-if="errors.content">
|
||||
{{ errors.content }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Is Published Field -->
|
||||
<div class="form-group">
|
||||
<label for="isPublished">发布状态</label>
|
||||
<CustomSelect
|
||||
v-model="form.isPublished"
|
||||
:options="publishStatusOptions"
|
||||
placeholder="请选择发布状态"
|
||||
v-model="form.isPublished"
|
||||
:options="publishStatusOptions"
|
||||
placeholder="请选择发布状态"
|
||||
/>
|
||||
<div class="error-message" v-if="errors.isPublished">
|
||||
{{ errors.isPublished }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Submit Buttons -->
|
||||
<div class="form-actions">
|
||||
<button type="button" class="cancel-btn" @click="handleCancel">
|
||||
@@ -107,6 +109,10 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted, computed } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
// 引入 md-editor-v3
|
||||
import { MdEditor } from 'md-editor-v3'
|
||||
import 'md-editor-v3/lib/style.css'
|
||||
|
||||
import { useToast } from '../../composables/useToast'
|
||||
import { createPost, updatePost, fetchPost } from '../../services/api'
|
||||
import CustomSelect from '../../components/CustomSelect.vue'
|
||||
@@ -140,33 +146,33 @@ const publishStatusOptions = [
|
||||
const validateForm = (): boolean => {
|
||||
// Reset errors
|
||||
Object.keys(errors).forEach(key => delete errors[key])
|
||||
|
||||
|
||||
let isValid = true
|
||||
|
||||
|
||||
// Validate title
|
||||
if (!form.title.trim()) {
|
||||
errors.title = '文章标题不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
// Validate category
|
||||
if (!form.category.trim()) {
|
||||
errors.category = '文章分类不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
// Validate date
|
||||
if (!form.date) {
|
||||
errors.date = '发布日期不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
// Validate content
|
||||
if (!form.content.trim()) {
|
||||
errors.content = '文章内容不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
return isValid
|
||||
}
|
||||
|
||||
@@ -175,9 +181,9 @@ const handleSubmit = async () => {
|
||||
if (!validateForm()) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
isSubmitting.value = true
|
||||
|
||||
|
||||
try {
|
||||
if (isEditing.value) {
|
||||
// Update existing post
|
||||
@@ -188,7 +194,7 @@ const handleSubmit = async () => {
|
||||
await createPost(form)
|
||||
toast.success('文章创建成功')
|
||||
}
|
||||
|
||||
|
||||
// Redirect to posts list
|
||||
router.push('/admin/posts')
|
||||
} catch (error: any) {
|
||||
@@ -211,7 +217,7 @@ onMounted(async () => {
|
||||
try {
|
||||
const postId = route.params.id as string
|
||||
const post = await fetchPost(postId)
|
||||
|
||||
|
||||
// Populate form with post data
|
||||
form.title = post.title
|
||||
form.category = post.category
|
||||
@@ -350,19 +356,54 @@ onMounted(async () => {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* MdEditor Customization */
|
||||
.editor-wrapper {
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 0.5rem;
|
||||
overflow: hidden;
|
||||
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
||||
}
|
||||
|
||||
.editor-wrapper:focus-within {
|
||||
border-color: #d4b383;
|
||||
box-shadow: 0 0 0 3px rgba(212, 179, 131, 0.2);
|
||||
}
|
||||
|
||||
:deep(.custom-md-editor) {
|
||||
background-color: rgba(255, 255, 255, 0.05) !important;
|
||||
--md-bk-color: transparent !important;
|
||||
--md-color: white !important;
|
||||
--md-border-color: transparent !important;
|
||||
height: 500px;
|
||||
}
|
||||
|
||||
:deep(.md-editor-toolbar-wrapper) {
|
||||
background-color: rgba(0, 0, 0, 0.2) !important;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
|
||||
}
|
||||
|
||||
:deep(.md-editor-content) {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
:deep(.md-editor-footer) {
|
||||
background-color: rgba(0, 0, 0, 0.2) !important;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
|
||||
}
|
||||
|
||||
/* Responsive Design */
|
||||
@media (max-width: 768px) {
|
||||
.form-container {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
|
||||
.form-actions {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
|
||||
.cancel-btn,
|
||||
.submit-btn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
@@ -1,68 +1,70 @@
|
||||
<template>
|
||||
<div class="snippet-form-container">
|
||||
<h1 class="page-title">{{ isEditing ? '编辑代码片段' : '新建代码片段' }}</h1>
|
||||
|
||||
|
||||
<div class="form-container">
|
||||
<form @submit.prevent="handleSubmit" class="snippet-form">
|
||||
<!-- Title Field -->
|
||||
<div class="form-group">
|
||||
<label for="title">标题</label>
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
v-model="form.title"
|
||||
placeholder="请输入代码片段标题"
|
||||
required
|
||||
<input
|
||||
type="text"
|
||||
id="title"
|
||||
v-model="form.title"
|
||||
placeholder="请输入代码片段标题"
|
||||
required
|
||||
/>
|
||||
<div class="error-message" v-if="errors.title">
|
||||
{{ errors.title }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Type Field -->
|
||||
<div class="form-group">
|
||||
<label for="type">类型</label>
|
||||
<input
|
||||
type="text"
|
||||
id="type"
|
||||
v-model="form.type"
|
||||
placeholder="请输入代码类型(如:js、css、html等)"
|
||||
required
|
||||
<input
|
||||
type="text"
|
||||
id="type"
|
||||
v-model="form.type"
|
||||
placeholder="请输入代码类型(如:js、css、html等)"
|
||||
required
|
||||
/>
|
||||
<div class="error-message" v-if="errors.type">
|
||||
{{ errors.type }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Code Field -->
|
||||
|
||||
<!-- Code Field - Updated to MdEditor for better editing experience -->
|
||||
<div class="form-group">
|
||||
<label for="code">代码内容</label>
|
||||
<textarea
|
||||
id="code"
|
||||
v-model="form.code"
|
||||
placeholder="请输入代码内容"
|
||||
rows="10"
|
||||
required
|
||||
></textarea>
|
||||
<label>代码内容</label>
|
||||
<div class="editor-wrapper">
|
||||
<MdEditor
|
||||
v-model="form.code"
|
||||
theme="dark"
|
||||
:preview="false"
|
||||
placeholder="请输入代码..."
|
||||
class="custom-md-editor"
|
||||
/>
|
||||
</div>
|
||||
<div class="error-message" v-if="errors.code">
|
||||
{{ errors.code }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Description Field -->
|
||||
<div class="form-group">
|
||||
<label for="description">描述</label>
|
||||
<textarea
|
||||
id="description"
|
||||
v-model="form.description"
|
||||
placeholder="请输入代码片段描述"
|
||||
rows="3"
|
||||
<textarea
|
||||
id="description"
|
||||
v-model="form.description"
|
||||
placeholder="请输入代码片段描述"
|
||||
rows="3"
|
||||
></textarea>
|
||||
<div class="error-message" v-if="errors.description">
|
||||
{{ errors.description }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!-- Submit Buttons -->
|
||||
<div class="form-actions">
|
||||
<button type="button" class="cancel-btn" @click="handleCancel">
|
||||
@@ -80,6 +82,10 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted, computed } from 'vue'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
// Import MdEditor
|
||||
import { MdEditor } from 'md-editor-v3'
|
||||
import 'md-editor-v3/lib/style.css'
|
||||
|
||||
import { useToast } from '../../composables/useToast'
|
||||
import { createSnippet, updateSnippet, fetchSnippet } from '../../services/api'
|
||||
|
||||
@@ -104,27 +110,27 @@ const form = reactive({
|
||||
const validateForm = (): boolean => {
|
||||
// Reset errors
|
||||
Object.keys(errors).forEach(key => delete errors[key])
|
||||
|
||||
|
||||
let isValid = true
|
||||
|
||||
|
||||
// Validate title
|
||||
if (!form.title.trim()) {
|
||||
errors.title = '代码片段标题不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
// Validate type
|
||||
if (!form.type.trim()) {
|
||||
errors.type = '代码类型不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
// Validate code
|
||||
if (!form.code.trim()) {
|
||||
errors.code = '代码内容不能为空'
|
||||
isValid = false
|
||||
}
|
||||
|
||||
|
||||
return isValid
|
||||
}
|
||||
|
||||
@@ -133,9 +139,9 @@ const handleSubmit = async () => {
|
||||
if (!validateForm()) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
isSubmitting.value = true
|
||||
|
||||
|
||||
try {
|
||||
if (isEditing.value) {
|
||||
// Update existing snippet
|
||||
@@ -146,7 +152,7 @@ const handleSubmit = async () => {
|
||||
await createSnippet(form)
|
||||
toast.success('代码片段创建成功')
|
||||
}
|
||||
|
||||
|
||||
// Redirect to snippets list
|
||||
router.push('/admin/snippets')
|
||||
} catch (error: any) {
|
||||
@@ -304,19 +310,54 @@ onMounted(async () => {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* MdEditor Customization (Consistent with PostForm) */
|
||||
.editor-wrapper {
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 0.5rem;
|
||||
overflow: hidden;
|
||||
transition: border-color 0.3s ease, box-shadow 0.3s ease;
|
||||
}
|
||||
|
||||
.editor-wrapper:focus-within {
|
||||
border-color: #d4b383;
|
||||
box-shadow: 0 0 0 3px rgba(212, 179, 131, 0.2);
|
||||
}
|
||||
|
||||
:deep(.custom-md-editor) {
|
||||
background-color: rgba(255, 255, 255, 0.05) !important;
|
||||
--md-bk-color: transparent !important;
|
||||
--md-color: white !important;
|
||||
--md-border-color: transparent !important;
|
||||
height: 500px;
|
||||
}
|
||||
|
||||
:deep(.md-editor-toolbar-wrapper) {
|
||||
background-color: rgba(0, 0, 0, 0.2) !important;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
|
||||
}
|
||||
|
||||
:deep(.md-editor-content) {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
:deep(.md-editor-footer) {
|
||||
background-color: rgba(0, 0, 0, 0.2) !important;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
|
||||
}
|
||||
|
||||
/* Responsive Design */
|
||||
@media (max-width: 768px) {
|
||||
.form-container {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
|
||||
.form-actions {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
|
||||
.cancel-btn,
|
||||
.submit-btn {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Reference in New Issue
Block a user