Skip to content

Commit 05c363b

Browse files
authored
fix(view): add support for opening folding-headings in useYfmShowElemWithId() hook (#320)
1 parent 936b177 commit 05c363b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/view/hooks/useYfmShowElemWithId/useYfmShowElemWithId.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RefObject, useEffect} from 'react';
1+
import {type RefObject, useEffect} from 'react';
22

33
const YfmCutCN = {
44
Open: 'open',
@@ -13,6 +13,11 @@ const YfmTabsCN = {
1313
TabPanel: 'yfm-tab-panel',
1414
} as const;
1515

16+
const FoldingHeadingsCN = {
17+
Open: 'open',
18+
Section: 'heading-section',
19+
} as const;
20+
1621
export function useYfmShowElemWithId(ref: RefObject<HTMLElement>, id: string) {
1722
useEffect(() => {
1823
const {current: containerDom} = ref;
@@ -23,7 +28,7 @@ export function useYfmShowElemWithId(ref: RefObject<HTMLElement>, id: string) {
2328

2429
while (elem && elem !== containerDom) {
2530
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
26-
openYfmCut(elem) || switchYfmTabs(elem);
31+
openYfmCut(elem) || openFoldingHeadings(elem, id) || switchYfmTabs(elem);
2732
elem = elem.parentElement;
2833
}
2934
}, [id]);
@@ -68,3 +73,15 @@ function switchYfmTabs(tabPanelElem: Element): boolean {
6873
}
6974
return true;
7075
}
76+
77+
function openFoldingHeadings(elem: Element, id: string): boolean {
78+
if (
79+
elem.classList.contains(FoldingHeadingsCN.Section) &&
80+
!elem.classList.contains(FoldingHeadingsCN.Open) &&
81+
id !== elem.firstElementChild?.id
82+
) {
83+
elem.classList.add(FoldingHeadingsCN.Open);
84+
return true;
85+
}
86+
return false;
87+
}

0 commit comments

Comments
 (0)