mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-07 13:34:08 +08:00
优化命令的导入、保存和定位
This commit is contained in:
parent
ddee4a5017
commit
7d3074b598
@ -1,9 +1,19 @@
|
||||
<template>
|
||||
<div></div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useCommandManager } from "js/commandManager";
|
||||
import { getUniqueId } from "js/common/uuid.js";
|
||||
|
||||
export default {
|
||||
mounted() {
|
||||
const commandManager = useCommandManager();
|
||||
utools.setExpendHeight(0);
|
||||
this.$root.enterData.payload.forEach((file) => {
|
||||
let uid = this.getUid();
|
||||
let uid = getUniqueId({
|
||||
short: true,
|
||||
});
|
||||
let fileInfo = window.getFileInfo({
|
||||
type: "file",
|
||||
argvs: file.path,
|
||||
@ -17,24 +27,17 @@ export default {
|
||||
icon: utools.getFileIcon(file.path),
|
||||
platform: [window.processPlatform],
|
||||
code: `key_${uid}`,
|
||||
mainHide: true,
|
||||
},
|
||||
program: "quickcommand",
|
||||
cmd: `open(\"${file.path.replace(/\\/g, "\\\\")}\")`,
|
||||
cmd: `utools.shellOpenPath(\"${file.path.replace(/\\/g, "\\\\")}\")`,
|
||||
output: "ignore",
|
||||
tags: [this.$root.profile.quickFileTag],
|
||||
};
|
||||
this.importCommand(command);
|
||||
this.commandManager.importCommand(JSON.stringify(command));
|
||||
});
|
||||
utools.showNotification("操作成功!");
|
||||
utools.outPlugin();
|
||||
},
|
||||
methods: {
|
||||
getUid() {
|
||||
return this.$parent.getUid();
|
||||
},
|
||||
importCommand(command) {
|
||||
this.$parent.importCommand(command);
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
@ -1,76 +1,60 @@
|
||||
<template>
|
||||
<div></div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getUniqueId } from "js/common/uuid.js";
|
||||
import { useCommandManager } from "js/commandManager";
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
cmdCtrlKey: window.processPlatform === "darwin" ? "command" : "control",
|
||||
};
|
||||
},
|
||||
async mounted() {
|
||||
const commandManager = useCommandManager();
|
||||
utools.setExpendHeight(0);
|
||||
utools.hideMainWindow();
|
||||
// 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();
|
||||
let url = utools.getCurrentBrowserUrl();
|
||||
if (!/^http/.test(url)) {
|
||||
utools.showMainWindow();
|
||||
utools.setExpendHeight(550);
|
||||
let choise = await quickcommand.showButtonBox(
|
||||
const choise = await quickcommand.showSystemButtonBox(
|
||||
["http", "https"],
|
||||
"当前浏览器网址显示不完整,请问访问的页面是哪一种?"
|
||||
);
|
||||
url = choise.text + "://" + url;
|
||||
}
|
||||
let title = this.$root.enterData.payload.title
|
||||
const title = this.$root.enterData.payload.title
|
||||
.replace(/和另外 \d+ 个页面.*/, "")
|
||||
.replace(/[-|—] .*?[Edge|Firefox|Chrome].*/, "")
|
||||
.trim();
|
||||
// let req = await axios(url)
|
||||
// let title = quickcommand.htmlParse(req.data).querySelector('title').innerText
|
||||
let base = /(http(s){0,1}:\/\/.*?(:\d+){0,1})(\/|$).*/.exec(url)[1];
|
||||
let iconUrl = base + "/favicon.ico";
|
||||
let iconPath = window.joinPath(
|
||||
const base = /(http(s){0,1}:\/\/.*?(:\d+){0,1})(\/|$).*/.exec(url)[1];
|
||||
const iconUrl = base + "/favicon.ico";
|
||||
const iconPath = window.joinPath(
|
||||
utools.getPath("temp"),
|
||||
"quickcommandfavicon.ico"
|
||||
);
|
||||
let uid = this.getUid();
|
||||
let command = {
|
||||
const uid = getUniqueId({
|
||||
short: true,
|
||||
});
|
||||
const command = {
|
||||
features: {
|
||||
explain: title,
|
||||
cmds: [title],
|
||||
platform: ["linux", "win32", "darwin"],
|
||||
code: `key_${uid}`,
|
||||
mainHide: true,
|
||||
icon: "features/fav.png",
|
||||
},
|
||||
program: "quickcommand",
|
||||
cmd: `visit(\"${url}\")\n`,
|
||||
cmd: `utools.shellOpenExternal(\"${url}\")\n`,
|
||||
output: "ignore",
|
||||
tags: [this.$root.profile.quickUrlTag],
|
||||
};
|
||||
try {
|
||||
let res = await quickcommand.downloadFile(iconUrl, iconPath);
|
||||
const res = await quickcommand.downloadFile(iconUrl, iconPath);
|
||||
if (res) command.features.icon = window.resolveFileToBase64(iconPath);
|
||||
} catch (e) {}
|
||||
this.importCommand(command);
|
||||
} catch (_) {}
|
||||
commandManager.importCommand(JSON.stringify(command));
|
||||
utools.showNotification("操作成功!");
|
||||
utools.outPlugin();
|
||||
},
|
||||
methods: {
|
||||
wait(ms) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, ms);
|
||||
});
|
||||
},
|
||||
getUid() {
|
||||
return this.$parent.getUid();
|
||||
},
|
||||
importCommand(command) {
|
||||
this.$parent.importCommand(command);
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
@ -85,6 +85,9 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useCommandManager } from "js/commandManager";
|
||||
import { getUniqueId } from "js/common/uuid.js";
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -113,10 +116,13 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
addPluNickName() {
|
||||
const commandManager = useCommandManager();
|
||||
if (!this.nickName.length)
|
||||
return quickcommand.showMessageBox("请填写别名", "warning");
|
||||
let uid = this.getUid();
|
||||
let command = {
|
||||
const uid = getUniqueId({
|
||||
short: true,
|
||||
});
|
||||
const command = {
|
||||
features: {
|
||||
cmds: this.nickName,
|
||||
explain: this.feature.explain,
|
||||
@ -129,16 +135,12 @@ export default {
|
||||
output: "ignore",
|
||||
tags: [this.$root.profile.pluNickNameTag],
|
||||
};
|
||||
this.importCommand(command);
|
||||
commandManager.importCommand(JSON.stringify(command), {
|
||||
showMessage: false,
|
||||
});
|
||||
this.nickName = [];
|
||||
quickcommand.showMessageBox("添加成功!");
|
||||
},
|
||||
getUid() {
|
||||
return this.$parent.getUid();
|
||||
},
|
||||
importCommand(command) {
|
||||
this.$parent.importCommand(command);
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
@ -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,
|
||||
});
|
||||
};
|
||||
|
||||
// 是否为默认命令
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -96,11 +96,13 @@ export default {
|
||||
methods: {
|
||||
// 初始化
|
||||
initPage() {
|
||||
// newcommand 直接新建命令
|
||||
if (this.fromNewCommand) this.addNewCommand();
|
||||
// importcommand 导入命令
|
||||
else if (this.fromImportCommand)
|
||||
this.importCommand(this.$root.enterData.payload);
|
||||
if (this.fromNewCommand) {
|
||||
// newcommand 直接新建命令
|
||||
this.addNewCommand();
|
||||
} else if (this.fromImportCommand) {
|
||||
// importcommand 导入命令
|
||||
this.commandManager.importCommand(this.$root.enterData.payload);
|
||||
}
|
||||
this.$router.push("/configuration");
|
||||
if (this.$route.params.tags) {
|
||||
this.changeCurrentTag(window.hexDecode(this.$route.params.tags));
|
||||
@ -178,32 +180,6 @@ export default {
|
||||
window.lodashM.cloneDeep(command);
|
||||
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") {
|
||||
this.editorComponent =
|
||||
@ -212,16 +188,10 @@ export default {
|
||||
this.commandManager.getDefaultCommand(program);
|
||||
this.isEditorShow = true;
|
||||
},
|
||||
saveCommand(command) {
|
||||
const code = this.commandManager.saveCommand(command);
|
||||
if (!code) return;
|
||||
this.locateToCommand(command.tags, code);
|
||||
quickcommand.showMessageBox("保存成功!");
|
||||
},
|
||||
handleEditorEvent(event, data) {
|
||||
switch (event) {
|
||||
case "save":
|
||||
this.saveCommand(data);
|
||||
this.commandManager.saveCommand(data);
|
||||
break;
|
||||
case "back":
|
||||
this.isEditorShow = false;
|
||||
|
@ -7,7 +7,6 @@ import PluginNickName from "components/quickFeatures/PluginNickName";
|
||||
import FavFile from "components/quickFeatures/FavFile";
|
||||
import FavUrl from "components/quickFeatures/FavUrl";
|
||||
import { markRaw } from "vue";
|
||||
import { utoolsFull, dbManager } from "js/utools.js";
|
||||
|
||||
export default {
|
||||
components: {
|
||||
@ -17,21 +16,8 @@ export default {
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
utools: utoolsFull,
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user