|
228 | 228 | } |
229 | 229 |
|
230 | 230 | function positionSidebar() { |
231 | | - if (!sidebarEl || !wrapperEl || !currentMdBody || !mountHost) return; |
| 231 | + if (!sidebarEl || !wrapperEl || !currentMdBody || !mountHost) { |
| 232 | + console.log('[GMTOC] positionSidebar skip:', { s: !!sidebarEl, w: !!wrapperEl, m: !!currentMdBody, h: !!mountHost }); |
| 233 | + return; |
| 234 | + } |
| 235 | + |
| 236 | + console.log('[GMTOC] positionSidebar called, mdBody connected:', currentMdBody.isConnected, 'mountHost connected:', mountHost.isConnected); |
232 | 237 |
|
233 | 238 | const defaultGap = 26; |
234 | 239 | const stickyTop = getFixedHeaderHeight() + defaultGap; |
|
242 | 247 | if (mountHost !== currentMdBody) { |
243 | 248 | const anchor = findReadmeContainer(currentMdBody, mountHost) || currentMdBody; |
244 | 249 | const anchorRect = anchor.getBoundingClientRect(); |
245 | | - wrapperEl.style.top = (anchorRect.top - hostRect.top) + 'px'; |
246 | | - wrapperEl.style.height = anchor.offsetHeight + 'px'; |
| 250 | + const anchorHeight = anchor.offsetHeight; |
| 251 | + console.log('[GMTOC] anchor:', anchor.tagName, anchor.className.substring(0, 60), 'h:', anchorHeight, 'top:', Math.round(anchorRect.top), 'hostTop:', Math.round(hostRect.top)); |
| 252 | + // 防御:anchor 尚未完成布局时不写入异常值,等待后续 ResizeObserver 触发修正 |
| 253 | + if (anchorHeight > 0) { |
| 254 | + wrapperEl.style.top = (anchorRect.top - hostRect.top) + 'px'; |
| 255 | + wrapperEl.style.height = anchorHeight + 'px'; |
| 256 | + wrapperEl.style.visibility = ''; |
| 257 | + } else { |
| 258 | + return; |
| 259 | + } |
| 260 | + } else { |
| 261 | + wrapperEl.style.visibility = ''; |
247 | 262 | } |
248 | 263 |
|
249 | 264 | const layout = document.querySelector('.Layout'); |
|
260 | 275 | ? window.innerWidth - mdRect.right - effectiveGap * 2 |
261 | 276 | : mdRect.left - effectiveGap * 2; |
262 | 277 |
|
| 278 | + console.log('[GMTOC] availableWidth:', Math.round(availableWidth), 'vpWidth:', window.innerWidth, 'mdRight:', Math.round(mdRect.right), 'gap:', effectiveGap); |
| 279 | + |
263 | 280 | const isCollapsed = sidebarEl.classList.contains('gmtoc-collapsed'); |
264 | 281 |
|
265 | 282 | // When collapsed, clear inline width so CSS `width: auto` takes effect; |
|
307 | 324 | destroyPositionTracking(); |
308 | 325 |
|
309 | 326 | if (currentMdBody) { |
310 | | - resizeObserver = new ResizeObserver(debouncedPosition); |
| 327 | + resizeObserver = new ResizeObserver(() => { |
| 328 | + console.log('[GMTOC] ResizeObserver fired, mdBody connected:', currentMdBody.isConnected); |
| 329 | + debouncedPosition(); |
| 330 | + }); |
311 | 331 | resizeObserver.observe(currentMdBody); |
312 | 332 | resizeObserver.observe(document.documentElement); |
313 | 333 |
|
314 | 334 | if (mountHost && mountHost !== currentMdBody) { |
315 | 335 | resizeObserver.observe(mountHost); |
| 336 | + const anchor = findReadmeContainer(currentMdBody, mountHost); |
| 337 | + if (anchor && anchor !== mountHost && anchor !== currentMdBody) { |
| 338 | + resizeObserver.observe(anchor); |
| 339 | + } |
316 | 340 | } |
317 | 341 |
|
318 | 342 | const stickyHeader = document.querySelector('#repos-sticky-header'); |
|
377 | 401 |
|
378 | 402 | const wrapper = document.createElement('div'); |
379 | 403 | wrapper.id = WRAPPER_ID; |
| 404 | + wrapper.style.visibility = 'hidden'; |
380 | 405 |
|
381 | 406 | const sidebar = document.createElement('div'); |
382 | 407 | sidebar.id = SIDEBAR_ID; |
|
668 | 693 | /* Main flow */ |
669 | 694 | /* ------------------------------------------------------------------ */ |
670 | 695 | function update() { |
| 696 | + console.log('[GMTOC] update() called'); |
671 | 697 | // 检测 stale DOM 引用:SPA 导航后旧 sidebar 已不在 DOM 中 |
672 | 698 | if (sidebarEl && !sidebarEl.isConnected) { |
673 | 699 | removeSidebar(); |
|
720 | 746 | const mdBody = findMarkdownBody(); |
721 | 747 | if (mdBody && isAllowedPage()) { |
722 | 748 | if (!sidebarEl) { |
| 749 | + console.log('[GMTOC] MO: no sidebar, triggering update'); |
723 | 750 | debouncedUpdate(); |
724 | 751 | return; |
725 | 752 | } |
726 | 753 | const newHeaders = extractHeaders(mdBody); |
727 | 754 | const newSig = newHeaders.map((h) => `${h.level}:${h.id}`).join('|'); |
728 | 755 | const oldSig = currentHeaders.map((h) => `${h.level}:${h.id}`).join('|'); |
729 | | - if (newSig !== oldSig) { |
| 756 | + const mdBodyChanged = mdBody !== currentMdBody; |
| 757 | + const sigChanged = newSig !== oldSig; |
| 758 | + console.log('[GMTOC] MO: mdBodyChanged:', mdBodyChanged, 'sigChanged:', sigChanged); |
| 759 | + if (sigChanged || mdBodyChanged) { |
730 | 760 | debouncedUpdate(); |
731 | 761 | } |
732 | 762 | } else if (sidebarEl) { |
|
0 commit comments