版本兼容处理,暗黑模式

This commit is contained in:
fofolee 2020-06-28 17:48:24 +08:00
parent cb0f6bb1a7
commit 535c06efed

View File

@ -1,182 +1,208 @@
utools.onPluginEnter(async ({ code, type, payload }) => { !function () {
var handleEnter utools.onPluginEnter(async ({ code, type, payload }) => {
utools.onPluginOut(() => { adaptDarkMode()
if (code == "code") { // oldVersionFix()
var cmd = window.editor.getValue(); var handleEnter
var program = $('#program').val(), utools.onPluginOut(() => {
scptarg = $('#scptarg').val(), // 暂存 codeRunner 的内容
customoptions; if (code == "code") {
if (program == 'custom') customoptions = { var cmd = window.editor.getValue();
custombin: $('#custombin').val(), var program = $('#program').val(),
customarg: $('#customarg').val(), scptarg = $('#scptarg').val(),
customext: $('#customext').val(), customoptions;
customcodec: $('#customcodec').val() if (program == 'custom') customoptions = {
} custombin: $('#custombin').val(),
putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory') customarg: $('#customarg').val(),
} customext: $('#customext').val(),
// 初始化 customcodec: $('#customcodec').val()
$("#options").empty()
$("#out").empty()
$("[id^=quick").remove()
swal.close()
if(handleEnter) document.removeEventListener('keydown', handleEnter)
})
// 配置页面
if (code == 'options') {
utools.setExpendHeight(600);
// $("#options").show();
showOptions();
} else if (code == 'code') {
var file = ""
// utools.setExpendHeight(600);
if (type == 'files') file = payload[0].path
showCodeEditor(file)
} else {
// console.log(new Date().getTime() - window.startTime);
utools.setExpendHeight(0);
$("#options").hide();
var db = utools.db.get('customFts').data[code],
cmd = db.cmd;
if (db.program == "custom") {
option = db.customOptions;
} else if(db.program == "simulation"){
option = "simulation";
}else{
option = programs[db.program];
}
option.scptarg = db.scptarg
cmd = special(cmd);
// 正则
if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload);
// 文件
if (type == 'files') cmd = cmd.replace(/\{\{MatchedFiles\}\}/mg, JSON.stringify(payload));
// 窗口
if (type == 'window') {
// 获取选中的文件
if (cmd.includes('{{SelectFile}}')) {
let repl = await getSelectFile(payload.id);
cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
}
// 获取资源管理器或访达当前目录
if (cmd.includes('{{pwd}}')) {
let repl = utools.getCurrentFolderPath().replace(/\\/g, '\\\\');
cmd = cmd.replace(/\{\{pwd\}\}/mg, repl)
}
// 获取窗口信息
if (cmd.includes('{{WindowInfo}}')) {
let repl = JSON.stringify(payload);
cmd = cmd.replace(/\{\{WindowInfo\}\}/mg, repl)
}
}
// 无输出的批处理
// if (db.output == 'ignore' && option.ext == 'bat') option.bin = 'explorer';
if (db.hasSubInput) {
// 启动子命令输入
// 清空输出
$("#out").empty();
var subinput = '';
var setSubInput = () => {
utools.setSubInput(({text}) => {
subinput = text;
}, '');
}
handleEnter = (event) => {
if (event.keyCode == 13) {
$("#out").append('\n-------------------------------------\n\n');
var execmd = cmd.replace(/\{\{subinput\}\}/mg, subinput);
runQuickCommand(execmd, option, db.output, true);
} }
}; putDB('history', { cmd: cmd, program: program, scptarg: scptarg, customoptions: customoptions }, 'codeHistory')
setSubInput(); }
document.addEventListener('keydown', handleEnter); // 初始化
} else { $("#options").empty()
runQuickCommand(cmd, option, db.output, false); $("#out").empty()
} $("[id^=quick").remove()
} $('body').css({overflow: 'hidden'})
}); swal.close()
if (handleEnter) document.removeEventListener('keydown', handleEnter)
})
let runQuickCommand = (cmd, option, output, autoScroll = false, autoHeight = true) => { // 配置页面
// 不需要输出的,提前关闭窗口 if (code == 'options') {
if (['ignore', 'clip', 'send', 'notice', 'terminal'].indexOf(output) !== -1) { utools.setExpendHeight(600);
utools.hideMainWindow(); // $("#options").show();
setTimeout(() => { utools.outPlugin(); }, 500); showOptions();
} } else if (code == 'code') {
var outputOpts = { type: output, autoScroll: autoScroll, autoHeight: autoHeight } var file = ""
if (option == "simulation") { // utools.setExpendHeight(600);
// 内置环境 if (type == 'files') file = payload[0].path
runCodeInVm(cmd, (stdout, stderr) => { showCodeEditor(file)
if (cmd.includes("utools.setExpendHeight")) outputOpts.autoHeight = false } else {
switchQuickCommandResult(stdout, stderr, outputOpts) // console.log(new Date().getTime() - window.startTime);
}) $('body').css({overflow: 'auto'})
} else { utools.setExpendHeight(0);
var terminal = output == 'terminal' ? true : false $("#options").hide();
outputOpts.scriptPath = getQuickCommandScriptFile(option.ext) var db = utools.db.get('customFts').data[code],
// 执行脚本 cmd = db.cmd;
runCodeFile(cmd, option, terminal, (stdout, stderr) => { if (db.program == "custom") {
switchQuickCommandResult(stdout, stderr, outputOpts) option = db.customOptions;
}) } else if(db.program == "quickcommand"){
} option = "quickcommand";
} }else{
option = programs[db.program];
switchQuickCommandResult = (stdout, stderr, outputOpts) => { }
var output = outputOpts.output, autoScroll = outputOpts.autoScroll, autoHeight = outputOpts.autoHeight; option.scptarg = db.scptarg
var outputAutoFix = (autoScroll, autoHeight) => { cmd = special(cmd);
var outputHeight = $("#out").height() + 26 // 正则
if (outputHeight > 600) outputHeight = 600 if (type == 'regex') cmd = cmd.replace(/\{\{input\}\}/mg, payload);
if (autoHeight) utools.setExpendHeight(outputHeight); // 文件
if (outputHeight == 600 && autoScroll) $(document).scrollTop($(document).height()); if (type == 'files') cmd = cmd.replace(/\{\{MatchedFiles\}\}/mg, JSON.stringify(payload));
} // 窗口
if (stderr) { if (type == 'window') {
$("#out").addClass('error') // 获取选中的文件
// 报错 if (cmd.includes('{{SelectFile}}')) {
if (output == 'text' || output == 'html') let repl = await getSelectFile(payload.id);
{ cmd = cmd.replace(/\{\{SelectFile\}\}/mg, repl)
$("#out").append(stderr) }
outputAutoFix(autoScroll, autoHeight) // 获取资源管理器或访达当前目录
} else { if (cmd.includes('{{pwd}}')) {
var index = utools.showMessageBox({ let repl = utools.getCurrentFolderPath().replace(/\\/g, '\\\\');
type: 'error', cmd = cmd.replace(/\{\{pwd\}\}/mg, repl)
title: '啊嘞?!', }
message: stderr, // 获取窗口信息
buttons: outputOpts.scriptPath ? ['转至脚本目录', '退出'] : ['退出'] if (cmd.includes('{{WindowInfo}}')) {
}) let repl = JSON.stringify(payload);
if (outputOpts.scriptPath && index == 0) { cmd = cmd.replace(/\{\{WindowInfo\}\}/mg, repl)
locate(outputOpts.scriptPath ); }
}
// 无输出的批处理
// if (db.output == 'ignore' && option.ext == 'bat') option.bin = 'explorer';
if (db.hasSubInput) {
// 启动子命令输入
// 清空输出
$("#out").empty();
var subinput = '';
var setSubInput = () => {
utools.setSubInput(({text}) => {
subinput = text;
}, '请输入');
}
handleEnter = (event) => {
if (event.keyCode == 13) {
$("#out").append(`<p style="color: #438eff">>> ${new Date()}</p>`);
var execmd = cmd.replace(/\{\{subinput\}\}/mg, subinput);
runQuickCommand(execmd, option, db.output, true);
}
};
setSubInput();
document.addEventListener('keydown', handleEnter);
} else {
runQuickCommand(cmd, option, db.output, false);
} }
copyTo(stderr);
message("已复制报错信息");
utools.outPlugin();
} }
} else if (stdout) { });
$("#out").removeClass("error")
// 有输出
switch (output) { let runQuickCommand = (cmd, option, output, autoScroll = false, autoHeight = true) => {
case "text": // 不需要输出的,提前关闭窗口
$("#out").append(htmlEncode(stdout, true)) if (['ignore', 'clip', 'send', 'notice', 'terminal'].indexOf(output) !== -1) {
outputAutoFix(autoScroll, autoHeight) utools.hideMainWindow();
break; setTimeout(() => { utools.outPlugin(); }, 500);
case "html": }
$("#out").append(stdout) var outputOpts = { type: output, autoScroll: autoScroll, autoHeight: autoHeight }
outputAutoFix(autoScroll, autoHeight) if (option == "quickcommand") {
break; // 内置环境
case "clip": runCodeInVm(cmd, (stdout, stderr) => {
copyTo(stdout) if (cmd.includes("utools.setExpendHeight")) outputOpts.autoHeight = false
break; switchQuickCommandResult(stdout, stderr, outputOpts)
case "send": })
send(stdout) } else {
break; var terminal = output == 'terminal' ? true : false
case "notice": outputOpts.scriptPath = getQuickCommandScriptFile(option.ext)
// 发送系统通知 // 执行脚本
message(stdout) runCodeFile(cmd, option, terminal, (stdout, stderr) => {
break; switchQuickCommandResult(stdout, stderr, outputOpts)
case "ignore": })
break;
default:
break;
} }
} else {
// 无输出
utools.outPlugin()
} }
}
let switchQuickCommandResult = (stdout, stderr, outputOpts) => {
var output = outputOpts.type, autoScroll = outputOpts.autoScroll, autoHeight = outputOpts.autoHeight;
var outputAutoFix = (autoScroll, autoHeight) => {
var outputHeight = $("#out").height() + 26
if (outputHeight > 600) outputHeight = 600
if (autoHeight) utools.setExpendHeight(outputHeight);
if (outputHeight == 600 && autoScroll) $(document).scrollTop($(document).height());
}
if (stderr) {
$("#out").addClass('error')
// 报错
if (output == 'text' || output == 'html')
{
$("#out").append(stderr)
outputAutoFix(autoScroll, autoHeight)
} else {
var index = utools.showMessageBox({
type: 'error',
title: '啊嘞?!',
message: stderr,
buttons: outputOpts.scriptPath ? ['转至脚本目录', '退出'] : ['退出']
})
if (outputOpts.scriptPath && index == 0) {
locate(outputOpts.scriptPath );
}
copyTo(stderr);
message("已复制报错信息");
utools.outPlugin();
}
} else if (stdout) {
$("#out").removeClass("error")
// 有输出
switch (output) {
case "text":
$("#out").append(htmlEncode(stdout, true))
outputAutoFix(autoScroll, autoHeight)
break;
case "html":
$("#out").append(stdout)
outputAutoFix(autoScroll, autoHeight)
break;
case "clip":
copyTo(stdout)
break;
case "send":
send(stdout)
break;
case "notice":
// 发送系统通知
message(stdout)
break;
case "ignore":
break;
default:
break;
}
} else {
// 无输出
utools.outPlugin()
}
}
// 替换上个版本弃用的功能
let oldVersionFix = () => {
var customFts = getDB('customFts');
Object.keys(customFts).forEach(x => {
if (customFts[x].program == 'simulation') customFts[x].program = 'quickcommand';
if (customFts[x].cmd.includes('await sleep')) customFts[x].cmd = customFts[x].cmd.replace(/await sleep/g, 'quickcommand.sleep')
putDB(x, customFts[x], 'customFts');
})
}
// 兼容暗黑模式
let adaptDarkMode = () => {
if (utools.isDarkColors()) {
!$('#darkmode').length && $('head').append('<link id="darkmode" rel="stylesheet" href="assets/style/darkmode.css">')
} else {
$('#darkmode').length && $('#darkmode').remove()
}
}
}()