Skip to content

Commit f50f85a

Browse files
author
unknown
committed
Update
1 parent 894929e commit f50f85a

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

README.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77

88

99
# Vue-Codemirror
10-
Codemirror components for Vue.js
10+
Codemirror component for Vue.js(1.x ~ 2.x),本组件基于 [Codemirror](http://codemirror.net/)构建,支持Vue全版本使用,支持100+多种语言、分支语言、语法,支持多种代码高亮theme,并可以自行配置,可使用Vue-Codemirror快速轻易构建出多种丰富全面的web code editor,并以此基础多次开发WebIDE,欢迎加入前端技术交流群:288325802
11+
1112

1213

1314
# Example
1415

15-
[Demos](https://surmon-china.github.io/vue-codemirror)
16+
[Demo Page](https://surmon-china.github.io/vue-codemirror)
1617

1718

1819
# Use Setup
1920

21+
22+
### install vue-codemirror
23+
2024
``` bash
21-
# install vue-codemirror
2225
npm install vue-codemirror --save
2326
```
2427

@@ -39,6 +42,22 @@ var CodeMirror = require('vue-codemirror')
3942

4043
// use
4144
Vue.use(CodeMirror)
45+
46+
47+
// --------------------------------------
48+
49+
50+
// or use with component(ES6)
51+
import Vue from 'vue'
52+
// ...
53+
import { codemirror } from 'vue-codemirror'
54+
55+
// use
56+
export default {
57+
components: {
58+
codemirror
59+
}
60+
}
4261
```
4362

4463

@@ -48,12 +67,16 @@ Vue.use(CodeMirror)
4867
<codemirror></codemirror>
4968

5069

51-
<!-- component data bind -->
70+
<!-- component data bind(Vue.js1.X) -->
5271
<codemirror :code.sync="code"></codemirror>
5372

5473

55-
<!-- component config example 1 -->
74+
<!-- component config example 1(Vue.js1.X) -->
5675
<codemirror :code.sync="code" :options="editorOption"></codemirror>
76+
77+
78+
<!-- in vue.js2.X .once and .sync are deprecated, parent component needs to explicitly emit an event instead of relying on implicit binding -->
79+
<codemirror :code="code" :options="editorOption" @changed="codeChange"></codemirror>
5780
```
5881

5982

@@ -72,10 +95,16 @@ export default {
7295
...
7396
}
7497
}
98+
},
99+
// if you use in vue2.X,parent component needs to explicitly emit an event instead of relying on implicit binding
100+
methods: {
101+
codeChange(newCode) {
102+
console.log(newCode)
103+
this.code = newCode
104+
}
75105
}
76106
}
77107

78-
79108
// editorOption mode types:
80109

81110
// string mode
@@ -89,7 +118,7 @@ mode: {
89118
```
90119

91120
``` html
92-
<!-- component config example 2 -->
121+
<!-- component config example 2(Vue.js1.X) -->
93122
<codemirror :code.sync="css" :options="{ tabSize: 2, mode: 'css' }"></codemirror>
94123
```
95124

@@ -106,11 +135,11 @@ data () {
106135

107136
[Code example](https://github.com/surmon-china/vue-codemirror/tree/master/example)
108137

109-
[More codemirror configs](http://codemirror.net/doc/manual.html#config)
138+
[Codemirror config APIs](http://codemirror.net/doc/manual.html#config)
110139

111-
[More codemirror themes](http://codemirror.net/demo/theme.html)
140+
[Codemirror themes](http://codemirror.net/demo/theme.html)
112141

113-
[More codemirror language modes](http://codemirror.net/mode/)
142+
[Codemirror language modes](http://codemirror.net/mode/)
114143

115144

116145

codemirror.vue

Lines changed: 17 additions & 4 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-
9+
// var
1010
export default {
1111
data: function() {
1212
return {
@@ -70,11 +70,24 @@
7070
_this.code = cm.getValue()
7171
})
7272
},
73+
mounted: function() {
74+
var _this = this
75+
this.editor = CodeMirror.fromTextArea(this.$el, this.options)
76+
this.editor.setValue(this.code || this.content)
77+
this.editor.on('change', function(cm) {
78+
_this.content = cm.getValue()
79+
if (!!_this.$emit) {
80+
_this.$emit('changed', _this.content)
81+
}
82+
})
83+
},
7384
watch: {
7485
'code': function(newVal, oldVal) {
75-
// console.log('update', newVal)
76-
// this.editor.setValue(newVal)
77-
// this.content = newVal
86+
const editor_value = this.editor.getValue()
87+
if (newVal !== editor_value) {
88+
this.editor.setValue(newVal)
89+
this.content = newVal
90+
}
7891
}
7992
}
8093
}

example/Example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="Example">
3-
<h1>It's Example page</h1>
3+
<h1>It's Example page with Vue.js1.X</h1>
44
<h3>Codemirror Example1:</h3>
55
<codemirror :code.sync="code" :options="editorOption"></codemirror>
66
<pre>{{ code }}</pre>

example/Example2.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<div class="Example">
3+
<h1>It's Example page with Vue.js2.X</h1>
4+
<h3>Codemirror Example:</h3>
5+
<codemirror :code="code" :options="editorOption" @changed="codeChange"></codemirror>
6+
<pre>{{ code }}</pre>
7+
</div>
8+
</template>
9+
10+
<script>
11+
export default {
12+
data () {
13+
return {
14+
code: '<div></div>',
15+
css: '.class { display: block }',
16+
editorOption: {
17+
tabSize: 4,
18+
styleActiveLine: true,
19+
line: true,
20+
mode: 'text/html',
21+
lineWrapping: true,
22+
theme: 'base16-dark'
23+
}
24+
}
25+
},
26+
methods: {
27+
codeChange(newCode) {
28+
console.log(newCode)
29+
this.code = newCode
30+
}
31+
}
32+
}
33+
</script>

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
/**
22
* Vue-CodeMirror
33
* @author Surmon.me
4-
* @date 2016-9-22
54
*/
65

76
var CmComponent = require('./codemirror.vue')
8-
9-
var codemirror = {
7+
var Codemirror = {
8+
codemirror: CmComponent,
109
install: function(Vue) {
1110
Vue.component('codemirror', CmComponent)
1211
}
1312
}
1413

15-
module.exports = codemirror
14+
module.exports = Codemirror

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue-codemirror",
3-
"version": "1.3.6",
4-
"description": "Codemirror component for Vue.js",
3+
"version": "2.0.0",
4+
"description": "Codemirror component for Vue.js(1.x ~ 2.x)",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)