diff --git a/package.json b/package.json index 0cd6acb..8d7afa8 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,9 @@ "jshint": "^2.11.1", "lib-flexible": "^0.3.2", "mavon-editor": "^2.7.7", + "monaco-editor-vue": "^1.0.10", + "monaco-editor-webpack-plugin": "^1.9.0", + "monaco-languages": "^1.10.0", "normalize.css": "7.0.0", "nprogress": "0.2.0", "path-to-regexp": "2.4.0", diff --git a/src/components/MonacoEditorVue/index.js b/src/components/MonacoEditorVue/index.js new file mode 100644 index 0000000..6d92560 --- /dev/null +++ b/src/components/MonacoEditorVue/index.js @@ -0,0 +1,147 @@ +import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'; + +require('monaco-languages/release/min/java/java.js') + +function noop() { } + +export { monaco }; + +export default { + name: 'MonacoEditor', + props: { + diffEditor: { type: Boolean, default: false }, //是否使用diff模式 + width: {type: [String, Number], default: '100%'}, + height: {type: [String, Number], default: '100%'}, + original: String, //只有在diff模式下有效 + value: String, + language: {type: String, default: 'javascript'}, + theme: {type: String, default: 'vs'}, + options: {type: Object, default() {return {};}}, + editorMounted: {type: Function, default: noop}, + editorBeforeMount: {type: Function, default: noop} + }, + + watch: { + options: { + deep: true, + handler(options) { + this.editor && this.editor.updateOptions(options); + } + }, + + value() { + this.editor && this.value !== this._getValue() && this._setValue(this.value); + }, + + language() { + // console.log(monaco.languages.getLanguages(),this.editor.getModel(),this.language) + if(!this.editor) return; + if(this.diffEditor){ //diff模式下更新language + const { original, modified } = this.editor.getModel(); + monaco.editor.setModelLanguage(original, this.language); + monaco.editor.setModelLanguage(modified, this.language); + }else{ + monaco.editor.setModelLanguage(this.editor.getModel(), this.language); + } + }, + + theme() { + this.editor && monaco.editor.setTheme(this.theme); + }, + + style() { + this.editor && this.$nextTick(() => { + this.editor.layout(); + }); + } + }, + + computed: { + style() { + return { + width: !/^\d+$/.test(this.width) ? this.width : `${this.width}px`, + height: !/^\d+$/.test(this.height) ? this.height : `${this.height}px` + } + } + }, + + mounted () { + this.initMonaco(); + }, + + beforeDestroy() { + this.editor && this.editor.dispose(); + }, + + render (h) { + return ( +
+ ); + }, + + methods: { + initMonaco() { + const { value, language, theme, options } = this; + Object.assign(options, this._editorBeforeMount()); //编辑器初始化前 + this.editor = monaco.editor[this.diffEditor ? 'createDiffEditor' : 'create'](this.$el, { + value: value, + language: language, + theme: theme, + ...options + }); + this.diffEditor && this._setModel(this.value, this.original); + this._editorMounted(this.editor); //编辑器初始化后 + }, + + _getEditor() { + if(!this.editor) return null; + return this.diffEditor ? this.editor.modifiedEditor : this.editor; + }, + + _setModel(value, original) { //diff模式下设置model + const { language } = this; + const originalModel = monaco.editor.createModel(original, language); + const modifiedModel = monaco.editor.createModel(value, language); + this.editor.setModel({ + original: originalModel, + modified: modifiedModel + }); + }, + + _setValue(value) { + let editor = this._getEditor(); + if(editor) return editor.setValue(value); + }, + + _getValue() { + let editor = this._getEditor(); + if(!editor) return ''; + return editor.getValue(); + }, + + _editorBeforeMount() { + const options = this.editorBeforeMount(monaco); + return options || {}; + }, + + _editorMounted(editor) { + this.editorMounted(editor, monaco); + if(this.diffEditor){ + editor.onDidUpdateDiff((event) => { + const value = this._getValue(); + this._emitChange(value, event); + }); + }else{ + editor.onDidChangeModelContent(event => { + const value = this._getValue(); + this._emitChange(value, event); + }); + } + }, + + _emitChange(value, event) { + this.$emit('change', value, event); + this.$emit('input', value); + } + } +} diff --git a/src/components/VueCodemirror/VueCodeMirror.vue b/src/components/VueCodemirror/VueCodeMirror.vue deleted file mode 100644 index 651fdb9..0000000 --- a/src/components/VueCodemirror/VueCodeMirror.vue +++ /dev/null @@ -1,202 +0,0 @@ - - - - - diff --git a/src/components/preview/ImageViewer.vue b/src/components/preview/ImageViewer.vue index aa70ac0..b9db961 100644 --- a/src/components/preview/ImageViewer.vue +++ b/src/components/preview/ImageViewer.vue @@ -6,8 +6,8 @@ @inited="inited" class="viewer" ref="viewer" > -