提交 c9709b67 编写于 作者: B baiy 提交者: ninecents

update codeEditor

上级 1767f6fd
...@@ -4,7 +4,6 @@ import './statics/theme.less' ...@@ -4,7 +4,6 @@ import './statics/theme.less'
import router from './tool.router' import router from './tool.router'
import optionBlock from './components/optionBlock' import optionBlock from './components/optionBlock'
import {plugin as modelPlugin} from './tool/model' import {plugin as modelPlugin} from './tool/model'
import codeEditor from "./views/tool/components/codeEditor";
import cache from './tool/cache' import cache from './tool/cache'
import setting from './tool/setting' import setting from './tool/setting'
import App from './tool.vue' import App from './tool.vue'
...@@ -19,7 +18,6 @@ const run = () => { ...@@ -19,7 +18,6 @@ const run = () => {
Vue.use(ViewUI) Vue.use(ViewUI)
Vue.use(modelPlugin) Vue.use(modelPlugin)
Vue.component('option-block', optionBlock); Vue.component('option-block', optionBlock);
Vue.component('code-editor', codeEditor);
new Vue({ new Vue({
router, router,
......
<template> <template>
<div> <div>
<div style="border: 1px solid #dcdee2; border-radius: 4px"> <div style="border: 1px solid #dcdee2; border-radius: 4px">
<code-editor ref="editor" v-model="current.content" auto-height="220" :language="this.current.lang"></code-editor> <code-editor ref="editor" v-model="current.content" :auto-height="220" :language="this.current.lang"></code-editor>
</div> </div>
<option-block> <option-block>
<FormItem> <FormItem>
...@@ -25,7 +25,11 @@ ...@@ -25,7 +25,11 @@
</template> </template>
<script> <script>
import _ from "lodash"; import _ from "lodash";
import codeEditor from "./components/codeEditor";
export default { export default {
components: {
codeEditor,
},
computed:{ computed:{
buttonLang(){ buttonLang(){
let data = _.slice(this.lang,0,8) let data = _.slice(this.lang,0,8)
...@@ -35,21 +39,25 @@ export default { ...@@ -35,21 +39,25 @@ export default {
return data return data
} }
}, },
created() {
this.current = Object.assign(this.current, this.$getToolData("content"))
},
methods: { methods: {
handle(language) { handle(language) {
this.current.lang = language;
setTimeout(()=>{
if (this.current.content) { if (this.current.content) {
let oldContent = this.current.content; try {
this.$refs.editor.getEditor().getAction('editor.action.formatDocument').run(); this.current.lang = language;
setTimeout(()=>{ this.$refs.editor.format(language);
if (oldContent !== this.current.content){
this.$saveToolData(this.current); this.$saveToolData(this.current);
return this.$Message.success('格式化完成'); return this.$Message.success('格式化完成');
} }
},300) catch (e) {
return this.$Modal.error({
title:"格式化错误",
content:`${e.message}`
});
}
} }
},200)
}, },
}, },
data() { data() {
...@@ -73,6 +81,7 @@ export default { ...@@ -73,6 +81,7 @@ export default {
"less", "less",
"graphql", "graphql",
"markdown", "markdown",
"vue",
], ],
}; };
}, },
......
<template> <template>
<div ref="container" class="monaco-editor" <div ref="container" class="code-editor" :style="`height:${containerHeight};width:${width}`"></div>
:style="`height:${containerHeight};width:${width}`">
</div>
</template> </template>
<script> <script>
import formatter from "../library/formatter"; import formatter from "../library/formatter";
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
// 注册文本格式化服务 const allowFormatterLanguage = {
// html/typescript/javascript/json 使用内置服务 html: "html",
const allowFormatterLanguage = [ typescript: "ts",
{id: "css", format: "css"}, javascript: "js",
{id: "graphql", format: "graphql"}, json: "json",
{id: "java", format: "java"}, graphql: "graphql",
{id: "markdown", format: "markdown"}, java: "java",
{id: "php", format: "php"}, markdown: "markdown",
{id: "scss", format: "scss"}, php: "php",
{id: "sql", format: "sql"}, css: "css",
{id: "xml", format: "xml"}, scss: "scss",
{id: "yaml", format: "yaml"}, less: "less",
] sql: "sql",
for (let lang of allowFormatterLanguage) { xml: "xml",
monaco.languages.registerDocumentFormattingEditProvider(lang.id, { yaml: "yaml",
provideDocumentFormattingEdits(model) { vue: "vue",
return [
{
range: model.getFullModelRange(),
text: formatter(model.getValue(), lang.format),
},
];
}
});
} }
export default { export default {
...@@ -43,6 +33,10 @@ export default { ...@@ -43,6 +33,10 @@ export default {
type: String, type: String,
default: "" default: ""
}, },
autoHeight: {
type: Number,
default: 0
},
theme: { theme: {
type: String, type: String,
default: 'vs' default: 'vs'
...@@ -51,10 +45,6 @@ export default { ...@@ -51,10 +45,6 @@ export default {
type: Boolean, type: Boolean,
default: true default: true
}, },
autoHeight: {
type: Number,
default: 0
},
height: { height: {
type: String, type: String,
default: "350px" default: "350px"
...@@ -108,12 +98,20 @@ export default { ...@@ -108,12 +98,20 @@ export default {
automaticLayout: true automaticLayout: true
}) })
this.editor.onDidChangeModelContent(() => { this.editor.onDidChangeModelContent(() => {
if (this.value !== this.editor.getValue()){
this.$emit('input', this.editor.getValue()) this.$emit('input', this.editor.getValue())
}
}) })
}, },
/** @return monaco.editor.IStandaloneCodeEditor*/ /** @return monaco.editor.IStandaloneCodeEditor*/
getEditor() { getEditor() {
return this.editor return this.editor
},
format(lang,option = {}) {
if (!(lang in allowFormatterLanguage)){
throw new Error("当前代码无法格式化");
}
this.$emit('input', formatter(this.editor.getValue(), allowFormatterLanguage[lang],option))
} }
} }
}; };
......
...@@ -22,8 +22,12 @@ ...@@ -22,8 +22,12 @@
<script> <script>
import axios from "axios" import axios from "axios"
import _ from "lodash" import _ from "lodash"
import codeEditor from "./components/codeEditor";
export default { export default {
components: {
codeEditor,
},
created() { created() {
this.current = Object.assign(this.current, this.$getToolData("input")) this.current = Object.assign(this.current, this.$getToolData("input"))
}, },
...@@ -36,6 +40,7 @@ export default { ...@@ -36,6 +40,7 @@ export default {
}).then(({data}) => { }).then(({data}) => {
this.current.output = JSON.stringify(_.isArray(data) && data.length < 2 ? data[0] : data,null, 4); this.current.output = JSON.stringify(_.isArray(data) && data.length < 2 ? data[0] : data,null, 4);
this.$saveToolData(this.current); this.$saveToolData(this.current);
this.$Message.success("查询成功")
}).catch((error) => { }).catch((error) => {
return this.$Message.error("ip地址信息查询错误:" + error); return this.$Message.error("ip地址信息查询错误:" + error);
}); });
......
...@@ -14,8 +14,12 @@ ...@@ -14,8 +14,12 @@
</template> </template>
<script> <script>
import Unicode from "./library/unicode" import Unicode from "./library/unicode"
import codeEditor from "./components/codeEditor";
export default { export default {
components: {
codeEditor,
},
created () { created () {
this.current = Object.assign(this.current, this.$getToolData('content')) this.current = Object.assign(this.current, this.$getToolData('content'))
}, },
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<Col span="10"> <Col span="10">
<Card :padding="0"> <Card :padding="0">
<p slot="title">JSON内容</p> <p slot="title">JSON内容</p>
<code-editor v-model="current.json" auto-height="330" language="json"></code-editor> <code-editor v-model="current.json" :auto-height="330" language="json"></code-editor>
</Card> </Card>
<option-block> <option-block>
<FormItem> <FormItem>
...@@ -28,18 +28,22 @@ ...@@ -28,18 +28,22 @@
@click="handle(item)">{{ item }} @click="handle(item)">{{ item }}
</Button> </Button>
</template> </template>
<code-editor v-model="current.output" auto-height="220" :language="languages[current.type]"></code-editor> <code-editor v-model="current.output" :auto-height="220" :language="languages[current.type]"></code-editor>
</Card> </Card>
</Col> </Col>
</Row> </Row>
</template> </template>
<script> <script>
import codeEditor from "./components/codeEditor";
import json2Go from './library/json2Go' import json2Go from './library/json2Go'
import json2CSharp from './library/json2CSharp' import json2CSharp from './library/json2CSharp'
import json2Java from './library/json2Java' import json2Java from './library/json2Java'
import json2Dart from './library/json2Dart' import json2Dart from './library/json2Dart'
export default { export default {
components: {
codeEditor,
},
created() { created() {
this.current = Object.assign(this.current, this.$getToolData()) this.current = Object.assign(this.current, this.$getToolData())
}, },
......
...@@ -25,15 +25,17 @@ const options = { ...@@ -25,15 +25,17 @@ const options = {
yaml: {parser: "yaml", plugins: [parserYaml]}, yaml: {parser: "yaml", plugins: [parserYaml]},
html: {parser: "html", plugins: [parserHtml]}, html: {parser: "html", plugins: [parserHtml]},
angular: {parser: "angular", plugins: [parserHtml]}, angular: {parser: "angular", plugins: [parserHtml]},
json: {parser: "json5", plugins: [parserJson5]}, json: {parser: "json5", plugins: [parserJson5], quoteProps: "preserve", trailingComma: "none"},
xml: {parser: "xml", plugins: [parserXml]}, xml: {parser: "xml", plugins: [parserXml]},
sql: {parser: "sql", plugins: [parserSql]}, sql: {parser: "sql", plugins: [parserSql]},
php: {parser: "php", plugins: [parserPhp]}, php: {parser: "php", plugins: [parserPhp]},
java: {parser: "java", plugins: [parserJava]}, java: {parser: "java", plugins: [parserJava]},
}; };
export default (code, lang) => { export default (code, lang, {tab = 4}) => {
if (!(lang in options)) { if (!(lang in options)) {
throw new Error(`${lang} can't format`); throw new Error(`${lang} can't format`);
} }
return prettier.format(code, options[lang]); let langOption = options[lang];
langOption.tabWidth = tab
return prettier.format(code, langOption);
}; };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册