Skip to content

Commit

Permalink
feat: 配置项 复制为纯文本 更改为 粘贴为纯文本 (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored and kohaiy committed Sep 20, 2024
1 parent 3a885da commit 4d8da60
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"auto_paste_single": "Single Click",
"auto_paste_double": "Double Click",
"image_ocr": "Image OCR",
"copy_as_plain": "Copy as Plain Text"
"paste_as_plain": "Paste as Plain Text"
},
"hints": {
"auto_paste": "Quickly paste content to the input field when using the left mouse button",
"image_ocr": "Note: Please be sure to download the",
"copy_as_plain": "Only retains plain text content when copying rich text or HTML formats"
"paste_as_plain": "Rich text and HTML formatting retains only plain text content when pasting"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"auto_paste_single": "シングルクリック",
"auto_paste_double": "ダブルクリック",
"image_ocr": "画像 OCR",
"copy_as_plain": "テキストでコピーする"
"paste_as_plain": "プレーンテキストとして貼り付ける"
},
"hints": {
"auto_paste": "左クリック時に、内容を素早く入力フィールドに貼り付けます",
"image_ocr": "注意:ご使用前に必ずダウンロードしてください。",
"copy_as_plain": "リッチテキストやHTML形式をコピーする際に、プレーンテキストのみを保持します"
"paste_as_plain": "リッチテキストとHTMLの書式設定は、貼り付け時にプレーンテキストの内容だけを保持する"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"auto_paste_single": "单击",
"auto_paste_double": "双击",
"image_ocr": "图片OCR",
"copy_as_plain": "复制为纯文本"
"paste_as_plain": "粘贴为纯文本"
},
"hints": {
"auto_paste": "鼠标左键操作时,快速粘贴内容至输入位置",
"image_ocr": "注意:使用前请务必先下载",
"copy_as_plain": "富文本和 HTML 格式在复制时仅保留纯文本内容"
"paste_as_plain": "富文本和 HTML 格式在粘贴时仅保留纯文本内容"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
"auto_paste_single": "單擊",
"auto_paste_double": "雙擊",
"image_ocr": "圖片 OCR",
"copy_as_plain": "複製為純文字"
"paste_as_plain": "粘貼為純文字"
},
"hints": {
"auto_paste": "當使用滑鼠左鍵時,快速將內容貼上到輸入位置",
"image_ocr": "注意:使用前請務必先下載",
"copy_as_plain": "富文本和 HTML 格式在複製時僅保留純文字內容"
"paste_as_plain": "富文字和 HTML 格式在粘貼時僅保留純文字內容"
}
}
},
Expand Down
29 changes: 11 additions & 18 deletions src/pages/Clipboard/Panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ const ClipboardPanel = () => {
useMount(async () => {
state.$eventBus = $eventBus;

// 开启监听
startListen();

// 监听剪切板更新
onClipboardUpdate(async (payload) => {
if (clipboardStore.audio.copy) {
audioRef.current?.play();
Expand All @@ -71,29 +73,13 @@ const ClipboardPanel = () => {
createTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
});
} else {
let group: ClipboardItem["group"];

switch (payload.type) {
case "files":
group = "files";
break;
case "image":
group = "image";
break;
default:
group = "text";
break;
}

await insertSQL("history", {
...payload,
group,
});
await insertSQL("history", payload);
}

getClipboardList();
});

// 监听清空历史记录
listen(LISTEN_KEY.CLEAR_HISTORY, async () => {
await deleteSQL("history");

Expand All @@ -102,6 +88,7 @@ const ClipboardPanel = () => {

listen(LISTEN_KEY.CHANGE_DATA_FILE, getClipboardList);

// 监听监听状态变更
listen<boolean>(LISTEN_KEY.TOGGLE_LISTENING, ({ payload }) => {
if (payload) {
startListen();
Expand All @@ -110,21 +97,25 @@ const ClipboardPanel = () => {
}
});

// 监听全局配置变更
listen(LISTEN_KEY.GLOBAL_STORE_CHANGED, ({ payload }) => {
if (isEqual(globalStore, payload)) return;

merge(globalStore, payload);
});

// 监听剪切板配置变更
listen(LISTEN_KEY.CLIPBOARD_STORE_CHANGED, ({ payload }) => {
if (isEqual(clipboardStore, payload)) return;

merge(clipboardStore, payload);
});

// 监听主窗口显示/隐藏
listen(LISTEN_KEY.TOGGLE_MAIN_WINDOW_VISIBLE, toggleWindowVisible);
});

// 监听窗口焦点
useFocus({
onBlur() {
if (state.pin) return;
Expand All @@ -133,8 +124,10 @@ const ClipboardPanel = () => {
},
});

// 监听快捷键
useRegister(toggleWindowVisible, [shortcut.clipboard]);

// 获取剪切板内容
const getClipboardList = async () => {
const { search, group, isCollected } = state;

Expand Down
10 changes: 6 additions & 4 deletions src/pages/Clipboard/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ const ClipboardSettings = () => {
/>

<ProSwitch
title={t("preference.clipboard.content_settings.label.copy_as_plain")}
title={t(
"preference.clipboard.content_settings.label.paste_as_plain",
)}
description={t(
"preference.clipboard.content_settings.hints.copy_as_plain",
"preference.clipboard.content_settings.hints.paste_as_plain",
)}
value={content.copyPlainText}
value={content.pastePlainText}
onChange={(value) => {
clipboardStore.content.copyPlainText = value;
clipboardStore.content.pastePlainText = value;
}}
/>
</ProList>
Expand Down
27 changes: 21 additions & 6 deletions src/plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const readFiles = async (): Promise<ClipboardPayload> => {
size,
search: fileNames.join(" "),
value: JSON.stringify(files),
group: "files",
};
};

Expand Down Expand Up @@ -113,8 +114,9 @@ export const readImage = async (): Promise<ClipboardPayload> => {
return {
...rest,
size,
search,
value,
search,
group: "image",
};
};

Expand All @@ -130,6 +132,7 @@ export const readHTML = async (): Promise<ClipboardPayload> => {
size,
value: html,
search: value,
group: "text",
};
};

Expand All @@ -142,9 +145,10 @@ export const readRichText = async (): Promise<ClipboardPayload> => {
const { value, size } = await readText();

return {
size,
value: richText,
search: value,
size,
group: "text",
};
};

Expand All @@ -158,6 +162,7 @@ export const readText = async (): Promise<ClipboardPayload> => {
value: text,
search: text,
size: text.length,
group: "text",
};
};

Expand All @@ -167,8 +172,6 @@ export const readText = async (): Promise<ClipboardPayload> => {
export const readClipboard = async () => {
let payload!: ClipboardPayload;

const { content } = clipboardStore;

const has = {
files: await hasFiles(),
image: await hasImage(),
Expand All @@ -185,11 +188,11 @@ export const readClipboard = async () => {
const imagePayload = await readImage();

payload = { ...imagePayload, type: "image" };
} else if (!content.copyPlainText && has.html) {
} else if (has.html) {
const htmlPayload = await readHTML();

payload = { ...htmlPayload, type: "html" };
} else if (!content.copyPlainText && has.richText) {
} else if (has.richText) {
const richTextPayload = await readRichText();

payload = { ...richTextPayload, type: "rich-text" };
Expand Down Expand Up @@ -224,6 +227,12 @@ export const writeImage = (value: string) => {
* HTML 内容写入剪贴板
*/
export const writeHTML = (text: string, html: string) => {
const { pastePlainText } = clipboardStore.content;

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

return invoke(CLIPBOARD_PLUGIN.WRITE_HTML, {
text,
html,
Expand All @@ -234,6 +243,12 @@ export const writeHTML = (text: string, html: string) => {
* 富文写入剪贴板
*/
export const writeRichText = (text: string, richText: string) => {
const { pastePlainText } = clipboardStore.content;

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

return invoke(CLIPBOARD_PLUGIN.WRITE_RICH_TEXT, {
text,
richText,
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,
copyPlainText: false,
pastePlainText: false,
},

history: {
Expand Down
1 change: 0 additions & 1 deletion src/types/database.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export type TableName = "history";

export interface ClipboardItem extends ClipboardPayload {
id: number;
group: "text" | "image" | "files";
createTime: string;
isCollected: boolean;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ReadImage {

export interface ClipboardPayload {
type?: "text" | "rich-text" | "html" | "image" | "files";
group: "text" | "image" | "files";
size: number;
value: string;
search: string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/store.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface ClipboardStore {
content: {
autoPaste: "single" | "double";
ocr: boolean;
copyPlainText: boolean;
pastePlainText: boolean;
};

// 历史记录
Expand Down

0 comments on commit 4d8da60

Please sign in to comment.