From bd2d75c36000e208af2052514f025b0ef15fadee Mon Sep 17 00:00:00 2001 From: qianlishi <1432731663@qq.com> Date: Fri, 6 Aug 2021 13:36:12 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E7=BC=96=E8=AF=91=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- report-ui/package.json | 2 + .../resultset/components/EditDataSet.vue | 20 +-- .../resultset/components/MonacoEditor.vue | 132 ++++++++++++++++++ .../components/util/javascript-completion.js | 38 +++++ .../resultset/components/util/log-language.js | 58 ++++++++ .../components/util/sql-completion.js | 82 +++++++++++ report-ui/webpack.config.js | 6 +- 7 files changed, 327 insertions(+), 11 deletions(-) create mode 100644 report-ui/src/views/report/resultset/components/MonacoEditor.vue create mode 100644 report-ui/src/views/report/resultset/components/util/javascript-completion.js create mode 100644 report-ui/src/views/report/resultset/components/util/log-language.js create mode 100644 report-ui/src/views/report/resultset/components/util/sql-completion.js diff --git a/report-ui/package.json b/report-ui/package.json index 0651496d..25549a78 100644 --- a/report-ui/package.json +++ b/report-ui/package.json @@ -24,6 +24,7 @@ "js-cookie": "2.2.0", "miment": "^0.0.9", "moment": "^2.29.1", + "monaco-editor": "^0.20.0", "normalize.css": "7.0.0", "nprogress": "0.2.0", "sortablejs": "^1.10.2", @@ -61,6 +62,7 @@ "html-webpack-plugin": "4.0.0-alpha", "js-md5": "^0.7.3", "mini-css-extract-plugin": "0.4.1", + "monaco-editor-webpack-plugin": "^4.1.1", "node-notifier": "5.2.1", "node-sass": "^4.7.2", "optimize-css-assets-webpack-plugin": "5.0.0", diff --git a/report-ui/src/views/report/resultset/components/EditDataSet.vue b/report-ui/src/views/report/resultset/components/EditDataSet.vue index 44d3abec..ea3f81d6 100644 --- a/report-ui/src/views/report/resultset/components/EditDataSet.vue +++ b/report-ui/src/views/report/resultset/components/EditDataSet.vue @@ -62,11 +62,11 @@ > - + language="sql" + style="height: 500px" + /> --> @@ -92,7 +92,7 @@ align="center" label="序号" type="index" - width="80" + min-width="80" /> @@ -349,10 +349,10 @@ > - @@ -382,10 +382,10 @@ import "codemirror/mode/javascript/javascript.js"; import "codemirror/lib/codemirror.css"; // 核心样式 import "codemirror/theme/cobalt.css"; // 引入主题后还需要在 options 中指定主题才会生效 import vueJsonEditor from "vue-json-editor"; - +import MonacoEditor from "./MonacoEditor.vue"; export default { name: "Support", - components: { Dictionary, codemirror, vueJsonEditor }, + components: { Dictionary, codemirror, vueJsonEditor, MonacoEditor }, props: { visib: { required: true, diff --git a/report-ui/src/views/report/resultset/components/MonacoEditor.vue b/report-ui/src/views/report/resultset/components/MonacoEditor.vue new file mode 100644 index 00000000..8f5325b1 --- /dev/null +++ b/report-ui/src/views/report/resultset/components/MonacoEditor.vue @@ -0,0 +1,132 @@ + + + + + + + diff --git a/report-ui/src/views/report/resultset/components/util/javascript-completion.js b/report-ui/src/views/report/resultset/components/util/javascript-completion.js new file mode 100644 index 00000000..3999cc29 --- /dev/null +++ b/report-ui/src/views/report/resultset/components/util/javascript-completion.js @@ -0,0 +1,38 @@ +import * as monaco from 'monaco-editor' +// js 有内置提示 +function createCompleter(getExtraHints) { + const createSuggestions = function (model, textUntilPosition) { + let text = model.getValue(); + textUntilPosition = textUntilPosition.replace(/[\*\[\]@\$\(\)]/g, "").replace(/(\s+|\.)/g, " "); + let arr = textUntilPosition.split(/[\s;]/); + let activeStr = arr[arr.length - 1]; + let len = activeStr.length; + let rexp = new RegExp("([^\\w]|^)" + activeStr + "\\w*", "gim"); + let match = text.match(rexp); + let mergeHints = Array.from(new Set([...getExtraHints(model)])) + .sort() + .filter(ele => { + let rexp = new RegExp(ele.substr(0, len), "gim"); + return (match && match.length === 1 && ele === activeStr) || + ele.length === 1 ? false : activeStr.match(rexp); + }); + return mergeHints.map(ele => ({ + label: ele, + kind: monaco.languages.CompletionItemKind.Text, + documentation: ele, + insertText: ele + })); + }; + return { + provideCompletionItems(model, position) { + let textUntilPosition = model.getValueInRange({ + startLineNumber: position.lineNumber, + startColumn: 1, + endLineNumber: position.lineNumber, + endColumn: position.column + }); + return { suggestions: createSuggestions(model, textUntilPosition) }; + } + } +} +export default createCompleter; \ No newline at end of file diff --git a/report-ui/src/views/report/resultset/components/util/log-language.js b/report-ui/src/views/report/resultset/components/util/log-language.js new file mode 100644 index 00000000..a3db9c6b --- /dev/null +++ b/report-ui/src/views/report/resultset/components/util/log-language.js @@ -0,0 +1,58 @@ +function registerLanguage(monaco) { + monaco.languages.register({ + id: "log" + }); + monaco.languages.setMonarchTokensProvider("log", { + tokenizer: { + root: [ + [/(^[=a-zA-Z].*|\d\s.*)/, "log-normal"], + [/\sERROR\s.*/, "log-error"], + [/\sWARN\s.*/, "log-warn"], + [/\sINFO\s.*/, "log-info"], + [ + /^([0-9]{4}||[0-9]{2})-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]{3})?/, + "log-date", + ], + [ + /^[0-9]{2}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]{3})?/, + "log-date", + ], + [/(^\*\*Waiting queue:.*)/, "log-info"], + [/(^\*\*result tips:.*)/, "log-info"], + ], + }, + }); + monaco.editor.defineTheme("log", { + base: "vs", + inherit: true, + rules: [{ + token: "log-info", + foreground: "4b71ca" + }, + { + token: "log-error", + foreground: "ff0000", + fontStyle: "bold" + }, + { + token: "log-warn", + foreground: "FFA500" + }, + { + token: "log-date", + foreground: "008800" + }, + { + token: "log-normal", + foreground: "808080" + }, + ], + colors: { + "editor.lineHighlightBackground": "#ffffff", + "editorGutter.background": "#f7f7f7", + }, + }); + + } + + export default registerLanguage; \ No newline at end of file diff --git a/report-ui/src/views/report/resultset/components/util/sql-completion.js b/report-ui/src/views/report/resultset/components/util/sql-completion.js new file mode 100644 index 00000000..537057be --- /dev/null +++ b/report-ui/src/views/report/resultset/components/util/sql-completion.js @@ -0,0 +1,82 @@ +import * as monaco from 'monaco-editor' +const hints = [ + "SELECT", + "INSERT", + "DELETE", + "UPDATE", + "CREATE TABLE", + "DROP TABLE", + "ALTER TABLE", + "CREATE VIEW", + "DROP VIEW", + "CREATE INDEX", + "DROP INDEX", + "CREATE PROCEDURE", + "DROP PROCEDURE", + "CREATE TRIGGER", + "DROP TRIGGER", + "CREATE SCHEMA", + "DROP SCHEMA", + "CREATE DOMAIN", + "ALTER DOMAIN", + "DROP DOMAIN", + "GRANT", + "DENY", + "REVOKE", + "COMMIT", + "ROLLBACK", + "SET TRANSACTION", + "DECLARE", + "EXPLAN", + "OPEN", + "FETCH", + "CLOSE", + "PREPARE", + "EXECUTE", + "DESCRIBE", + "FORM", + "ORDER BY"] +function createCompleter(getExtraHints) { + const createSuggestions = function (model, textUntilPosition) { + let text = model.getValue(); + textUntilPosition = textUntilPosition.replace(/[\*\[\]@\$\(\)]/g, "").replace(/(\s+|\.)/g, " "); + let arr = textUntilPosition.split(/[\s;]/); + let activeStr = arr[arr.length - 1]; + let len = activeStr.length; + let rexp = new RegExp("([^\\w]|^)" + activeStr + "\\w*", "gim"); + let match = text.match(rexp); + let textHints = !match ? [] : + match.map(ele => { + let rexp = new RegExp(activeStr, "gim"); + let search = ele.search(rexp); + return ele.substr(search); + }); + let mergeHints = Array.from(new Set([...hints, ...textHints, ...getExtraHints(model)])) + .sort() + .filter(ele => { + let rexp = new RegExp(ele.substr(0, len), "gim"); + return (match && match.length === 1 && ele === activeStr) || + ele.length === 1 ? false : activeStr.match(rexp); + }); + return mergeHints.map(ele => ({ + label: ele, + kind: hints.indexOf(ele) > -1 ? + monaco.languages.CompletionItemKind.Keyword : + monaco.languages.CompletionItemKind.Text, + documentation: ele, + insertText: ele + })); + }; + return { + provideCompletionItems(model, position) { + let textUntilPosition = model.getValueInRange({ + startLineNumber: position.lineNumber, + startColumn: 1, + endLineNumber: position.lineNumber, + endColumn: position.column + }); + return { suggestions: createSuggestions(model, textUntilPosition) }; + } + } +} +export default createCompleter; \ No newline at end of file diff --git a/report-ui/webpack.config.js b/report-ui/webpack.config.js index ae796712..d731edd7 100644 --- a/report-ui/webpack.config.js +++ b/report-ui/webpack.config.js @@ -1,5 +1,6 @@ 'use strict' const path = require('path') +const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin'); module.exports = { resolve: { @@ -8,5 +9,8 @@ module.exports = { '@': path.resolve('src'), 'vue$': 'vue/dist/vue.esm.js' } - } + }, + plugins: [ + new MonacoWebpackPlugin() + ] }