@wexample/js-helpers 0.0.26 → 0.0.34
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 -128
- package/package.json +14 -6
- package/src/Common/AsyncConstructor.ts +47 -0
- package/src/Common/RetryBackoffScheduler.ts +64 -0
- package/src/Helper/AbstractMixin.ts +18 -0
- package/src/Helper/Animation.ts +47 -0
- package/src/Helper/Array.ts +30 -0
- package/src/Helper/Bytes.ts +12 -0
- package/src/Helper/Dom.ts +142 -0
- package/src/Helper/ElementSize.ts +87 -0
- package/src/Helper/Event.ts +30 -0
- package/src/Helper/Function.ts +5 -0
- package/src/Helper/Height.ts +20 -0
- package/src/Helper/Id.ts +3 -0
- package/src/Helper/KeyCode.ts +5 -0
- package/src/Helper/Location.ts +65 -0
- package/src/Helper/Log.ts +41 -0
- package/src/Helper/Mixin.ts +33 -0
- package/src/Helper/NodeEnv.ts +46 -0
- package/src/Helper/NodeFs.ts +37 -0
- package/src/Helper/NodePath.ts +23 -0
- package/src/Helper/Object.ts +124 -0
- package/src/Helper/Pointer.ts +3 -0
- package/src/Helper/Queue.ts +112 -0
- package/src/Helper/Reconnect.ts +170 -0
- package/src/Helper/Serialize.ts +69 -0
- package/src/Helper/String.ts +316 -0
- package/src/Helper/Time.ts +5 -0
- package/src/Helper/Transition.ts +50 -0
- package/src/Helper/Url.ts +23 -0
- package/src/Helper/Variables.ts +13 -0
- package/dist/Common/AsyncConstructor.js +0 -42
- package/dist/Common/AsyncConstructor.js.map +0 -1
- package/dist/Helper/Array.js +0 -25
- package/dist/Helper/Array.js.map +0 -1
- package/dist/Helper/Bytes.js +0 -11
- package/dist/Helper/Bytes.js.map +0 -1
- package/dist/Helper/Dom.js +0 -74
- package/dist/Helper/Dom.js.map +0 -1
- package/dist/Helper/Event.js +0 -29
- package/dist/Helper/Event.js.map +0 -1
- package/dist/Helper/Function.js +0 -4
- package/dist/Helper/Function.js.map +0 -1
- package/dist/Helper/KeyCode.js +0 -4
- package/dist/Helper/KeyCode.js.map +0 -1
- package/dist/Helper/Location.js +0 -49
- package/dist/Helper/Location.js.map +0 -1
- package/dist/Helper/Log.js +0 -27
- package/dist/Helper/Log.js.map +0 -1
- package/dist/Helper/Mixin.js +0 -29
- package/dist/Helper/Mixin.js.map +0 -1
- package/dist/Helper/Object.js +0 -90
- package/dist/Helper/Object.js.map +0 -1
- package/dist/Helper/Pointer.js +0 -4
- package/dist/Helper/Pointer.js.map +0 -1
- package/dist/Helper/Queue.js +0 -80
- package/dist/Helper/Queue.js.map +0 -1
- package/dist/Helper/String.js +0 -261
- package/dist/Helper/String.js.map +0 -1
- package/dist/Helper/Time.js +0 -6
- package/dist/Helper/Time.js.map +0 -1
- package/dist/Helper/Url.js +0 -17
- package/dist/Helper/Url.js.map +0 -1
- package/dist/Helper/Variables.js +0 -11
- package/dist/Helper/Variables.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,137 +1,10 @@
|
|
|
1
1
|
# @wexample/js-helpers
|
|
2
2
|
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.34
|
|
4
4
|
|
|
5
5
|
Modern, type-safe helper utilities for Wexample projects.
|
|
6
6
|
|
|
7
7
|
## Table of Contents
|
|
8
8
|
|
|
9
|
-
- [Api Reference](#api-reference)
|
|
10
|
-
- [Code Quality](#code-quality)
|
|
11
|
-
- [Versioning](#versioning)
|
|
12
|
-
- [Changelog](#changelog)
|
|
13
|
-
- [Migration Notes](#migration-notes)
|
|
14
|
-
- [Security](#security)
|
|
15
|
-
- [Privacy](#privacy)
|
|
16
|
-
- [Support](#support)
|
|
17
|
-
- [Contribution Guidelines](#contribution-guidelines)
|
|
18
|
-
- [Maintainers](#maintainers)
|
|
19
|
-
- [License](#license)
|
|
20
|
-
- [Suite Integration](#suite-integration)
|
|
21
|
-
- [Compatibility Matrix](#compatibility-matrix)
|
|
22
|
-
- [Dependencies](#dependencies)
|
|
23
|
-
- [Suite Signature](#suite-signature)
|
|
24
9
|
|
|
25
10
|
|
|
26
|
-
## API Reference
|
|
27
|
-
|
|
28
|
-
Full API documentation is available in the source code docstrings.
|
|
29
|
-
|
|
30
|
-
Key modules and classes are documented with type hints for better IDE support.
|
|
31
|
-
|
|
32
|
-
## Code Quality & Typing
|
|
33
|
-
|
|
34
|
-
All the suite packages follow strict quality standards:
|
|
35
|
-
|
|
36
|
-
- **Type hints**: Full type coverage with mypy validation
|
|
37
|
-
- **Code formatting**: Enforced with black and isort
|
|
38
|
-
- **Linting**: Comprehensive checks with custom scripts and tools
|
|
39
|
-
- **Testing**: High test coverage requirements
|
|
40
|
-
|
|
41
|
-
These standards ensure reliability and maintainability across the suite.
|
|
42
|
-
|
|
43
|
-
## Versioning & Compatibility Policy
|
|
44
|
-
|
|
45
|
-
Wexample packages follow **Semantic Versioning** (SemVer):
|
|
46
|
-
|
|
47
|
-
- **MAJOR**: Breaking changes
|
|
48
|
-
- **MINOR**: New features, backward compatible
|
|
49
|
-
- **PATCH**: Bug fixes, backward compatible
|
|
50
|
-
|
|
51
|
-
We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
|
|
52
|
-
|
|
53
|
-
## Changelog
|
|
54
|
-
|
|
55
|
-
See [CHANGELOG.md](CHANGELOG.md) for detailed version history and release notes.
|
|
56
|
-
|
|
57
|
-
Major changes are documented with migration guides when applicable.
|
|
58
|
-
|
|
59
|
-
## Migration Notes
|
|
60
|
-
|
|
61
|
-
When upgrading between major versions, refer to the migration guides in the documentation.
|
|
62
|
-
|
|
63
|
-
Breaking changes are clearly documented with upgrade paths and examples.
|
|
64
|
-
|
|
65
|
-
## Security Policy
|
|
66
|
-
|
|
67
|
-
### Reporting Vulnerabilities
|
|
68
|
-
|
|
69
|
-
If you discover a security vulnerability, please email contact@wexample.com.
|
|
70
|
-
|
|
71
|
-
**Do not** open public issues for security vulnerabilities.
|
|
72
|
-
|
|
73
|
-
We take security seriously and will respond promptly to verified reports.
|
|
74
|
-
|
|
75
|
-
## Privacy & Telemetry
|
|
76
|
-
|
|
77
|
-
This package does **not** collect any telemetry or usage data.
|
|
78
|
-
|
|
79
|
-
Your privacy is respected — no data is transmitted to external services.
|
|
80
|
-
|
|
81
|
-
## Support Channels
|
|
82
|
-
|
|
83
|
-
- **GitHub Issues**: Bug reports and feature requests
|
|
84
|
-
- **GitHub Discussions**: Questions and community support
|
|
85
|
-
- **Documentation**: Comprehensive guides and API reference
|
|
86
|
-
- **Email**: contact@wexample.com for general inquiries
|
|
87
|
-
|
|
88
|
-
Community support is available through GitHub Discussions.
|
|
89
|
-
|
|
90
|
-
## Contribution Guidelines
|
|
91
|
-
|
|
92
|
-
We welcome contributions to the Wexample suite!
|
|
93
|
-
|
|
94
|
-
### How to Contribute
|
|
95
|
-
|
|
96
|
-
1. **Fork** the repository
|
|
97
|
-
2. **Create** a feature branch
|
|
98
|
-
3. **Make** your changes
|
|
99
|
-
4. **Test** thoroughly
|
|
100
|
-
5. **Submit** a pull request
|
|
101
|
-
|
|
102
|
-
## Maintainers & Authors
|
|
103
|
-
|
|
104
|
-
Maintained by the Wexample team and community contributors.
|
|
105
|
-
|
|
106
|
-
See [CONTRIBUTORS.md](CONTRIBUTORS.md) for the full list of contributors.
|
|
107
|
-
|
|
108
|
-
## License
|
|
109
|
-
|
|
110
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
111
|
-
|
|
112
|
-
Free to use in both personal and commercial projects.
|
|
113
|
-
|
|
114
|
-
## Integration in the Suite
|
|
115
|
-
|
|
116
|
-
This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
|
|
117
|
-
|
|
118
|
-
### Related Packages
|
|
119
|
-
|
|
120
|
-
The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
|
|
121
|
-
|
|
122
|
-
Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
|
|
123
|
-
|
|
124
|
-
## Compatibility Matrix
|
|
125
|
-
|
|
126
|
-
This package is part of the Wexample suite and is compatible with other suite packages.
|
|
127
|
-
|
|
128
|
-
Refer to each package's documentation for specific version compatibility requirements.
|
|
129
|
-
|
|
130
|
-
# About us
|
|
131
|
-
|
|
132
|
-
[Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
|
|
133
|
-
|
|
134
|
-
This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
|
|
135
|
-
|
|
136
|
-
Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
|
|
137
|
-
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wexample/js-helpers",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.34",
|
|
4
4
|
"description": "Modern, type-safe helper utilities for Wexample projects.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"build": "tsc --noEmit",
|
|
10
|
+
"typecheck": "tsc --noEmit",
|
|
11
11
|
"lint": "npm run typecheck",
|
|
12
12
|
"prepublishOnly": "npm run build"
|
|
13
13
|
},
|
|
@@ -24,17 +24,25 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"@types/node": "^22.0.0",
|
|
27
28
|
"tsup": "^8.5.1",
|
|
28
29
|
"typescript": "^5.9.3"
|
|
29
30
|
},
|
|
30
31
|
"repository": "https://github.com/wexample/js-helpers",
|
|
31
32
|
"files": [
|
|
32
|
-
"
|
|
33
|
+
"src"
|
|
33
34
|
],
|
|
34
35
|
"exports": {
|
|
35
36
|
"./*": {
|
|
36
|
-
"types": "./
|
|
37
|
-
"default": "./
|
|
37
|
+
"types": "./src/*.ts",
|
|
38
|
+
"default": "./src/*.ts"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"typesVersions": {
|
|
42
|
+
"*": {
|
|
43
|
+
"*": [
|
|
44
|
+
"src/*"
|
|
45
|
+
]
|
|
38
46
|
}
|
|
39
47
|
}
|
|
40
48
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { functionIsType } from '../Helper/Function.js';
|
|
2
|
+
|
|
3
|
+
type AnyFn<TThis, TArgs extends unknown[]> = (this: TThis, ...args: TArgs) => void | Promise<void>;
|
|
4
|
+
|
|
5
|
+
export default abstract class AsyncConstructor<TArgs extends unknown[] = unknown[]> {
|
|
6
|
+
private _isReady = false;
|
|
7
|
+
private _readyArgs: TArgs | null = null;
|
|
8
|
+
private readonly readyCallbacks: Array<AnyFn<this, TArgs>> = [];
|
|
9
|
+
|
|
10
|
+
get isReady(): boolean {
|
|
11
|
+
return this._isReady;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected defer(fn: () => void): void {
|
|
15
|
+
if (typeof queueMicrotask === 'function') queueMicrotask(fn);
|
|
16
|
+
else setTimeout(fn, 0);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
ready(cb: AnyFn<this, TArgs>): void {
|
|
20
|
+
if (this._isReady) {
|
|
21
|
+
const args = this._readyArgs ?? ([] as unknown as TArgs);
|
|
22
|
+
this.defer(() => void cb.apply(this, args));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
this.readyCallbacks.push(cb);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected async readyComplete(...args: TArgs): Promise<void> {
|
|
29
|
+
if (this._isReady) return;
|
|
30
|
+
|
|
31
|
+
this._isReady = true;
|
|
32
|
+
this._readyArgs = args;
|
|
33
|
+
|
|
34
|
+
const callbacks = this.readyCallbacks.splice(0);
|
|
35
|
+
|
|
36
|
+
for (const cb of callbacks) {
|
|
37
|
+
if (!functionIsType(cb)) {
|
|
38
|
+
throw new TypeError('Ready callback must be a function.');
|
|
39
|
+
}
|
|
40
|
+
await cb.apply(this, args);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected seal(): void {
|
|
45
|
+
Object.seal(this);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ReconnectBackoffController,
|
|
3
|
+
type ReconnectBackoffOptions,
|
|
4
|
+
reconnectBackoffCreateController,
|
|
5
|
+
} from '../Helper/Reconnect';
|
|
6
|
+
|
|
7
|
+
export type RetryBackoffScheduleContext = {
|
|
8
|
+
attempt: number;
|
|
9
|
+
delayMs: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default class RetryBackoffScheduler {
|
|
13
|
+
private readonly controller: ReconnectBackoffController;
|
|
14
|
+
private timeout: ReturnType<typeof setTimeout> | null = null;
|
|
15
|
+
|
|
16
|
+
constructor(options: ReconnectBackoffOptions = {}) {
|
|
17
|
+
this.controller = reconnectBackoffCreateController(options);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get attempt(): number {
|
|
21
|
+
return this.controller.getAttempt();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get isScheduled(): boolean {
|
|
25
|
+
return this.timeout !== null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
canRetry(): boolean {
|
|
29
|
+
return this.controller.canRetry();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
reset(): void {
|
|
33
|
+
this.cancel();
|
|
34
|
+
this.controller.reset();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
cancel(): void {
|
|
38
|
+
if (!this.timeout) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
clearTimeout(this.timeout);
|
|
43
|
+
this.timeout = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
schedule(run: () => void): RetryBackoffScheduleContext | null {
|
|
47
|
+
if (this.timeout || !this.controller.canRetry()) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const delayMs = this.controller.nextDelay();
|
|
52
|
+
const context: RetryBackoffScheduleContext = {
|
|
53
|
+
attempt: this.controller.getAttempt(),
|
|
54
|
+
delayMs,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
this.timeout = setTimeout(() => {
|
|
58
|
+
this.timeout = null;
|
|
59
|
+
run();
|
|
60
|
+
}, delayMs);
|
|
61
|
+
|
|
62
|
+
return context;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default abstract class AbstractMixin {
|
|
2
|
+
protected static applyOnce(
|
|
3
|
+
instance: any,
|
|
4
|
+
apply: (instance: any) => void,
|
|
5
|
+
flag?: string
|
|
6
|
+
): boolean {
|
|
7
|
+
const key = flag || `__mixin_${AbstractMixin.name}`;
|
|
8
|
+
|
|
9
|
+
if (instance[key]) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
instance[key] = true;
|
|
14
|
+
apply(instance);
|
|
15
|
+
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export const waitForAnimationEnd = (
|
|
2
|
+
el: HTMLElement | null,
|
|
3
|
+
softTimeoutMs = 220,
|
|
4
|
+
hardTimeoutMs = 10000
|
|
5
|
+
): Promise<void> => {
|
|
6
|
+
if (!el) {
|
|
7
|
+
return Promise.resolve();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
let started = false;
|
|
12
|
+
let resolved = false;
|
|
13
|
+
|
|
14
|
+
const clear = () => {
|
|
15
|
+
el.removeEventListener('animationstart', onStart);
|
|
16
|
+
el.removeEventListener('animationend', onEnd);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const finish = () => {
|
|
20
|
+
if (resolved) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
resolved = true;
|
|
24
|
+
clear();
|
|
25
|
+
resolve();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const onStart = () => {
|
|
29
|
+
started = true;
|
|
30
|
+
window.setTimeout(finish, hardTimeoutMs);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const onEnd = () => {
|
|
34
|
+
finish();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
el.addEventListener('animationstart', onStart, { once: true });
|
|
38
|
+
el.addEventListener('animationend', onEnd, { once: true });
|
|
39
|
+
|
|
40
|
+
// Soft timeout if no animation starts.
|
|
41
|
+
window.setTimeout(() => {
|
|
42
|
+
if (!started) {
|
|
43
|
+
finish();
|
|
44
|
+
}
|
|
45
|
+
}, softTimeoutMs);
|
|
46
|
+
});
|
|
47
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function arrayDeleteItem<T>(haystack: T[], needle: T): T[] {
|
|
2
|
+
return arrayDeleteByIndex(haystack, haystack.indexOf(needle));
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function arrayDeleteByIndex<T>(haystack: T[], index: number): T[] {
|
|
6
|
+
if (index !== -1) {
|
|
7
|
+
haystack.splice(index, 1);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return haystack;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Functions "arguments" object may be transformed to real array for extra manipulations.
|
|
15
|
+
*/
|
|
16
|
+
export function arrayFromArguments<T>(args: ArrayLike<T>): T[] {
|
|
17
|
+
return Array.prototype.slice.call(args);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function arrayShallowCopy<T>(array: T[]): T[] {
|
|
21
|
+
return array.slice(0);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function arrayUnique<T>(array: T[]): T[] {
|
|
25
|
+
return array.filter((value, index) => array.indexOf(value) === index);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function arrayFindByIndex<T>(array: T[], position: number): T | undefined {
|
|
29
|
+
return array[position >= 0 ? position : array.length + position];
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function bytesFormatBytes(bytes: number, decimals = 2): string {
|
|
2
|
+
if (bytes === 0) {
|
|
3
|
+
return '0 Bytes';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const k = 1024;
|
|
7
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
8
|
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
9
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
10
|
+
|
|
11
|
+
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
|
12
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
export const DOM_ATTRIBUTE = {
|
|
2
|
+
HREF: 'href',
|
|
3
|
+
ID: 'id',
|
|
4
|
+
REL: 'rel',
|
|
5
|
+
SRC: 'src',
|
|
6
|
+
} as const;
|
|
7
|
+
|
|
8
|
+
export const DOM_ATTRIBUTE_VALUE = {
|
|
9
|
+
STYLESHEET: 'stylesheet',
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
export const DOM_INSERT_POSITION = {
|
|
13
|
+
BEFORE_END: 'beforeend' as InsertPosition,
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
export const DOM_TAG_NAME = {
|
|
17
|
+
A: 'a',
|
|
18
|
+
DIV: 'div',
|
|
19
|
+
LINK: 'link',
|
|
20
|
+
SCRIPT: 'script',
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
export type DomScrollToBottomOptions = {
|
|
24
|
+
container?: HTMLElement | null;
|
|
25
|
+
behavior?: ScrollBehavior;
|
|
26
|
+
retries?: number;
|
|
27
|
+
includeHidden?: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function domAppendInnerHtml(el: HTMLElement, html: string): void {
|
|
31
|
+
el.insertAdjacentHTML(DOM_INSERT_POSITION.BEFORE_END, html);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function domFindPreviousNode(el: HTMLElement): HTMLElement | null {
|
|
35
|
+
let current: ChildNode | null = el;
|
|
36
|
+
|
|
37
|
+
do {
|
|
38
|
+
current = current?.previousSibling ?? null;
|
|
39
|
+
} while (current && current.nodeType === Node.TEXT_NODE);
|
|
40
|
+
|
|
41
|
+
return current as HTMLElement | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Return first scrollable parent.
|
|
46
|
+
*
|
|
47
|
+
* @see https://stackoverflow.com/a/42543908/2057976
|
|
48
|
+
*/
|
|
49
|
+
export function domFindScrollParent(element: HTMLElement, includeHidden = false): HTMLElement {
|
|
50
|
+
const style = getComputedStyle(element);
|
|
51
|
+
const overflowRegex = includeHidden ? /(auto|scroll|hidden)/ : /(auto|scroll)/;
|
|
52
|
+
const excludeStaticParent = style.position === 'absolute';
|
|
53
|
+
|
|
54
|
+
if (style.position === 'fixed') {
|
|
55
|
+
return document.body;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let parent: HTMLElement | null = element.parentElement;
|
|
59
|
+
while (parent) {
|
|
60
|
+
const parentStyle = getComputedStyle(parent);
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
(!excludeStaticParent || parentStyle.position !== 'static') &&
|
|
64
|
+
overflowRegex.test(parentStyle.overflow + parentStyle.overflowY + parentStyle.overflowX)
|
|
65
|
+
) {
|
|
66
|
+
return parent;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
parent = parent.parentElement;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return document.body;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function domToggleMainOverlay(visible: boolean | null = null): void {
|
|
76
|
+
const overlay = document.getElementById('main-overlay');
|
|
77
|
+
if (!overlay) return;
|
|
78
|
+
|
|
79
|
+
const classList = overlay.classList;
|
|
80
|
+
const shouldShow = visible !== null ? visible : !classList.contains('visible');
|
|
81
|
+
|
|
82
|
+
classList[shouldShow ? 'add' : 'remove']('visible');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function domCreateHtmlDocumentFromHtml(html: string): HTMLHtmlElement {
|
|
86
|
+
const root = document.createElement('html');
|
|
87
|
+
root.innerHTML = html;
|
|
88
|
+
return root;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function domRemoveAllClasses(el: HTMLElement, classesToRemove: Iterable<string>): void {
|
|
92
|
+
for (const className of classesToRemove) {
|
|
93
|
+
el.classList.remove(className);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function domReplaceByOneClass(
|
|
98
|
+
el: HTMLElement,
|
|
99
|
+
newState: string,
|
|
100
|
+
classesToRemove: Iterable<string>
|
|
101
|
+
): void {
|
|
102
|
+
domRemoveAllClasses(el, classesToRemove);
|
|
103
|
+
el.classList.add(newState);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function domScrollToBottom(
|
|
107
|
+
anchorElement: HTMLElement | null,
|
|
108
|
+
options: DomScrollToBottomOptions = {}
|
|
109
|
+
): void {
|
|
110
|
+
if (!anchorElement) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const { container = null, behavior = 'auto', retries = 0, includeHidden = false } = options;
|
|
115
|
+
|
|
116
|
+
const scrollContainer = container ?? domFindScrollParent(anchorElement, includeHidden);
|
|
117
|
+
if (!scrollContainer) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const targetTop = scrollContainer.scrollHeight;
|
|
122
|
+
|
|
123
|
+
if (typeof scrollContainer.scrollTo === 'function') {
|
|
124
|
+
scrollContainer.scrollTo({
|
|
125
|
+
top: targetTop,
|
|
126
|
+
behavior,
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
scrollContainer.scrollTop = targetTop;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (retries > 0) {
|
|
133
|
+
window.requestAnimationFrame(() => {
|
|
134
|
+
domScrollToBottom(anchorElement, {
|
|
135
|
+
container: scrollContainer,
|
|
136
|
+
behavior,
|
|
137
|
+
retries: retries - 1,
|
|
138
|
+
includeHidden,
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export const waitForElementSize = (
|
|
2
|
+
el: HTMLElement | null,
|
|
3
|
+
minHeight = 1,
|
|
4
|
+
timeoutMs = 500,
|
|
5
|
+
ready?: (el: HTMLElement) => boolean
|
|
6
|
+
): Promise<void> => {
|
|
7
|
+
if (!el) {
|
|
8
|
+
return Promise.resolve();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const hasSize = () => (ready ? ready(el) : el.getBoundingClientRect().height >= minHeight);
|
|
12
|
+
|
|
13
|
+
if (hasSize()) {
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const runRafProbe = () =>
|
|
18
|
+
new Promise<void>((resolve) => {
|
|
19
|
+
let attempts = 0;
|
|
20
|
+
const tick = () => {
|
|
21
|
+
if (hasSize() || attempts >= 8) {
|
|
22
|
+
resolve();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
attempts += 1;
|
|
26
|
+
requestAnimationFrame(tick);
|
|
27
|
+
};
|
|
28
|
+
requestAnimationFrame(tick);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const runResizeObserver = () =>
|
|
32
|
+
new Promise<void>((resolve) => {
|
|
33
|
+
if (typeof ResizeObserver === 'undefined') {
|
|
34
|
+
resolve();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let done = false;
|
|
39
|
+
const observer = new ResizeObserver(() => {
|
|
40
|
+
if (done) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (hasSize()) {
|
|
44
|
+
done = true;
|
|
45
|
+
observer.disconnect();
|
|
46
|
+
resolve();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
observer.observe(el);
|
|
51
|
+
|
|
52
|
+
window.setTimeout(() => {
|
|
53
|
+
if (done) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
done = true;
|
|
57
|
+
observer.disconnect();
|
|
58
|
+
resolve();
|
|
59
|
+
}, timeoutMs);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const runIntervalProbe = () =>
|
|
63
|
+
new Promise<void>((resolve) => {
|
|
64
|
+
let attempts = 0;
|
|
65
|
+
const maxAttempts = 6;
|
|
66
|
+
const intervalMs = 500;
|
|
67
|
+
const timer = window.setInterval(() => {
|
|
68
|
+
attempts += 1;
|
|
69
|
+
if (hasSize() || attempts >= maxAttempts) {
|
|
70
|
+
clearInterval(timer);
|
|
71
|
+
resolve();
|
|
72
|
+
}
|
|
73
|
+
}, intervalMs);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return runRafProbe().then(() => {
|
|
77
|
+
if (hasSize()) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
return runResizeObserver().then(() => {
|
|
81
|
+
if (hasSize()) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
return runIntervalProbe();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const EVENT = {
|
|
2
|
+
CLICK: 'click',
|
|
3
|
+
DBLCLICK: 'dblclick',
|
|
4
|
+
CONTEXTMENU: 'contextmenu',
|
|
5
|
+
KEYDOWN: 'keydown',
|
|
6
|
+
KEYUP: 'keyup',
|
|
7
|
+
KEYPRESS: 'keypress',
|
|
8
|
+
INPUT: 'input',
|
|
9
|
+
CHANGE: 'change',
|
|
10
|
+
FOCUS: 'focus',
|
|
11
|
+
BLUR: 'blur',
|
|
12
|
+
SUBMIT: 'submit',
|
|
13
|
+
RESET: 'reset',
|
|
14
|
+
MOUSEUP: 'mouseup',
|
|
15
|
+
MOUSEDOWN: 'mousedown',
|
|
16
|
+
MOUSEMOVE: 'mousemove',
|
|
17
|
+
MOUSEENTER: 'mouseenter',
|
|
18
|
+
MOUSELEAVE: 'mouseleave',
|
|
19
|
+
WHEEL: 'wheel',
|
|
20
|
+
SCROLL: 'scroll',
|
|
21
|
+
RESIZE: 'resize',
|
|
22
|
+
TOUCHSTART: 'touchstart',
|
|
23
|
+
TOUCHMOVE: 'touchmove',
|
|
24
|
+
TOUCHEND: 'touchend',
|
|
25
|
+
POINTERDOWN: 'pointerdown',
|
|
26
|
+
POINTERMOVE: 'pointermove',
|
|
27
|
+
POINTERUP: 'pointerup',
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
export type EventName = (typeof EVENT)[keyof typeof EVENT];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { waitForTransitionEnd } from './Transition';
|
|
2
|
+
|
|
3
|
+
export const expandHeight = async (el: HTMLElement, propertyName = 'height'): Promise<void> => {
|
|
4
|
+
el.style.height = '0px';
|
|
5
|
+
el.style.overflow = 'hidden';
|
|
6
|
+
void el.offsetHeight;
|
|
7
|
+
const targetHeight = el.scrollHeight;
|
|
8
|
+
el.style.height = `${targetHeight}px`;
|
|
9
|
+
await waitForTransitionEnd(el, propertyName);
|
|
10
|
+
el.style.height = '';
|
|
11
|
+
el.style.overflow = '';
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const collapseHeight = async (el: HTMLElement, propertyName = 'height'): Promise<void> => {
|
|
15
|
+
el.style.height = `${el.scrollHeight}px`;
|
|
16
|
+
el.style.overflow = 'hidden';
|
|
17
|
+
void el.offsetHeight;
|
|
18
|
+
el.style.height = '0px';
|
|
19
|
+
await waitForTransitionEnd(el, propertyName);
|
|
20
|
+
};
|
package/src/Helper/Id.ts
ADDED