Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ esbuild.buildSync({
target: dev ? "esnext" : "es5",
});

esbuild.buildSync({
entryPoints: ["js/resizeIframe.js"],
outdir: "dist",
bundle: true,
minify: !dev,
sourcemap: dev ? "inline" : true,
target: dev ? "esnext" : "es5",
globalName: "resizeIframe",
});
fs.copyFileSync("js/resizeIframe.js", "dist/resizeIframe.js");

["interwikiFrame", "styleFrame", "index"].forEach(function (frame) {
fs.copyFileSync("html/" + frame + ".html", "dist/" + frame + ".html");
Expand Down
4 changes: 3 additions & 1 deletion js/interwiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { scpBranches } from "./branches-info-scp";
import { wlBranches } from "./branches-info-wl";

/**
* @type {import("./resizeIframe").createResizeIframe}
* Provided globally by resizeIframe.js
*
* @type {(site: String, frameId: String, debounceTime?: Number) => ((height?: Number) => void)}
*/
var createResizeIframe = window.resizeIframe.createResizeIframe;

Expand Down
52 changes: 49 additions & 3 deletions js/resizeIframe.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
/**
* This script is used to resize iframes to match their contents. It is intended for use in the interwiki iframe to accommodate different numbers of translations per page, but it can be used to resize any iframe on wikidot.
*
* How to use:
*
* 1. Import this script in the <head> of the page to be used as an iframe:
*
* <script src="https://interwiki.scpwiki.com/resizeIframe.js" defer></script>
*
* 2. Define a dummy resize function to be used before the script loads:
*
* window.resize = () => {};
*
* 3. After load, replace the dummy function with the actual resizer function:
*
* addEventListener("load", () => {
* window.resize = window.resizeIframe.createResizeIframe(
* document.referrer,
* location.href.replace(/^.*\//, "/"),
* 100,
* );
* });
*
* 4. Whenever you want to resize the iframe, call the function:
*
* window.resize(); // Auto-resize to match the document height
* window.resize(500); // Resize to 500px
*
* 5. If your iframe is defined in a [[html]] block, it already has Wikidot's auto-resizing script.
* Disable it if you need to use this script to control the resizing:
*
* document.querySelectorAll(
* "script[src*='common--javascript/html-block-iframe.js']"
* ).forEach(s => s.remove())
* addEventListener("load", () => {
* document.querySelectorAll(
* "iframe[src*='common--javascript/resize-iframe.html']"
* ).forEach(i => i.remove())
* });
*/

/**
* Constructs and returns a function that, when called, resizes the current iframes to match its contents or the given height. The function is debounced.
*
* @param {String} site - The base URL of the site.
* @param {String} frameId - The last segment of the URL of the interwiki iframe, used by Wikidot to identify it when resizing it.
* @param {Number=} [debounceTime] - Debounce delay to stagger repeated calls to the resizer. Defaults to 750 ms.
* @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.
* @returns {((height: Number=) => void)} Debounced function that resizes the iframe. Optional height parameter sets the height of the iframe in pixels; if not set, the height is calculated from the document. Float and string values are OK (e.g. 10.5 and "10.5").
*/
export function createResizeIframe(site, frameId, debounceTime) {
function createResizeIframe(site, frameId, debounceTime) {
if (debounceTime == null) debounceTime = 750;

var container = document.getElementById("resizer-container");
Expand Down Expand Up @@ -56,7 +97,7 @@ export function createResizeIframe(site, frameId, debounceTime) {
* @param {Number} wait - The number of milliseconds to wait after any call to the debounced function before executing it.
* @returns {Function} The debounced function.
*/
export function debounce(func, wait) {
function debounce(func, wait) {
var timeout = 0;
return function () {
var context = this;
Expand All @@ -67,3 +108,8 @@ export function debounce(func, wait) {
}, wait);
};
}

window.resizeIframe = {
createResizeIframe: createResizeIframe,
debounce: debounce,
};
Loading