tiny-essentials 1.21.2 → 1.21.4
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/dist/v1/TinyDragger.min.js +1 -1
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyHtml.min.js +1 -1
- package/dist/v1/TinySmartScroller.min.js +1 -1
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/libs/TinyHtml.cjs +174 -85
- package/dist/v1/libs/TinyHtml.d.mts +49 -22
- package/dist/v1/libs/TinyHtml.mjs +162 -85
- package/docs/v1/libs/TinyHtml.md +25 -0
- package/package.json +1 -1
|
@@ -550,6 +550,17 @@ class TinyHtml {
|
|
|
550
550
|
return TinyHtml.getByTagNameNS(localName, namespaceURI, TinyHtml._preElem(this, 'getByTagNameNS'));
|
|
551
551
|
}
|
|
552
552
|
//////////////////////////////////////////////////////////////////
|
|
553
|
+
/**
|
|
554
|
+
* Iterates over all elements, executing the provided callback on each.
|
|
555
|
+
* @param {(element: TinyHtml, index: number, items: TinyHtml[]) => void} callback - Function invoked for each element.
|
|
556
|
+
* @returns {TinyHtml} The current instance for chaining.
|
|
557
|
+
*/
|
|
558
|
+
forEach(callback) {
|
|
559
|
+
const elems = this.getAll().map((el, index) => this.extract(index));
|
|
560
|
+
for (const index in elems)
|
|
561
|
+
callback(elems[index], Number(index), elems);
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
553
564
|
/**
|
|
554
565
|
* Returns the current target held by this instance.
|
|
555
566
|
*
|
|
@@ -1153,8 +1164,28 @@ class TinyHtml {
|
|
|
1153
1164
|
return TinyHtml.isSameDom(this, elem);
|
|
1154
1165
|
}
|
|
1155
1166
|
//////////////////////////////////////////////////////////////////
|
|
1156
|
-
/**
|
|
1157
|
-
|
|
1167
|
+
/**
|
|
1168
|
+
* Internal data storage for element information.
|
|
1169
|
+
* @type {ElementDataStore}
|
|
1170
|
+
*/
|
|
1171
|
+
#data = {};
|
|
1172
|
+
/**
|
|
1173
|
+
* Returns a shallow copy of the internal data.
|
|
1174
|
+
* @type {ElementDataStore}
|
|
1175
|
+
*/
|
|
1176
|
+
get _data() {
|
|
1177
|
+
return { ...this.#data };
|
|
1178
|
+
}
|
|
1179
|
+
/**
|
|
1180
|
+
* Replaces the internal data with a new object.
|
|
1181
|
+
* @param {ElementDataStore} value - Must be a non-null object.
|
|
1182
|
+
* @throws {Error} If the value is not a valid object.
|
|
1183
|
+
*/
|
|
1184
|
+
set _data(value) {
|
|
1185
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
1186
|
+
throw new Error('value must be a non-null object.');
|
|
1187
|
+
this.#data = value;
|
|
1188
|
+
}
|
|
1158
1189
|
/**
|
|
1159
1190
|
* Internal data selectors for accessing public or private data stores.
|
|
1160
1191
|
*
|
|
@@ -1174,7 +1205,7 @@ class TinyHtml {
|
|
|
1174
1205
|
private: (where, el) => {
|
|
1175
1206
|
if (!(el instanceof TinyHtml))
|
|
1176
1207
|
throw new Error(`Element must be a TinyHtml instance to execute ${where}().`);
|
|
1177
|
-
return el
|
|
1208
|
+
return el.#data;
|
|
1178
1209
|
},
|
|
1179
1210
|
};
|
|
1180
1211
|
/**
|
|
@@ -2495,6 +2526,27 @@ class TinyHtml {
|
|
|
2495
2526
|
blur() {
|
|
2496
2527
|
return TinyHtml.blur(this);
|
|
2497
2528
|
}
|
|
2529
|
+
/**
|
|
2530
|
+
* Select the text content of an input or textarea element.
|
|
2531
|
+
*
|
|
2532
|
+
* @param {TinyHtml|HTMLInputElement|HTMLTextAreaElement} el - The element or a selector string.
|
|
2533
|
+
* @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
|
|
2534
|
+
*/
|
|
2535
|
+
static select(el) {
|
|
2536
|
+
const elem = TinyHtml._preHtmlElem(el, 'select');
|
|
2537
|
+
if (elem instanceof HTMLInputElement || elem instanceof HTMLTextAreaElement)
|
|
2538
|
+
elem.select();
|
|
2539
|
+
else
|
|
2540
|
+
throw new Error('Element must be an <input> or <textarea> to use select().');
|
|
2541
|
+
return el;
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* Select the text content of an input or textarea element.
|
|
2545
|
+
* @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
|
|
2546
|
+
*/
|
|
2547
|
+
select() {
|
|
2548
|
+
return TinyHtml.select(this);
|
|
2549
|
+
}
|
|
2498
2550
|
/**
|
|
2499
2551
|
* Interprets a value as a boolean `true` if it matches a common truthy representation.
|
|
2500
2552
|
*
|
|
@@ -4025,135 +4077,155 @@ class TinyHtml {
|
|
|
4025
4077
|
* Registers an event listener on the specified element.
|
|
4026
4078
|
*
|
|
4027
4079
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
|
|
4028
|
-
* @param {string}
|
|
4080
|
+
* @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
|
|
4029
4081
|
* @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
|
|
4030
4082
|
* @param {EventRegistryOptions} [options] - Optional event listener options.
|
|
4031
4083
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4032
4084
|
*/
|
|
4033
|
-
static on(el,
|
|
4034
|
-
if (typeof
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
if (
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
events
|
|
4045
|
-
|
|
4046
|
-
|
|
4085
|
+
static on(el, events, handler, options) {
|
|
4086
|
+
if (typeof events !== 'string' &&
|
|
4087
|
+
(!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
|
|
4088
|
+
throw new TypeError('The events must be a string or array of strings.');
|
|
4089
|
+
for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
|
|
4090
|
+
if (typeof event !== 'string')
|
|
4091
|
+
throw new TypeError('The event name must be a string.');
|
|
4092
|
+
TinyHtml._preEventTargetElems(el, 'on').forEach((elem) => {
|
|
4093
|
+
elem.addEventListener(event, handler, options);
|
|
4094
|
+
if (!__eventRegistry.has(elem))
|
|
4095
|
+
__eventRegistry.set(elem, {});
|
|
4096
|
+
const events = __eventRegistry.get(elem);
|
|
4097
|
+
if (!events)
|
|
4098
|
+
return;
|
|
4099
|
+
if (!Array.isArray(events[event]))
|
|
4100
|
+
events[event] = [];
|
|
4101
|
+
events[event].push({ handler, options });
|
|
4102
|
+
});
|
|
4103
|
+
}
|
|
4047
4104
|
return el;
|
|
4048
4105
|
}
|
|
4049
4106
|
/**
|
|
4050
4107
|
* Registers an event listener on the specified element.
|
|
4051
4108
|
*
|
|
4052
|
-
* @param {string}
|
|
4109
|
+
* @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
|
|
4053
4110
|
* @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
|
|
4054
4111
|
* @param {EventRegistryOptions} [options] - Optional event listener options.
|
|
4055
4112
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4056
4113
|
*/
|
|
4057
|
-
on(
|
|
4058
|
-
return TinyHtml.on(this,
|
|
4114
|
+
on(events, handler, options) {
|
|
4115
|
+
return TinyHtml.on(this, events, handler, options);
|
|
4059
4116
|
}
|
|
4060
4117
|
/**
|
|
4061
4118
|
* Registers an event listener that runs only once, then is removed.
|
|
4062
4119
|
*
|
|
4063
4120
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
|
|
4064
|
-
* @param {string}
|
|
4121
|
+
* @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
|
|
4065
4122
|
* @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
|
|
4066
4123
|
* @param {EventRegistryOptions} [options={}] - Optional event listener options.
|
|
4067
4124
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4068
4125
|
*/
|
|
4069
|
-
static once(el,
|
|
4070
|
-
if (typeof
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4126
|
+
static once(el, events, handler, options = {}) {
|
|
4127
|
+
if (typeof events !== 'string' &&
|
|
4128
|
+
(!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
|
|
4129
|
+
throw new TypeError('The events must be a string or array of strings.');
|
|
4130
|
+
for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
|
|
4131
|
+
if (typeof event !== 'string')
|
|
4132
|
+
throw new TypeError('The event name must be a string.');
|
|
4133
|
+
TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
|
|
4134
|
+
/** @type {EventListenerOrEventListenerObject} */
|
|
4135
|
+
const wrapped = (e) => {
|
|
4136
|
+
TinyHtml.off(elem, event, wrapped);
|
|
4137
|
+
if (typeof handler === 'function')
|
|
4138
|
+
handler(e);
|
|
4139
|
+
};
|
|
4140
|
+
TinyHtml.on(elem, event, wrapped, typeof options === 'boolean' ? options : { ...options, once: true });
|
|
4141
|
+
});
|
|
4142
|
+
}
|
|
4081
4143
|
return el;
|
|
4082
4144
|
}
|
|
4083
4145
|
/**
|
|
4084
4146
|
* Registers an event listener that runs only once, then is removed.
|
|
4085
4147
|
*
|
|
4086
|
-
* @param {string}
|
|
4148
|
+
* @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
|
|
4087
4149
|
* @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
|
|
4088
4150
|
* @param {EventRegistryOptions} [options={}] - Optional event listener options.
|
|
4089
4151
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4090
4152
|
*/
|
|
4091
|
-
once(
|
|
4092
|
-
return TinyHtml.once(this,
|
|
4153
|
+
once(events, handler, options = {}) {
|
|
4154
|
+
return TinyHtml.once(this, events, handler, options);
|
|
4093
4155
|
}
|
|
4094
4156
|
/**
|
|
4095
4157
|
* Removes a specific event listener from an element.
|
|
4096
4158
|
*
|
|
4097
4159
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
|
|
4098
|
-
* @param {string}
|
|
4160
|
+
* @param {string|string[]} events - The event type.
|
|
4099
4161
|
* @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
|
|
4100
4162
|
* @param {boolean|EventListenerOptions} [options] - Optional listener options.
|
|
4101
4163
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4102
4164
|
*/
|
|
4103
|
-
static off(el,
|
|
4104
|
-
if (typeof
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4165
|
+
static off(el, events, handler, options) {
|
|
4166
|
+
if (typeof events !== 'string' &&
|
|
4167
|
+
(!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
|
|
4168
|
+
throw new TypeError('The events must be a string or array of strings.');
|
|
4169
|
+
for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
|
|
4170
|
+
if (typeof event !== 'string')
|
|
4171
|
+
throw new TypeError('The event name must be a string.');
|
|
4172
|
+
TinyHtml._preEventTargetElems(el, 'off').forEach((elem) => {
|
|
4173
|
+
elem.removeEventListener(event, handler, options);
|
|
4174
|
+
const events = __eventRegistry.get(elem);
|
|
4175
|
+
if (events && events[event]) {
|
|
4176
|
+
events[event] = events[event].filter((entry) => entry.handler !== handler);
|
|
4177
|
+
if (events[event].length === 0)
|
|
4178
|
+
delete events[event];
|
|
4179
|
+
}
|
|
4180
|
+
});
|
|
4181
|
+
}
|
|
4115
4182
|
return el;
|
|
4116
4183
|
}
|
|
4117
4184
|
/**
|
|
4118
4185
|
* Removes a specific event listener from an element.
|
|
4119
4186
|
*
|
|
4120
|
-
* @param {string}
|
|
4187
|
+
* @param {string|string[]} events - The event type.
|
|
4121
4188
|
* @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
|
|
4122
4189
|
* @param {boolean|EventListenerOptions} [options] - Optional listener options.
|
|
4123
4190
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4124
4191
|
*/
|
|
4125
|
-
off(
|
|
4126
|
-
return TinyHtml.off(this,
|
|
4192
|
+
off(events, handler, options) {
|
|
4193
|
+
return TinyHtml.off(this, events, handler, options);
|
|
4127
4194
|
}
|
|
4128
4195
|
/**
|
|
4129
4196
|
* Removes all event listeners of a specific type from the element.
|
|
4130
4197
|
*
|
|
4131
4198
|
* @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
|
|
4132
|
-
* @param {string}
|
|
4199
|
+
* @param {string|string[]} events - The event type to remove (e.g. 'click').
|
|
4133
4200
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4134
4201
|
*/
|
|
4135
|
-
static offAll(el,
|
|
4136
|
-
if (typeof
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
if (
|
|
4141
|
-
|
|
4142
|
-
|
|
4202
|
+
static offAll(el, events) {
|
|
4203
|
+
if (typeof events !== 'string' &&
|
|
4204
|
+
(!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
|
|
4205
|
+
throw new TypeError('The events must be a string or array of strings.');
|
|
4206
|
+
for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
|
|
4207
|
+
if (typeof event !== 'string')
|
|
4208
|
+
throw new TypeError('The event name must be a string.');
|
|
4209
|
+
TinyHtml._preEventTargetElems(el, 'offAll').forEach((elem) => {
|
|
4210
|
+
const events = __eventRegistry.get(elem);
|
|
4211
|
+
if (events && events[event]) {
|
|
4212
|
+
for (const entry of events[event]) {
|
|
4213
|
+
elem.removeEventListener(event, entry.handler, entry.options);
|
|
4214
|
+
}
|
|
4215
|
+
delete events[event];
|
|
4143
4216
|
}
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
});
|
|
4217
|
+
});
|
|
4218
|
+
}
|
|
4147
4219
|
return el;
|
|
4148
4220
|
}
|
|
4149
4221
|
/**
|
|
4150
4222
|
* Removes all event listeners of a specific type from the element.
|
|
4151
4223
|
*
|
|
4152
|
-
* @param {string}
|
|
4224
|
+
* @param {string|string[]} events - The event type to remove (e.g. 'click').
|
|
4153
4225
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4154
4226
|
*/
|
|
4155
|
-
offAll(
|
|
4156
|
-
return TinyHtml.offAll(this,
|
|
4227
|
+
offAll(events) {
|
|
4228
|
+
return TinyHtml.offAll(this, events);
|
|
4157
4229
|
}
|
|
4158
4230
|
/**
|
|
4159
4231
|
* Removes all event listeners of all types from the element.
|
|
@@ -4195,34 +4267,39 @@ class TinyHtml {
|
|
|
4195
4267
|
* Triggers all handlers associated with a specific event on the given element.
|
|
4196
4268
|
*
|
|
4197
4269
|
* @param {TinyEventTarget|TinyEventTarget[]} el - Target element where the event should be triggered.
|
|
4198
|
-
* @param {string}
|
|
4270
|
+
* @param {string|string[]} events - Name of the event to trigger.
|
|
4199
4271
|
* @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
|
|
4200
4272
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4201
4273
|
*/
|
|
4202
|
-
static trigger(el,
|
|
4203
|
-
if (typeof
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4274
|
+
static trigger(el, events, payload = {}) {
|
|
4275
|
+
if (typeof events !== 'string' &&
|
|
4276
|
+
(!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
|
|
4277
|
+
throw new TypeError('The events must be a string or array of strings.');
|
|
4278
|
+
for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
|
|
4279
|
+
if (typeof event !== 'string')
|
|
4280
|
+
throw new TypeError('The event name must be a string.');
|
|
4281
|
+
TinyHtml._preEventTargetElems(el, 'trigger').forEach((elem) => {
|
|
4282
|
+
const evt = payload instanceof Event || payload instanceof CustomEvent
|
|
4283
|
+
? payload
|
|
4284
|
+
: new CustomEvent(event, {
|
|
4285
|
+
bubbles: true,
|
|
4286
|
+
cancelable: true,
|
|
4287
|
+
detail: payload,
|
|
4288
|
+
});
|
|
4289
|
+
elem.dispatchEvent(evt);
|
|
4290
|
+
});
|
|
4291
|
+
}
|
|
4215
4292
|
return el;
|
|
4216
4293
|
}
|
|
4217
4294
|
/**
|
|
4218
4295
|
* Triggers all handlers associated with a specific event on the given element.
|
|
4219
4296
|
*
|
|
4220
|
-
* @param {string}
|
|
4297
|
+
* @param {string|string[]} events - Name of the event to trigger.
|
|
4221
4298
|
* @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
|
|
4222
4299
|
* @returns {TinyEventTarget|TinyEventTarget[]}
|
|
4223
4300
|
*/
|
|
4224
|
-
trigger(
|
|
4225
|
-
return TinyHtml.trigger(this,
|
|
4301
|
+
trigger(events, payload = {}) {
|
|
4302
|
+
return TinyHtml.trigger(this, events, payload);
|
|
4226
4303
|
}
|
|
4227
4304
|
///////////////////////////////////////////////////////////////
|
|
4228
4305
|
/**
|
package/docs/v1/libs/TinyHtml.md
CHANGED
|
@@ -587,6 +587,17 @@ Checks whether the element exists at the given index.
|
|
|
587
587
|
|
|
588
588
|
---
|
|
589
589
|
|
|
590
|
+
### `forEach(callback)`
|
|
591
|
+
|
|
592
|
+
Iterates over all elements in the current instance, executing the provided callback on each one.
|
|
593
|
+
|
|
594
|
+
* **Parameters**:
|
|
595
|
+
|
|
596
|
+
* `callback`: `(element: TinyHtml, index: number, items: TinyHtml[]) => void`
|
|
597
|
+
Function invoked for each element. Receives the current `TinyHtml`, its index, and the full list.
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
590
601
|
### `get(index)`
|
|
591
602
|
Returns the raw DOM element associated with this instance.
|
|
592
603
|
|
|
@@ -2238,6 +2249,20 @@ tinyElem.blur();
|
|
|
2238
2249
|
|
|
2239
2250
|
---
|
|
2240
2251
|
|
|
2252
|
+
### 🖊️ `TinyHtml.select(el)` / `el.select()`
|
|
2253
|
+
|
|
2254
|
+
Selects all the text inside the given element.
|
|
2255
|
+
|
|
2256
|
+
```js
|
|
2257
|
+
TinyHtml.select(element);
|
|
2258
|
+
// or
|
|
2259
|
+
tinyElem.select();
|
|
2260
|
+
```
|
|
2261
|
+
|
|
2262
|
+
⚠️ Throws an error if the target element is not an `<input>` or `<textarea>`.
|
|
2263
|
+
|
|
2264
|
+
---
|
|
2265
|
+
|
|
2241
2266
|
## 🌐 Window Scroll & Viewport Helpers
|
|
2242
2267
|
|
|
2243
2268
|
These methods let you control and query scroll positions and viewport size with simple, readable functions.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.4",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|