diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 536140d..17a5855 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -2029,6 +2029,30 @@ pub fn copy_text_to_clipboard(_text: String) -> Result<(), String> { Err("Text clipboard copy is only supported on desktop".to_string()) } +/// Read text from the system clipboard through the native backend. +/// Works around the WebKitGTK clipboard bug where the async `navigator.clipboard` +/// API returns empty for text copied from external applications. +#[cfg(desktop)] +#[tauri::command] +pub async fn read_clipboard_text() -> Result { + // Reading the clipboard can block while the owning application answers, like the image read below. + tauri::async_runtime::spawn_blocking(move || { + let mut clipboard = + arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; + clipboard + .get_text() + .map_err(|e| format!("Failed to read text from clipboard: {}", e)) + }) + .await + .map_err(|error| error.to_string())? +} + +#[cfg(mobile)] +#[tauri::command] +pub async fn read_clipboard_text() -> Result { + Err("Text clipboard read is only supported on desktop".to_string()) +} + /// Read image from system clipboard (bypasses WebKitGTK clipboard bug). /// Returns PNG bytes as Vec, or error if no image on clipboard. #[cfg(desktop)] diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index c1279fe..aa8ac26 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -212,6 +212,7 @@ pub fn run() { commands::load_vault_state, commands::save_vault_state, commands::copy_text_to_clipboard, + commands::read_clipboard_text, commands::read_clipboard_image, commands::copy_image_to_clipboard, commands::save_image, diff --git a/src/lib/api.ts b/src/lib/api.ts index a61b290..c7947f0 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -280,6 +280,10 @@ export async function readClipboardImage(): Promise { return invoke("read_clipboard_image"); } +export async function readClipboardText(): Promise { + return invoke("read_clipboard_text"); +} + export async function copyImageToClipboard(path: string): Promise { return invoke("copy_image_to_clipboard", { path }); } diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index b9967bc..830ac94 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -35,7 +35,7 @@ import { openFile, openUrl, copyFileTo, copyImageToClipboard as copyImageToClipboardCmd, writeBytesTo, copyPngToClipboard, copyTextToClipboard } from '$lib/api'; import { save as saveDialog } from '@tauri-apps/plugin-dialog'; import { activeNote, activeNotePath, appConfig, editorDirty, sourceMode, focusMode, readOnly, quickAccessPaths, notes, navHistory, canGoBack, canGoForward, viewerNote, viewMode, notebooks, outlineWidth } from '$lib/stores/app'; - import { saveNote, importNote, createNote, saveImage, saveAttachment, readClipboardImage, addQuickAccess, removeQuickAccess, getQuickAccess, getNoteVersions, getNoteVersionContent, createVersion, aiAsk, getAllNoteTitles, readNote, renameNote, setToolbarLayout } from '$lib/api'; + import { saveNote, importNote, createNote, saveImage, saveAttachment, readClipboardImage, readClipboardText, addQuickAccess, removeQuickAccess, getQuickAccess, getNoteVersions, getNoteVersionContent, createVersion, aiAsk, getAllNoteTitles, readNote, renameNote, setToolbarLayout } from '$lib/api'; import type { NoteContent, VersionEntry, AiStreamEvent, NoteTitleEntry, TaskItem as TaskRecord, FileEvent, SearchTarget, ToolbarLayout } from '$lib/types'; import { listen } from '@tauri-apps/api/event'; import { debounce } from '$lib/utils/debounce'; @@ -62,6 +62,7 @@ import { restoreTitleHeading, stripTitleHeading, type HiddenTitleHeading } from '$lib/editor/titleVisibility'; import { documentHeadings, sourceHeadings, activeHeadingIndex, mapDocumentHeadings, mapSourceHeadings, type OutlineHeading } from '$lib/editor/outline'; import { createEditorMarkdownParser } from '$lib/editor/markdownParser'; + import { collectClipboardPayload, isEmptyPayload, type ClipboardPastePayload } from '$lib/utils/clipboard-paste'; import { externalNoteReferences } from '$lib/editor/externalNote'; import { tagIterationKey } from '$lib/utils/tag-styles'; import { replaceWithWikiLink } from '$lib/editor/wikiLinks'; @@ -5850,28 +5851,60 @@ if (isOriginCurrent(origin)) closeTextContextMenu(); } + /** + * Replays clipboard data as a paste event on the editor DOM, so it takes the same path as + * Ctrl/Cmd+V. ProseMirror's paste handler then runs every `handlePaste` prop (file paste, + * callout paste, link paste), `transformPastedHTML`, the clipboard parsers and TipTap's paste rules. + */ + function replayPaste(payload: ClipboardPastePayload): boolean { + if (!editor) return false; + const data = new DataTransfer(); + if (payload.text) data.setData('text/plain', payload.text); + if (payload.html) data.setData('text/html', payload.html); + for (const [type, value] of payload.strings) data.setData(type, value); + for (const file of payload.files) data.items.add(file); + const event = new ClipboardEvent('paste', { clipboardData: data, bubbles: true, cancelable: true }); + editor.view.focus(); + editor.view.dom.dispatchEvent(event); + return event.defaultPrevented; + } + + async function readWebClipboard(): Promise { + try { + const payload = await collectClipboardPayload(await navigator.clipboard.read()); + return isEmptyPayload(payload) ? null : payload; + } catch (e) { + // WebKitGTK (bug #218519) often cannot read content copied from other applications. + console.warn('Web clipboard read failed, using the native clipboard:', e); + return null; + } + } + async function ctxPaste() { // insertClipboardImage() saves the attachment before inserting it, so an // unguarded paste here would leave an orphan file behind when the editor // cannot accept the insertion. if (!editor || $readOnly || $viewerNote) { closeTextContextMenu(); return; } const origin = captureOrigin(); + const payload = await readWebClipboard(); + if (!canInsert(origin) || !editor) return; + if (payload && replayPaste(payload)) { + closeTextContextMenu(); + return; + } + // Native fallback when the web clipboard is unavailable or empty. An image comes first, so a + // browser "Copy image" pastes the bitmap rather than the image URL that may come with it. + if (await insertClipboardImage(origin)) { + if (isOriginCurrent(origin)) closeTextContextMenu(); + return; + } try { - const text = await navigator.clipboard.readText(); + const text = await readClipboardText(); if (!canInsert(origin) || !editor) return; - if (text) { - editor.commands.insertContent(text); - closeTextContextMenu(); - return; - } + if (text) replayPaste({ text, html: '', strings: [], files: [] }); } catch (e) { - console.error('Paste failed:', e); + console.error('Native clipboard read failed:', e); } - // readText() only reads text/plain. An image copied from a browser has none, - // so pasting an image from this menu used to do nothing at all. Fall back to - // the same native clipboard reader the paste handler uses; it is a no-op when - // the clipboard holds no image. - await insertClipboardImage(origin); if (isOriginCurrent(origin)) closeTextContextMenu(); } @@ -6506,14 +6539,17 @@ return false; } - async function insertClipboardImage(origin = captureOrigin()) { - if (!canInsert(origin)) return; + /** Inserts the native clipboard image. Resolves true when there was an image to insert. */ + async function insertClipboardImage(origin = captureOrigin()): Promise { + if (!canInsert(origin)) return false; try { const data = await readClipboardImage(); - if (!canInsert(origin)) return; + if (!canInsert(origin)) return false; await uploadAsset(origin, 'pasted-image.png', data, 'image'); + return true; } catch (e) { console.error('Clipboard image fallback failed:', e); + return false; } } diff --git a/src/lib/utils/clipboard-paste.test.mjs b/src/lib/utils/clipboard-paste.test.mjs new file mode 100644 index 0000000..4c8272c --- /dev/null +++ b/src/lib/utils/clipboard-paste.test.mjs @@ -0,0 +1,81 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { + classifyClipboardType, + collectClipboardPayload, + isEmptyPayload, + pastedFileName, + plainTextFromUriList +} from './clipboard-paste.ts'; + +const item = entries => ({ + types: Object.keys(entries), + getType: async type => { + const value = entries[type]; + return value instanceof Blob ? value : new Blob([value], { type }); + } +}); +const png = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: 'image/png' }); + +test('classifies text formats as text, attachments as files and metadata as ignored', () => { + assert.equal(classifyClipboardType('text/plain'), 'plain'); + assert.equal(classifyClipboardType('text/plain;charset=utf-8'), 'plain'); + assert.equal(classifyClipboardType('TEXT/HTML'), 'html'); + for (const type of ['text/uri-list', 'text/rtf', 'text/x-moz-url', 'text/csv']) { + assert.equal(classifyClipboardType(type), 'string', type); + } + for (const type of ['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf']) { + assert.equal(classifyClipboardType(type), 'file', type); + } + for (const type of ['web application/x-custom', 'chromium/x-web-custom-data', 'application/x-moz-nativeimage', 'application/octet-stream', '']) { + assert.equal(classifyClipboardType(type), 'ignore', type); + } +}); + +test('a copied link stays text and never becomes a uri-list attachment', async () => { + const payload = await collectClipboardPayload([item({ 'text/uri-list': 'https://example.com/a\r\n' })]); + assert.equal(payload.files.length, 0); + assert.equal(payload.text, 'https://example.com/a'); + assert.deepEqual(payload.strings, [['text/uri-list', 'https://example.com/a\r\n']]); +}); + +test('text/plain wins over the uri-list fallback', async () => { + const payload = await collectClipboardPayload([item({ 'text/plain': 'Example', 'text/uri-list': 'https://example.com' })]); + assert.equal(payload.text, 'Example'); + assert.equal(payload.files.length, 0); +}); + +test('uri-list comments and blank lines are skipped', () => { + assert.equal(plainTextFromUriList('# copied from browser\n\n https://a.example \nhttps://b.example'), 'https://a.example'); + assert.equal(plainTextFromUriList('# only a comment\n'), ''); +}); + +test('images and PDFs become named files alongside the text and HTML', async () => { + const pdf = new Blob(['%PDF-1.7'], { type: 'application/pdf' }); + const payload = await collectClipboardPayload([ + item({ 'text/html': '', 'image/png': png }), + item({ 'application/pdf': pdf }) + ]); + assert.equal(payload.html, ''); + assert.deepEqual(payload.files.map(file => [file.name, file.type]), [ + ['pasted-image.png', 'image/png'], + ['pasted-file-2.pdf', 'application/pdf'] + ]); + assert.equal(isEmptyPayload(payload), false); +}); + +test('metadata-only clipboards are empty and are never read', async () => { + const read = []; + const payload = await collectClipboardPayload([{ + types: ['chromium/x-web-custom-data', 'web application/x-custom'], + getType: async type => { read.push(type); return new Blob(['x']); } + }]); + assert.deepEqual(read, []); + assert.equal(isEmptyPayload(payload), true); +}); + +test('pasted file names use friendly extensions', () => { + assert.equal(pastedFileName('image/jpeg', 0), 'pasted-image.jpg'); + assert.equal(pastedFileName('image/svg+xml', 1), 'pasted-image-2.svg'); + assert.equal(pastedFileName('application/pdf', 0), 'pasted-file.pdf'); +}); diff --git a/src/lib/utils/clipboard-paste.ts b/src/lib/utils/clipboard-paste.ts new file mode 100644 index 0000000..52d932c --- /dev/null +++ b/src/lib/utils/clipboard-paste.ts @@ -0,0 +1,95 @@ +/** + * Turns what the async clipboard API (`navigator.clipboard.read()`) returns into the data a keyboard + * paste would carry, so the editor's context-menu Paste can replay it as a real paste event. + * + * The clipboard holds many representations of one copy: plain text, HTML, URI lists, bitmaps, and + * browser/OS bookkeeping formats. Only actual attachments may become files. Text formats such as + * `text/uri-list` stay text, and unknown metadata is dropped. Turning them into files would insert + * junk attachments like `pasted-0.uri-list`. + */ + +/** How one clipboard MIME type is pasted. */ +export type ClipboardTypeRole = 'plain' | 'html' | 'string' | 'file' | 'ignore'; + +/** Attachment types a keyboard paste would deliver as files (see `insertFile` in Editor.svelte). */ +const FILE_TYPES = /^(?:image\/[\w.+-]+|application\/pdf)$/; + +/** Strips parameters (`text/plain;charset=utf-8`) and case. */ +export function normalizeClipboardType(type: string): string { + return type.split(';')[0].trim().toLowerCase(); +} + +export function classifyClipboardType(type: string): ClipboardTypeRole { + const mime = normalizeClipboardType(type); + if (mime === 'text/plain') return 'plain'; + if (mime === 'text/html') return 'html'; + // Other text formats (uri-list, rtf, x-moz-url, ...) are string data on the paste event, never files. + if (/^text\/[\w.+-]+$/.test(mime)) return 'string'; + if (FILE_TYPES.test(mime)) return 'file'; + // `web ...` custom formats, chromium/..., application/x-..., and anything else unrecognized. + return 'ignore'; +} + +/** First URI of an RFC 2483 `text/uri-list`. Lines starting with `#` are comments. */ +export function plainTextFromUriList(list: string): string { + for (const line of list.split(/\r?\n/)) { + const uri = line.trim(); + if (uri && !uri.startsWith('#')) return uri; + } + return ''; +} + +const EXTENSIONS: Record = { 'image/jpeg': 'jpg', 'image/svg+xml': 'svg', 'application/pdf': 'pdf' }; + +/** File name for a pasted attachment, matching the `pasted-image.png` name of the native image fallback. */ +export function pastedFileName(type: string, index: number): string { + const mime = normalizeClipboardType(type); + const extension = EXTENSIONS[mime] ?? (mime.split('/')[1]?.replace(/[^\w]/g, '') || 'bin'); + const base = mime.startsWith('image/') ? 'pasted-image' : 'pasted-file'; + return `${base}${index > 0 ? `-${index + 1}` : ''}.${extension}`; +} + +export interface ClipboardItemLike { + readonly types: readonly string[]; + getType(type: string): Promise; +} + +export interface ClipboardPastePayload { + text: string; + html: string; + /** Other text formats, keyed by normalized MIME type. */ + strings: [type: string, value: string][]; + files: File[]; +} + +export function isEmptyPayload(payload: ClipboardPastePayload): boolean { + return !payload.text && !payload.html && payload.files.length === 0; +} + +/** Reads every clipboard item into paste data, classifying each representation. */ +export async function collectClipboardPayload(items: readonly ClipboardItemLike[]): Promise { + const payload: ClipboardPastePayload = { text: '', html: '', strings: [], files: [] }; + for (const item of items) { + for (const type of item.types) { + const role = classifyClipboardType(type); + if (role === 'ignore') continue; + const blob = await item.getType(type); + if (role === 'file') { + const mime = normalizeClipboardType(type); + payload.files.push(new File([blob], pastedFileName(mime, payload.files.length), { type: mime })); + continue; + } + const value = await blob.text(); + if (!value) continue; + if (role === 'plain') payload.text ||= value; + else if (role === 'html') payload.html ||= value; + else payload.strings.push([normalizeClipboardType(type), value]); + } + } + // Some apps offer a copied link only as a URI list. Paste the link as text instead of nothing. + if (!payload.text) { + const uriList = payload.strings.find(([type]) => type === 'text/uri-list'); + if (uriList) payload.text = plainTextFromUriList(uriList[1]); + } + return payload; +}