Skip to content

Commit eae9659

Browse files
committed
extract mode select & theme select
Signed-off-by: zhangtianli2006 <zhangtianli2006@163.com>
1 parent 66b95f9 commit eae9659

File tree

2 files changed

+75
-55
lines changed

2 files changed

+75
-55
lines changed

src/components/app/editor.vue

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,81 @@
55
<i class="el-icon-document" />
66
Code Editor
77
</div>
8-
<codeMirror />
8+
<el-select
9+
v-model="mode"
10+
placeholder="Select Mode"
11+
style="margin-bottom: 10px;"
12+
>
13+
<el-option
14+
v-for="item in langTable"
15+
:key="item.value"
16+
:label="item.label"
17+
:value="item.value"
18+
/>
19+
</el-select>
20+
<el-select
21+
v-model="theme"
22+
placeholder="Select Theme"
23+
style="margin-bottom: 10px; margin-left: 20px;"
24+
>
25+
<el-option-group
26+
v-for="group in themeTable"
27+
:key="group.label"
28+
:label="group.label"
29+
>
30+
<el-option
31+
v-for="item in group.themes"
32+
:key="item.value"
33+
:label="item.label"
34+
:value="item.value"
35+
/>
36+
</el-option-group>
37+
</el-select>
38+
<codeMirror :mode="mode" :theme="theme" />
939
</el-card>
1040
</div>
1141
</template>
1242

1343
<script>
1444
import codeMirror from './../lib/editor.vue';
45+
import apiurl from './../../apiurl';
46+
import sfconfig from './../../sfconfig';
1547
1648
export default {
1749
name: 'CodeEditorContainer',
1850
data() {
1951
return {
20-
source: null
52+
mode: '-',
53+
theme: '-',
54+
langTable: sfconfig.codeMirrorModeTable,
55+
themeTable: sfconfig.CodeMirrorThemeTableOptions,
56+
CodeMirrorThemeTable: sfconfig.CodeMirrorThemeTable
2157
};
2258
},
59+
watch: {
60+
theme(val) {
61+
this.$axios
62+
.patch(apiurl('/account/' + this.$store.state.user.userid), {
63+
editor_theme: val
64+
});
65+
}
66+
},
67+
methods: {
68+
loadUserLangMode() {
69+
this.mode = this.langTable[sfconfig.langTable[this.$store.state.user.userlang].codeMirror].label;
70+
},
71+
loadUserTheme() {
72+
this.$axios
73+
.get(apiurl('/account/' + this.$store.state.user.userid))
74+
.then(res => {
75+
this.theme = String(res.data.res.editor_theme);
76+
});
77+
}
78+
},
79+
mounted() {
80+
this.loadUserLangMode();
81+
this.loadUserTheme();
82+
},
2383
components: {
2484
codeMirror
2585
}

src/components/lib/editor.vue

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
11
<template>
22
<div>
3-
<el-select
4-
v-model="mode"
5-
placeholder="Select Mode"
6-
style="margin-bottom: 10px;"
7-
>
8-
<el-option
9-
v-for="item in langTable"
10-
:key="item.value"
11-
:label="item.label"
12-
:value="item.value"
13-
/>
14-
</el-select>
15-
<el-select
16-
v-model="theme"
17-
placeholder="Select Theme"
18-
style="margin-bottom: 10px; margin-left: 20px;"
19-
>
20-
<el-option-group
21-
v-for="group in themeTable"
22-
:key="group.label"
23-
:label="group.label"
24-
>
25-
<el-option
26-
v-for="item in group.themes"
27-
:key="item.value"
28-
:label="item.label"
29-
:value="item.value"
30-
/>
31-
</el-option-group>
32-
</el-select>
333
<textarea ref="editor" v-model="source" />
344
</div>
355
</template>
366

377
<script>
38-
import apiurl from './../../apiurl';
398
import sfconfig from './../../sfconfig';
409
4110
import * as CodeMirror from 'codemirror/lib/codemirror';
@@ -91,30 +60,32 @@ export default {
9160
return {
9261
source: null,
9362
langTable: sfconfig.codeMirrorModeTable,
94-
editor: null,
95-
mode: '-',
96-
theme: '-',
97-
themeTable: sfconfig.CodeMirrorThemeTableOptions,
9863
CodeMirrorThemeTable: sfconfig.CodeMirrorThemeTable
9964
};
10065
},
66+
props: {
67+
mode: {
68+
type: String,
69+
default: 'text/x-c++src'
70+
},
71+
theme: {
72+
type: String,
73+
default: '3024-day'
74+
}
75+
},
10176
watch: {
10277
mode(val) {
10378
editor.setOption('mode', this.langTable[val].mode);
10479
},
10580
theme(val) {
10681
editor.setOption('theme', this.CodeMirrorThemeTable[val].theme);
107-
this.$axios
108-
.patch(apiurl('/account/' + this.$store.state.user.userid), {
109-
editor_theme: this.theme
110-
});
11182
}
11283
},
11384
methods: {
11485
loadEditor() {
11586
editor = CodeMirror.fromTextArea(this.$refs.editor, {
11687
theme: '3024-day',
117-
mode: 'text/x-markdown',
88+
mode: 'text/x-c++src',
11889
indentUnit: 4,
11990
smartIndent: true,
12091
indentWithTabs: false,
@@ -143,22 +114,11 @@ export default {
143114
editor.showHint();
144115
});
145116
},
146-
loadUserLangMode() {
147-
this.mode = this.langTable[sfconfig.langTable[this.$store.state.user.userlang].codeMirror].label;
148-
editor.setOption('mode', this.langTable[this.langTable[sfconfig.langTable[this.$store.state.user.userlang].codeMirror].value].mode);
149-
},
150-
loadUserTheme() {
151-
this.$axios
152-
.get(apiurl('/account/' + this.$store.state.user.userid))
153-
.then(res => {
154-
this.theme = String(res.data.res.editor_theme);
155-
});
156-
}
157117
},
158118
mounted() {
159119
this.loadEditor();
160-
this.loadUserLangMode();
161-
this.loadUserTheme();
120+
editor.setOption('mode', this.langTable[this.mode].mode);
121+
editor.setOption('theme', this.langTable[this.theme].mode);
162122
}
163123
};
164124
</script>

0 commit comments

Comments
 (0)