MonocaEditor采用异步加载,优化整体加载速度

This commit is contained in:
fofolee 2024-01-23 00:03:34 +08:00
parent 325c6c746a
commit 632adf11e1
2 changed files with 33 additions and 12 deletions

View File

@ -211,7 +211,8 @@
class="absolute-bottom" class="absolute-bottom"
:placeholder="true" :placeholder="true"
ref="editor" ref="editor"
@typing="(val) => (quickcommandInfo.cmd = val)" @loaded="monacoInit"
@typing="(val) => monacoTyping(val)"
@keyStroke="monacoKeyStroke" @keyStroke="monacoKeyStroke"
:style="{ :style="{
top: languageBarHeight + 'px', top: languageBarHeight + 'px',
@ -225,11 +226,15 @@
</template> </template>
<script> <script>
import MonacoEditor from "components/MonacoEditor"; import { defineAsyncComponent } from "vue";
import CommandSideBar from "components/CommandSideBar"; import CommandSideBar from "components/CommandSideBar";
import CommandRunResult from "components/CommandRunResult"; import CommandRunResult from "components/CommandRunResult";
import QuickAction from "components/popup/QuickAction"; import QuickAction from "components/popup/QuickAction";
import KeyRecorder from "components/popup/KeyRecorder"; import KeyRecorder from "components/popup/KeyRecorder";
// Monaco
const MonacoEditor = defineAsyncComponent(() =>
import("components/MonacoEditor")
);
export default { export default {
components: { components: {
@ -270,8 +275,11 @@ export default {
props: { props: {
action: Object, action: Object,
}, },
created() {
this.commandInit();
},
mounted() { mounted() {
this.init(); this.sidebarInit();
}, },
computed: { computed: {
configurationPage() { configurationPage() {
@ -288,23 +296,29 @@ export default {
}, },
}, },
methods: { methods: {
init() { //
commandInit() {
let quickCommandInfo = let quickCommandInfo =
this.action.type === "run" this.action.type === "run"
? this.$root.utools.getDB("cfg_codeHistory") ? this.$root.utools.getDB("cfg_codeHistory")
: this.action.data; : this.action.data;
quickCommandInfo?.program && quickCommandInfo?.program &&
Object.assign(this.quickcommandInfo, _.cloneDeep(quickCommandInfo)); Object.assign(this.quickcommandInfo, _.cloneDeep(quickCommandInfo));
// monaco
this.$refs.editor.setEditorValue(this.quickcommandInfo.cmd);
this.setLanguage(this.quickcommandInfo.program);
this.$refs.editor.setCursorPosition(this.quickcommandInfo.cursorPosition);
// //
if (this.quickcommandInfo.tags?.includes("默认") && !utools.isDev()) { if (this.quickcommandInfo.tags?.includes("默认") && !utools.isDev()) {
this.canCommandSave = false; this.canCommandSave = false;
} }
},
//
sidebarInit() {
this.$refs.sidebar?.init(); this.$refs.sidebar?.init();
}, },
// MonacoMonaco
monacoInit() {
this.$refs.editor.setEditorValue(this.quickcommandInfo.cmd);
this.setLanguage(this.quickcommandInfo.program);
this.$refs.editor.setCursorPosition(this.quickcommandInfo.cursorPosition);
},
programChanged(value) { programChanged(value) {
this.setLanguage(value); this.setLanguage(value);
if (value === "custom") this.$refs.settings.show(); if (value === "custom") this.$refs.settings.show();
@ -373,6 +387,9 @@ export default {
command.cursorPosition = this.$refs.editor.getCursorPosition(); command.cursorPosition = this.$refs.editor.getCursorPosition();
this.$root.utools.putDB(command, "cfg_codeHistory"); this.$root.utools.putDB(command, "cfg_codeHistory");
}, },
monacoTyping(val) {
this.quickcommandInfo.cmd = val;
},
monacoKeyStroke(event, data) { monacoKeyStroke(event, data) {
switch (event) { switch (event) {
case "run": case "run":

View File

@ -46,6 +46,7 @@ export default {
}, },
mounted() { mounted() {
this.initEditor(); this.initEditor();
this.$emit("loaded");
}, },
props: { props: {
placeholder: Boolean, placeholder: Boolean,
@ -213,10 +214,13 @@ export default {
} }
); );
// alt + z // alt + z
this.rawEditor().addCommand(monaco.KeyMod.Alt | monaco.KeyCode.KeyZ, () => { this.rawEditor().addCommand(
that.wordWrap = that.wordWrap === "off" ? "on" : "off"; monaco.KeyMod.Alt | monaco.KeyCode.KeyZ,
that.rawEditor.updateOptions({ wordWrap: that.wordWrap }); () => {
}); that.wordWrap = that.wordWrap === "off" ? "on" : "off";
that.rawEditor.updateOptions({ wordWrap: that.wordWrap });
}
);
// ctrl + s // ctrl + s
this.rawEditor().addCommand( this.rawEditor().addCommand(
monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS,