Skip to content

Commit 1d06729

Browse files
author
unknown
committed
Update
1 parent c5fc357 commit 1d06729

File tree

6 files changed

+41
-23
lines changed

6 files changed

+41
-23
lines changed

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,24 @@ export default {
6767
<codemirror></codemirror>
6868

6969

70-
<!-- component data bind(Vue.js1.X) -->
71-
<codemirror :code.sync="code"></codemirror>
72-
73-
74-
<!-- component config example 1(Vue.js1.X) -->
70+
<!-- component data bind and config in Vue.js1.X -->
71+
<codemirror :code="code" :options="editorOption"></codemirror>
72+
<!-- Bidirectional data binding in Vue.js1.X -->
7573
<codemirror :code.sync="code" :options="editorOption"></codemirror>
7674

7775

78-
<!-- in vue.js2.X .once and .sync are deprecated, parent component needs to explicitly emit an event instead of relying on implicit binding -->
76+
<!-- component data bind and config in Vue.js2.X -->
77+
<codemirror :code="code" :options="editorOption"></codemirror>
78+
<!-- Bidirectional data binding in Vue.js2.X -->
79+
<codemirror v-model="code" :options="editorOption"></codemirror>
80+
<!-- or -->
81+
<!-- If you need to manually control the data synchronization, you can monitor the code change event like this -->
7982
<codemirror :code="code" :options="editorOption" @changed="codeChange"></codemirror>
8083
```
8184

8285

8386
``` javascript
84-
// editorOption example:
87+
// editor option example:
8588
export default {
8689
data () {
8790
return {
@@ -90,13 +93,13 @@ export default {
9093
tabSize: 4,
9194
mode: 'text/javascript',
9295
theme: 'base16-dark',
93-
lineNumbers: true,
96+
lineNumbers: true,
9497
line: true,
9598
...
9699
}
97100
}
98101
},
99-
// if you use in vue2.X,parent component needs to explicitly emit an event instead of relying on implicit binding
102+
// If you need to manually control the data synchronization, parent component needs to explicitly emit an event instead of relying on implicit binding
100103
methods: {
101104
codeChange(newCode) {
102105
console.log(newCode)
@@ -118,8 +121,8 @@ mode: {
118121
```
119122

120123
``` html
121-
<!-- component config example 2(Vue.js1.X) -->
122-
<codemirror :code.sync="css" :options="{ tabSize: 2, mode: 'css' }"></codemirror>
124+
<!-- component config example 2 (Vue.js1.X) -->
125+
<codemirror :code.sync="css" :options="{ tabSize: 2, mode: 'text/css' }"></codemirror>
123126
```
124127

125128
``` javascript
@@ -139,7 +142,7 @@ data () {
139142

140143
[Codemirror themes](http://codemirror.net/demo/theme.html)
141144

142-
[Codemirror language modes](http://codemirror.net/mode/)
145+
[Codemirror language modes](http://codemirror.net/mode/) (MIME types defined)
143146

144147

145148

codemirror.vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var CodeMirror = require('codemirror/lib/codemirror.js')
77
var CodeMirrorMetas = require('./metas.js')
88
require('codemirror/lib/codemirror.css')
9-
// var
9+
// var
1010
export default {
1111
data: function() {
1212
return {
@@ -15,6 +15,7 @@
1515
},
1616
props: {
1717
code: String,
18+
value: String,
1819
options: {
1920
type: Object,
2021
default: function() {
@@ -41,7 +42,7 @@
4142
try {
4243
language = CodeMirrorMetas.findModeByMIME(language).mode
4344
} catch (e) {
44-
throw new Error('CodeMirror language mode: ' + language + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
45+
throw new Error('CodeMirror language mode: ' + language + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
4546
}
4647
}
4748
@@ -50,7 +51,7 @@
5051
try {
5152
language = CodeMirrorMetas.findModeByName(language.name).mode
5253
} catch (e) {
53-
throw new Error('CodeMirror language mode: ' + language.name + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
54+
throw new Error('CodeMirror language mode: ' + language.name + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
5455
}
5556
}
5657
@@ -64,20 +65,22 @@
6465
ready: function() {
6566
var _this = this
6667
this.editor = CodeMirror.fromTextArea(this.$el, this.options)
67-
this.editor.setValue(this.code || this.content)
68+
this.editor.setValue(this.code || this.value || this.content)
6869
this.editor.on('change', function(cm) {
6970
_this.content = cm.getValue()
71+
// _this.value = cm.getValue()
7072
_this.code = cm.getValue()
7173
})
7274
},
7375
mounted: function() {
7476
var _this = this
7577
this.editor = CodeMirror.fromTextArea(this.$el, this.options)
76-
this.editor.setValue(this.code || this.content)
78+
this.editor.setValue(this.code || this.value || this.content)
7779
this.editor.on('change', function(cm) {
7880
_this.content = cm.getValue()
7981
if (!!_this.$emit) {
8082
_this.$emit('changed', _this.content)
83+
_this.$emit('input', _this.content)
8184
}
8285
})
8386
},
@@ -88,6 +91,13 @@
8891
this.editor.setValue(newVal)
8992
this.content = newVal
9093
}
94+
},
95+
'value': function(newVal, oldVal) {
96+
const editor_value = this.editor.getValue()
97+
if (newVal !== editor_value) {
98+
this.editor.setValue(newVal)
99+
this.content = newVal
100+
}
91101
}
92102
}
93103
}

example/Example.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
<h3>Codemirror Example1:</h3>
55
<codemirror :code.sync="code" :options="editorOption"></codemirror>
66
<pre>{{ code }}</pre>
7+
<textarea v-model="code"></textarea>
78
<hr>
89
<h3>Codemirror Example2:</h3>
910
<codemirror :code.sync="css" :options="{ mode: 'text/css', tabSize: 2 }"></codemirror>
1011
<pre>{{ css }}</pre>
12+
<textarea v-model="css"></textarea>
1113
</div>
1214
</template>
1315

example/Example2.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<h3>Codemirror Example:</h3>
55
<codemirror :code="code" :options="editorOption" @changed="codeChange"></codemirror>
66
<pre>{{ code }}</pre>
7+
<textarea v-model="code"></textarea>
8+
<hr>
9+
<h3>Codemirror Example2:</h3>
10+
<codemirror v-model="code2" :options="editorOption"></codemirror>
11+
<pre>{{ code2 }}</pre>
12+
<textarea v-model="code2"></textarea>
713
</div>
814
</template>
915

@@ -15,7 +21,8 @@
1521
},
1622
data () {
1723
return {
18-
code: '<div></div>',
24+
code: '<div>1</div>',
25+
code2: '<div>2</div>',
1926
css: '.class { display: block }',
2027
editorOption: {
2128
tabSize: 4,

example/app.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
// Libs
88
import Vue from 'vue'
9-
import Router from 'vue-router'
10-
import Resource from 'vue-resource'
119

1210
// App
1311
import App from './App.vue'
@@ -19,8 +17,6 @@ import Example from './Example.vue'
1917
import CodeMirror from 'vue-codemirror'
2018

2119
// use
22-
Vue.use(Router)
23-
Vue.use(Resource)
2420
Vue.use(CodeMirror)
2521

2622
// router

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-codemirror",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Codemirror component for Vue.js(1.x ~ 2.x)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)