UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

107 lines (106 loc) • 3.68 kB
import { __exportAll } from "../_virtual/_rolldown/runtime.js"; import { Point } from "../geometry/Point.js"; import { off, on, preventDefault } from "./DomEvent.js"; //#region src/dom/DomUtil.js var DomUtil_exports = /* @__PURE__ */ __exportAll({ create: () => create, disableImageDrag: () => disableImageDrag, disableTextSelection: () => disableTextSelection, enableImageDrag: () => enableImageDrag, enableTextSelection: () => enableTextSelection, get: () => get, getPosition: () => getPosition, getScale: () => getScale, getSizedParentNode: () => getSizedParentNode, preventOutline: () => preventOutline, restoreOutline: () => restoreOutline, setPosition: () => setPosition, setTransform: () => setTransform, toBack: () => toBack, toFront: () => toFront }); function get(id) { return typeof id === "string" ? document.getElementById(id) : id; } function create(tagName, className, container) { const el = document.createElement(tagName); el.className = className ?? ""; container?.appendChild(el); return el; } function toFront(el) { const parent = el.parentNode; if (parent && parent.lastChild !== el) parent.appendChild(el); } function toBack(el) { const parent = el.parentNode; if (parent && parent.firstChild !== el) parent.insertBefore(el, parent.firstChild); } function setTransform(el, offset, scale) { const pos = offset ?? new Point(0, 0); const transform = `translate3d(${pos.x}px, ${pos.y}px, 0px)${scale ? ` scale(${scale})` : ""}`; if (el.style.transform !== transform) el.style.transform = transform; } const positions = /* @__PURE__ */ new WeakMap(); function setPosition(el, point) { positions.set(el, point); setTransform(el, point); } function getPosition(el) { return positions.get(el) ?? new Point(0, 0); } const documentStyle = typeof document === "undefined" ? {} : document.documentElement.style; const userSelectProp = ["userSelect", "WebkitUserSelect"].find((prop) => prop in documentStyle); let prevUserSelect; function disableTextSelection() { const value = documentStyle[userSelectProp]; if (value === "none") return; prevUserSelect = value; documentStyle[userSelectProp] = "none"; } function enableTextSelection() { if (typeof prevUserSelect === "undefined") return; documentStyle[userSelectProp] = prevUserSelect; prevUserSelect = void 0; } function disableImageDrag() { on(window, "dragstart", preventDefault); } function enableImageDrag() { off(window, "dragstart", preventDefault); } let _outlineElement; let _outlineStyle; function preventOutline(element) { while (element.tabIndex === -1) element = element.parentNode; if (!element.style) return; restoreOutline(); _outlineElement = element; _outlineStyle = element.style.outlineStyle; element.style.outlineStyle = "none"; on(window, "keydown", restoreOutline); } function restoreOutline() { if (!_outlineElement) return; _outlineElement.style.outlineStyle = _outlineStyle; _outlineElement = void 0; _outlineStyle = void 0; off(window, "keydown", restoreOutline); } function getSizedParentNode(element) { do element = element.parentNode; while ((!element.offsetWidth || !element.offsetHeight) && element !== document.body); return element; } function getScale(element) { const rect = element.getBoundingClientRect(); return { x: rect.width / element.offsetWidth || 1, y: rect.height / element.offsetHeight || 1, boundingClientRect: rect }; } //#endregion export { DomUtil_exports, create, disableImageDrag, disableTextSelection, enableImageDrag, enableTextSelection, get, getPosition, getScale, getSizedParentNode, preventOutline, restoreOutline, setPosition, setTransform, toBack, toFront }; //# sourceMappingURL=DomUtil.js.map