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: 新增快捷键粘贴为纯文本功能 #530

Merged
merged 1 commit into from
Sep 17, 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
8 changes: 4 additions & 4 deletions src/components/ProShortcut/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { type Key, keys, modifierKeys, normalKeys } from "./keys";
interface ProShortcutProps {
title: string;
description?: string;
defaultValue?: string;
value?: string;
onChange?: (value: string) => void;
}

Expand All @@ -26,14 +26,14 @@ interface State {
}

const ProShortcut: FC<ProShortcutProps> = (props) => {
const { title, description, defaultValue = "", onChange } = props;
const { title, description, value = "", onChange } = props;

const { t } = useTranslation();

const handleDefaultValue = () => {
if (!defaultValue) return [];
if (!value) return [];

return defaultValue.split("+").map((shortcut) => find(keys, { shortcut })!);
return value.split("+").map((shortcut) => find(keys, { shortcut })!);
};

const containerRef = useRef<HTMLElement>(null);
Expand Down
2 changes: 1 addition & 1 deletion src/database/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const initDatabase = async () => {
type TEXT,
[group] TEXT,
value TEXT,
search TEXT,
search TEXT,
count INTEGER,
width INTEGER,
height INTEGER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const Item: FC<ItemProps> = (props) => {
};

// 粘贴纯文本
const pastePlainText = () => {
const pastePlain = () => {
pasteClipboard(data, true);
};

Expand Down Expand Up @@ -129,12 +129,12 @@ const Item: FC<ItemProps> = (props) => {
{
label: t("clipboard.button.context_menu.paste_ocr_text"),
hide: type !== "image" || /^[\s]*$/.test(search),
event: pastePlainText,
event: pastePlain,
},
{
label: t("clipboard.button.context_menu.paste_as_plain_text"),
hide: type !== "html" && type !== "rtf",
event: pastePlainText,
event: pastePlain,
},
{
label: favorite
Expand Down
14 changes: 13 additions & 1 deletion src/pages/Clipboard/Panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Audio from "@/components/Audio";
import type { ClipboardItem, TablePayload } from "@/types/database";
import { listen } from "@tauri-apps/api/event";
import { registerAll, unregister } from "@tauri-apps/api/globalShortcut";
import { appWindow } from "@tauri-apps/api/window";
import type { EventEmitter } from "ahooks/lib/useEventEmitter";
import { find, findIndex, isEqual, last, merge, range } from "lodash-es";
import { nanoid } from "nanoid";
Expand Down Expand Up @@ -128,9 +129,20 @@ const ClipboardPanel = () => {
},
});

// 监听快捷键
// 监听窗口显隐的快捷键
useRegister(toggleWindowVisible, [shortcut.clipboard]);

// 监听粘贴为纯文本的快捷键
useRegister(async () => {
const focused = await appWindow.isFocused();

if (!focused) return;

const data = find(state.list, { id: state.activeId });

pasteClipboard(data, true);
}, [shortcut.pastePlain]);

// 获取剪切板内容
const getList = async () => {
const { search, group, favorite } = state;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Clipboard/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ const ClipboardSettings = () => {
description={t(
"preference.clipboard.content_settings.hints.paste_as_plain",
)}
value={content.pastePlainText}
value={content.pastePlain}
onChange={(value) => {
clipboardStore.content.pastePlainText = value;
clipboardStore.content.pastePlain = value;
}}
/>
</ProList>
Expand Down
13 changes: 11 additions & 2 deletions src/pages/Shortcut/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ const Shortcut = () => {
<ProList header={t("preference.shortcut.shortcut.title")}>
<ProShortcut
title={t("preference.shortcut.shortcut.label.open_clipboard")}
defaultValue={shortcut.clipboard}
value={shortcut.clipboard}
onChange={(value) => {
globalStore.shortcut.clipboard = value;
}}
/>

<ProShortcut
title={t("preference.shortcut.shortcut.label.open_settings")}
defaultValue={shortcut.preference}
value={shortcut.preference}
onChange={(value) => {
globalStore.shortcut.preference = value;
}}
/>

<QuickPaste />

<ProShortcut
title="粘贴为纯文本"
description="将内容粘贴为纯文本或 OCR 文本"
value={shortcut.pastePlain}
onChange={(value) => {
globalStore.shortcut.pastePlain = value;
}}
/>
</ProList>
);
};
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ export const writeImage = (value: string) => {
* HTML 内容写入剪贴板
*/
export const writeHTML = (text: string, html: string) => {
const { pastePlainText } = clipboardStore.content;
const { pastePlain } = clipboardStore.content;

if (pastePlainText) {
if (pastePlain) {
return writeText(text);
}

Expand All @@ -244,9 +244,9 @@ export const writeHTML = (text: string, html: string) => {
* 富文写入剪贴板
*/
export const writeRTF = (text: string, rtf: string) => {
const { pastePlainText } = clipboardStore.content;
const { pastePlain } = clipboardStore.content;

if (pastePlainText) {
if (pastePlain) {
return writeText(text);
}

Expand Down Expand Up @@ -295,7 +295,7 @@ export const onClipboardUpdate = (
* 将数据写入剪切板
* @param data 数据
*/
export const writeClipboard = async (data: ClipboardItem) => {
export const writeClipboard = async (data?: ClipboardItem) => {
if (!data) return;

const { type, value, search } = data;
Expand All @@ -319,7 +319,7 @@ export const writeClipboard = async (data: ClipboardItem) => {
* @param data 数据
* @param plain 是否纯文本粘贴
*/
export const pasteClipboard = async (data: ClipboardItem, plain = false) => {
export const pasteClipboard = async (data?: ClipboardItem, plain = false) => {
if (!data) return;

const { type, value } = data;
Expand Down
2 changes: 1 addition & 1 deletion src/stores/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const CLIPBOARD_STORE_INITIAL_STATE: ClipboardStore = {
content: {
autoPaste: "double",
ocr: true,
pastePlainText: false,
pastePlain: false,
},

history: {
Expand Down
3 changes: 2 additions & 1 deletion src/types/store.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface GlobalStore {
enable: boolean;
value: ModifierKey;
};
pastePlain?: string;
};

// 只在当前系统环境使用
Expand Down Expand Up @@ -72,7 +73,7 @@ export interface ClipboardStore {
content: {
autoPaste: "single" | "double";
ocr: boolean;
pastePlainText: boolean;
pastePlain: boolean;
};

// 历史记录
Expand Down