UNPKG

vite-plugin-vue-inspector

Version:

Jump to local IDE source code when clicking Vue elements in the browser.

68 lines (67 loc) 2.04 kB
import { findTraceAtPointer, getInternalStore } from "./client/record.mjs"; import { customRef, ref, shallowRef } from "vue"; //#region node_modules/.pnpm/nanoevents@9.1.0/node_modules/nanoevents/index.js let createNanoEvents = () => ({ emit(event, ...args) { for (let callbacks = this.events[event] || [], i = 0, length = callbacks.length; i < length; i++) callbacks[i](...args); }, events: {}, on(event, cb) { (this.events[event] ||= []).push(cb); return () => { this.events[event] = this.events[event]?.filter((i) => cb !== i); }; } }); //#endregion //#region packages/core/src/client/listeners.ts const KEY_IGNORE = "data-v-inspector-ignore"; const lastMatchedElement = shallowRef(); const store = getInternalStore(); const events = store.events ||= createNanoEvents(); const isEnabled = customRef(() => { const value = ref(false); return { get() { return value.value; }, set(newValue) { if (newValue === value.value) return; value.value = newValue; events.emit(newValue ? "enabled" : "disabled"); } }; }); function isIgnoredEvent(event) { const target = event.target; return target instanceof Element && Boolean(target.closest(`[${KEY_IGNORE}]`)); } if (typeof document !== "undefined") { document.addEventListener("pointermove", (event) => { if (!isEnabled.value) return; if (isIgnoredEvent(event)) return; const result = findTraceAtPointer({ x: event.clientX, y: event.clientY }); if (result?.el === lastMatchedElement.value?.el) return; lastMatchedElement.value = result; events.emit("hover", result, event); }); document.addEventListener("click", (event) => { if (!isEnabled.value) return; if (isIgnoredEvent(event)) return; const result = findTraceAtPointer({ x: event.clientX, y: event.clientY }); if (!result) return; events.emit("click", result, event); event.preventDefault(); event.stopPropagation(); event.stopImmediatePropagation(); return false; }, true); } //#endregion export { isEnabled as n, lastMatchedElement as r, events as t };