-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 在 Windows 和 MacOS 上支持显示剪贴板文件内容对应的系统图标 (#899)
- Loading branch information
Showing
11 changed files
with
1,228 additions
and
470 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...lipboard/Panel/components/List/components/Item/components/Files/components/File/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { exists, mkdir, writeFile } from "@tauri-apps/plugin-fs"; | ||
import { Flex } from "antd"; | ||
import clsx from "clsx"; | ||
import type { FC } from "react"; | ||
import { type Metadata, icon, metadata } from "tauri-plugin-fs-pro-api"; | ||
import Image from "../../../Image"; | ||
|
||
interface FileProps { | ||
path: string; | ||
count: number; | ||
} | ||
|
||
interface State extends Partial<Metadata> { | ||
iconPath?: string; | ||
} | ||
|
||
const File: FC<FileProps> = (props) => { | ||
const { path, count } = props; | ||
|
||
const state = useReactive<State>({}); | ||
|
||
useAsyncEffect(async () => { | ||
try { | ||
const data = await metadata(path, { omitSize: true }); | ||
|
||
Object.assign(state, data); | ||
|
||
if (isLinux()) return; | ||
|
||
state.iconPath = await getIconPath(); | ||
} catch { | ||
Object.assign(state, { | ||
fullName: path.split("/").pop(), | ||
}); | ||
} | ||
}, [path]); | ||
|
||
const getIconPath = async () => { | ||
const iconPath = joinPath(getSaveIconPath(), `${getIconName()}.png`); | ||
|
||
const existed = await exists(iconPath); | ||
|
||
if (existed) return iconPath; | ||
|
||
await mkdir(getSaveIconPath(), { recursive: true }); | ||
|
||
const bytes = await icon(path, 256); | ||
|
||
await writeFile(iconPath, bytes); | ||
|
||
return iconPath; | ||
}; | ||
|
||
const getIconName = () => { | ||
const { isDir, extname, name, fullName } = state; | ||
|
||
const isMacApp = isMac() && extname === "app"; | ||
const isWinApp = isWin() && extname === "exe"; | ||
|
||
if (isMacApp || isWinApp) { | ||
return fullName; | ||
} | ||
|
||
if (isDir) { | ||
return "__ECOPASTE_DIRECTORY__"; | ||
} | ||
|
||
if (!extname) { | ||
return name; | ||
} | ||
|
||
return extname; | ||
}; | ||
|
||
const renderContent = () => { | ||
const height = 100 / Math.min(count, 3); | ||
|
||
if (state.isExist && count === 1) { | ||
if (isImage(path)) { | ||
return <Image value={path} />; | ||
} | ||
} | ||
|
||
return ( | ||
<Flex align="center" gap={4} style={{ height: `${height}%` }}> | ||
{state.isExist && <Image value={state.iconPath} className="h-full" />} | ||
|
||
<span | ||
className={clsx("truncate", { | ||
"text-danger line-through": !state.isExist, | ||
})} | ||
> | ||
{state.fullName} | ||
</span> | ||
</Flex> | ||
); | ||
}; | ||
|
||
return renderContent(); | ||
}; | ||
|
||
export default File; |
20 changes: 4 additions & 16 deletions
20
src/pages/Clipboard/Panel/components/List/components/Item/components/Files/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,15 @@ | ||
import type { HistoryTablePayload } from "@/types/database"; | ||
import type { FC } from "react"; | ||
import Image from "../Image"; | ||
import File from "./components/File"; | ||
|
||
const Files: FC<HistoryTablePayload> = (props) => { | ||
const { value } = props; | ||
|
||
const paths: string[] = JSON.parse(value); | ||
|
||
const renderContent = () => { | ||
if (paths.length === 1) { | ||
const [path] = paths; | ||
|
||
if (isImage(path)) { | ||
return <Image value={path} />; | ||
} | ||
|
||
return path; | ||
} | ||
|
||
return paths.join("\n"); | ||
}; | ||
|
||
return renderContent(); | ||
return paths.map((path) => { | ||
return <File key={path} path={path} count={paths.length} />; | ||
}); | ||
}; | ||
|
||
export default memo(Files); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters