|
@@ -4,8 +4,10 @@ import CateSelect from '@/components/CateSelect.vue'
|
|
import TagCloud from '@/components/TagCloud.vue'
|
|
import TagCloud from '@/components/TagCloud.vue'
|
|
import type { PostBody, Tag } from '@/models'
|
|
import type { PostBody, Tag } from '@/models'
|
|
import { api } from '@/utils/axios.ts'
|
|
import { api } from '@/utils/axios.ts'
|
|
-import { getQuery } from '@/router'
|
|
|
|
|
|
+import { getQuery, router } from '@/router'
|
|
import { optionOfCate } from '@/utils/cates.ts'
|
|
import { optionOfCate } from '@/utils/cates.ts'
|
|
|
|
+import { useMessagePipeStore } from '@/stores/msg.ts'
|
|
|
|
+import { type DraftBody, useDraftStore } from '@/stores/draft.ts'
|
|
|
|
|
|
const editorConfig = {
|
|
const editorConfig = {
|
|
leftToolbar: 'undo redo | image',
|
|
leftToolbar: 'undo redo | image',
|
|
@@ -63,7 +65,7 @@ onMounted(() => {
|
|
|
|
|
|
const title = ref('')
|
|
const title = ref('')
|
|
|
|
|
|
-const text = ref(`
|
|
|
|
|
|
+const content = ref(`
|
|
# 这是一个标题
|
|
# 这是一个标题
|
|
|
|
|
|
这是一个段落
|
|
这是一个段落
|
|
@@ -86,46 +88,78 @@ const cateOptions = [
|
|
const newTags = ref<string[]>([])
|
|
const newTags = ref<string[]>([])
|
|
|
|
|
|
const validContent = computed(() => {
|
|
const validContent = computed(() => {
|
|
- return text.value.length > 0 && title.value.length > 0 && selectedCate.value.length > 0
|
|
|
|
|
|
+ return content.value.length > 0 && title.value.length > 0 && selectedCate.value.length > 0
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+function createDraft(): DraftBody {
|
|
|
|
+ return {
|
|
|
|
+ content: content.value,
|
|
|
|
+ tags: [...activeTags.value, ...newTags.value],
|
|
|
|
+ title: title.value,
|
|
|
|
+ cate: selectedCate.value,
|
|
|
|
+ timemill: Date.now(),
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
function buildPost(): PostBody | undefined {
|
|
function buildPost(): PostBody | undefined {
|
|
if (!validContent.value) {
|
|
if (!validContent.value) {
|
|
return undefined
|
|
return undefined
|
|
}
|
|
}
|
|
return {
|
|
return {
|
|
- content: text.value,
|
|
|
|
|
|
+ content: content.value,
|
|
tags: [...activeTags.value, ...newTags.value],
|
|
tags: [...activeTags.value, ...newTags.value],
|
|
title: title.value,
|
|
title: title.value,
|
|
cate: selectedCate.value,
|
|
cate: selectedCate.value,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const msg = useMessagePipeStore()
|
|
|
|
+
|
|
function handleSubmit() {
|
|
function handleSubmit() {
|
|
const postBody = buildPost()
|
|
const postBody = buildPost()
|
|
if (postBody) {
|
|
if (postBody) {
|
|
- api.postPush(postBody)
|
|
|
|
|
|
+ api.postPush(postBody).then((res) => {
|
|
|
|
+ if (res.code == 200) {
|
|
|
|
+ msg.success('发布成功~')
|
|
|
|
+ router.push(`/detail?id=${res.data.id}`)
|
|
|
|
+ } else if (res.code == 401) {
|
|
|
|
+ msg.danger('认证失败,请重新登录')
|
|
|
|
+ }
|
|
|
|
+ })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+const targetDraft = ref<string | null>(null)
|
|
|
|
+
|
|
|
|
+const draftStore = useDraftStore()
|
|
|
|
+
|
|
|
|
+function handleSaveDraft() {
|
|
|
|
+ const draft = createDraft()
|
|
|
|
+ targetDraft.value = draftStore.push(draft, targetDraft.value)
|
|
|
|
+ msg.info('草稿已保存')
|
|
|
|
+}
|
|
</script>
|
|
</script>
|
|
|
|
|
|
<template>
|
|
<template>
|
|
<div>
|
|
<div>
|
|
<div class="post-item" style="padding: 12px 30px; overflow: hidden">
|
|
<div class="post-item" style="padding: 12px 30px; overflow: hidden">
|
|
- <div style="display: flex; align-items: end; padding-bottom: 12px">
|
|
|
|
- <cate-select :options="cateOptions" v-model="selectedCate" placeholder="类型" />
|
|
|
|
- <input
|
|
|
|
- style="margin-bottom: 0"
|
|
|
|
- class="p-input"
|
|
|
|
- type="text"
|
|
|
|
- placeholder="博文标题"
|
|
|
|
- v-model="title"
|
|
|
|
- />
|
|
|
|
|
|
+ <div style="display: flex; align-items: center; justify-content: space-between">
|
|
|
|
+ <div style="display: flex; align-items: end; padding-bottom: 12px">
|
|
|
|
+ <CateSelect :options="cateOptions" v-model="selectedCate" placeholder="类型" />
|
|
|
|
+ <input
|
|
|
|
+ style="margin-bottom: 0"
|
|
|
|
+ class="p-input"
|
|
|
|
+ type="text"
|
|
|
|
+ placeholder="博文标题"
|
|
|
|
+ v-model="title"
|
|
|
|
+ />
|
|
|
|
+ </div>
|
|
|
|
+ <div>载入草稿</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="post-item" style="padding: 0; overflow: hidden">
|
|
<div class="post-item" style="padding: 0; overflow: hidden">
|
|
<v-md-editor
|
|
<v-md-editor
|
|
- v-model="text"
|
|
|
|
|
|
+ v-model="content"
|
|
height="400px"
|
|
height="400px"
|
|
:left-toolbar="editorConfig.leftToolbar"
|
|
:left-toolbar="editorConfig.leftToolbar"
|
|
:right-toolbar="editorConfig.rightToolbar"
|
|
:right-toolbar="editorConfig.rightToolbar"
|
|
@@ -144,7 +178,7 @@ function handleSubmit() {
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div class="post-item footer" style="padding: 0 30px">
|
|
<div class="post-item footer" style="padding: 0 30px">
|
|
- <div class="p-button">保存草稿</div>
|
|
|
|
|
|
+ <div class="p-button" @click="handleSaveDraft">保存草稿</div>
|
|
<div
|
|
<div
|
|
:class="validContent ? ['p-button-success'] : ['p-button-disabled']"
|
|
:class="validContent ? ['p-button-success'] : ['p-button-disabled']"
|
|
@click="handleSubmit"
|
|
@click="handleSubmit"
|