Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 update dicts #1026

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions enjoy/src/constants/dicts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
export const DICTS = [
{
name: "ccalecd",
fileName: "ccalecd.zip",
title: "Collins COBUILD Advanced British EN-CN Dictionary",
pronunciation: true,
lang: "En-CN",
downloadUrl: "https://dl.enjoy.bot/dicts/ccalecd.zip",
size: "13.879MB",
hash: "96940f85e52df4586b287e1859723a39",
addition: '<link href="theme.css" rel="stylesheet" type="text/css" />',
},

{
name: "ccabeld",
fileName: "ccabeld.zip",
title: "Collins COBUILD Advanced British English Learners Dictionary",
pronunciation: true,
lang: "En-En",
downloadUrl: "https://dl.enjoy.bot/dicts/ccabeld.zip",
size: "485.6MB",
hash: "5b53498536f3ce3ed173752b7888ca51",
addition: '<link href="theme.css" rel="stylesheet" type="text/css" />',
},
{
name: "ldoce5",
fileName: "ldoce5.zip",
Expand All @@ -13,7 +36,7 @@ export const DICTS = [
{
name: "oxford_en_mac",
fileName: "oxford_en_mac.zip",
title: "Oxford Dictionary of English (Mac)",
title: "Oxford Dictionary of English",
pronunciation: false,
lang: "En-En",
downloadUrl: "https://dl.enjoy.bot/dicts/oxford_en_mac.zip",
Expand All @@ -24,7 +47,7 @@ export const DICTS = [
{
name: "koen_mac",
fileName: "koen_mac.zip",
title: "Korean English Dictionary (Mac)",
title: "Korean English Dictionary",
pronunciation: false,
lang: "Ko-En",
downloadUrl: "https://dl.enjoy.bot/dicts/koen_mac.zip",
Expand All @@ -46,7 +69,7 @@ export const DICTS = [
{
name: "deen_mac",
fileName: "deen_mac.zip",
title: "German English Dictionary (Mac)",
title: "German English Dictionary",
pronunciation: false,
lang: "Ge-En",
downloadUrl: "https://dl.enjoy.bot/dicts/deen_mac.zip",
Expand All @@ -57,7 +80,7 @@ export const DICTS = [
{
name: "ruen_mac",
fileName: "ruen_mac.zip",
title: "Russian English Dictionary (Mac)",
title: "Russian English Dictionary",
pronunciation: false,
lang: "Ru-En",
downloadUrl: "https://dl.enjoy.bot/dicts/ruen_mac.zip",
Expand Down
6 changes: 4 additions & 2 deletions enjoy/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@
"dictFileExist": "{{name}} has already been imported",
"dictFileAddSuccess": "Add {{name}} successfully",
"dictFileRemoveSuccess": "Remove {{name}} successfully",
"dictFileSetDefaultSuccess": "Set default successfully",
"dictFileSetDefaultSuccess": "Successfully set as default dictionary",
"dictFileRemoveDefaultSuccess": "Operation successful",
"dictionaries": "Dictionaries",
"import": "Import",
"default": "Default",
Expand All @@ -769,5 +770,6 @@
"removing": "Removing",
"removeDictTitle": "Are you sure you want to delete this dictionary? ",
"removeDictDescription": "It will delete the dictionary file from your local computer and you will have to download it again next time.",
"downloadingDict": "Downloading"
"downloadingDict": "Downloading",
"removeDefault": "No longer as Default"
}
6 changes: 4 additions & 2 deletions enjoy/src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@
"dictFileExist": "{{name}} 已经存在了",
"dictFileAddSuccess": "已添加 {{name}}",
"dictFileRemoveSuccess": "已删除 {{name}}",
"dictFileSetDefaultSuccess": "设置成功",
"dictFileSetDefaultSuccess": "成功设置为默认词典",
"dictFileRemoveDefaultSuccess": "操作成功",
"dictionaries": "词典",
"import": "导入",
"default": "默认",
Expand All @@ -769,5 +770,6 @@
"removing": "正在删除",
"removeDictTitle": "你确定要删除词典吗?",
"removeDictDescription": "此操作将会从本地删除词典文件,下次安装需要重新下载",
"downloadingDict": "正在下载"
"downloadingDict": "正在下载",
"removeDefault": "不再设置为默认"
}
4 changes: 3 additions & 1 deletion enjoy/src/main/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ class Downloader {
resume(filename: string) {
this.tasks
.filter(
(t) => t.getFilename() === filename && t.getState() === "progressing"
(t) =>
t.getFilename() === filename &&
["progressing", "interrupted"].includes(t.getState())
)
.forEach((t) => {
t.resume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { useContext } from "react";
import {
AppSettingsProviderContext,
MediaPlayerProviderContext,
DictProviderContext,
} from "@renderer/context";
import { TabsContent, Separator } from "@renderer/components/ui";
import { t } from "i18next";
import { TimelineEntry } from "echogarden/dist/utilities/Timeline.d.js";
import { convertWordIpaToNormal } from "@/utils";
import {
CamdictLookupResult,
DictLookupResult,
AiLookupResult,
TranslateResult,
} from "@renderer/components";
Expand Down Expand Up @@ -40,6 +42,7 @@ const SelectedWords = (props: {
}) => {
const { selectedIndices, caption } = props;

const { currentDictValue } = useContext(DictProviderContext);
const { transcription } = useContext(MediaPlayerProviderContext);
const { learningLanguage, ipaMappings } = useContext(
AppSettingsProviderContext
Expand Down Expand Up @@ -100,11 +103,16 @@ const SelectedWords = (props: {
})}
</div>

{learningLanguage.startsWith("en") && (
{currentDictValue === "cambridge" ? (
<>
<Separator className="my-2" />
<CamdictLookupResult word={word} />
</>
) : (
<>
<Separator className="my-2" />
<DictLookupResult word={word} autoHeight={true} />
</>
)}

<Separator className="my-2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { useContext, useEffect, useState } from "react";
import { Button, toast } from "@renderer/components/ui";
import { t } from "i18next";
import { LoaderSpin } from "@renderer/components";
import { LoaderIcon } from "lucide-react";

export const DownloadingDictList = function () {
const { EnjoyApp } = useContext(AppSettingsProviderContext);
Expand Down Expand Up @@ -129,7 +129,12 @@ const DownloadingDictItem = function ({ dict }: { dict: Dict }) {
}

function renderActions() {
if (loading) return <LoaderSpin />;
if (loading)
return (
<div>
<LoaderIcon className="text-muted-foreground animate-spin" />
</div>
);

if (
dict.downloadState?.state === "progressing" &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const InstalledDictItem = function ({ dict }: { dict: Dict }) {
}
}

async function handleRemoveDefault() {
try {
await setDefault(null);
toast.success(t("dictFileRemoveDefaultSuccess"));
} catch (err) {
toast.error(err.message);
}
}

async function handleRemove() {
setRemoving(true);

Expand Down Expand Up @@ -120,9 +129,15 @@ const InstalledDictItem = function ({ dict }: { dict: Dict }) {
</AlertDialogContent>
</AlertDialog>

<Button size="sm" variant="secondary" onClick={handleSetDefault}>
{t("setDefault")}
</Button>
{settings.default === dict.name ? (
<Button size="sm" variant="secondary" onClick={handleRemoveDefault}>
{t("removeDefault")}
</Button>
) : (
<Button size="sm" variant="secondary" onClick={handleSetDefault}>
{t("setDefault")}
</Button>
)}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ const MIME: Record<string, string> = {

export function DictLookupResult({
word,
autoHeight,
onJump,
}: {
word: string;
onJump: (v: string) => void;
autoHeight?: boolean;
onJump?: (v: string) => void;
}) {
const { colorScheme } = useContext(ThemeProviderContext);
const initialContent = `<!DOCTYPE html><html class=${colorScheme}><head></head><body></body></html>`;
Expand All @@ -36,6 +38,7 @@ export function DictLookupResult({
const [looking, setLooking] = useState(false);
const [notFound, setNotFound] = useState(false);
const [error, setError] = useState(false);
const [height, setHeight] = useState<number>();

useEffect(() => {
if (currentDict && word) {
Expand Down Expand Up @@ -72,6 +75,10 @@ export function DictLookupResult({
setError(false);
}

function handleResize(h: number) {
setHeight(h);
}

if (looking) {
return (
<div className="text-center">
Expand Down Expand Up @@ -100,27 +107,53 @@ export function DictLookupResult({
<Frame
initialContent={initialContent}
mountTarget="body"
className="h-full"
style={{ height: autoHeight ? `${height}px` : "100%" }}
>
<DictLookupResultInner text={definition} onJump={onJump} />
<DictLookupResultInner
text={definition}
onJump={onJump}
autoHeight={autoHeight}
onResize={handleResize}
/>
</Frame>
);
}

export const DictLookupResultInner = ({
text,
onJump,
onResize,
autoHeight,
}: {
text: string;
onJump: (v: string) => void;
autoHeight: boolean;
onJump?: (v: string) => void;
onResize?: (v: number) => void;
}) => {
const { EnjoyApp } = useContext(AppSettingsProviderContext);
const { currentDict } = useContext(DictProviderContext);
const { document: innerDocument } = useFrame();
const [html, setHtml] = useState("");
const [hash, setHash] = useState("");

useEffect(() => {
if (autoHeight) {
const resizeObserver = new ResizeObserver(() => {
const html = innerDocument.getElementsByTagName("html")[0];
onResize(html.scrollHeight);
});

resizeObserver.observe(innerDocument.getElementById("inner-dict"));
}
}, []);

useEffect(() => {
normalize();
normalize().then(() => {
if (hash) {
handleScroll();
setHash("");
}
});

return () => {
normalizer.revoke();
Expand Down Expand Up @@ -166,7 +199,18 @@ export const DictLookupResultInner = ({

function handleJump(el: Element) {
const word = el.getAttribute("data-word");
onJump(word);
const hash = el.getAttribute("data-hash");
onJump?.(word);
setHash(hash);
}

function handleScroll() {
setTimeout(() => {
const el = innerDocument.querySelector(`a[name='${hash}']`);
if (el) {
el.scrollIntoView();
}
}, 200);
}

const registerAudioHandler = () => {
Expand Down Expand Up @@ -199,5 +243,5 @@ export const DictLookupResultInner = ({
};
};

return <div dangerouslySetInnerHTML={{ __html: html }}></div>;
return <div id="inner-dict" dangerouslySetInnerHTML={{ __html: html }}></div>;
};
Loading
Loading