https://microsoft.github.io/monaco-editor/api/index.html
https://microsoft.github.io/monaco-editor/
npm install editor@0.31.1"">monaco-editor@0.31.1
使用案例
https://www.cnblogs.com/clownblogs/p/16535457.html
https://www.jb51.net/article/206911.htm
差异使用案例
https://www.cnblogs.com/zhenl/p/15758472.html
vue
首先需要下载monaco-editor组件,monaco-editor-webpack-plugin组件
npm install monaco-editor
npm install monaco-editor-webpack-plugin
当然啦,npm下载很是缓慢,换个国内镜像,淘宝的吧。果然,速度就嗖嗖的起来。
npm install -g cnpm –registry=https://registry.npm.taobao.org
cnpm install
cnpm install monaco-editor
cnpm install monaco-editor-webpack-plugin
Github:
https://github.com/microsoft/monaco-editor
文档和示例等:
https://microsoft.github.io/monaco-editor/
React版本:
https://github.com/react-monaco-editor/react-monaco-editor
Vue版本:
https://github.com/matt-oconnell/vue-monaco-editor
Angular版本:
https://github.com/materiahq/ngx-monaco-editor
<template>
<div class="bo-code-editor" ref="bceContainer"></div>
</template>
<script>
/* eslint-disable */
// 集成失败,未实现
// import * as monaco from "monaco-editor";
let sqlStr =
"ADD EXCEPT PERCENT ALL EXEC PLAN ALTER EXECUTE PRECISION AND EXISTS PRIMARY ANY EXIT PRINT AS FETCH PROC ASC FILE PROCEDURE AUTHORIZATION FILLFACTOR PUBLIC BACKUP FOR RAISERROR BEGIN FOREIGN READ BETWEEN FREETEXT READTEXT BREAK FREETEXTTABLE RECONFIGURE BROWSE FROM REFERENCES BULK FULL REPLICATION BY FUNCTION RESTORE CASCADE GOTO RESTRICT CASE GRANT RETURN CHECK GROUP REVOKE CHECKPOINT HAVING RIGHT CLOSE HOLDLOCK ROLLBACK CLUSTERED IDENTITY ROWCOUNT COALESCE IDENTITY_INSERT ROWGUIDCOL COLLATE IDENTITYCOL RULE COLUMN IF SAVE COMMIT IN SCHEMA COMPUTE INDEX SELECT CONSTRAINT INNER SESSION_USER CONTAINS INSERT SET CONTAINSTABLE INTERSECT SETUSER CONTINUE INTO SHUTDOWN CONVERT IS SOME CREATE JOIN STATISTICS CROSS KEY SYSTEM_USER CURRENT KILL TABLE CURRENT_DATE LEFT TEXTSIZE CURRENT_TIME LIKE THEN CURRENT_TIMESTAMP LINENO TO CURRENT_USER LOAD TOP CURSOR NATIONAL TRAN DATABASE NOCHECK TRANSACTION DBCC NONCLUSTERED TRIGGER DEALLOCATE NOT TRUNCATE DECLARE NULL TSEQUAL DEFAULT NULLIF UNION DELETE OF UNIQUE DENY OFF UPDATE DESC OFFSETS UPDATETEXT DISK ON USE DISTINCT OPEN USER DISTRIBUTED OPENDATASOURCE VALUES DOUBLE OPENQUERY VARYING DROP OPENROWSET VIEW DUMMY OPENXML WAITFOR DUMP OPTION WHEN ELSE OR WHERE END ORDER WHILE ERRLVL OUTER WITH ESCAPE OVER WRITETEXT";
export default {
name: "bo-code-editor",
props: {
options: {
type: Object,
default() {
return {
theme: "vs-dark", // 主题
value: "console.log(1111)", // 默认显示的值
language: "javascript",
folding: true, // 是否折叠
foldingHighlight: true, // 折叠等高线
foldingStrategy: "indentation", // 折叠方式 auto | indentation
showFoldingControls: "always", // 是否一直显示折叠 always | mouseover
disableLayerHinting: true, // 等宽优化
emptySelectionClipboard: false, // 空选择剪切板
selectionClipboard: false, // 选择剪切板
automaticLayout: true, // 自动布局
codeLens: false, // 代码镜头
scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕
colorDecorators: true, // 颜色装饰器
accessibilitySupport: "off", // 辅助功能支持 "auto" | "off" | "on"
lineNumbers: "on", // 行号 取值: "on" | "off" | "relative" | "interval" | function
lineNumbersMinChars: 5, // 行号最小字符 number
enableSplitViewResizing: false,
readOnly: false, //是否只读 取值 true | false
};
}
},
value: {
type: String,
default: ""
}
},
data() {
return {
monacoInstance: null,
provider: null,
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"
]
};
},
created() {
// this.initHints();
},
mounted() {
// this.init();
},
beforeDestroy() {
// this.dispose();
},
methods: {
dispose() {
if (this.monacoInstance) {
if (this.monacoInstance.getModel()) {
this.monacoInstance.getModel().dispose();
}
this.monacoInstance.dispose();
this.monacoInstance = null;
if (this.provider) {
this.provider.dispose();
this.provider = null
}
}
},
initHints() {
let sqlArr = sqlStr.split(" ");
this.hints = Array.from(new Set([...this.hints, ...sqlArr])).sort();
},
init() {
let that = this;
this.dispose();
let createCompleters = textUntilPosition => {
//过滤特殊字符
let _textUntilPosition = textUntilPosition
.replace(/[\*\[\]@\$\(\)]/g, "")
.replace(/(\s+|\.)/g, " ");
//切割成数组
let arr = _textUntilPosition.split(" ");
//取当前输入值
let activeStr = arr[arr.length - 1];
//获得输入值的长度
let len = activeStr.length;
//获得编辑区域内已经存在的内容
let rexp = new RegExp('([^\\w]|^)' + activeStr + '\\w*', "gim");
let match = that.value.match(rexp);
let _hints = !match ? [] : match.map(ele => {
let rexp = new RegExp(activeStr, "gim");
let search = ele.search(rexp);
return ele.substr(search)
})
//查找匹配当前输入值的元素
let hints = Array.from(new Set([...that.hints, ..._hints])).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);
});
//添加内容提示
let res = hints.map(ele => {
return {
label: ele,
kind: that.hints.indexOf(ele) > -1 ? monaco.languages.CompletionItemKind.Keyword : monaco.languages.CompletionItemKind.Text,
documentation: ele,
insertText: ele
};
});
return res;
};
this.provider = monaco.languages.registerCompletionItemProvider(this.options.provider || "sql", {
provideCompletionItems(model, position) {
var textUntilPosition = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column
});
var suggestions = createCompleters(textUntilPosition);
return {
suggestions: suggestions
};
return createCompleters(textUntilPosition);
}
});
// 初始化编辑器实例
this.monacoInstance = monaco.editor.create(this.$refs["bceContainer"], {
value: this.value,
theme: "vs-dark",
autoIndex: true,
...this.options
});
// 监听编辑器content变化事件
this.monacoInstance.onDidChangeModelContent(() => {
this.$emit("change", this.monacoInstance.getValue());
});
}
}
};
</script>