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

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

@ -1,9 +1,19 @@
<template>
<div></div>
</template>
<script> <script>
import { useCommandManager } from "js/commandManager";
import { getUniqueId } from "js/common/uuid.js";
export default { export default {
mounted() { mounted() {
const commandManager = useCommandManager();
utools.setExpendHeight(0); utools.setExpendHeight(0);
this.$root.enterData.payload.forEach((file) => { this.$root.enterData.payload.forEach((file) => {
let uid = this.getUid(); let uid = getUniqueId({
short: true,
});
let fileInfo = window.getFileInfo({ let fileInfo = window.getFileInfo({
type: "file", type: "file",
argvs: file.path, argvs: file.path,
@ -17,24 +27,17 @@ export default {
icon: utools.getFileIcon(file.path), icon: utools.getFileIcon(file.path),
platform: [window.processPlatform], platform: [window.processPlatform],
code: `key_${uid}`, code: `key_${uid}`,
mainHide: true,
}, },
program: "quickcommand", program: "quickcommand",
cmd: `open(\"${file.path.replace(/\\/g, "\\\\")}\")`, cmd: `utools.shellOpenPath(\"${file.path.replace(/\\/g, "\\\\")}\")`,
output: "ignore", output: "ignore",
tags: [this.$root.profile.quickFileTag], tags: [this.$root.profile.quickFileTag],
}; };
this.importCommand(command); this.commandManager.importCommand(JSON.stringify(command));
}); });
utools.showNotification("操作成功!"); utools.showNotification("操作成功!");
utools.outPlugin(); utools.outPlugin();
}, },
methods: {
getUid() {
return this.$parent.getUid();
},
importCommand(command) {
this.$parent.importCommand(command);
},
},
}; };
</script> </script>

View File

@ -1,76 +1,60 @@
<template>
<div></div>
</template>
<script> <script>
import { getUniqueId } from "js/common/uuid.js";
import { useCommandManager } from "js/commandManager";
export default { export default {
data() {
return {
cmdCtrlKey: window.processPlatform === "darwin" ? "command" : "control",
};
},
async mounted() { async mounted() {
const commandManager = useCommandManager();
utools.setExpendHeight(0); utools.setExpendHeight(0);
utools.hideMainWindow(); utools.hideMainWindow();
// getCurrentBrowserUrl let url = utools.getCurrentBrowserUrl();
// let url = utools.getCurrentBrowserUrl();
utools.simulateKeyboardTap("l", this.cmdCtrlKey);
await this.wait(50);
utools.simulateKeyboardTap("c", this.cmdCtrlKey);
await this.wait(50);
let url = window.clipboardReadText();
if (!/^http/.test(url)) { if (!/^http/.test(url)) {
utools.showMainWindow(); const choise = await quickcommand.showSystemButtonBox(
utools.setExpendHeight(550);
let choise = await quickcommand.showButtonBox(
["http", "https"], ["http", "https"],
"当前浏览器网址显示不完整,请问访问的页面是哪一种?" "当前浏览器网址显示不完整,请问访问的页面是哪一种?"
); );
url = choise.text + "://" + url; url = choise.text + "://" + url;
} }
let title = this.$root.enterData.payload.title const title = this.$root.enterData.payload.title
.replace(/和另外 \d+ 个页面.*/, "") .replace(/和另外 \d+ 个页面.*/, "")
.replace(/[-|—] .*?[Edge|Firefox|Chrome].*/, "") .replace(/[-|—] .*?[Edge|Firefox|Chrome].*/, "")
.trim(); .trim();
// let req = await axios(url) // let req = await axios(url)
// let title = quickcommand.htmlParse(req.data).querySelector('title').innerText // let title = quickcommand.htmlParse(req.data).querySelector('title').innerText
let base = /(http(s){0,1}:\/\/.*?(:\d+){0,1})(\/|$).*/.exec(url)[1]; const base = /(http(s){0,1}:\/\/.*?(:\d+){0,1})(\/|$).*/.exec(url)[1];
let iconUrl = base + "/favicon.ico"; const iconUrl = base + "/favicon.ico";
let iconPath = window.joinPath( const iconPath = window.joinPath(
utools.getPath("temp"), utools.getPath("temp"),
"quickcommandfavicon.ico" "quickcommandfavicon.ico"
); );
let uid = this.getUid(); const uid = getUniqueId({
let command = { short: true,
});
const command = {
features: { features: {
explain: title, explain: title,
cmds: [title], cmds: [title],
platform: ["linux", "win32", "darwin"], platform: ["linux", "win32", "darwin"],
code: `key_${uid}`, code: `key_${uid}`,
mainHide: true,
icon: "features/fav.png",
}, },
program: "quickcommand", program: "quickcommand",
cmd: `visit(\"${url}\")\n`, cmd: `utools.shellOpenExternal(\"${url}\")\n`,
output: "ignore", output: "ignore",
tags: [this.$root.profile.quickUrlTag], tags: [this.$root.profile.quickUrlTag],
}; };
try { try {
let res = await quickcommand.downloadFile(iconUrl, iconPath); const res = await quickcommand.downloadFile(iconUrl, iconPath);
if (res) command.features.icon = window.resolveFileToBase64(iconPath); if (res) command.features.icon = window.resolveFileToBase64(iconPath);
} catch (e) {} } catch (_) {}
this.importCommand(command); commandManager.importCommand(JSON.stringify(command));
utools.showNotification("操作成功!"); utools.showNotification("操作成功!");
utools.outPlugin(); utools.outPlugin();
}, },
methods: {
wait(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
},
getUid() {
return this.$parent.getUid();
},
importCommand(command) {
this.$parent.importCommand(command);
},
},
}; };
</script> </script>

View File

@ -85,6 +85,9 @@
</template> </template>
<script> <script>
import { useCommandManager } from "js/commandManager";
import { getUniqueId } from "js/common/uuid.js";
export default { export default {
data() { data() {
return { return {
@ -113,10 +116,13 @@ export default {
}, },
methods: { methods: {
addPluNickName() { addPluNickName() {
const commandManager = useCommandManager();
if (!this.nickName.length) if (!this.nickName.length)
return quickcommand.showMessageBox("请填写别名", "warning"); return quickcommand.showMessageBox("请填写别名", "warning");
let uid = this.getUid(); const uid = getUniqueId({
let command = { short: true,
});
const command = {
features: { features: {
cmds: this.nickName, cmds: this.nickName,
explain: this.feature.explain, explain: this.feature.explain,
@ -129,16 +135,12 @@ export default {
output: "ignore", output: "ignore",
tags: [this.$root.profile.pluNickNameTag], tags: [this.$root.profile.pluNickNameTag],
}; };
this.importCommand(command); commandManager.importCommand(JSON.stringify(command), {
showMessage: false,
});
this.nickName = []; this.nickName = [];
quickcommand.showMessageBox("添加成功!"); quickcommand.showMessageBox("添加成功!");
}, },
getUid() {
return this.$parent.getUid();
},
importCommand(command) {
this.$parent.importCommand(command);
},
}, },
}; };
</script> </script>

View File

@ -109,11 +109,13 @@ export function useCommandManager() {
}; };
// 保存命令 // 保存命令
const saveCommand = (command) => { const saveCommand = (command, options = {}) => {
const { showMessage = true } = options;
try { try {
command = getValidCommand(command); command = getValidCommand(command);
} catch (e) { } catch (e) {
return quickcommand.showMessageBox(e.toString(), "error"); showMessage && quickcommand.showMessageBox(e.toString(), "error");
return false;
} }
const code = command.features.code; const code = command.features.code;
state.allQuickCommands[code] = command; state.allQuickCommands[code] = command;
@ -130,6 +132,9 @@ export function useCommandManager() {
} }
getAllQuickCommandTags(); getAllQuickCommandTags();
locateToCommand(command.tags, code);
showMessage && quickcommand.showMessageBox("保存成功!");
return code; return code;
}; };
@ -175,45 +180,76 @@ export function useCommandManager() {
}; };
// 导入命令 // 导入命令
const importCommand = async (quickCommandInfo) => { const importCommand = async (quickCommandInfo, options = {}) => {
const { showMessage = true } = options;
if (!quickCommandInfo) { if (!quickCommandInfo) {
quickcommand.showMessageBox("导入未完成!", "warning"); showMessage && quickcommand.showMessageBox("导入未完成!", "warning");
return false; return false;
} }
let parsedData = await quickcommandParser(quickCommandInfo); let parsedData = await quickcommandParser(quickCommandInfo);
if (!parsedData) { if (!parsedData) {
quickcommand.showMessageBox("格式错误", "error"); showMessage && quickcommand.showMessageBox("格式错误", "error");
return false; return false;
} }
let dataToPushed = {}; let dataToPushed = {};
if (parsedData.single) { if (parsedData.single) {
if (isDefaultCommand(parsedData.qc.features.code)) { const { code } = parsedData.qc.features;
quickcommand.showMessageBox("默认命令不能导入!", "error"); if (isDefaultCommand(code)) {
showMessage &&
quickcommand.showMessageBox("默认命令不能导入!", "error");
return false; return false;
} }
dataToPushed[parsedData.qc.features.code] = parsedData.qc; dataToPushed[code] = parsedData.qc;
} else { } else {
dataToPushed = parsedData.qc; dataToPushed = parsedData.qc;
} }
for (var code of Object.keys(dataToPushed)) { for (const code of Object.keys(dataToPushed)) {
if (isDefaultCommand(code)) continue; if (isDefaultCommand(code)) continue;
dbManager.putDB(dataToPushed[code], "qc_" + code); dbManager.putDB(dataToPushed[code], "qc_" + code);
} }
Object.assign(state.allQuickCommands, dataToPushed); Object.assign(state.allQuickCommands, dataToPushed);
getAllQuickCommandTags(); getAllQuickCommandTags();
quickcommand.showMessageBox("导入成功!"); if (parsedData.single) {
const { tags, features } = parsedData.qc;
locateToCommand(tags, features.code);
enableCommand(features.code);
}
showMessage && quickcommand.showMessageBox("导入成功!");
return parsedData.qc; 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 createCommandCopy = (code) => {
const command = window.lodashM.cloneDeep(state.allQuickCommands[code]); const command = window.lodashM.cloneDeep(state.allQuickCommands[code]);
command.features.code = getFeatureCode(command.features.cmds); command.features.code = getFeatureCode(command.features.cmds);
saveCommand(command); saveCommand(command, {
showMessage: false,
});
}; };
// 是否为默认命令 // 是否为默认命令

View File

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

View File

@ -1,48 +1,52 @@
const quickFeatures = { const quickFeatures = {
favFile: { favFile: {
code: "feature_favFile", code: "feature_favFile",
explain: "快速将选中的文件收藏到快捷命令当中", explain: "快速将选中的文件收藏到快捷命令当中",
cmds: [{ cmds: [
label: "收藏文件", {
type: "files", label: "收藏文件",
match: "/.*+/i", type: "files",
}, ], match: "/.*+/i",
icon: "features/fav.png", },
platform: ["win32", "darwin", "linux"], ],
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"],
},
};
}, export default quickFeatures;
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

View File

@ -96,11 +96,13 @@ export default {
methods: { methods: {
// //
initPage() { initPage() {
// newcommand if (this.fromNewCommand) {
if (this.fromNewCommand) this.addNewCommand(); // newcommand
// importcommand this.addNewCommand();
else if (this.fromImportCommand) } else if (this.fromImportCommand) {
this.importCommand(this.$root.enterData.payload); // importcommand
this.commandManager.importCommand(this.$root.enterData.payload);
}
this.$router.push("/configuration"); this.$router.push("/configuration");
if (this.$route.params.tags) { if (this.$route.params.tags) {
this.changeCurrentTag(window.hexDecode(this.$route.params.tags)); this.changeCurrentTag(window.hexDecode(this.$route.params.tags));
@ -178,32 +180,6 @@ export default {
window.lodashM.cloneDeep(command); window.lodashM.cloneDeep(command);
this.isEditorShow = true; this.isEditorShow = true;
}, },
//
async importCommand(command) {
const result = await this.commandManager.importCommand(command);
if (result) {
this.locateToCommand(result.tags, result.features?.code);
}
},
// , changeCurrentTag
locateToCommand(tags = ["默认"], code) {
this.currentTag = !tags || !tags.length ? "未分类" : tags[0];
if (!code) return;
// dom
this.$nextTick(() => {
let el = document.getElementById(code);
el.scrollIntoViewIfNeeded();
el.querySelector(".q-card").style.boxShadow =
"0px 1px var(--q-primary)";
setTimeout(() => {
el.querySelector(".q-card").style.boxShadow = "";
}, 5000);
//
document
.querySelector(".q-tab--active")
.scrollIntoView({ behavior: "smooth" });
});
},
// //
addNewCommand(program = "quickcommand") { addNewCommand(program = "quickcommand") {
this.editorComponent = this.editorComponent =
@ -212,16 +188,10 @@ export default {
this.commandManager.getDefaultCommand(program); this.commandManager.getDefaultCommand(program);
this.isEditorShow = true; this.isEditorShow = true;
}, },
saveCommand(command) {
const code = this.commandManager.saveCommand(command);
if (!code) return;
this.locateToCommand(command.tags, code);
quickcommand.showMessageBox("保存成功!");
},
handleEditorEvent(event, data) { handleEditorEvent(event, data) {
switch (event) { switch (event) {
case "save": case "save":
this.saveCommand(data); this.commandManager.saveCommand(data);
break; break;
case "back": case "back":
this.isEditorShow = false; this.isEditorShow = false;

View File

@ -7,7 +7,6 @@ import PluginNickName from "components/quickFeatures/PluginNickName";
import FavFile from "components/quickFeatures/FavFile"; import FavFile from "components/quickFeatures/FavFile";
import FavUrl from "components/quickFeatures/FavUrl"; import FavUrl from "components/quickFeatures/FavUrl";
import { markRaw } from "vue"; import { markRaw } from "vue";
import { utoolsFull, dbManager } from "js/utools.js";
export default { export default {
components: { components: {
@ -17,21 +16,8 @@ export default {
}, },
data() { data() {
return { return {
utools: utoolsFull,
currentComponent: this.$route.params.featuretype, currentComponent: this.$route.params.featuretype,
}; };
}, },
methods: {
importCommand(command) {
command = window.lodashM.cloneDeep(command);
dbManager.putDB(command, "qc_" + command.features.code);
this.utools.setFeature(command.features);
},
getUid() {
return Number(
Math.random().toString().substr(3, 3) + Date.now()
).toString(36);
},
},
}; };
</script> </script>