Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,3 @@ jobs:

- name: Publish to npm
run: pnpm publish --no-git-checks

- name: Publish to JSR
run: pnpx jsr publish
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
<a href="https://www.npmjs.com/package/pointeract">
<img src="https://img.shields.io/npm/v/pointeract?logo=npm&labelColor=red&logoColor=white&color=333333" alt="npm package" />
</a>
<a href="https://jsr.io/@hesprs/pointeract">
<img src="https://img.shields.io/jsr/v/@hesprs/pointeract?logo=jsr&labelColor=f7df1e&logoColor=white&color=333333" alt="JSR package" />
</a>
<a href="https://snyk.io/test/npm/pointeract">
<img src="https://img.shields.io/badge/Snyk%20Security-Monitored-333333?logo=snyk&style=flat&labelColor=8A2BE2&logoColor=white" alt="library security" />
</a>
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions docs/en/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 0 additions & 21 deletions jsr.json

This file was deleted.

22 changes: 9 additions & 13 deletions src/BaseModule.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -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: <K extends keyof E>(
...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;
Expand Down
113 changes: 56 additions & 57 deletions src/Pointeract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export class Pointeract<T extends ModuleInputCtor = []> {
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,
Expand All @@ -51,73 +54,69 @@ export class Pointeract<T extends ModuleInputCtor = []> {
this.#subscribers[type]?.add(listener);
return this;
};
off<K extends keyof Events<T>>(type: K, listener: (event: Events<T>[K]) => void) {

off = <K extends keyof Events<T>>(type: K, listener: (event: Events<T>[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: <N extends keyof Events<T>>(
...args: undefined extends Events<T>[N] ? [N] : [N, Events<T>[N]]
) => {
const name = args[0];
const e = args[1];
let lastResult: boolean | Events<T>[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<T>[N],
) => boolean | Events<T>[N]
)(e);
if (lastResult === false) return;
}
let event: Events<T>[N];
if (lastResult === true) event = e as Events<T>[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 = <N extends keyof Events<T>>(
...args: undefined extends Events<T>[N] ? [N] : [N, Events<T>[N]]
) => {
const name = args[0];
const e = args[1];
let lastResult: boolean | Events<T>[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<T>[N]) => boolean | Events<T>[N])(
e,
);
if (lastResult === false) return;
}
let event: Events<T>[N];
if (lastResult === true) event = e as Events<T>[N];
else event = lastResult;
this.#subscribers[name]?.forEach((listener) => listener(event));
};

#augment = (aug: GeneralObject) => {
const descriptors = Object.getOwnPropertyDescriptors(aug);
Object.defineProperties(this, descriptors);
};

#runHooks<K extends HookKeys>(field: K, ...args: Parameters<Required<BaseModule>[K]>) {
#runHooks = <K extends HookKeys>(field: K, ...args: Parameters<Required<BaseModule>[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;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type General = any;
export type GeneralArray = ReadonlyArray<General>;
export type GeneralObject = object;
export type GeneralDictionary = Record<Indexable, General>;
export type GeneralFunction = (...args: General[]) => General;
export type GeneralConstructor = new (...args: General[]) => General;
type Indexable = string | number | symbol;

Expand Down