Skip to content

Commit 2e66722

Browse files
committed
fix: Add mountHost null check to positionSidebar function.
1 parent fc801ea commit 2e66722

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

content.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@
228228
}
229229

230230
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);
232237

233238
const defaultGap = 26;
234239
const stickyTop = getFixedHeaderHeight() + defaultGap;
@@ -242,8 +247,18 @@
242247
if (mountHost !== currentMdBody) {
243248
const anchor = findReadmeContainer(currentMdBody, mountHost) || currentMdBody;
244249
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 = '';
247262
}
248263

249264
const layout = document.querySelector('.Layout');
@@ -260,6 +275,8 @@
260275
? window.innerWidth - mdRect.right - effectiveGap * 2
261276
: mdRect.left - effectiveGap * 2;
262277

278+
console.log('[GMTOC] availableWidth:', Math.round(availableWidth), 'vpWidth:', window.innerWidth, 'mdRight:', Math.round(mdRect.right), 'gap:', effectiveGap);
279+
263280
const isCollapsed = sidebarEl.classList.contains('gmtoc-collapsed');
264281

265282
// When collapsed, clear inline width so CSS `width: auto` takes effect;
@@ -307,12 +324,19 @@
307324
destroyPositionTracking();
308325

309326
if (currentMdBody) {
310-
resizeObserver = new ResizeObserver(debouncedPosition);
327+
resizeObserver = new ResizeObserver(() => {
328+
console.log('[GMTOC] ResizeObserver fired, mdBody connected:', currentMdBody.isConnected);
329+
debouncedPosition();
330+
});
311331
resizeObserver.observe(currentMdBody);
312332
resizeObserver.observe(document.documentElement);
313333

314334
if (mountHost && mountHost !== currentMdBody) {
315335
resizeObserver.observe(mountHost);
336+
const anchor = findReadmeContainer(currentMdBody, mountHost);
337+
if (anchor && anchor !== mountHost && anchor !== currentMdBody) {
338+
resizeObserver.observe(anchor);
339+
}
316340
}
317341

318342
const stickyHeader = document.querySelector('#repos-sticky-header');
@@ -377,6 +401,7 @@
377401

378402
const wrapper = document.createElement('div');
379403
wrapper.id = WRAPPER_ID;
404+
wrapper.style.visibility = 'hidden';
380405

381406
const sidebar = document.createElement('div');
382407
sidebar.id = SIDEBAR_ID;
@@ -668,6 +693,7 @@
668693
/* Main flow */
669694
/* ------------------------------------------------------------------ */
670695
function update() {
696+
console.log('[GMTOC] update() called');
671697
// 检测 stale DOM 引用:SPA 导航后旧 sidebar 已不在 DOM 中
672698
if (sidebarEl && !sidebarEl.isConnected) {
673699
removeSidebar();
@@ -720,13 +746,17 @@
720746
const mdBody = findMarkdownBody();
721747
if (mdBody && isAllowedPage()) {
722748
if (!sidebarEl) {
749+
console.log('[GMTOC] MO: no sidebar, triggering update');
723750
debouncedUpdate();
724751
return;
725752
}
726753
const newHeaders = extractHeaders(mdBody);
727754
const newSig = newHeaders.map((h) => `${h.level}:${h.id}`).join('|');
728755
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) {
730760
debouncedUpdate();
731761
}
732762
} else if (sidebarEl) {

0 commit comments

Comments
 (0)