|
1 | 1 | <template>
|
2 |
| - <div ref="tuiEditor"></div> |
| 2 | + <div ref="tuiEditor"></div> |
3 | 3 | </template>
|
4 | 4 | <script>
|
5 |
| -import Editor from 'tui-editor'; |
| 5 | +import Editor from "tui-editor"; |
6 | 6 |
|
7 |
| -import editorEvents from './editorEvents'; |
8 |
| -import editorDefaultOptions from './editorDefaultOptions'; |
9 |
| -import valueUpdateMethod from './valueUpdateMethod'; |
| 7 | +import editorEvents from "./editorEvents"; |
| 8 | +import editorDefaultOptions from "./editorDefaultOptions"; |
| 9 | +import valueUpdateMethod from "./valueUpdateMethod"; |
10 | 10 |
|
11 | 11 | export default {
|
12 |
| - name: 'TuiEditor', |
13 |
| - props: { |
14 |
| - previewStyle: { |
15 |
| - type: String, |
16 |
| - default: 'tab' |
17 |
| - }, |
18 |
| - height: { |
19 |
| - type: String, |
20 |
| - default: '300px' |
21 |
| - }, |
22 |
| - value: { |
23 |
| - type: String, |
24 |
| - default: '' |
25 |
| - }, |
26 |
| - mode: { |
27 |
| - type: String, |
28 |
| - default: 'markdown' |
29 |
| - }, |
30 |
| - options: { |
31 |
| - type: Object, |
32 |
| - default() { |
33 |
| - return editorDefaultOptions; |
34 |
| - } |
35 |
| - }, |
36 |
| - html: { |
37 |
| - type: String |
38 |
| - }, |
39 |
| - visible: { |
40 |
| - type: Boolean, |
41 |
| - default: true |
42 |
| - } |
| 12 | + name: "TuiEditor", |
| 13 | + props: { |
| 14 | + previewStyle: { |
| 15 | + type: String, |
| 16 | + default: "tab" |
43 | 17 | },
|
44 |
| - data() { |
45 |
| - return { |
46 |
| - editor: null |
47 |
| - }; |
| 18 | + height: { |
| 19 | + type: String, |
| 20 | + default: "300px" |
48 | 21 | },
|
49 |
| - computed: { |
50 |
| - editorOptions() { |
51 |
| - const options = Object.assign({}, editorDefaultOptions, this.options); |
52 |
| - options.initialValue = this.value; |
53 |
| - options.initialEditType = this.mode; |
54 |
| - options.height = this.height; |
55 |
| - options.previewStyle = this.previewStyle; |
56 |
| -
|
57 |
| - return options; |
58 |
| - } |
| 22 | + value: { |
| 23 | + type: String, |
| 24 | + default: "" |
59 | 25 | },
|
60 |
| - watch: { |
61 |
| - previewStyle(newValue) { |
62 |
| - this.editor.changePreviewStyle(newValue); |
63 |
| - }, |
64 |
| - value(newValue, preValue) { |
65 |
| - if (newValue !== preValue && newValue !== this.editor.getValue()) { |
66 |
| - this.editor.setValue(newValue); |
67 |
| - } |
68 |
| - }, |
69 |
| - height(newValue) { |
70 |
| - this.editor.height(newValue); |
71 |
| - }, |
72 |
| - mode(newValue) { |
73 |
| - this.editor.changeMode(newValue); |
74 |
| - }, |
75 |
| - html(newValue) { |
76 |
| - this.editor.setHtml(newValue); |
77 |
| - this.$emit('input', this.editor.getValue()); |
78 |
| - }, |
79 |
| - visible(newValue) { |
80 |
| - if (newValue) { |
81 |
| - this.editor.show(); |
82 |
| - } else { |
83 |
| - this.editor.hide(); |
84 |
| - } |
85 |
| - } |
| 26 | + mode: { |
| 27 | + type: String, |
| 28 | + default: "markdown" |
86 | 29 | },
|
87 |
| - mounted() { |
88 |
| - const eventOption = {}; |
89 |
| - editorEvents.forEach(event => { |
90 |
| - eventOption[event] = (...args) => { |
91 |
| - this.$emit(event, ...args); |
92 |
| - }; |
93 |
| - }); |
94 |
| -
|
95 |
| - const options = Object.assign(this.editorOptions, { |
96 |
| - el: this.$refs.tuiEditor, |
97 |
| - events: eventOption |
98 |
| - }); |
| 30 | + options: { |
| 31 | + type: Object, |
| 32 | + default() { |
| 33 | + return editorDefaultOptions; |
| 34 | + } |
| 35 | + }, |
| 36 | + html: { |
| 37 | + type: String |
| 38 | + }, |
| 39 | + visible: { |
| 40 | + type: Boolean, |
| 41 | + default: true |
| 42 | + } |
| 43 | + }, |
| 44 | + data() { |
| 45 | + return { |
| 46 | + editor: null |
| 47 | + }; |
| 48 | + }, |
| 49 | + computed: { |
| 50 | + editorOptions() { |
| 51 | + const options = Object.assign({}, editorDefaultOptions, this.options); |
| 52 | + options.initialValue = this.value; |
| 53 | + options.initialEditType = this.mode; |
| 54 | + options.height = this.height; |
| 55 | + options.previewStyle = this.previewStyle; |
99 | 56 |
|
100 |
| - this.editor = new Editor(options); |
101 |
| - if (this.$listeners.input) { |
102 |
| - this.editor.on('change', () => { |
103 |
| - this.$emit('input', this.editor.getValue()); |
104 |
| - }); |
105 |
| - } |
| 57 | + return options; |
| 58 | + } |
| 59 | + }, |
| 60 | + watch: { |
| 61 | + previewStyle(newValue) { |
| 62 | + this.editor.changePreviewStyle(newValue); |
| 63 | + }, |
| 64 | + value(newValue, preValue) { |
| 65 | + if (newValue !== preValue && newValue !== this.editor.getValue()) { |
| 66 | + this.editor.setValue(newValue); |
| 67 | + } |
| 68 | + }, |
| 69 | + height(newValue) { |
| 70 | + this.editor.height(newValue); |
| 71 | + }, |
| 72 | + mode(newValue) { |
| 73 | + this.editor.changeMode(newValue); |
106 | 74 | },
|
107 |
| - destroyed() { |
108 |
| - editorEvents.forEach(event => { |
109 |
| - this.editor.off(event); |
110 |
| - }); |
111 |
| - this.editor.remove(); |
| 75 | + html(newValue) { |
| 76 | + this.editor.setHtml(newValue); |
| 77 | + this.$emit("input", this.editor.getValue()); |
112 | 78 | },
|
113 |
| - methods: { |
114 |
| - invoke(methodName, ...args) { |
115 |
| - let result = null; |
116 |
| - if (this.editor[methodName]) { |
117 |
| - result = this.editor[methodName](...args); |
118 |
| - if (valueUpdateMethod.indexOf(methodName) > -1) { |
119 |
| - this.$emit('input', this.editor.getValue()); |
120 |
| - } |
121 |
| - } |
| 79 | + visible(newValue) { |
| 80 | + if (newValue) { |
| 81 | + this.editor.show(); |
| 82 | + } else { |
| 83 | + this.editor.hide(); |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + mounted() { |
| 88 | + const eventOption = {}; |
| 89 | + editorEvents.forEach(event => { |
| 90 | + eventOption[event] = (...args) => { |
| 91 | + this.$emit(event, ...args); |
| 92 | + }; |
| 93 | + }); |
| 94 | +
|
| 95 | + const options = Object.assign(this.editorOptions, { |
| 96 | + el: this.$refs.tuiEditor, |
| 97 | + events: eventOption |
| 98 | + }); |
122 | 99 |
|
123 |
| - return result; |
| 100 | + this.editor = new Editor(options); |
| 101 | + if (this.$listeners.input) { |
| 102 | + this.editor.on("change", () => { |
| 103 | + this.$emit("input", this.editor.getValue()); |
| 104 | + }); |
| 105 | + } |
| 106 | + }, |
| 107 | + destroyed() { |
| 108 | + editorEvents.forEach(event => { |
| 109 | + this.editor.off(event); |
| 110 | + }); |
| 111 | + this.editor.remove(); |
| 112 | + }, |
| 113 | + methods: { |
| 114 | + invoke(methodName, ...args) { |
| 115 | + let result = null; |
| 116 | + if (this.editor[methodName]) { |
| 117 | + result = this.editor[methodName](...args); |
| 118 | + if (valueUpdateMethod.indexOf(methodName) > -1) { |
| 119 | + this.$emit("input", this.editor.getValue()); |
124 | 120 | }
|
| 121 | + } |
| 122 | +
|
| 123 | + return result; |
125 | 124 | }
|
| 125 | + } |
126 | 126 | };
|
127 | 127 | </script>
|
0 commit comments