mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:29:37 +08:00
Follow-up to #615/#608/#634: the isComposingRef + composition-handler trio duplicated across five textareas (ComposerInput, SimpleComposerInput, ChatMessages edit, BookChatPanel, PartnerComposer) becomes one useImeComposing() hook; the citation-anchor fallback chain + <details> auto-open duplicated in both markdown renderers moves to lib/markdown-anchors.ts; and the chat scrollport reserves the scrollbar gutter on both edges so the message column centers on the same axis as header and composer on classic-scrollbar platforms. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
/**
|
|
* DOM-side citation-anchor resolution shared by RichMarkdownRenderer and
|
|
* SimpleMarkdownRenderer. Deliberately kept out of markdown-display.ts,
|
|
* which is pure string logic exercised by node tests — this helper touches
|
|
* `document`.
|
|
*/
|
|
|
|
import {
|
|
citationAnchorIdFor,
|
|
safeDecodeURIComponent,
|
|
} from "@/lib/markdown-display";
|
|
|
|
/**
|
|
* Resolve the element a citation link should scroll to and auto-open its
|
|
* enclosing `<details>` so the target is actually visible. Fallback chain:
|
|
* the citation id's own anchor, then the link's `#hash`, then the
|
|
* `references` section (also the fallback when the resolved id is missing
|
|
* from the DOM). Returns the target, or null when nothing matched; the
|
|
* caller performs its own scroll.
|
|
*/
|
|
export function findCitationAnchor(
|
|
href: string | undefined,
|
|
id?: string,
|
|
): HTMLElement | null {
|
|
const hashTarget =
|
|
id && citationAnchorIdFor(id)
|
|
? citationAnchorIdFor(id)
|
|
: href?.startsWith("#")
|
|
? safeDecodeURIComponent(href.slice(1))
|
|
: "references";
|
|
const target =
|
|
document.getElementById(hashTarget || "") ??
|
|
document.getElementById("references");
|
|
const parentDetails = target?.closest("details");
|
|
if (parentDetails instanceof HTMLDetailsElement) {
|
|
parentDetails.open = true;
|
|
}
|
|
return target;
|
|
}
|