livebook/assets/js/lib/utils.js
Jonatan Kłosko 5c8e117800
Setup user profiles (#253)
* Add initial user config modal

* Assign user ids

* Update session data to hold user ids

* Get users list for specific ids

* Render user avatar

* User update

* Refactor user changes

* Subscribe to individual user updates

* Show users in side panel

* Add sidebar to homepage

* Don't generate the same color twice in a row

* Add documentation notes

* Fix tests

* Add tests

* Keep users in session data

* Rename color to hex_color
2021-05-03 20:03:19 +02:00

65 lines
1.7 KiB
JavaScript

export function isMacOS() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}
export function isEditableElement(element) {
return (
["input", "textarea"].includes(element.tagName.toLowerCase()) ||
element.contentEditable === "true"
);
}
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) {
const { height } = element.getBoundingClientRect();
if (height < window.innerHeight) {
element.scrollIntoView({ behavior: "smooth", block: "center" });
} else {
element.scrollIntoView({ behavior: "smooth", block: "start" });
}
}
/**
* 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)));
}