Skip to content

Commit 2cb23e6

Browse files
committed
将自定义设置保存到本地
1 parent ba6ec1b commit 2cb23e6

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/views/settings/code.vue

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@
1212
<span class="item-title title-xs">默认初始代码</span>
1313
<div v-for="(value, key, index) in prepList" :key="index" class="d-flex flex-clo code-area">
1414
<span class="code-area-title">{{form.prep[index]}}</span>
15-
<v-textarea solo :label="`${form.prep[index]} code...`" background-color="info" rows="3"
15+
<v-textarea :style="fontStyle" solo :label="`${form.prep[index]} code...`" background-color="info" rows="3"
1616
v-model="form.code[key]" hide-details>
1717
</v-textarea>
1818
</div>
1919
</div>
20+
<div class="code-item code-def-code d-flex flex-clo">
21+
<span class="item-title title-xs">默认 head</span>
22+
<div class="code-area">
23+
<v-textarea :style="fontStyle" solo label="输入你想在 <head> 中添加的标签如 <meta...>" background-color="info" rows="3"
24+
v-model="form.headTags" hide-details>
25+
</v-textarea>
26+
</div>
27+
</div>
2028
<div class="code-item code-def-indent d-flex flex-clo">
2129
<span class="item-title title-xs">代码缩进</span>
22-
<v-checkbox v-model="form.indent.replace" label="用等宽空格替换Tab"></v-checkbox>
30+
<v-checkbox v-model="form.indent.replace" label="用等宽空格替换Tab" @change="setIndentReplace"></v-checkbox>
2331
<div class="d-flex flex-ai">
2432
<span class="sub-title">缩进数</span>
2533
<v-slider v-model="form.indent.width" color="primary" thumb-label ticks="always" step="2" :max="8" hide-details
26-
@change="updateIndentSpaces">
34+
@change="setIndentWidth">
2735
</v-slider>
2836
</div>
2937
</div>
@@ -48,7 +56,7 @@
4856
</codemirror>
4957
</div>
5058
</div>
51-
<v-btn block x-large color="primary">保存</v-btn>
59+
<v-btn block x-large color="primary" @click="save">保存</v-btn>
5260
</div>
5361
</template>
5462

@@ -58,7 +66,7 @@ import { codemirror } from 'vue-codemirror'
5866
import '@assets/themes/default.css'
5967
import 'codemirror/mode/javascript/javascript'
6068
import 'codemirror/lib/codemirror.css'
61-
69+
import localStore from '@utils/local-storage'
6270
export default {
6371
data() {
6472
return {
@@ -86,9 +94,15 @@ export default {
8694
family: fontFamList[0],
8795
size: 16,
8896
},
97+
headTags: '',
8998
},
9099
}
91100
},
101+
created() {
102+
// 根据本地存储配置初始化表单
103+
const settings = JSON.parse(localStore.get('JSE_PERSONAL_SETTINGS'))
104+
settings && (this.form = settings)
105+
},
92106
mounted() {
93107
this.initEditor()
94108
},
@@ -103,12 +117,12 @@ export default {
103117
},
104118
methods: {
105119
initEditor() {
106-
const form = this.form
120+
const { width, replace } = this.form.indent
107121
this.codeOptions = {
108-
indentSpaces: form.indent.width,
122+
tabSize: width,
109123
mode: 'text/javascript',
110124
theme: 'default',
111-
indentWithTabs: !form.indent.replace,
125+
indentWithTabs: !replace,
112126
readOnly: 'nocursor',
113127
matchBrackets: false,
114128
scrollPastEnd: false,
@@ -117,12 +131,20 @@ export default {
117131
}
118132
this.editorCode = `function sum(a, b){\n\tconst res = a + b;\n\treturn res;\n}\nconsole.log(sum(1, 2))`
119133
},
120-
updateIndentSpaces(indentSpaces) {
121-
this.codeOptions.indentSpaces = indentSpaces
134+
setIndentWidth(width) {
135+
this.codeOptions.tabSize = width
136+
},
137+
setIndentReplace(replace) {
138+
this.codeOptions.indentWithTabs = !replace
122139
},
123140
refreshEditor() {
124141
this.$refs.editor.refresh()
125142
},
143+
save() {
144+
const settings = this.form
145+
localStore.set('JSE_PERSONAL_SETTINGS', JSON.stringify(settings))
146+
this.$message.success('编码设置保存成功!')
147+
},
126148
},
127149
components: {
128150
codemirror,

0 commit comments

Comments
 (0)