This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
/
component.vue
92 lines (83 loc) · 2.19 KB
/
component.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
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<textarea></textarea>
</template>
<script type="text/javascript">
const jQuery = window.jQuery || require("jquery");
import 'trumbowyg';
// You have to import css yourself
// You may have to configure webpack to load svg files
import iconsSvg from 'trumbowyg/dist/ui/icons.svg';
export default {
name: 'trumbowyg',
props: {
value: {
default: null,
required: true,
validator(value) {
return value === null || typeof value === 'string' || value instanceof String
}
},
// http://alex-d.github.io/Trumbowyg/documentation.html#basic-options
config: {
type: Object,
default: () => ({})
},
// https://alex-d.github.io/Trumbowyg/documentation/#svg-icons
svgPath: {
type: [String, Boolean],
default: iconsSvg,
},
},
data() {
return {
// jQuery DOM
el: null,
}
},
mounted() {
// Return early if instance is already loaded
if (this.el) return;
// Store DOM
this.el = jQuery(this.$el);
// Init editor with config
this.el.trumbowyg(Object.assign({svgPath: this.svgPath}, this.config));
// Set initial value
this.el.trumbowyg('html', this.value);
// Watch for changes for further updates
this.el.on('tbwchange', this.onChange);
},
beforeDestroy() {
// Free up memory
if (this.el) {
this.el.trumbowyg('destroy');
this.el = null;
}
},
watch: {
/**
* Listen to change from outside of component and update DOM
*
* @param newValue String
*/
value(newValue) {
if (this.el) {
// Prevent multiple input events
if (newValue === this.el.trumbowyg('html')) return;
this.el.trumbowyg('html', newValue)
}
},
},
methods: {
/**
* Emit input event with current editor value
* This will update v-model on parent component
* This method gets called when value gets changed by editor itself
*
* @param event
*/
onChange(event) {
this.$emit('input', event.target.value);
},
}
};
</script>