This commit is contained in:
2026-09-12 13:59:12 +08:00
commit 941ce4baab
71 changed files with 13037 additions and 0 deletions

31
lib/clip-payload.js Normal file
View File

@@ -0,0 +1,31 @@
'use strict';
/**
* 把一行 clips 记录转成"要写进系统剪贴板的东西"。
*
* 单独抽出来是因为这里踩过坑copyClipToClipboard 里读的是
* rowToClip() 的返回值上的 .image而 rowToClip() 只产出 imageBase64
* 于是 Buffer.from(undefined) 抛错 —— 图片条目的"复制"整个静默失败,
* 而且错误被 catch 吞掉,用户只会觉得"点了没反应"。
*
* 这一层不碰 electron可以直接单测。
*
* @param {object} row 来自 `SELECT * FROM clips`(必须含 image 列)
* @returns {{kind:'text', text:string} | {kind:'image', buffer:Buffer}}
* @throws {Error} row 缺失或图片数据为空时
*/
function clipPayload(row) {
if (!row) throw new Error('记录不存在');
if (row.type === 'text') {
return { kind: 'text', text: row.text || '' };
}
if (row.type === 'image') {
if (row.image == null) throw new Error('图片数据为空');
const buffer = Buffer.isBuffer(row.image) ? row.image : Buffer.from(row.image);
if (buffer.length === 0) throw new Error('图片数据为空');
return { kind: 'image', buffer };
}
throw new Error(`未知的记录类型: ${row.type}`);
}
module.exports = { clipPayload };