Skip to content

Commit 64e7b75

Browse files
committed
在实例详情头部加上喜欢功能
1 parent ed37b8c commit 64e7b75

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

src/views/instance/components/header.vue

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,41 @@
66
</router-link>
77
</div>
88
<div class="instance-name flex-sh pointer d-flex flex-ai">
9-
<div class="d-flex flex-clo">
9+
<div class="d-flex flex-end">
1010
<span class="text-small">{{isNewWork?'新建实例':curInstanceDetail.title}}</span>
11-
<span class="text-xs author" @click="goToUserProfile" v-if="!isNewWork">{{curInstanceDetail.username}}</span>
11+
<span class="text-xs author" @click="goToUserProfile" v-if="!isNewWork">By {{curInstanceDetail.nickname}}</span>
1212
</div>
13-
<v-btn icon small @click="setVisibleDialogName('instanceConfig')" v-if="!isNewWork&&isSelfProfile">
13+
<v-btn icon small @click="setVisibleDialogName('instanceConfig')" v-if="!isNewWork&&isSelfInstance">
1414
<v-icon>mdi-pencil-outline</v-icon>
1515
</v-btn>
16-
<v-btn icon small v-if="!isNewWork&&!isSelfProfile">
17-
<v-icon>mdi-label-multiple-outline</v-icon>
18-
</v-btn>
16+
<v-menu :close-on-content-click="false" offset-y open-delay="200" close-delay="200" bottom>
17+
<template v-slot:activator="{ on, attrs }">
18+
<v-btn icon small v-bind="attrs" v-on="on" v-if="!isNewWork&&!isSelfInstance" :disabled="tags.length===0">
19+
<v-icon>mdi-label-multiple-outline</v-icon>
20+
</v-btn>
21+
</template>
22+
<v-card color="info" style="padding: 10px">
23+
<v-chip v-for="(item,index) in tags" :key="index" small style="margin: 0 5px">{{item}}</v-chip>
24+
</v-card>
25+
</v-menu>
1926
</div>
2027
<v-spacer></v-spacer>
2128
<div class="d-flex flex-ai">
2229
<div class="btn-opts">
2330
<v-btn color="#2a53cd" :disabled="disableSave" class="radius-2" small depressed @click="saveInstance"
24-
:loading="saveInstanceLoading" v-if="isSelfProfile || isNewWork">
31+
:loading="saveInstanceLoading" v-if="isSelfInstance || isNewWork">
2532
<v-icon left dark>mdi-cloud-upload</v-icon>保存
2633
</v-btn>
27-
<v-btn small color="info" class="radius-2" depressed @click="like" :loading="likeLoading"
28-
:disabled="!loginState" v-if="!hideLike">
29-
<v-icon left small color="red">mdi-heart</v-icon>喜爱
30-
</v-btn>
34+
<div v-if="!hideLike">
35+
<v-btn small color="info" class="radius-2" depressed @click="like" :loading="likeLoading"
36+
:disabled="!loginState" v-show="curInstanceDetail.liked">
37+
<v-icon left small color="red">mdi-heart</v-icon>取消喜爱
38+
</v-btn>
39+
<v-btn small color="info" class="radius-2" depressed @click="like" :loading="likeLoading"
40+
:disabled="!loginState" v-show="!curInstanceDetail.liked">
41+
<v-icon left small color="gray">mdi-heart</v-icon>喜爱
42+
</v-btn>
43+
</div>
3144
</div>
3245
<header-account dense></header-account>
3346
</div>
@@ -41,21 +54,27 @@ import { mapMutations, mapState, mapGetters } from 'vuex'
4154
import HeaderAccount from '@components/headerAccount.vue'
4255
import InstanceConfig from '@components/dialog/instanceConfig.vue'
4356
export default {
57+
inject: ['changeRouterKey'],
4458
data() {
4559
return {
4660
saveInstanceLoading: false,
4761
likeLoading: false,
62+
tags: [],
4863
}
4964
},
65+
created() {
66+
const tags = this.curInstanceDetail.tags
67+
this.tags = tags ? tags.split(',') : []
68+
},
5069
computed: {
5170
...mapState(['loginState', 'loginInfo', 'curInstanceDetail']),
52-
...mapGetters(['instanceContent', 'isSelfProfile']),
71+
...mapGetters(['instanceContent', 'isSelfInstance']),
5372
isNewWork() {
5473
return this.$route.name === 'NewWork'
5574
},
5675
hideLike() {
5776
const isNewWork = this.isNewWork
58-
return isNewWork || this.isSelfProfile
77+
return isNewWork || this.isSelfInstance
5978
},
6079
disableSave() {
6180
return !this.loginState || this.curInstanceDetail.saved
@@ -81,7 +100,6 @@ export default {
81100
headTags,
82101
})
83102
const reqData = {
84-
username: loginInfo.username,
85103
exampleName: isNewWork ? '新建实例' : instance.title,
86104
ispublic: instance.ispublic,
87105
label: instance.tags,
@@ -103,8 +121,9 @@ export default {
103121
if (isNewWork) {
104122
this.$router
105123
.replace(`/work/${loginInfo.username}/${res.data}`)
106-
.catch((err) => {})
107-
this.$emit('initInstanceData')
124+
.then(() => {
125+
this.changeRouterKey()
126+
})
108127
}
109128
} else {
110129
this.$message.error('实例保存失败!')
@@ -119,8 +138,22 @@ export default {
119138
const username = this.curInstanceDetail.username
120139
this.$router.push(`/user/${username}`)
121140
},
122-
like() {
141+
async like() {
142+
const { liked: myFavorites, id: exampleId } = this.curInstanceDetail
123143
this.likeLoading = true
144+
try {
145+
// 根据当前是否已喜欢来判定调用喜欢还是取消喜欢接口
146+
const api = this.$http
147+
const req = myFavorites ? api.delLikeWork : api.addLikeWork
148+
const res = await req({ username: this.loginInfo.username, exampleId })
149+
if (res.state) {
150+
this.setCurInstanceDetail({ liked: !myFavorites })
151+
this.$message.success(myFavorites ? '已取消喜爱!' : '已喜爱!')
152+
}
153+
} catch (err) {
154+
console.log(err)
155+
}
156+
this.likeLoading = false
124157
},
125158
},
126159
components: {
@@ -149,7 +182,8 @@ export default {
149182
color: $light-5;
150183
margin-left: 20px;
151184
.author {
152-
margin-top: -2px;
185+
margin-left: 5px;
186+
margin-right: 10px;
153187
color: $light-7;
154188
&:hover {
155189
color: $light-2;

src/views/instance/components/instance.vue

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div id="instance">
33
<div class="instance-content">
4-
<instance-header @initInstanceData="initInstanceData"></instance-header>
4+
<instance-header></instance-header>
55
<div class="main-body d-flex">
66
<sidebar class="flex-sh"></sidebar>
77
<div class="d-flex flex-1 area">
@@ -80,7 +80,6 @@ export default {
8080
publicPath: process.env.BASE_URL,
8181
isChildrenMounted: false,
8282
iframeFullScreen: false,
83-
iframeInit: false,
8483
consoleInfo: [],
8584
isCompiling: false,
8685
cursorPos: { col: 1, ln: 1 },
@@ -95,7 +94,7 @@ export default {
9594
// 一开始就初始化console并执行代码
9695
new IframeConsole(this.$refs.iframeBox)
9796
this.runCode().then(() => {
98-
this.iframeInit = true
97+
this.setIframeInit(true)
9998
})
10099
})
101100
},
@@ -113,6 +112,7 @@ export default {
113112
'instanceSetting',
114113
'instanceExtLinks',
115114
'consoleSettings',
115+
'iframeInit',
116116
]),
117117
isMD() {
118118
return this.prep[0] === 'Markdown'
@@ -136,10 +136,8 @@ export default {
136136
'setConsoleInfo',
137137
'setConsoleInfoCount',
138138
'setCurInstanceDetail',
139+
'setIframeInit',
139140
]),
140-
initInstanceData() {
141-
this.$emit('init')
142-
},
143141
viewResize(e) {
144142
// 用鼠标拖动分割线改变编辑器和预览窗口的宽度
145143
// 拖动时需要在iframe上显示一个遮罩层,否则鼠标滑动到iframe中会影响拖动事件监听
@@ -191,7 +189,7 @@ export default {
191189
// iframe.contentWindow.location.reload()
192190
const consoleSettings = this.consoleSettings
193191
if (consoleSettings.clear) {
194-
docConsole.clear()
192+
IframeConsole.clear()
195193
this.setConsoleInfo([])
196194
}
197195
await new Promise((resolve) => {
@@ -306,9 +304,6 @@ export default {
306304
DownloadDialog,
307305
ShortcutDialog,
308306
},
309-
beforeRouteLeave (to, from, next) {
310-
new IframeHandler().clearIframe()
311-
}
312307
}
313308
</script>
314309

0 commit comments

Comments
 (0)