ArticleDetail.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div class="createPost-container">
  3. <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
  4. <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
  5. <CommentDropdown v-model="postForm.comment_disabled" />
  6. <PlatformDropdown v-model="postForm.platforms" />
  7. <SourceUrlDropdown v-model="postForm.source_uri" />
  8. <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm">
  9. Publish
  10. </el-button>
  11. <el-button v-loading="loading" type="warning" @click="draftForm">
  12. Draft
  13. </el-button>
  14. </sticky>
  15. <div class="createPost-main-container">
  16. <el-row>
  17. <Warning />
  18. <el-col :span="24">
  19. <el-form-item style="margin-bottom: 40px;" prop="title">
  20. <MDinput v-model="postForm.title" :maxlength="100" name="name" required>
  21. Title
  22. </MDinput>
  23. </el-form-item>
  24. <div class="postInfo-container">
  25. <el-row>
  26. <el-col :span="8">
  27. <el-form-item label-width="60px" label="Author:" class="postInfo-container-item">
  28. <el-select v-model="postForm.author" :remote-method="getRemoteUserList" filterable default-first-option remote placeholder="Search user">
  29. <el-option v-for="(item,index) in userListOptions" :key="item+index" :label="item" :value="item" />
  30. </el-select>
  31. </el-form-item>
  32. </el-col>
  33. <el-col :span="10">
  34. <el-form-item label-width="120px" label="Publish Time:" class="postInfo-container-item">
  35. <el-date-picker v-model="displayTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="Select date and time" />
  36. </el-form-item>
  37. </el-col>
  38. <el-col :span="6">
  39. <el-form-item label-width="90px" label="Importance:" class="postInfo-container-item">
  40. <el-rate
  41. v-model="postForm.importance"
  42. :max="3"
  43. :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
  44. :low-threshold="1"
  45. :high-threshold="3"
  46. style="display:inline-block"
  47. />
  48. </el-form-item>
  49. </el-col>
  50. </el-row>
  51. </div>
  52. </el-col>
  53. </el-row>
  54. <el-form-item style="margin-bottom: 40px;" label-width="70px" label="Summary:">
  55. <el-input v-model="postForm.content_short" :rows="1" type="textarea" class="article-textarea" autosize placeholder="Please enter the content" />
  56. <span v-show="contentShortLength" class="word-counter">{{ contentShortLength }}words</span>
  57. </el-form-item>
  58. <el-form-item prop="content" style="margin-bottom: 30px;">
  59. <Tinymce ref="editor" v-model="postForm.content" :height="400" />
  60. </el-form-item>
  61. <el-form-item prop="image_uri" style="margin-bottom: 30px;">
  62. <Upload v-model="postForm.image_uri" />
  63. </el-form-item>
  64. </div>
  65. </el-form>
  66. </div>
  67. </template>
  68. <script>
  69. import Tinymce from '@/components/Tinymce'
  70. import Upload from '@/components/Upload/SingleImage3'
  71. import MDinput from '@/components/MDinput'
  72. import Sticky from '@/components/Sticky' // 粘性header组件
  73. import { validURL } from '@/utils/validate'
  74. import { fetchArticle } from '@/api/article'
  75. import { searchUser } from '@/api/remote-search'
  76. import Warning from './Warning'
  77. import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from './Dropdown'
  78. const defaultForm = {
  79. status: 'draft',
  80. title: '', // 文章题目
  81. content: '', // 文章内容
  82. content_short: '', // 文章摘要
  83. source_uri: '', // 文章外链
  84. image_uri: '', // 文章图片
  85. display_time: undefined, // 前台展示时间
  86. id: undefined,
  87. platforms: ['a-platform'],
  88. comment_disabled: false,
  89. importance: 0
  90. }
  91. export default {
  92. name: 'ArticleDetail',
  93. components: { Tinymce, MDinput, Upload, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  94. props: {
  95. isEdit: {
  96. type: Boolean,
  97. default: false
  98. }
  99. },
  100. data() {
  101. const validateRequire = (rule, value, callback) => {
  102. if (value === '') {
  103. this.$message({
  104. message: rule.field + '为必传项',
  105. type: 'error'
  106. })
  107. callback(new Error(rule.field + '为必传项'))
  108. } else {
  109. callback()
  110. }
  111. }
  112. const validateSourceUri = (rule, value, callback) => {
  113. if (value) {
  114. if (validURL(value)) {
  115. callback()
  116. } else {
  117. this.$message({
  118. message: '外链url填写不正确',
  119. type: 'error'
  120. })
  121. callback(new Error('外链url填写不正确'))
  122. }
  123. } else {
  124. callback()
  125. }
  126. }
  127. return {
  128. postForm: Object.assign({}, defaultForm),
  129. loading: false,
  130. userListOptions: [],
  131. rules: {
  132. image_uri: [{ validator: validateRequire }],
  133. title: [{ validator: validateRequire }],
  134. content: [{ validator: validateRequire }],
  135. source_uri: [{ validator: validateSourceUri, trigger: 'blur' }]
  136. },
  137. tempRoute: {}
  138. }
  139. },
  140. computed: {
  141. contentShortLength() {
  142. return this.postForm.content_short.length
  143. },
  144. lang() {
  145. return this.$store.getters.language
  146. },
  147. displayTime: {
  148. // set and get is useful when the data
  149. // returned by the back end api is different from the front end
  150. // back end return => "2013-06-25 06:59:25"
  151. // front end need timestamp => 1372114765000
  152. get() {
  153. return (+new Date(this.postForm.display_time))
  154. },
  155. set(val) {
  156. this.postForm.display_time = new Date(val)
  157. }
  158. }
  159. },
  160. created() {
  161. if (this.isEdit) {
  162. const id = this.$route.params && this.$route.params.id
  163. this.fetchData(id)
  164. }
  165. // Why need to make a copy of this.$route here?
  166. // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
  167. // https://github.com/PanJiaChen/vue-element-admin/issues/1221
  168. this.tempRoute = Object.assign({}, this.$route)
  169. },
  170. methods: {
  171. fetchData(id) {
  172. fetchArticle(id).then(response => {
  173. this.postForm = response.data
  174. // just for test
  175. this.postForm.title += ` Article Id:${this.postForm.id}`
  176. this.postForm.content_short += ` Article Id:${this.postForm.id}`
  177. // set tagsview title
  178. this.setTagsViewTitle()
  179. // set page title
  180. this.setPageTitle()
  181. }).catch(err => {
  182. console.log(err)
  183. })
  184. },
  185. setTagsViewTitle() {
  186. const title = this.lang === 'zh' ? '编辑文章' : 'Edit Article'
  187. const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
  188. this.$store.dispatch('tagsView/updateVisitedView', route)
  189. },
  190. setPageTitle() {
  191. const title = 'Edit Article'
  192. document.title = `${title} - ${this.postForm.id}`
  193. },
  194. submitForm() {
  195. console.log(this.postForm)
  196. this.$refs.postForm.validate(valid => {
  197. if (valid) {
  198. this.loading = true
  199. this.$notify({
  200. title: '成功',
  201. message: '发布文章成功',
  202. type: 'success',
  203. duration: 2000
  204. })
  205. this.postForm.status = 'published'
  206. this.loading = false
  207. } else {
  208. console.log('error submit!!')
  209. return false
  210. }
  211. })
  212. },
  213. draftForm() {
  214. if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
  215. this.$message({
  216. message: '请填写必要的标题和内容',
  217. type: 'warning'
  218. })
  219. return
  220. }
  221. this.$message({
  222. message: '保存成功',
  223. type: 'success',
  224. showClose: true,
  225. duration: 1000
  226. })
  227. this.postForm.status = 'draft'
  228. },
  229. getRemoteUserList(query) {
  230. searchUser(query).then(response => {
  231. if (!response.data.items) return
  232. this.userListOptions = response.data.items.map(v => v.name)
  233. })
  234. }
  235. }
  236. }
  237. </script>
  238. <style lang="scss" scoped>
  239. @import "~@/styles/mixin.scss";
  240. .createPost-container {
  241. position: relative;
  242. .createPost-main-container {
  243. padding: 40px 45px 20px 50px;
  244. .postInfo-container {
  245. position: relative;
  246. @include clearfix;
  247. margin-bottom: 10px;
  248. .postInfo-container-item {
  249. float: left;
  250. }
  251. }
  252. }
  253. .word-counter {
  254. width: 40px;
  255. position: absolute;
  256. right: 10px;
  257. top: 0px;
  258. }
  259. }
  260. .article-textarea ::v-deep {
  261. textarea {
  262. padding-right: 40px;
  263. resize: none;
  264. border: none;
  265. border-radius: 0px;
  266. border-bottom: 1px solid #bfcbd9;
  267. }
  268. }
  269. </style>