feat: 封装 enterData, 命令图标改进

This commit is contained in:
lee 2020-10-15 21:52:52 +08:00
parent 60968ac334
commit 9e8e0287b0
3 changed files with 25 additions and 28 deletions

View File

@ -88,7 +88,7 @@
if (db.program == "custom") {
option = db.customOptions;
} else if(db.program == "quickcommand"){
option = { mode: "quickcommand", payload: payload };
option = { mode: "quickcommand", enterData: { code, type, payload } };
}else{
option = programs[db.program];
}
@ -174,7 +174,7 @@
runCodeInVm(cmd, (stdout, stderr) => {
if (cmd.includes("utools.setExpendHeight")) outputOpts.autoHeight = false
switchQuickCommandResult(stdout, stderr, outputOpts)
}, option.payload)
}, option.enterData)
} else {
var terminal = output == 'terminal' ? true : false
outputOpts.scriptPath = getQuickCommandScriptFile(option.ext)
@ -871,6 +871,8 @@
$("#ruleWord").html("配 置");
$(".var.regex, .var.window, .var.files").prop("disabled", false)
$("#rule").prop("placeholder", '等效于 features.cmds');
let sample = `["关键词",{"type":"img","label":"图片匹配"},{"type":"files","label":"文件匹配","fileType":"file","match":"/aaa/","minLength":1,"maxLength":99},{"type":"regex","label":"文本正则匹配","match":"/bbb/i","minLength":1,"maxLength":99},{"type":"over","label":"无匹配时","exclude":"/ccc/i","minLength":1,"maxLength":99},{"type":"window","label":"窗口动作","match":{"app":["ddd.app","eee.exe"],"title":"/fff/","class":["ggg"]}}]`
!$('#rule').val() && $('#rule').val(sample)
default:
break;
}
@ -886,7 +888,7 @@
let hasCustomIcon = () => {
var src = $("#icon").attr('src');
var iconame = $("#iconame").val();
return /data:image\/png;base64,/.test(src) || iconame
return /data:image\/\w+;base64,/.test(src) || iconame
}
let programCheck = () => {
@ -1339,18 +1341,16 @@
})
// 选择图标
$("#options").on('click', '#icon', function () {
$("#options").on('click', '#icon', async function () {
var options = {
buttonLabel: '选择',
filters: [{
name: 'Images',
extensions: ['png']
}, ]
properties: ['openFile']
}
var file = getFileInfo({ type: 'dialog', argvs: options, readfile: false })
if (file) {
$("#iconame").val(file.name);
$("#icon").attr('src', file.path);
let src = await getBase64Ico(file.path);
$("#icon").attr('src', src);
}
})
@ -1373,18 +1373,9 @@
scptarg = $('#scptarg').val(),
program = $('#program').val(),
desc = $('#desc').val(),
iconame = $("#iconame").val(),
iconpath = $("#icon").attr('src'),
icon,
icon = $("#icon").attr('src'),
hasSubInput;
if (!desc) desc = ' ';
// 选择了图标的情况下
if (iconame) {
icon = await window.getBase64Ico(iconpath);
// 未自定义使用默认
} else {
icon = iconpath;
}
if (type == 'key') {
cmds = rule.split(",").map(x => x.trim())
} else if (type == 'regex') {

View File

@ -4,7 +4,7 @@
"main": "index.html",
"homepage": "https://github.com/fofolee/uTools-QuickerCommand",
"publishPage": "https://yuanliao.info/d/424",
"version": "2.1.0",
"version": "2.1.1",
"author": "云之轩",
"unpack":"autopep8.py",
"logo": "logo.png",

View File

@ -440,9 +440,10 @@ let getSandboxFuns = () => {
return sandbox
}
let createNodeVM = (payload = "") => {
let createNodeVM = (enterData = {}) => {
var sandbox = getSandboxFuns()
sandbox.quickcommand.payload = payload
sandbox.quickcommand.enterData = enterData
sandbox.quickcommand.payload = enterData.payload
const vm = new NodeVM({
require: {
external: true,
@ -488,8 +489,8 @@ let parseItem = item => {
return item.toString()
}
runCodeInVm = (cmd, cb, payload = "") => {
const vm = createNodeVM(payload)
runCodeInVm = (cmd, cb, enterData = {}) => {
const vm = createNodeVM(enterData)
//重定向 console
vm.on('console.log', stdout => {
console.log(stdout);
@ -596,7 +597,7 @@ dirPythonMod = (mod, cb) => {
getNodeJsCommand = () => {
var obj = getSandboxFuns()
obj.Buffer = Buffer
obj.quickcommand.payload = ''
obj.quickcommand.enterData = {code: '', type: '', payload: ''}
return obj
}
@ -621,9 +622,14 @@ getQuickCommandScriptFile = ext => {
return path.join(os.tmpdir(), `QuickCommandTempScript.${ext}`)
}
getBase64Ico = async path => {
let sourceImage = 'data:image/png;base64,' + fs.readFileSync(path, 'base64')
let compressedImage = await getCompressedIco(path)
getBase64Ico = async filepath => {
let sourceImage, ext = path.extname(filepath).slice(1)
if (ext == 'png' || ext == 'jpg' || ext == 'jpeg' || ext == 'bmp' || ext == 'ico') {
sourceImage = `data:image/${ext};base64,` + fs.readFileSync(filepath, 'base64')
} else {
sourceImage = utools.getFileIcon(filepath)
}
let compressedImage = await getCompressedIco(sourceImage)
return compressedImage.length > sourceImage.length ? sourceImage : compressedImage
}