Skip to content

Commit dcb2227

Browse files
author
unknown
committed
Update to V2.1.5
1 parent 31c9dbf commit dcb2227

22 files changed

+1258
-282
lines changed

.npmignore

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# Dependency directories
22
node_modules
33

4-
example
5-
/example
6-
example/
7-
8-
screenshots
9-
screenshots/
10-
/screenshots
4+
examples
5+
/examples

README.md

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88

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

12+
> ### V2.1.5
13+
> 增加代码补全提示、选中高亮、整行高亮、(sublime、emacs、vim)三种键位模式、代码块折叠、vue编码的支持...
14+
15+
> ### V2.1.3
16+
> 重构example页面,优化释放方法,修复部分小问题
17+
18+
> ### V2.1.2
19+
> add unseen line marker
1220
1321

1422
# Example
@@ -28,15 +36,13 @@ npm install vue-codemirror --save
2836
### Vue use
2937

3038
``` javascript
31-
// import with ES6
39+
// import in ES6
3240
import Vue from 'vue'
33-
// ...
3441
import CodeMirror from 'vue-codemirror'
3542

3643

37-
// require with Webpack/Node.js
44+
// require in Webpack/Node.js
3845
var Vue = require('vue')
39-
// ...
4046
var CodeMirror = require('vue-codemirror')
4147

4248

@@ -49,10 +55,9 @@ Vue.use(CodeMirror)
4955

5056
// or use with component(ES6)
5157
import Vue from 'vue'
52-
// ...
5358
import { codemirror } from 'vue-codemirror'
5459

55-
// use
60+
// use in component
5661
export default {
5762
components: {
5863
codemirror
@@ -64,22 +69,31 @@ export default {
6469
### Use in components
6570

6671
``` html
72+
<!-- 基础使用 -->
6773
<codemirror></codemirror>
6874

6975

70-
<!-- component data bind and config in Vue.js1.X -->
76+
<!-- use in vue1.0(Vue1中应用) -->
77+
78+
<!-- component data bind and config(基础配置/父->子单向数据流) -->
7179
<codemirror :code="code" :options="editorOption"></codemirror>
72-
<!-- Bidirectional data binding in Vue.js1.X -->
80+
<!-- Bidirectional data binding(双向数据绑定) -->
7381
<codemirror :code.sync="code" :options="editorOption"></codemirror>
82+
<!-- 也可以这样简约配置 -->
83+
<codemirror :code.sync="code" :options="{ tabSize: 2, mode: 'text/css' }"></codemirror>
84+
7485

86+
<!-- use in vue2.0(Vue2中应用) -->
7587

76-
<!-- component data bind and config in Vue.js2.X -->
88+
<!-- component data bind and config(基础配置/父->子单向数据流) -->
7789
<codemirror :code="code" :options="editorOption"></codemirror>
78-
<!-- Bidirectional data binding in Vue.js2.X -->
90+
<!-- Bidirectional data binding(双向数据绑定) -->
7991
<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 -->
92+
<!-- If you need to manually control the data synchronization, you can monitor the code change event like this(如果你需要手动控制数据流,就需要像这样手动监听changed事件) -->
8293
<codemirror :code="code" :options="editorOption" @changed="codeChange"></codemirror>
94+
95+
<!-- 如果需要代码提示功能,要在组件传入hint属性为true,如果需要在后续获取codemirror对象做什么事,应该在这里定义一个ref属性 -->
96+
<codemirror v-model="code" :options="editorOption" :hint="true" ref="myEditor"></codemirror>
8397
```
8498

8599

@@ -90,49 +104,63 @@ export default {
90104
return {
91105
code: 'const a = 10',
92106
editorOption: {
107+
// 下面所有配置同Codemirror配置,均为可选
93108
tabSize: 4,
94109
mode: 'text/javascript',
95110
theme: 'base16-dark',
96111
lineNumbers: true,
97112
line: true,
98-
...
113+
// sublime、emacs、vim三种键位模式,支持你的不同操作习惯
114+
keyMap: "sublime",
115+
// 按键映射,比如Ctrl键映射autocomplete,autocomplete是hint代码提示事件
116+
extraKeys: { "Ctrl": "autocomplete" },
117+
// 代码折叠
118+
foldGutter: true,
119+
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
120+
// 选中文本自动高亮,及高亮方式
121+
styleSelectedText: true,
122+
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: true },
123+
// more codemirror config...
124+
// 如果有hint方面的配置,也应该出现在这里
99125
}
100126
}
101127
},
102128
// If you need to manually control the data synchronization, parent component needs to explicitly emit an event instead of relying on implicit binding
129+
// 在2.0中如果需要手动控制数据同步,父组件需要显式地处理changed事件
103130
methods: {
104131
codeChange(newCode) {
105-
console.log(newCode)
132+
console.log('this is new code', newCode)
106133
this.code = newCode
107134
}
135+
},
136+
// example code (if you need to get the current editor object, you can find the editor object like this, the $ref object is a ref attribute corresponding to the dom redefined)
137+
// 如果你需要得到当前的editor对象来做一些事情,你可以像下面这样定义一个方法属性来获取当前的editor对象,实际上这里的$refs对应的是当前组件内所有关联了ref属性的组件元素对象
138+
computed: {
139+
editor() {
140+
return this.$refs.myEditor.editor
141+
}
142+
},
143+
mounted() {
144+
// you can use current editor object to do something(editor methods)
145+
// 然后你就可以使用当前上下文内的editor对象去做你想做的事了
146+
console.log('this is current editor object', this.editor)
147+
// this.editor to do something...
108148
}
109149
}
110150

111-
// editorOption mode types:
151+
// editor options mode types:
152+
// 编辑器的模式(mode属性)分为 字符串、对象两种方式,可以在下面的相关链接中找到语言列表
112153

113-
// string mode
154+
// string mode(MIME types/字符串方式)
114155
mode: 'text/javascript'
115156

116-
// object mode
157+
// object mode(对象方式)
117158
mode: {
118159
name: 'javascript',
119160
json: true
120161
}
121162
```
122163

123-
``` html
124-
<!-- component config example 2 (Vue.js1.X) -->
125-
<codemirror :code.sync="css" :options="{ tabSize: 2, mode: 'text/css' }"></codemirror>
126-
```
127-
128-
``` javascript
129-
data () {
130-
return {
131-
css: '.class { display: block }'
132-
}
133-
}
134-
```
135-
136164

137165
# More Config
138166

