forked from scpwiki/interwiki
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from r74tech/main
Decouple resizer from interwiki
- Loading branch information
Showing
10 changed files
with
116 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html id="interwiki" style="min-width: max-content"> | ||
<head> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
|
||
<script src="./interwiki.js"></script> | ||
</head> | ||
<head> | ||
<meta name="viewport" content="width=device-width,initial-scale=1" /> | ||
|
||
<body> | ||
<div class="side-block"> | ||
<div class="heading"> | ||
<p></p> | ||
</div> | ||
<script src="./resizeIframe.js"></script> | ||
<script src="./interwiki.js"></script> | ||
</head> | ||
|
||
<body> | ||
<div class="side-block"> | ||
<div class="heading"> | ||
<p></p> | ||
</div> | ||
<div id="resizer-container"></div> | ||
</body> | ||
</html> | ||
</div> | ||
<div id="resizer-container"></div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* 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. | ||
*/ | ||
export function createResizeIframe(site, frameId, debounceTime) { | ||
if (debounceTime == null) debounceTime = 750; | ||
|
||
var container = document.getElementById("resizer-container"); | ||
if (container == null) { | ||
container = document.createElement("div"); | ||
container.id = "resizer-container"; | ||
document.body.appendChild(container); | ||
} | ||
var resizer = document.createElement("iframe"); | ||
resizer.style.display = "none"; | ||
container.appendChild(resizer); | ||
|
||
// Trim leading slashes from frame ID | ||
frameId = frameId.replace(/^\/+/, ""); | ||
|
||
var resize = function (height) { | ||
if (height == null) { | ||
// Measure from the top of the document to the iframe container to get the document height | ||
// This takes into account inner margins, unlike e.g. document.body.clientHeight | ||
// The container must not have display:none for this to work, which is why the iframe has it instead | ||
height = container.getBoundingClientRect().top; | ||
// Brute-force past any subpixel issues | ||
if (height) height += 1; | ||
} | ||
resizer.src = | ||
site + | ||
"/common--javascript/resize-iframe.html?" + | ||
"#" + | ||
height + | ||
"/" + | ||
frameId; | ||
}; | ||
|
||
return debounce(resize, debounceTime); | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @param {Function} func - The function to call. | ||
* @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) { | ||
var timeout = 0; | ||
return function () { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(function () { | ||
func.apply(null, arguments); | ||
}, wait); | ||
}; | ||
} |