Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
450283b80a | ||
|
|
44aeed03a6 | ||
|
|
fa4569415d | ||
|
|
a341bf30ba | ||
|
|
34a7354c84 | ||
|
|
21b5dfbe98 | ||
|
|
c1920f5cdd | ||
|
|
3e24568df9 | ||
|
|
b785cfe854 | ||
|
|
15367bd117 | ||
|
|
d7eaac5aca | ||
|
|
d4526d605c | ||
|
|
52979356ca | ||
|
|
c6d3d6454f | ||
|
|
0d7112187d | ||
|
|
045ff3c3d9 | ||
|
|
dd68a73efd |
2
.env
2
.env
@@ -2,7 +2,7 @@ GENERATE_SOURCEMAP=false
|
|||||||
|
|
||||||
REACT_APP_NAME=KISS Translator
|
REACT_APP_NAME=KISS Translator
|
||||||
REACT_APP_NAME_CN=简约翻译
|
REACT_APP_NAME_CN=简约翻译
|
||||||
REACT_APP_VERSION=1.7.9
|
REACT_APP_VERSION=1.7.11
|
||||||
|
|
||||||
REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator
|
REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,13 @@ A simple [bilingual translation extension & Greasemonkey script](https://github.
|
|||||||
- [x] Chrome/Edge/Firefox/Kiwi
|
- [x] Chrome/Edge/Firefox/Kiwi
|
||||||
- [ ] Safari
|
- [ ] Safari
|
||||||
- [x] Supports multiple translation services
|
- [x] Supports multiple translation services
|
||||||
- [x] Google/Microsoft/DeepL/OpenAI/Baidu/Tencent
|
- [x] Google/Microsoft/DeepL/OpenAI/CloudflareAI/Baidu/Tencent
|
||||||
- [x] Custom translation interface
|
- [x] Custom translation interface
|
||||||
- [x] Covers common translation scenarios
|
- [x] Covers common translation scenarios
|
||||||
- [x] Web bilingual translation
|
- [x] Web bilingual translation
|
||||||
- [x] Input box translation
|
- [x] Input box translation
|
||||||
- [x] Seletction translation
|
- [x] Seletction translation
|
||||||
|
- [x] Favorite Words
|
||||||
- [x] Mouseover translation
|
- [x] Mouseover translation
|
||||||
- [x] YouTube subtitle translation
|
- [x] YouTube subtitle translation
|
||||||
- [x] Cross-client data synchronization
|
- [x] Cross-client data synchronization
|
||||||
|
|||||||
@@ -12,12 +12,13 @@
|
|||||||
- [x] Chrome/Edge/Firefox/Kiwi
|
- [x] Chrome/Edge/Firefox/Kiwi
|
||||||
- [ ] Safari
|
- [ ] Safari
|
||||||
- [x] 支持多种翻译服务
|
- [x] 支持多种翻译服务
|
||||||
- [x] Google/Microsoft/DeepL/OpenAI/Baidu/Tencent
|
- [x] Google/Microsoft/DeepL/OpenAI/CloudflareAI/Baidu/Tencent
|
||||||
- [x] 自定义翻译接口
|
- [x] 自定义翻译接口
|
||||||
- [x] 覆盖常见翻译场景
|
- [x] 覆盖常见翻译场景
|
||||||
- [x] 网页双语对照翻译
|
- [x] 网页双语对照翻译
|
||||||
- [x] 输入框翻译
|
- [x] 输入框翻译
|
||||||
- [x] 划词翻译
|
- [x] 划词翻译
|
||||||
|
- [x] 收藏词汇
|
||||||
- [x] 鼠标悬停翻译
|
- [x] 鼠标悬停翻译
|
||||||
- [x] YouTube 字幕翻译
|
- [x] YouTube 字幕翻译
|
||||||
- [x] 跨客户端数据同步
|
- [x] 跨客户端数据同步
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "kiss-translator",
|
"name": "kiss-translator",
|
||||||
"description": "A minimalist bilingual translation Extension & Greasemonkey Script",
|
"description": "A minimalist bilingual translation Extension & Greasemonkey Script",
|
||||||
"version": "1.7.9",
|
"version": "1.7.11",
|
||||||
"author": "Gabe<yugang2002@gmail.com>",
|
"author": "Gabe<yugang2002@gmail.com>",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "__MSG_app_name__",
|
"name": "__MSG_app_name__",
|
||||||
"description": "__MSG_app_description__",
|
"description": "__MSG_app_description__",
|
||||||
"version": "1.7.9",
|
"version": "1.7.11",
|
||||||
"default_locale": "en",
|
"default_locale": "en",
|
||||||
"author": "Gabe<yugang2002@gmail.com>",
|
"author": "Gabe<yugang2002@gmail.com>",
|
||||||
"homepage_url": "https://github.com/fishjar/kiss-translator",
|
"homepage_url": "https://github.com/fishjar/kiss-translator",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "__MSG_app_name__",
|
"name": "__MSG_app_name__",
|
||||||
"description": "__MSG_app_description__",
|
"description": "__MSG_app_description__",
|
||||||
"version": "1.7.9",
|
"version": "1.7.11",
|
||||||
"default_locale": "en",
|
"default_locale": "en",
|
||||||
"author": "Gabe<yugang2002@gmail.com>",
|
"author": "Gabe<yugang2002@gmail.com>",
|
||||||
"homepage_url": "https://github.com/fishjar/kiss-translator",
|
"homepage_url": "https://github.com/fishjar/kiss-translator",
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
OPT_TRANS_BAIDU,
|
OPT_TRANS_BAIDU,
|
||||||
OPT_TRANS_TENCENT,
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
OPT_TRANS_CUSTOMIZE,
|
OPT_TRANS_CUSTOMIZE,
|
||||||
URL_CACHE_TRAN,
|
URL_CACHE_TRAN,
|
||||||
KV_SALT_SYNC,
|
KV_SALT_SYNC,
|
||||||
@@ -176,12 +177,15 @@ export const apiTranslate = async ({
|
|||||||
trText = res?.choices?.[0].message.content;
|
trText = res?.choices?.[0].message.content;
|
||||||
isSame = text === trText;
|
isSame = text === trText;
|
||||||
break;
|
break;
|
||||||
|
case OPT_TRANS_CLOUDFLAREAI:
|
||||||
|
trText = res?.result?.translated_text;
|
||||||
|
isSame = text === trText;
|
||||||
|
break;
|
||||||
case OPT_TRANS_CUSTOMIZE:
|
case OPT_TRANS_CUSTOMIZE:
|
||||||
trText = res.text;
|
trText = res.text;
|
||||||
isSame = to === res.from;
|
isSame = to === res.from;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [trText, isSame, res];
|
return [trText, isSame, res];
|
||||||
|
|||||||
@@ -555,11 +555,11 @@ export const I18N = {
|
|||||||
zh: `全局规则`,
|
zh: `全局规则`,
|
||||||
en: `Global Rule`,
|
en: `Global Rule`,
|
||||||
},
|
},
|
||||||
input_setting: {
|
input_translate: {
|
||||||
zh: `输入框设置`,
|
zh: `输入框翻译`,
|
||||||
en: `Input Box Setting`,
|
en: `Input Box Translation`,
|
||||||
},
|
},
|
||||||
input_box_translation: {
|
use_input_box_translation: {
|
||||||
zh: `启用输入框翻译`,
|
zh: `启用输入框翻译`,
|
||||||
en: `Input Box Translation`,
|
en: `Input Box Translation`,
|
||||||
},
|
},
|
||||||
@@ -639,4 +639,8 @@ export const I18N = {
|
|||||||
zh: `原文`,
|
zh: `原文`,
|
||||||
en: `Original Text`,
|
en: `Original Text`,
|
||||||
},
|
},
|
||||||
|
favorite_words: {
|
||||||
|
zh: `收藏词汇`,
|
||||||
|
en: `Favorite Words`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export const STOKEY_MSAUTH = `${APP_NAME}_msauth`;
|
|||||||
export const STOKEY_BDAUTH = `${APP_NAME}_bdauth`;
|
export const STOKEY_BDAUTH = `${APP_NAME}_bdauth`;
|
||||||
export const STOKEY_SETTING = `${APP_NAME}_setting`;
|
export const STOKEY_SETTING = `${APP_NAME}_setting`;
|
||||||
export const STOKEY_RULES = `${APP_NAME}_rules`;
|
export const STOKEY_RULES = `${APP_NAME}_rules`;
|
||||||
|
export const STOKEY_WORDS = `${APP_NAME}_words`;
|
||||||
export const STOKEY_SYNC = `${APP_NAME}_sync`;
|
export const STOKEY_SYNC = `${APP_NAME}_sync`;
|
||||||
export const STOKEY_FAB = `${APP_NAME}_fab`;
|
export const STOKEY_FAB = `${APP_NAME}_fab`;
|
||||||
export const STOKEY_RULESCACHE_PREFIX = `${APP_NAME}_rulescache_`;
|
export const STOKEY_RULESCACHE_PREFIX = `${APP_NAME}_rulescache_`;
|
||||||
@@ -40,6 +41,7 @@ export const CLIENT_USERSCRIPT = "userscript";
|
|||||||
export const CLIENT_EXTS = [CLIENT_CHROME, CLIENT_EDGE, CLIENT_FIREFOX];
|
export const CLIENT_EXTS = [CLIENT_CHROME, CLIENT_EDGE, CLIENT_FIREFOX];
|
||||||
|
|
||||||
export const KV_RULES_KEY = "kiss-rules.json";
|
export const KV_RULES_KEY = "kiss-rules.json";
|
||||||
|
export const KV_WORDS_KEY = "kiss-words.json";
|
||||||
export const KV_RULES_SHARE_KEY = "kiss-rules-share.json";
|
export const KV_RULES_SHARE_KEY = "kiss-rules-share.json";
|
||||||
export const KV_SETTING_KEY = "kiss-setting.json";
|
export const KV_SETTING_KEY = "kiss-setting.json";
|
||||||
export const KV_SALT_SYNC = "KISS-Translator-SYNC";
|
export const KV_SALT_SYNC = "KISS-Translator-SYNC";
|
||||||
@@ -87,6 +89,7 @@ export const OPT_TRANS_DEEPLFREE = "DeepLFree";
|
|||||||
export const OPT_TRANS_BAIDU = "Baidu";
|
export const OPT_TRANS_BAIDU = "Baidu";
|
||||||
export const OPT_TRANS_TENCENT = "Tencent";
|
export const OPT_TRANS_TENCENT = "Tencent";
|
||||||
export const OPT_TRANS_OPENAI = "OpenAI";
|
export const OPT_TRANS_OPENAI = "OpenAI";
|
||||||
|
export const OPT_TRANS_CLOUDFLAREAI = "CloudflareAI";
|
||||||
export const OPT_TRANS_CUSTOMIZE = "Custom";
|
export const OPT_TRANS_CUSTOMIZE = "Custom";
|
||||||
export const OPT_TRANS_ALL = [
|
export const OPT_TRANS_ALL = [
|
||||||
OPT_TRANS_GOOGLE,
|
OPT_TRANS_GOOGLE,
|
||||||
@@ -97,6 +100,7 @@ export const OPT_TRANS_ALL = [
|
|||||||
OPT_TRANS_BAIDU,
|
OPT_TRANS_BAIDU,
|
||||||
OPT_TRANS_TENCENT,
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
OPT_TRANS_CUSTOMIZE,
|
OPT_TRANS_CUSTOMIZE,
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -218,6 +222,20 @@ export const OPT_LANGS_SPECIAL = {
|
|||||||
[OPT_TRANS_OPENAI]: new Map(
|
[OPT_TRANS_OPENAI]: new Map(
|
||||||
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
|
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
|
||||||
),
|
),
|
||||||
|
[OPT_TRANS_CLOUDFLAREAI]: new Map([
|
||||||
|
["auto", ""],
|
||||||
|
["zh-CN", "chinese"],
|
||||||
|
["zh-TW", "chinese"],
|
||||||
|
["en", "english"],
|
||||||
|
["ar", "arabic"],
|
||||||
|
["de", "german"],
|
||||||
|
["ru", "russian"],
|
||||||
|
["fr", "french"],
|
||||||
|
["pt", "portuguese"],
|
||||||
|
["ja", "japanese"],
|
||||||
|
["es", "spanish"],
|
||||||
|
["hi", "hindi"],
|
||||||
|
]),
|
||||||
[OPT_TRANS_CUSTOMIZE]: new Map([
|
[OPT_TRANS_CUSTOMIZE]: new Map([
|
||||||
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
||||||
["auto", ""],
|
["auto", ""],
|
||||||
@@ -360,6 +378,10 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
model: "gpt-4",
|
model: "gpt-4",
|
||||||
prompt: `You will be provided with a sentence in ${PROMPT_PLACE_FROM}, and your task is to translate it into ${PROMPT_PLACE_TO}.`,
|
prompt: `You will be provided with a sentence in ${PROMPT_PLACE_FROM}, and your task is to translate it into ${PROMPT_PLACE_TO}.`,
|
||||||
},
|
},
|
||||||
|
[OPT_TRANS_CLOUDFLAREAI]: {
|
||||||
|
url: "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/m2m100-1.2b",
|
||||||
|
key: "",
|
||||||
|
},
|
||||||
[OPT_TRANS_CUSTOMIZE]: {
|
[OPT_TRANS_CUSTOMIZE]: {
|
||||||
url: "",
|
url: "",
|
||||||
key: "",
|
key: "",
|
||||||
|
|||||||
67
src/hooks/FavWords.js
Normal file
67
src/hooks/FavWords.js
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import { KV_WORDS_KEY } from "../config";
|
||||||
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
import { trySyncWords } from "../libs/sync";
|
||||||
|
import { getWordsWithDefault, setWords } from "../libs/storage";
|
||||||
|
import { useSyncMeta } from "./Sync";
|
||||||
|
|
||||||
|
export function useFavWords() {
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [favWords, setFavWords] = useState({});
|
||||||
|
const { updateSyncMeta } = useSyncMeta();
|
||||||
|
|
||||||
|
const toggleFav = useCallback(
|
||||||
|
async (word) => {
|
||||||
|
const favs = { ...favWords };
|
||||||
|
if (favs[word]) {
|
||||||
|
delete favs[word];
|
||||||
|
} else {
|
||||||
|
favs[word] = { createdAt: Date.now() };
|
||||||
|
}
|
||||||
|
await setWords(favs);
|
||||||
|
await updateSyncMeta(KV_WORDS_KEY);
|
||||||
|
await trySyncWords();
|
||||||
|
setFavWords(favs);
|
||||||
|
},
|
||||||
|
[updateSyncMeta, favWords]
|
||||||
|
);
|
||||||
|
|
||||||
|
const mergeWords = useCallback(
|
||||||
|
async (newWords) => {
|
||||||
|
const favs = { ...favWords };
|
||||||
|
newWords.forEach((word) => {
|
||||||
|
if (!favs[word]) {
|
||||||
|
favs[word] = { createdAt: Date.now() };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await setWords(favs);
|
||||||
|
await updateSyncMeta(KV_WORDS_KEY);
|
||||||
|
await trySyncWords();
|
||||||
|
setFavWords(favs);
|
||||||
|
},
|
||||||
|
[updateSyncMeta, favWords]
|
||||||
|
);
|
||||||
|
|
||||||
|
const clearWords = useCallback(async () => {
|
||||||
|
await setWords({});
|
||||||
|
await updateSyncMeta(KV_WORDS_KEY);
|
||||||
|
await trySyncWords();
|
||||||
|
setFavWords({});
|
||||||
|
}, [updateSyncMeta]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await trySyncWords();
|
||||||
|
const favWords = await getWordsWithDefault();
|
||||||
|
setFavWords(favWords);
|
||||||
|
} catch (err) {
|
||||||
|
console.log("[query fav]", err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { loading, favWords, toggleFav, mergeWords, clearWords };
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
OPT_TRANS_BAIDU,
|
OPT_TRANS_BAIDU,
|
||||||
OPT_TRANS_TENCENT,
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
OPT_TRANS_CUSTOMIZE,
|
OPT_TRANS_CUSTOMIZE,
|
||||||
URL_MICROSOFT_TRAN,
|
URL_MICROSOFT_TRAN,
|
||||||
URL_TENCENT_TRANSMART,
|
URL_TENCENT_TRANSMART,
|
||||||
@@ -143,7 +144,7 @@ const genTencent = ({ text, from, to }) => {
|
|||||||
return [URL_TENCENT_TRANSMART, init];
|
return [URL_TENCENT_TRANSMART, init];
|
||||||
};
|
};
|
||||||
|
|
||||||
const genOpenai = ({ text, from, to, url, key, prompt, model }) => {
|
const genOpenAI = ({ text, from, to, url, key, prompt, model }) => {
|
||||||
prompt = prompt
|
prompt = prompt
|
||||||
.replaceAll(PROMPT_PLACE_FROM, from)
|
.replaceAll(PROMPT_PLACE_FROM, from)
|
||||||
.replaceAll(PROMPT_PLACE_TO, to);
|
.replaceAll(PROMPT_PLACE_TO, to);
|
||||||
@@ -177,6 +178,25 @@ const genOpenai = ({ text, from, to, url, key, prompt, model }) => {
|
|||||||
return [url, init];
|
return [url, init];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const genCloudflareAI = ({ text, from, to, url, key }) => {
|
||||||
|
const data = {
|
||||||
|
text,
|
||||||
|
source_lang: from,
|
||||||
|
target_lang: to,
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = {
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json",
|
||||||
|
Authorization: `Bearer ${key}`,
|
||||||
|
},
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
};
|
||||||
|
|
||||||
|
return [url, init];
|
||||||
|
};
|
||||||
|
|
||||||
const genCustom = ({ text, from, to, url, key }) => {
|
const genCustom = ({ text, from, to, url, key }) => {
|
||||||
const data = {
|
const data = {
|
||||||
text,
|
text,
|
||||||
@@ -197,6 +217,11 @@ const genCustom = ({ text, from, to, url, key }) => {
|
|||||||
return [url, init];
|
return [url, init];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造翻译接口 request
|
||||||
|
* @param {*}
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
|
export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
|
||||||
const args = { text, from, to, ...apiSetting };
|
const args = { text, from, to, ...apiSetting };
|
||||||
switch (translator) {
|
switch (translator) {
|
||||||
@@ -215,7 +240,9 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
|
|||||||
case OPT_TRANS_TENCENT:
|
case OPT_TRANS_TENCENT:
|
||||||
return genTencent(args);
|
return genTencent(args);
|
||||||
case OPT_TRANS_OPENAI:
|
case OPT_TRANS_OPENAI:
|
||||||
return genOpenai(args);
|
return genOpenAI(args);
|
||||||
|
case OPT_TRANS_CLOUDFLAREAI:
|
||||||
|
return genCloudflareAI(args);
|
||||||
case OPT_TRANS_CUSTOMIZE:
|
case OPT_TRANS_CUSTOMIZE:
|
||||||
return genCustom(args);
|
return genCustom(args);
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
STOKEY_SETTING,
|
STOKEY_SETTING,
|
||||||
STOKEY_RULES,
|
STOKEY_RULES,
|
||||||
|
STOKEY_WORDS,
|
||||||
STOKEY_FAB,
|
STOKEY_FAB,
|
||||||
STOKEY_SYNC,
|
STOKEY_SYNC,
|
||||||
STOKEY_MSAUTH,
|
STOKEY_MSAUTH,
|
||||||
@@ -97,6 +98,13 @@ export const getRulesWithDefault = async () =>
|
|||||||
(await getRules()) || DEFAULT_RULES;
|
(await getRules()) || DEFAULT_RULES;
|
||||||
export const setRules = (val) => setObj(STOKEY_RULES, val);
|
export const setRules = (val) => setObj(STOKEY_RULES, val);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 词汇列表
|
||||||
|
*/
|
||||||
|
export const getWords = () => getObj(STOKEY_WORDS);
|
||||||
|
export const getWordsWithDefault = async () => (await getWords()) || {};
|
||||||
|
export const setWords = (val) => setObj(STOKEY_WORDS, val);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 订阅规则
|
* 订阅规则
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import {
|
|||||||
APP_LCNAME,
|
APP_LCNAME,
|
||||||
KV_SETTING_KEY,
|
KV_SETTING_KEY,
|
||||||
KV_RULES_KEY,
|
KV_RULES_KEY,
|
||||||
|
KV_WORDS_KEY,
|
||||||
KV_RULES_SHARE_KEY,
|
KV_RULES_SHARE_KEY,
|
||||||
KV_SALT_SHARE,
|
KV_SALT_SHARE,
|
||||||
OPT_SYNCTYPE_WEBDAV,
|
OPT_SYNCTYPE_WEBDAV,
|
||||||
@@ -11,8 +12,10 @@ import {
|
|||||||
updateSync,
|
updateSync,
|
||||||
getSettingWithDefault,
|
getSettingWithDefault,
|
||||||
getRulesWithDefault,
|
getRulesWithDefault,
|
||||||
|
getWordsWithDefault,
|
||||||
setSetting,
|
setSetting,
|
||||||
setRules,
|
setRules,
|
||||||
|
setWords,
|
||||||
} from "./storage";
|
} from "./storage";
|
||||||
import { apiSyncData } from "../apis";
|
import { apiSyncData } from "../apis";
|
||||||
import { sha256, removeEndchar } from "./utils";
|
import { sha256, removeEndchar } from "./utils";
|
||||||
@@ -135,6 +138,25 @@ export const trySyncRules = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步词汇
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const syncWords = async () => {
|
||||||
|
const res = await syncData(KV_WORDS_KEY, getWordsWithDefault);
|
||||||
|
if (res?.isNew) {
|
||||||
|
await setWords(res.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const trySyncWords = async () => {
|
||||||
|
try {
|
||||||
|
await syncWords();
|
||||||
|
} catch (err) {
|
||||||
|
console.log("[sync fav words]", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步分享规则
|
* 同步分享规则
|
||||||
* @param {*} param0
|
* @param {*} param0
|
||||||
@@ -163,9 +185,11 @@ export const syncShareRules = async ({ rules, syncUrl, syncKey }) => {
|
|||||||
export const syncSettingAndRules = async () => {
|
export const syncSettingAndRules = async () => {
|
||||||
await syncSetting();
|
await syncSetting();
|
||||||
await syncRules();
|
await syncRules();
|
||||||
|
await syncWords();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const trySyncSettingAndRules = async () => {
|
export const trySyncSettingAndRules = async () => {
|
||||||
await trySyncSetting();
|
await trySyncSetting();
|
||||||
await trySyncRules();
|
await trySyncRules();
|
||||||
|
await trySyncWords();
|
||||||
};
|
};
|
||||||
|
|||||||
27
src/views/Options/DownloadButton.js
Normal file
27
src/views/Options/DownloadButton.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import FileDownloadIcon from "@mui/icons-material/FileDownload";
|
||||||
|
import Button from "@mui/material/Button";
|
||||||
|
|
||||||
|
export default function DownloadButton({ data, text, fileName }) {
|
||||||
|
const handleClick = (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (data) {
|
||||||
|
const url = window.URL.createObjectURL(new Blob([data]));
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = url;
|
||||||
|
link.setAttribute("download", fileName || `${Date.now()}.json`);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
link.remove();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
onClick={handleClick}
|
||||||
|
startIcon={<FileDownloadIcon />}
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
150
src/views/Options/FavWords.js
Normal file
150
src/views/Options/FavWords.js
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import { OPT_TRANS_BAIDU } from "../../config";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
|
import Accordion from "@mui/material/Accordion";
|
||||||
|
import AccordionSummary from "@mui/material/AccordionSummary";
|
||||||
|
import AccordionDetails from "@mui/material/AccordionDetails";
|
||||||
|
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
|
||||||
|
import CircularProgress from "@mui/material/CircularProgress";
|
||||||
|
import { useI18n } from "../../hooks/I18n";
|
||||||
|
import Alert from "@mui/material/Alert";
|
||||||
|
import { apiTranslate } from "../../apis";
|
||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import { useFavWords } from "../../hooks/FavWords";
|
||||||
|
import DictCont from "../Selection/DictCont";
|
||||||
|
import DownloadButton from "./DownloadButton";
|
||||||
|
import UploadButton from "./UploadButton";
|
||||||
|
import Button from "@mui/material/Button";
|
||||||
|
import ClearAllIcon from "@mui/icons-material/ClearAll";
|
||||||
|
import { isValidWord } from "../../libs/utils";
|
||||||
|
|
||||||
|
function DictField({ word }) {
|
||||||
|
const [dictResult, setDictResult] = useState(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
setError("");
|
||||||
|
const dictRes = await apiTranslate({
|
||||||
|
text: word,
|
||||||
|
translator: OPT_TRANS_BAIDU,
|
||||||
|
fromLang: "en",
|
||||||
|
toLang: "zh-CN",
|
||||||
|
});
|
||||||
|
setDictResult(dictRes[2].dict_result);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err.message);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, [word]);
|
||||||
|
|
||||||
|
if (loading) {
|
||||||
|
return <CircularProgress size={24} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return <Alert severity="error">{error}</Alert>;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <DictCont dictResult={dictResult} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
function FavAccordion({ word, index }) {
|
||||||
|
const [expanded, setExpanded] = useState(false);
|
||||||
|
|
||||||
|
const handleChange = (e) => {
|
||||||
|
setExpanded((pre) => !pre);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Accordion expanded={expanded} onChange={handleChange}>
|
||||||
|
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
||||||
|
{/* <Typography>{`[${new Date(
|
||||||
|
createdAt
|
||||||
|
).toLocaleString()}] ${word}`}</Typography> */}
|
||||||
|
<Typography>{`${index + 1}. ${word}`}</Typography>
|
||||||
|
</AccordionSummary>
|
||||||
|
<AccordionDetails>
|
||||||
|
{expanded && <DictField word={word} />}
|
||||||
|
</AccordionDetails>
|
||||||
|
</Accordion>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function FavWords() {
|
||||||
|
const i18n = useI18n();
|
||||||
|
const { loading, favWords, mergeWords, clearWords } = useFavWords();
|
||||||
|
const favList = Object.entries(favWords).sort((a, b) =>
|
||||||
|
a[0].localeCompare(b[0])
|
||||||
|
);
|
||||||
|
const downloadList = favList.map(([word]) => word);
|
||||||
|
|
||||||
|
const handleImport = async (data) => {
|
||||||
|
try {
|
||||||
|
const newWords = data
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.split(",")[0].trim())
|
||||||
|
.filter(isValidWord);
|
||||||
|
await mergeWords(newWords);
|
||||||
|
} catch (err) {
|
||||||
|
console.log("[import rules]", err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box>
|
||||||
|
<Stack spacing={3}>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
spacing={2}
|
||||||
|
useFlexGap
|
||||||
|
flexWrap="wrap"
|
||||||
|
>
|
||||||
|
<UploadButton
|
||||||
|
text={i18n("import")}
|
||||||
|
handleImport={handleImport}
|
||||||
|
fileType="text"
|
||||||
|
fileExts={[".txt", ".csv"]}
|
||||||
|
/>
|
||||||
|
<DownloadButton
|
||||||
|
data={downloadList.join("\n")}
|
||||||
|
text={i18n("export")}
|
||||||
|
fileName={`kiss-words_${Date.now()}.txt`}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
onClick={() => {
|
||||||
|
clearWords();
|
||||||
|
}}
|
||||||
|
startIcon={<ClearAllIcon />}
|
||||||
|
>
|
||||||
|
{i18n("clear_all")}
|
||||||
|
</Button>
|
||||||
|
</Stack>
|
||||||
|
|
||||||
|
<Box>
|
||||||
|
{loading ? (
|
||||||
|
<CircularProgress size={24} />
|
||||||
|
) : (
|
||||||
|
favList.map(([word, { createdAt }], index) => (
|
||||||
|
<FavAccordion
|
||||||
|
key={word}
|
||||||
|
index={index}
|
||||||
|
word={word}
|
||||||
|
createdAt={createdAt}
|
||||||
|
/>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -67,7 +67,7 @@ export default function InputSetting() {
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
label={i18n("input_box_translation")}
|
label={i18n("use_input_box_translation")}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import SyncIcon from "@mui/icons-material/Sync";
|
|||||||
import ApiIcon from "@mui/icons-material/Api";
|
import ApiIcon from "@mui/icons-material/Api";
|
||||||
import SendTimeExtensionIcon from "@mui/icons-material/SendTimeExtension";
|
import SendTimeExtensionIcon from "@mui/icons-material/SendTimeExtension";
|
||||||
import InputIcon from "@mui/icons-material/Input";
|
import InputIcon from "@mui/icons-material/Input";
|
||||||
import TranslateIcon from '@mui/icons-material/Translate';
|
import SelectAllIcon from '@mui/icons-material/SelectAll';
|
||||||
|
import EventNoteIcon from '@mui/icons-material/EventNote';
|
||||||
|
|
||||||
function LinkItem({ label, url, icon }) {
|
function LinkItem({ label, url, icon }) {
|
||||||
const match = useMatch(url);
|
const match = useMatch(url);
|
||||||
@@ -41,8 +42,8 @@ export default function Navigator(props) {
|
|||||||
icon: <DesignServicesIcon />,
|
icon: <DesignServicesIcon />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "input_setting",
|
id: "input_translate",
|
||||||
label: i18n("input_setting"),
|
label: i18n("input_translate"),
|
||||||
url: "/input",
|
url: "/input",
|
||||||
icon: <InputIcon />,
|
icon: <InputIcon />,
|
||||||
},
|
},
|
||||||
@@ -50,7 +51,7 @@ export default function Navigator(props) {
|
|||||||
id: "selection_translate",
|
id: "selection_translate",
|
||||||
label: i18n("selection_translate"),
|
label: i18n("selection_translate"),
|
||||||
url: "/tranbox",
|
url: "/tranbox",
|
||||||
icon: <TranslateIcon />,
|
icon: <SelectAllIcon />,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "apis_setting",
|
id: "apis_setting",
|
||||||
@@ -70,6 +71,12 @@ export default function Navigator(props) {
|
|||||||
url: "/webfix",
|
url: "/webfix",
|
||||||
icon: <SendTimeExtensionIcon />,
|
icon: <SendTimeExtensionIcon />,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "words",
|
||||||
|
label: i18n("favorite_words"),
|
||||||
|
url: "/words",
|
||||||
|
icon: <EventNoteIcon />,
|
||||||
|
},
|
||||||
{ id: "about", label: i18n("about"), url: "/about", icon: <InfoIcon /> },
|
{ id: "about", label: i18n("about"), url: "/about", icon: <InfoIcon /> },
|
||||||
];
|
];
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {
|
|||||||
URL_KISS_RULES_NEW_ISSUE,
|
URL_KISS_RULES_NEW_ISSUE,
|
||||||
OPT_SYNCTYPE_WORKER,
|
OPT_SYNCTYPE_WORKER,
|
||||||
} from "../../config";
|
} from "../../config";
|
||||||
import { useState, useRef, useEffect, useMemo } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import { useI18n } from "../../hooks/I18n";
|
import { useI18n } from "../../hooks/I18n";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
import Accordion from "@mui/material/Accordion";
|
import Accordion from "@mui/material/Accordion";
|
||||||
@@ -26,8 +26,6 @@ import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
|
|||||||
import { useRules } from "../../hooks/Rules";
|
import { useRules } from "../../hooks/Rules";
|
||||||
import MenuItem from "@mui/material/MenuItem";
|
import MenuItem from "@mui/material/MenuItem";
|
||||||
import Grid from "@mui/material/Grid";
|
import Grid from "@mui/material/Grid";
|
||||||
import FileDownloadIcon from "@mui/icons-material/FileDownload";
|
|
||||||
import FileUploadIcon from "@mui/icons-material/FileUpload";
|
|
||||||
import { useSetting } from "../../hooks/Setting";
|
import { useSetting } from "../../hooks/Setting";
|
||||||
import FormControlLabel from "@mui/material/FormControlLabel";
|
import FormControlLabel from "@mui/material/FormControlLabel";
|
||||||
import Switch from "@mui/material/Switch";
|
import Switch from "@mui/material/Switch";
|
||||||
@@ -50,6 +48,8 @@ import OwSubRule from "./OwSubRule";
|
|||||||
import ClearAllIcon from "@mui/icons-material/ClearAll";
|
import ClearAllIcon from "@mui/icons-material/ClearAll";
|
||||||
import HelpButton from "./HelpButton";
|
import HelpButton from "./HelpButton";
|
||||||
import { useSyncCaches } from "../../hooks/Sync";
|
import { useSyncCaches } from "../../hooks/Sync";
|
||||||
|
import DownloadButton from "./DownloadButton";
|
||||||
|
import UploadButton from "./UploadButton";
|
||||||
|
|
||||||
function RuleFields({ rule, rules, setShow, setKeyword }) {
|
function RuleFields({ rule, rules, setShow, setKeyword }) {
|
||||||
const initFormValues = rule || {
|
const initFormValues = rule || {
|
||||||
@@ -375,8 +375,9 @@ function RuleAccordion({ rule, rules }) {
|
|||||||
<Accordion expanded={expanded} onChange={handleChange}>
|
<Accordion expanded={expanded} onChange={handleChange}>
|
||||||
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
|
||||||
<Typography
|
<Typography
|
||||||
style={{
|
sx={{
|
||||||
opacity: rules ? 1 : 0.5,
|
opacity: rules ? 1 : 0.5,
|
||||||
|
overflowWrap: "anywhere",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{rule.pattern === GLOBAL_KEY
|
{rule.pattern === GLOBAL_KEY
|
||||||
@@ -391,56 +392,6 @@ function RuleAccordion({ rule, rules }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function DownloadButton({ data, text, fileName }) {
|
|
||||||
const handleClick = (e) => {
|
|
||||||
e.preventDefault();
|
|
||||||
if (data) {
|
|
||||||
const url = window.URL.createObjectURL(new Blob([data]));
|
|
||||||
const link = document.createElement("a");
|
|
||||||
link.href = url;
|
|
||||||
link.setAttribute("download", fileName || `${Date.now()}.json`);
|
|
||||||
document.body.appendChild(link);
|
|
||||||
link.click();
|
|
||||||
link.remove();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return (
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
variant="outlined"
|
|
||||||
onClick={handleClick}
|
|
||||||
startIcon={<FileDownloadIcon />}
|
|
||||||
>
|
|
||||||
{text}
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function UploadButton({ onChange, text }) {
|
|
||||||
const inputRef = useRef(null);
|
|
||||||
const handleClick = () => {
|
|
||||||
inputRef.current && inputRef.current.click();
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
variant="outlined"
|
|
||||||
onClick={handleClick}
|
|
||||||
startIcon={<FileUploadIcon />}
|
|
||||||
>
|
|
||||||
{text}
|
|
||||||
<input
|
|
||||||
type="file"
|
|
||||||
accept=".json"
|
|
||||||
ref={inputRef}
|
|
||||||
onChange={onChange}
|
|
||||||
hidden
|
|
||||||
/>
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function ShareButton({ rules, injectRules, selectedUrl }) {
|
function ShareButton({ rules, injectRules, selectedUrl }) {
|
||||||
const alert = useAlert();
|
const alert = useAlert();
|
||||||
const i18n = useI18n();
|
const i18n = useI18n();
|
||||||
@@ -493,26 +444,12 @@ function UserRules({ subRules }) {
|
|||||||
const injectRules = !!setting?.injectRules;
|
const injectRules = !!setting?.injectRules;
|
||||||
const { selectedUrl, selectedRules } = subRules;
|
const { selectedUrl, selectedRules } = subRules;
|
||||||
|
|
||||||
const handleImport = (e) => {
|
const handleImport = async (data) => {
|
||||||
const file = e.target.files[0];
|
try {
|
||||||
if (!file) {
|
await rules.merge(JSON.parse(data));
|
||||||
return;
|
} catch (err) {
|
||||||
|
console.log("[import rules]", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.type.includes("json")) {
|
|
||||||
alert(i18n("error_wrong_file_type"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const reader = new FileReader();
|
|
||||||
reader.onload = async (e) => {
|
|
||||||
try {
|
|
||||||
await rules.merge(JSON.parse(e.target.result));
|
|
||||||
} catch (err) {
|
|
||||||
console.log("[import rules]", err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reader.readAsText(file);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInject = () => {
|
const handleInject = () => {
|
||||||
@@ -552,10 +489,11 @@ function UserRules({ subRules }) {
|
|||||||
{i18n("add")}
|
{i18n("add")}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<UploadButton text={i18n("import")} onChange={handleImport} />
|
<UploadButton text={i18n("import")} handleImport={handleImport} />
|
||||||
<DownloadButton
|
<DownloadButton
|
||||||
data={JSON.stringify([...rules.list].reverse(), null, 2)}
|
data={JSON.stringify([...rules.list].reverse(), null, 2)}
|
||||||
text={i18n("export")}
|
text={i18n("export")}
|
||||||
|
fileName={`kiss-rules_${Date.now()}.json`}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ShareButton
|
<ShareButton
|
||||||
@@ -663,7 +601,14 @@ function SubRulesItem({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack direction="row" alignItems="center" spacing={2}>
|
<Stack direction="row" alignItems="center" spacing={2}>
|
||||||
<FormControlLabel value={url} control={<Radio />} label={url} />
|
<FormControlLabel
|
||||||
|
value={url}
|
||||||
|
control={<Radio />}
|
||||||
|
sx={{
|
||||||
|
overflowWrap: "anywhere",
|
||||||
|
}}
|
||||||
|
label={url}
|
||||||
|
/>
|
||||||
|
|
||||||
{syncAt && (
|
{syncAt && (
|
||||||
<span style={{ marginLeft: "0.5em", opacity: 0.5 }}>
|
<span style={{ marginLeft: "0.5em", opacity: 0.5 }}>
|
||||||
|
|||||||
55
src/views/Options/UploadButton.js
Normal file
55
src/views/Options/UploadButton.js
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { useRef } from "react";
|
||||||
|
import FileUploadIcon from "@mui/icons-material/FileUpload";
|
||||||
|
import { useI18n } from "../../hooks/I18n";
|
||||||
|
import Button from "@mui/material/Button";
|
||||||
|
|
||||||
|
export default function UploadButton({
|
||||||
|
handleImport,
|
||||||
|
text,
|
||||||
|
fileType = "json",
|
||||||
|
fileExts = [".json"],
|
||||||
|
}) {
|
||||||
|
const i18n = useI18n();
|
||||||
|
const inputRef = useRef(null);
|
||||||
|
const handleClick = () => {
|
||||||
|
if (inputRef.current) {
|
||||||
|
inputRef.current.click();
|
||||||
|
inputRef.current.value = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const onChange = (e) => {
|
||||||
|
const file = e.target.files[0];
|
||||||
|
if (!file) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.type.includes(fileType)) {
|
||||||
|
alert(i18n("error_wrong_file_type"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = async (e) => {
|
||||||
|
handleImport(e.target.result);
|
||||||
|
};
|
||||||
|
reader.readAsText(file);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
onClick={handleClick}
|
||||||
|
startIcon={<FileUploadIcon />}
|
||||||
|
>
|
||||||
|
{text}
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept={fileExts.join(", ")}
|
||||||
|
ref={inputRef}
|
||||||
|
onChange={onChange}
|
||||||
|
hidden
|
||||||
|
/>
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ import Apis from "./Apis";
|
|||||||
import Webfix from "./Webfix";
|
import Webfix from "./Webfix";
|
||||||
import InputSetting from "./InputSetting";
|
import InputSetting from "./InputSetting";
|
||||||
import Tranbox from "./Tranbox";
|
import Tranbox from "./Tranbox";
|
||||||
|
import FavWords from "./FavWords";
|
||||||
|
|
||||||
export default function Options() {
|
export default function Options() {
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
@@ -125,6 +126,7 @@ export default function Options() {
|
|||||||
<Route path="apis" element={<Apis />} />
|
<Route path="apis" element={<Apis />} />
|
||||||
<Route path="sync" element={<SyncSetting />} />
|
<Route path="sync" element={<SyncSetting />} />
|
||||||
<Route path="webfix" element={<Webfix />} />
|
<Route path="webfix" element={<Webfix />} />
|
||||||
|
<Route path="words" element={<FavWords />} />
|
||||||
<Route path="about" element={<About />} />
|
<Route path="about" element={<About />} />
|
||||||
</Route>
|
</Route>
|
||||||
</Routes>
|
</Routes>
|
||||||
|
|||||||
35
src/views/Selection/CopyBtn.js
Normal file
35
src/views/Selection/CopyBtn.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
|
||||||
|
import LibraryAddCheckIcon from "@mui/icons-material/LibraryAddCheck";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
|
export default function CopyBtn({ text }) {
|
||||||
|
const [copied, setCopied] = useState(false);
|
||||||
|
const handleClick = (e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
navigator.clipboard.writeText(text);
|
||||||
|
setCopied(true);
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
setCopied(false);
|
||||||
|
}, 500);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
sx={{
|
||||||
|
opacity: 0.5,
|
||||||
|
"&:hover": {
|
||||||
|
opacity: 1,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
onClick={handleClick}
|
||||||
|
>
|
||||||
|
{copied ? (
|
||||||
|
<LibraryAddCheckIcon fontSize="inherit" />
|
||||||
|
) : (
|
||||||
|
<ContentCopyIcon fontSize="inherit" />
|
||||||
|
)}
|
||||||
|
</IconButton>
|
||||||
|
);
|
||||||
|
}
|
||||||
64
src/views/Selection/DictCont.js
Normal file
64
src/views/Selection/DictCont.js
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import Box from "@mui/material/Box";
|
||||||
|
import Chip from "@mui/material/Chip";
|
||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import FavBtn from "./FavBtn";
|
||||||
|
|
||||||
|
const exchangeMap = {
|
||||||
|
word_third: "第三人称单数",
|
||||||
|
word_ing: "现在分词",
|
||||||
|
word_done: "过去式",
|
||||||
|
word_past: "过去分词",
|
||||||
|
word_pl: "复数",
|
||||||
|
word_proto: "原词",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function DictCont({ dictResult }) {
|
||||||
|
if (!dictResult) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
justifyContent="space-between"
|
||||||
|
alignItems="flex-start"
|
||||||
|
>
|
||||||
|
<div style={{ fontWeight: "bold" }}>
|
||||||
|
{dictResult.simple_means?.word_name}
|
||||||
|
</div>
|
||||||
|
<FavBtn word={dictResult.simple_means?.word_name} />
|
||||||
|
</Stack>
|
||||||
|
|
||||||
|
{dictResult.simple_means?.symbols?.map(({ ph_en, ph_am, parts }, idx) => (
|
||||||
|
<div key={idx}>
|
||||||
|
{(ph_en || ph_am) && (
|
||||||
|
<div>{`英/${ph_en || ""}/ 美/${ph_am || ""}/`}</div>
|
||||||
|
)}
|
||||||
|
<ul style={{ margin: "0.5em 0" }}>
|
||||||
|
{parts.map(({ part, means }, idx) => (
|
||||||
|
<li key={idx}>
|
||||||
|
{part ? `[${part}] ${means.join("; ")}` : means.join("; ")}
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{Object.entries(dictResult.simple_means?.exchange || {})
|
||||||
|
.map(([key, val]) => `${exchangeMap[key] || key}: ${val.join(", ")}`)
|
||||||
|
.join("; ")}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Stack direction="row" spacing={1} flexWrap="wrap" useFlexGap>
|
||||||
|
{Object.values(dictResult.simple_means?.tags || {})
|
||||||
|
.flat()
|
||||||
|
.filter((item) => item)
|
||||||
|
.map((item) => (
|
||||||
|
<Chip label={item} size="small" />
|
||||||
|
))}
|
||||||
|
</Stack>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
31
src/views/Selection/FavBtn.js
Normal file
31
src/views/Selection/FavBtn.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import FavoriteIcon from "@mui/icons-material/Favorite";
|
||||||
|
import FavoriteBorderIcon from "@mui/icons-material/FavoriteBorder";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { useFavWords } from "../../hooks/FavWords";
|
||||||
|
|
||||||
|
export default function FavBtn({ word }) {
|
||||||
|
const { favWords, toggleFav } = useFavWords();
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const handleClick = async () => {
|
||||||
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
await toggleFav(word);
|
||||||
|
} catch (err) {
|
||||||
|
console.log("[set fav]", err);
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<IconButton disabled={loading} size="small" onClick={handleClick}>
|
||||||
|
{favWords[word] ? (
|
||||||
|
<FavoriteIcon fontSize="inherit" />
|
||||||
|
) : (
|
||||||
|
<FavoriteBorderIcon fontSize="inherit" />
|
||||||
|
)}
|
||||||
|
</IconButton>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -8,10 +8,13 @@ import MenuItem from "@mui/material/MenuItem";
|
|||||||
import Grid from "@mui/material/Grid";
|
import Grid from "@mui/material/Grid";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import Divider from "@mui/material/Divider";
|
import Divider from "@mui/material/Divider";
|
||||||
|
import IconButton from "@mui/material/IconButton";
|
||||||
|
import DoneIcon from "@mui/icons-material/Done";
|
||||||
import { useI18n } from "../../hooks/I18n";
|
import { useI18n } from "../../hooks/I18n";
|
||||||
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config";
|
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config";
|
||||||
import { useState, useRef } from "react";
|
import { useState, useRef } from "react";
|
||||||
import TranCont from "./TranCont";
|
import TranCont from "./TranCont";
|
||||||
|
import CopyBtn from "./CopyBtn";
|
||||||
|
|
||||||
function TranForm({ text, setText, tranboxSetting, transApis }) {
|
function TranForm({ text, setText, tranboxSetting, transApis }) {
|
||||||
const i18n = useI18n();
|
const i18n = useI18n();
|
||||||
@@ -113,6 +116,31 @@ function TranForm({ text, setText, tranboxSetting, transApis }) {
|
|||||||
setEditMode(false);
|
setEditMode(false);
|
||||||
setText(editText.trim());
|
setText(editText.trim());
|
||||||
}}
|
}}
|
||||||
|
InputProps={{
|
||||||
|
endAdornment: (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
sx={{
|
||||||
|
position: "absolute",
|
||||||
|
right: 0,
|
||||||
|
top: 0,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{editMode ? (
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DoneIcon fontSize="inherit" />
|
||||||
|
</IconButton>
|
||||||
|
) : (
|
||||||
|
<CopyBtn text={text} />
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
),
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
@@ -2,61 +2,14 @@ import TextField from "@mui/material/TextField";
|
|||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import Alert from "@mui/material/Alert";
|
import Alert from "@mui/material/Alert";
|
||||||
import CircularProgress from "@mui/material/CircularProgress";
|
import CircularProgress from "@mui/material/CircularProgress";
|
||||||
import Chip from "@mui/material/Chip";
|
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import { useI18n } from "../../hooks/I18n";
|
import { useI18n } from "../../hooks/I18n";
|
||||||
import { DEFAULT_TRANS_APIS, OPT_TRANS_BAIDU } from "../../config";
|
import { DEFAULT_TRANS_APIS, OPT_TRANS_BAIDU } from "../../config";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { apiTranslate } from "../../apis";
|
import { apiTranslate } from "../../apis";
|
||||||
import { isValidWord } from "../../libs/utils";
|
import { isValidWord } from "../../libs/utils";
|
||||||
|
import CopyBtn from "./CopyBtn";
|
||||||
const exchangeMap = {
|
import DictCont from "./DictCont";
|
||||||
word_third: "第三人称单数",
|
|
||||||
word_ing: "现在分词",
|
|
||||||
word_done: "过去式",
|
|
||||||
word_past: "过去分词",
|
|
||||||
word_pl: "复数",
|
|
||||||
word_proto: "原词",
|
|
||||||
};
|
|
||||||
|
|
||||||
function DictCont({ dictResult }) {
|
|
||||||
if (!dictResult) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Box>
|
|
||||||
<div style={{ fontWeight: "bold" }}>
|
|
||||||
{dictResult.simple_means?.word_name}
|
|
||||||
</div>
|
|
||||||
{dictResult.simple_means?.symbols?.map(({ ph_en, ph_am, parts }, idx) => (
|
|
||||||
<div key={idx}>
|
|
||||||
<div>{`英[${ph_en}] 美[${ph_am}]`}</div>
|
|
||||||
<ul style={{ margin: "0.5em 0" }}>
|
|
||||||
{parts.map(({ part, means }, idx) => (
|
|
||||||
<li key={idx}>
|
|
||||||
{part ? `[${part}] ${means.join("; ")}` : means.join("; ")}
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
<div>
|
|
||||||
{Object.entries(dictResult.simple_means?.exchange || {})
|
|
||||||
.map(([key, val]) => `${exchangeMap[key] || key}: ${val.join(", ")}`)
|
|
||||||
.join("; ")}
|
|
||||||
</div>
|
|
||||||
<Stack direction="row" spacing={1}>
|
|
||||||
{Object.values(dictResult.simple_means?.tags || {})
|
|
||||||
.flat()
|
|
||||||
.filter((item) => item)
|
|
||||||
.map((item) => (
|
|
||||||
<Chip label={item} size="small" />
|
|
||||||
))}
|
|
||||||
</Stack>
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function TranCont({
|
export default function TranCont({
|
||||||
text,
|
text,
|
||||||
@@ -79,8 +32,8 @@ export default function TranCont({
|
|||||||
setError("");
|
setError("");
|
||||||
setDictResult(null);
|
setDictResult(null);
|
||||||
|
|
||||||
const apis = { ...transApis, ...DEFAULT_TRANS_APIS };
|
const apiSetting =
|
||||||
const apiSetting = apis[translator];
|
transApis[translator] || DEFAULT_TRANS_APIS[translator];
|
||||||
const tranRes = await apiTranslate({
|
const tranRes = await apiTranslate({
|
||||||
text,
|
text,
|
||||||
translator,
|
translator,
|
||||||
@@ -100,7 +53,6 @@ export default function TranCont({
|
|||||||
translator: OPT_TRANS_BAIDU,
|
translator: OPT_TRANS_BAIDU,
|
||||||
fromLang: "en",
|
fromLang: "en",
|
||||||
toLang: "zh-CN",
|
toLang: "zh-CN",
|
||||||
apiSetting: apis[OPT_TRANS_BAIDU],
|
|
||||||
});
|
});
|
||||||
setDictResult(dictRes[2].dict_result);
|
setDictResult(dictRes[2].dict_result);
|
||||||
}
|
}
|
||||||
@@ -118,9 +70,24 @@ export default function TranCont({
|
|||||||
<Box>
|
<Box>
|
||||||
<TextField
|
<TextField
|
||||||
label={i18n("translated_text")}
|
label={i18n("translated_text")}
|
||||||
|
// disabled
|
||||||
fullWidth
|
fullWidth
|
||||||
multiline
|
multiline
|
||||||
value={trText}
|
value={trText}
|
||||||
|
InputProps={{
|
||||||
|
endAdornment: (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
sx={{
|
||||||
|
position: "absolute",
|
||||||
|
right: 0,
|
||||||
|
top: 0,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<CopyBtn text={trText} />
|
||||||
|
</Stack>
|
||||||
|
),
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import TranBtn from "./Tranbtn";
|
import TranBtn from "./TranBtn";
|
||||||
import TranBox from "./Tranbox";
|
import TranBox from "./TranBox";
|
||||||
import { shortcutRegister } from "../../libs/shortcut";
|
import { shortcutRegister } from "../../libs/shortcut";
|
||||||
|
|
||||||
export default function Slection({ tranboxSetting, transApis }) {
|
export default function Slection({ tranboxSetting, transApis }) {
|
||||||
|
|||||||
Reference in New Issue
Block a user