Theme engine v3 step 10: the frame marker
installFrameMarker: framed parts inside the watched scopes get data-wb-void when nothing in them renders and collapse (frameMarkerCss); a ResizeObserver per part and a child-list/attribute observer on the scopes trigger the check, batched per frame. The per-part 1.5 s polling of the X app's void cards is replaced.
This commit is contained in:
parent
306b6add47
commit
7cd6c99007
1 changed files with 68 additions and 0 deletions
|
|
@ -138,3 +138,71 @@ export function installLayerGuard(win: Window, options: LayerGuardOptions): () =
|
||||||
win.addEventListener('resize', onResize);
|
win.addEventListener('resize', onResize);
|
||||||
return () => { observer?.disconnect(); moved.disconnect(); sized?.disconnect(); win.clearInterval(retry); win.removeEventListener('resize', onResize); };
|
return () => { observer?.disconnect(); moved.disconnect(); sized?.disconnect(); win.clearInterval(retry); win.removeEventListener('resize', onResize); };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
// Frame marker (theme engine v3 4.3, step 10): a framed part (a card, a panel) whose content is all
|
||||||
|
// hidden (a feature switch, a plugin, the site itself) collapses instead of leaving an empty frame.
|
||||||
|
// Static CSS cannot see `display:none` inside, so the marker sets `data-wb-void` and a static rule
|
||||||
|
// hides the part. Watched by a ResizeObserver per framed part (a hidden module changes the part's size)
|
||||||
|
// and a child-list observer on the scopes (new parts), never by a timer.
|
||||||
|
|
||||||
|
export const VOID_ATTR = 'data-wb-void';
|
||||||
|
/** Elements that make a part non-empty when they render. */
|
||||||
|
export const VISIBLE_INSIDE = 'a,button,h2,img,svg,input,video,[role="heading"],[data-testid="tweetText"]';
|
||||||
|
|
||||||
|
/** The static rule the marker relies on. */
|
||||||
|
export const frameMarkerCss = () => `:root:root [${VOID_ATTR}]{display:none !important}`;
|
||||||
|
|
||||||
|
export type FrameMarkerOptions = {
|
||||||
|
/** Containers to watch (X: the sidebar column). */
|
||||||
|
scopes: string[];
|
||||||
|
/** Framed parts inside them (one selector list). */
|
||||||
|
parts: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Pure: whether `part` renders nothing (every candidate inside has no client rect). */
|
||||||
|
export function isVoid(part: Element): boolean {
|
||||||
|
return ![...part.querySelectorAll(VISIBLE_INSIDE)].some(el => el.getClientRects().length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function installFrameMarker(win: Window, options: FrameMarkerOptions): () => void {
|
||||||
|
const doc = win.document;
|
||||||
|
const Resize = (win as Window & { ResizeObserver?: typeof ResizeObserver }).ResizeObserver;
|
||||||
|
const Observer = (win as Window & { MutationObserver: typeof MutationObserver }).MutationObserver;
|
||||||
|
const watched = new WeakSet<Element>();
|
||||||
|
let frame = 0;
|
||||||
|
const check = (part: Element) => {
|
||||||
|
// A void part is display:none itself: lift the mark to see whether something came back.
|
||||||
|
const was = part.hasAttribute(VOID_ATTR);
|
||||||
|
if (was) part.removeAttribute(VOID_ATTR);
|
||||||
|
if (isVoid(part)) part.setAttribute(VOID_ATTR, '');
|
||||||
|
};
|
||||||
|
const sweep = () => {
|
||||||
|
frame = 0;
|
||||||
|
for (const scope of options.scopes) {
|
||||||
|
const root = doc.querySelector(scope);
|
||||||
|
if (!root) continue;
|
||||||
|
for (const part of root.querySelectorAll(options.parts)) {
|
||||||
|
if (!watched.has(part)) { watched.add(part); sized?.observe(part); }
|
||||||
|
check(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const schedule = () => { if (!frame) frame = win.requestAnimationFrame(sweep); };
|
||||||
|
const sized = Resize ? new Resize(schedule) : null;
|
||||||
|
// Inner modules change while the part keeps its size only when hidden content was already 0 tall:
|
||||||
|
// child-list changes under the scopes cover those, and new parts.
|
||||||
|
const added = new Observer(schedule);
|
||||||
|
const roots = new WeakSet<Element>();
|
||||||
|
const attach = () => {
|
||||||
|
for (const scope of options.scopes) {
|
||||||
|
const root = doc.querySelector(scope);
|
||||||
|
if (root && !roots.has(root)) { roots.add(root); added.observe(root, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class', 'hidden'] }); schedule(); }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
attach();
|
||||||
|
// Scopes mount late and are replaced on navigation: looked up again every 2 s (one querySelector per
|
||||||
|
// scope; the per-part work is all observer-driven).
|
||||||
|
const retry = win.setInterval(attach, 2000);
|
||||||
|
return () => { sized?.disconnect(); added.disconnect(); win.clearInterval(retry); if (frame) win.cancelAnimationFrame(frame); for (const el of doc.querySelectorAll(`[${VOID_ATTR}]`)) el.removeAttribute(VOID_ATTR); };
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue