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) {
|
|
|
|
return (
|
|
|
|
["input", "textarea"].includes(element.tagName.toLowerCase()) ||
|
|
|
|
element.contentEditable === "true"
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
}
|