Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
feat: update time when visible & support img preview
Browse files Browse the repository at this point in the history
  • Loading branch information
flytam committed Mar 28, 2023
1 parent 7c5f650 commit 3745cfb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"observe-element-in-viewport": "^0.0.15",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-photo-view": "^1.2.3",
"vconsole": "^3.15.0"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/components/ClipBoardItemContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { FC } from "react";
import { ClipBoardData } from "../hooks/useClipboardData";
import { ClipBoardDataType } from "../utils/clipboard";

import { PhotoProvider, PhotoView } from "react-photo-view";
import "react-photo-view/dist/react-photo-view.css";

export const ClipBoardItemContent: FC<{
item: ClipBoardData;
}> = ({ item }) => {
Expand All @@ -13,7 +16,11 @@ export const ClipBoardItemContent: FC<{
width: "100%",
}}
>
<img src={item.base64} style={{ height: "300px" }} />
<PhotoProvider>
<PhotoView src={item.base64}>
<img src={item.base64} style={{ height: "200px" }} />
</PhotoView>
</PhotoProvider>
<Typography
color={colors.grey[500]}
>{`${item.width} x ${item.height}`}</Typography>
Expand Down Expand Up @@ -42,8 +49,16 @@ export const ClipBoardItemContent: FC<{
<ListItemText
sx={{
padding: 0,
maxHeight: "200px",
overflow: "hidden",
textOverflow: "ellipsis",
display: "-webkit-box",
WebkitLineClamp: 3,
WebkitBoxOrient: "vertical",
width: "200px",
}}
inset
disableTypography
primary={item.data}
/>
);
Expand Down
27 changes: 24 additions & 3 deletions src/components/ClipBoardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import {
ListItemText,
colors,
} from "@mui/material";
import { FC, forwardRef, useEffect, useImperativeHandle, useRef } from "react";
import { useDocumentVisibility } from "ahooks";
import {
FC,
forwardRef,
useEffect,
useImperativeHandle,
useMemo,
useRef,
} from "react";
import { ClipBoardData } from "../hooks/useClipboardData";
import { ClipBoardRawData } from "../utils/clipboard";
import { timeAgo } from "../utils/format";
import { ClipBoardItemContent } from "./ClipBoardItemContent";

Expand All @@ -25,21 +34,33 @@ export const ClipBoardList = forwardRef<IClipboardRef, IProps>(
({ clipBoardList, activeIndexList, onActiveChange }, ref) => {
const containerRef = useRef<HTMLUListElement | null>(null);

const documentVisibility = useDocumentVisibility();

useImperativeHandle(ref, () => ({
container: containerRef.current,
getActiveItem() {
return document.getElementById("clip-board-active");
},
}));

const formatClipBoardList = useMemo(() => {
if (documentVisibility !== "visible") {
return clipBoardList as (ClipBoardRawData & { ago?: number })[];
}
return clipBoardList.map((x) => ({
...x,
ago: timeAgo(x.timestamp!),
}));
}, [clipBoardList, documentVisibility]);

return (
<List
sx={{
overflowX: "auto",
}}
ref={(node) => (containerRef.current = node)}
>
{clipBoardList.map((x, index) => (
{formatClipBoardList.map((x, index) => (
<ListItem
key={index}
disablePadding
Expand All @@ -61,7 +82,7 @@ export const ClipBoardList = forwardRef<IClipboardRef, IProps>(
>
<ListItemText
inset
secondary={timeAgo(x.timestamp!)}
secondary={x.ago}
sx={{
flexGrow: 0,
paddingLeft: "10px",
Expand Down

0 comments on commit 3745cfb

Please sign in to comment.