Skip to content

Commit 3dc6b78

Browse files
committed
init
0 parents  commit 3dc6b78

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# vue2-ace-bind
2+
3+
A Vue2 component for binding the [ace editor][1]
4+
5+
## Install
6+
7+
### NPM
8+
9+
```bash
10+
npm install vue2-ace-bind --save-dev
11+
```
12+
13+
### YARN
14+
15+
```bash
16+
yarn add vue2-ace-bind -D
17+
```
18+
19+
## How to use
20+
21+
### Import the component firstly
22+
23+
```javascript
24+
import aceEditor from "vue2-ace-bind";
25+
```
26+
27+
### Then import the language mode and theme which you want to use
28+
29+
```javascript
30+
import javascript from "brace/mode/javascript"
31+
import sh from "brace/mode/sh"
32+
```
33+
34+
```javascript
35+
import xcode from "brace/theme/xcode"
36+
import terminal from "brace/theme/terminal"
37+
```
38+
39+
The default config mode is bash and theme is terminal
40+
41+
Ace-editor's mode and theme list is here:
42+
43+
- [Mode][2]
44+
- [Theme][3]
45+
46+
Register the `component`.
47+
48+
```javascript
49+
components: {
50+
editor
51+
}
52+
53+
```
54+
55+
### Use the component in `template`
56+
57+
```javascript
58+
<editor :content="variable"></editor>
59+
```
60+
61+
The content is required,other props have the following defaults:
62+
63+
```javascript
64+
lang: sh
65+
theme: terminal
66+
height: 100%
67+
width: 100%
68+
options: {}
69+
```
70+
71+
if you want to configure the ace-editor,just define options in data.
72+
73+
```javascript
74+
<editor :content="variable" :options="options" ></editor>
75+
```
76+
77+
```javascript
78+
option: {
79+
showLineNumbers: false,
80+
showFoldWidgets: true,
81+
showGutter: false,
82+
displayIndentGuides: false,
83+
showPrintMargin: false
84+
....
85+
}
86+
87+
```
88+
89+
The official doc is here: [Configuring ACE][4].
90+
91+
[1]: https://ace.c9.io/
92+
[2]: https://github.com/ajaxorg/ace/tree/master/lib/ace/mode
93+
[3]: https://github.com/ajaxorg/ace/tree/master/lib/ace/theme
94+
[4]: https://github.com/ajaxorg/ace/wiki/Configuring-Ace

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "vue2-ace-bind",
3+
"version": "0.0.1",
4+
"description": "A simple vuejs 2 binding for ace-edit",
5+
"main": "vue2-ace.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/leozhang2018/vue2-ace-bind.git"
12+
},
13+
"keywords": [
14+
"vue",
15+
"vue2",
16+
"ace-editor"
17+
],
18+
"author": "leozhang2018",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/leozhang2018/vue2-ace-bind/issues"
22+
},
23+
"homepage": "https://github.com/leozhang2018/vue2-ace-bind#readme",
24+
"dependencies": {
25+
"brace": "^0.8.0",
26+
"emmet": "git+https://github.com/cloud9ide/emmet-core.git#41973fcc70392864c7a469cf5dcd875b88b93d4a"
27+
}
28+
}

vue2-ace.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var ace = require('brace');
2+
3+
require(['emmet/emmet'], function (data) {
4+
window.emmet = data.emmet;
5+
});
6+
7+
module.exports = {
8+
template: "<div :style=\"{height: height ? px(height) : '100%',width: width ? px(width) : '100%'}\"></div>",
9+
props: {
10+
value: {
11+
type: String,
12+
required: true
13+
},
14+
lang: String,
15+
theme: String,
16+
height: true,
17+
width: true,
18+
options: {
19+
type: Object,
20+
default: function () { return {}; }
21+
}
22+
},
23+
data: function () {
24+
return {
25+
editor: null,
26+
contentSwap: ""
27+
}
28+
},
29+
methods: {
30+
px: function (n) {
31+
if (/^\d*$/.test(n)) {
32+
return n + "px";
33+
}
34+
return n;
35+
}
36+
},
37+
watch: {
38+
value: function (val) {
39+
if (this.contentSwap !== val)
40+
this.editor.setValue(val, 1);
41+
}
42+
},
43+
mounted: function () {
44+
var vm = this;
45+
var lang = this.lang || 'sh';
46+
var theme = this.theme || 'terminal';
47+
require('brace/ext/emmet');
48+
var editor = vm.editor = ace.edit(this.$el);
49+
this.$emit('init', editor);
50+
51+
editor.$blockScrolling = Infinity;
52+
editor.setOptions(this.options);
53+
editor.getSession().setMode('ace/mode/' + lang);
54+
editor.setTheme('ace/theme/' + theme);
55+
editor.setValue(this.value, 1);
56+
57+
editor.on('change', function () {
58+
var content = editor.getValue();
59+
vm.$emit('input', content);
60+
vm.contentSwap = content;
61+
});
62+
63+
64+
}
65+
}

0 commit comments

Comments
 (0)