-
Notifications
You must be signed in to change notification settings - Fork 0
/
Noodle.vue
79 lines (67 loc) · 1.35 KB
/
Noodle.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<template>
<div class="noodle">
<slot
:output="spaggetified"
:errors="errors"
:previewAttrs="{ html: html }"
:previewEvents="{ spaghettified: onSpaghettified }"
></slot>
</div>
</template>
<script>
import debounce from 'debounce';
import { compile } from './compiler';
export default {
name: 'Noodle',
props: {
input: String,
debounceTimeout: {
type: Number,
default: 250,
},
},
data() {
return {
html: '',
spaggetified: '',
errors: [],
};
},
watch: {
input(newInput) {
this.debouncedCompile(newInput);
},
debounceTimeout() {
this.initDebouncedCompile();
},
},
created() {
this.initDebouncedCompile();
},
mounted() {
this.debouncedCompile(this.input);
},
methods: {
initDebouncedCompile() {
if (this.debouncedCompile && this.debouncedCompile.clear) {
this.debouncedCompile.clear();
}
this.debouncedCompile =
this.debounceTimeout > 0
? debounce(this.compile, this.debounceTimeout)
: this.compile;
},
onSpaghettified(spaghettified) {
this.spaggetified = spaghettified;
},
compile(input) {
try {
this.html = compile(input);
this.errors = [];
} catch (err) {
this.errors = [err];
}
},
},
};
</script>