添加历史代码功能

This commit is contained in:
fofolee 2022-03-31 21:18:45 +08:00
parent 4d07847196
commit 27e077b56b
8 changed files with 104 additions and 36 deletions

View File

@ -1,24 +0,0 @@
import { Dialog } from 'quasar'
import inputBox from "../components/InputBox"
let showInputBox = (labels = [], title = "", hints = []) => {
return new Promise((reslove, reject) => {
Dialog.create({
component: inputBox,
componentProps: {
labels: labels,
title: title,
hints: hints
}
}).onOk(results => {
reslove(Array.from(results))
}).onCancel(() => {
console.log('取消')
}).onDismiss(() => {
console.log('对话框被关闭')
})
})
};
export default {
showInputBox,
};

View File

@ -5,7 +5,7 @@
<script>
import * as monaco from "monaco-editor";
import { toRaw } from "vue";
import importAll from "../api/importAll.js";
import importAll from "../js/importAll.js";
//
let apis = importAll(
@ -135,6 +135,9 @@ export default {
getEditorValue() {
return toRaw(this.editor).getValue();
},
setEditorValue(value) {
toRaw(this.editor).setValue(value);
},
setEditorLanguage(language) {
monaco.editor.setModelLanguage(this.editor.getModel(), language);
},

View File

@ -1,3 +1,7 @@
/**
* 快速导入同一目录下的所有脚本
*/
const importAll = context => {
const map = {}
for (const key of context.keys()) {

View File

@ -1,3 +1,7 @@
/**
* 所有支持的编程语言
*/
const programs = {
quickcommand: {
name: "quickcommand",

View File

@ -14,8 +14,8 @@ let showInputBox = (options = [], title = "") => {
if (!(options instanceof Object)) return reject(new TypeError("必须为数组或对象"))
if (options instanceof Array) props.labels = options
else props = options
if (!props.values) props.values = options.map(() => "")
if (!props.hints) props.hints = options.map(() => "")
if (!props.values) props.values = options.labels.map(() => "")
if (!props.hints) props.hints = options.labels.map(() => "")
Dialog.create({
component: inputBox,
componentProps: props

51
src/js/utools.js Normal file
View File

@ -0,0 +1,51 @@
/**
* 阉割utools同时返回一个满血版的UTOOLS
* 防止输出html输出时通过script标签调用utools执行危险函数
*/
// 禁用危险函数
let whole = window.utools
if (!utools.isDev()) window.utools = window.getuToolsLite()
// 数据库前缀
const DBPRE = {
QC: 'qc_',
CFG: 'cfg_',
PAN: 'pan_'
}
// 数据库函数封装
let getDB = id => {
let db = whole.db.get(id)
return db ? db.data : {}
}
let putDB = (value, id) => {
let db = whole.db.get(id);
if (db) whole.db.put({
_id: id,
data: value,
_rev: db._rev
})
else whole.db.put({
_id: id,
data: value
});
}
let delDB = id => {
return whole.db.remove(id)
}
let getDocs = key => {
return whole.db.allDocs(key)
}
export default {
whole,
getDB,
putDB,
delDB,
getDocs,
DBPRE
}

View File

@ -127,8 +127,9 @@
</template>
<script>
import allProgrammings from "../api/programs.js";
import allProgrammings from "../js/programs.js";
import MonocaEditor from "components/MonocaEditor";
import UTOOLS from "../js/utools.js";
export default {
components: { MonocaEditor },
@ -149,6 +150,7 @@ export default {
},
mounted() {
this.bindKeys();
this.loadOrSaveHistory();
},
computed: {},
created() {},
@ -158,6 +160,30 @@ export default {
},
},
methods: {
loadOrSaveHistory() {
//
var history = UTOOLS.getDB(UTOOLS.DBPRE.CFG + "codeHistory");
if (history.program) {
this.$refs.editor.setEditorValue(history.cmd);
this.program = history.program;
this.scptarg = history.scptarg || "";
this.scriptCode = history.scriptCode || "";
this.outputCode = history.outputCode || "";
if (history.customOptions) this.customOptions = history.customOptions;
}
utools.onPluginOut(() => {
var saveData = {
cmd: this.$refs.editor.getEditorValue(),
program: this.program,
scptarg: this.scptarg,
scriptCode: this.scriptCode,
outputCode: this.outputCode,
customOptions: JSON.parse(JSON.stringify(this.customOptions))
};
//
UTOOLS.putDB(saveData, UTOOLS.DBPRE.CFG + "codeHistory");
});
},
bindKeys() {
let that = this;
// ctrl+b
@ -184,14 +210,18 @@ export default {
});
},
showCodingPage() {
quickcommand.showInputBox(
["文件编码", "输出编码"],
"编码设置",
["基于iconv-lite进行编码无乱码请留空", "无乱码请留空"],
(res) => {
quickcommand
.showInputBox(
{
labels: ["文件编码", "输出编码"],
hints: ["基于iconv-lite进行编码无乱码请留空", "无乱码请留空"],
values: [this.scriptCode, this.outputCode],
},
"编码设置"
)
.then((res) => {
if (res) [this.scriptCode, this.outputCode] = res;
}
);
});
},
async runCurrentCommand() {
let cmd = this.$refs.editor.getEditorValue();

View File

@ -1,7 +1,7 @@
import { route } from 'quasar/wrappers'
import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router'
import routes from './routes'
import quickcommand from '../api/quickcommand'
import quickcommand from '../js/quickcommand'
window.quickcommand = quickcommand