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.

perf(editor): admit large pastes before clipboard parsing (#34839)

OrcaWin9baf51d2e5da
diff --git a/src/renderer/src/components/editor/rich-markdown-editor-config.test.ts b/src/renderer/src/components/editor/rich-markdown-editor-config.test.ts
index 7578b138183..f396fdfddf7 100644
--- a/src/renderer/src/components/editor/rich-markdown-editor-config.test.ts
+++ b/src/renderer/src/components/editor/rich-markdown-editor-config.test.ts
@@ -7,6 +7,7 @@ import { createRichMarkdownExtensions } from './rich-markdown-extensions'
import { handleRichMarkdownEditorClick } from './rich-markdown-editor-click-routing'
import type { DocLinkMenuState } from './rich-markdown-commands'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { DOMParser as DocumentParser } from '@tiptap/pm/model'
import type { LinkBubbleState } from './RichMarkdownLinkBubble'
import {
createRichMarkdownEditorConfig,
@@ -136,6 +137,117 @@ describe('createRichMarkdownEditorConfig', () => {
})
})
+describe('rich Markdown DOM paste admission', () => {
+ const editors: Editor[] = []
+
+ afterEach(() => {
+ editors.splice(0).forEach((editor) => editor.destroy())
+ document.body.replaceChildren()
+ vi.useRealTimers()
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+ })
+
+ function pasteEditor() {
+ vi.useFakeTimers()
+ const saveClipboardImageAsTempFile = vi.fn().mockResolvedValue(null)
+ vi.stubGlobal('api', undefined)
+ Object.defineProperty(window, 'api', {
+ configurable: true,
+ value: { ui: { setMarkdownEditorFocused: vi.fn(), saveClipboardImageAsTempFile } }
+ })
+ const params = createConfigParams({ content: 'hello world\n\nother paragraph' })
+ const config = createRichMarkdownEditorConfig(params)
+ const editor = new Editor({
+ ...config,
+ extensions: createRichMarkdownExtensions({
+ codec: params.codec,
+ htmlSuperscriptLinks: true,
+ htmlSuperscriptLinkContext: params.htmlSuperscriptLinkContext
+ })
+ })
+ params.editorRef.current = editor
+ document.body.append(editor.view.dom)
+ editor.view.dom.focus()
+ editor.commands.setTextSelection({ from: 7, to: 12 })
+ editors.push(editor)
+ const parser = DocumentParser.fromSchema(editor.schema)
+ const parseSlice = vi.spyOn(parser, 'parseSlice')
+ editor.setOptions({ editorProps: { ...config.editorProps, clipboardParser: parser } })
+ return { editor, parseSlice, saveClipboardImageAsTempFile }
+ }
+
+ function paste(editor: Editor, text: string, html = ''): ClipboardEvent {
+ const clipboardData = new DataTransfer()
+ clipboardData.setData('text/plain', text)
+ if (html) {
+ clipboardData.setData('text/html', html)
+ }
+ const event = new ClipboardEvent('paste', { clipboardData, bubbles: true, cancelable: true })
+ editor.view.dom.dispatchEvent(event)
+ return event
+ }
+
+ it('chunks a large HTML paste without building a clipboard slice', async () => {
+ const { editor, parseSlice } = pasteEditor()
+ const text = 'large paste '.repeat(6_000)
+
+ expect(paste(editor, text, `<p>${text}</p>`).defaultPrevented).toBe(true)
+ await vi.runAllTimersAsync()
+
+ expect(editor.state.doc.firstChild?.textContent).toBe(`hello ${text}`)
+ expect(editor.state.doc.child(1).textContent).toBe('other paragraph')
+ expect(parseSlice).not.toHaveBeenCalled()
+ })
+
+ it('keeps source-rich clipboard content on the parsed preservation route', async () => {
+ const { editor } = pasteEditor()
+ const text = 'source '.repeat(10_000)
+ const source = '<sup><a href="https://example.com">[1]</a></sup>'
+ const document = editor.schema.node('doc', null, [
+ editor.schema.node('paragraph', null, [
+ editor.schema.text(text),
+ editor.schema.node('richMarkdownHtmlSuperscriptLink', {
+ source,
+ href: 'https://example.com',
+ label: '[1]',
+ title: null
+ })
+ ])
+ ])
+ const html = editor.view.serializeForClipboard(document.slice(1, document.content.size - 1)).dom
+ .innerHTML
+ paste(editor, `${text}[1]`, html)
+ await vi.runAllTimersAsync()
+
+ expect(editor.getMarkdown()).toContain(source)
+ expect(editor.state.doc.firstChild?.textContent).toBe(`hello ${text}`)
+ let citations = 0
+ editor.state.doc.descendants((node) => {
+ if (node.type.name === 'richMarkdownHtmlSuperscriptLink') {
+ citations += 1
+ }
+ })
+ expect(citations).toBe(1)
+ })
+
+ it('keeps image import ahead of a large text fallback', async () => {
+ const { editor, parseSlice, saveClipboardImageAsTempFile } = pasteEditor()
+ const before = editor.state.doc
+ const clipboardData = new DataTransfer()
+ clipboardData.items.add(new File(['image'], 'image.png', { type: 'image/png' }))
+ clipboardData.setData('text/plain', 'fallback '.repeat(10_000))
+ const event = new ClipboardEvent('paste', { clipboardData, bubbles: true, cancelable: true })
+ editor.view.dom.dispatchEvent(event)
+ await vi.runAllTimersAsync()
+
+ expect(event.defaultPrevented).toBe(true)
+ expect(saveClipboardImageAsTempFile.mock.calls.length).toBe(1)
+ expect(editor.state.doc.eq(before)).toBe(true)
+ expect(parseSlice).not.toHaveBeenCalled()
+ })
+})
+
describe('rich Markdown DOM click routing', () => {
let editor: Editor
diff --git a/src/renderer/src/components/editor/rich-markdown-editor-config.ts b/src/renderer/src/components/editor/rich-markdown-editor-config.ts
index a2b418c05f8..28d0c0e5f64 100644
--- a/src/renderer/src/components/editor/rich-markdown-editor-config.ts
+++ b/src/renderer/src/components/editor/rich-markdown-editor-config.ts
@@ -2,6 +2,7 @@ import type { Editor, UseEditorOptions } from '@tiptap/react'
import type { EditorView } from '@tiptap/pm/view'
import { handleRichMarkdownCut } from './rich-markdown-cut-handler'
import { handleRichMarkdownPaste } from './rich-markdown-paste-handler'
+import { handleRichMarkdownPasteAdmission } from './rich-markdown-paste-admission'
import { encodeRawMarkdownHtmlForRichEditor } from './raw-markdown-html'
import { normalizeEmptyListItems } from './rich-markdown-normalize'
import { autoFocusRichEditor } from './rich-markdown-auto-focus'
@@ -165,6 +166,15 @@ export function createRichMarkdownEditorConfig(params: EditorConfigParams): UseE
},
handleDOMEvents: {
cut: handleRichMarkdownCut,
+ paste: (view, event) =>
+ handleRichMarkdownPasteAdmission({
+ editor: editorRef.current,
+ event,
+ filePath,
+ worktreeId,
+ runtimeEnvironmentId,
+ view
+ }),
// ProseMirror skips handleClick for Shift gestures to extend the selection.
click: (view, event) => {
if (
diff --git a/src/renderer/src/components/editor/rich-markdown-paste-admission.ts b/src/renderer/src/components/editor/rich-markdown-paste-admission.ts
new file mode 100644
index 00000000000..e31063f3afb
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-paste-admission.ts
@@ -0,0 +1,25 @@
+import type { EditorView } from '@tiptap/pm/view'
+import {
+ handleRichMarkdownImagePaste,
+ type RichMarkdownImagePasteArgs
+} from './rich-markdown-paste-image'
+import { handleRichMarkdownLargeTextPaste } from './rich-markdown-large-text-paste'
+
+export function handleRichMarkdownPasteAdmission({
+ view,
+ ...args
+}: RichMarkdownImagePasteArgs & { view: EditorView }): boolean {
+ const { editor, event } = args
+ if (event.defaultPrevented || !editor || !editor.isEditable || !view.editable || view.composing) {
+ return false
+ }
+ if (handleRichMarkdownImagePaste(args)) {
+ return true
+ }
+ const html = event.clipboardData?.getData('text/html') ?? ''
+ // Citation source preservation needs the existing parsed-slice validation and aggregate limits.
+ if (/data-rich-markdown-html-superscript-link/i.test(html)) {
+ return false
+ }
+ return handleRichMarkdownLargeTextPaste(editor, event)
+}