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): reveal collapsed sections before navigating to their content (#35075)

OrcaWinab9d846d7936
diff --git a/src/renderer/src/components/editor/markdown-anchor-scroll.ts b/src/renderer/src/components/editor/markdown-anchor-scroll.ts
index bb6be5cb9dc..f849a5b9390 100644
--- a/src/renderer/src/components/editor/markdown-anchor-scroll.ts
+++ b/src/renderer/src/components/editor/markdown-anchor-scroll.ts
@@ -1,3 +1,4 @@
+import { revealMarkdownNavigationDetails } from './markdown-navigation-details'
import { MarkdownHeadingSlugger } from './markdown-heading-slug'
import { motionSafeScrollBehavior } from '@/hooks/usePrefersReducedMotion'
@@ -12,10 +13,11 @@ export function scrollToAnchorInEditor(root: HTMLElement | null, anchor: string)
} catch {
// Malformed %-escapes: fall back to the raw fragment.
}
- const headings = root.querySelectorAll('h1, h2, h3, h4, h5, h6')
+ const headings = root.querySelectorAll<HTMLElement>('h1, h2, h3, h4, h5, h6')
const slugger = new MarkdownHeadingSlugger()
for (const heading of headings) {
if (slugger.slug(heading.textContent ?? '') === decoded) {
+ revealMarkdownNavigationDetails(heading)
heading.scrollIntoView({ behavior: motionSafeScrollBehavior(), block: 'start' })
return
}
diff --git a/src/renderer/src/components/editor/markdown-navigation-details.test.ts b/src/renderer/src/components/editor/markdown-navigation-details.test.ts
new file mode 100644
index 00000000000..a8762e1d2f6
--- /dev/null
+++ b/src/renderer/src/components/editor/markdown-navigation-details.test.ts
@@ -0,0 +1,146 @@
+// @vitest-environment happy-dom
+import { Editor, type JSONContent } from '@tiptap/core'
+import StarterKit from '@tiptap/starter-kit'
+import { afterEach, expect, it } from 'vitest'
+import { createOrcaDetailsExtensions } from './rich-markdown-details-extension'
+import { revealMarkdownNavigationDetails } from './markdown-navigation-details'
+
+const editors: Editor[] = []
+
+afterEach(() => {
+ editors.splice(0).forEach((editor) => editor.destroy())
+ document.body.innerHTML = ''
+})
+
+function detailsBlock(title: string, content: JSONContent[], variant = 'heading-2'): JSONContent {
+ return {
+ type: 'details',
+ attrs: { open: false, variant },
+ content: [
+ { type: 'detailsSummary', content: [{ type: 'text', text: title }] },
+ { type: 'detailsContent', content }
+ ]
+ }
+}
+
+function mountDetails(): Editor {
+ const element = document.createElement('div')
+ document.body.append(element)
+ const editor = new Editor({
+ element,
+ extensions: [StarterKit, ...createOrcaDetailsExtensions()],
+ content: {
+ type: 'doc',
+ content: [
+ { type: 'paragraph', content: [{ type: 'text', text: 'Keep selection' }] },
+ detailsBlock('Outer', [
+ detailsBlock(
+ 'Inner',
+ [{ type: 'heading', attrs: { level: 2 }, content: [{ type: 'text', text: 'Target' }] }],
+ 'heading-3'
+ )
+ ]),
+ detailsBlock('Sibling', [{ type: 'paragraph' }])
+ ]
+ }
+ })
+ editors.push(editor)
+ return editor
+}
+
+it('opens only native details ancestors that conceal the destination', () => {
+ document.body.innerHTML = `
+ <details id="outer"><summary>Outer</summary>
+ <details id="inner"><summary>Inner</summary><h2>Target</h2></details>
+ </details>
+ <details id="sibling"><summary>Sibling</summary></details>
+ `
+ revealMarkdownNavigationDetails(document.querySelector('h2')!)
+ expect([...document.querySelectorAll('details')].map((details) => details.open)).toEqual([
+ true,
+ true,
+ false
+ ])
+ document.querySelector('#inner')?.removeAttribute('open')
+ document.querySelector('#outer')?.removeAttribute('open')
+ revealMarkdownNavigationDetails(document.querySelector<HTMLElement>('#inner summary')!)
+ expect([...document.querySelectorAll('details')].map((details) => details.open)).toEqual([
+ true,
+ false,
+ false
+ ])
+})
+
+it('opens rich ancestors in one undoable transaction while retaining selection and variants', () => {
+ const editor = mountDetails()
+ editor.commands.setTextSelection(2)
+ const selection = editor.state.selection
+ const text = editor.state.doc.textContent
+ let transactions = 0
+ editor.on('transaction', () => {
+ transactions += 1
+ })
+ const heading = editor.view.dom.querySelector<HTMLElement>('h2')!
+
+ revealMarkdownNavigationDetails(heading)
+
+ expect(transactions).toBe(1)
+ expect(editor.state.selection.eq(selection)).toBe(true)
+ expect(editor.state.doc.textContent).toBe(text)
+ expect(editor.state.doc.child(1).attrs).toMatchObject({ open: true, variant: 'heading-2' })
+ expect(editor.state.doc.child(1).child(1).firstChild?.attrs).toMatchObject({
+ open: true,
+ variant: 'heading-3'
+ })
+ expect(editor.state.doc.child(2).attrs.open).toBe(false)
+ expect(editor.view.dom.querySelector('[data-type="detailsContent"][hidden] h2')).toBeNull()
+ revealMarkdownNavigationDetails(heading)
+ expect(transactions).toBe(1)
+ expect(editor.commands.undo()).toBe(true)
+ expect(editor.state.doc.child(1).attrs.open).toBe(false)
+ expect(editor.state.doc.child(1).child(1).firstChild?.attrs.open).toBe(false)
+})
+
+it('opens the outer rich fold without expanding a destination in its own summary', () => {
+ const editor = mountDetails()
+ revealMarkdownNavigationDetails(editor.view.dom.querySelectorAll<HTMLElement>('summary')[1])
+ expect(editor.state.doc.child(1).attrs.open).toBe(true)
+ expect(editor.state.doc.child(1).child(1).firstChild?.attrs.open).toBe(false)
+ expect(editor.state.doc.child(2).attrs.open).toBe(false)
+})
+
+it.each([false, true])(
+ 'reveals readonly rich content without changing its document (open: %s)',
+ (open) => {
+ const editor = mountDetails()
+ if (open) {
+ editor.view.dispatch(
+ editor.state.tr.setNodeMarkup(editor.state.doc.firstChild!.nodeSize, undefined, {
+ open: true,
+ variant: 'heading-2'
+ })
+ )
+ }
+ editor.setEditable(false)
+ if (open) {
+ editor.view.dom.querySelector<HTMLButtonElement>('[data-type="details"] > button')?.click()
+ }
+ const doc = editor.state.doc
+ let transactions = 0
+ editor.on('transaction', () => {
+ transactions += 1
+ })
+ const heading = editor.view.dom.querySelector<HTMLElement>('h2')!
+
+ revealMarkdownNavigationDetails(heading)
+
+ expect(editor.view.dom.querySelector('[data-type="detailsContent"][hidden] h2')).toBeNull()
+ expect(editor.state.doc).toBe(doc)
+ expect(editor.isFocused).toBe(false)
+ expect(transactions).toBe(0)
+ expect(editor.view.dom.querySelectorAll('[data-type="detailsContent"][hidden]')).toHaveLength(1)
+ revealMarkdownNavigationDetails(heading)
+ expect(editor.view.dom.querySelector('[data-type="detailsContent"][hidden] h2')).toBeNull()
+ expect(transactions).toBe(0)
+ }
+)
diff --git a/src/renderer/src/components/editor/markdown-navigation-details.ts b/src/renderer/src/components/editor/markdown-navigation-details.ts
new file mode 100644
index 00000000000..35c7f595b3d
--- /dev/null
+++ b/src/renderer/src/components/editor/markdown-navigation-details.ts
@@ -0,0 +1,40 @@
+import type { TiptapEditorHTMLElement } from '@tiptap/core'
+
+export function revealMarkdownNavigationDetails(target: HTMLElement): void {
+ for (let ancestor = target.parentElement; ancestor; ancestor = ancestor.parentElement) {
+ if (ancestor instanceof HTMLDetailsElement) {
+ const summary = ancestor.querySelector(':scope > summary')
+ if (!summary?.contains(target)) {
+ ancestor.open = true
+ }
+ }
+ }
+
+ const element = target.closest<TiptapEditorHTMLElement>('.tiptap')
+ const editor = element?.editor
+ if (!editor || editor.isDestroyed || editor.view.dom !== element) {
+ return
+ }
+ const position = editor.state.doc.resolve(editor.view.posAtDOM(target, 0))
+ const transaction = editor.state.tr
+ for (let depth = 1; depth <= position.depth; depth += 1) {
+ const node = position.node(depth)
+ if (node.type.name !== 'details' || position.index(depth) === 0) {
+ continue
+ }
+ if (!editor.isEditable) {
+ const nodeElement = editor.view.nodeDOM(position.before(depth))
+ if (
+ nodeElement instanceof HTMLElement &&
+ nodeElement.querySelector(':scope > div > [data-type="detailsContent"][hidden]')
+ ) {
+ nodeElement.querySelector<HTMLButtonElement>(':scope > button')?.click()
+ }
+ } else if (!node.attrs.open) {
+ transaction.setNodeMarkup(position.before(depth), undefined, { ...node.attrs, open: true })
+ }
+ }
+ if (transaction.docChanged) {
+ editor.view.dispatch(transaction)
+ }
+}
diff --git a/src/renderer/src/components/editor/use-markdown-preview-navigation.test.tsx b/src/renderer/src/components/editor/use-markdown-preview-navigation.test.tsx
index 7468959a838..115e54b9c52 100644
--- a/src/renderer/src/components/editor/use-markdown-preview-navigation.test.tsx
+++ b/src/renderer/src/components/editor/use-markdown-preview-navigation.test.tsx
@@ -25,15 +25,18 @@ it.each(['anchor', 'source'] as const)(
heading.id = 'destination'
heading.dataset.sourceLine = '1'
heading.dataset.sourceEndLine = '1'
- block.append(heading)
+ const details = document.createElement('details')
+ details.innerHTML = '<summary>More</summary>'
+ details.append(heading)
+ block.append(details)
root.append(block)
Object.defineProperties(root, {
scrollHeight: { value: 1000 },
clientHeight: { value: 100 }
})
vi.spyOn(root, 'getBoundingClientRect').mockReturnValue(new DOMRect(0, 0, 100, 100))
- vi.spyOn(heading, 'getBoundingClientRect').mockImplementation(
- () => new DOMRect(0, 400 - root.scrollTop, 100, 40)
+ vi.spyOn(heading, 'getBoundingClientRect').mockImplementation(() =>
+ details.open ? new DOMRect(0, 400 - root.scrollTop, 100, 40) : new DOMRect()
)
const scroll = vi
.spyOn(root, 'scrollTo')
@@ -91,6 +94,7 @@ it.each(['anchor', 'source'] as const)(
expect(scroll).not.toHaveBeenCalled()
virtualizer.isScrolling = false
rerender({ anchor, viewportReady: true })
+ expect(details.open).toBe(true)
expect(scroll).toHaveBeenCalledExactlyOnceWith({ top: kind === 'anchor' ? 388 : 370 })
expect(record).not.toHaveBeenCalled()
virtualizer.isScrolling = true
diff --git a/src/renderer/src/components/editor/use-markdown-preview-navigation.ts b/src/renderer/src/components/editor/use-markdown-preview-navigation.ts
index 754dbd3d9d4..c9384615b96 100644
--- a/src/renderer/src/components/editor/use-markdown-preview-navigation.ts
+++ b/src/renderer/src/components/editor/use-markdown-preview-navigation.ts
@@ -1,3 +1,4 @@
+import { revealMarkdownNavigationDetails } from './markdown-navigation-details'
import { useEffect, useImperativeHandle, useMemo, useRef, type RefObject } from 'react'
import type { Virtualizer } from '@tanstack/react-virtual'
import type { ProgrammaticScrollMarks } from '@/hooks/programmatic-scroll-marks'
@@ -123,6 +124,7 @@ export function useMarkdownPreviewNavigation({
Number(node.dataset.sourceLine) <= anchor.line &&
Number(node.dataset.sourceEndLine) >= anchor.line
)) ?? block
+ revealMarkdownNavigationDetails(target)
const top = Math.min(
getMarkdownPreviewAnchorScrollTop(
container,
diff --git a/src/renderer/src/components/editor/use-markdown-preview-viewport.ts b/src/renderer/src/components/editor/use-markdown-preview-viewport.ts
index 6ba4a4f6cd8..7296d9ef6c2 100644
--- a/src/renderer/src/components/editor/use-markdown-preview-viewport.ts
+++ b/src/renderer/src/components/editor/use-markdown-preview-viewport.ts
@@ -1,3 +1,4 @@
+import { revealMarkdownNavigationDetails } from './markdown-navigation-details'
import type { MarkdownPreviewDocument } from './markdown-preview-document-types'
import type { VirtualMarkdownPreviewNavigation } from './VirtualMarkdownPreviewBody'
import { useCallback, useEffect, useLayoutEffect, type MutableRefObject } from 'react'
@@ -162,6 +163,7 @@ export function useMarkdownPreviewViewport({
return false
}
+ revealMarkdownNavigationDetails(target)
container.scrollTo({ top: getMarkdownPreviewAnchorScrollTop(container, target) })
target.focus({ preventScroll: true })
return true
diff --git a/src/renderer/src/components/editor/use-rich-markdown-table-of-contents.ts b/src/renderer/src/components/editor/use-rich-markdown-table-of-contents.ts
index 86f1418d1c6..c5d652d90bf 100644
--- a/src/renderer/src/components/editor/use-rich-markdown-table-of-contents.ts
+++ b/src/renderer/src/components/editor/use-rich-markdown-table-of-contents.ts
@@ -1,3 +1,4 @@
+import { revealMarkdownNavigationDetails } from './markdown-navigation-details'
import { useCallback, useMemo, type MutableRefObject } from 'react'
import type { MarkdownTocItem } from './markdown-table-of-contents'
import { findRichMarkdownTocHeadingTarget } from './rich-markdown-toc-heading-target'
@@ -31,7 +32,10 @@ export function useRichMarkdownTableOfContents(
return
}
const heading = findRichMarkdownTocHeadingTarget(container, flatTableOfContentsItems, id)
- heading?.scrollIntoView({ block: 'center' })
+ if (heading) {
+ revealMarkdownNavigationDetails(heading)
+ heading.scrollIntoView({ block: 'center' })
+ }
},
[flatTableOfContentsItems, scrollContainerRef]
)