Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(editor): save pasted blob: images beside the note without withholding the paste (#35625)

OrcaWincb33f2b2d864
diff --git a/src/renderer/src/components/editor/rich-markdown-blob-image-paste.test.ts b/src/renderer/src/components/editor/rich-markdown-blob-image-paste.test.ts
new file mode 100644
index 00000000000..a7e408ba603
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-blob-image-paste.test.ts
@@ -0,0 +1,141 @@
+// @vitest-environment happy-dom
+import type * as ImageBlobPng from '@/lib/image-blob-png'
+import { Editor } from '@tiptap/react'
+import StarterKit from '@tiptap/starter-kit'
+import Image from '@tiptap/extension-image'
+import { Markdown } from '@tiptap/markdown'
+import { DOMParser } from '@tiptap/pm/model'
+import { toast } from 'sonner'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+import { handleRichMarkdownPaste } from './rich-markdown-paste-handler'
+
+vi.mock('@/store', () => ({
+ useAppStore: {
+ getState: () => ({ settings: null, folderWorkspaces: [], worktreesByRepo: {}, openFiles: [] })
+ }
+}))
+vi.mock('@/lib/connection-context', () => ({ getConnectionId: () => null }))
+vi.mock('@/lib/image-blob-png', async (original) => {
+ const actual = await original<typeof ImageBlobPng>()
+ return { ...actual, convertImageBlobToPng: async (blob: Blob) => blob }
+})
+vi.mock('sonner', () => ({ toast: { error: vi.fn(), info: vi.fn() } }))
+
+const editors: Editor[] = []
+const saves = vi.fn()
+const imports = vi.fn()
+const ownBlob = (id: string): string => `blob:${window.location.origin}/${id}`
+
+function createEditor(): Editor {
+ const element = document.createElement('div')
+ document.body.append(element)
+ const editor = new Editor({
+ element,
+ extensions: [StarterKit, Image.configure({ inline: true }), Markdown],
+ content: 'left right',
+ contentType: 'markdown'
+ })
+ editors.push(editor)
+ editor.commands.setTextSelection(6)
+ return editor
+}
+
+function paste(editor: Editor, html: string): void {
+ const container = document.createElement('div')
+ container.innerHTML = html
+ const slice = DOMParser.fromSchema(editor.schema).parseSlice(container)
+ const clipboardData = new DataTransfer()
+ clipboardData.setData('text/html', html)
+ const event = new ClipboardEvent('paste', { clipboardData, cancelable: true })
+ if (
+ !handleRichMarkdownPaste({
+ editor,
+ event,
+ filePath: '/notes/note.md',
+ worktreeId: '',
+ slice,
+ view: editor.view
+ })
+ ) {
+ editor.view.dispatch(editor.state.tr.replaceSelection(slice).setMeta('uiEvent', 'paste'))
+ }
+}
+
+beforeEach(() => {
+ vi.clearAllMocks()
+ let next = 0
+ saves.mockImplementation(async () => ({ ok: true, result: `/tmp/pasted-${++next}.png` }))
+ imports.mockImplementation(async ({ sourcePaths }: { sourcePaths: string[] }) => ({
+ results: sourcePaths.map((sourcePath) => ({
+ status: 'imported',
+ kind: 'file',
+ sourcePath,
+ destPath: `/notes/${sourcePath.split('/').at(-1)}`,
+ renamed: false
+ }))
+ }))
+ Object.defineProperty(window, 'api', {
+ configurable: true,
+ value: { runtime: { call: saves }, fs: { importExternalPaths: imports } }
+ })
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (src: string) => ({
+ ok: true,
+ blob: async () => new Blob([src], { type: 'image/png' })
+ }))
+ )
+})
+
+afterEach(() => {
+ editors.splice(0).forEach((editor) => editor.destroy())
+ document.body.replaceChildren()
+ vi.unstubAllGlobals()
+})
+
+it('inserts the paste immediately, then saves blob images beside the note', async () => {
+ const editor = createEditor()
+ paste(
+ editor,
+ `<p>caption <strong>bold</strong><img src="${ownBlob('a')}" alt="first" title="one"> between <img src="${ownBlob('b')}" alt="second"></p>`
+ )
+ expect(editor.getMarkdown()).toContain('**bold**')
+ await vi.waitFor(() => expect(editor.getMarkdown()).not.toContain('blob:'))
+ const markdown = editor.getMarkdown()
+ expect(markdown).toContain('![first](pasted-1.png "one")')
+ expect(markdown).toContain('![second](pasted-2.png)')
+ expect(saves.mock.calls.map(([args]) => args.params.contentBase64)).toEqual([
+ btoa(ownBlob('a')),
+ btoa(ownBlob('b'))
+ ])
+ expect(imports.mock.calls[0]?.[0]).toMatchObject({ destDir: '/notes' })
+ expect(editor.commands.undo()).toBe(true)
+ expect(editor.getMarkdown()).toBe('left right')
+})
+
+it('keeps the pasted text and the remaining images when one image cannot be read', async () => {
+ vi.mocked(fetch).mockImplementation(async (input) => {
+ if (input === ownBlob('gone')) {
+ throw new TypeError('Failed to fetch')
+ }
+ return new Response(new Blob(['ok'], { type: 'image/png' }))
+ })
+ const editor = createEditor()
+ paste(
+ editor,
+ `<p>kept text <img src="${ownBlob('gone')}" alt="gone"><img src="${ownBlob('ok')}" alt="ok"></p>`
+ )
+ await vi.waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1))
+ const markdown = editor.getMarkdown()
+ expect(markdown).toContain('kept text')
+ expect(markdown).toContain(`![gone](${ownBlob('gone')})`)
+ expect(markdown).toContain('![ok](pasted-1.png)')
+})
+
+it('leaves blob images from other origins to the native paste', async () => {
+ const editor = createEditor()
+ paste(editor, '<p>external <img src="blob:https://example.com/abc" alt="x"></p>')
+ await Promise.resolve()
+ expect(fetch).not.toHaveBeenCalled()
+ expect(editor.getMarkdown()).toContain('external ![x](blob:https://example.com/abc)')
+})
diff --git a/src/renderer/src/components/editor/rich-markdown-blob-image-paste.ts b/src/renderer/src/components/editor/rich-markdown-blob-image-paste.ts
new file mode 100644
index 00000000000..3e868635a88
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-blob-image-paste.ts
@@ -0,0 +1,156 @@
+import type { Editor } from '@tiptap/react'
+import type { Slice } from '@tiptap/pm/model'
+import { toast } from 'sonner'
+import { translate } from '@/i18n/i18n'
+import { blobToBase64, convertImageBlobToPng } from '@/lib/image-blob-png'
+import { importExternalPathsToRuntime } from '@/runtime/runtime-file-import-client'
+import { callRuntimeRpc } from '@/runtime/runtime-rpc-client'
+import { assertClipboardImageBase64LengthWithinLimit } from '../../../../shared/clipboard-image'
+import { getRichMarkdownImageResolverContextVersion } from './rich-markdown-image-context'
+import {
+ captureRichMarkdownImageImportDestination,
+ encodeMarkdownImageBasename,
+ type RichMarkdownImageImportDestination
+} from './rich-markdown-image-insert'
+
+type BlobImagePasteArgs = {
+ editor: Editor | null
+ slice: Slice | undefined
+ filePath: string
+ worktreeId: string | null
+ runtimeEnvironmentId?: string | null
+}
+
+/** Only object URLs minted by this renderer can be read back; foreign ones are left as pasted. */
+function collectOwnBlobImageSources(slice: Slice): string[] {
+ const prefix = `blob:${window.location.origin}/`
+ const sources = new Set<string>()
+ slice.content.descendants((node) => {
+ const src: unknown = node.attrs.src
+ if (node.type.name === 'image' && typeof src === 'string' && src.startsWith(prefix)) {
+ sources.add(src)
+ }
+ })
+ return [...sources]
+}
+
+/**
+ * Lets the paste insert natively, then saves images that point at this renderer's blob: URLs
+ * beside the note and swaps their src, since object URLs die with their source or on restart.
+ * Any image that fails keeps its pasted src; the pasted content itself is never withheld.
+ */
+export function scheduleRichMarkdownBlobImageImport({
+ editor,
+ slice,
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId
+}: BlobImagePasteArgs): void {
+ if (!editor || !slice || editor.isDestroyed) {
+ return
+ }
+ const sources = collectOwnBlobImageSources(slice)
+ if (sources.length === 0) {
+ return
+ }
+ const view = editor.view
+ const contextVersion = getRichMarkdownImageResolverContextVersion(editor)
+ const controller = new AbortController()
+ const abort = (): void => controller.abort()
+ editor.on('destroy', abort)
+ const isTargetCurrent = (): boolean =>
+ !controller.signal.aborted &&
+ !editor.isDestroyed &&
+ editor.isEditable &&
+ editor.view === view &&
+ getRichMarkdownImageResolverContextVersion(editor) === contextVersion
+ // Why: fetch now, before the source surface can revoke its object URLs.
+ const reads = sources.map((src) => fetch(src, { signal: controller.signal }))
+ reads.forEach((read) => read.catch(() => {}))
+
+ void (async () => {
+ let failed = false
+ let destination: RichMarkdownImageImportDestination | null | undefined
+ for (const [index, src] of sources.entries()) {
+ try {
+ const response = await reads[index]
+ if (!response.ok) {
+ throw new Error(`Failed to read ${src}`)
+ }
+ const png = await convertImageBlobToPng(await response.blob())
+ const contentBase64 = await blobToBase64(png)
+ assertClipboardImageBase64LengthWithinLimit(contentBase64.length)
+ // Skip the write when the paste was transformed (e.g. to plain text) or the image was removed.
+ if (!isTargetCurrent() || findImagePositions(editor, src).length === 0) {
+ continue
+ }
+ destination ??= captureRichMarkdownImageImportDestination({
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId
+ })
+ if (!destination) {
+ return
+ }
+ // Why: the temp file is local, as for OS clipboard images; the import uploads it to SSH/runtime owners.
+ const tempPath = await callRuntimeRpc<string>(
+ { kind: 'local' },
+ 'clipboard.saveImageAsTempFile',
+ { contentBase64 },
+ { signal: controller.signal }
+ )
+ const { results } = await importExternalPathsToRuntime(
+ destination.context,
+ [tempPath],
+ destination.destinationDir,
+ { access: destination.access }
+ )
+ const imported = results.find((result) => result.status === 'imported')
+ if (!imported) {
+ throw new Error(`Failed to import ${src}`)
+ }
+ if (isTargetCurrent()) {
+ replaceImageSource(editor, src, encodeMarkdownImageBasename(imported.destPath))
+ }
+ } catch {
+ if (controller.signal.aborted) {
+ return
+ }
+ failed = true
+ }
+ }
+ if (failed && isTargetCurrent()) {
+ toast.error(
+ translate(
+ 'auto.components.editor.richMarkdownBlobImagePaste.saveFailed',
+ "Couldn't save a pasted image beside the document. It may not load after Orca restarts."
+ )
+ )
+ }
+ })()
+ .catch(() => {})
+ .finally(() => editor.off('destroy', abort))
+}
+
+function findImagePositions(editor: Editor, src: string): number[] {
+ const positions: number[] = []
+ editor.state.doc.descendants((node, pos) => {
+ if (node.type.name === 'image' && node.attrs.src === src) {
+ positions.push(pos)
+ }
+ })
+ return positions
+}
+
+function replaceImageSource(editor: Editor, src: string, nextSrc: string): void {
+ const positions = findImagePositions(editor, src)
+ if (positions.length === 0) {
+ return
+ }
+ const tr = editor.state.tr
+ for (const pos of positions) {
+ tr.setNodeAttribute(pos, 'src', nextSrc)
+ }
+ // Why: undoing the paste should remove it in one step, not first restore the dead blob: src.
+ editor.view.dispatch(tr.setMeta('addToHistory', false))
+}
diff --git a/src/renderer/src/components/editor/rich-markdown-image-insert.ts b/src/renderer/src/components/editor/rich-markdown-image-insert.ts
index 7057511b7a5..e896295f173 100644
--- a/src/renderer/src/components/editor/rich-markdown-image-insert.ts
+++ b/src/renderer/src/components/editor/rich-markdown-image-insert.ts
@@ -16,6 +16,8 @@ import { showRichMarkdownImageInsertionCanceled } from './rich-markdown-image-in
import type { RichMarkdownImageInsertionRange } from './rich-markdown-image-insertion-target'
import { richMarkdownClipboardInsertionOrderKey } from './rich-markdown-clipboard-insertion-order'
import { selectFolderWorkspaces, selectWorktreesByRepo } from '@/store/workspace-record-selectors'
+import type { RuntimeFileOperationArgs } from '@/runtime/runtime-file-client-types'
+import type { LocalFileAccess } from '../../../../shared/local-file-access'
export type RichMarkdownImageInsertArgs = {
editor: Editor
@@ -26,6 +28,70 @@ export type RichMarkdownImageInsertArgs = {
getInsertionRange: () => RichMarkdownImageInsertionRange | null
}
+export type RichMarkdownImageImportDestination = {
+ context: RuntimeFileOperationArgs
+ destinationDir: string
+ access: LocalFileAccess | undefined
+}
+
+/** Resolves where image bytes for this note are saved; null after reporting a missing worktree path. */
+export function captureRichMarkdownImageImportDestination({
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId
+}: Pick<
+ RichMarkdownImageInsertArgs,
+ 'filePath' | 'worktreeId' | 'runtimeEnvironmentId'
+>): RichMarkdownImageImportDestination | null {
+ const state = useAppStore.getState()
+ const worktreePath = getWorktreePath(worktreeId)
+ const parsedWorkspace = worktreeId ? parseWorkspaceKey(worktreeId) : null
+ const resolvedConnectionId = getConnectionId(worktreeId)
+ if (parsedWorkspace?.type === 'folder' && resolvedConnectionId === undefined) {
+ throw new Error(
+ translate(
+ 'auto.components.editor.richMarkdownImageInsert.hostUnverified',
+ "Couldn't verify which host owns this file. Reopen the file and try again."
+ )
+ )
+ }
+ const connectionId = resolvedConnectionId ?? undefined
+ const fileContext =
+ worktreeId && parsedWorkspace?.type !== 'folder'
+ ? getEditorFileOperationContext(state, { worktreeId, runtimeEnvironmentId }, worktreePath)
+ : {
+ target: requireRuntimeTargetForFileOwner(state, worktreeId, runtimeEnvironmentId),
+ worktreeId,
+ worktreePath,
+ connectionId,
+ expectedExecutionHostId: connectionId
+ ? (`ssh:${encodeURIComponent(connectionId)}` as const)
+ : ('local' as const),
+ ...(connectionId
+ ? captureDirectSshMutationExpectation(state, connectionId, runtimeEnvironmentId)
+ : {})
+ }
+ if (fileContext.target.kind === 'environment' && !worktreePath) {
+ toast.error(
+ translate(
+ 'auto.components.editor.useLocalImagePick.91d835dc88',
+ 'Worktree path not available.'
+ )
+ )
+ return null
+ }
+
+ // Save image bytes beside the note, including outside projects, so rich-mode size checks measure document text only.
+ const openDocument = state.openFiles.find(
+ (file) => file.filePath === filePath && file.worktreeId === worktreeId
+ )
+ return {
+ context: fileContext,
+ destinationDir: dirname(filePath),
+ access: openDocument ? editorTabDocumentFolderAccess(state, openDocument) : undefined
+ }
+}
+
export async function insertRichMarkdownImageFromPath({
editor,
filePath,
@@ -35,53 +101,19 @@ export async function insertRichMarkdownImageFromPath({
getInsertionRange
}: RichMarkdownImageInsertArgs): Promise<void> {
try {
- const state = useAppStore.getState()
- const worktreePath = getWorktreePath(worktreeId)
- const parsedWorkspace = worktreeId ? parseWorkspaceKey(worktreeId) : null
- const resolvedConnectionId = getConnectionId(worktreeId)
- if (parsedWorkspace?.type === 'folder' && resolvedConnectionId === undefined) {
- throw new Error(
- translate(
- 'auto.components.editor.richMarkdownImageInsert.hostUnverified',
- "Couldn't verify which host owns this file. Reopen the file and try again."
- )
- )
- }
- const connectionId = resolvedConnectionId ?? undefined
- const fileContext =
- worktreeId && parsedWorkspace?.type !== 'folder'
- ? getEditorFileOperationContext(state, { worktreeId, runtimeEnvironmentId }, worktreePath)
- : {
- target: requireRuntimeTargetForFileOwner(state, worktreeId, runtimeEnvironmentId),
- worktreeId,
- worktreePath,
- connectionId,
- expectedExecutionHostId: connectionId
- ? (`ssh:${encodeURIComponent(connectionId)}` as const)
- : ('local' as const),
- ...(connectionId
- ? captureDirectSshMutationExpectation(state, connectionId, runtimeEnvironmentId)
- : {})
- }
- if (fileContext.target.kind === 'environment' && !worktreePath) {
- toast.error(
- translate(
- 'auto.components.editor.useLocalImagePick.91d835dc88',
- 'Worktree path not available.'
- )
- )
+ const destination = captureRichMarkdownImageImportDestination({
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId
+ })
+ if (!destination) {
return
}
-
- // Save image bytes beside the note, including outside projects, so rich-mode size checks measure document text only.
- const openDocument = state.openFiles.find(
- (file) => file.filePath === filePath && file.worktreeId === worktreeId
- )
const { results } = await importExternalPathsToRuntime(
- fileContext,
+ destination.context,
[sourcePath],
- dirname(filePath),
- { access: openDocument ? editorTabDocumentFolderAccess(state, openDocument) : undefined }
+ destination.destinationDir,
+ { access: destination.access }
)
const imported = results.find((result) => result.status === 'imported')
if (!imported) {
@@ -135,7 +167,7 @@ export async function insertRichMarkdownImageFromPath({
}
}
-function encodeMarkdownImageBasename(destPath: string): string {
+export function encodeMarkdownImageBasename(destPath: string): string {
// Escape filename spaces and delimiters or Markdown interprets them as broken destinations or literal text.
return encodeURIComponent(basename(destPath))
}
diff --git a/src/renderer/src/components/editor/rich-markdown-paste-handler.ts b/src/renderer/src/components/editor/rich-markdown-paste-handler.ts
index a7df0247a54..cd88d310842 100644
--- a/src/renderer/src/components/editor/rich-markdown-paste-handler.ts
+++ b/src/renderer/src/components/editor/rich-markdown-paste-handler.ts
@@ -2,6 +2,7 @@ import type { Editor } from '@tiptap/react'
import type { Slice } from '@tiptap/pm/model'
import type { EditorView } from '@tiptap/pm/view'
import { handleRichMarkdownImagePaste } from './rich-markdown-paste-image'
+import { scheduleRichMarkdownBlobImageImport } from './rich-markdown-blob-image-paste'
import { handleRichMarkdownLargeTextPaste } from './rich-markdown-large-text-paste'
import { handleRichMarkdownTerminalPathPaste } from './rich-markdown-terminal-path-paste'
import { inspectRichMarkdownSourceOwningSlice } from './rich-markdown-source-owning-slice'
@@ -37,6 +38,13 @@ export function handleRichMarkdownPaste({
) {
return true
}
+ scheduleRichMarkdownBlobImageImport({
+ editor,
+ slice,
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId
+ })
const sourceOwningStatus = slice ? inspectRichMarkdownSourceOwningSlice(slice) : null
if (sourceOwningStatus?.containsSourceOwningNode && slice && view) {
diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json
index b394b956455..b16badcaa4c 100644
--- a/src/renderer/src/i18n/locales/en.json
+++ b/src/renderer/src/i18n/locales/en.json
@@ -16870,6 +16870,9 @@
"tooLarge": "{{value0}} is too large to open in {{value1}} ({{value2}}; the limit is {{value3}}).",
"unsupported": "{{value0}} can't read this file from where it is stored. Update the Orca server that hosts it, or switch to Source.",
"readFailed": "{{value0}} couldn't be read for {{value1}}. {{value2}}"
+ },
+ "richMarkdownBlobImagePaste": {
+ "saveFailed": "Couldn't save a pasted image beside the document. It may not load after Orca restarts."
}
},
"diff": {