|
1 | 1 | import { sessionStoreSetters } from "@features/sessions/stores/sessionStore"; |
| 2 | +import { useSettingsStore as useFeatureSettingsStore } from "@features/settings/stores/settingsStore"; |
2 | 3 | import { trpcVanilla } from "@renderer/trpc/client"; |
3 | 4 | import { toast } from "@renderer/utils/toast"; |
4 | 5 | import { useSettingsStore } from "@stores/settingsStore"; |
@@ -238,41 +239,72 @@ export function useTiptapEditor(options: UseTiptapEditorOptions) { |
238 | 239 | } |
239 | 240 | } |
240 | 241 |
|
241 | | - if (imageItems.length === 0) return false; |
| 242 | + if (imageItems.length > 0) { |
| 243 | + event.preventDefault(); |
242 | 244 |
|
243 | | - event.preventDefault(); |
| 245 | + (async () => { |
| 246 | + for (const item of imageItems) { |
| 247 | + const file = item.getAsFile(); |
| 248 | + if (!file) continue; |
| 249 | + |
| 250 | + try { |
| 251 | + const arrayBuffer = await file.arrayBuffer(); |
| 252 | + const base64 = btoa( |
| 253 | + new Uint8Array(arrayBuffer).reduce( |
| 254 | + (data, byte) => data + String.fromCharCode(byte), |
| 255 | + "", |
| 256 | + ), |
| 257 | + ); |
| 258 | + |
| 259 | + const result = await trpcVanilla.os.saveClipboardImage.mutate( |
| 260 | + { |
| 261 | + base64Data: base64, |
| 262 | + mimeType: file.type, |
| 263 | + originalName: file.name, |
| 264 | + }, |
| 265 | + ); |
| 266 | + |
| 267 | + setAttachments((prev) => { |
| 268 | + if (prev.some((a) => a.id === result.path)) return prev; |
| 269 | + return [...prev, { id: result.path, label: result.name }]; |
| 270 | + }); |
| 271 | + } catch (_error) { |
| 272 | + toast.error("Failed to paste image"); |
| 273 | + } |
| 274 | + } |
| 275 | + })(); |
244 | 276 |
|
245 | | - (async () => { |
246 | | - for (const item of imageItems) { |
247 | | - const file = item.getAsFile(); |
248 | | - if (!file) continue; |
| 277 | + return true; |
| 278 | + } |
249 | 279 |
|
250 | | - try { |
251 | | - const arrayBuffer = await file.arrayBuffer(); |
252 | | - const base64 = btoa( |
253 | | - new Uint8Array(arrayBuffer).reduce( |
254 | | - (data, byte) => data + String.fromCharCode(byte), |
255 | | - "", |
256 | | - ), |
257 | | - ); |
| 280 | + // Auto-convert long pasted text into a file attachment |
| 281 | + const pastedText = event.clipboardData?.getData("text/plain"); |
| 282 | + if ( |
| 283 | + pastedText && |
| 284 | + pastedText.length > 500 && |
| 285 | + useFeatureSettingsStore.getState().autoConvertLongText |
| 286 | + ) { |
| 287 | + event.preventDefault(); |
258 | 288 |
|
259 | | - const result = await trpcVanilla.os.saveClipboardImage.mutate({ |
260 | | - base64Data: base64, |
261 | | - mimeType: file.type, |
262 | | - originalName: file.name, |
| 289 | + (async () => { |
| 290 | + try { |
| 291 | + const result = await trpcVanilla.os.saveClipboardText.mutate({ |
| 292 | + text: pastedText, |
263 | 293 | }); |
264 | 294 |
|
265 | 295 | setAttachments((prev) => { |
266 | 296 | if (prev.some((a) => a.id === result.path)) return prev; |
267 | 297 | return [...prev, { id: result.path, label: result.name }]; |
268 | 298 | }); |
269 | 299 | } catch (_error) { |
270 | | - toast.error("Failed to paste image"); |
| 300 | + toast.error("Failed to convert pasted text to attachment"); |
271 | 301 | } |
272 | | - } |
273 | | - })(); |
| 302 | + })(); |
274 | 303 |
|
275 | | - return true; |
| 304 | + return true; |
| 305 | + } |
| 306 | + |
| 307 | + return false; |
276 | 308 | }, |
277 | 309 | }, |
278 | 310 | onCreate: () => { |
|
0 commit comments