nebula/components/common/comment/main.vue

488 lines
17 KiB
Vue
Raw Normal View History

2020-07-31 16:17:12 +08:00
<template>
<el-row class="pt-5">
2020-08-02 00:25:44 +08:00
<el-col>
<el-col style="margin-top: 1rem;" v-if="user">
<el-col :sm="1" :xl="1" :xs="2">
2020-07-31 16:17:12 +08:00
<el-avatar :src="avatar"></el-avatar>
</el-col>
<el-col :sm="23" :xl="23" :xs="22" style="padding-left: 1rem;">
2020-07-31 16:17:12 +08:00
<el-input @click.native="showComment" placeholder="请输入回帖内容"></el-input>
</el-col>
<el-col>
<el-drawer
:direction="direction"
:visible.sync="drawer"
size="50%">
2020-07-31 16:17:12 +08:00
<el-col slot="title">
<el-col>
<el-avatar :src="commentAuthorAvatar" v-show="commentAuthorAvatar"></el-avatar>
2020-07-31 16:17:12 +08:00
<span class="text-default" style="padding-left: 1rem;">{{ commentTitle }}</span>
</el-col>
</el-col>
<el-col>
<div id="contentEditor"></div>
</el-col>
<el-col style="margin-top: 1rem;padding-right:3rem;text-align: right;">
<el-button :loading="loading" @click="postComment" plain type="primary">发布</el-button>
2020-07-31 16:17:12 +08:00
</el-col>
</el-drawer>
</el-col>
</el-col>
<el-col class="text-center" style="margin-top: 1rem;" v-else>
<el-button @click="gotoLogin" plain size="medium" type="primary">登录</el-button>
2020-07-31 16:17:12 +08:00
后发布评论
</el-col>
2020-09-29 17:36:42 +08:00
<el-col>
<el-col :key="comment.idComment" v-for="comment in comments.list">
<el-card :id="'comment-' + comment.idComment" style="margin-top: 1rem;">
<el-col :sm="1" :xl="1" :xs="3">
<el-avatar :src="comment.commenter.userAvatarURL" v-show="comment.commenter.userAvatarURL"></el-avatar>
<el-avatar src="https://static.rymcu.com/article/1578475481946.png"
v-show="!comment.commenter.userAvatarURL"></el-avatar>
2020-07-31 16:17:12 +08:00
</el-col>
<el-col :sm="23" :xl="23" :xs="21" style="padding-bottom: 10px;" v-if="comment.commentStatus === '0'">
2020-07-31 16:17:12 +08:00
<el-col style="margin-left: 1rem;">
2020-08-02 00:25:44 +08:00
<el-col v-show="comment.commentOriginalCommentId">
2020-09-29 17:36:42 +08:00
<el-col :span="16">
2022-10-29 22:48:52 +08:00
<el-link :underline="false" @click="onRouter('user', comment.commenter.userAccount)"
class="text-default"
rel="nofollow">{{ comment.commenter.userNickname }}
</el-link>
2022-04-18 11:01:52 +08:00
<small class="text-default" style="margin: 0 0.2rem">回复了</small><span
style="font-weight: bold;"> {{ comment.commentOriginalAuthorNickname }}</span>
2020-09-29 17:36:42 +08:00
</el-col>
<el-col :span="8" class="text-right" style="padding-right: 1rem;">
2022-10-29 22:48:52 +08:00
<el-link :underline="false"
@click.native="toggleShowOriginalComment(comment.commentOriginalCommentId)" rel="nofollow"
title="查看原评论"><i
2022-04-18 11:01:52 +08:00
class="el-icon-reading"></i> 查看原评论
</el-link>
<el-tag effect="plain" size="mini" style="margin-left: 5px;"
v-show="isAuthor(comment.commentAuthorId)">
2022-04-18 11:01:52 +08:00
作者
</el-tag>
2020-09-29 17:36:42 +08:00
</el-col>
2020-07-31 16:17:12 +08:00
</el-col>
2020-08-02 00:25:44 +08:00
<el-col v-show="!comment.commentOriginalCommentId">
<el-col :span="16">
2022-10-29 22:48:52 +08:00
<el-link :underline="false" @click="onRouter('user', comment.commenter.userAccount)"
class="text-default"
rel="nofollow">{{ comment.commenter.userNickname }}
</el-link>
</el-col>
<el-col :span="8" class="text-right" style="padding-right: 1rem;">
<el-tag effect="plain" size="mini" style="margin-left: 5px;"
v-show="isAuthor(comment.commentAuthorId)">
作者
</el-tag>
</el-col>
2020-07-31 16:17:12 +08:00
</el-col>
</el-col>
<el-col style="padding: 1rem;">
<el-col>
2021-04-20 22:24:45 +08:00
<div class="vditor-reset comment-content" v-html="comment.commentContent"></div>
2020-07-31 16:17:12 +08:00
</el-col>
</el-col>
2020-09-29 17:36:42 +08:00
<el-col :span="16" style="padding-left: 1rem;">
<el-link :underline="false" class="text-default" rel="nofollow">{{ comment.timeAgo }}</el-link>
2020-07-31 16:17:12 +08:00
</el-col>
<el-col :span="8" class="text-right" style="margin-bottom: 0.5rem;" v-if="user">
<el-link :underline="false" @click.native="replyComment(comment)" rel="nofollow" title="评论"><i
2022-04-18 11:01:52 +08:00
class="el-icon-s-comment"></i> 评论
</el-link>
</el-col>
</el-col>
<el-col :sm="23" :xl="23" :xs="21" style="padding-bottom: 10px;" v-else>
2022-04-18 11:01:52 +08:00
<el-col style="margin-left: 1rem;">
<el-col :span="16">
2022-10-29 22:48:52 +08:00
<el-link :underline="false" @click="onRouter('user', comment.commenter.userAccount)"
class="text-default"
rel="nofollow">{{ comment.commenter.userNickname }}
2022-04-18 11:01:52 +08:00
</el-link>
</el-col>
<el-col :span="8" class="text-right" style="padding-right: 1rem;">
<el-tag effect="plain" size="mini" style="margin-left: 5px;"
v-show="isAuthor(comment.commentAuthorId)">
2022-04-18 11:01:52 +08:00
作者
</el-tag>
</el-col>
</el-col>
<el-col style="padding: 1rem;">
<el-col>
<div class="vditor-reset comment-content">
<small>内容不相关已被隐藏</small>
</div>
</el-col>
</el-col>
<el-col :span="16" style="padding-left: 1rem;">
<el-link :underline="false" class="text-default" rel="nofollow">{{ comment.timeAgo }}</el-link>
2020-07-31 16:17:12 +08:00
</el-col>
</el-col>
</el-card>
2022-10-29 22:48:52 +08:00
<el-col v-if="comment.commentOriginalCommentId" :id="'original-' + comment.commentOriginalCommentId" style="background-color: #d9d9d9;padding-left: 1.5rem;
2020-09-29 17:36:42 +08:00
margin-top: 0.3rem;border-radius: 0.5rem;cursor: pointer;display: none;">
<el-col :span="2" v-show="comment.commentOriginalCommentId">
2020-09-29 17:36:42 +08:00
<p>
2022-04-18 11:01:52 +08:00
<span>{{ comment.commentOriginalAuthorNickname }} :</span>
2020-09-29 17:36:42 +08:00
</p>
</el-col>
<el-col :span="20" v-show="comment.commentOriginalCommentId">
2021-04-20 22:24:45 +08:00
<div class="vditor-reset comment-content" v-html="comment.commentOriginalContent"></div>
2020-09-29 17:36:42 +08:00
</el-col>
</el-col>
2020-07-31 16:17:12 +08:00
</el-col>
<el-col>
<div class="vertical-container text-center">
<el-pagination :hide-on-single-page="true"
layout="prev, pager, next"
:page-size="comments.pageSize"
:current-page="comments.pageNum"
:total="comments.total"
prev-text="上一页"
next-text="下一页"
@current-change="currentChange">
</el-pagination>
</div>
</el-col>
2020-07-31 16:17:12 +08:00
</el-col>
</el-col>
</el-row>
</template>
<script>
2022-10-29 22:48:52 +08:00
import Vue from 'vue';
import {mapState} from 'vuex';
import apiConfig from '~/config/api.config';
2020-07-31 16:17:12 +08:00
2022-10-29 22:48:52 +08:00
export default {
name: "Comment",
props: {
fetching: {
type: Boolean,
default: false
2020-07-31 16:17:12 +08:00
},
2022-10-29 22:48:52 +08:00
authorId: {
type: Number,
default: false
2022-04-18 11:01:52 +08:00
},
2022-10-29 22:48:52 +08:00
title: {
type: String,
default: ''
}
},
fetch() {
let {store, params, query} = this.$nuxt.context
2022-10-29 22:48:52 +08:00
return Promise.all([
store.dispatch('comment/fetchList', {post_id: params.article_id, page: query.page})
2022-10-29 22:48:52 +08:00
])
},
computed: {
...mapState({
comments: state => state.comment.list.data,
isFetchingComment: state => state.comment.list.fetching,
// isPostingComment: state => state.comment.posting,
2022-10-29 22:48:52 +08:00
constants: state => state.global.constants,
language: state => state.global.language,
isMobile: state => state.global.isMobile,
user: state => state.auth.user,
loggedIn: state => state.auth.loggedIn,
2022-10-29 22:48:52 +08:00
avatar: state => state.auth.user?.avatarUrl
}),
isFetching() {
// 1. 宿主组件还在加载时,列表和 tool 都呈加载状态
// 2. 宿主组件加载完成,如果自己还在请求,则列表呈加载状态
// 3. 自己已请求完,宿主组件还在加载,列表和 tool 都呈加载状态
return this.fetching || this.isFetchingComment
}
},
data() {
return {
tokenURL: {
URL: '',
linkToImageURL: '',
token: ''
2020-07-31 16:17:12 +08:00
},
2022-10-29 22:48:52 +08:00
drawer: false,
direction: 'btt',
initEditor: false,
isShow: true,
loading: false,
commentOriginalCommentId: 0,
commentAuthorAvatar: '',
commentTitle: '',
postId: 0
2022-10-29 22:48:52 +08:00
}
},
methods: {
onRouter(name, data) {
this.$router.push(
{
path: '/user/' + data
}
)
},
_initEditor(data) {
let _ts = this;
let toolbar = [
'emoji',
'headings',
'bold',
'italic',
'strike',
'link',
'|',
'list',
'ordered-list',
'check',
'outdent',
'indent',
'|',
'quote',
'line',
'code',
'inline-code',
'insert-before',
'insert-after',
'|',
'upload',
// 'record',
'table',
'|',
'undo',
'redo',
'|',
'edit-mode',
{
name: 'more',
toolbar: [
'fullscreen',
'both',
'preview',
'info'
],
}]
return new Vue.Vditor(data.id, {
toolbar,
mode: 'ir',
tab: '\t',
cdn: apiConfig.VDITOR,
cache: {
enable: this.postId ? false : true,
id: this.postId ? this.postId : '',
},
after() {
_ts.contentEditor.setValue(data.value ? data.value : '');
},
hint: {
emoji: Vue.emoji
},
preview: {
hljs: {
enable: true,
lineNumber: true,
style: 'github'
2020-08-02 00:25:44 +08:00
},
2022-10-29 22:48:52 +08:00
markdown: {
toc: true,
2021-12-22 11:50:20 +08:00
},
2022-10-29 22:48:52 +08:00
delay: 500,
mode: data.mode,
/*url: `${process.env.Server}/api/console/markdown`,*/
parse: (element) => {
if (element.style.display === 'none') {
return
2020-07-31 16:17:12 +08:00
}
2022-10-29 22:48:52 +08:00
// LazyLoadImage();
// Vue.Vditor.highlightRender({style:'github'}, element, document);
2022-05-24 11:07:05 +08:00
},
2022-10-29 22:48:52 +08:00
theme: {
cdn: apiConfig.VDITOR_CSS
2020-07-31 16:17:12 +08:00
}
2022-10-29 22:48:52 +08:00
},
upload: {
max: 10 * 1024 * 1024,
url: this.tokenURL.URL,
linkToImgUrl: this.tokenURL.linkToImageURL,
token: this.tokenURL.token,
filename: name => name.replace(/[^(a-zA-Z0-9\u4e00-\u9fa5\.)]/g, '').replace(/[\?\\/:|<>\*\[\]\(\)\$%\{\}@~]/g, '').replace('/\\s/g', '')
},
height: data.height,
counter: 102400,
resize: {
enable: data.resize,
},
lang: this.$store.state.locale,
placeholder: data.placeholder,
})
},
_loadEditor() {
let _ts = this;
if (!_ts.initEditor) {
_ts.$set(_ts, 'initEditor', true);
setTimeout(function () {
_ts.contentEditor = _ts._initEditor({
id: 'contentEditor',
mode: 'both',
height: 200,
placeholder: '', //this.$t('inputContent', this.$store.state.locale)
resize: false,
value: ''
});
}, 500);
2020-07-31 16:17:12 +08:00
}
},
2022-10-29 22:48:52 +08:00
gotoComment(commentId) {
console.log(commentId);
},
replyComment(comment) {
2020-08-02 22:45:24 +08:00
let _ts = this;
2022-10-29 22:48:52 +08:00
_ts.$set(_ts, 'drawer', true);
_ts.$set(_ts, 'commentTitle', comment.commenter.userNickname);
_ts.$set(_ts, 'commentAuthorAvatar', comment.commenter.userAvatarURL);
_ts.$set(_ts, 'commentOriginalCommentId', comment.idComment);
_ts._loadEditor();
},
showComment() {
let _ts = this;
_ts.$set(_ts, 'drawer', true);
_ts.$set(_ts, 'commentTitle', _ts.title);
_ts.$set(_ts, 'commentAuthorAvatar', '');
_ts.$set(_ts, 'commentOriginalCommentId', 0);
_ts._loadEditor();
},
async postComment() {
let _ts = this;
_ts.$set(_ts, 'loading', true);
let commentContent = await _ts.contentEditor.getHTML();
if (!(commentContent)) {
_ts.$message("回帖内容不能为空!");
return false;
2020-07-31 16:17:12 +08:00
}
2022-10-29 22:48:52 +08:00
let comment = {
commentArticleId: _ts.postId,
commentContent: commentContent,
commentOriginalCommentId: _ts.commentOriginalCommentId,
commentAuthorId: _ts.user.idUser
};
_ts.$axios.$post('/api/comment/post', comment).then(function (res) {
if (res) {
if (res.message) {
_ts.$message(res.message);
return false;
2022-04-18 11:01:52 +08:00
}
2022-10-29 22:48:52 +08:00
_ts.contentEditor.setValue('');
_ts.$set(_ts, 'drawer', false);
_ts.$fetch()
}
2022-10-29 22:48:52 +08:00
_ts.$set(_ts, 'loading', false);
})
},
2022-10-29 22:48:52 +08:00
gotoLogin() {
this.$emit('gotoLogin');
},
// 取消回复
cancelCommentReply() {
this.commentOriginalCommentId = 0
},
toggleShowOriginalComment(commentId) {
let ele = document.getElementById('original-' + commentId);
if (ele.style.display === 'none') {
ele.style.display = 'block';
} else {
ele.style.display = 'none';
2022-04-18 11:01:52 +08:00
}
},
2022-10-29 22:48:52 +08:00
isAuthor(commentAuthorId) {
return this.authorId === commentAuthorId;
},
currentChange(page) {
this.$router.push({
path: `/article/${this.postId}`,
query: {
page: page
}
})
2022-10-29 22:48:52 +08:00
}
},
async mounted() {
let _ts = this;
_ts.$set(_ts, 'postId', _ts.$route.params.article_id);
2022-10-29 22:48:52 +08:00
_ts.$store.commit('setActiveMenu', 'post-article');
if (_ts.loggedIn) {
2022-10-29 22:48:52 +08:00
const responseData = await _ts.$axios.$get('/api/upload/token');
if (responseData) {
_ts.$set(_ts, 'tokenURL', {
token: responseData.uploadToken || '',
URL: responseData.uploadURL || '',
linkToImageURL: responseData.linkToImageURL || ''
})
}
}
Vue.nextTick(function () {
// 评论渲染
const previewElements = document.getElementsByClassName("comment-content");
if (previewElements && previewElements.length > 0) {
for (let i = 0; i<previewElements.length; i++) {
const previewElement = previewElements[i];
Vue.VditorPreview.codeRender(previewElement, 'zh_CN');
Vue.VditorPreview.highlightRender({
"enable": true,
"lineNumber": true,
"style": "github"
}, previewElement, apiConfig.VDITOR);
Vue.VditorPreview.mathRender(previewElement, {
math: {"engine": "KaTeX", "inlineDigit": false, "macros": {}}, cdn: apiConfig.VDITOR
});
Vue.VditorPreview.mermaidRender(previewElement, apiConfig.VDITOR);
Vue.VditorPreview.graphvizRender(previewElement, apiConfig.VDITOR);
Vue.VditorPreview.chartRender(previewElement, apiConfig.VDITOR);
Vue.VditorPreview.mindmapRender(previewElement, apiConfig.VDITOR);
Vue.VditorPreview.abcRender(previewElement, apiConfig.VDITOR);
Vue.VditorPreview.mediaRender(previewElement);
Vue.VditorPreview.lazyLoadImageRender(previewElement);
//VditorPreview.outlineRender(previewElement, outLineElement);
previewElement.addEventListener("click", (event) => {
if (event.target.tagName === "IMG") {
Vue.VditorPreview.previewImage(event.target);
}
});
}
}
// 评论定位
if (_ts.$route.hash) {
const element = document.getElementById(_ts.$route.hash.replace('#', ''));
if (element) {
let actualTop = element.offsetTop;
let current = element.offsetParent;
while (current !== null) {
actualTop += current.offsetTop;
current = current.offsetParent;
}
window.scroll(0, actualTop);
}
}
})
},
watch: {
isFetching(isFetching) {
if (isFetching) {
this.cancelCommentReply()
}
},
'$route'(to, from) {
if ((to.query.page || 1) !== (from.query.page || 1)) {
this.$fetch()
}
2020-07-31 16:17:12 +08:00
}
2022-10-29 22:48:52 +08:00
},
destroyed() {
this.$store.commit('comment/clearListData')
2020-07-31 16:17:12 +08:00
}
2022-10-29 22:48:52 +08:00
}
2020-07-31 16:17:12 +08:00
</script>
<style scoped>
</style>