-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeEditor.vue
More file actions
156 lines (152 loc) · 3.86 KB
/
Copy pathcodeEditor.vue
File metadata and controls
156 lines (152 loc) · 3.86 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<template>
<div class="code-editor">
<textarea></textarea>
</div>
</template>
<script>
window.CodeMirror = require('codemirror')
require('codemirror/mode/meta')
export default {
name: 'codemirror',
data: function() {
return {
content: ''
}
},
props: {
code: String,
value: String,
events: Array,
unseenLines: Array,
marker: Function,
loadtheme: {
type: Boolean,
default: function() {
return true
}
},
debugger: {
type: Boolean,
default: function() {
return true
}
},
options: {
type: Object,
required: true
},
},
mounted: function() {
var _this = this
this.editor = CodeMirror.fromTextArea(this.$el.firstElementChild, this.options)
this.editor.setValue(this.code || this.value || this.content)
this.editor.on('change', function(cm) {
_this.content = cm.getValue()
if (!!_this.$emit) {
_this.$emit('change', _this.content)
_this.$emit('input', _this.content)
}
})
var events = [
'scroll',
'changes',
'beforeChange',
'cursorActivity',
'keyHandled',
'inputRead',
'electricInput',
'beforeSelectionChange',
'viewportChange',
'swapDoc',
'gutterClick',
'gutterContextMenu',
'focus',
'blur',
'refresh',
'optionChange',
'scrollCursorIntoView',
'update'
];
if (this.events && this.events.length) {
events = events.concat(this.events)
}
for (var i = 0; i < events.length; i++) {
if (events.indexOf(events[i]) == i) {
(function(event) {
_this.editor.on(event, function(a, b, c) {
_this.$emit(event, a, b, c)
})
})(events[i])
}
}
this.$emit('ready', this.editor)
this.unseenLineMarkers()
// prevents funky dynamic rendering
window.setTimeout(function() {
_this.editor.refresh()
}, 0)
},
beforeDestroy: function() {
// garbage cleanup
var element = this.editor.doc.cm.getWrapperElement()
if (element && element.remove) {
element.remove()
}
},
watch: {
options: {
deep: true,
handler(options, oldOptions) {
var key
for (key in options) {
this.editor.setOption(key, options[key])
}
}
},
code: function(newVal, oldVal) {
var editor_value = this.editor.getValue()
if (newVal !== editor_value) {
var scrollInfo = this.editor.getScrollInfo()
this.editor.setValue(newVal)
this.content = newVal
this.editor.scrollTo(scrollInfo.left, scrollInfo.top)
}
this.unseenLineMarkers()
},
value: function(newVal, oldVal) {
var editor_value = this.editor.getValue()
if (newVal !== editor_value) {
var scrollInfo = this.editor.getScrollInfo()
this.editor.setValue(newVal)
this.content = newVal
this.editor.scrollTo(scrollInfo.left, scrollInfo.top)
}
this.unseenLineMarkers()
}
},
methods: {
refresh: function() {
this.editor.refresh()
},
unseenLineMarkers: function () {
var _this = this
if (_this.unseenLines !== undefined && _this.marker !== undefined) {
_this.unseenLines.forEach(line => {
var info = _this.editor.lineInfo(line)
_this.editor.setGutterMarker(line, "breakpoints", info.gutterMarkers ? null : _this.marker())
})
}
}
}
}
</script>
<style>
.code-editor {
padding:5px;
font-size:13px;
}
.code-editor .CodeMirror {
border:1px solid #c4c4c4;
padding:0px;
}
</style>