Skip to content

Commit 1ec34f3

Browse files
authored
Merge pull request #25 from scpwiki/resize-cache
Decouple resizer from interwiki
2 parents cf2f12d + 112cb14 commit 1ec34f3

File tree

7 files changed

+84
-76
lines changed

7 files changed

+84
-76
lines changed

.github/workflows/preview.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515
concurrency: preview-${{ github.ref }}
1616
jobs:
1717
deploy-preview:
18+
if: github.repository == 'scpwiki/interwiki'
1819
runs-on: ubuntu-latest
1920
steps:
2021
- name: Checkout

build.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ esbuild.buildSync({
1616
target: dev ? "esnext" : "es5",
1717
});
1818

19+
esbuild.buildSync({
20+
entryPoints: ["js/resizeIframe.js"],
21+
outdir: "dist",
22+
bundle: true,
23+
minify: !dev,
24+
sourcemap: dev ? "inline" : true,
25+
target: dev ? "esnext" : "es5",
26+
globalName: "resizeIframe",
27+
});
28+
1929
["interwikiFrame", "styleFrame", "index"].forEach(function (frame) {
2030
fs.copyFileSync("html/" + frame + ".html", "dist/" + frame + ".html");
2131
});

html/interwikiFrame.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<head>
55
<meta name="viewport" content="width=device-width,initial-scale=1" />
66

7+
<script src="./resizeIframe.js"></script>
78
<script src="./interwiki.js"></script>
89
</head>
910

js/createResizeIframe.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

js/interwiki.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { createResizeIframe } from "./createResizeIframe";
2-
import { addTranslations } from "./links";
31
import { addExternalStyle, createRequestStyleChange } from "./styles";
42

3+
import { ResizeObserver } from "@juggle/resize-observer";
4+
import { addTranslations } from "./links";
55
import { scpBranches } from "./branches-info-scp";
66
import { wlBranches } from "./branches-info-wl";
77

8-
import { ResizeObserver } from "@juggle/resize-observer";
8+
/**
9+
* @type {import("./resizeIframe").createResizeIframe}
10+
*/
11+
var createResizeIframe = window.resizeIframe.createResizeIframe;
912

1013
addEventListener("DOMContentLoaded", function () {
1114
var community = getQueryString(location.search, "community");
@@ -108,7 +111,10 @@ export function createInterwiki(
108111
var resize = createResizeIframe(site, frameId);
109112

110113
// Resize frame when size changes are detected
111-
var observer = new ResizeObserver(resize);
114+
var observer = new ResizeObserver(function () {
115+
// Only resize if there's at least one translation link
116+
if (document.getElementsByClassName("menu-item").length) resize();
117+
});
112118
observer.observe(document.documentElement);
113119

114120
// Construct the function that will be called internally and by

js/links.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { flags } from "./createResizeIframe";
21
import { cromLookup } from "./lookup/crom";
32

43
// Configure which lookup method is currently active
@@ -58,16 +57,7 @@ export function addTranslations(branches, currentBranchLang, pagename) {
5857
var header = document.querySelector(".heading p");
5958
header.innerText = currentBranch.head;
6059

61-
lookupMethod(
62-
currentBranch,
63-
branches,
64-
pagename,
65-
function (pageUrl, branchName, branchLang, isOriginal) {
66-
addTranslationLink(pageUrl, branchName, branchLang, isOriginal);
67-
// Indicate that data has been received
68-
flags.showInterwiki = true;
69-
}
70-
);
60+
lookupMethod(currentBranch, branches, pagename, addTranslationLink);
7161
}
7262

7363
/**

js/resizeIframe.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Constructs and returns a function that, when called, resizes the current iframes to match its contents or the given height. The function is debounced.
3+
*
4+
* @param {String} site - The base URL of the site.
5+
* @param {String} frameId - The last segment of the URL of the interwiki iframe, used by Wikidot to identify it when resizing it.
6+
* @param {Number=} [debounceTime] - Debounce delay to stagger repeated calls to the resizer. Defaults to 750 ms.
7+
* @returns {((height: Number=) => void)} Debounced function that resizes the iframe. Optional height parameter sets the height of the iframe; if not set, the height is calculated from the document.
8+
*/
9+
export function createResizeIframe(site, frameId, debounceTime) {
10+
if (debounceTime == null) debounceTime = 750;
11+
12+
var container = document.getElementById("resizer-container");
13+
if (container == null) {
14+
container = document.createElement("div");
15+
container.id = "resizer-container";
16+
document.body.appendChild(container);
17+
}
18+
var resizer = document.createElement("iframe");
19+
resizer.style.display = "none";
20+
container.appendChild(resizer);
21+
22+
// Trim leading slashes from frame ID
23+
frameId = frameId.replace(/^\/+/, "");
24+
25+
var resize = function (height) {
26+
if (height == null) {
27+
// Measure from the top of the document to the iframe container to get the document height
28+
// This takes into account inner margins, unlike e.g. document.body.clientHeight
29+
// The container must not have display:none for this to work, which is why the iframe has it instead
30+
height = container.getBoundingClientRect().top;
31+
// Brute-force past any subpixel issues
32+
if (height) height += 1;
33+
}
34+
resizer.src =
35+
site +
36+
"/common--javascript/resize-iframe.html?" +
37+
"#" +
38+
height +
39+
"/" +
40+
frameId;
41+
};
42+
43+
return debounce(resize, debounceTime);
44+
}
45+
46+
/**
47+
* Debounces a function, delaying its execution until a certain amount of time has passed since the last time it was called, and aggregating all calls made in that time into one.
48+
*
49+
* @param {Function} func - The function to call.
50+
* @param {Number} wait - The number of milliseconds to wait after any call to the debounced function before executing it.
51+
* @returns {Function} The debounced function.
52+
*/
53+
export function debounce(func, wait) {
54+
var timeout = 0;
55+
return function () {
56+
clearTimeout(timeout);
57+
timeout = setTimeout(function () {
58+
func.apply(null, arguments);
59+
}, wait);
60+
};
61+
}

0 commit comments

Comments
 (0)