mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 11:24:37 +00:00
fix(editor): open a wikilink from the right half of its label
ProseMirror resolves a click on the right half of an inline atom to the position after it, so the rich editor's modifier-click router read the following node. Wikilinks and superscript citation links did nothing when clicked there. Resolve the atom the click landed on instead. Co-authored-by: Wooseong Kim <innocarpe@gmail.com>
This commit is contained in:
co-authored by
Wooseong Kim
parent
9f32c6d06d
commit
321cc12f1e
+79
@@ -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'])
|
||||
})
|
||||
})
|
||||
+15
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user