diff --git a/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.test.ts b/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.test.ts new file mode 100644 index 00000000000..b9155234366 --- /dev/null +++ b/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.test.ts @@ -0,0 +1,79 @@ +// @vitest-environment happy-dom + +import { Editor } from '@tiptap/core' +import { afterEach, describe, expect, it } from 'vitest' +import { createRichMarkdownHtmlSuperscriptLinkContext } from '../citations/rich-markdown-html-superscript-link-context' +import { createRichMarkdownEditorCodec } from '../document/rich-markdown-source-transport' +import { encodeRawMarkdownHtmlForRichEditor } from '../raw-source/raw-markdown-html' +import { createRichMarkdownExtensions } from '../schema/rich-markdown-extensions' +import { handleRichMarkdownEditorClick } from './rich-markdown-editor-click-routing' + +const editors: Editor[] = [] + +afterEach(() => { + for (const editor of editors.splice(0)) { + editor.destroy() + } +}) + +function createDocLinkEditor(): Editor { + const codec = createRichMarkdownEditorCodec() + const editor = new Editor({ + element: document.body.appendChild(document.createElement('div')), + extensions: createRichMarkdownExtensions({ codec }), + content: encodeRawMarkdownHtmlForRichEditor('See [[docs/guide|some label]] next', codec), + contentType: 'markdown' + }) + editors.push(editor) + return editor +} + +function docLinkEnd(editor: Editor): number { + let end = -1 + editor.state.doc.descendants((node, pos) => { + if (node.type.name === 'markdownDocLink') { + end = pos + node.nodeSize + } + }) + return end +} + +// posAtCoords puts a click on the right half of an inline atom at the position after it. +function modClickAt(editor: Editor, pos: number, target: Element): string[] { + const opened: string[] = [] + const event = new MouseEvent('click', { metaKey: true }) + Object.defineProperty(event, 'target', { value: target }) + handleRichMarkdownEditorClick({ + activateMarkdownLink: () => undefined, + editorRef: { current: editor }, + event, + filePath: '/repo/docs/index.md', + isMac: true, + htmlSuperscriptLinkContext: createRichMarkdownHtmlSuperscriptLinkContext({ + sourceFilePath: '/repo/docs/index.md', + worktreeId: 'worktree-1', + worktreeRoot: '/repo', + sourceOwner: { kind: 'local' } + }), + markdownCommentsRef: { current: [] }, + markdownSourceLineOffsetRef: { current: 0 }, + onOpenDocLinkRef: { current: (docTarget) => opened.push(docTarget) }, + pos, + rootRef: { current: null }, + scrollRichMarkdownReviewNoteCardIntoView: () => undefined, + view: editor.view, + worktreeId: 'worktree-1', + worktreeRoot: '/repo' + }) + return opened +} + +describe('rich markdown modifier-click on a wikilink', () => { + it('opens the wikilink from the right half of its label', () => { + const editor = createDocLinkEditor() + const label = editor.view.dom.querySelector('[data-doc-link-target]') + expect(label).not.toBeNull() + + expect(modClickAt(editor, docLinkEnd(editor), label!)).toEqual(['docs/guide']) + }) +}) diff --git a/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.ts b/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.ts index bf4e6a7d2bc..41a5000de79 100644 --- a/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.ts +++ b/src/renderer/src/components/editor/rich-markdown/links/rich-markdown-editor-click-routing.ts @@ -28,6 +28,7 @@ import type { RichMarkdownHtmlSuperscriptLinkContext } from '../citations/rich-m import type { MutableRefObject } from 'react' import type { Editor } from '@tiptap/react' import type { EditorView } from '@tiptap/pm/view' +import type { Node as ProseMirrorNode } from '@tiptap/pm/model' import type { HttpLinkSourceOwner } from '@/lib/browser/navigation/http-link-routing' import type { DiffComment } from '../../../../../../shared/diff-comment-types' @@ -98,7 +99,7 @@ export function handleRichMarkdownEditorClick({ } return false } - const clickedNode = view.state.doc.nodeAt(pos) + const clickedNode = getClickedNode(view, pos, event) if (clickedNode?.type.name === 'image') { return activateMarkdownImageClick({ activateMarkdownLink, @@ -183,6 +184,19 @@ function activateMarkdownImageClick({ return true } +// posAtCoords resolves a click on an inline atom's right half to the position after it. +function getClickedNode(view: EditorView, pos: number, event: MouseEvent): ProseMirrorNode | null { + const before = view.state.doc.resolve(pos).nodeBefore + if ( + before?.isAtom && + event.target instanceof Node && + view.nodeDOM(pos - before.nodeSize)?.contains(event.target) + ) { + return before + } + return view.state.doc.nodeAt(pos) +} + function getClickedLinkHref(view: EditorView, event: MouseEvent): string { const target = event.target instanceof Element