优化命令的导入、保存和定位

This commit is contained in:
fofolee
2025-02-24 20:50:11 +08:00
parent ddee4a5017
commit 7d3074b598
8 changed files with 168 additions and 183 deletions

View File

@@ -109,11 +109,13 @@ export function useCommandManager() {
};
// 保存命令
const saveCommand = (command) => {
const saveCommand = (command, options = {}) => {
const { showMessage = true } = options;
try {
command = getValidCommand(command);
} catch (e) {
return quickcommand.showMessageBox(e.toString(), "error");
showMessage && quickcommand.showMessageBox(e.toString(), "error");
return false;
}
const code = command.features.code;
state.allQuickCommands[code] = command;
@@ -130,6 +132,9 @@ export function useCommandManager() {
}
getAllQuickCommandTags();
locateToCommand(command.tags, code);
showMessage && quickcommand.showMessageBox("保存成功!");
return code;
};
@@ -175,45 +180,76 @@ export function useCommandManager() {
};
// 导入命令
const importCommand = async (quickCommandInfo) => {
const importCommand = async (quickCommandInfo, options = {}) => {
const { showMessage = true } = options;
if (!quickCommandInfo) {
quickcommand.showMessageBox("导入未完成!", "warning");
showMessage && quickcommand.showMessageBox("导入未完成!", "warning");
return false;
}
let parsedData = await quickcommandParser(quickCommandInfo);
if (!parsedData) {
quickcommand.showMessageBox("格式错误", "error");
showMessage && quickcommand.showMessageBox("格式错误", "error");
return false;
}
let dataToPushed = {};
if (parsedData.single) {
if (isDefaultCommand(parsedData.qc.features.code)) {
quickcommand.showMessageBox("默认命令不能导入!", "error");
const { code } = parsedData.qc.features;
if (isDefaultCommand(code)) {
showMessage &&
quickcommand.showMessageBox("默认命令不能导入!", "error");
return false;
}
dataToPushed[parsedData.qc.features.code] = parsedData.qc;
dataToPushed[code] = parsedData.qc;
} else {
dataToPushed = parsedData.qc;
}
for (var code of Object.keys(dataToPushed)) {
for (const code of Object.keys(dataToPushed)) {
if (isDefaultCommand(code)) continue;
dbManager.putDB(dataToPushed[code], "qc_" + code);
}
Object.assign(state.allQuickCommands, dataToPushed);
getAllQuickCommandTags();
quickcommand.showMessageBox("导入成功!");
if (parsedData.single) {
const { tags, features } = parsedData.qc;
locateToCommand(tags, features.code);
enableCommand(features.code);
}
showMessage && quickcommand.showMessageBox("导入成功!");
return parsedData.qc;
};
// 定位命令, 包含changeCurrentTag
const locateToCommand = (tags = ["默认"], code) => {
state.currentTag = !tags || !tags.length ? "未分类" : tags[0];
if (!code) return;
// 等待 dom 渲染
nextTick(() => {
let el = document.getElementById(code);
if (!el) return;
el.scrollIntoViewIfNeeded();
el.querySelector(".q-card").style.boxShadow =
"0 1px 5px var(--q-primary), 0 2px 2px var(--q-primary), 0 3px 1px -2px var(--q-primary)";
setTimeout(() => {
el.querySelector(".q-card").style.boxShadow = "";
}, 5000);
// 跳转标签
document
.querySelector(".q-tab--active")
.scrollIntoView({ behavior: "smooth" });
});
};
// 创建命令副本
const createCommandCopy = (code) => {
const command = window.lodashM.cloneDeep(state.allQuickCommands[code]);
command.features.code = getFeatureCode(command.features.cmds);
saveCommand(command);
saveCommand(command, {
showMessage: false,
});
};
// 是否为默认命令

View File

@@ -4,15 +4,13 @@
// 是否含有 quickcommand 键值
let isJsonQc = (obj, strict = true) => {
var keys = strict
? ["features", "program", "output"]
: ["program"];
if (keys.filter((x) => typeof obj[x] == "undefined").length) return false;
const keys = strict ? ["features", "program", "output"] : ["program"];
if (keys.find((x) => !obj[x])) return false;
return true;
};
let payloadParser = async (payload) => {
let [, format, value] = payload.split("/");
const [, format, value] = payload.split("/");
if (format === "base64") return window.base64Decode(value);
else if (format === "id") return await window.getSharedQcById(value);
else throw new Error("不支持的格式");
@@ -21,15 +19,17 @@ let payloadParser = async (payload) => {
// 判断是否为可导入的快捷命令
let quickcommandParser = async (payload, strict = true) => {
try {
if (payload.slice(0, 3) === "qc/") payload = await payloadParser(payload);
var qc = JSON.parse(payload);
} catch (error) {
return false;
}
if (isJsonQc(qc, strict)) return { single: true, qc: qc };
else if (!Object.values(qc).filter((q) => !isJsonQc(q, strict)).length)
return { single: false, qc: qc };
else return false;
if (payload.slice(0, 3) === "qc/") {
payload = await payloadParser(payload);
}
const qc = JSON.parse(payload);
if (isJsonQc(qc, strict)) {
return { single: true, qc };
} else if (!Object.values(qc).find((q) => !isJsonQc(q, strict))) {
return { single: false, qc };
}
} catch (_) {}
return false;
};
export default quickcommandParser;

View File

@@ -1,48 +1,52 @@
const quickFeatures = {
favFile: {
code: "feature_favFile",
explain: "快速将选中的文件收藏到快捷命令当中",
cmds: [{
label: "收藏文件",
type: "files",
match: "/.*+/i",
}, ],
icon: "features/fav.png",
platform: ["win32", "darwin", "linux"],
favFile: {
code: "feature_favFile",
explain: "快速将选中的文件收藏到快捷命令当中",
cmds: [
{
label: "收藏文件",
type: "files",
match: "/.*+/i",
},
],
icon: "features/fav.png",
platform: ["win32", "darwin", "linux"],
mainHide: true,
},
favUrl: {
code: "feature_favUrl",
explain: "快速将选中的网址收藏到快捷命令当中",
mainHide: true,
cmds: [
{
label: "收藏网址",
type: "window",
match: {
app: [
"chrome.exe",
"firefox.exe",
"MicrosoftEdge.exe",
"iexplore.exe",
"msedge.exe",
"Google Chrome.app",
"Safari.app",
"Microsoft Edge.app",
"chrome",
"firefox",
],
},
},
],
icon: "features/fav.png",
platform: ["win32", "darwin", "linux"],
},
pluNickName: {
code: "feature_pluNickName",
explain: "为插件设置别名",
cmds: ["插件别名"],
icon: "features/plugin.png",
platform: ["win32", "darwin", "linux"],
},
};
},
favUrl: {
code: "feature_favUrl",
explain: "快速将选中的网址收藏到快捷命令当中",
cmds: [{
label: "收藏网址",
type: "window",
match: {
app: [
"chrome.exe",
"firefox.exe",
"MicrosoftEdge.exe",
"iexplore.exe",
"msedge.exe",
"Google Chrome.app",
"Safari.app",
"Microsoft Edge.app",
"chrome",
"firefox",
],
},
}, ],
icon: "features/fav.png",
platform: ["win32", "darwin", "linux"],
},
pluNickName: {
code: "feature_pluNickName",
explain: "为插件设置别名",
cmds: ["插件别名"],
icon: "features/plugin.png",
platform: ["win32", "darwin", "linux"],
}
}
export default quickFeatures
export default quickFeatures;