| @@ -102,35 +102,53 @@ const getScrollTargetForAnchor = (target: HTMLElement) => { |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | const TAB_QUERY_PARAM = 'tabs' |
| 105 | const COLLAPSIBLE_QUERY_PARAM = 'c' |
| 105 | 106 | |
| 106 | | const getRequestedTabPath = () => { |
| 107 | | const value = new URLSearchParams(window.location.search).get( |
| 108 | | TAB_QUERY_PARAM |
| 109 | | ) |
| 107 | const getRequestedAnchorPath = (param: string) => { |
| 108 | const value = new URLSearchParams(window.location.search).get(param) |
| 110 | 109 | return value |
| 111 | 110 | ?.split(',') |
| 112 | | .map((tab) => tab.trim()) |
| 111 | .map((anchor) => anchor.trim()) |
| 113 | 112 | .filter(Boolean) ?? [] |
| 114 | 113 | } |
| 115 | 114 | |
| 116 | | const buildUrlWithTabs = (tabs: string[], hash = '') => { |
| 115 | const getRequestedTabPath = () => getRequestedAnchorPath(TAB_QUERY_PARAM) |
| 116 | const getRequestedCollapsiblePath = () => |
| 117 | getRequestedAnchorPath(COLLAPSIBLE_QUERY_PARAM) |
| 118 | |
| 119 | const encodeAnchorList = (anchors: string[]) => |
| 120 | anchors.map((anchor) => encodeURIComponent(anchor)).join(',') |
| 121 | |
| 122 | const buildUrl = ( |
| 123 | options: { tabs?: string[]; collapsibles?: string[]; hash?: string } = {} |
| 124 | ) => { |
| 117 | 125 | const params = new URLSearchParams(window.location.search) |
| 126 | const currentTabs = params.get(TAB_QUERY_PARAM) |
| 127 | const currentCollapsibles = params.get(COLLAPSIBLE_QUERY_PARAM) |
| 118 | 128 | params.delete(TAB_QUERY_PARAM) |
| 129 | params.delete(COLLAPSIBLE_QUERY_PARAM) |
| 130 | |
| 131 | const tabs = options.tabs ?? |
| 132 | (currentTabs ? currentTabs.split(',').filter(Boolean) : []) |
| 133 | const collapsibles = options.collapsibles ?? |
| 134 | (currentCollapsibles ? currentCollapsibles.split(',').filter(Boolean) : []) |
| 119 | 135 | |
| 120 | 136 | const queryParts = [ |
| 121 | 137 | params.toString(), |
| 122 | | tabs.length |
| 123 | | ? `${TAB_QUERY_PARAM}=${ |
| 124 | | tabs.map((tab) => encodeURIComponent(tab)).join(',') |
| 125 | | }` |
| 138 | tabs.length ? `${TAB_QUERY_PARAM}=${encodeAnchorList(tabs)}` : '', |
| 139 | collapsibles.length |
| 140 | ? `${COLLAPSIBLE_QUERY_PARAM}=${encodeAnchorList(collapsibles)}` |
| 126 | 141 | : '' |
| 127 | 142 | ].filter(Boolean) |
| 128 | 143 | |
| 144 | const hash = options.hash ?? '' |
| 129 | 145 | return `${window.location.pathname}${ |
| 130 | 146 | queryParts.length ? `?${queryParts.join('&')}` : '' |
| 131 | 147 | }${hash ? `#${encodeURIComponent(hash)}` : ''}` |
| 132 | 148 | } |
| 133 | 149 | |
| 150 | const buildUrlWithTabs = (tabs: string[], hash = '') => buildUrl({ tabs, hash }) |
| 151 | |
| 134 | 152 | const getSelectedTabAnchor = (tabs: HTMLElement) => |
| 135 | 153 | tabs.querySelector<HTMLButtonElement>( |
| 136 | 154 | '.plugin-tabs--tab[aria-selected="true"][data-tab-anchor]' |
| @@ -165,7 +183,9 @@ const updateQueryForSelectedTab = async (tabs: HTMLElement) => { |
| 165 | 183 | const tabPath = getTabPathForGroup(tabs) |
| 166 | 184 | if (!tabPath.length) return |
| 167 | 185 | |
| 168 | | const nextUrl = buildUrlWithTabs(tabPath) |
| 186 | const collapsibles = getOpenCollapsiblePath(tabs) |
| 187 | |
| 188 | const nextUrl = buildUrl({ tabs: tabPath, collapsibles }) |
| 169 | 189 | const currentUrl = |
| 170 | 190 | `${window.location.pathname}${window.location.search}${window.location.hash}` |
| 171 | 191 | if (nextUrl === currentUrl) return |
| @@ -184,6 +204,87 @@ const queueTabQueryUpdateForSelection = (target: EventTarget | null) => { |
| 184 | 204 | if (tabs) void updateQueryForSelectedTab(tabs) |
| 185 | 205 | } |
| 186 | 206 | |
| 207 | const COLLAPSIBLE_SELECTOR = 'details[data-collapsible-anchor]' |
| 208 | |
| 209 | const getOpenCollapsiblePath = (details: HTMLElement) => { |
| 210 | const path: string[] = [] |
| 211 | let el: HTMLElement | null = details |
| 212 | |
| 213 | while (el) { |
| 214 | const current: HTMLElement | null = el.closest(COLLAPSIBLE_SELECTOR) |
| 215 | if (!current) break |
| 216 | |
| 217 | if ((current as HTMLDetailsElement).open) { |
| 218 | const anchor = current.dataset.collapsibleAnchor |
| 219 | if (anchor) path.unshift(anchor) |
| 220 | } |
| 221 | |
| 222 | el = current.parentElement |
| 223 | } |
| 224 | |
| 225 | return path |
| 226 | } |
| 227 | |
| 228 | const getEnclosingTabPath = (el: HTMLElement) => { |
| 229 | const panel = el.closest<HTMLElement>( |
| 230 | '.plugin-tabs--content[data-tab-anchor]' |
| 231 | ) |
| 232 | const tabs = panel?.closest<HTMLElement>('.plugin-tabs') |
| 233 | return tabs ? getTabPathForGroup(tabs) : [] |
| 234 | } |
| 235 | |
| 236 | const updateQueryForCollapsible = async (details: HTMLElement) => { |
| 237 | await nextTick() |
| 238 | await nextFrame() |
| 239 | |
| 240 | const collapsiblePath = getOpenCollapsiblePath(details) |
| 241 | const tabPath = getEnclosingTabPath(details) |
| 242 | |
| 243 | const nextUrl = buildUrl({ |
| 244 | tabs: tabPath.length ? tabPath : undefined, |
| 245 | collapsibles: collapsiblePath |
| 246 | }) |
| 247 | const currentUrl = |
| 248 | `${window.location.pathname}${window.location.search}${window.location.hash}` |
| 249 | if (nextUrl === currentUrl) return |
| 250 | |
| 251 | window.history.pushState(null, '', nextUrl) |
| 252 | rewriteAnchorLinks() |
| 253 | } |
| 254 | |
| 255 | const queueCollapsibleQueryUpdate = (target: EventTarget | null) => { |
| 256 | if (!(target instanceof HTMLElement)) return |
| 257 | |
| 258 | const summary = target.closest<HTMLElement>('summary') |
| 259 | const details = summary?.parentElement |
| 260 | if (details instanceof HTMLElement && details.matches(COLLAPSIBLE_SELECTOR)) { |
| 261 | void updateQueryForCollapsible(details) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | const selectCollapsiblesByPath = async (collapsiblePath: string[]) => { |
| 266 | let root: ParentNode = document |
| 267 | let opened: HTMLElement | undefined |
| 268 | |
| 269 | for (const anchor of collapsiblePath) { |
| 270 | const details = root.querySelector<HTMLDetailsElement>( |
| 271 | `details[data-collapsible-anchor="${CSS.escape(anchor)}"]` |
| 272 | ) |
| 273 | if (!details) return opened |
| 274 | |
| 275 | opened = details |
| 276 | if (!details.open) { |
| 277 | details.open = true |
| 278 | await nextTick() |
| 279 | await nextFrame() |
| 280 | } |
| 281 | |
| 282 | root = details |
| 283 | } |
| 284 | |
| 285 | return opened |
| 286 | } |
| 287 | |
| 187 | 288 | const TAB_LINK_MARKER = 'tab-' |
| 188 | 289 | |
| 189 | 290 | const rewriteAnchorLinks = () => { |
| @@ -375,11 +476,17 @@ const tryOpenAnchoredContent = async () => { |
| 375 | 476 | const selectedTabs = requestedTabs.length |
| 376 | 477 | ? await selectTabsByPath(requestedTabs) |
| 377 | 478 | : undefined |
| 479 | const requestedCollapsibles = getRequestedCollapsiblePath() |
| 480 | const openedCollapsible = requestedCollapsibles.length |
| 481 | ? await selectCollapsiblesByPath(requestedCollapsibles) |
| 482 | : undefined |
| 378 | 483 | const hash = window.location.hash.slice(1) |
| 379 | 484 | |
| 380 | 485 | if (!hash) { |
| 381 | 486 | resetSearchNavigation() |
| 382 | | if (selectedTabs) { |
| 487 | if (openedCollapsible) { |
| 488 | scrollToElement(openedCollapsible, false) |
| 489 | } else if (selectedTabs) { |
| 383 | 490 | scrollToElement(getSelectedTabScrollTarget(selectedTabs), false) |
| 384 | 491 | } |
| 385 | 492 | return |
| @@ -403,6 +510,19 @@ const tryOpenAnchoredContent = async () => { |
| 403 | 510 | |
| 404 | 511 | if (searchNavigated) { |
| 405 | 512 | await openCollapsibles(target) |
| 513 | const collapsiblePath = getOpenCollapsiblePath(target) |
| 514 | if (collapsiblePath.length) { |
| 515 | const tabPath = getEnclosingTabPath(target) |
| 516 | window.history.replaceState( |
| 517 | null, |
| 518 | '', |
| 519 | buildUrl({ |
| 520 | tabs: tabPath.length ? tabPath : undefined, |
| 521 | collapsibles: collapsiblePath |
| 522 | }) |
| 523 | ) |
| 524 | rewriteAnchorLinks() |
| 525 | } |
| 406 | 526 | resetSearchNavigation() |
| 407 | 527 | return |
| 408 | 528 | } |
| @@ -972,6 +1092,7 @@ onMounted(() => { |
| 972 | 1092 | useEventListener(document, 'click', (e: MouseEvent) => { |
| 973 | 1093 | if (!e.isTrusted) return |
| 974 | 1094 | queueTabQueryUpdateForSelection(e.target) |
| 1095 | queueCollapsibleQueryUpdate(e.target) |
| 975 | 1096 | }) |
| 976 | 1097 | |
| 977 | 1098 | useEventListener(document, 'keydown', (e: KeyboardEvent) => { |