Skip to content

Commit bca32b2

Browse files
committed
接入ACE编辑器
1 parent 0d32dc5 commit bca32b2

File tree

7 files changed

+191
-23
lines changed

7 files changed

+191
-23
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"codemirror": "^5.58.3",
1616
"file-saver": "^2.0.5",
1717
"qrcode.vue": "^1.6.2",
18-
"vue": "^2.5.2"
18+
"vue": "^2.5.2",
19+
"vue2-ace-editor": "^0.0.15"
1920
},
2021
"devDependencies": {
2122
"autoprefixer": "^7.1.2",

src/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="app">
3-
<PythonEditor />
3+
<PythonEditor editorType="ace"/>
44
</div>
55
</template>
66

src/AppPlayer.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="player">
3-
<PythonPlayer />
3+
<PythonPlayer editorType="ace"/>
44
</div>
55
</template>
66

src/components/AceEditor.vue

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<template>
2+
<div>
3+
<editor
4+
v-model="code"
5+
@init="editorInit"
6+
:options="options"
7+
:lang="language"
8+
theme="monokai"
9+
:height="height"
10+
ref="aceEditor"
11+
/>
12+
</div>
13+
</template>
14+
15+
<script>
16+
import ace from "brace";
17+
export default {
18+
name: "AceEditor",
19+
components: {
20+
editor: require("vue2-ace-editor"),
21+
},
22+
props: {
23+
// 外部传入的内容,用于实现双向绑定
24+
value: {
25+
type: String,
26+
default: "",
27+
},
28+
//是否只读
29+
readOnly: {
30+
type: [Boolean, String],
31+
default: false,
32+
},
33+
// 外部传入的语法类型
34+
language: {
35+
type: String,
36+
default: "python",
37+
},
38+
height: {
39+
type: [Number, String],
40+
default: 800,
41+
},
42+
},
43+
data() {
44+
return {
45+
options: {
46+
readOnly: this.readOnly,
47+
enableBasicAutocompletion: true,
48+
enableSnippets: true,
49+
enableLiveAutocompletion: true,
50+
// enableBasicAutocompletion: [{
51+
// getCompletions: (editor, session, pos, prefix, callback) => {
52+
// // note, won't fire if caret is at a word that does not have these letters
53+
// callback(null, [
54+
// {name: 'hello', value: 'hello', score: 1, meta: 'some comment'},
55+
// {name: 'world', value: 'world', score: 1, meta: 'some other comment'},
56+
// ]);
57+
// },
58+
// }],
59+
},
60+
code: "",
61+
editor: null,
62+
};
63+
},
64+
mounted() {
65+
this.setCodeContent(this.value || this.code);
66+
console.log(this.editor.completers);
67+
// 加入自定义语法提示
68+
},
69+
methods: {
70+
editorInit(editor) {
71+
require("brace/ext/language_tools");
72+
require("brace/mode/python");
73+
require("brace/theme/tomorrow_night_blue");
74+
require("brace/theme/monokai");
75+
require("brace/snippets/python");
76+
77+
// editor.on('change', this.change);
78+
// editor.commands.addCommand({
79+
// name: "save",
80+
// bindKey: { win: "Ctrl-S", mac: "Command-S" },
81+
// exec: (editor) => this.$emit("save-change", this.value, editor),
82+
// });
83+
// editor.commands.addCommand({
84+
// name: "formatter",
85+
// bindKey: { win: "Ctrl-Shift-F", mac: "Command-Shift-F" },
86+
// exec: () => this.$emit("formatter", this.editor),
87+
// });
88+
let that = this;
89+
let langTools = ace.acequire("ace/ext/language_tools");
90+
var customCompleter = {
91+
getCompletions: function (editor, session, pos, prefix, callback) {
92+
that.setCompletions(editor, session, pos, prefix, callback);
93+
},
94+
};
95+
langTools.addCompleter(customCompleter);
96+
97+
this.editor = editor;
98+
},
99+
setCompletions(editor, session, pos, prefix, callback) {
100+
// ace自定义提示文字列表
101+
let data = [
102+
{ caption: "test", meta: "解释:测试关键词", value: "testKeyWord" },
103+
];
104+
if (prefix.length == 0) {
105+
return callback(null, []);
106+
} else {
107+
return callback(null, data);
108+
}
109+
},
110+
getCodeContent() {
111+
return this.code;
112+
},
113+
setCodeContent(val) {
114+
this.code = val;
115+
},
116+
},
117+
};
118+
</script>
119+
120+
<style>
121+
</style>

src/components/PythonEditor.vue

+30-10
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,20 @@
4646
<a-layout-content>
4747
<a-form-item>
4848
<JCodeEditor
49+
v-if="editorType == 'codemirror'"
4950
language="python"
5051
v-model="code"
5152
:fullScreen="true"
5253
placeholder="请输入代码"
5354
ref="codeEditor"
5455
/>
56+
<AceEditor
57+
v-if="editorType == 'ace'"
58+
language="python"
59+
v-model="code"
60+
:height="'calc(100vh - 74px)'"
61+
ref="codeEditor"
62+
/>
5563
</a-form-item>
5664
</a-layout-content>
5765
<a-layout-sider :collapsible="false" :width="500" :theme="'light'">
@@ -69,6 +77,7 @@
6977
<script>
7078
import { saveAs } from "file-saver";
7179
import JCodeEditor from "./JCodeEditor";
80+
import AceEditor from "./AceEditor";
7281
import "../python/skulpt.min.js";
7382
import "../python/skulpt-stdlib.js";
7483
@@ -80,6 +89,13 @@ export default {
8089
name: "PythonEditor",
8190
components: {
8291
JCodeEditor,
92+
AceEditor,
93+
},
94+
props: {
95+
editorType: {
96+
type: String,
97+
default: "ace", //ace codemirror
98+
},
8399
},
84100
data() {
85101
return {
@@ -90,7 +106,7 @@ export default {
90106
},
91107
mounted() {},
92108
created() {
93-
var that = this
109+
var that = this;
94110
//载入网络项目
95111
var url = this.urlParam("url");
96112
if (url) {
@@ -99,22 +115,22 @@ export default {
99115
this.downloadFile("./static/defaultPython.py");
100116
}
101117
//兼容载入项目事件
102-
window.document.addEventListener('loadPorject', function(e) {
103-
console.log("load project:"+e.detail.projectName);
104-
console.log(e.detail.url);
105-
that.projectName = e.detail.projectName
106-
that.downloadFile(e.detail.url)
118+
window.document.addEventListener("loadPorject", function (e) {
119+
console.log("load project:" + e.detail.projectName);
120+
console.log(e.detail.url);
121+
that.projectName = e.detail.projectName;
122+
that.downloadFile(e.detail.url);
107123
});
108124
},
109125
methods: {
110126
runit() {
111127
var that = this;
112128
this.clear();
113129
Sk.pre = "output";
114-
Sk.configure({ output: that.outf, read: that.builtinRead });
130+
Sk.configure({ output: that.terminalOut, read: that.builtinRead });
115131
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = "mycanvas";
116132
var myPromise = Sk.misceval.asyncToPromise(function () {
117-
return Sk.importMainWithBody("<stdin>", false, that.code, true);
133+
return Sk.importMainWithBody("<stdin>", false, that.getCode(), true);
118134
});
119135
myPromise.then(
120136
function (mod) {
@@ -167,11 +183,15 @@ export default {
167183
this.code = code;
168184
this.$refs.codeEditor.setCodeContent(code);
169185
},
186+
getCode() {
187+
this.code = this.$refs.codeEditor.getCodeContent();
188+
return this.code;
189+
},
170190
clear() {
171191
document.getElementById("output").innerHTML = "";
172192
document.getElementById("mycanvas").innerHTML = "";
173193
},
174-
outf(v) {
194+
terminalOut(v) {
175195
var mypre = document.getElementById("output");
176196
mypre.innerHTML = mypre.innerHTML + v;
177197
},
@@ -200,7 +220,7 @@ export default {
200220
.CodeMirror {
201221
/* Set height, width, borders, and global font properties here */
202222
font-family: monospace;
203-
height: calc(100vh - 64px) !important;
223+
height: calc(100vh - 74px) !important;
204224
color: black;
205225
direction: ltr;
206226
}

src/components/PythonPlayer.vue

+24-10
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@
1010
<div id="mycanvas"></div>
1111
<!-- 代码输出 -->
1212
<pre id="output">{{ out }}</pre>
13-
1413
<a-form-item>
1514
<JCodeEditor
15+
v-if="editorType == 'codemirror'"
16+
language="python"
17+
v-model="code"
18+
:fullScreen="true"
19+
readOnly="true"
20+
placeholder="请输入代码"
21+
ref="codeEditor"
22+
/>
23+
<AceEditor
24+
v-if="editorType == 'ace'"
1625
language="python"
1726
v-model="code"
18-
:readOnly="'nocursor'"
19-
:autoHeight="false"
20-
:height="'20rem'"
27+
readOnly="true"
28+
:height="500"
2129
ref="codeEditor"
2230
/>
2331
</a-form-item>
@@ -27,6 +35,7 @@
2735
<script>
2836
import { saveAs } from "file-saver";
2937
import JCodeEditor from "./JCodeEditor";
38+
import AceEditor from "./AceEditor";
3039
import "../python/skulpt.min.js";
3140
import "../python/skulpt-stdlib.js";
3241
@@ -38,6 +47,13 @@ export default {
3847
name: "PythonEditor",
3948
components: {
4049
JCodeEditor,
50+
AceEditor,
51+
},
52+
props: {
53+
editorType: {
54+
type: String,
55+
default: "ace", //ace codemirror
56+
},
4157
},
4258
data() {
4359
return {
@@ -51,9 +67,7 @@ export default {
5167
this.downloadFile(url);
5268
}
5369
},
54-
created() {
55-
56-
},
70+
created() {},
5771
methods: {
5872
runit() {
5973
var that = this;
@@ -161,14 +175,14 @@ export default {
161175
}
162176
.control {
163177
margin: 10px;
164-
button{
178+
button {
165179
margin: 10px 0;
166180
}
167181
}
168-
.ant-layout-header{
182+
.ant-layout-header {
169183
background: #dfefff;
170184
}
171-
.ant-layout-sider{
185+
.ant-layout-sider {
172186
padding: 10px;
173187
}
174188
</style>

yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,11 @@ brace-expansion@^1.1.7:
11141114
balanced-match "^1.0.0"
11151115
concat-map "0.0.1"
11161116

1117+
brace@^0.11.0:
1118+
version "0.11.1"
1119+
resolved "https://registry.yarnpkg.com/brace/-/brace-0.11.1.tgz#4896fcc9d544eef45f4bb7660db320d3b379fe58"
1120+
integrity sha1-SJb8ydVE7vRfS7dmDbMg07N5/lg=
1121+
11171122
braces@^2.3.1, braces@^2.3.2:
11181123
version "2.3.2"
11191124
resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729"
@@ -7099,6 +7104,13 @@ vue-template-es2015-compiler@^1.6.0:
70997104
resolved "https://registry.yarnpkg.com/vue-template-es2015-compiler/-/vue-template-es2015-compiler-1.9.1.tgz#1ee3bc9a16ecbf5118be334bb15f9c46f82f5825"
71007105
integrity sha512-4gDntzrifFnCEvyoO8PqyJDmguXgVPxKiIxrBKjIowvL9l+N66196+72XVYR8BBf1Uv1Fgt3bGevJ+sEmxfZzw==
71017106

7107+
vue2-ace-editor@^0.0.15:
7108+
version "0.0.15"
7109+
resolved "https://registry.yarnpkg.com/vue2-ace-editor/-/vue2-ace-editor-0.0.15.tgz#569b208e54ae771ae1edd3b8902ac42f0edc74e3"
7110+
integrity sha512-e3TR9OGXc71cGpvYcW068lNpRcFt3+OONCC81oxHL/0vwl/V3OgqnNMw2/RRolgQkO/CA5AjqVHWmANWKOtNnQ==
7111+
dependencies:
7112+
brace "^0.11.0"
7113+
71027114
vue@^2.5.2:
71037115
version "2.6.12"
71047116
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.12.tgz#f5ebd4fa6bd2869403e29a896aed4904456c9123"

0 commit comments

Comments
 (0)