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): install one code highlighting plugin per editor (#35112)

OrcaWin24e7643517d8
diff --git a/src/renderer/src/components/editor/rich-markdown-lowlight-ownership.test.ts b/src/renderer/src/components/editor/rich-markdown-lowlight-ownership.test.ts
new file mode 100644
index 00000000000..bc21b7329b9
--- /dev/null
+++ b/src/renderer/src/components/editor/rich-markdown-lowlight-ownership.test.ts
@@ -0,0 +1,49 @@
+// @vitest-environment happy-dom
+import { Editor } from '@tiptap/core'
+import { DecorationSet } from '@tiptap/pm/view'
+import StarterKit from '@tiptap/starter-kit'
+import { common, createLowlight } from 'lowlight'
+import { afterEach, expect, it } from 'vitest'
+import { RichMarkdownCodeBlockLowlight } from './rich-markdown-lowlight'
+
+const editors: Editor[] = []
+afterEach(() => editors.splice(0).forEach((editor) => editor.destroy()))
+
+// Production extends the lowlight extension again to add its node view.
+function mountExtended(content: string) {
+ const editor = new Editor({
+ element: document.createElement('div'),
+ extensions: [
+ StarterKit.configure({ codeBlock: false, trailingNode: false }),
+ RichMarkdownCodeBlockLowlight.extend({}).configure({ lowlight: createLowlight(common) })
+ ],
+ content
+ })
+ editors.push(editor)
+ return editor
+}
+
+it('installs one highlighting plugin when the extension is extended', () => {
+ const editor = mountExtended(
+ '<pre><code class="language-javascript">const answer = 42;</code></pre>'
+ )
+ const highlightingStates = editor.state.plugins.flatMap((plugin) => {
+ const state: unknown = plugin.getState(editor.state)
+ return state instanceof DecorationSet ? [state] : []
+ })
+ expect(highlightingStates).toHaveLength(1)
+ expect(highlightingStates[0].find().length).toBeGreaterThan(0)
+})
+
+it('keeps VS Code paste when the extension is extended', () => {
+ const editor = mountExtended('<p>Before</p>')
+ editor.commands.selectAll()
+ const clipboardData = new DataTransfer()
+ clipboardData.setData('text/plain', 'const answer = 42;\r\nconsole.log(answer);')
+ clipboardData.setData('vscode-editor-data', JSON.stringify({ mode: 'javascript' }))
+ const event = new ClipboardEvent('paste', { clipboardData, bubbles: true, cancelable: true })
+ editor.view.dom.dispatchEvent(event)
+ expect(event.defaultPrevented).toBe(true)
+ expect(editor.state.doc.firstChild?.type.name).toBe('codeBlock')
+ expect(editor.state.doc.firstChild?.attrs.language).toBe('javascript')
+})
diff --git a/src/renderer/src/components/editor/rich-markdown-lowlight.ts b/src/renderer/src/components/editor/rich-markdown-lowlight.ts
index 4da137d9dca..ea4a0888939 100644
--- a/src/renderer/src/components/editor/rich-markdown-lowlight.ts
+++ b/src/renderer/src/components/editor/rich-markdown-lowlight.ts
@@ -1,4 +1,4 @@
-import { getChangedRanges } from '@tiptap/core'
+import { getChangedRanges, getExtensionField } from '@tiptap/core'
import { CodeBlock } from '@tiptap/extension-code-block'
import CodeBlockLowlight, {
type CodeBlockLowlightOptions
@@ -211,8 +211,12 @@ export const RichMarkdownCodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightO
return { ...CodeBlockLowlight.options }
},
addProseMirrorPlugins() {
+ // Why: Tiptap copies this method into child configs, so this.parent() would re-enter it and install a second plugin.
+ const basePlugins = getExtensionField<
+ NonNullable<typeof CodeBlock.config.addProseMirrorPlugins>
+ >(CodeBlock, 'addProseMirrorPlugins', this)
return [
- ...(this.parent?.() ?? []),
+ ...(basePlugins?.() ?? []),
createRichMarkdownLowlightPlugin({
name: this.name,
lowlight: this.options.lowlight as Lowlight,