|
| 1 | +<template> |
| 2 | + <div :id="`monaco-${id}`" class="monaco-view"></div> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script> |
| 6 | +import * as monaco from "monaco-editor"; |
| 7 | +// import * as monaco from "monaco-editor/esm/vs/editor/editor.api"; |
| 8 | +// import { createMonacoComplete, createMonacoHover } from "@/common/monacoUtils"; |
| 9 | +
|
| 10 | +export default { |
| 11 | + name: "Monaco", |
| 12 | +
|
| 13 | + props: { |
| 14 | + id: { |
| 15 | + type: String, |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + value: { |
| 19 | + type: String, |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + lang: { |
| 23 | + type: String, |
| 24 | + required: true, |
| 25 | + }, |
| 26 | + disabled: { |
| 27 | + type: Boolean, |
| 28 | + default: false, |
| 29 | + }, |
| 30 | + // warp: { |
| 31 | + // type: Boolean, |
| 32 | + // default: false, |
| 33 | + // }, |
| 34 | + // provider: { |
| 35 | + // type: Array, |
| 36 | + // default: () => [], |
| 37 | + // }, |
| 38 | + scrollLoading: { |
| 39 | + type: Boolean, |
| 40 | + default: false, |
| 41 | + }, |
| 42 | + scrollFunc: { |
| 43 | + type: Function, |
| 44 | + default: () => () => {}, |
| 45 | + }, |
| 46 | + }, |
| 47 | +
|
| 48 | + data() { |
| 49 | + return { |
| 50 | + editor: null, |
| 51 | + providerDisposeID: null, |
| 52 | + hoverDisposeID: null, |
| 53 | + sqlHints: [ |
| 54 | + { |
| 55 | + name: "SELECT", |
| 56 | + type: "Keyword", |
| 57 | + detail: "SQL", |
| 58 | + documentation: "SQL selector.", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "FROM", |
| 62 | + type: "Keyword", |
| 63 | + detail: "SQL", |
| 64 | + documentation: "What event.", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "WHERE", |
| 68 | + type: "Keyword", |
| 69 | + detail: "SQL", |
| 70 | + documentation: |
| 71 | + "Filters a result set to include only records that fulfill a specified condition. ", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "and", |
| 75 | + type: "Keyword", |
| 76 | + detail: "SQL", |
| 77 | + documentation: "Operator.", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "or", |
| 81 | + type: "Keyword", |
| 82 | + detail: "SQL", |
| 83 | + documentation: "Operator.", |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + }, |
| 88 | +
|
| 89 | + watch: { |
| 90 | + // value(val) { |
| 91 | + // if (this.editor) { |
| 92 | + // if (val !== this.editor.getValue()) { |
| 93 | + // this.editor.setValue(val); |
| 94 | + // } |
| 95 | + // } |
| 96 | + // }, |
| 97 | + // lang() { |
| 98 | + // if (this.editor) { |
| 99 | + // this.editor.dispose(); |
| 100 | + // this.initEditor(); |
| 101 | + // } |
| 102 | + // }, |
| 103 | + }, |
| 104 | +
|
| 105 | + created() { |
| 106 | + window.onresize = () => { |
| 107 | + console.log("resize"); |
| 108 | + // if (this.editor) { |
| 109 | + // this.editor.layout(); |
| 110 | + // } |
| 111 | + }; |
| 112 | + // if (this.provider.length) { |
| 113 | + // this.registerCustomHintsProvider(); |
| 114 | + // this.registerCustomHoverProvider(); |
| 115 | + // } |
| 116 | + }, |
| 117 | +
|
| 118 | + mounted() { |
| 119 | + this.initEditor(); |
| 120 | + }, |
| 121 | + // beforeUpdate() { |
| 122 | + // this.editor.dispose(); |
| 123 | + // }, |
| 124 | +
|
| 125 | + beforeUnmount() { |
| 126 | + if (this.editor) { |
| 127 | + // this.editor.getModel().dispose(); |
| 128 | + this.editor.dispose(); |
| 129 | + // this.editor = null; |
| 130 | + } |
| 131 | + // if (this.providerDisposeID) { |
| 132 | + // this.providerDisposeID.dispose(); |
| 133 | + // } |
| 134 | + // if (this.hoverDisposeID) { |
| 135 | + // this.hoverDisposeID.dispose(); |
| 136 | + // } |
| 137 | + }, |
| 138 | +
|
| 139 | + methods: { |
| 140 | + initEditor() { |
| 141 | + const id = `monaco-${this.id}`; |
| 142 | + const defaultOptions = { |
| 143 | + value: this.value, |
| 144 | + language: this.lang, |
| 145 | + readOnly: this.disabled, |
| 146 | + // fontSize: 12, |
| 147 | + // automaticLayout: true, |
| 148 | + // scrollBeyondLastLine: false, |
| 149 | + theme: "vs", |
| 150 | + minimap: { |
| 151 | + enabled: false, |
| 152 | + }, |
| 153 | + // hover: { |
| 154 | + // delay: 500, |
| 155 | + // enabled: true, |
| 156 | + // }, |
| 157 | + }; |
| 158 | + const options = this.beforeMonacoCreate(defaultOptions); |
| 159 | + // Create |
| 160 | + this.editor = monaco.editor.create(document.getElementById(id), options); |
| 161 | + // event changed |
| 162 | + // this.editor.onDidChangeModelContent((event) => { |
| 163 | + // const value = this.editor.getValue(); |
| 164 | + // if (value !== this.value) { |
| 165 | + // this.$emit("input", value, event); |
| 166 | + // this.$emit("change", value, event); |
| 167 | + // } |
| 168 | + // }); |
| 169 | + // Qucik save method |
| 170 | + // eslint-disable-next-line |
| 171 | + // this.editor.addCommand( |
| 172 | + // monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, |
| 173 | + // () => { |
| 174 | + // this.$emit("qucik-save", this.value); |
| 175 | + // } |
| 176 | + // ); |
| 177 | + // Update editor options |
| 178 | + // this.editor.getModel().updateOptions({ tabSize: 2 }); |
| 179 | +
|
| 180 | + if (this.scrollLoading) this.editor.onDidScrollChange(this.scrollFunc); |
| 181 | + }, |
| 182 | + beforeMonacoCreate(options) { |
| 183 | + // if (this.warp) { |
| 184 | + // const warpOptions = { |
| 185 | + // wordWrap: "on", |
| 186 | + // wrappingIndent: "indent", |
| 187 | + // }; |
| 188 | + // Object.assign(options, warpOptions); |
| 189 | + // } |
| 190 | + return options; |
| 191 | + }, |
| 192 | + // getHints() { |
| 193 | + // const $hints = [...this.provider]; |
| 194 | + // if (this.lang === "sql") { |
| 195 | + // $hints.push(...this.sqlHints); |
| 196 | + // } |
| 197 | + // return $hints; |
| 198 | + // }, |
| 199 | + // registerCustomHintsProvider() { |
| 200 | + // this.providerDisposeID = monaco.languages.registerCompletionItemProvider( |
| 201 | + // this.lang, |
| 202 | + // { |
| 203 | + // provideCompletionItems: (model, position) => { |
| 204 | + // const wordObj = model.getWordUntilPosition(position); |
| 205 | + // const hints = this.getHints(wordObj); |
| 206 | + // const range = { |
| 207 | + // startLineNumber: position.lineNumber, |
| 208 | + // endLineNumber: position.lineNumber, |
| 209 | + // startColumn: wordObj.startColumn, |
| 210 | + // endColumn: wordObj.endColumn, |
| 211 | + // }; |
| 212 | + // return { |
| 213 | + // suggestions: createMonacoComplete(hints, range, wordObj), |
| 214 | + // }; |
| 215 | + // }, |
| 216 | + // triggerCharacters: [" "], |
| 217 | + // } |
| 218 | + // ); |
| 219 | + // }, |
| 220 | + // registerCustomHoverProvider() { |
| 221 | + // monaco.languages.register({ id: this.lang }); |
| 222 | + // this.hoverDisposeID = monaco.languages.registerHoverProvider(this.lang, { |
| 223 | + // provideHover: (model, position) => { |
| 224 | + // if (!model.getWordAtPosition(position)) { |
| 225 | + // return {}; |
| 226 | + // } |
| 227 | + // const { word } = model.getWordAtPosition(position); |
| 228 | + // return { |
| 229 | + // contents: createMonacoHover(word, this.provider), |
| 230 | + // }; |
| 231 | + // }, |
| 232 | + // }); |
| 233 | + // }, |
| 234 | + }, |
| 235 | +}; |
| 236 | +</script> |
| 237 | + |
| 238 | +<style lang="scss"> |
| 239 | +.monaco-view { |
| 240 | + height: 100%; |
| 241 | + position: relative; |
| 242 | +} |
| 243 | +</style> |
0 commit comments