| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <div class="discussion-area">
- <h1 class="sub-title vertical-center">
- 讨论区
- <c-button theme="green" :left="12" :icon="focus ? 'unfold_less' : 'edit'" text @click="handleFocusButtonClick">
- {{focus ? '隐藏编辑器' : '讨论|笔记'}}
- </c-button>
- <c-button :no-bold="personal" text :theme="personal ? 'blue' : ''"
- :icon="personal ? 'check_box' : 'check_box_outline_blank'"
- title="如果设置为私人笔记,则这条讨论仅自己可见"
- @click="handleSetPersonal">笔记模式
- </c-button>
- </h1>
- <div class="discussion-area-content">
- <show-transition>
- <write-new-comment :show="personal" v-show="focus" class="mb-8" ref="writeComment"
- @comment-submit="handleCommentSubmit"
- :placeholder="writingAreaPlaceholder"></write-new-comment>
- </show-transition>
- <div v-show="notes.length">
- <div class="line"><span>私人笔记</span></div>
- <template v-for="comment of notes">
- <c-comment :key="'note' + comment.id" :comment="comment" personal
- @need-refresh="updateComments"
- >
- </c-comment>
- </template>
- </div>
- <div class="line"><span>公共讨论区</span></div>
- <div class="no-content-warning" v-if="comments === null">
- <div class="no-content-warning-tip">加载中...</div>
- </div>
- <div class="no-content-warning" v-else-if="comments.length === 0 && wsComments.length === 0">
- 本页还没有讨论
- </div>
- <template v-for="comment of wsComments">
- <c-comment v-if="comment.pageNumber === currentPage" :key="comment.id" :comment="comment" is-new
- @need-refresh="updateComments"
- >
- </c-comment>
- </template>
- <template v-for="comment of comments">
- <c-comment :key="comment.id" :comment="comment"
- @need-refresh="updateComments"
- >
- </c-comment>
- </template>
- </div>
- </div>
- </template>
- <script>
- import CComment from '@/components/Discussion/CComment'
- import WriteNewComment from '@/components/Discussion/WriteNewComment'
- import { mapState } from 'vuex'
- import { createComment, getComments, getSelfCommentsBySlide } from '@/server/comment'
- import WebsocketPublisher from '@/server/websocket/ws'
- import { useCommentWsServer } from '@/config/server-info'
- import CButton from '@/components/Basic/CButton'
- import ShowTransition from '@/animations/ShowTransition'
- import { FOCUS_ON_INPUT, SET_PERSONAL, USE_FOCUS } from '@/store/types/pdf-types'
- export default {
- components: { ShowTransition, CButton, WriteNewComment, CComment },
- data () {
- return {
- comments: null,
- notes: [],
- wsComments: [],
- slideId: this.$route.params.slideId
- }
- },
- mounted () {
- this.initWs()
- this.updateComments()
- },
- computed: {
- ...mapState({
- userInfo: state => state.main.userInfo,
- currentPage: state => state.pdf.currentPage,
- focus: state => state.pdf.focus,
- personal: state => state.pdf.personal
- }),
- writingAreaPlaceholder () {
- return '主题'
- }
- },
- watch: {
- currentPage () {
- this.comments = null
- this.updateComments()
- }
- },
- beforeDestroy () {
- this._ws.close()
- },
- methods: {
- initWs () {
- this._ws = new WebsocketPublisher(useCommentWsServer('/slide/') + this.$route.params.slideId)
- this._ws.subscribe({
- onNext: data => {
- this.wsComments.unshift(data)
- },
- onError: err => {
- this.$setErrorMessage(err)
- },
- onClose: () => {}
- })
- },
- updateComments () {
- this.getComments(1000)
- this.getNote(1000)
- },
- getComments (size) {
- return getComments({
- slideId: this.slideId,
- size,
- pageNumber: this.currentPage,
- sort: ['topNumber,desc', 'createAt,desc']
- }).then(res => {
- this.wsComments = []
- this.comments = res.data
- }).catch(err => {
- this.$setErrorMessage('获取本页评论区的过程中:' + err.response.data.msg)
- })
- },
- getNote (size) {
- return getSelfCommentsBySlide({
- slideId: this.slideId,
- pageNumber: this.currentPage,
- show: false,
- size: 1000,
- sort: ['createAt,desc']
- })
- .then(res => {
- this.notes = []
- this.notes = res.data
- })
- .catch(err => this.$setErrorMessage(err.response.data.msg))
- },
- handleCommentSubmit (comment) {
- createComment({
- slideId: this.slideId,
- pageNumber: this.currentPage,
- title: comment.title,
- content: comment.content,
- show: !this.personal
- }).then(res => {
- this.$refs.writeComment.resetContent()
- this.$setSuccessMessage('您的讨论帖发表成功')
- if (this.personal) {
- this.getNote(1000)
- }
- }).catch(err => {
- this.$setErrorMessage(err.response.data.msg)
- })
- },
- handleFocusButtonClick () {
- this.focus ? this.$store.commit(USE_FOCUS) : this.$store.commit(FOCUS_ON_INPUT)
- },
- handleSetPersonal () {
- this.$store.commit(SET_PERSONAL, { personal: !this.personal })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .discussion-area {
- max-width: 410px;
- padding: 12px 12px 0 12px;
- box-sizing: border-box;
- position: fixed;
- min-width: 405px;
- max-height: calc(100vh - 76px);
- overflow: auto;
- overflow-x: hidden;
- bottom: 0;
- right: 0;
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
- background-color: white;
- border: 1px solid #e8e8e8;
- }
- .no-content-warning {
- height: 50px;
- }
- .c-comment:nth-child(2n-1) {
- background-color: #f9f9f9;
- }
- .c-comment:nth-child(2n) {
- background-color: white;
- }
- @media (max-width: 500px) {
- .discussion-area {
- min-width: unset;
- max-width: unset;
- left: 0;
- right: 0;
- background-color: rgba(255, 255, 255, 1);
- }
- }
- .line {
- position: relative;
- font-size: 12px;
- font-weight: normal;
- text-align: center;
- line-height: 0;
- margin: 4px 0;
- & > span {
- color: #5a6a7a;
- display: inline-block;
- background-color: white;
- padding: 4px 12px;
- line-height: 12px;
- }
- &:before {
- z-index: -1;
- content: '';
- width: 100%;
- position: absolute;
- left: 0;
- top: 6px;
- border-bottom: 1px solid #aabaca;
- }
- }
- </style>
|