hyb
2025-05-16 9cf793804cd7489ca4206e273da5a96b00820478
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
    <div ref="editor" class="main"></div>
</template>
 
<script>
import * as monaco from "monaco-editor";
import createSqlCompleter from "./util/sql-completion";
import createJavascriptCompleter from "./util/javascript-completion";
import createPythonCompleter from "./util/python-completion";
import registerLanguage from "./util/log-language";
const global = {};
 
const getHints = model => {
    let id = model.id.substring(6);
    return (global[id] && global[id].hints) || [];
};
monaco.languages.registerCompletionItemProvider(
    "sql",
    createSqlCompleter(getHints)
);
monaco.languages.registerCompletionItemProvider(
    "javascript",
    createJavascriptCompleter(getHints)
);
monaco.languages.registerCompletionItemProvider(
    "python",
    createPythonCompleter(getHints)
);
registerLanguage(monaco);
/**
 * monaco options
 * https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandaloneeditorconstructionoptions.html
 */
export default {
    props: {
        options: {
            type: Object,
            default() {
                return {};
            }
        },
        value: {
            type: String,
            required: true
        },
        language: {
            type: String
        },
        hints: {
            type: Array,
            default() {
                return [];
            }
        }
    },
    name: "BaseMonacoEditor",
    data() {
        return {
            editorInstance: null,
            defaultOptions: {
                theme: "vs",
                fontSize: 14
            }
        };
    },
    watch: {
        value() {
            if (this.value !== this.editorInstance.getValue()) {
                this.editorInstance.setValue(this.value);
            }
        }
    },
    mounted() {
        this.initEditor();
        global[this.editorInstance._id] = this;
        window.addEventListener("resize", this.layout);
    },
    destroyed() {
        this.editorInstance.dispose();
        global[this.editorInstance._id] = null;
        window.removeEventListener("resize", this.layout);
    },
    methods: {
        layout() {
            this.editorInstance.layout();
        },
        undo() {
            this.editorInstance.trigger("anyString", "undo");
            this.onValueChange();
        },
        redo() {
            this.editorInstance.trigger("anyString", "redo");
            this.onValueChange();
        },
        getOptions() {
            let props = { value: this.value };
            this.language !== undefined && (props.language = this.language);
            let options = Object.assign(
                {},
                this.defaultOptions,
                this.options,
                props
            );
            return options;
        },
        onValueChange() {
            this.$emit("input", this.editorInstance.getValue());
            this.$emit("change", this.editorInstance.getValue());
        },
        initEditor() {
            this.MonacoEnvironment = {
                getWorkerUrl: function() {
                    return "./editor.worker.bundle.js";
                }
            };
 
            this.editorInstance = monaco.editor.create(
                this.$refs.editor,
                this.getOptions()
            );
            this.editorInstance.onContextMenu(e => {
                this.$emit("contextmenu", e);
            });
            this.editorInstance.onDidChangeModelContent(() => {
                this.onValueChange();
            });
            this.editorInstance.addCommand(
                monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S,
                () => {
                    this.$emit("save", this.editorInstance.getValue());
                }
            );
        }
    }
};
</script>
 
<style scoped>
.main /deep/ .view-lines * {
    font-family: Consolas, "Courier New", monospace !important;
}
</style>