Merge pull request 'fix(pages): a short Custom HTML block shrinks to its content height' (#71) from fix-frame-content-height into main
CI / build-and-publish (push) Failing after 0s
CI / build-and-publish (push) Failing after 0s
This commit was merged in pull request #71.
This commit is contained in:
commit
dbb58e6e94
2 files changed
+13
-2
No files matched your search
@@ -37,3 +37,14 @@ describe("frame script: same-page links", () => {
|
||||
expect(post.mock.calls.filter(([d]) => (d as { type: string }).type === "colite-frame-anchor")).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("frame script: height report", () => {
|
||||
it("reports the content's height, not the frame's own, so a short block can shrink below its start height", () => {
|
||||
const post = vi.spyOn(window.parent, "postMessage").mockImplementation(() => {});
|
||||
const root = document.documentElement;
|
||||
vi.spyOn(root, "scrollHeight", "get").mockReturnValue(600);
|
||||
vi.spyOn(root, "getBoundingClientRect").mockReturnValue({ height: 69.4 } as DOMRect);
|
||||
runFrameScript("<header>Top bar</header>");
|
||||
expect(post).toHaveBeenCalledWith({ type: "colite-frame-height", height: 70 }, "*");
|
||||
});
|
||||
});
|
||||
@@ -15,10 +15,10 @@ export const FRAME_ANCHOR_MESSAGE = "colite-frame-anchor";
|
||||
const ANCHOR_SOURCE = "^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$";
|
||||
const ANCHOR = new RegExp(ANCHOR_SOURCE);
|
||||
|
||||
// Posts the document height to the parent page on load and whenever it changes. A click on a #name link
|
||||
// Posts the content height (not scrollHeight, which never drops below the frame itself) to the parent page on load and whenever it changes. A click on a #name link
|
||||
// (add-page-anchor-links) asks the page to scroll instead: the frame is as tall as its content and never scrolls.
|
||||
const FRAME_SCRIPT =
|
||||
`<script>(function(){function s(){parent.postMessage({type:"${FRAME_MESSAGE}",height:Math.ceil(document.documentElement.scrollHeight)},"*")}` +
|
||||
`<script>(function(){function s(){parent.postMessage({type:"${FRAME_MESSAGE}",height:Math.ceil(document.documentElement.getBoundingClientRect().height)},"*")}` +
|
||||
`addEventListener("load",s);if(window.ResizeObserver)new ResizeObserver(s).observe(document.documentElement);s();` +
|
||||
`document.addEventListener("click",function(e){var a=e.target&&e.target.closest&&e.target.closest('a[href^="#"]');if(!a)return;` +
|
||||
`var n=a.getAttribute("href").slice(1);if(!/${ANCHOR_SOURCE}/.test(n))return;e.preventDefault();var t=document.getElementById(n);` +
|
||||
|
||||
Reference in new issue
Block a user