Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 6c40449

Browse files
authored
fix: add watch to text property (#7)
1 parent 7422e4c commit 6c40449

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export default Vue.extend({
5858
### Alternatively
5959
```html
6060
<!-- VueTypeText JavaScript -->
61-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-type-text.min.js"></script>
62-
61+
<script src="https://cdn.jsdelivr.net/npm/vue-type-text/dist/vue-type-text.min.js"></script>
6362
```
6463
## License
6564
Code released under [MIT](https://github.com/ming-tsai/vue-type-text/blob/master/LICENSE) license.

dev/serve.vue

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export default Vue.extend({
66
name: 'ServeDev',
77
components: {
88
VueTypeText
9+
},
10+
data() {
11+
return {
12+
bindingText: "Welcome to VueTypeText",
13+
};
914
}
1015
});
1116
</script>
@@ -14,7 +19,8 @@ export default Vue.extend({
1419
<div id="app">
1520
<img alt="Vue logo" src="./assets/logo.png" />
1621
<vue-type-text tag="h1" :text="['VueTypeText', 'vue-type-text', 'vue type text']"/>
17-
<vue-type-text tag="p" text="Welcome to Your VueTypeText" />
22+
<vue-type-text tag="p" :text="bindingText" />
23+
<button @click="bindingText = 'Welcome to Your typed demo'">Change text</button>
1824
</div>
1925
</template>
2026

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-type-text",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Typed components for VueJs",
55
"repository": {
66
"type": "git",

src/vue-type-text.ts

+30-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ interface VueTypeText {
44
isDone: boolean;
55
arrayIndex: number;
66
bindingText: string;
7+
writeTimer: any;
8+
eraseTimer: any;
9+
arrayTimer: any;
710
}
811

912
export default Vue.extend({
@@ -40,7 +43,10 @@ export default Vue.extend({
4043
return {
4144
isDone: false,
4245
arrayIndex: 0,
43-
bindingText: ''
46+
bindingText: '',
47+
writeTimer: null,
48+
eraseTimer: null,
49+
arrayTimer: null,
4450
};
4551
},
4652
methods: {
@@ -51,7 +57,7 @@ export default Vue.extend({
5157
this.isDone = true;
5258
} else {
5359
if (Array.isArray(this.text)) {
54-
this.setIntervalImmediately(
60+
this.arrayTimer = this.setIntervalImmediately(
5561
async () => await this.managerArray(),
5662
this.timeTakes * 1.4 * 2
5763
);
@@ -62,9 +68,9 @@ export default Vue.extend({
6268
let index = 0;
6369
this.bindingText = '';
6470
const period = this.timeTakes / text.length;
65-
const timer = setInterval(() => {
71+
this.writeTimer = setInterval(() => {
6672
if (index >= text.length - 1) {
67-
clearInterval(timer);
73+
clearInterval(this.writeTimer);
6874
}
6975
index = this.appendText(text[index], index);
7076
}, period);
@@ -78,9 +84,9 @@ export default Vue.extend({
7884
let index = text.length;
7985
this.bindingText = text;
8086
const period = this.timeTakes / text.length;
81-
const timer = setInterval(() => {
87+
this.eraseTimer = setInterval(() => {
8288
if (index <= 0) {
83-
clearInterval(timer);
89+
clearInterval(this.eraseTimer);
8490
}
8591
index = this.removeText(index);
8692
}, period);
@@ -113,4 +119,22 @@ export default Vue.extend({
113119
async mounted() {
114120
await this.writeInit();
115121
},
122+
watch: {
123+
text() {
124+
if(this.writeTimer != null) {
125+
clearInterval(this.writeTimer);
126+
}
127+
128+
if(this.eraseTimer != null) {
129+
clearInterval(this.eraseTimer);
130+
}
131+
132+
if(this.arrayTimer != null) {
133+
this.arrayIndex = 0;
134+
clearInterval(this.arrayTimer);
135+
}
136+
137+
this.writeInit();
138+
}
139+
},
116140
});

0 commit comments

Comments
 (0)