Skip to content

Commit

Permalink
feat: 过滤掉长度为 0 的字符串 (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Dec 2, 2024
1 parent 1754180 commit cc53b38
Showing 1 changed file with 56 additions and 55 deletions.
111 changes: 56 additions & 55 deletions src/plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,47 +182,6 @@ export const readText = async (): Promise<ClipboardPayload> => {
return data;
};

/**
* 读取剪贴板内容
*/
export const readClipboard = async () => {
let payload!: ClipboardPayload;

const { copyPlain } = clipboardStore.content;

const has = {
files: await hasFiles(),
image: await hasImage(),
html: await hasHTML(),
rtf: await hasRTF(),
text: await hasText(),
};

if (has.files) {
const filesPayload = await readFiles();

payload = { ...filesPayload, type: "files" };
} else if (has.image && !has.text) {
const imagePayload = await readImage();

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

payload = { ...htmlPayload, type: "html" };
} else if (!copyPlain && has.rtf) {
const rtfPayload = await readRTF();

payload = { ...rtfPayload, type: "rtf" };
} else {
const textPayload = await readText();

payload = { ...textPayload, type: "text" };
}

return payload;
};

/**
* 文件写入剪贴板
*/
Expand Down Expand Up @@ -282,29 +241,71 @@ export const writeText = (value: string) => {
});
};

/**
* 读取剪贴板内容
*/
export const readClipboard = async () => {
let payload!: ClipboardPayload;

const { copyPlain } = clipboardStore.content;

const has = {
files: await hasFiles(),
image: await hasImage(),
html: await hasHTML(),
rtf: await hasRTF(),
text: await hasText(),
};

if (has.files) {
const filesPayload = await readFiles();

payload = { ...filesPayload, type: "files" };
} else if (has.image && !has.text) {
const imagePayload = await readImage();

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

payload = { ...htmlPayload, type: "html" };
} else if (!copyPlain && has.rtf) {
const rtfPayload = await readRTF();

payload = { ...rtfPayload, type: "rtf" };
} else {
const textPayload = await readText();

payload = { ...textPayload, type: "text" };
}

return payload;
};

/**
* 剪贴板更新
*/
export const onClipboardUpdate = (
fn: (payload: ClipboardPayload, oldPayload: ClipboardPayload) => void,
) => {
// 防抖间隔(ms)
const DEBOUNCE = 200;
let lastUpdatedAt = 0;
let oldPayload: ClipboardPayload;
export const onClipboardUpdate = (fn: (payload: ClipboardPayload) => void) => {
let lastUpdated = 0;
let previousPayload: ClipboardPayload;

return listen(CLIPBOARD_PLUGIN.CLIPBOARD_UPDATE, async () => {
const payload = await readClipboard();

if (
Date.now() - lastUpdatedAt > DEBOUNCE ||
!isEqual(payload, oldPayload)
) {
fn(payload, { ...oldPayload });
const { group, count } = payload;

if (group === "text" && count === 0) {
return;
}

const expired = Date.now() - lastUpdated > 200;

if (expired || !isEqual(payload, previousPayload)) {
fn(payload);
}

lastUpdatedAt = Date.now();
oldPayload = payload;
lastUpdated = Date.now();
previousPayload = payload;
});
};

Expand Down

0 comments on commit cc53b38

Please sign in to comment.