halo/src/views/post/components/PostSettingDrawer.vue

407 lines
12 KiB
Vue
Raw Normal View History

2019-08-26 13:49:52 +00:00
<template>
<a-drawer
title="文章设置"
:width="isMobile()?'100%':'460'"
placement="right"
closable
destroyOnClose
2019-08-27 14:48:56 +00:00
@close="onClose"
:visible="visible"
2019-08-26 13:49:52 +00:00
>
2019-08-27 03:10:22 +00:00
<a-skeleton
active
:loading="settingLoading"
:paragraph="{ rows: 24 }"
>
<div class="post-setting-drawer-content">
<div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">基本设置</h3>
<div class="post-setting-drawer-item">
2019-08-26 13:49:52 +00:00
<a-form layout="vertical">
2019-08-27 03:10:22 +00:00
<a-form-item
label="文章标题:"
v-if="needTitle"
>
<a-input v-model="selectedPost.title" />
2019-08-26 13:49:52 +00:00
</a-form-item>
2019-08-27 03:10:22 +00:00
<a-form-item
label="文章路径:"
:help="options.blog_url+'/archives/' + (selectedPost.url ? selectedPost.url : '{auto_generate}')"
>
<a-input v-model="selectedPost.url" />
2019-08-26 13:49:52 +00:00
</a-form-item>
2019-09-06 08:55:50 +00:00
<a-form-item label="访问密码:">
2019-11-19 06:23:10 +00:00
<a-input-password v-model="selectedPost.password" autocomplete="new-password"/>
2019-09-06 08:55:50 +00:00
</a-form-item>
2019-08-27 03:10:22 +00:00
<a-form-item label="发表时间:">
<a-date-picker
showTime
:defaultValue="pickerDefaultValue"
format="YYYY-MM-DD HH:mm:ss"
placeholder="选择文章发表时间"
@change="onPostDateChange"
@ok="onPostDateOk"
2019-08-26 13:49:52 +00:00
/>
</a-form-item>
2019-08-27 03:10:22 +00:00
<a-form-item label="开启评论:">
<a-radio-group
v-model="selectedPost.disallowComment"
:defaultValue="false"
>
<a-radio :value="false">开启</a-radio>
<a-radio :value="true">关闭</a-radio>
</a-radio-group>
2019-08-26 13:49:52 +00:00
</a-form-item>
2019-08-30 12:58:22 +00:00
<a-form-item label="是否置顶:">
<a-radio-group
v-model="selectedPost.topPriority"
:defaultValue="0"
>
<a-radio :value="1"></a-radio>
<a-radio :value="0"></a-radio>
</a-radio-group>
</a-form-item>
2019-08-26 13:49:52 +00:00
</a-form>
</div>
</div>
2019-08-27 03:10:22 +00:00
<a-divider />
2019-08-26 13:49:52 +00:00
2019-08-27 03:10:22 +00:00
<div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">分类目录</h3>
<div class="post-setting-drawer-item">
<category-tree
v-model="selectedCategoryIds"
:categories="categories"
/>
<div>
<a-form layout="vertical">
<a-form-item v-if="categoryFormVisible">
<category-select-tree
:categories="categories"
v-model="categoryToCreate.parentId"
/>
</a-form-item>
<a-form-item v-if="categoryFormVisible">
<a-input
placeholder="分类名称"
v-model="categoryToCreate.name"
/>
</a-form-item>
<a-form-item v-if="categoryFormVisible">
<a-input
placeholder="分类路径"
v-model="categoryToCreate.slugNames"
/>
</a-form-item>
<a-form-item>
<a-button
type="primary"
style="marginRight: 8px"
v-if="categoryFormVisible"
@click="handlerCreateCategory"
>保存</a-button>
<a-button
type="dashed"
style="marginRight: 8px"
v-if="!categoryFormVisible"
@click="toggleCategoryForm"
>新增</a-button>
<a-button
v-if="categoryFormVisible"
@click="toggleCategoryForm"
>取消</a-button>
</a-form-item>
</a-form>
</div>
</div>
2019-08-26 13:49:52 +00:00
</div>
2019-08-27 03:10:22 +00:00
<a-divider />
<div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">标签</h3>
<div class="post-setting-drawer-item">
<a-form layout="vertical">
<a-form-item>
<TagSelect v-model="selectedTagIds" />
</a-form-item>
</a-form>
</div>
</div>
<a-divider />
2019-08-26 13:49:52 +00:00
2019-08-27 03:10:22 +00:00
<div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">摘要</h3>
<div class="post-setting-drawer-item">
<a-form layout="vertical">
<a-form-item>
<a-input
type="textarea"
:autosize="{ minRows: 5 }"
v-model="selectedPost.summary"
placeholder="不填写则会自动生成"
/>
</a-form-item>
</a-form>
</div>
2019-08-26 13:49:52 +00:00
</div>
2019-08-27 03:10:22 +00:00
<a-divider />
2019-08-26 13:49:52 +00:00
2019-08-27 03:10:22 +00:00
<div :style="{ marginBottom: '16px' }">
<h3 class="post-setting-drawer-title">缩略图</h3>
<div class="post-setting-drawer-item">
2019-08-27 08:10:45 +00:00
<div class="post-thumb">
2019-08-27 03:10:22 +00:00
<img
class="img"
2019-10-22 11:48:54 +00:00
:src="selectedPost.thumbnail || '/images/placeholder.png'"
2019-08-27 08:10:45 +00:00
@click="()=>this.thumbDrawerVisible=true"
2019-08-27 03:10:22 +00:00
>
<a-button
2019-08-27 08:10:45 +00:00
class="post-thumb-remove"
2019-08-27 03:10:22 +00:00
type="dashed"
2019-10-18 11:46:58 +00:00
@click="handleRemoveThumb"
2019-08-27 03:10:22 +00:00
>移除</a-button>
</div>
2019-08-26 13:49:52 +00:00
</div>
</div>
2019-08-27 03:10:22 +00:00
<a-divider class="divider-transparent" />
2019-08-26 13:49:52 +00:00
</div>
2019-08-27 03:10:22 +00:00
</a-skeleton>
2019-08-27 03:36:44 +00:00
<AttachmentSelectDrawer
2019-08-27 08:10:45 +00:00
v-model="thumbDrawerVisible"
2019-08-27 03:36:44 +00:00
@listenToSelect="handleSelectPostThumb"
:drawerWidth="460"
/>
2019-08-27 03:45:14 +00:00
<div class="bottom-control">
<a-button
style="marginRight: 8px"
@click="handleDraftClick"
v-if="saveDraftButton"
>保存草稿</a-button>
<a-button
@click="handlePublishClick"
type="primary"
v-if="savePublishButton"
>发布</a-button>
<a-button
@click="handlePublishClick"
type="primary"
v-if="saveButton"
>保存</a-button>
</div>
2019-08-26 13:49:52 +00:00
</a-drawer>
</template>
<script>
import { mixin, mixinDevice } from '@/utils/mixin.js'
import moment from 'moment'
import CategoryTree from './CategoryTree'
import CategorySelectTree from './CategorySelectTree'
import TagSelect from './TagSelect'
import AttachmentSelectDrawer from '../../attachment/components/AttachmentSelectDrawer'
2019-09-03 08:58:30 +00:00
import { mapGetters } from 'vuex'
2019-08-26 13:49:52 +00:00
import categoryApi from '@/api/category'
import postApi from '@/api/post'
export default {
name: 'PostSettingDrawer',
2019-08-26 13:49:52 +00:00
mixins: [mixin, mixinDevice],
components: {
CategoryTree,
CategorySelectTree,
TagSelect,
AttachmentSelectDrawer
},
data() {
return {
2019-08-27 08:10:45 +00:00
thumbDrawerVisible: false,
2019-08-26 13:49:52 +00:00
categoryFormVisible: false,
2019-08-27 03:10:22 +00:00
settingLoading: true,
2019-08-27 02:49:56 +00:00
selectedPost: this.post,
selectedTagIds: this.tagIds,
selectedCategoryIds: this.categoryIds,
2019-08-26 13:49:52 +00:00
categories: [],
categoryToCreate: {}
}
},
props: {
post: {
type: Object,
required: true
},
2019-08-27 02:49:56 +00:00
tagIds: {
2019-08-26 13:49:52 +00:00
type: Array,
required: true
},
2019-08-27 02:49:56 +00:00
categoryIds: {
2019-08-26 13:49:52 +00:00
type: Array,
required: true
},
2019-08-27 14:48:56 +00:00
visible: {
2019-08-26 13:49:52 +00:00
type: Boolean,
required: false,
default: true
},
needTitle: {
type: Boolean,
required: false,
default: false
},
saveDraftButton: {
type: Boolean,
required: false,
default: true
},
savePublishButton: {
type: Boolean,
required: false,
default: true
},
saveButton: {
type: Boolean,
required: false,
default: false
}
},
created() {
2019-08-27 03:10:22 +00:00
this.loadSkeleton()
2019-08-26 13:49:52 +00:00
this.loadCategories()
},
2019-08-27 02:49:56 +00:00
watch: {
post(val) {
this.selectedPost = val
},
selectedPost(val) {
this.$emit('onRefreshPost', val)
},
tagIds(val) {
this.selectedTagIds = val
},
selectedTagIds(val) {
this.$emit('onRefreshTagIds', val)
},
categoryIds(val) {
this.selectedCategoryIds = val
},
selectedCategoryIds(val) {
this.$emit('onRefreshCategoryIds', val)
2019-08-27 03:10:22 +00:00
},
2019-08-27 14:48:56 +00:00
visible: function(newValue, oldValue) {
2019-08-27 03:10:22 +00:00
if (newValue) {
this.loadSkeleton()
}
2019-08-27 02:49:56 +00:00
}
},
2019-08-26 13:49:52 +00:00
computed: {
pickerDefaultValue() {
2019-08-27 02:49:56 +00:00
if (this.selectedPost.createTime) {
var date = new Date(this.selectedPost.createTime)
2019-08-26 13:49:52 +00:00
return moment(date, 'YYYY-MM-DD HH:mm:ss')
}
return moment(new Date(), 'YYYY-MM-DD HH:mm:ss')
2019-09-03 08:58:30 +00:00
},
...mapGetters(['options'])
2019-08-26 13:49:52 +00:00
},
methods: {
2019-08-27 03:10:22 +00:00
loadSkeleton() {
this.settingLoading = true
setTimeout(() => {
this.settingLoading = false
}, 500)
},
2019-08-26 13:49:52 +00:00
loadCategories() {
categoryApi.listAll().then(response => {
this.categories = response.data.data
})
},
handleSelectPostThumb(data) {
2019-08-27 02:49:56 +00:00
this.selectedPost.thumbnail = encodeURI(data.path)
2019-08-27 08:10:45 +00:00
this.thumbDrawerVisible = false
2019-08-26 13:49:52 +00:00
},
2019-10-18 11:46:58 +00:00
handleRemoveThumb() {
2019-08-27 02:49:56 +00:00
this.selectedPost.thumbnail = null
2019-08-26 13:49:52 +00:00
},
handlerCreateCategory() {
2019-08-28 01:46:10 +00:00
if (!this.categoryToCreate.name) {
this.$notification['error']({
message: '提示',
description: '分类名称不能为空!'
})
return
}
2019-08-26 13:49:52 +00:00
categoryApi.create(this.categoryToCreate).then(response => {
this.loadCategories()
this.categoryToCreate = {}
2019-08-31 15:18:07 +00:00
this.toggleCategoryForm()
2019-08-26 13:49:52 +00:00
})
},
toggleCategoryForm() {
this.categoryFormVisible = !this.categoryFormVisible
},
handleDraftClick() {
2019-08-27 02:49:56 +00:00
this.selectedPost.status = 'DRAFT'
2019-08-26 13:49:52 +00:00
this.savePost()
},
handlePublishClick() {
2019-08-27 02:49:56 +00:00
this.selectedPost.status = 'PUBLISHED'
2019-08-26 13:49:52 +00:00
this.savePost()
},
savePost() {
this.createOrUpdatePost(
2019-08-28 01:46:10 +00:00
() => this.$message.success('文章发布成功'),
() => this.$message.success('文章发布成功'),
2019-08-26 13:49:52 +00:00
false
)
},
createOrUpdatePost(createSuccess, updateSuccess, autoSave) {
2019-08-28 01:46:10 +00:00
if (!this.selectedPost.title) {
this.$notification['error']({
message: '提示',
description: '文章标题不能为空!'
})
return
}
if (!this.selectedPost.originalContent) {
this.$notification['error']({
message: '提示',
description: '文章内容不能为空!'
})
return
}
2019-08-26 13:49:52 +00:00
// Set category ids
2019-08-27 02:49:56 +00:00
this.selectedPost.categoryIds = this.selectedCategoryIds
2019-08-26 13:49:52 +00:00
// Set tag ids
2019-08-27 02:49:56 +00:00
this.selectedPost.tagIds = this.selectedTagIds
2019-08-26 13:49:52 +00:00
2019-08-27 02:49:56 +00:00
if (this.selectedPost.id) {
2019-08-26 13:49:52 +00:00
// Update the post
2019-08-27 02:49:56 +00:00
postApi.update(this.selectedPost.id, this.selectedPost, autoSave).then(response => {
2019-08-26 13:49:52 +00:00
this.$log.debug('Updated post', response.data.data)
if (updateSuccess) {
updateSuccess()
2019-09-03 02:41:46 +00:00
this.$router.push({ name: 'PostList' })
2019-08-26 13:49:52 +00:00
}
})
} else {
// Create the post
2019-08-27 02:49:56 +00:00
postApi.create(this.selectedPost, autoSave).then(response => {
2019-08-26 13:49:52 +00:00
this.$log.debug('Created post', response.data.data)
if (createSuccess) {
createSuccess()
2019-09-03 02:41:46 +00:00
this.$router.push({ name: 'PostList' })
2019-08-26 13:49:52 +00:00
}
2019-08-27 02:49:56 +00:00
this.selectedPost = response.data.data
2019-08-26 13:49:52 +00:00
})
}
},
onClose() {
this.$emit('close', false)
},
onPostDateChange(value, dateString) {
2019-08-27 02:49:56 +00:00
this.selectedPost.createTime = value.valueOf()
2019-08-26 13:49:52 +00:00
},
onPostDateOk(value) {
2019-08-27 02:49:56 +00:00
this.selectedPost.createTime = value.valueOf()
2019-08-26 13:49:52 +00:00
}
}
}
</script>