Complete post creation feature

pull/9/head
johnniang 2019-04-12 12:12:31 +08:00
parent e7ee3b6d22
commit 9c18f52889
4 changed files with 110 additions and 17 deletions

View File

@ -41,13 +41,16 @@ function concreteTree(parentCategory, categories) {
} }
parentCategory.children.push({ parentCategory.children.push({
key: category.id, key: category.id,
title: category.name title: category.name,
isLeaf: false
}) })
} }
}) })
if (parentCategory.children) { if (parentCategory.children) {
parentCategory.children.forEach(category => concreteTree(category, categories)) parentCategory.children.forEach(category => concreteTree(category, categories))
} else {
parentCategory.isLeaf = true
} }
} }

View File

@ -19,6 +19,22 @@ postApi.query = params => {
}) })
} }
postApi.create = postToCreate => {
return service({
url: baseUrl,
method: 'post',
data: postToCreate
})
}
postApi.update = (postId, postToUpdate) => {
return service({
url: `${baseUrl}/${postId}`,
method: 'put',
data: postToUpdate
})
}
postApi.postStatus = { postApi.postStatus = {
PUBLISHED: { PUBLISHED: {
color: 'green', color: 'green',

View File

@ -11,6 +11,7 @@
<a-card> <a-card>
<div style="margin-bottom: 16px"> <div style="margin-bottom: 16px">
<a-input <a-input
v-model="postToStage.title"
v-decorator="['title', { rules: [{ required: true, message: '请输入文章标题' }] }]" v-decorator="['title', { rules: [{ required: true, message: '请输入文章标题' }] }]"
size="large" size="large"
placeholder="请输入文章标题" placeholder="请输入文章标题"
@ -26,7 +27,7 @@
<div id="editor"> <div id="editor">
<mavon-editor <mavon-editor
:toolbars="markdownOption" :toolbars="markdownOption"
v-model="value" v-model="postToStage.originalContent"
:boxShadow="false" :boxShadow="false"
:ishljs="true" :ishljs="true"
/> />
@ -55,18 +56,21 @@
<a-form layout="vertical"> <a-form layout="vertical">
<a-form-item <a-form-item
label="文章路径:" label="文章路径:"
:help="'https://localhost:8090/archives/' + postUrl" :help="'/archives/' + (postToStage.url ? postToStage.url : '{auto_generate}')"
> >
<a-input v-model="postUrl" /> <a-input v-model="postToStage.url" />
</a-form-item> </a-form-item>
<a-form-item label="文章密码:"> <a-form-item label="文章密码:">
<a-input type="password" /> <a-input type="password" />
</a-form-item> </a-form-item>
<a-form-item label="是否开启评论:"> <a-form-item label="是否关闭评论:">
<a-select defaultValue="1"> <a-radio-group
<a-select-option value="1"></a-select-option> v-model="postToStage.disallowComment"
<a-select-option value="0"></a-select-option> :defaultValue="false"
</a-select> >
<a-radio :value="false">开启</a-radio>
<a-radio :value="true">关闭</a-radio>
</a-radio-group>
</a-form-item> </a-form-item>
</a-form> </a-form>
</div> </div>
@ -76,7 +80,10 @@
<div :style="{ marginBottom: '16px' }"> <div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">分类目录</h3> <h3 class="post-setting-drawer-title">分类目录</h3>
<div class="post-setting-drawer-item"> <div class="post-setting-drawer-item">
<category-tree :categories="categories" /> <category-tree
v-model="selectedCategoryIds"
:categories="categories"
/>
</div> </div>
</div> </div>
<a-divider /> <a-divider />
@ -87,13 +94,15 @@
<a-form layout="vertical"> <a-form layout="vertical">
<a-form-item> <a-form-item>
<a-select <a-select
mode="tags" v-model="selectedTagIds"
allowClear
mode="multiple"
placeholder="选择或输入标签" placeholder="选择或输入标签"
> >
<a-select-option <a-select-option
v-for="tag in tags" v-for="tag in tags"
:key="tag.id" :key="tag.id"
:value="tag.id.toString()" :value="tag.id"
>{{ tag.name }}</a-select-option> >{{ tag.name }}</a-select-option>
</a-select> </a-select>
</a-form-item> </a-form-item>
@ -118,12 +127,12 @@
<div class="postControl"> <div class="postControl">
<a-button <a-button
style="marginRight: 8px" style="marginRight: 8px"
@click="onClose" @click="handleDraftClick"
>保存草稿</a-button> >保存草稿</a-button>
<a-button <a-button
@click="onClose" @click="handlePublishClick"
type="primary" type="primary"
>发布</a-button> >{{ publishText }}</a-button>
</div> </div>
</a-drawer> </a-drawer>
</a-col> </a-col>
@ -138,6 +147,7 @@ import { mixin, mixinDevice } from '@/utils/mixin.js'
import 'mavon-editor/dist/css/index.css' import 'mavon-editor/dist/css/index.css'
import tagApi from '@/api/tag' import tagApi from '@/api/tag'
import categoryApi from '@/api/category' import categoryApi from '@/api/category'
import postApi from '@/api/post'
const toolbars = { const toolbars = {
bold: true, // bold: true, //
@ -177,10 +187,20 @@ export default {
value: 'Hello World', value: 'Hello World',
visible: false, visible: false,
drawerWidth: '460', drawerWidth: '460',
postUrl: 'hello-world',
tags: [], tags: [],
categories: [], categories: [],
markdownOption: toolbars selectedCategoryIds: [],
selectedTagIds: [],
markdownOption: toolbars,
postToStage: {}
}
},
computed: {
publishText() {
if (this.postToStage.id) {
return '更新'
}
return '创建并发布'
} }
}, },
mounted() { mounted() {
@ -205,9 +225,38 @@ export default {
this.categories = response.data.data this.categories = response.data.data
}) })
}, },
createOrUpdatePost() {
// Set category ids
this.postToStage.categoryIds = this.selectedCategoryIds
// Set tag ids
this.postToStage.tagIds = this.selectedTagIds
if (this.postToStage.id) {
// Update the post
postApi.update(this.postToStage.id, this.postToStage).then(response => {
this.$log.debug('Updated post', response.data.data)
this.$message.success('文章更新成功')
})
} else {
// Create the post
postApi.create(this.postToStage).then(response => {
this.$log.debug('Created post', response.data.data)
this.$message.success('文章创建成功')
this.postToStage.id = response.data.data.id
})
}
},
showDrawer() { showDrawer() {
this.visible = true this.visible = true
}, },
handlePublishClick() {
this.postToStage.status = 'PUBLISHED'
this.createOrUpdatePost()
},
handleDraftClick() {
this.postToStage.status = 'DRAFT'
this.createOrUpdatePost()
},
onClose() { onClose() {
this.visible = false this.visible = false
} }

View File

@ -3,6 +3,7 @@
checkable checkable
:treeData="categoryTree" :treeData="categoryTree"
:defaultExpandAll="true" :defaultExpandAll="true"
@check="onCheck"
> >
<span <span
slot="title0010" slot="title0010"
@ -16,7 +17,16 @@ import categoryApi from '@/api/category'
export default { export default {
name: 'CategoryTree', name: 'CategoryTree',
model: {
prop: 'categoryIds',
event: 'check'
},
props: { props: {
categoryIds: {
type: Array,
required: false,
default: () => []
},
categories: { categories: {
type: Array, type: Array,
required: false, required: false,
@ -27,6 +37,21 @@ export default {
categoryTree() { categoryTree() {
return categoryApi.concreteTree(this.categories) return categoryApi.concreteTree(this.categories)
} }
},
methods: {
onCheck(checkedKeys, e) {
this.$log.debug('Chekced keys', checkedKeys)
this.$log.debug('e', e)
const categoryIds = e.checkedNodes
.filter(node => {
return node.data.props.isLeaf
})
.map(node => node.key)
this.$log.debug('Effectively selected category ids', categoryIds)
this.$emit('check', categoryIds)
}
} }
} }
</script> </script>