tiny-essentials 1.13.2 → 1.15.0
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/TinyBasicsEs.js +656 -26
- package/dist/v1/TinyBasicsEs.min.js +1 -1
- package/dist/v1/TinyDragger.js +196 -26
- package/dist/v1/TinyEssentials.js +896 -26
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyNotifications.js +408 -0
- package/dist/v1/TinyNotifications.min.js +1 -0
- package/dist/v1/TinyUploadClicker.js +198 -26
- package/dist/v1/basics/collision.cjs +413 -0
- package/dist/v1/basics/collision.d.mts +187 -0
- package/dist/v1/basics/collision.mjs +350 -0
- package/dist/v1/basics/html.cjs +201 -26
- package/dist/v1/basics/html.d.mts +71 -7
- package/dist/v1/basics/html.mjs +186 -23
- package/dist/v1/basics/index.cjs +24 -0
- package/dist/v1/basics/index.d.mts +24 -1
- package/dist/v1/basics/index.mjs +4 -3
- package/dist/v1/basics/text.cjs +43 -0
- package/dist/v1/basics/text.d.mts +16 -0
- package/dist/v1/basics/text.mjs +37 -0
- package/dist/v1/build/TinyNotifications.cjs +7 -0
- package/dist/v1/build/TinyNotifications.d.mts +3 -0
- package/dist/v1/build/TinyNotifications.mjs +2 -0
- package/dist/v1/index.cjs +26 -0
- package/dist/v1/index.d.mts +25 -1
- package/dist/v1/index.mjs +5 -3
- package/dist/v1/libs/TinyNotifications.cjs +238 -0
- package/dist/v1/libs/TinyNotifications.d.mts +106 -0
- package/dist/v1/libs/TinyNotifications.mjs +211 -0
- package/docs/v1/README.md +5 -3
- package/docs/v1/basics/array.md +16 -43
- package/docs/v1/basics/collision.md +237 -0
- package/docs/v1/basics/html.md +117 -28
- package/docs/v1/basics/text.md +44 -14
- package/docs/v1/libs/TinyNotifications.md +189 -0
- package/package.json +5 -2
package/docs/v1/basics/html.md
CHANGED
|
@@ -11,16 +11,12 @@ It compares the bounding rectangles of both elements:
|
|
|
11
11
|
* ✅ Checks if **rect1** is NOT entirely to the left, right, above, or below **rect2**.
|
|
12
12
|
* ✅ If none of these are true, then the elements are overlapping.
|
|
13
13
|
|
|
14
|
-
---
|
|
15
|
-
|
|
16
14
|
#### 🧠 Syntax
|
|
17
15
|
|
|
18
16
|
```javascript
|
|
19
17
|
areHtmlElsColliding(elem1, elem2);
|
|
20
18
|
```
|
|
21
19
|
|
|
22
|
-
---
|
|
23
|
-
|
|
24
20
|
#### 🎯 Parameters
|
|
25
21
|
|
|
26
22
|
| Parameter | Type | Description |
|
|
@@ -28,16 +24,12 @@ areHtmlElsColliding(elem1, elem2);
|
|
|
28
24
|
| `elem1` | `Element` | The first DOM element. |
|
|
29
25
|
| `elem2` | `Element` | The second DOM element. |
|
|
30
26
|
|
|
31
|
-
---
|
|
32
|
-
|
|
33
27
|
#### 🔁 Return
|
|
34
28
|
|
|
35
29
|
| Type | Description |
|
|
36
30
|
| --------- | ------------------------------------------------------------------ |
|
|
37
31
|
| `boolean` | ✅ `true` if elements are colliding. <br>❌ `false` if they are not. |
|
|
38
32
|
|
|
39
|
-
---
|
|
40
|
-
|
|
41
33
|
#### 📦 Example
|
|
42
34
|
|
|
43
35
|
```javascript
|
|
@@ -51,8 +43,6 @@ if (areHtmlElsColliding(box1, box2)) {
|
|
|
51
43
|
}
|
|
52
44
|
```
|
|
53
45
|
|
|
54
|
-
---
|
|
55
|
-
|
|
56
46
|
#### 🚧 Limitations
|
|
57
47
|
|
|
58
48
|
* Only works with **axis-aligned elements** (rectangular shapes).
|
|
@@ -60,9 +50,93 @@ if (areHtmlElsColliding(box1, box2)) {
|
|
|
60
50
|
|
|
61
51
|
---
|
|
62
52
|
|
|
53
|
+
### 📖 `readBase64Blob(file: File, isDataUrl?: boolean | string): Promise<string>`
|
|
54
|
+
|
|
55
|
+
Reads a file and returns its Base64 content using the FileReader API, with optional formatting as a full Data URL.
|
|
56
|
+
|
|
57
|
+
#### 📥 Parameters
|
|
58
|
+
|
|
59
|
+
* `file` *(File)*: The file object selected by the user (e.g., from an `<input type="file">` element).
|
|
60
|
+
* `isDataUrl` *(boolean | string, optional)*:
|
|
61
|
+
|
|
62
|
+
* If `false` *(default)*: returns only the Base64 portion.
|
|
63
|
+
* If `true`: returns the original Data URL string from `FileReader`.
|
|
64
|
+
* If a string: treated as a custom MIME type for building a new Data URL.
|
|
65
|
+
|
|
66
|
+
#### 📤 Returns
|
|
67
|
+
|
|
68
|
+
* `Promise<string>`: Resolves with either the Base64 string or a complete Data URL, depending on `isDataUrl`.
|
|
69
|
+
|
|
70
|
+
#### ⚠️ Throws
|
|
71
|
+
|
|
72
|
+
* `TypeError` if:
|
|
73
|
+
|
|
74
|
+
* The result is not a string.
|
|
75
|
+
* The `isDataUrl` argument is not a boolean or a string.
|
|
76
|
+
* `Error` if:
|
|
77
|
+
|
|
78
|
+
* The string is not a valid Base64 or Data URL format.
|
|
79
|
+
* The MIME string format is invalid.
|
|
80
|
+
* `DOMException` if the file cannot be read by `FileReader`.
|
|
81
|
+
|
|
82
|
+
#### 🧪 Example
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
const input = document.querySelector('input[type="file"]');
|
|
86
|
+
input.addEventListener('change', async () => {
|
|
87
|
+
try {
|
|
88
|
+
const base64 = await readBase64Blob(input.files[0], false);
|
|
89
|
+
console.log(base64); // Logs only the Base64 string
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error('Error reading file:', err.message);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### 📖 `readFileBlob(file: File, method: 'readAsArrayBuffer' | 'readAsDataURL' | 'readAsText' | 'readAsBinaryString'): Promise<any>`
|
|
99
|
+
|
|
100
|
+
Reads the contents of a file using a specified FileReader method.
|
|
101
|
+
|
|
102
|
+
#### 📥 Parameters
|
|
103
|
+
|
|
104
|
+
* `file` *(File)*: The file object selected by the user (e.g., from an `<input type="file">` element).
|
|
105
|
+
* `method` *(string)*: The FileReader method to use:
|
|
106
|
+
|
|
107
|
+
* `'readAsArrayBuffer'` — for binary buffers
|
|
108
|
+
* `'readAsDataURL'` — for Base64 encoded data URLs
|
|
109
|
+
* `'readAsText'` — for plain text
|
|
110
|
+
* `'readAsBinaryString'` — for legacy binary string output
|
|
111
|
+
|
|
112
|
+
#### 📤 Returns
|
|
113
|
+
|
|
114
|
+
* `Promise<any>`: Resolves with the file content, depending on the method used.
|
|
115
|
+
|
|
116
|
+
#### ⚠️ Throws
|
|
117
|
+
|
|
118
|
+
* `Error` if an unexpected error occurs while resolving the result.
|
|
119
|
+
* `DOMException` if `FileReader` encounters a failure during the read process.
|
|
120
|
+
|
|
121
|
+
#### 🧪 Example
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
const input = document.querySelector('input[type="file"]');
|
|
125
|
+
input.addEventListener('change', async () => {
|
|
126
|
+
try {
|
|
127
|
+
const text = await readFileBlob(input.files[0], 'readAsText');
|
|
128
|
+
console.log(text); // Logs the file content as plain text
|
|
129
|
+
} catch (err) {
|
|
130
|
+
console.error('Error reading file:', err.message);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
63
137
|
### 📖 `readJsonBlob(file: File): Promise<any>`
|
|
64
138
|
|
|
65
|
-
Reads and parses a JSON file using the
|
|
139
|
+
Reads and parses a JSON file using the FileReader API.
|
|
66
140
|
|
|
67
141
|
#### 📥 Parameters
|
|
68
142
|
|
|
@@ -205,6 +279,32 @@ getHtmlElBordersWidth(el: Element): HtmlElBoxSides
|
|
|
205
279
|
|
|
206
280
|
---
|
|
207
281
|
|
|
282
|
+
### 👁️ `isInViewport(element)`
|
|
283
|
+
|
|
284
|
+
🔍 Checks if an element is **partially visible** in the current viewport.
|
|
285
|
+
|
|
286
|
+
```js
|
|
287
|
+
isInViewport(element: HTMLElement): boolean
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
* `element`: The DOM element to check.
|
|
291
|
+
* **Returns**: `true` if the element is at least partially visible; `false` otherwise.
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
### ✅ `isScrolledIntoView(element)`
|
|
296
|
+
|
|
297
|
+
📦 Checks if an element is **fully visible** (top and bottom) within the viewport.
|
|
298
|
+
|
|
299
|
+
```js
|
|
300
|
+
isScrolledIntoView(element: HTMLElement): boolean
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
* `element`: The DOM element to check.
|
|
304
|
+
* **Returns**: `true` if the entire element is within the viewport; `false` otherwise.
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
208
308
|
### 🔳 `getHtmlElBorders(el)`
|
|
209
309
|
|
|
210
310
|
📐 Returns the total **border size** of an element using `border{Side}` shorthand values from computed styles.
|
|
@@ -250,17 +350,14 @@ Automatically toggles CSS classes on a given element based on the browser window
|
|
|
250
350
|
|
|
251
351
|
Perfect for UI states like dimming, pausing animations, or showing "away" statuses.
|
|
252
352
|
|
|
253
|
-
---
|
|
254
|
-
|
|
255
353
|
#### 🧠 Features
|
|
256
354
|
|
|
257
355
|
* ✅ Adds or removes custom CSS classes depending on page visibility or focus
|
|
258
356
|
* ✅ Supports modern and legacy browsers (including IE9)
|
|
259
357
|
* ✅ Automatically dispatches an initial state check on load
|
|
358
|
+
* ✅ Allows custom **callbacks** for visibility changes (`onVisible`, `onHidden`)
|
|
260
359
|
* ✅ Returns a cleanup function to remove all listeners
|
|
261
360
|
|
|
262
|
-
---
|
|
263
|
-
|
|
264
361
|
#### 🧪 Usage
|
|
265
362
|
|
|
266
363
|
```js
|
|
@@ -270,14 +367,14 @@ const uninstall = installWindowHiddenScript({
|
|
|
270
367
|
element: document.getElementById('app'),
|
|
271
368
|
hiddenClass: 'is-hidden',
|
|
272
369
|
visibleClass: 'is-visible',
|
|
370
|
+
onVisible: () => console.log('Window is now visible'),
|
|
371
|
+
onHidden: () => console.log('Window is now hidden'),
|
|
273
372
|
});
|
|
274
373
|
|
|
275
374
|
// To remove all listeners later
|
|
276
375
|
uninstall();
|
|
277
376
|
```
|
|
278
377
|
|
|
279
|
-
---
|
|
280
|
-
|
|
281
378
|
#### ⚙️ Options
|
|
282
379
|
|
|
283
380
|
| Option | Type | Default | Description |
|
|
@@ -285,8 +382,8 @@ uninstall();
|
|
|
285
382
|
| `element` | `HTMLElement` | `document.body` | The element to which the visibility classes will be applied |
|
|
286
383
|
| `hiddenClass` | `string` | `'windowHidden'` | Class name to apply when the window is **not visible or blurred** |
|
|
287
384
|
| `visibleClass` | `string` | `'windowVisible'` | Class name to apply when the window is **visible or focused** |
|
|
288
|
-
|
|
289
|
-
|
|
385
|
+
| `onVisible` | `() => void` | `undefined` | Optional callback fired when the window becomes visible |
|
|
386
|
+
| `onHidden` | `() => void` | `undefined` | Optional callback fired when the window becomes hidden |
|
|
290
387
|
|
|
291
388
|
#### 🔄 Return Value
|
|
292
389
|
|
|
@@ -299,8 +396,6 @@ Returns a function that, when called, will:
|
|
|
299
396
|
* 🧹 Remove all attached event listeners
|
|
300
397
|
* ❌ Remove both visibility classes from the target element
|
|
301
398
|
|
|
302
|
-
---
|
|
303
|
-
|
|
304
399
|
#### 🚦 Events Supported
|
|
305
400
|
|
|
306
401
|
The script handles multiple events depending on browser support:
|
|
@@ -310,13 +405,9 @@ The script handles multiple events depending on browser support:
|
|
|
310
405
|
* `pageshow`, `pagehide`
|
|
311
406
|
* IE fallback: `onfocusin`, `onfocusout`
|
|
312
407
|
|
|
313
|
-
---
|
|
314
|
-
|
|
315
408
|
#### 🔍 Initial Trigger
|
|
316
409
|
|
|
317
|
-
Immediately after installation, the script simulates a `focus` or `blur` event based on the current visibility state to **ensure the classes are applied from the start**.
|
|
318
|
-
|
|
319
|
-
---
|
|
410
|
+
Immediately after installation, the script simulates a `focus` or `blur` event based on the current visibility state to **ensure the classes and callbacks are applied from the start**.
|
|
320
411
|
|
|
321
412
|
#### 🧯 Uninstalling
|
|
322
413
|
|
|
@@ -327,8 +418,6 @@ const stopWatching = installWindowHiddenScript(...);
|
|
|
327
418
|
stopWatching(); // later
|
|
328
419
|
```
|
|
329
420
|
|
|
330
|
-
---
|
|
331
|
-
|
|
332
421
|
#### 🎨 CSS Example
|
|
333
422
|
|
|
334
423
|
```css
|
package/docs/v1/basics/text.md
CHANGED
|
@@ -43,32 +43,24 @@ toTitleCaseLowerFirst('hello world'); // → "hello World"
|
|
|
43
43
|
|
|
44
44
|
Enables a keyboard shortcut (`Ctrl + Alt + [key]`) that toggles a CSS class on the `<body>` element. Useful for marking or highlighting AI-generated content dynamically.
|
|
45
45
|
|
|
46
|
-
---
|
|
47
|
-
|
|
48
46
|
### 🔤 Syntax
|
|
49
47
|
|
|
50
48
|
```js
|
|
51
49
|
addAiMarkerShortcut(key)
|
|
52
50
|
```
|
|
53
51
|
|
|
54
|
-
---
|
|
55
|
-
|
|
56
52
|
### 🧾 Parameters
|
|
57
53
|
|
|
58
54
|
| Name | Type | Default | Description |
|
|
59
55
|
| ----- | -------- | ------- | ---------------------------------------------------------------------------- |
|
|
60
56
|
| `key` | `string` | `'a'` | The character key to use in combination with `Ctrl + Alt`. Case-insensitive. |
|
|
61
57
|
|
|
62
|
-
---
|
|
63
|
-
|
|
64
58
|
### ⚙️ Behavior
|
|
65
59
|
|
|
66
60
|
* ⌨️ When the user presses `Ctrl + Alt + [key]`, the function toggles the CSS class `detect-made-by-ai` on the `<body>` element.
|
|
67
61
|
* 🧠 The shortcut only works in environments where the DOM is available (e.g., browsers).
|
|
68
62
|
* 🚫 If `document.body` is not available when the shortcut is used (e.g., if the DOM hasn't finished loading), a warning is logged and nothing happens.
|
|
69
63
|
|
|
70
|
-
---
|
|
71
|
-
|
|
72
64
|
### ❗ Error Handling
|
|
73
65
|
|
|
74
66
|
Two types of errors are handled:
|
|
@@ -85,8 +77,6 @@ Two types of errors are handled:
|
|
|
85
77
|
[AiMarkerShortcut] <body> element not found. Cannot toggle class. Ensure the DOM is fully loaded when using the shortcut.
|
|
86
78
|
```
|
|
87
79
|
|
|
88
|
-
---
|
|
89
|
-
|
|
90
80
|
### 🧪 Example
|
|
91
81
|
|
|
92
82
|
```js
|
|
@@ -94,8 +84,6 @@ addAiMarkerShortcut(); // Uses default key 'a'
|
|
|
94
84
|
// Pressing Ctrl + Alt + A toggles the class "detect-made-by-ai" on <body>
|
|
95
85
|
```
|
|
96
86
|
|
|
97
|
-
---
|
|
98
|
-
|
|
99
87
|
### 🎨 CSS Integration Example
|
|
100
88
|
|
|
101
89
|
Define the class in your stylesheet to make the toggle visually meaningful:
|
|
@@ -107,8 +95,6 @@ body.detect-made-by-ai .ai-content {
|
|
|
107
95
|
}
|
|
108
96
|
```
|
|
109
97
|
|
|
110
|
-
---
|
|
111
|
-
|
|
112
98
|
### 💡 Tip
|
|
113
99
|
|
|
114
100
|
To avoid the `<body>` warning, make sure you only call the function after the DOM is ready:
|
|
@@ -127,3 +113,47 @@ You can use a pre-built CSS template for the `detect-made-by-ai` class, availabl
|
|
|
127
113
|
* `/dist/v1/css/aiMarker.css` – The non-minified version for easier readability and customization.
|
|
128
114
|
|
|
129
115
|
Simply include the appropriate file in your project to style the elements marked with the `detect-made-by-ai` class.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 🎯 `safeTextTrim(text, limit, safeCutZone = 0.6)`
|
|
120
|
+
|
|
121
|
+
Trims a text string to a specified character limit, attempting to avoid cutting words in half. If a space is found before the limit and it’s not too far from the limit (at least a fraction controlled by `safeCutZone`), the cut is made at that space; otherwise, the text is hard-cut at the limit. If the input text is shorter than or equal to the limit, it is returned unchanged.
|
|
122
|
+
|
|
123
|
+
### 🔤 Syntax
|
|
124
|
+
|
|
125
|
+
```js
|
|
126
|
+
safeTextTrim(text, limit, safeCutZone = 0.6)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 🧾 Parameters
|
|
130
|
+
|
|
131
|
+
| Name | Type | Default | Description |
|
|
132
|
+
| ------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------- |
|
|
133
|
+
| `text` | `string` | — | The input text to be trimmed. Must be a string. |
|
|
134
|
+
| `limit` | `number` | — | The maximum number of characters allowed. Must be a positive integer. |
|
|
135
|
+
| `safeCutZone` | `number` | `0.6` | A decimal between 0 and 1 representing the minimal acceptable position (fraction of `limit`) to cut at a space. |
|
|
136
|
+
|
|
137
|
+
### ⚙️ Behavior
|
|
138
|
+
|
|
139
|
+
* If `text` length is less than or equal to `limit`, returns `text` unchanged.
|
|
140
|
+
* Attempts to cut at the last space character before `limit` but only if it’s within the `safeCutZone` (e.g., at least 60% of the `limit`).
|
|
141
|
+
* If no suitable space is found within the zone, the text is cut strictly at the `limit`.
|
|
142
|
+
* The resulting trimmed text ends with an ellipsis (`"..."`) if it was cut.
|
|
143
|
+
* Leading and trailing whitespace on input is trimmed before processing.
|
|
144
|
+
|
|
145
|
+
### ❗ Error Handling
|
|
146
|
+
|
|
147
|
+
Throws a `TypeError` in these cases:
|
|
148
|
+
|
|
149
|
+
* If `text` is not a string.
|
|
150
|
+
* If `limit` is not a positive integer.
|
|
151
|
+
* If `safeCutZone` is not a number between 0 and 1 (inclusive).
|
|
152
|
+
|
|
153
|
+
### 🧪 Example
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
const longText = "This is a sample sentence that will be trimmed properly.";
|
|
157
|
+
console.log(safeTextTrim(longText, 30));
|
|
158
|
+
// Output: "This is a sample sentence that..."
|
|
159
|
+
```
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# 📣 TinyNotifications
|
|
2
|
+
|
|
3
|
+
> A utility class to manage browser notifications with sound, custom icons, text truncation, and click behavior.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 🚀 Features
|
|
8
|
+
|
|
9
|
+
* ✅ Request and manage user permission for notifications
|
|
10
|
+
* 🔔 Play a sound when a notification is shown
|
|
11
|
+
* 🖼️ Support for default avatar/icon
|
|
12
|
+
* ✂️ Automatic body text truncation
|
|
13
|
+
* 💥 Strong validation with `TypeError` and runtime checks
|
|
14
|
+
* ❗ Prevents use of `send()` before permission request
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🏗️ Constructor
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
new TinyNotifications(options?)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Parameters
|
|
25
|
+
|
|
26
|
+
| Name | Type | Default | Description |
|
|
27
|
+
| ---------------- | ----------------------------------------- | ------- | -------------------------------------------------- |
|
|
28
|
+
| `audio` | `string` \| `HTMLAudioElement` \| `null` | `null` | Sound to be played with the notification |
|
|
29
|
+
| `defaultIcon` | `string` \| `null` | `null` | Default icon to use in notifications |
|
|
30
|
+
| `bodyLimit` | `number` | `100` | Maximum number of characters in body text |
|
|
31
|
+
| `defaultOnClick` | `(this: Notification, evt: Event) => any` | `fn` | Function executed when the notification is clicked |
|
|
32
|
+
|
|
33
|
+
### Throws
|
|
34
|
+
|
|
35
|
+
* `TypeError` if any parameter is of invalid type.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 🧠 Methods
|
|
40
|
+
|
|
41
|
+
### 🔐 `requestPerm()`
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
requestPerm(): Promise<boolean>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Requests permission from the browser to send notifications.
|
|
48
|
+
|
|
49
|
+
* Sets internal `#allowed` and `#permissionRequested` flags.
|
|
50
|
+
* Must be called before using `send()`.
|
|
51
|
+
|
|
52
|
+
**Returns:** `Promise<boolean>` — `true` if permission was granted.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
### ⚙️ `isCompatible()`
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
isCompatible(): boolean
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Checks if the current browser supports the Notification API.
|
|
63
|
+
|
|
64
|
+
**Returns:** `true` or `false`
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
### 📤 `send(title, config?)`
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
send(title: string, config?: NotificationOptions): Notification | null
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Sends a notification to the user with optional configuration.
|
|
75
|
+
Truncates long body texts and plays a sound if set.
|
|
76
|
+
|
|
77
|
+
#### Parameters
|
|
78
|
+
|
|
79
|
+
* `title`: *(string)* — Title of the notification
|
|
80
|
+
* `config`: *(object)* — Optional notification settings (`body`, `icon`, `vibrate`, etc.)
|
|
81
|
+
|
|
82
|
+
#### Throws
|
|
83
|
+
|
|
84
|
+
* `Error` if `requestPerm()` was never called
|
|
85
|
+
* `TypeError` if `title` is not a string or `config` is not an object
|
|
86
|
+
|
|
87
|
+
#### Returns
|
|
88
|
+
|
|
89
|
+
* `Notification` instance if allowed
|
|
90
|
+
* `null` if permission was denied
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## 📥 Getters & Setters
|
|
95
|
+
|
|
96
|
+
### 📌 `wasPermissionRequested()`
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
wasPermissionRequested(): boolean
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Checks if `requestPerm()` was called.
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
### 🟢 `isAllowed()`
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
isAllowed(): boolean
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Checks if permission has been granted.
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### 🔊 `getAudio()` / `setAudio(value)`
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
getAudio(): HTMLAudioElement | null
|
|
120
|
+
setAudio(value: HTMLAudioElement | string | null)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Set or retrieve the notification sound.
|
|
124
|
+
**Throws:** `TypeError` if invalid type is used.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### 📏 `getBodyLimit()` / `setBodyLimit(value)`
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
getBodyLimit(): number
|
|
132
|
+
setBodyLimit(value: number)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Set or retrieve the maximum number of characters in the notification body.
|
|
136
|
+
**Throws:** `TypeError` if value is not a non-negative number.
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
### 🖼️ `getDefaultAvatar()` / `setDefaultAvatar(value)`
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
getDefaultAvatar(): string | null
|
|
144
|
+
setDefaultAvatar(value: string | null)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Get or set the default icon for notifications.
|
|
148
|
+
**Throws:** `TypeError` if value is not a string or `null`.
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### 🖱️ `getDefaultOnClick()` / `setDefaultOnClick(value)`
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
getDefaultOnClick(): (this: Notification, evt: Event) => any
|
|
156
|
+
setDefaultOnClick(value: function)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Set or retrieve the default click handler for all notifications.
|
|
160
|
+
**Throws:** `TypeError` if value is not a function.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 📦 Example
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
import TinyNotifications from './TinyNotifications.js';
|
|
168
|
+
|
|
169
|
+
const notify = new TinyNotifications({
|
|
170
|
+
audio: '/sounds/ping.mp3',
|
|
171
|
+
defaultIcon: '/img/icon.png',
|
|
172
|
+
bodyLimit: 80
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
await notify.requestPerm();
|
|
176
|
+
|
|
177
|
+
notify.send('Hello World!', {
|
|
178
|
+
body: 'This message will be truncated if too long.',
|
|
179
|
+
vibrate: [100, 50, 100]
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## 🧪 Safety Notes
|
|
186
|
+
|
|
187
|
+
* Always call `requestPerm()` **before** using `send()`
|
|
188
|
+
* Invalid or unsafe inputs throw strong errors
|
|
189
|
+
* Compatible with all modern browsers that support `Notification` API
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
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",
|
|
7
7
|
"test:js": "npx babel-node test/index.js",
|
|
8
8
|
"test:cjs": "node test/index.cjs",
|
|
9
9
|
"test:mjs": "node test/index.mjs",
|
|
10
|
+
"test:mjs:web": "node test/express.mjs",
|
|
10
11
|
"test:mjs:promisequeue": "node test/index.mjs promiseQueue",
|
|
11
12
|
"test:mjs:objtype": "node test/index.mjs objType",
|
|
12
13
|
"test:mjs:jsoncolor": "node test/index.mjs colorStringify",
|
|
@@ -97,11 +98,13 @@
|
|
|
97
98
|
"byte-length": "^1.0.2",
|
|
98
99
|
"clone": "^2.1.2",
|
|
99
100
|
"compare-versions": "^6.1.1",
|
|
101
|
+
"esbuild": "^0.25.5",
|
|
100
102
|
"express": "^5.1.0",
|
|
101
103
|
"firebase": "^11.7.1",
|
|
102
104
|
"firebase-functions": "^6.3.2",
|
|
103
105
|
"latest-version": "^9.0.0",
|
|
104
106
|
"lodash": "^4.17.21",
|
|
107
|
+
"marked": "^16.0.0",
|
|
105
108
|
"md5": "^2.3.0",
|
|
106
109
|
"moment": "^2.30.1",
|
|
107
110
|
"moment-timezone": "^0.5.48",
|
|
@@ -113,7 +116,7 @@
|
|
|
113
116
|
"rollup": "^4.40.0",
|
|
114
117
|
"rollup-preserve-directives": "^1.1.3",
|
|
115
118
|
"safe-stable-stringify": "^2.5.0",
|
|
116
|
-
"sass": "^1.
|
|
119
|
+
"sass": "^1.89.2",
|
|
117
120
|
"tinycolor2": "^1.6.0",
|
|
118
121
|
"tslib": "^2.8.1",
|
|
119
122
|
"type-fest": "^4.40.0",
|