forked from janwirth/vue-medium-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.js
70 lines (64 loc) · 1.79 KB
/
interactive.js
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
import MediumEditor from 'medium-editor'
export default {
name: 'medium-editor',
props: {
text: [String],
customTag: {
type: [String],
default: () => 'div'
},
options: {
type: [Object],
default: () => {}
}
},
render (h) {
return h(this.customTag, { ref: 'element' })
},
mounted (evt) {
this.createAndSubscribe()
},
beforeDestroy (evt) {
this.tearDown()
},
methods: {
tearDown () {
this.api.unsubscribe('editableInput', this.emit)
this.api.destroy()
},
createAndSubscribe () {
this.$refs.element.innerHTML = this.text
this.api = new MediumEditor(this.$refs.element, this.options)
// bind edit operations to model
// we need to store the handler in order to later on detach it again
this.emit = event => this.$emit('edit', {event, api: this.api})
this.api.subscribe('editableInput', this.emit)
// emit event to give parent access to MediumEditor instance
this.$emit('editorCreated', this.api);
}
},
watch: {
text (newText) {
// innerHTML MUST not be performed if the text did not actually change.
// otherwise, the caret position will be reset.
if (newText !== this.$refs.element.innerHTML) {
this.api.setContent(this.text, 0)
this.$refs.element.innerHTML = this.text
}
},
/**
* There is currently no way to change the options of a medium editor
* without destroying and re-setting up the MediumEditor object.
* We only tear down the editor, if the options actually changed.
* See: https://github.com/yabwe/medium-editor/issues/1129
*/
options: {
handler(newOptions) {
this.tearDown()
this.createAndSubscribe()
},
deep: true
}
},
MediumEditor
}