4
4
[ ![ GitHub forks] ( https://img.shields.io/github/forks/surmon-china/vue-codemirror.svg?style=flat-square )] ( https://github.com/surmon-china/vue-codemirror/network )
5
5
[ ![ GitHub last commit] ( https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square )] ( https://github.com/surmon-china/vue-codemirror )
6
6
[ ![ license] ( https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square )] ( https://github.com/surmon-china/vue-codemirror )
7
- [ ![ Twitter] ( https://img.shields.io/twitter/url/https/github.com/surmon-china/vue-codemirror.svg?style=flat-square )] ( https://twitter.com/intent/tweet?url=https://github.com/surmon-china/vue-codemirror )
8
7
9
8
[ ![ NPM] ( https://nodei.co/npm/vue-codemirror.png?downloads=true&downloadRank=true&stars=true )] ( https://nodei.co/npm/vue-codemirror/ )
10
9
[ ![ NPM] ( https://nodei.co/npm-dl/vue-codemirror.png?months=9&height=3 )] ( https://nodei.co/npm/vue-codemirror/ )
11
10
12
11
13
- # Vue-Codemirror
14
- [ Codemirror ] ( http://codemirror.net/ ) component for Vue.
12
+ ## vue-codemirror
13
+ [ CodeMirror ] ( http://codemirror.net/ ) component for Vue.
15
14
16
- 基于 [ Codemirror ] ( http://codemirror.net/ ) ,适用于 Vue 的 Web 代码编辑器。
15
+ 基于 [ CodeMirror ] ( http://codemirror.net/ ) ,适用于 Vue 的 Web 代码编辑器。
17
16
18
- # Example
17
+ ### Example
19
18
20
- [ Demo Page] ( https://surmon-china.github.io/vue-codemirror )
19
+ - [ Demo Page] ( https://surmon-china.github.io/vue-codemirror )
20
+ - [ CDN Example] ( https://jsfiddle.net/surmon/cp01hvq2/ )
21
+ - [ Nuxt.js/SSR example code] ( https://github.com/surmon-china/surmon-china.github.io/tree/source/projects/vue-codemirror/nuxt )
21
22
22
- [ CDN Example ] ( https://jsfiddle.net/u1f16q85/ )
23
+ ### Events
23
24
24
- [ Nuxt.js/SSR example code ] ( https://github.com/surmon-china/vue- codemirror/blob/master/examples/nuxt-ssr-example )
25
+ To make it easier to handle events, the component converts some codemirror built-in native events into a single vue component event, where you can listen for events from both the component itself and from codemirror. If you need to listen for more and more complex events, you can pass in the event names (Array) you need for the global ` Vue.use(, { events: [] }) ` and the component parameters ` :events ` , respectively, or by the ` this. codemirror.on(event, hanger) ` method of the codemirror instance. Here's a list of events:
25
26
26
- # Events
27
+ ** codemirror event list:**
28
+ - ` scroll `
29
+ - ` changes `
30
+ - ` beforeChange `
31
+ - ` cursorActivity `
32
+ - ` keyHandled `
33
+ - ` inputRead `
34
+ - ` electricInput `
35
+ - ` beforeSelectionChange `
36
+ - ` viewportChange `
37
+ - ` swapDoc `
38
+ - ` gutterClick `
39
+ - ` gutterContextMenu `
40
+ - ` focus `
41
+ - ` blur `
42
+ - ` refresh `
43
+ - ` optionChange `
44
+ - ` scrollCursorIntoView `
45
+ - ` update `
46
+
47
+ ** component event list:**
48
+ - ` ready `
49
+ - ` input `
50
+
51
+
52
+ ### Install
53
+
54
+ ** NPM**
27
55
28
- To make it easier to handle events, the component converts some codemirror built-in native events into a single vue component event, where you can listen for events from both the component itself and from codemirror. If you need to listen for more and more complex events, you can pass in the event names (Array) you need for the global ` Vue.use(, { events: [] }) ` and the component parameters ` :events ` , respectively, or by the ` this.codemirror.on(event, hanger) ` method of the codemirror instance. Here's a list of events:
56
+ ``` bash
57
+ npm install vue-codemirror --save
58
+
59
+ # or
60
+ yarn add vue-codemirror
61
+ ```
29
62
30
- ** codemirror events list:**
31
- - scroll
32
- - changes
33
- - beforeChange
34
- - cursorActivity
35
- - keyHandled
36
- - inputRead
37
- - electricInput
38
- - beforeSelectionChange
39
- - viewportChange
40
- - swapDoc
41
- - gutterClick
42
- - gutterContextMenu
43
- - focus
44
- - blur
45
- - refresh
46
- - optionChange
47
- - scrollCursorIntoView
48
- - update
49
-
50
- ** component events list:**
51
- - ready
52
- - input
53
-
54
-
55
- # Install
56
-
57
- #### CDN
63
+ ** CDN**
58
64
59
65
``` html
60
66
<link rel =" stylesheet" href =" path/to/codemirror/lib/codemirror.css" >
61
67
<script type =" text/javascript" src =" path/to/codemirror.js" ></script >
62
68
<script type =" text/javascript" src =" path/to/vue.min.js" ></script >
63
69
<script type =" text/javascript" src =" path/to/dist/vue-codemirror.js" ></script >
64
- <script type =" text/javascript" src =" path/to/codemirror/some-resources.js " ></script >
70
+ <script type =" text/javascript" src =" path/to/codemirror/{ some-resources} " ></script >
65
71
<script type =" text/javascript" >
66
72
Vue .use (window .VueCodemirror )
67
73
</script >
68
74
```
69
75
70
- #### NPM
71
-
72
- ``` bash
73
- npm install vue-codemirror --save
74
- ```
75
-
76
76
### Mount
77
77
78
- #### mount with global
78
+ ** mount with global**
79
79
80
80
``` javascript
81
- // require lib
82
81
import Vue from ' vue'
83
82
import VueCodemirror from ' vue-codemirror'
84
83
85
- // require styles
84
+ // import base styles
86
85
import ' codemirror/lib/codemirror.css'
87
86
88
- // require more codemirror resource...
87
+ // import more codemirror resource...
89
88
90
- // you can set default global options and events when use
91
- Vue .use (VueCodemirror, /* {
89
+ // you can set default global options and events when Vue. use
90
+ Vue .use (VueCodemirror, /* {
92
91
options: { theme: 'base16-dark', ... },
93
92
events: ['scroll', ...]
94
93
} */ )
95
94
```
96
95
97
- #### mount with component
96
+ ** mount with component**
98
97
99
98
``` javascript
100
- // require component
101
99
import { codemirror } from ' vue-codemirror'
102
100
103
- // require styles
101
+ // import base styles
104
102
import ' codemirror/lib/codemirror.css'
105
103
106
- // require more codemirror resource...
104
+ // import more codemirror resource...
107
105
108
- // component
109
106
export default {
110
107
components: {
111
108
codemirror
112
109
}
113
110
}
114
111
```
115
112
116
- #### mount with ssr
117
-
118
- ``` javascript
119
- // If used in nuxt.js/ssr, you should keep it only in browser build environment
120
- if (process .browser ) {
121
- const VueCodemirror = require (' vue-codemirror' )
122
- Vue .use (VueCodemirror)
123
- }
124
- ```
125
-
126
- #### defined codemirror mode
113
+ ** defined codemirror mode**
127
114
128
115
``` javascript
129
116
import CodeMirror from ' codemirror'
@@ -141,55 +128,60 @@ CodeMirror.defineMode('mymode', () => {
141
128
}
142
129
}
143
130
})
131
+
132
+ // Vue app...
144
133
```
145
134
146
135
### Component
147
136
148
137
``` vue
149
138
<template>
150
- <!-- bidirectional data binding(双向数据绑定) -->
151
- <codemirror v-model="code" :options="cmOptions"></codemirror>
152
-
153
- <!-- or to manually control the datasynchronization(或者手动控制数据流,需要像这样手动监听changed事件) -->
154
- <codemirror ref="myCm"
155
- :value="code"
156
- :options="cmOptions"
157
- @ready="onCmReady"
158
- @focus="onCmFocus"
159
- @input="onCmCodeChange">
160
- </codemirror>
161
-
162
- <!-- if Nust.js/SSR(如果在 Nuxt.js 环境下,需要外面包裹一层 no-ssr) -->
163
- <no-ssr placeholder="Codemirror Loading...">
164
- <codemirror ref="myCm"
165
- :value="code"
166
- :options="cmOptions"
167
- @ready="onCmReady"
168
- @focus="onCmFocus"
169
- @input="onCmCodeChange">
170
- </codemirror>
171
- </no-ssr>
139
+ <!-- Two-way Data-Binding -->
140
+ <codemirror v-model="code" :options="cmOptions" />
141
+
142
+ <!-- Or manually control the data synchronization -->
143
+ <codemirror
144
+ ref="cmEditor"
145
+ :value="code"
146
+ :options="cmOptions"
147
+ @ready="onCmReady"
148
+ @focus="onCmFocus"
149
+ @input="onCmCodeChange"
150
+ />
151
+
152
+ <!-- Nuxt.js -->
153
+ <client-only placeholder="Codemirror Loading...">
154
+ <codemirror
155
+ ref="cmEditor"
156
+ :value="code"
157
+ :options="cmOptions"
158
+ @ready="onCmReady"
159
+ @focus="onCmFocus"
160
+ @input="onCmCodeChange"
161
+ />
162
+ </client-only>
172
163
</template>
173
164
174
165
<script>
175
- // language js
166
+ // import language js
176
167
import 'codemirror/mode/javascript/javascript.js'
177
- // theme css
168
+
169
+ // import theme css
178
170
import 'codemirror/theme/base16-dark.css'
179
- // more codemirror resources
180
- // import 'codemirror/some-resource...'
171
+
172
+ // import more 'codemirror/some-resource...'
173
+
181
174
export default {
182
175
data () {
183
176
return {
184
177
code: 'const a = 10',
185
178
cmOptions: {
186
- // codemirror options
187
179
tabSize: 4,
188
180
mode: 'text/javascript',
189
181
theme: 'base16-dark',
190
182
lineNumbers: true,
191
183
line: true,
192
- // more codemirror options, 更多 codemirror 的高级配置 ...
184
+ // more codemirror options...
193
185
}
194
186
}
195
187
},
@@ -198,7 +190,7 @@ export default {
198
190
console.log('the editor is readied!', cm)
199
191
},
200
192
onCmFocus(cm) {
201
- console.log('the editor is focus !', cm)
193
+ console.log('the editor is focused !', cm)
202
194
},
203
195
onCmCodeChange(newCode) {
204
196
console.log('this is new code', newCode)
@@ -207,11 +199,11 @@ export default {
207
199
},
208
200
computed: {
209
201
codemirror() {
210
- return this.$refs.myCm .codemirror
202
+ return this.$refs.cmEditor .codemirror
211
203
}
212
204
},
213
205
mounted() {
214
- console.log('this is current codemirror object', this.codemirror)
206
+ console.log('the current codemirror instance object: ', this.codemirror)
215
207
// you can use this.codemirror to do something...
216
208
}
217
209
}
@@ -224,21 +216,25 @@ export default {
224
216
225
217
``` vue
226
218
<template>
227
- <codemirror :merge="true" :options="cmOption" @scroll="onCmScroll"></codemirror >
219
+ <codemirror :merge="true" :options="cmOption" @scroll="onCmScroll" / >
228
220
</template>
229
221
230
222
<script>
231
223
// merge js
232
224
import 'codemirror/addon/merge/merge.js'
225
+
233
226
// merge css
234
227
import 'codemirror/addon/merge/merge.css'
228
+
235
229
// google DiffMatchPatch
236
230
import DiffMatchPatch from 'diff-match-patch'
231
+
237
232
// DiffMatchPatch config with global
238
233
window.diff_match_patch = DiffMatchPatch
239
234
window.DIFF_DELETE = -1
240
235
window.DIFF_INSERT = 1
241
236
window.DIFF_EQUAL = 0
237
+
242
238
export default {
243
239
data() {
244
240
return {
@@ -263,11 +259,9 @@ export default {
263
259
</script>
264
260
```
265
261
266
-
267
262
### Codemirror language mode types
268
- 编辑器的模式(mode属性)分为 字符串、对象两种方式,Codemirror 官方文档有说明
269
263
270
- ` mode: ' string' || object `
264
+ Codemirror language mode have [ string | object] ( https://codemirror.net/doc/manual.html#option_mode ) types.
271
265
272
266
``` javascript
273
267
// MIME types
@@ -295,14 +289,10 @@ mode: {
295
289
}
296
290
```
297
291
298
- # CodeMirror
292
+ ### CodeMirror
299
293
300
294
- [ CodeMirror language modes] ( http://codemirror.net/mode/ ) (MIME types defined)
301
295
- [ CodeMirror Autoresize] ( https://codemirror.net/demo/resize.html )
302
296
- [ CodeMirror themes] ( http://codemirror.net/demo/theme.html )
303
297
- [ CodeMirror events] ( https://codemirror.net/doc/manual.html#events )
304
298
- [ CodeMirror APIs] ( http://codemirror.net/doc/manual.html#config )
305
-
306
-
307
- # Author
308
- [ Surmon] ( https://surmon.me )
0 commit comments