diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2e0ce08..174aae1 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -36,6 +36,3 @@ jobs: - name: Publish to npm run: pnpm publish --no-git-checks - - - name: Publish to JSR - run: pnpx jsr publish diff --git a/README.md b/README.md index 5b8353d..59ff135 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,6 @@ npm package - - JSR package - library security @@ -78,9 +75,6 @@ yarn add pointeract # bun bun add pointeract - -# deno -deno add jsr:@hesprs/pointeract ``` Or include the following lines directly in your HTML file: diff --git a/docs/en/get-started.md b/docs/en/get-started.md index 2f42372..8f390b5 100644 --- a/docs/en/get-started.md +++ b/docs/en/get-started.md @@ -28,10 +28,6 @@ yarn add pointeract bun add pointeract ``` -```sh [deno] -deno add jsr:@hesprs/pointeract -``` - ::: Or include the following lines directly in your HTML file: diff --git a/jsr.json b/jsr.json deleted file mode 100644 index a8613c3..0000000 --- a/jsr.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "pointeract", - "version": "1.1.2", - "description": "A 3KB, tree-shakable, TypeScript-native alternative to Hammer.js for robust pan/zoom gestures — runtime-flexible, extensible, and safe.", - "keywords": ["lightweight", "frontend", "gesture-detection", "pan-zoom", "typescript"], - "homepage": "https://pointeract.consensia.cc", - "bugs": { - "url": "https://github.com/hesprs/pointeract/issues" - }, - "license": "MIT", - "author": { - "name": "Hēsperus", - "email": "hesprs@outlook.com" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/hesprs/pointeract.git" - }, - "exports": "./src/index.ts", - "sideEffects": false -} diff --git a/src/BaseModule.ts b/src/BaseModule.ts index 24d2707..ef58f53 100644 --- a/src/BaseModule.ts +++ b/src/BaseModule.ts @@ -1,13 +1,13 @@ -import type { Pointeract, PointeractInterface } from '@/Pointeract'; import type { BaseOptions, - GeneralObject, Pointer, Pointers, StdEvents, ModuleInput as MI, Orchestratable, General, + Coordinates, + GeneralFunction, } from '@/types'; export type HookKeys = @@ -36,25 +36,21 @@ export default class BaseModule< declare private static readonly _BaseModuleBrand: unique symbol; // Nominal marker declare readonly _Events: E; declare readonly _Augmentation: A; - protected getNthPointer: Pointeract<[]>['moduleUtils']['getNthPointer']; - protected toTargetCoords: Pointeract<[]>['moduleUtils']['toTargetCoords']; - protected augment: (augmentation: A) => void; protected dispatch: ( ...arg: undefined extends E[K] ? [K] : [K, E[K]] ) => void; - options: O; + constructor( - utils: PointeractInterface['moduleUtils'], + protected augment: (augmentation: A) => void, + dispatch: GeneralFunction, + protected getNthPointer: (n: number) => Pointer, + protected toTargetCoords: (raw: Coordinates) => Coordinates, protected window: Window, protected pointers: Pointers, protected element: HTMLElement, - options: GeneralObject, + public options: O, ) { - this.getNthPointer = utils.getNthPointer; - this.toTargetCoords = utils.toTargetCoords; - this.augment = utils.augment; - this.dispatch = utils.dispatch as typeof this.dispatch; - this.options = options as O; + this.dispatch = dispatch; } onPointerDown?: (...args: [event: PointerEvent, pointer: Pointer, pointers: Pointers]) => void; diff --git a/src/Pointeract.ts b/src/Pointeract.ts index 2e0d24d..54914fe 100644 --- a/src/Pointeract.ts +++ b/src/Pointeract.ts @@ -35,7 +35,10 @@ export class Pointeract { this.options = options; modules.forEach((module) => { const instance = new module( - this.moduleUtils as PointeractInterface['moduleUtils'], + this.#augment, + this.dispatch, + this.#getNthPointer, + this.#toTargetCoords, this.#window, this.#pointers, this.#element, @@ -51,73 +54,69 @@ export class Pointeract { this.#subscribers[type]?.add(listener); return this; }; - off>(type: K, listener: (event: Events[K]) => void) { + + off = >(type: K, listener: (event: Events[K]) => void) => { this.#subscribers[type]?.delete(listener); return this; - } + }; + + #getNthPointer = (n: number) => { + const error = new Error('[Pointeract] Invalid pointer index.'); + if (n < 0 || n >= this.#pointers.size) throw error; + let i = 0; + for (const value of this.#pointers.values()) { + if (i === n) return value; + i++; + } + throw error; + }; - private moduleUtils = { - getNthPointer: (n: number) => { - const error = new Error('[Pointeract] Invalid pointer index.'); - if (n < 0 || n >= this.#pointers.size) throw error; - let i = 0; - for (const value of this.#pointers.values()) { - if (i === n) return value; - i++; - } - throw error; - }, - - // Screen to Container - toTargetCoords: (raw: Coordinates) => { - if (this.options.coordinateOutput === 'absolute') return raw; - const rect = this.#element.getBoundingClientRect(); - raw.x -= rect.left; - raw.y -= rect.top; - if (this.options.coordinateOutput === 'relative') return raw; - raw.x /= rect.width; - raw.y /= rect.height; - return raw; - }, - - dispatch: >( - ...args: undefined extends Events[N] ? [N] : [N, Events[N]] - ) => { - const name = args[0]; - const e = args[1]; - let lastResult: boolean | Events[N] = true; - for (const value of Object.values(this.#modules)) { - if (!value.modifiers || !(name in value.modifiers)) continue; - lastResult = - e === undefined - ? (value.modifiers[name] as () => boolean)() - : ( - value.modifiers[name] as ( - detail?: Events[N], - ) => boolean | Events[N] - )(e); - if (lastResult === false) return; - } - let event: Events[N]; - if (lastResult === true) event = e as Events[N]; - else event = lastResult; - this.#subscribers[name]?.forEach((listener) => listener(event)); - }, - - augment: (aug: GeneralObject) => { - Object.entries(aug).forEach(([key, value]) => (this[key as '_augmentSlot'] = value)); - }, + // Screen to Container + #toTargetCoords = (raw: Coordinates) => { + if (this.options.coordinateOutput === 'absolute') return raw; + const rect = this.#element.getBoundingClientRect(); + raw.x -= rect.left; + raw.y -= rect.top; + if (this.options.coordinateOutput === 'relative') return raw; + raw.x /= rect.width; + raw.y /= rect.height; + return raw; }; - dispatch = this.moduleUtils.dispatch; + dispatch = >( + ...args: undefined extends Events[N] ? [N] : [N, Events[N]] + ) => { + const name = args[0]; + const e = args[1]; + let lastResult: boolean | Events[N] = true; + for (const value of Object.values(this.#modules)) { + if (!value.modifiers || !(name in value.modifiers)) continue; + lastResult = + e === undefined + ? (value.modifiers[name] as () => boolean)() + : (value.modifiers[name] as (detail?: Events[N]) => boolean | Events[N])( + e, + ); + if (lastResult === false) return; + } + let event: Events[N]; + if (lastResult === true) event = e as Events[N]; + else event = lastResult; + this.#subscribers[name]?.forEach((listener) => listener(event)); + }; + + #augment = (aug: GeneralObject) => { + const descriptors = Object.getOwnPropertyDescriptors(aug); + Object.defineProperties(this, descriptors); + }; - #runHooks(field: K, ...args: Parameters[K]>) { + #runHooks = (field: K, ...args: Parameters[K]>) => { Object.values(this.#modules).forEach((module) => { const hook = module[field]; // oxlint-disable-next-line typescript/no-explicit-any if (hook) hook(...(args as any)); }); - } + }; #onPointerDown = (e: PointerEvent) => { if (this.#pointers.size >= 2) return; diff --git a/src/types.ts b/src/types.ts index 9fdd1af..9f4a603 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,7 @@ export type General = any; export type GeneralArray = ReadonlyArray; export type GeneralObject = object; export type GeneralDictionary = Record; +export type GeneralFunction = (...args: General[]) => General; export type GeneralConstructor = new (...args: General[]) => General; type Indexable = string | number | symbol;