@vnejs/plugins.scenario.autoread 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,6 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ TRY: "vne:autoread:try",
3
+
4
+ PUSH: "vne:autoread:push",
5
+ POP: "vne:autoread:pop",
6
+ };
@@ -0,0 +1,2 @@
1
+ export const DEFAULT_DELAY = 5000;
2
+ export const DEFAULT_ENABLED = false;
@@ -0,0 +1,4 @@
1
+ export const SETTINGS_KEYS = {
2
+ ENABLED: "vne:autoread:enabled",
3
+ DELAY: "vne:autoread:delay",
4
+ };
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 { Autoread } from "./modules/autoread";
9
+
10
+ regPlugin("AUTOREAD", { constants, events: SUBSCRIBE_EVENTS, params, settings: SETTINGS_KEYS }, [Autoread]);
@@ -0,0 +1,67 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class Autoread extends Module {
4
+ name = "autoread";
5
+
6
+ disableSources = [];
7
+
8
+ subscribe = () => {
9
+ this.on(this.EVENTS.AUTOREAD.TRY, this.onTry);
10
+ this.on(this.EVENTS.AUTOREAD.PUSH, this.onPush);
11
+ this.on(this.EVENTS.AUTOREAD.POP, this.onPop);
12
+
13
+ this.on(this.EVENTS.SETTINGS.CHANGED, this.onSettingsChanged);
14
+
15
+ this.on(this.EVENTS.STATE.CLEAR, this.clearTimeout);
16
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
17
+ };
18
+
19
+ init = () =>
20
+ Promise.all([
21
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.AUTOREAD.ENABLED, value: this.PARAMS.AUTOREAD.DEFAULT_ENABLED }),
22
+ this.emit(this.EVENTS.SETTINGS.INIT, { name: this.SETTINGS.AUTOREAD.DELAY, value: this.PARAMS.AUTOREAD.DEFAULT_DELAY }),
23
+ ]);
24
+
25
+ onTry = async () => {
26
+ this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "try" } });
27
+
28
+ if (!this.shared.settings[this.SETTINGS.AUTOREAD.ENABLED] || this.disableSources.length) return;
29
+
30
+ await this.waitRerender();
31
+
32
+ const elements = document.elementsFromPoint(window.innerHeight / 2, window.innerHeight / 2);
33
+
34
+ for (let i = 0; i < elements.length; i++) {
35
+ if (elements[i].hasAttribute("vne-autoread-allow")) return this.emit(this.EVENTS.INTERACT.EMIT);
36
+ if (elements[i].hasAttribute("vne-autoread-disable")) return this.createTimeout();
37
+ }
38
+
39
+ this.createTimeout();
40
+ };
41
+ onPush = ({ source } = {}) => {
42
+ this.disableSources.push(source);
43
+ this.clearTimeout();
44
+ };
45
+ onPop = ({ source } = {}) => {
46
+ this.disableSources = this.disableSources.filter((s) => s !== source);
47
+
48
+ if (!this.disableSources.length) this.createTimeout();
49
+ };
50
+
51
+ onSettingsChanged = ({ name, value } = {}) =>
52
+ name === this.SETTINGS.AUTOREAD.ENABLED && this.emit(value ? this.EVENTS.AUTOREAD.POP : this.EVENTS.AUTOREAD.PUSH, { source: this.name });
53
+
54
+ onStateSet = () => {
55
+ this.disableSources = [];
56
+
57
+ this.clearTimeout();
58
+ this.createTimeout();
59
+ };
60
+
61
+ createTimeout = () => {
62
+ this.autoreadTimeout = setTimeout(this.emitAutoread, this.shared.settings[this.SETTINGS.AUTOREAD.DELAY] || 0);
63
+ };
64
+ clearTimeout = () => clearTimeout(this.autoreadTimeout);
65
+
66
+ emitAutoread = () => this.emit(this.EVENTS.AUTOREAD.TRY);
67
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@vnejs/plugins.scenario.autoread",
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
+ }