tiny-essentials 1.12.2 โ 1.13.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/README.md +1 -0
- package/dist/v1/TinyBasicsEs.js +38 -0
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDomReadyManager.js +213 -0
- package/dist/v1/TinyDomReadyManager.min.js +1 -0
- package/dist/v1/TinyEssentials.js +214 -0
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyUploadClicker.js +1 -0
- package/dist/v1/basics/index.cjs +1 -0
- package/dist/v1/basics/index.d.mts +2 -1
- package/dist/v1/basics/index.mjs +2 -2
- package/dist/v1/basics/simpleMath.cjs +38 -0
- package/dist/v1/basics/simpleMath.d.mts +24 -0
- package/dist/v1/basics/simpleMath.mjs +29 -0
- package/dist/v1/build/TinyDomReadyManager.cjs +7 -0
- package/dist/v1/build/TinyDomReadyManager.d.mts +3 -0
- package/dist/v1/build/TinyDomReadyManager.mjs +2 -0
- package/dist/v1/index.cjs +3 -0
- package/dist/v1/index.d.mts +3 -1
- package/dist/v1/index.mjs +3 -2
- package/dist/v1/libs/TinyDomReadyManager.cjs +174 -0
- package/dist/v1/libs/TinyDomReadyManager.d.mts +100 -0
- package/dist/v1/libs/TinyDomReadyManager.mjs +158 -0
- package/docs/v1/README.md +1 -0
- package/docs/v1/basics/simpleMath.md +28 -0
- package/docs/v1/libs/TinyDomReadyManager.md +198 -0
- package/package.json +1 -1
package/dist/v1/index.cjs
CHANGED
|
@@ -19,6 +19,7 @@ var TinyDragDropDetector = require('./libs/TinyDragDropDetector.cjs');
|
|
|
19
19
|
var normalFuncs = require('./fileManager/normalFuncs.cjs');
|
|
20
20
|
var asyncFuncs = require('./fileManager/asyncFuncs.cjs');
|
|
21
21
|
var TinyDragger = require('./libs/TinyDragger.cjs');
|
|
22
|
+
var TinyDomReadyManager = require('./libs/TinyDomReadyManager.cjs');
|
|
22
23
|
|
|
23
24
|
|
|
24
25
|
|
|
@@ -45,6 +46,7 @@ exports.offFullScreenChange = fullScreen.offFullScreenChange;
|
|
|
45
46
|
exports.onFullScreenChange = fullScreen.onFullScreenChange;
|
|
46
47
|
exports.requestFullScreen = fullScreen.requestFullScreen;
|
|
47
48
|
exports.formatBytes = simpleMath.formatBytes;
|
|
49
|
+
exports.genFibonacciSeq = simpleMath.genFibonacciSeq;
|
|
48
50
|
exports.getAge = simpleMath.getAge;
|
|
49
51
|
exports.getSimplePerc = simpleMath.getSimplePerc;
|
|
50
52
|
exports.ruleOfThree = simpleMath.ruleOfThree;
|
|
@@ -94,3 +96,4 @@ exports.isDirEmptyAsync = asyncFuncs.isDirEmptyAsync;
|
|
|
94
96
|
exports.listDirsAsync = asyncFuncs.listDirsAsync;
|
|
95
97
|
exports.listFilesAsync = asyncFuncs.listFilesAsync;
|
|
96
98
|
exports.TinyDragger = TinyDragger;
|
|
99
|
+
exports.TinyDomReadyManager = TinyDomReadyManager;
|
package/dist/v1/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import TinyDomReadyManager from './libs/TinyDomReadyManager.mjs';
|
|
1
2
|
import TinyDragger from './libs/TinyDragger.mjs';
|
|
2
3
|
import TinyDragDropDetector from './libs/TinyDragDropDetector.mjs';
|
|
3
4
|
import TinyToastNotify from './libs/TinyToastNotify.mjs';
|
|
@@ -6,6 +7,7 @@ import TinyRateLimiter from './libs/TinyRateLimiter.mjs';
|
|
|
6
7
|
import ColorSafeStringify from './libs/ColorSafeStringify.mjs';
|
|
7
8
|
import TinyPromiseQueue from './libs/TinyPromiseQueue.mjs';
|
|
8
9
|
import TinyLevelUp from '../legacy/libs/userLevel.mjs';
|
|
10
|
+
import { genFibonacciSeq } from './basics/simpleMath.mjs';
|
|
9
11
|
import { isDirEmptyAsync } from './fileManager/asyncFuncs.mjs';
|
|
10
12
|
import { fileSizeAsync } from './fileManager/asyncFuncs.mjs';
|
|
11
13
|
import { dirSizeAsync } from './fileManager/asyncFuncs.mjs';
|
|
@@ -70,5 +72,5 @@ import { getTimeDuration } from './basics/clock.mjs';
|
|
|
70
72
|
import { shuffleArray } from './basics/array.mjs';
|
|
71
73
|
import { toTitleCase } from './basics/text.mjs';
|
|
72
74
|
import { toTitleCaseLowerFirst } from './basics/text.mjs';
|
|
73
|
-
export { TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, readJsonBlob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
75
|
+
export { TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, readJsonBlob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst };
|
|
74
76
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/v1/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import { shuffleArray } from './basics/array.mjs';
|
|
|
5
5
|
import { formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, } from './basics/clock.mjs';
|
|
6
6
|
import { countObj, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, objType, checkObj, isJsonObject, } from './basics/objFilter.mjs';
|
|
7
7
|
import { documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, } from './basics/fullScreen.mjs';
|
|
8
|
-
import { formatBytes, getAge, getSimplePerc, ruleOfThree } from './basics/simpleMath.mjs';
|
|
8
|
+
import { formatBytes, genFibonacciSeq, getAge, getSimplePerc, ruleOfThree, } from './basics/simpleMath.mjs';
|
|
9
9
|
import { addAiMarkerShortcut, toTitleCase, toTitleCaseLowerFirst } from './basics/text.mjs';
|
|
10
10
|
import ColorSafeStringify from './libs/ColorSafeStringify.mjs';
|
|
11
11
|
import TinyPromiseQueue from './libs/TinyPromiseQueue.mjs';
|
|
@@ -17,4 +17,5 @@ import TinyDragDropDetector from './libs/TinyDragDropDetector.mjs';
|
|
|
17
17
|
import { readJsonFile, writeJsonFile, ensureDirectory, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, getLatestBackupPath, } from './fileManager/normalFuncs.mjs';
|
|
18
18
|
import { listFilesAsync, listDirsAsync, clearDirectoryAsync, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, } from './fileManager/asyncFuncs.mjs';
|
|
19
19
|
import TinyDragger from './libs/TinyDragger.mjs';
|
|
20
|
-
|
|
20
|
+
import TinyDomReadyManager from './libs/TinyDomReadyManager.mjs';
|
|
21
|
+
export { TinyDomReadyManager, TinyDragger, TinyDragDropDetector, TinyToastNotify, TinyNotifyCenter, TinyRateLimiter, ColorSafeStringify, TinyPromiseQueue, TinyLevelUp, genFibonacciSeq, isDirEmptyAsync, fileSizeAsync, dirSizeAsync, listFilesAsync, listDirsAsync, getHtmlElBorders, getHtmlElBordersWidth, getHtmlElMargin, getHtmlElPadding, getLatestBackupPath, fetchJson, readJsonBlob, saveJsonFile, readJsonFile, writeJsonFile, ensureDirectory, clearDirectoryAsync, clearDirectory, fileExists, dirExists, isDirEmpty, ensureCopyFile, tryDeleteFile, writeTextFile, listFiles, listDirs, fileSize, dirSize, backupFile, restoreLatestBackup, renameFileBatch, renameFileRegex, renameFileAddPrefixSuffix, renameFileNormalizeCase, renameFilePadNumbers, documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, areHtmlElsColliding, isJsonObject, arraySortPositions, formatBytes, addAiMarkerShortcut, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, countObj, checkObj, objType, ruleOfThree, getSimplePerc, asyncReplace, getAge, formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, shuffleArray, toTitleCase, toTitleCaseLowerFirst, };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A basic function that performs a task when the system is ready.
|
|
5
|
+
* Used for handlers in the readiness queue.
|
|
6
|
+
*
|
|
7
|
+
* @typedef {() => void} Fn
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A function that determines whether a specific handler should be executed.
|
|
12
|
+
* Should return `true` to allow execution, or `false` to skip the handler.
|
|
13
|
+
*
|
|
14
|
+
* @typedef {() => boolean} FnFilter
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {Object} Handler
|
|
19
|
+
* @property {Fn} fn - Function to execute when ready.
|
|
20
|
+
* @property {boolean} once - Whether to execute only once.
|
|
21
|
+
* @property {number} priority - Execution order (higher priority runs first).
|
|
22
|
+
* @property {FnFilter|null} filter - Optional filter function to determine execution.
|
|
23
|
+
* @property {boolean} domOnly - Whether to run as soon as DOM is ready (before full readiness).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
class TinyDomReadyManager {
|
|
27
|
+
/** @type {Handler[]} */
|
|
28
|
+
#handlers = [];
|
|
29
|
+
|
|
30
|
+
/** @type {boolean} */
|
|
31
|
+
#isDomReady = false;
|
|
32
|
+
|
|
33
|
+
/** @type {boolean} */
|
|
34
|
+
#isFullyReady = false;
|
|
35
|
+
|
|
36
|
+
/** @type {Promise<any>[]} */
|
|
37
|
+
#promises = [];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Checks if the DOM is ready and if all Promises have been resolved.
|
|
41
|
+
*/
|
|
42
|
+
#checkAllReady() {
|
|
43
|
+
if (this.#isDomReady) {
|
|
44
|
+
Promise.all(this.#promises)
|
|
45
|
+
.then(() => {
|
|
46
|
+
this.#isFullyReady = true;
|
|
47
|
+
this.#runHandlers(false); // run non-domOnly
|
|
48
|
+
})
|
|
49
|
+
.catch((err) => {
|
|
50
|
+
console.error('[TinyDomReadyManager] Promise rejected:', err);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Executes handlers by filtering them by `domOnly` flag and sorting by priority.
|
|
57
|
+
* @param {boolean} domOnlyOnly - Whether to run only `domOnly` handlers.
|
|
58
|
+
*/
|
|
59
|
+
#runHandlers(domOnlyOnly) {
|
|
60
|
+
this.#handlers
|
|
61
|
+
.filter((h) => h.domOnly === domOnlyOnly)
|
|
62
|
+
.sort((a, b) => b.priority - a.priority)
|
|
63
|
+
.forEach((handler) => this.#invokeHandler(handler));
|
|
64
|
+
|
|
65
|
+
this.#handlers = this.#handlers.filter((h) => !(h.once && (domOnlyOnly ? h.domOnly : true)));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Executes a handler if its filter passes.
|
|
70
|
+
* @param {Handler} handler
|
|
71
|
+
*/
|
|
72
|
+
#invokeHandler(handler) {
|
|
73
|
+
if (typeof handler.filter === 'function') {
|
|
74
|
+
try {
|
|
75
|
+
if (!handler.filter()) return;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.warn('[TinyDomReadyManager] Filter error:', err);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
handler.fn();
|
|
84
|
+
} catch (err) {
|
|
85
|
+
console.error('[TinyDomReadyManager] Handler error:', err);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Marks the system as DOM-ready and runs DOM-only handlers.
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
_markDomReady() {
|
|
94
|
+
this.#isDomReady = true;
|
|
95
|
+
this.#runHandlers(true); // Run domOnly
|
|
96
|
+
this.#checkAllReady(); // Then check for full readiness
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Initializes the manager using `DOMContentLoaded`.
|
|
101
|
+
*/
|
|
102
|
+
init() {
|
|
103
|
+
if (this.#isDomReady) throw new Error('[TinyDomReadyManager] init() has already been called.');
|
|
104
|
+
|
|
105
|
+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
106
|
+
this._markDomReady();
|
|
107
|
+
} else {
|
|
108
|
+
document.addEventListener('DOMContentLoaded', () => this._markDomReady());
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Adds a Promise to delay full readiness.
|
|
114
|
+
* @param {Promise<any>} promise
|
|
115
|
+
* @throws {TypeError}
|
|
116
|
+
*/
|
|
117
|
+
addPromise(promise) {
|
|
118
|
+
if (!(promise instanceof Promise))
|
|
119
|
+
throw new TypeError('[TinyDomReadyManager] promise must be a valid Promise.');
|
|
120
|
+
|
|
121
|
+
if (this.#isFullyReady) return;
|
|
122
|
+
this.#promises.push(promise);
|
|
123
|
+
if (this.#isDomReady) this.#checkAllReady();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Registers a handler to run either after DOM is ready or after full readiness.
|
|
128
|
+
*
|
|
129
|
+
* @param {Fn} fn - Function to execute.
|
|
130
|
+
* @param {Object} [options]
|
|
131
|
+
* @param {boolean} [options.once=true] - Execute only once.
|
|
132
|
+
* @param {number} [options.priority=0] - Higher priority runs first.
|
|
133
|
+
* @param {FnFilter|null} [options.filter=null] - Optional filter function.
|
|
134
|
+
* @param {boolean} [options.domOnly=false] - If true, executes after DOM ready only.
|
|
135
|
+
* @throws {TypeError} If fn is not a function.
|
|
136
|
+
*/
|
|
137
|
+
onReady(fn, { once = true, priority = 0, filter = null, domOnly = false } = {}) {
|
|
138
|
+
if (typeof fn !== 'function')
|
|
139
|
+
throw new TypeError('[TinyDomReadyManager] fn must be a function.');
|
|
140
|
+
|
|
141
|
+
const handler = { fn, once, priority, filter, domOnly };
|
|
142
|
+
|
|
143
|
+
if (domOnly && this.#isDomReady) {
|
|
144
|
+
this.#invokeHandler(handler);
|
|
145
|
+
if (!once) this.#handlers.push(handler);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!domOnly && this.#isFullyReady) {
|
|
150
|
+
this.#invokeHandler(handler);
|
|
151
|
+
} else {
|
|
152
|
+
this.#handlers.push(handler);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Returns whether the system is fully ready (DOM + Promises).
|
|
158
|
+
* @returns {boolean}
|
|
159
|
+
*/
|
|
160
|
+
isReady() {
|
|
161
|
+
return this.#isFullyReady;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Returns whether the DOM is ready (DOMContentLoaded has fired).
|
|
166
|
+
* Does not wait for promises.
|
|
167
|
+
* @returns {boolean}
|
|
168
|
+
*/
|
|
169
|
+
isDomReady() {
|
|
170
|
+
return this.#isDomReady;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports = TinyDomReadyManager;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
export default TinyDomReadyManager;
|
|
2
|
+
/**
|
|
3
|
+
* A basic function that performs a task when the system is ready.
|
|
4
|
+
* Used for handlers in the readiness queue.
|
|
5
|
+
*/
|
|
6
|
+
export type Fn = () => void;
|
|
7
|
+
/**
|
|
8
|
+
* A function that determines whether a specific handler should be executed.
|
|
9
|
+
* Should return `true` to allow execution, or `false` to skip the handler.
|
|
10
|
+
*/
|
|
11
|
+
export type FnFilter = () => boolean;
|
|
12
|
+
export type Handler = {
|
|
13
|
+
/**
|
|
14
|
+
* - Function to execute when ready.
|
|
15
|
+
*/
|
|
16
|
+
fn: Fn;
|
|
17
|
+
/**
|
|
18
|
+
* - Whether to execute only once.
|
|
19
|
+
*/
|
|
20
|
+
once: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* - Execution order (higher priority runs first).
|
|
23
|
+
*/
|
|
24
|
+
priority: number;
|
|
25
|
+
/**
|
|
26
|
+
* - Optional filter function to determine execution.
|
|
27
|
+
*/
|
|
28
|
+
filter: FnFilter | null;
|
|
29
|
+
/**
|
|
30
|
+
* - Whether to run as soon as DOM is ready (before full readiness).
|
|
31
|
+
*/
|
|
32
|
+
domOnly: boolean;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* A basic function that performs a task when the system is ready.
|
|
36
|
+
* Used for handlers in the readiness queue.
|
|
37
|
+
*
|
|
38
|
+
* @typedef {() => void} Fn
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* A function that determines whether a specific handler should be executed.
|
|
42
|
+
* Should return `true` to allow execution, or `false` to skip the handler.
|
|
43
|
+
*
|
|
44
|
+
* @typedef {() => boolean} FnFilter
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {Object} Handler
|
|
48
|
+
* @property {Fn} fn - Function to execute when ready.
|
|
49
|
+
* @property {boolean} once - Whether to execute only once.
|
|
50
|
+
* @property {number} priority - Execution order (higher priority runs first).
|
|
51
|
+
* @property {FnFilter|null} filter - Optional filter function to determine execution.
|
|
52
|
+
* @property {boolean} domOnly - Whether to run as soon as DOM is ready (before full readiness).
|
|
53
|
+
*/
|
|
54
|
+
declare class TinyDomReadyManager {
|
|
55
|
+
/**
|
|
56
|
+
* Marks the system as DOM-ready and runs DOM-only handlers.
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
private _markDomReady;
|
|
60
|
+
/**
|
|
61
|
+
* Initializes the manager using `DOMContentLoaded`.
|
|
62
|
+
*/
|
|
63
|
+
init(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Adds a Promise to delay full readiness.
|
|
66
|
+
* @param {Promise<any>} promise
|
|
67
|
+
* @throws {TypeError}
|
|
68
|
+
*/
|
|
69
|
+
addPromise(promise: Promise<any>): void;
|
|
70
|
+
/**
|
|
71
|
+
* Registers a handler to run either after DOM is ready or after full readiness.
|
|
72
|
+
*
|
|
73
|
+
* @param {Fn} fn - Function to execute.
|
|
74
|
+
* @param {Object} [options]
|
|
75
|
+
* @param {boolean} [options.once=true] - Execute only once.
|
|
76
|
+
* @param {number} [options.priority=0] - Higher priority runs first.
|
|
77
|
+
* @param {FnFilter|null} [options.filter=null] - Optional filter function.
|
|
78
|
+
* @param {boolean} [options.domOnly=false] - If true, executes after DOM ready only.
|
|
79
|
+
* @throws {TypeError} If fn is not a function.
|
|
80
|
+
*/
|
|
81
|
+
onReady(fn: Fn, { once, priority, filter, domOnly }?: {
|
|
82
|
+
once?: boolean | undefined;
|
|
83
|
+
priority?: number | undefined;
|
|
84
|
+
filter?: FnFilter | null | undefined;
|
|
85
|
+
domOnly?: boolean | undefined;
|
|
86
|
+
}): void;
|
|
87
|
+
/**
|
|
88
|
+
* Returns whether the system is fully ready (DOM + Promises).
|
|
89
|
+
* @returns {boolean}
|
|
90
|
+
*/
|
|
91
|
+
isReady(): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Returns whether the DOM is ready (DOMContentLoaded has fired).
|
|
94
|
+
* Does not wait for promises.
|
|
95
|
+
* @returns {boolean}
|
|
96
|
+
*/
|
|
97
|
+
isDomReady(): boolean;
|
|
98
|
+
#private;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=TinyDomReadyManager.d.mts.map
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A basic function that performs a task when the system is ready.
|
|
3
|
+
* Used for handlers in the readiness queue.
|
|
4
|
+
*
|
|
5
|
+
* @typedef {() => void} Fn
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* A function that determines whether a specific handler should be executed.
|
|
9
|
+
* Should return `true` to allow execution, or `false` to skip the handler.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {() => boolean} FnFilter
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} Handler
|
|
15
|
+
* @property {Fn} fn - Function to execute when ready.
|
|
16
|
+
* @property {boolean} once - Whether to execute only once.
|
|
17
|
+
* @property {number} priority - Execution order (higher priority runs first).
|
|
18
|
+
* @property {FnFilter|null} filter - Optional filter function to determine execution.
|
|
19
|
+
* @property {boolean} domOnly - Whether to run as soon as DOM is ready (before full readiness).
|
|
20
|
+
*/
|
|
21
|
+
class TinyDomReadyManager {
|
|
22
|
+
/** @type {Handler[]} */
|
|
23
|
+
#handlers = [];
|
|
24
|
+
/** @type {boolean} */
|
|
25
|
+
#isDomReady = false;
|
|
26
|
+
/** @type {boolean} */
|
|
27
|
+
#isFullyReady = false;
|
|
28
|
+
/** @type {Promise<any>[]} */
|
|
29
|
+
#promises = [];
|
|
30
|
+
/**
|
|
31
|
+
* Checks if the DOM is ready and if all Promises have been resolved.
|
|
32
|
+
*/
|
|
33
|
+
#checkAllReady() {
|
|
34
|
+
if (this.#isDomReady) {
|
|
35
|
+
Promise.all(this.#promises)
|
|
36
|
+
.then(() => {
|
|
37
|
+
this.#isFullyReady = true;
|
|
38
|
+
this.#runHandlers(false); // run non-domOnly
|
|
39
|
+
})
|
|
40
|
+
.catch((err) => {
|
|
41
|
+
console.error('[TinyDomReadyManager] Promise rejected:', err);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Executes handlers by filtering them by `domOnly` flag and sorting by priority.
|
|
47
|
+
* @param {boolean} domOnlyOnly - Whether to run only `domOnly` handlers.
|
|
48
|
+
*/
|
|
49
|
+
#runHandlers(domOnlyOnly) {
|
|
50
|
+
this.#handlers
|
|
51
|
+
.filter((h) => h.domOnly === domOnlyOnly)
|
|
52
|
+
.sort((a, b) => b.priority - a.priority)
|
|
53
|
+
.forEach((handler) => this.#invokeHandler(handler));
|
|
54
|
+
this.#handlers = this.#handlers.filter((h) => !(h.once && (domOnlyOnly ? h.domOnly : true)));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Executes a handler if its filter passes.
|
|
58
|
+
* @param {Handler} handler
|
|
59
|
+
*/
|
|
60
|
+
#invokeHandler(handler) {
|
|
61
|
+
if (typeof handler.filter === 'function') {
|
|
62
|
+
try {
|
|
63
|
+
if (!handler.filter())
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
console.warn('[TinyDomReadyManager] Filter error:', err);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
handler.fn();
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
console.error('[TinyDomReadyManager] Handler error:', err);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Marks the system as DOM-ready and runs DOM-only handlers.
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
_markDomReady() {
|
|
83
|
+
this.#isDomReady = true;
|
|
84
|
+
this.#runHandlers(true); // Run domOnly
|
|
85
|
+
this.#checkAllReady(); // Then check for full readiness
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Initializes the manager using `DOMContentLoaded`.
|
|
89
|
+
*/
|
|
90
|
+
init() {
|
|
91
|
+
if (this.#isDomReady)
|
|
92
|
+
throw new Error('[TinyDomReadyManager] init() has already been called.');
|
|
93
|
+
if (document.readyState === 'interactive' || document.readyState === 'complete') {
|
|
94
|
+
this._markDomReady();
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
document.addEventListener('DOMContentLoaded', () => this._markDomReady());
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Adds a Promise to delay full readiness.
|
|
102
|
+
* @param {Promise<any>} promise
|
|
103
|
+
* @throws {TypeError}
|
|
104
|
+
*/
|
|
105
|
+
addPromise(promise) {
|
|
106
|
+
if (!(promise instanceof Promise))
|
|
107
|
+
throw new TypeError('[TinyDomReadyManager] promise must be a valid Promise.');
|
|
108
|
+
if (this.#isFullyReady)
|
|
109
|
+
return;
|
|
110
|
+
this.#promises.push(promise);
|
|
111
|
+
if (this.#isDomReady)
|
|
112
|
+
this.#checkAllReady();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Registers a handler to run either after DOM is ready or after full readiness.
|
|
116
|
+
*
|
|
117
|
+
* @param {Fn} fn - Function to execute.
|
|
118
|
+
* @param {Object} [options]
|
|
119
|
+
* @param {boolean} [options.once=true] - Execute only once.
|
|
120
|
+
* @param {number} [options.priority=0] - Higher priority runs first.
|
|
121
|
+
* @param {FnFilter|null} [options.filter=null] - Optional filter function.
|
|
122
|
+
* @param {boolean} [options.domOnly=false] - If true, executes after DOM ready only.
|
|
123
|
+
* @throws {TypeError} If fn is not a function.
|
|
124
|
+
*/
|
|
125
|
+
onReady(fn, { once = true, priority = 0, filter = null, domOnly = false } = {}) {
|
|
126
|
+
if (typeof fn !== 'function')
|
|
127
|
+
throw new TypeError('[TinyDomReadyManager] fn must be a function.');
|
|
128
|
+
const handler = { fn, once, priority, filter, domOnly };
|
|
129
|
+
if (domOnly && this.#isDomReady) {
|
|
130
|
+
this.#invokeHandler(handler);
|
|
131
|
+
if (!once)
|
|
132
|
+
this.#handlers.push(handler);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!domOnly && this.#isFullyReady) {
|
|
136
|
+
this.#invokeHandler(handler);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
this.#handlers.push(handler);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns whether the system is fully ready (DOM + Promises).
|
|
144
|
+
* @returns {boolean}
|
|
145
|
+
*/
|
|
146
|
+
isReady() {
|
|
147
|
+
return this.#isFullyReady;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns whether the DOM is ready (DOMContentLoaded has fired).
|
|
151
|
+
* Does not wait for promises.
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
isDomReady() {
|
|
155
|
+
return this.#isDomReady;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export default TinyDomReadyManager;
|
package/docs/v1/README.md
CHANGED
|
@@ -31,6 +31,7 @@ This folder contains the core scripts we have worked on so far. Each file is a m
|
|
|
31
31
|
- ๐ฅ **[TinyDragDropDetector](./libs/TinyDragDropDetector.md)** โ A lightweight drag-and-drop detector for files, handling the full drag lifecycle (`enter`, `over`, `leave`, `drop`) with CSS hover management and safe event handling on any DOM element or the full page.
|
|
32
32
|
* ๐ **[TinyUploadClicker](./libs/TinyUploadClicker.md)** โ A minimal utility to bind any clickable element to a hidden file input, offering full control over styling, behavior, and upload event hooks.
|
|
33
33
|
* ๐งฒ **[TinyDragger](./libs/TinyDragger.md)** โ A flexible drag-and-drop manager with collision detection, jail constraints, vibration feedback, visual proxies, revert-on-drop, and full custom event support.
|
|
34
|
+
* ๐ **[TinyDomReadyManager](./libs/TinyDomReadyManager.md)** โ A readiness manager for DOM and async conditions, supporting prioritized callbacks, custom filters, and event-based or promise-based bootstrapping.
|
|
34
35
|
|
|
35
36
|
### 3. **`fileManager/`**
|
|
36
37
|
* ๐ **[main](./fileManager/main.md)** โ A Node.js file/directory utility module with support for JSON, backups, renaming, size analysis, and more.
|
|
@@ -119,3 +119,31 @@ formatBytes(1073741824, 2, 'MB');
|
|
|
119
119
|
* **Formatting:** Converts bytes to the most appropriate unit (from 'Bytes' to 'YB') based on the byte value.
|
|
120
120
|
* **Max Unit:** The `maxUnit` parameter allows you to limit the highest unit for conversion. If not provided, it will convert all the way up to 'YB'.
|
|
121
121
|
* **Decimals:** The result can be customized with a specified number of decimal places for precision. If `decimals` is `null`, no rounding is applied, and the full precision value is returned.
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
### ๐ข `genFibonacciSeq()` โ Custom Fibonacci Sequence Generator
|
|
126
|
+
|
|
127
|
+
๐ Flexible, combinable, and vector-friendly!
|
|
128
|
+
|
|
129
|
+
Generates a customizable Fibonacci-like sequence. You can use simple numbers or even 2D/ND vectors by passing a custom `combiner` function.
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
genFibonacciSeq({
|
|
133
|
+
baseValues: [[0, 1], [1, 1]],
|
|
134
|
+
length: 10,
|
|
135
|
+
combiner: ([x1, y1], [x2, y2]) => [x1 + x2, y1 + y2]
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
#### โ๏ธ Options
|
|
140
|
+
|
|
141
|
+
* `baseValues` ๐งฌ: Two starting values (default: `[0, 1]`)
|
|
142
|
+
* `length` ๐: How many items to generate (default: `10`)
|
|
143
|
+
* `combiner` ๐งช: Custom function to generate next value from the last two (default: `(a, b) => a + b`)
|
|
144
|
+
|
|
145
|
+
#### ๐ง Returns
|
|
146
|
+
|
|
147
|
+
An array representing the generated sequence.
|
|
148
|
+
|
|
149
|
+
> ๐ **Beta** โ Still experimental and fun to tweak!
|