Files
kiss-translator/src/hooks/Rules.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import {
STOKEY_RULES,
OPT_TRANS_ALL,
OPT_STYLE_ALL,
OPT_LANGS_FROM,
OPT_LANGS_TO,
2023-08-08 13:29:15 +08:00
GLOBAL_KEY,
2023-07-20 13:45:41 +08:00
} from "../config";
import storage from "../libs/storage";
import { useStorages } from "./Storage";
import { matchValue } from "../libs/utils";
2023-07-31 15:08:51 +08:00
import { syncRules } from "../libs/sync";
import { useSync } from "./Sync";
2023-07-20 13:45:41 +08:00
/**
* 匹配规则增删改查 hook
* @returns
*/
export function useRules() {
const storages = useStorages();
2023-07-28 15:34:05 +08:00
const list = storages?.[STOKEY_RULES] || [];
2023-07-31 15:08:51 +08:00
const sync = useSync();
2023-07-20 13:45:41 +08:00
2023-07-31 03:10:09 +08:00
const update = async (rules) => {
2023-07-31 15:08:51 +08:00
const updateAt = sync.opt?.rulesUpdateAt ? Date.now() : 0;
2023-07-31 03:10:09 +08:00
await storage.setObj(STOKEY_RULES, rules);
2023-07-31 15:08:51 +08:00
await sync.update({ rulesUpdateAt: updateAt });
2023-08-17 13:27:22 +08:00
syncRules();
2023-07-31 03:10:09 +08:00
};
2023-07-20 13:45:41 +08:00
const add = async (rule) => {
2023-07-28 15:34:05 +08:00
const rules = [...list];
2023-07-20 13:45:41 +08:00
if (rule.pattern === "*") {
return;
}
if (rules.map((item) => item.pattern).includes(rule.pattern)) {
return;
}
2023-07-28 15:34:05 +08:00
rules.unshift(rule);
2023-07-31 03:10:09 +08:00
await update(rules);
2023-07-20 13:45:41 +08:00
};
const del = async (pattern) => {
2023-07-28 15:34:05 +08:00
let rules = [...list];
2023-07-20 13:45:41 +08:00
if (pattern === "*") {
return;
}
2023-07-28 15:34:05 +08:00
rules = rules.filter((item) => item.pattern !== pattern);
2023-07-31 03:10:09 +08:00
await update(rules);
2023-07-20 13:45:41 +08:00
};
2023-07-28 15:34:05 +08:00
const put = async (pattern, obj) => {
const rules = [...list];
if (pattern === "*") {
2023-07-20 13:45:41 +08:00
obj.pattern = "*";
}
2023-07-28 15:34:05 +08:00
const rule = rules.find((r) => r.pattern === pattern);
rule && Object.assign(rule, obj);
2023-07-31 03:10:09 +08:00
await update(rules);
2023-07-20 13:45:41 +08:00
};
const merge = async (newRules) => {
2023-07-31 03:10:09 +08:00
const rules = [...list];
2023-07-20 13:45:41 +08:00
const fromLangs = OPT_LANGS_FROM.map((item) => item[0]);
const toLangs = OPT_LANGS_TO.map((item) => item[0]);
newRules
2023-08-08 13:29:15 +08:00
.filter(({ pattern }) => pattern && typeof pattern === "string")
2023-07-20 13:45:41 +08:00
.map(
({
pattern,
selector,
translator,
fromLang,
toLang,
textStyle,
transOpen,
2023-08-08 16:41:47 +08:00
bgColor,
2023-07-20 13:45:41 +08:00
}) => ({
pattern,
2023-08-08 13:29:15 +08:00
selector: typeof selector === "string" ? selector : "",
2023-08-08 16:41:47 +08:00
bgColor: typeof bgColor === "string" ? bgColor : "",
2023-08-08 13:29:15 +08:00
translator: matchValue([GLOBAL_KEY, ...OPT_TRANS_ALL], translator),
fromLang: matchValue([GLOBAL_KEY, ...fromLangs], fromLang),
toLang: matchValue([GLOBAL_KEY, ...toLangs], toLang),
textStyle: matchValue([GLOBAL_KEY, ...OPT_STYLE_ALL], textStyle),
transOpen: matchValue([GLOBAL_KEY, "true", "false"], transOpen),
2023-07-20 13:45:41 +08:00
})
)
.forEach((newRule) => {
const rule = rules.find(
(oldRule) => oldRule.pattern === newRule.pattern
);
if (rule) {
Object.assign(rule, newRule);
} else {
rules.unshift(newRule);
}
});
2023-07-31 03:10:09 +08:00
await update(rules);
2023-07-20 13:45:41 +08:00
};
2023-07-28 15:34:05 +08:00
return { list, add, del, put, merge };
2023-07-20 13:45:41 +08:00
}