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): keep slash and document triggers literal inside code (#34971)

OrcaWin17e5d72a35d8
diff --git a/src/renderer/src/components/editor/rich-markdown-commands.tsx b/src/renderer/src/components/editor/rich-markdown-commands.tsx
index c01b5330099..685c7c3be7b 100644
--- a/src/renderer/src/components/editor/rich-markdown-commands.tsx
+++ b/src/renderer/src/components/editor/rich-markdown-commands.tsx
@@ -72,8 +72,7 @@ export function syncDocLinkMenu(
setDocLinkMenu(null)
return
}
- const codeMarkType = state.schema.marks.code
- if (codeMarkType && state.doc.rangeHasMark($from.pos, $from.pos, codeMarkType)) {
+ if (editor.isActive('code')) {
setDocLinkMenu(null)
return
}
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 090a9fdae25..7578b138183 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
@@ -3,6 +3,7 @@
import type { Dispatch, MutableRefObject, SetStateAction } from 'react'
import { Editor } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
+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'
@@ -11,7 +12,7 @@ import {
createRichMarkdownEditorConfig,
type EditorConfigParams
} from './rich-markdown-editor-config'
-import type { SlashMenuState } from './rich-markdown-slash-commands'
+import { slashCommands, type SlashMenuState } from './rich-markdown-slash-commands'
import { createRichMarkdownEditorCodec } from './rich-markdown-source-transport'
import { createRichMarkdownHtmlSuperscriptLinkContext } from './rich-markdown-html-superscript-link-context'
@@ -198,3 +199,110 @@ describe('rich Markdown DOM click routing', () => {
expect(event.defaultPrevented).toBe(false)
})
})
+
+describe('rich Markdown slash commands in code', () => {
+ const editors: Editor[] = []
+
+ afterEach(() => {
+ editors.splice(0).forEach((editor) => editor.destroy())
+ document.body.replaceChildren()
+ vi.useRealTimers()
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+ })
+
+ function slashEditor(content: string) {
+ vi.useFakeTimers()
+ vi.stubGlobal('api', undefined)
+ Object.defineProperty(window, 'api', {
+ configurable: true,
+ value: { ui: { setMarkdownEditorFocused: vi.fn() } }
+ })
+ const root = document.createElement('div')
+ document.body.append(root)
+ const params = createConfigParams({ content, rootRef: ref(root) })
+ params.filteredSlashCommandsRef.current = slashCommands
+ params.setDocLinkMenu = (next) => {
+ params.docLinkMenuRef.current =
+ typeof next === 'function' ? next(params.docLinkMenuRef.current) : next
+ }
+ params.setSlashMenu = (next) => {
+ params.slashMenuRef.current =
+ typeof next === 'function' ? next(params.slashMenuRef.current) : next
+ }
+ const editor = new Editor({
+ ...createRichMarkdownEditorConfig(params),
+ extensions: createRichMarkdownExtensions({ codec: params.codec })
+ })
+ params.editorRef.current = editor
+ root.append(editor.view.dom)
+ vi.spyOn(editor.view, 'coordsAtPos').mockReturnValue({ top: 0, bottom: 20, left: 0, right: 0 })
+ editors.push(editor)
+ return { editor, params }
+ }
+
+ function enter(editor: Editor) {
+ const event = new KeyboardEvent('keydown', {
+ key: 'Enter',
+ bubbles: true,
+ cancelable: true
+ })
+ editor.view.dom.dispatchEvent(event)
+ }
+
+ it('keeps a literal slash and Enter inside a fenced code block', () => {
+ const { editor, params } = slashEditor('```sh\n\n```')
+ editor.commands.setTextSelection(1)
+ editor.view.dispatch(editor.state.tr.insertText('/'))
+
+ const menuWasOpen = params.slashMenuRef.current !== null
+ enter(editor)
+
+ expect(editor.state.doc.firstChild?.type.name).toBe('codeBlock')
+ expect(editor.state.doc.firstChild?.textContent).toBe('/\n')
+ expect(menuWasOpen).toBe(false)
+ })
+
+ it('does not offer block conversion for a slash in inline code', () => {
+ const { editor, params } = slashEditor('')
+ editor.commands.setTextSelection(1)
+ editor.commands.toggleCode()
+ editor.view.dispatch(editor.state.tr.insertText('/'))
+
+ const menuWasOpen = params.slashMenuRef.current !== null
+ enter(editor)
+
+ expect(editor.state.doc.firstChild?.type.name).toBe('paragraph')
+ expect(editor.state.doc.firstChild?.textContent).toBe('/')
+ expect(editor.state.doc.firstChild?.firstChild?.marks.map((mark) => mark.type.name)).toEqual([
+ 'code'
+ ])
+ expect(menuWasOpen).toBe(false)
+ })
+
+ it('keeps an inline-code document-link trigger literal', () => {
+ const { editor, params } = slashEditor('')
+ editor.commands.setTextSelection(1)
+ editor.commands.toggleCode()
+ editor.view.dispatch(editor.state.tr.insertText('[['))
+
+ expect(editor.state.doc.firstChild?.textContent).toBe('[[')
+ expect(editor.state.doc.firstChild?.firstChild?.marks.map((mark) => mark.type.name)).toEqual([
+ 'code'
+ ])
+ expect(params.docLinkMenuRef.current).toBeNull()
+ })
+
+ it('still offers and commits a block command from ordinary prose', () => {
+ const { editor, params } = slashEditor('')
+ editor.commands.setTextSelection(1)
+ editor.view.dispatch(editor.state.tr.insertText('/'))
+
+ expect(params.slashMenuRef.current?.query).toBe('')
+ enter(editor)
+
+ expect(editor.state.doc.firstChild?.type.name).toBe('heading')
+ expect(editor.state.doc.firstChild?.attrs.level).toBe(1)
+ expect(editor.state.doc.firstChild?.textContent).toBe('')
+ })
+})
diff --git a/src/renderer/src/components/editor/rich-markdown-slash-commands.tsx b/src/renderer/src/components/editor/rich-markdown-slash-commands.tsx
index 602d8a46009..b3af7d8152e 100644
--- a/src/renderer/src/components/editor/rich-markdown-slash-commands.tsx
+++ b/src/renderer/src/components/editor/rich-markdown-slash-commands.tsx
@@ -49,7 +49,7 @@ export function syncSlashMenu(
}
const { $from } = selection
- if (!$from.parent.isTextblock) {
+ if (!$from.parent.isTextblock || $from.parent.type.spec.code || editor.isActive('code')) {
setSlashMenu(null)
return
}