MVP: 按 07 风格重写前端 + Go 后端落地(长文/短文、编辑器、后台管理)
This commit is contained in:
@@ -0,0 +1,605 @@
|
||||
<script setup>
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { adminApi } from '../api'
|
||||
import { md } from '../utils'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const id = computed(() => (route.params.id ? Number(route.params.id) : 0))
|
||||
const isEdit = computed(() => id.value > 0)
|
||||
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
const savedAt = ref('')
|
||||
const error = ref('')
|
||||
const tab = ref('write') // write | preview
|
||||
const tagInput = ref('')
|
||||
const dirty = ref(false)
|
||||
|
||||
const DRAFT_KEY = 'one.draft.new'
|
||||
|
||||
const form = reactive({
|
||||
kind: 'long',
|
||||
title: '',
|
||||
slug: '',
|
||||
summary: '',
|
||||
content_md: '',
|
||||
tags: [],
|
||||
status: 'draft',
|
||||
published_at: '',
|
||||
reading_minutes: null,
|
||||
override_minutes: false
|
||||
})
|
||||
|
||||
// ---------- 阅读时长 ----------
|
||||
|
||||
function estimateMinutes(text) {
|
||||
if (!text || !text.trim()) return 1
|
||||
const cjk = (text.match(/[㐀-鿿 -〿-]/g) || []).length
|
||||
const latin = (text.match(/[A-Za-z0-9']+/g) || []).length
|
||||
return Math.max(1, Math.floor(cjk / 400) + Math.floor(latin / 220))
|
||||
}
|
||||
|
||||
const autoMinutes = computed(() => estimateMinutes(form.content_md))
|
||||
const finalMinutes = computed(() =>
|
||||
form.override_minutes && form.reading_minutes ? Number(form.reading_minutes) : autoMinutes.value
|
||||
)
|
||||
|
||||
// ---------- 时间 ----------
|
||||
|
||||
function isoToLocal(iso) {
|
||||
if (!iso) return ''
|
||||
const d = new Date(iso)
|
||||
if (Number.isNaN(d.getTime())) return ''
|
||||
const pad = (n) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(
|
||||
d.getMinutes()
|
||||
)}`
|
||||
}
|
||||
|
||||
function localToIso(local) {
|
||||
if (!local) return ''
|
||||
const d = new Date(local)
|
||||
if (Number.isNaN(d.getTime())) return ''
|
||||
return d.toISOString()
|
||||
}
|
||||
|
||||
const publishedLocal = ref('')
|
||||
|
||||
// ---------- 载入 ----------
|
||||
|
||||
onMounted(async () => {
|
||||
if (isEdit.value) {
|
||||
try {
|
||||
const p = await adminApi.post(id.value)
|
||||
form.kind = p.kind || 'long'
|
||||
form.title = p.title || ''
|
||||
form.slug = p.slug || ''
|
||||
form.summary = p.summary || ''
|
||||
form.content_md = p.content_md || ''
|
||||
form.tags = p.tags ? [...p.tags] : []
|
||||
form.status = p.status || 'draft'
|
||||
publishedLocal.value = isoToLocal(p.published_at)
|
||||
if (p.reading_minutes && p.reading_minutes !== estimateMinutes(p.content_md || '')) {
|
||||
form.override_minutes = true
|
||||
form.reading_minutes = p.reading_minutes
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e.message || '载入失败'
|
||||
}
|
||||
} else {
|
||||
const raw = localStorage.getItem(DRAFT_KEY)
|
||||
if (raw) {
|
||||
try {
|
||||
Object.assign(form, JSON.parse(raw))
|
||||
savedAt.value = '本地草稿已恢复'
|
||||
} catch (e) {
|
||||
localStorage.removeItem(DRAFT_KEY)
|
||||
}
|
||||
}
|
||||
publishedLocal.value = isoToLocal(new Date().toISOString())
|
||||
}
|
||||
loading.value = false
|
||||
// 载入完成后的第一次变更才触发自动保存
|
||||
setTimeout(() => {
|
||||
dirty.value = false
|
||||
watchForm()
|
||||
}, 0)
|
||||
})
|
||||
|
||||
// ---------- 自动保存 ----------
|
||||
|
||||
let timer
|
||||
let stopWatch
|
||||
|
||||
function watchForm() {
|
||||
stopWatch = watch(
|
||||
form,
|
||||
() => {
|
||||
dirty.value = true
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(autosave, 1500)
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
watch(publishedLocal, () => {
|
||||
dirty.value = true
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(autosave, 1500)
|
||||
})
|
||||
}
|
||||
|
||||
async function autosave() {
|
||||
if (loading.value) return
|
||||
const payload = buildPayload()
|
||||
if (!payload.content_md.trim() && !payload.title.trim() && !isEdit.value) return
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
if (isEdit.value) {
|
||||
const saved = await adminApi.updatePost(id.value, payload)
|
||||
applySaved(saved)
|
||||
} else {
|
||||
localStorage.setItem(DRAFT_KEY, JSON.stringify({ ...form, tags: [...form.tags] }))
|
||||
}
|
||||
dirty.value = false
|
||||
savedAt.value = new Date().toLocaleTimeString('zh-CN', { hour12: false })
|
||||
} catch (e) {
|
||||
error.value = '自动保存失败:' + (e.message || '')
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function applySaved(saved) {
|
||||
form.slug = saved.slug
|
||||
form.status = saved.status
|
||||
publishedLocal.value = isoToLocal(saved.published_at)
|
||||
}
|
||||
|
||||
function buildPayload() {
|
||||
return {
|
||||
kind: form.kind,
|
||||
title: form.title,
|
||||
slug: form.slug,
|
||||
summary: form.summary,
|
||||
content_md: form.content_md,
|
||||
tags: [...form.tags],
|
||||
status: form.status,
|
||||
published_at: localToIso(publishedLocal.value) || new Date().toISOString(),
|
||||
reading_minutes: form.override_minutes && form.reading_minutes ? Number(form.reading_minutes) : null
|
||||
}
|
||||
}
|
||||
|
||||
async function save(status) {
|
||||
if (status) form.status = status
|
||||
if (!isEdit.value && !form.content_md.trim() && !form.title.trim()) {
|
||||
error.value = '先写点什么再保存'
|
||||
return
|
||||
}
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
clearTimeout(timer)
|
||||
try {
|
||||
const payload = buildPayload()
|
||||
if (isEdit.value) {
|
||||
applySaved(await adminApi.updatePost(id.value, payload))
|
||||
} else {
|
||||
const created = await adminApi.createPost(payload)
|
||||
localStorage.removeItem(DRAFT_KEY)
|
||||
dirty.value = false
|
||||
router.replace(`/admin/${created.id}`)
|
||||
return
|
||||
}
|
||||
dirty.value = false
|
||||
savedAt.value = new Date().toLocaleTimeString('zh-CN', { hour12: false })
|
||||
} catch (e) {
|
||||
error.value = e.message || '保存失败'
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function publishNow() {
|
||||
await save('published')
|
||||
}
|
||||
|
||||
async function unpublish() {
|
||||
await save('draft')
|
||||
}
|
||||
|
||||
// ---------- 标签 ----------
|
||||
|
||||
function addTag() {
|
||||
const v = tagInput.value.trim().replace(/[,,]$/, '')
|
||||
if (!v) return
|
||||
if (!form.tags.includes(v)) form.tags.push(v)
|
||||
tagInput.value = ''
|
||||
}
|
||||
|
||||
function onTagInput(e) {
|
||||
if (e.key === 'Enter' || e.key === ',' || e.key === ',') {
|
||||
e.preventDefault()
|
||||
addTag()
|
||||
}
|
||||
}
|
||||
|
||||
function removeTag(t) {
|
||||
form.tags = form.tags.filter((x) => x !== t)
|
||||
}
|
||||
|
||||
// ---------- 预览 ----------
|
||||
|
||||
const preview = computed(() => md.render(form.content_md || ''))
|
||||
|
||||
function onKeydown(e) {
|
||||
// Tab 缩进,而不是跳出输入框
|
||||
if (e.key === 'Tab') {
|
||||
e.preventDefault()
|
||||
const el = e.target
|
||||
const start = el.selectionStart
|
||||
const end = el.selectionEnd
|
||||
form.content_md =
|
||||
form.content_md.slice(0, start) + ' ' + form.content_md.slice(end)
|
||||
setTimeout(() => el.setSelectionRange(start + 2, start + 2))
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section v-if="loading" class="loading">载入中…</section>
|
||||
|
||||
<section v-else class="editor">
|
||||
<header class="head">
|
||||
<div class="left">
|
||||
<h1 class="title">{{ isEdit ? '编辑文章' : '写新的' }}</h1>
|
||||
<span class="state">
|
||||
<span v-if="saving">保存中…</span>
|
||||
<span v-else-if="dirty">有未保存的改动</span>
|
||||
<span v-else-if="savedAt">已保存 {{ savedAt }}</span>
|
||||
<span v-else>还没改过</span>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="right">
|
||||
<button class="btn" @click="form.status === 'published' ? unpublish() : save()">
|
||||
存草稿
|
||||
</button>
|
||||
<button v-if="form.status !== 'published'" class="btn btn-primary" @click="publishNow">
|
||||
发布
|
||||
</button>
|
||||
<button v-else class="btn" @click="unpublish">转为草稿</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
|
||||
<div class="grid">
|
||||
<!-- 左:正文 -->
|
||||
<div class="main">
|
||||
<div class="kind-switch">
|
||||
<button class="kind" :class="{ on: form.kind === 'long' }" @click="form.kind = 'long'">
|
||||
长文
|
||||
</button>
|
||||
<button class="kind" :class="{ on: form.kind === 'short' }" @click="form.kind = 'short'">
|
||||
短文
|
||||
</button>
|
||||
<span class="hint">
|
||||
{{ form.kind === 'long' ? '有标题、有结构,适合讲完整一件事' : '一两段话,时间线里不显示标题' }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<input
|
||||
v-model="form.title"
|
||||
class="input title-input"
|
||||
:placeholder="form.kind === 'short' ? '标题(可留空,仅作归档索引)' : '标题'"
|
||||
/>
|
||||
|
||||
<div class="tabs">
|
||||
<button class="tab" :class="{ on: tab === 'write' }" @click="tab = 'write'">编写</button>
|
||||
<button class="tab" :class="{ on: tab === 'preview' }" @click="tab = 'preview'">预览</button>
|
||||
<span class="counter">{{ form.content_md.length }} 字</span>
|
||||
</div>
|
||||
|
||||
<textarea
|
||||
v-show="tab === 'write'"
|
||||
v-model="form.content_md"
|
||||
class="textarea md-input"
|
||||
rows="20"
|
||||
:placeholder="form.kind === 'short' ? '写点什么…' : '# 标题\n\n正文,支持 Markdown。'"
|
||||
@keydown="onKeydown"
|
||||
></textarea>
|
||||
|
||||
<div v-show="tab === 'preview'" class="prose preview" v-html="preview"></div>
|
||||
</div>
|
||||
|
||||
<!-- 右:元信息 -->
|
||||
<aside class="side">
|
||||
<div class="field">
|
||||
<label>类型</label>
|
||||
<p class="value">{{ form.kind === 'short' ? '短文' : '长文' }}</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="slug">Slug(链接)</label>
|
||||
<input id="slug" v-model="form.slug" class="input" placeholder="留空自动生成" />
|
||||
<p v-if="form.slug" class="sub">/post/{{ form.slug }}</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="summary">摘要</label>
|
||||
<textarea
|
||||
id="summary"
|
||||
v-model="form.summary"
|
||||
class="textarea"
|
||||
rows="3"
|
||||
placeholder="长文显示在时间线里的一段话"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>标签</label>
|
||||
<div v-if="form.tags.length" class="taglist">
|
||||
<span v-for="t in form.tags" :key="t" class="tag-chip">
|
||||
{{ t }}
|
||||
<button class="x" @click="removeTag(t)">×</button>
|
||||
</span>
|
||||
</div>
|
||||
<input
|
||||
v-model="tagInput"
|
||||
class="input"
|
||||
placeholder="输入后回车添加"
|
||||
@keydown="onTagInput"
|
||||
@blur="addTag"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="pub">发布时间</label>
|
||||
<input id="pub" v-model="publishedLocal" type="datetime-local" class="input" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="mins">阅读时长(分钟)</label>
|
||||
<div class="mins-row">
|
||||
<input
|
||||
id="mins"
|
||||
v-model="form.reading_minutes"
|
||||
type="number"
|
||||
min="1"
|
||||
class="input"
|
||||
:disabled="!form.override_minutes"
|
||||
:placeholder="String(autoMinutes)"
|
||||
/>
|
||||
<label class="check">
|
||||
<input v-model="form.override_minutes" type="checkbox" />
|
||||
手动指定
|
||||
</label>
|
||||
</div>
|
||||
<p class="sub">自动估算:{{ finalMinutes }} 分钟</p>
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label>状态</label>
|
||||
<p class="value">
|
||||
<span class="dot" :class="form.status"></span>
|
||||
{{ form.status === 'published' ? '已发布' : '草稿' }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<button class="btn wide" @click="save()">立即保存</button>
|
||||
<RouterLink to="/admin" class="back">← 返回列表</RouterLink>
|
||||
</aside>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.left {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.state {
|
||||
font-size: 12.5px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.right {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #9a5b45;
|
||||
font-size: 13px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 280px;
|
||||
gap: 22px;
|
||||
align-items: start;
|
||||
}
|
||||
|
||||
@media (max-width: 860px) {
|
||||
.grid {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.kind-switch {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.kind {
|
||||
padding: 5px 14px;
|
||||
border: 1px solid var(--line);
|
||||
background: var(--card);
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.kind.on {
|
||||
background: var(--accent);
|
||||
border-color: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.hint {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.title-input {
|
||||
font-family: var(--serif);
|
||||
font-size: 19px;
|
||||
padding: 10px 12px;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.tab {
|
||||
padding: 7px 14px;
|
||||
border: 1px solid transparent;
|
||||
border-bottom: 0;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
font-size: 13.5px;
|
||||
color: var(--muted);
|
||||
border-radius: 3px 3px 0 0;
|
||||
}
|
||||
|
||||
.tab.on {
|
||||
color: var(--accent);
|
||||
border-color: var(--line);
|
||||
background: var(--card);
|
||||
margin-bottom: -1px;
|
||||
}
|
||||
|
||||
.counter {
|
||||
margin-left: auto;
|
||||
font-size: 12px;
|
||||
color: var(--faint);
|
||||
padding-bottom: 6px;
|
||||
}
|
||||
|
||||
.md-input {
|
||||
min-height: 420px;
|
||||
font-family: var(--mono);
|
||||
font-size: 14px;
|
||||
line-height: 1.8;
|
||||
border-radius: 0 3px 3px 3px;
|
||||
}
|
||||
|
||||
.preview {
|
||||
min-height: 420px;
|
||||
padding: 18px 20px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 0 3px 3px 3px;
|
||||
}
|
||||
|
||||
.side {
|
||||
border: 1px solid var(--line);
|
||||
background: var(--card);
|
||||
border-radius: 4px;
|
||||
padding: 18px;
|
||||
}
|
||||
|
||||
.value {
|
||||
margin: 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.sub {
|
||||
margin: 4px 0 0;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.taglist {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.x {
|
||||
border: 0;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
color: var(--muted);
|
||||
padding: 0 0 0 4px;
|
||||
}
|
||||
|
||||
.mins-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.check {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
white-space: nowrap;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.dot {
|
||||
display: inline-block;
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--faint);
|
||||
margin-right: 6px;
|
||||
vertical-align: 2px;
|
||||
}
|
||||
|
||||
.dot.published {
|
||||
background: var(--accent);
|
||||
}
|
||||
|
||||
.wide {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.back {
|
||||
display: inline-block;
|
||||
margin-top: 14px;
|
||||
font-size: 13px;
|
||||
color: var(--accent);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user