'use strict'; var eventListener = require('@solid-primitives/event-listener'); var immutable = require('@solid-primitives/utils/immutable'); var rootless = require('@solid-primitives/rootless'); var utils = require('@solid-primitives/utils'); var solidJs = require('solid-js'); var web = require('solid-js/web'); // src/index.ts exports.getPositionToElement = (poz, el) => { const { top, left, width, height } = el.getBoundingClientRect(), x = poz.x - left, y = poz.y - top; return { ...poz, x, y, isInside: x >= 0 && y >= 0 && x <= width && y <= height }; }; var parseOnEventName = (name) => name.substring(2).toLowerCase(); var parseHandlersMap = (handlers) => { const result = {}; Object.entries(handlers).forEach(([name, fn]) => result[parseOnEventName(name)] = fn); return result; }; var pointerStateKeys = [ "x", "y", "pointerId", "pressure", "tiltX", "tiltY", "width", "height", "twist", "pointerType" ]; var toState = (e) => immutable.pick(e, ...pointerStateKeys); var toStateActive = (e, isActive) => ({ ...toState(e), isActive }); var DEFAULT_STATE = { x: 0, y: 0, pointerId: 0, pressure: 0, tiltX: 0, tiltY: 0, width: 0, height: 0, twist: 0, pointerType: null, isActive: false }; // src/index.ts function createPointerListeners(config) { if (web.isServer) { return; } const [{ target = document.body, pointerTypes, passive = true }, handlers] = immutable.split( config, "target", "pointerTypes", "passive" ); const [{ gotcapture: onGotCapture, lostcapture: onLostCapture }, nativeHandlers] = immutable.split( parseHandlersMap(handlers), "gotcapture", "lostcapture" ); const guardCB = (handler) => (event) => (!pointerTypes || pointerTypes.includes(event.pointerType)) && handler(event); const addEventListener = (type, fn) => eventListener.createEventListener(target, type, guardCB(fn), { passive }); utils.entries(nativeHandlers).forEach( ([name, fn]) => fn && addEventListener(`pointer${name}`, fn) ); if (onGotCapture) addEventListener("gotpointercapture", onGotCapture); if (onLostCapture) addEventListener("lostpointercapture", onLostCapture); } function createPerPointerListeners(config) { if (web.isServer) { return; } const [{ target = document.body, pointerTypes, passive = true }, handlers] = immutable.split( config, "pointerTypes", "target", "passive" ); const { down: onDown, enter: onEnter } = parseHandlersMap(handlers); const owner = solidJs.getOwner(); const onlyInitMessage = "All listeners need to be added synchronously in the initial event."; const addListener = (type, fn, pointerId) => eventListener.createEventListener( target, type, (e) => (!pointerTypes || pointerTypes.includes(e.pointerType)) && (!pointerId || e.pointerId === pointerId) && fn(e), { passive } ); if (onEnter) { const handleEnter = (e) => { rootless.createSubRoot((dispose) => { const { pointerId } = e; let init = true; let onLeave; addListener( "pointerleave", (e2) => { onLeave?.(e2); dispose(); }, pointerId ); onEnter( e, new Proxy( {}, { get: (_, key) => { const type = "pointer" + key.substring(2).toLowerCase(); return (fn) => { if (!init) { if (!web.isServer && solidJs.DEV) console.warn(onlyInitMessage); return; } if (type === "pointerleave") onLeave = fn; else addListener(type, fn, pointerId); }; } } ) ); init = false; }, owner); }; addListener("pointerenter", handleEnter); } if (onDown) { const handleDown = (e) => { rootless.createSubRoot((dispose) => { const { pointerId } = e; let init = true; let onUp; addListener( ["pointerup", "pointercancel"], (e2) => { onUp?.(e2); dispose(); }, pointerId ); onDown( e, // onMove() (fn) => { if (init) addListener("pointermove", fn, pointerId); else if (!web.isServer && solidJs.DEV) console.warn(onlyInitMessage); }, // onUp() (fn) => { if (init) onUp = fn; else if (!web.isServer && solidJs.DEV) console.warn(onlyInitMessage); } ); init = false; }, owner); }; addListener("pointerdown", handleDown); } } function createPointerPosition(config = {}) { if (web.isServer) { return () => DEFAULT_STATE; } const [state, setState] = solidJs.createSignal(config.value ?? DEFAULT_STATE); let pointer = null; const handler = (e, active = true) => setState(toStateActive(e, active)); createPointerListeners({ ...config, onEnter: (e) => { if (pointer === null) { pointer = e.pointerId; handler(e); } }, onMove: (e) => { if (e.pointerId === pointer) handler(e); }, onLeave: (e) => { if (e.pointerId === pointer) { pointer = null; handler(e, false); } } }); return state; } function createPointerList(config = {}) { if (web.isServer) { return () => []; } const [pointers, setPointers] = solidJs.createSignal([]); createPerPointerListeners({ ...config, onEnter(e, { onMove, onDown, onUp, onLeave }) { const [pointer, setPointer] = solidJs.createSignal({ ...toState(e), isDown: false }); setPointers((p) => [...p, pointer]); onMove((e2) => setPointer((p) => ({ ...toState(e2), isDown: p.isDown }))); onDown((e2) => setPointer({ ...toState(e2), isDown: true })); onUp((e2) => setPointer({ ...toState(e2), isDown: false })); onLeave(() => setPointers((p) => immutable.remove(p, pointer))); } }); return pointers; } exports.pointerPosition = (el, props) => { const { pointerTypes, handler } = (() => { const v = props(); return typeof v === "function" ? { handler: v, pointerTypes: void 0 } : v; })(); const runHandler = (e, active = true) => handler(toStateActive(e, active), el); let pointer = null; createPointerListeners({ target: el, pointerTypes, onEnter: (e) => { if (pointer === null) { pointer = e.pointerId; runHandler(e); } }, onMove: (e) => { if (e.pointerId === pointer) runHandler(e); }, onLeave: (e) => { if (e.pointerId === pointer) { pointer = null; runHandler(e, false); } } }); }; exports.pointerHover = (el, props) => { const { pointerTypes, handler } = (() => { const v = props(); return typeof v === "function" ? { handler: v, pointerTypes: void 0 } : v; })(); const pointers = /* @__PURE__ */ new Set(); createPointerListeners({ target: el, pointerTypes, onEnter: (e) => { pointers.add(e.pointerId); handler(true, el); }, onLeave: (e) => { pointers.delete(e.pointerId); if (pointers.size === 0) handler(false, el); } }); }; exports.createPerPointerListeners = createPerPointerListeners; exports.createPointerList = createPointerList; exports.createPointerListeners = createPointerListeners; exports.createPointerPosition = createPointerPosition;