Skip to content

Commit

Permalink
feat(Content,SidePabel): add sidepanel for word history
Browse files Browse the repository at this point in the history
  • Loading branch information
couriourc committed Apr 24, 2024
1 parent fc95efa commit 7efe00a
Show file tree
Hide file tree
Showing 8 changed files with 8,157 additions and 6,429 deletions.
27 changes: 13 additions & 14 deletions entrypoints/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,26 @@ interface IWordInfo {

function Origin({className, ...props}: IOriginProps) {
const {data} = useSWR(() => '/segment?selection=' + selection, async () => {
// console.log(await db.system.get("version"));
// await db.system.add({
// "version": 1,
// });
// console.log(await db.system.count());

const data = await segment(selection)!;
return [...data]?.filter((seg) => !/\s/.test(seg.segment)).filter((word) => word.isWordLike);
});

const searchWords = useMemo(() => {
return data?.map(word => word.segment) ?? [];
}, [data]);
const textToHighlight = useMemo(() => selection, [selection]);
return <div
{...props}
className={cx(className, "border-box ")}
className={cx(className, "border-box p-4px border-solid border-1px border-gray max-w-50vw rounded-6px")}
>
<Highlighter
highlightClassName={cx("cursor-pointer hover:bg-yellow rounded-lg bg-transparent px-2px")}
searchWords={data?.map(word => word.segment) ?? []}
autoEscape={true}
textToHighlight={selection}
highlightTag={({children, highlightIndex, className, ...props}) =>
<HighlighterMarker {...{highlightIndex, className, ...props}}>{children}</HighlighterMarker>
highlightClassName={cx("cursor-pointer hover:bg-yellow rounded-lg bg-transparent px-2px text-sm")}
searchWords={searchWords}
textToHighlight={textToHighlight}
highlightTag={({children, highlightIndex, className, ...props}) => {
console.log(children, searchWords);
return <HighlighterMarker {...{highlightIndex, className, ...props}}>{children}</HighlighterMarker>;
}
}
/>
</div>;
Expand Down Expand Up @@ -383,7 +382,7 @@ function ContentApp() {

minHeight: (panel.isOpen ? popupCardMinWidth : 0) + "px",
minWidth: popupCardMinWidth + "px",
maxWidth: popupCardMaxWidth + "px",
maxWidth: '50vw',
zIndex: 1,
});
};
Expand Down
125 changes: 122 additions & 3 deletions entrypoints/sidepanel/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,125 @@
export default function App() {
import {Accordion, Box, ActionIcon, AccordionControlProps, Center, ThemeIcon, Text, Flex, Title} from '@mantine/core';
import {HiDotsHorizontal} from "react-icons/hi";
import useSWR from "swr";
import React, {forwardRef, PropsWithChildren, ReactElement, Ref, useEffect, useState} from "react";
import {useDrag} from "react-dnd";
import {IconDots} from "@tabler/icons-react";
import {Button} from "@nextui-org/react";
import {CopyIcon} from "@nextui-org/shared-icons";
import {useCopyToClipboard, useWindowSize} from "react-use";
import '@mantine/notifications/styles.css';
import {notifications} from "@mantine/notifications";
import {CopyToClipboardState} from "react-use/lib/useCopyToClipboard";

interface ExtendedAccordionControlProps extends AccordionControlProps {
Actions?: ReactElement | ReactElement[];
}

function AccordionControl({Actions, ...props}: ExtendedAccordionControlProps) {
return (
<Center>
<Accordion.Control {...props} />
<Flex align={"center"}>
{Actions}
</Flex>
</Center>
);
}

function useCopy(): ReturnType<typeof useCopyToClipboard> {
const [binding, copy] = useCopyToClipboard();
useEffect(() => {
if (!binding.value) return;
if (binding.error) {
notifications.show({
title: '复制失败🤥',
message: <>{binding.error}</>,
});
} else {
notifications.show({
title: '复制成功💕',
message: "Cool😘",
});
}
}, [binding]);

return [binding, copy];
}

function TranslateResult({children}: PropsWithChildren) {
const [binding, copy] = useCopy();

return <Box className={" px-12px text-justify text-sm text-#333 dark:text-#FFF cursor-pointer break-words max-w-97vw"}
onClick={() => {
copy(children as string);
}}
>{children}</Box>;
}

function HistoryItem({children, ref, width, value}: PropsWithChildren<{
value: string;
width: number;
height?: number;
ref?: Ref<HTMLDivElement>
}>) {
const [binding, copy] = useCopy();
return <Accordion.Item ref={ref} value={value}>
<AccordionControl
Actions={<>
<Button onClick={() => {
copy(value);
}} isIconOnly color="default" variant={"light"} aria-label="More">
<CopyIcon></CopyIcon>
</Button>
<Button isIconOnly color="default" variant={"light"} aria-label="More">
<HiDotsHorizontal/>
</Button>
</>}
>
<Box w={width * 0.8}>
<Text truncate="end" size={"md"}>
{value}
</Text>
</Box>
</AccordionControl>
<Accordion.Panel>
<TranslateResult>{children}</TranslateResult>
</Accordion.Panel>
</Accordion.Item>;

}


function HistoryList() {
const {data} = useSWR(() => "a", () => {
return Promise.resolve(["asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时",
"asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时asdjasdjaskldnaskldajskdljasd按时间第哦啊是对焦速度加啊四哦大家按时",
]);
});
const [drag, source] = useDrag({
type: "asdas",
},);
const {width, height} = useWindowSize();
console.log(source);
return <>
Sidebar
词汇表收集界面
<Accordion chevronPosition="left" w={"100vw"}
className={'box-border'}
mx="auto"
>
{data?.map((item) => {
return <HistoryItem
ref={source}
{...{width, height}}
value={item + ""}
>{item}</HistoryItem>;
})}
</Accordion>
</>;
}

export default function App() {
return <Box className={"p-8px box-border"}>
<Title className={"m-6px!"} order={4}><Text c="blue.4">翻译历史</Text></Title>
<HistoryList></HistoryList>
</Box>;
}
7 changes: 6 additions & 1 deletion entrypoints/sidepanel/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.tsx';
import {TranslatorAppWrapper} from "@/shared/components/App.tsx";
import "uno.css";
import {DndProvider} from "react-dnd";
import {HTML5Backend} from "react-dnd-html5-backend";

ReactDOM.createRoot(document.getElementById('root')!).render(
<TranslatorAppWrapper>
<App></App>
<DndProvider backend={HTML5Backend}>
<App></App>
</DndProvider>
</TranslatorAppWrapper>
);
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
"dependencies": {
"@emotion/css": "^11.11.2",
"@lottiefiles/react-lottie-player": "^3.5.3",
"@mantine/core": "^7.8.0",
"@mantine/hooks": "^7.8.0",
"@mantine/core": "^7.8.1",
"@mantine/hooks": "^7.8.1",
"@mantine/notifications": "^7.8.1",
"@nextui-org/avatar": "^2.0.25",
"@nextui-org/badge": "^2.0.25",
"@nextui-org/button": "^2.0.28",
Expand All @@ -50,6 +51,7 @@
"@nextui-org/spinner": "^2.0.28",
"@nextui-org/system": "^2.1.0",
"@nextui-org/theme": "^2.2.0",
"@tabler/icons-react": "^3.2.0",
"@types/react-highlight-words": "^0.16.7",
"@uidotdev/usehooks": "^2.4.1",
"autoprefixer": "^10.4.19",
Expand Down Expand Up @@ -82,7 +84,6 @@
"react-transition-group": "^4.4.5",
"react-use": "^17.5.0",
"remark-gfm": "^4.0.0",
"segmentit": "^2.0.3",
"swr": "^2.2.5",
"typewriter-effect": "^2.21.0",
"underscore": "^1.13.6",
Expand Down Expand Up @@ -112,6 +113,7 @@
"postcss-simple-vars": "^7.0.1",
"puppeteer": "^21.6.1",
"react-router": "^6.22.3",
"segmentit": "^2.0.3",
"standard-version": "^9.5.0",
"tailwindcss": "^3.4.3",
"typescript": "^5.3.3",
Expand Down
Loading

0 comments on commit 7efe00a

Please sign in to comment.