提交 adab38b5 编写于 作者: W wuyudi 提交者: ninecents

feat: use prettier to enhance fmt function

上级 41c03d0e
此差异已折叠。
...@@ -8,6 +8,9 @@ ...@@ -8,6 +8,9 @@
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {
"@babel/parser": "^7.15.8",
"@typescript-eslint/typescript-estree": "^5.0.0",
"angular-html-parser": "^1.8.0",
"axios": "^0.21.4", "axios": "^0.21.4",
"babel-runtime": "^6.26.0", "babel-runtime": "^6.26.0",
"code-formatter": "0.0.1", "code-formatter": "0.0.1",
...@@ -30,6 +33,9 @@ ...@@ -30,6 +33,9 @@
"moment": "^2.29.1", "moment": "^2.29.1",
"php-array-reader": "^1.3.0", "php-array-reader": "^1.3.0",
"phparr": "^0.2.0", "phparr": "^0.2.0",
"postcss-less": "^5.0.0",
"postcss-scss": "^4.0.1",
"prettier": "^2.4.1",
"properties-to-json": "^0.1.7", "properties-to-json": "^0.1.7",
"qrcode": "^1.4.4", "qrcode": "^1.4.4",
"qrcode-parser": "^1.2.0", "qrcode-parser": "^1.2.0",
...@@ -91,4 +97,4 @@ ...@@ -91,4 +97,4 @@
"last 2 versions", "last 2 versions",
"not ie <= 8" "not ie <= 8"
] ]
} }
\ No newline at end of file
<template> <template>
<div> <div>
<div style="border: 1px solid #dcdee2;border-radius: 4px;"> <div style="border: 1px solid #dcdee2; border-radius: 4px">
<codemirror ref="code" v-model="current.content" :options="options"></codemirror> <codemirror
</div> ref="code"
<option-block> v-model="current.content"
<FormItem> :options="options"
<ButtonGroup> ></codemirror>
<Button type="primary" @click="handle(k,v)" v-for="(v,k) in lang" :key="k">{{ k }}</Button>
</ButtonGroup>
</FormItem>
<FormItem>
<Checkbox v-model="current.isCompress">压缩</Checkbox>
</FormItem>
</option-block>
</div> </div>
<option-block>
<FormItem>
<ButtonGroup>
<Button
type="primary"
@click="handle(k, v)"
v-for="(v, k) in lang"
:key="k"
>{{ k }}</Button
>
</ButtonGroup>
</FormItem>
<FormItem>
<Checkbox v-model="current.isCompress">压缩</Checkbox>
</FormItem>
</option-block>
</div>
</template> </template>
<script> <script>
import {codemirror} from 'vue-codemirror' import { codemirror } from "vue-codemirror";
import formatter from './library/formatter' import formatter from "./library/formatter";
import 'codemirror/lib/codemirror.css' import "codemirror/lib/codemirror.css";
import 'codemirror/mode/javascript/javascript.js' import "codemirror/mode/javascript/javascript.js";
import 'codemirror/mode/htmlmixed/htmlmixed.js' import "codemirror/mode/htmlmixed/htmlmixed.js";
import 'codemirror/mode/css/css.js' import "codemirror/mode/css/css.js";
import 'codemirror/mode/xml/xml.js' import "codemirror/mode/xml/xml.js";
import 'codemirror/mode/sql/sql.js' import "codemirror/mode/sql/sql.js";
import 'codemirror/addon/fold/foldcode.js' import "codemirror/addon/fold/foldcode.js";
import 'codemirror/addon/fold/foldgutter.js' import "codemirror/addon/fold/foldgutter.js";
import 'codemirror/addon/fold/brace-fold.js' import "codemirror/addon/fold/brace-fold.js";
import 'codemirror/addon/fold/comment-fold.js' import "codemirror/addon/fold/comment-fold.js";
import 'codemirror/addon/fold/foldgutter.css' import "codemirror/addon/fold/foldgutter.css";
export default { export default {
components: { components: {
codemirror, codemirror,
},
created() {
this.current = Object.assign(this.current, this.$getToolData("content"));
},
mounted() {
this.codemirror.setSize(null, 350);
if (this.current.lang) {
this.codemirror.setOption("mode", this.options[this.current.lang]);
}
},
computed: {
codemirror() {
return this.$refs.code.codemirror;
}, },
created() { },
this.current = Object.assign(this.current, this.$getToolData('content')) methods: {
handle(lang, mode) {
if (this.current.content) {
this.current.content = formatter(
this.current.content,
lang,
this.current.isCompress
);
this.codemirror.setOption("mode", mode);
this.current.lang = lang;
this.$saveToolData(this.current);
}
}, },
mounted() { },
this.codemirror.setSize(null, 350) data() {
if (this.current.lang) { return {
this.codemirror.setOption('mode', this.options[this.current.lang]) current: {
} content: "",
}, lang: "",
computed: { isCompress: false,
codemirror() { },
return this.$refs.code.codemirror options: {
}, mode: null,
}, lineNumbers: true,
methods: { lineWrapping: false,
handle(lang, mode) { foldGutter: true,
if (this.current.content) { indentUnit: 4,
this.current.content = formatter(this.current.content, lang, this.current.isCompress) gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
this.codemirror.setOption('mode', mode) },
this.current.lang = lang lang: {
this.$saveToolData(this.current) js: "text/javascript",
} ts: "text/typescript",
}, css: "text/css",
}, scss: "text/scss",
data() { less: "text/less",
return { json5: "text/json5",
current: { graphql: "text/graphql",
content: '', markdown: "text/markdown",
lang: '', html: "text/html",
isCompress: false, xml: "application/xml",
}, sql: "text/x-mysql",
options: { vue: "text/vue",
mode: null, angular: "text/angular",
lineNumbers: true, },
lineWrapping: false, };
foldGutter: true, },
indentUnit: 4, };
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
},
lang: {
'js': 'text/javascript',
'html': 'text/html',
'css': 'text/css',
'xml': 'application/xml',
'sql': 'text/x-mysql',
},
}
},
}
</script> </script>
\ No newline at end of file
import codeFormatter from 'code-formatter' import codeFormatter from "code-formatter";
import xmlFormatter from "xml-formatter" import xmlFormatter from "xml-formatter";
import prettier from "prettier/standalone";
import parser from "prettier/parser-babel";
import parserTypeScript from "prettier/parser-typescript";
import parserGraphql from "prettier/parser-graphql";
import parserMarkdown from "prettier/parser-markdown";
import parserCss from "prettier/parser-postcss";
import parserYaml from "prettier/parser-yaml";
import parserHtml from "prettier/parser-html";
import parserJson5 from "prettier/parser-babel";
//https://github.com/prettier/prettier/issues/6264#issuecomment-507535391
const options = {
js: { parser: "babel", plugins: [parser] },
ts: { parser: "typescript", plugins: [parserTypeScript] },
vue: { parser: "vue", plugins: [parserHtml] },
graphql: { parser: "graphql", plugins: [parserGraphql] },
markdown: { parser: "markdown", plugins: [parserMarkdown] },
css: { parser: "css", plugins: [parserCss] },
less: { parser: "less", plugins: [parserCss] },
scss: { parser: "scss", plugins: [parserCss] },
yaml: { parser: "yaml", plugins: [parserYaml] },
html: { parser: "html", plugins: [parserHtml] },
angular: { parser: "angular", plugins: [parserHtml] },
json5: { parser: "json5", plugins: [parserJson5] },
};
export default (code, type, isCompress = false) => { export default (code, type, isCompress = false) => {
if (type === "xml") { if (type === "xml") {
return xmlFormatter(code, { return xmlFormatter(code, {
indentation: isCompress ? '' : ' ', indentation: isCompress ? "" : " ",
collapseContent: true, collapseContent: true,
lineSeparator: isCompress ? '' : '\n' lineSeparator: isCompress ? "" : "\n",
}); });
} } else if (type === "sql") {
return codeFormatter(code, { return codeFormatter(code, {
method: isCompress ? type + "min" : type method: isCompress ? type + "min" : type,
}) });
} } else {
\ No newline at end of file return prettier.format(code, options[type]);
}
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册