codemirror.vue

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
var CodeMirror = require('codemirror/lib/codemirror.js')
77
var CodeMirrorMetas = require('./metas.js')
88
require('codemirror/lib/codemirror.css')
9-
// var
9+
require('codemirror/addon/display/fullscreen.css')
10+
require('codemirror/addon/display/fullscreen.js')
1011
export default {
1112
data: function() {
1213
return {
1314
content: ''
1415
}
1516
},
1617
props: {
18+
hint: Boolean,
1719
code: String,
1820
value: String,
1921
unseenLines: Array,
@@ -34,33 +36,87 @@
3436
this.options = this.options || {}
3537
var language = this.options.mode || 'text/javascript'
3638
var theme = this.options.theme
37-
38-
// string config or object config
39-
var isString = (typeof language == 'string')
40-
// console.log(language, typeof language, isString)
39+
var hint = this.hint || false
40+
var hints = ['css', 'html', 'javascript', 'sql', 'xml']
4141
4242
// string config
43-
if (isString) {
43+
if (typeof language == 'string') {
4444
try {
4545
language = CodeMirrorMetas.findModeByMIME(language).mode
4646
} catch (e) {
47-
throw new Error('CodeMirror language mode: ' + language + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
47+
throw new Error('CodeMirror language mode: ' + language + ' configuration error (CodeMirror语言模式配置错误,或者不支持此语言) See http://codemirror.net/mode/ for more details.')
4848
}
4949
}
5050
5151
// object config
52-
if (!isString) {
52+
if (typeof language == 'object') {
5353
try {
5454
language = CodeMirrorMetas.findModeByName(language.name).mode
5555
} catch (e) {
56-
throw new Error('CodeMirror language mode: ' + language.name + ' Configuration error (CodeMirror语言模式配置错误,或者不支持此语言)')
56+
throw new Error('CodeMirror language mode: ' + language.name + ' configuration error (CodeMirror语言模式配置错误,或者不支持此语言) See http://codemirror.net/mode/ for more details.')
5757
}
5858
}
5959
60-
// console.log(language)
61-
// require editor language and theme config
60+
// require hint config
61+
if (hint) {
62+
require('codemirror/addon/hint/show-hint.js')
63+
require('codemirror/addon/hint/show-hint.css')
64+
var isAnyword = hints.indexOf(language) == -1
65+
require('codemirror/addon/hint/' + (isAnyword ? 'anyword' : language) + '-hint.js')
66+
}
67+
68+
// require active-line.js
69+
if (this.options.styleActiveLine) require('codemirror/addon/selection/active-line.js')
70+
71+
// require closebrackets.js
72+
if (this.options.autoCloseBrackets) require('codemirror/addon/edit/closebrackets.js')
73+
74+
// require closetag.js
75+
if (this.options.autoCloseTags) require('codemirror/addon/edit/closetag.js')
76+
77+
// require styleSelectedText.js
78+
if (this.options.styleSelectedText) {
79+
require('codemirror/addon/selection/mark-selection.js')
80+
require('codemirror/addon/search/searchcursor.js')
81+
}
82+
83+
// highlightSelectionMatches
84+
if (this.options.highlightSelectionMatches) {
85+
require('codemirror/addon/scroll/annotatescrollbar.js')
86+
require('codemirror/addon/search/matchesonscrollbar.js')
87+
require('codemirror/addon/search/searchcursor.js')
88+
require('codemirror/addon/search/match-highlighter.js')
89+
}
90+
91+
// require emacs
92+
if (!!this.options.keyMap && ['emacs', 'sublime', 'vim'].indexOf(this.options.keyMap) > -1) {
93+
require('codemirror/mode/clike/clike.js')
94+
require('codemirror/addon/edit/matchbrackets.js')
95+
require('codemirror/addon/comment/comment.js')
96+
require('codemirror/addon/dialog/dialog.js')
97+
require('codemirror/addon/dialog/dialog.css')
98+
require('codemirror/addon/search/searchcursor.js')
99+
require('codemirror/addon/search/search.js')
100+
// console.log(this.options.keyMap)
101+
require('codemirror/keymap/'+ this.options.keyMap +'.js')
102+
}
103+
104+
// require fold js
105+
if (this.options.foldGutter) {
106+
require('codemirror/addon/fold/foldgutter.css')
107+
require('codemirror/addon/fold/brace-fold.js')
108+
require('codemirror/addon/fold/comment-fold.js')
109+
require('codemirror/addon/fold/foldcode.js')
110+
require('codemirror/addon/fold/foldgutter.js')
111+
require('codemirror/addon/fold/indent-fold.js')
112+
require('codemirror/addon/fold/markdown-fold.js')
113+
require('codemirror/addon/fold/xml-fold.js')
114+
}
115+
116+
// require language mode config
62117
require('codemirror/mode/' + language + '/' + language + '.js')
63118
119+
// require theme config
64120
if (!!theme && theme == 'solarized light') theme = 'solarized'
65121
if (!!theme && theme != 'default') require('codemirror/theme/' + theme + '.css')
66122
},
@@ -124,9 +180,8 @@
124180
</script>
125181

126182
<style>
127-
.CodeMirror,
128-
.CodeMirror pre {
129-
font-family: Menlo, Monaco, Consolas, "Courier New", monospace!important;
130-
padding: 0 20px !important; /* Horizontal padding of content */
183+
.CodeMirror-code {
184+
line-height: 1.6em;
185+
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
131186
}
132187
</style>

example/App.vue

Lines changed: 0 additions & 11 deletions
This file was deleted.

example/app.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)