@vnejs/plugins.feature.cursor 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/const/const.js ADDED
File without changes
@@ -0,0 +1,13 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ SHOW: "vne:cursor:show",
3
+ HIDE: "vne:cursor:hide",
4
+ UPDATE: "vne:cursor:update",
5
+
6
+ CLICK: "vne:cursor:click",
7
+
8
+ VIEW_SET: "vne:cursor:view_set",
9
+
10
+ MOVE: "vne:cursor:move",
11
+ MOVED: "vne:cursor:moved",
12
+ SHIFT: "vne:cursor:shift",
13
+ };
@@ -0,0 +1,2 @@
1
+ export const DEFAULT_VIEW = "default";
2
+ export const DEFAULT_SHIFT_SPEED = 16;
@@ -0,0 +1,3 @@
1
+ export const SETTINGS_KEYS = {
2
+ SHIFT_SPEED: "vne:cursor:shift_speed",
3
+ };
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import * as constants from "./const/const";
4
+ import { SUBSCRIBE_EVENTS } from "./const/events";
5
+ import * as params from "./const/params";
6
+ import { SETTINGS_KEYS } from "./const/settings";
7
+
8
+ import { Cursor } from "./modules/cursor";
9
+
10
+ regPlugin("CURSOR", { constants, events: SUBSCRIBE_EVENTS, settings: SETTINGS_KEYS, params }, [Cursor]);
@@ -0,0 +1,73 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Cursor extends Module {
4
+ name = "cursor";
5
+
6
+ maxX = document.documentElement.clientWidth;
7
+ maxY = document.documentElement.clientHeight;
8
+
9
+ mouseMoveHandler = (e = {}) => this.emit(this.EVENTS.CURSOR.MOVE, { x: e.clientX, y: e.clientY });
10
+
11
+ subscribe = () => {
12
+ this.on(this.EVENTS.CURSOR.CLICK, this.onClick);
13
+
14
+ this.on(this.EVENTS.CURSOR.MOVE, this.onMove);
15
+ this.on(this.EVENTS.CURSOR.SHIFT, this.onShift);
16
+
17
+ this.on(this.EVENTS.CURSOR.VIEW_SET, this.onViewSet);
18
+
19
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
20
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
21
+
22
+ this.on(this.EVENTS.SYSTEM.STARTED, this.onStarted);
23
+ this.on(this.EVENTS.SYSTEM.RESIZE, this.onResize);
24
+ };
25
+
26
+ init = () => {
27
+ document.addEventListener("mousemove", this.mouseMoveHandler);
28
+
29
+ return this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.CURSOR.SHIFT_SPEED, value: this.PARAMS.CURSOR.DEFAULT_SHIFT_SPEED });
30
+ };
31
+
32
+ onClick = () =>
33
+ document
34
+ .elementFromPoint(this.state.x, this.state.y)
35
+ .dispatchEvent(new MouseEvent("click", { view: window, bubbles: true, cancelable: true, clientX: this.state.x, clientY: this.state.y, button: 0 }));
36
+
37
+ onMove = ({ x, y } = {}) => {
38
+ this.emit(this.EVENTS.CURSOR.SHOW);
39
+
40
+ return this.setXandY(x, y);
41
+ };
42
+ onShift = ({ x, y } = {}) => {
43
+ const shiftSpeed = this.shared.settings[this.SETTINGS.CURSOR.SHIFT_SPEED];
44
+
45
+ return this.setXandY(this.state.x + shiftSpeed * x, this.state.y + shiftSpeed * y);
46
+ };
47
+
48
+ onViewSet = ({ view = this.PARAMS.CURSOR.DEFAULT_VIEW } = {}) => this.updateState({ view });
49
+
50
+ onStarted = () => {
51
+ this.emit(this.EVENTS.CURSOR.VIEW_SET, { view: this.PARAMS.CURSOR.DEFAULT_VIEW });
52
+ this.emit(this.EVENTS.CURSOR.SHOW);
53
+ this.emit(this.EVENTS.CURSOR.MOVED);
54
+ };
55
+ onStateSet = ({ [this.name]: { view = this.PARAMS.CURSOR.DEFAULT_VIEW } = {} } = {}) => this.emit(this.EVENTS.CURSOR.VIEW_SET, { view });
56
+ onStateClear = () => this.emit(this.EVENTS.CURSOR.VIEW_SET, { view: this.PARAMS.CURSOR.DEFAULT_VIEW });
57
+ onResize = () => {
58
+ this.maxX = document.documentElement.clientWidth;
59
+ this.maxY = document.documentElement.clientHeight;
60
+
61
+ this.setXandY(this.state.x, this.state.y);
62
+ };
63
+
64
+ setXandY = (x, y) => {
65
+ if (this.state.x === x && this.state.y === y) return;
66
+
67
+ this.updateState({ x: Math.min(Math.max(x, 0), this.maxX), y: Math.min(Math.max(y, 0), this.maxY) });
68
+
69
+ this.emit(this.EVENTS.CURSOR.MOVED);
70
+ };
71
+
72
+ getDefaultState = () => ({ view: this.PARAMS.CURSOR.DEFAULT_VIEW, x: this.maxX / 2, y: this.maxY / 2 });
73
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.feature.cursor",
3
+ "version": "0.1.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major:plugin": "npm run publish:major",
8
+ "publish:minor:plugin": "npm run publish:minor",
9
+ "publish:patch:plugin": "npm run publish:patch",
10
+ "publish:major": "npm version major && npm publish --access public",
11
+ "publish:minor": "npm version minor && npm publish --access public",
12
+ "publish:patch": "npm version patch && npm publish --access public"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "description": ""
17
+ }