2021-06-10 20:54:55 +08:00
|
|
|
import md5 from "crypto-js/md5";
|
|
|
|
import encBase64 from "crypto-js/enc-base64";
|
|
|
|
|
2021-02-18 20:14:09 +08:00
|
|
|
export function isMacOS() {
|
|
|
|
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
|
|
|
|
}
|
2021-02-18 22:11:24 +08:00
|
|
|
|
|
|
|
export function isEditableElement(element) {
|
2022-01-19 21:43:35 +08:00
|
|
|
return (
|
|
|
|
element.matches && element.matches("input, textarea, [contenteditable]")
|
|
|
|
);
|
2021-02-18 22:11:24 +08:00
|
|
|
}
|
2021-03-11 22:28:18 +08:00
|
|
|
|
|
|
|
export function clamp(n, x, y) {
|
|
|
|
return Math.min(Math.max(n, x), y);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLineHeight(element) {
|
|
|
|
const computedStyle = window.getComputedStyle(element);
|
|
|
|
const lineHeight = parseInt(computedStyle.lineHeight, 10);
|
|
|
|
|
|
|
|
if (Number.isNaN(lineHeight)) {
|
|
|
|
const clone = element.cloneNode();
|
|
|
|
clone.innerHTML = "<br>";
|
|
|
|
element.appendChild(clone);
|
|
|
|
const singleLineHeight = clone.clientHeight;
|
|
|
|
clone.innerHTML = "<br><br>";
|
|
|
|
const doubleLineHeight = clone.clientHeight;
|
|
|
|
element.removeChild(clone);
|
|
|
|
const lineHeight = doubleLineHeight - singleLineHeight;
|
|
|
|
return lineHeight;
|
|
|
|
} else {
|
|
|
|
return lineHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function selectElementContent(element) {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function smoothlyScrollToElement(element) {
|
2021-04-05 21:06:14 +08:00
|
|
|
const { height } = element.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (height < window.innerHeight) {
|
|
|
|
element.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
|
|
} else {
|
|
|
|
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
|
|
}
|
2021-03-11 22:28:18 +08:00
|
|
|
}
|
2021-05-04 02:03:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms a UTF8 string into base64 encoding.
|
|
|
|
*/
|
|
|
|
export function encodeBase64(string) {
|
|
|
|
return btoa(unescape(encodeURIComponent(string)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms base64 encoding into UTF8 string.
|
|
|
|
*/
|
|
|
|
export function decodeBase64(binary) {
|
|
|
|
return decodeURIComponent(escape(atob(binary)));
|
|
|
|
}
|
2021-05-07 22:41:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random string.
|
|
|
|
*/
|
|
|
|
export function randomId() {
|
2021-12-24 21:18:34 +08:00
|
|
|
return randomString(24);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a random long string.
|
|
|
|
*/
|
|
|
|
export function randomToken() {
|
|
|
|
return randomString(40);
|
|
|
|
}
|
|
|
|
|
|
|
|
function randomString(byteSize) {
|
|
|
|
const array = new Uint8Array(byteSize);
|
2021-05-07 22:41:37 +08:00
|
|
|
crypto.getRandomValues(array);
|
|
|
|
const byteString = String.fromCharCode(...array);
|
|
|
|
return btoa(byteString);
|
|
|
|
}
|
2021-06-10 20:54:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates MD5 of the given string and returns
|
|
|
|
* the base64 encoded binary.
|
|
|
|
*/
|
|
|
|
export function md5Base64(string) {
|
|
|
|
return md5(string).toString(encBase64);
|
|
|
|
}
|
2021-06-27 00:05:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple throttle version that ensures
|
|
|
|
* the given function is called at most once
|
|
|
|
* within the given time window.
|
|
|
|
*/
|
|
|
|
export function throttle(fn, windowMs) {
|
|
|
|
let ignore = false;
|
|
|
|
|
|
|
|
return (...args) => {
|
|
|
|
if (!ignore) {
|
|
|
|
fn(...args);
|
|
|
|
ignore = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
ignore = false;
|
|
|
|
}, windowMs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-07-29 01:35:36 +08:00
|
|
|
|
2021-10-16 18:24:19 +08:00
|
|
|
export function setFavicon(name) {
|
|
|
|
let link = document.querySelector(`[rel="icon"]`);
|
|
|
|
|
|
|
|
if (!link) {
|
|
|
|
link = document.createElement("link");
|
|
|
|
link.rel = "icon";
|
|
|
|
document.head.appendChild(link);
|
|
|
|
}
|
|
|
|
|
|
|
|
link.href = `/${name}.svg`;
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:35:36 +08:00
|
|
|
export function findChildOrThrow(element, selector) {
|
|
|
|
const child = element.querySelector(selector);
|
|
|
|
|
|
|
|
if (!child) {
|
|
|
|
throw new Error(
|
|
|
|
`expected a child matching ${selector}, but none was found`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
2021-12-02 23:45:00 +08:00
|
|
|
|
|
|
|
export function cancelEvent(event) {
|
|
|
|
// Cancel any default browser behavior.
|
|
|
|
event.preventDefault();
|
|
|
|
// Stop event propagation (e.g. so it doesn't reach the editor).
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|