Skip to content

Commit 8f8629b

Browse files
committed
fix: fixed multiple toast issue
1 parent 6f3b701 commit 8f8629b

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ For detailed explanation on how things work, check out the [documentation](https
6060

6161
# TODO
6262

63-
- Add support for multiple languages like json,yaml,xml etc
64-
- Enable syntax highlighting
65-
- Auto detect language
66-
- Add small shadow to line numbers
67-
- Fix toast issue. (Click on copy button while toast is on)
63+
- [ ] Add support for multiple languages like json,yaml,xml etc
64+
- [ ] Enable syntax highlighting
65+
- [ ] Auto detect language
66+
- [ ] Add small shadow to line numbers
67+
- [x] Fix toast issue. (Click on copy button while toast is on)

pages/diff/_diff.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export default {
127127
methods: {
128128
putToClipboard(textToPut, toastContent) {
129129
navigator.clipboard.writeText(textToPut)
130-
this.$store.commit('toast/setData', {
130+
this.$store.commit('toast/show', {
131+
show: true,
131132
content: toastContent,
132133
iconHTML: `
133134
<svg
@@ -147,13 +148,6 @@ export default {
147148
`,
148149
theme: 'success',
149150
})
150-
this.$store.commit('toast/toggle')
151-
setTimeout(() => {
152-
this.$store.commit('toast/toggle')
153-
}, 5000)
154-
setTimeout(() => {
155-
this.$store.commit('toast/clean')
156-
}, 5500)
157151
},
158152
copyUrlToClipboard() {
159153
this.putToClipboard(window.location.href, 'Link copied to your clipboard')

store/toast.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,30 @@ export const state = () => ({
66
autoDismis: true,
77
timeout: 5000
88
})
9-
9+
let timeoutId = null;
1010
export const mutations = {
11-
toggle(state) {
12-
state.show = state.content && !state.show;
13-
},
14-
setData(state, { theme = "info", content = "", iconHTML = "" }) {
11+
show(state, {
12+
show,
13+
content,
14+
iconHTML,
15+
theme
16+
}) {
17+
if (!show) {
18+
state.show = show;
19+
return;
20+
}
21+
const isAlreadyShowing = state.show;
1522
state.content = content;
1623
state.theme = theme;
1724
state.iconHTML = iconHTML
25+
state.show = content && show;
26+
if (isAlreadyShowing && state.show && timeoutId) {
27+
clearTimeout(timeoutId);
28+
}
29+
if (state.show) {
30+
timeoutId = setTimeout(() => {
31+
this.commit('toast/show', { show: false })
32+
}, 5000)
33+
}
1834
},
19-
clean(state) {
20-
state.show = false;
21-
state.content = "";
22-
state.theme = "info";
23-
state.iconHTML = ""
24-
}
2535
}

0 commit comments

Comments
 (0)