'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 };