diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index b024aa7..5a3f68a 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -37,7 +37,7 @@ import { readFile } from '@tauri-apps/plugin-fs'; import { openUrl } from '@tauri-apps/plugin-opener'; import { openFile, copyFileTo } from '$lib/api'; - import { save as saveDialog, open as openDialog } from '@tauri-apps/plugin-dialog'; + import { save as saveDialog } from '@tauri-apps/plugin-dialog'; import { activeNote, activeNotePath, appConfig, editorDirty, sourceMode, focusMode, readOnly, quickAccessPaths, notes } from '$lib/stores/app'; import { saveNote, saveImage, saveAttachment, addQuickAccess, removeQuickAccess, getQuickAccess, getNoteVersions, getNoteVersionContent, createVersion, aiAsk, getAllNoteTitles, readNote } from '$lib/api'; import type { VersionEntry, AiStreamEvent, NoteTitleEntry } from '$lib/types'; @@ -2827,77 +2827,6 @@ } } - async function pickAndInsertImage() { - try { - const selected = await openDialog({ - multiple: false, - filters: [{ name: 'Images', extensions: ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'bmp', 'ico', 'tiff', 'tif', 'avif'] }] - }); - if (!selected) return; - const filePath = typeof selected === 'string' ? selected : selected; - const fileName = (filePath as string).split('/').pop() || 'image.png'; - const fileData = await readFile(filePath as string); - const data = Array.from(fileData); - const relativePath = await saveImage(fileName, data); - if (editor) { - const displaySrc = resolveImageSrc(relativePath); - editor.chain().focus().setImage({ src: displaySrc }).run(); - } - } catch (e) { - console.error('Failed to pick and insert image:', e); - } - } - - async function pickAndInsertFile() { - try { - const selected = await openDialog({ - multiple: false, - }); - if (!selected) return; - const filePath = typeof selected === 'string' ? selected : selected; - const fileName = (filePath as string).split('/').pop() || 'file'; - const ext = fileName.split('.').pop()?.toLowerCase() || ''; - const imageExts = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'bmp', 'ico', 'tiff', 'tif', 'avif']; - const fileData = await readFile(filePath as string); - const data = Array.from(fileData); - - if (imageExts.includes(ext)) { - const relativePath = await saveImage(fileName, data); - if (editor) { - const displaySrc = resolveImageSrc(relativePath); - editor.chain().focus().setImage({ src: displaySrc }).run(); - } - } else if (ext === 'pdf') { - const relativePath = await saveAttachment(fileName, data); - if (!editor) return; - const usePdfPreview = $appConfig?.pdf_preview ?? false; - if (usePdfPreview) { - editor.chain().focus().insertContent({ - type: 'pdfEmbed', - attrs: { src: relativePath, name: fileName }, - }).run(); - } else { - const sizeKB = Math.round(fileData.length / 1024); - const label = `${fileName} (${sizeKB} kB)`; - editor.chain().focus() - .insertContent(`${label} `) - .run(); - } - } else { - const relativePath = await saveAttachment(fileName, data); - if (editor) { - const sizeKB = Math.round(fileData.length / 1024); - const label = `${fileName} (${sizeKB} kB)`; - editor.chain().focus() - .insertContent(`${label} `) - .run(); - } - } - } catch (e) { - console.error('Failed to pick and insert file:', e); - } - } - // Source mode toggle — only react to explicit user toggle, not note switches $effect(() => { const isSource = $sourceMode; @@ -3453,11 +3382,11 @@ {#if insertDropdown}
e.stopPropagation()}> - - @@ -3488,11 +3417,11 @@ {#if insertDropdown}
e.stopPropagation()}> - - @@ -3787,6 +3716,21 @@ {/if} {/if} + + { + const file = (e.target as HTMLInputElement).files?.[0]; + if (file) insertImage(file); + (e.target as HTMLInputElement).value = ''; + }} /> + { + const file = (e.target as HTMLInputElement).files?.[0]; + if (file) { + if (file.type.startsWith('image/')) insertImage(file); + else if (file.type === 'application/pdf') insertPdf(file); + else insertFileAttachment(file); + } + (e.target as HTMLInputElement).value = ''; + }} />
{#if linkContextMenu}