@thednp/shorty 2.0.0-alpha9 → 2.0.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/.eslintrc.cjs +224 -0
- package/.lgtm.yml +8 -0
- package/.prettierrc.json +15 -0
- package/README.md +5 -5
- package/cypress/e2e/att.cy.ts +46 -0
- package/cypress/e2e/boolean.cy.ts +44 -0
- package/cypress/e2e/class.cy.ts +28 -0
- package/cypress/e2e/event.cy.ts +51 -0
- package/cypress/e2e/get.cy.ts +168 -0
- package/cypress/e2e/is.cy.ts +233 -0
- package/cypress/e2e/misc.cy.ts +354 -0
- package/cypress/e2e/selectors.cy.ts +85 -0
- package/cypress/fixtures/custom-elem.js +18 -0
- package/cypress/plugins/esbuild-istanbul.ts +50 -0
- package/cypress/plugins/tsCompile.ts +34 -0
- package/cypress/support/commands.ts +37 -0
- package/cypress/support/e2e.ts +21 -0
- package/cypress/support/index.js +22 -0
- package/cypress/test.html +63 -0
- package/cypress.config.ts +30 -0
- package/dist/shorty.cjs +1 -1
- package/dist/shorty.cjs.map +1 -1
- package/dist/shorty.d.ts +204 -6
- package/dist/shorty.js +1 -1
- package/dist/shorty.js.map +1 -1
- package/dist/shorty.mjs +443 -326
- package/dist/shorty.mjs.map +1 -1
- package/dts.config.ts +21 -0
- package/package.json +73 -73
- package/src/event/off.ts +4 -4
- package/src/event/on.ts +4 -4
- package/src/event/one.ts +6 -5
- package/src/index.ts +9 -0
- package/src/interface/event.d.ts +247 -0
- package/src/misc/ObjectFromEntries.ts +11 -0
- package/src/misc/createElement.ts +4 -10
- package/src/misc/createElementNS.ts +8 -13
- package/src/misc/data.ts +1 -0
- package/src/strings/keyNumpadEnter.ts +7 -0
- package/tsconfig.json +30 -0
- package/vite.config.ts +30 -0
package/dts.config.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const packageJson = require("./package.json");
|
|
2
|
+
|
|
3
|
+
const getPackageName = () => {
|
|
4
|
+
return packageJson.name.includes('@') ? packageJson.name.split('/')[1] : packageJson.name;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const config = {
|
|
8
|
+
entries: [
|
|
9
|
+
{
|
|
10
|
+
filePath: "./src/index.ts",
|
|
11
|
+
outFile: `./dist/${getPackageName()}.d.ts`,
|
|
12
|
+
noCheck: false,
|
|
13
|
+
output: {
|
|
14
|
+
umdModuleName: 'SHORTY',
|
|
15
|
+
noBanner: true,
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports = config;
|
package/package.json
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@thednp/shorty",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"import": "./dist/shorty.mjs"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
},
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
}
|
|
73
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@thednp/shorty",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "TypeScript shorties for the web",
|
|
5
|
+
"source": "./src/index.ts",
|
|
6
|
+
"main": "./dist/shorty.js",
|
|
7
|
+
"module": "./dist/shorty.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/shorty.d.ts",
|
|
11
|
+
"require": "./dist/shorty.cjs",
|
|
12
|
+
"import": "./dist/shorty.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public",
|
|
17
|
+
"registry": "https://registry.npmjs.org/"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/thednp/shorty.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"shorty",
|
|
25
|
+
"javascript",
|
|
26
|
+
"typescript"
|
|
27
|
+
],
|
|
28
|
+
"author": "thednp",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/thednp/shorty/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/thednp/shorty",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@bahmutov/cypress-esbuild-preprocessor": "^2.2.0",
|
|
36
|
+
"@cypress/code-coverage": "^3.12.12",
|
|
37
|
+
"@types/istanbul-lib-instrument": "^1.7.7",
|
|
38
|
+
"@types/node": "^20.10.0",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
40
|
+
"@typescript-eslint/parser": "^5.62.0",
|
|
41
|
+
"cypress": "^13.6.0",
|
|
42
|
+
"dts-bundle-generator": "^8.1.2",
|
|
43
|
+
"eslint": "^8.54.0",
|
|
44
|
+
"eslint-plugin-jsdoc": "^46.9.0",
|
|
45
|
+
"eslint-plugin-prefer-arrow": "^1.2.3",
|
|
46
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
47
|
+
"istanbul-lib-coverage": "^3.2.2",
|
|
48
|
+
"istanbul-lib-instrument": "^5.2.1",
|
|
49
|
+
"nyc": "^15.1.0",
|
|
50
|
+
"prettier": "^2.8.8",
|
|
51
|
+
"rimraf": "^5.0.5",
|
|
52
|
+
"typescript": "^5.3.2",
|
|
53
|
+
"vite": "^4.5.0"
|
|
54
|
+
},
|
|
55
|
+
"packageManager": "pnpm@8.6.12",
|
|
56
|
+
"engines": {
|
|
57
|
+
"node": ">=16",
|
|
58
|
+
"pnpm": ">=8.6.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"pre-test": "pnpm clean-coverage",
|
|
62
|
+
"test": "pnpm pre-test && npx cypress run",
|
|
63
|
+
"clean-coverage": "rimraf coverage .nyc_output",
|
|
64
|
+
"cypress": "pnpm pre-test && npx cypress open",
|
|
65
|
+
"coverage:report": "nyc report --reporter=lcov --reporter=json --reporter=text --reporter=json-summary",
|
|
66
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
67
|
+
"lint:ts": "eslint -c .eslintrc.cjs --ext .ts src",
|
|
68
|
+
"fix:ts": "eslint -c .eslintrc.cjs --ext .ts src --fix",
|
|
69
|
+
"check:ts": "tsc -noEmit",
|
|
70
|
+
"build": "pnpm check:ts && pnpm lint:ts && vite build && pnpm dts",
|
|
71
|
+
"dts": "dts-bundle-generator --config ./dts.config.ts"
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/event/off.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Remove eventListener from an `EventTarget` object.
|
|
3
3
|
*/
|
|
4
|
-
const off = (
|
|
5
|
-
element:
|
|
4
|
+
const off = <T extends EventTarget, L = EventListener>(
|
|
5
|
+
element: T,
|
|
6
6
|
eventName: string,
|
|
7
|
-
listener:
|
|
7
|
+
listener: L,
|
|
8
8
|
options?: AddEventListenerOptions,
|
|
9
9
|
) => {
|
|
10
10
|
const ops = options || false;
|
|
11
|
-
element.removeEventListener(eventName, listener, ops);
|
|
11
|
+
element.removeEventListener(eventName, listener as EventListenerOrEventListenerObject, ops);
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export default off;
|
package/src/event/on.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Add eventListener to an `EventTarget` object.
|
|
3
3
|
*/
|
|
4
|
-
const on = (
|
|
5
|
-
element:
|
|
4
|
+
const on = <T extends EventTarget, L = EventListener>(
|
|
5
|
+
element: T,
|
|
6
6
|
eventName: string,
|
|
7
|
-
listener:
|
|
7
|
+
listener: L,
|
|
8
8
|
options?: AddEventListenerOptions,
|
|
9
9
|
) => {
|
|
10
10
|
const ops = options || false;
|
|
11
|
-
element.addEventListener(eventName, listener, ops);
|
|
11
|
+
element.addEventListener(eventName, listener as EventListenerOrEventListenerObject, ops);
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
export default on;
|
package/src/event/one.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import on from './on';
|
|
2
2
|
import off from './off';
|
|
3
|
+
import { NativeEventHandler } from '../interface/event';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Add an `eventListener` to an `EventTarget`
|
|
6
7
|
* element and remove it once callback is called.
|
|
7
8
|
*/
|
|
8
|
-
const one = (
|
|
9
|
-
element:
|
|
9
|
+
const one = <T extends EventTarget, L = EventListener>(
|
|
10
|
+
element: T,
|
|
10
11
|
eventName: string,
|
|
11
|
-
listener:
|
|
12
|
+
listener: L,
|
|
12
13
|
options?: AddEventListenerOptions,
|
|
13
14
|
) => {
|
|
14
15
|
/** Wrap the listener for easy on -> off */
|
|
15
|
-
const handlerWrapper =
|
|
16
|
+
const handlerWrapper: NativeEventHandler<T> = e => {
|
|
16
17
|
/* istanbul ignore else */
|
|
17
18
|
if (e.target === element || e.currentTarget === element) {
|
|
18
|
-
listener.apply(element, [e]);
|
|
19
|
+
(listener as NativeEventHandler<T>).apply(element, [e]);
|
|
19
20
|
off(element, eventName, handlerWrapper, options);
|
|
20
21
|
}
|
|
21
22
|
};
|
package/src/index.ts
CHANGED
|
@@ -96,6 +96,7 @@ import keyCapsLock from './strings/keyCapsLock';
|
|
|
96
96
|
import keyControl from './strings/keyControl';
|
|
97
97
|
import keyDelete from './strings/keyDelete';
|
|
98
98
|
import keyEnter from './strings/keyEnter';
|
|
99
|
+
import keyNumpadEnter from './strings/keyNumpadEnter';
|
|
99
100
|
import keyEscape from './strings/keyEscape';
|
|
100
101
|
import keyInsert from './strings/keyInsert';
|
|
101
102
|
import keyMeta from './strings/keyMeta';
|
|
@@ -185,6 +186,7 @@ import ObjectEntries from './misc/ObjectEntries';
|
|
|
185
186
|
import ObjectHasOwn from './misc/ObjectHasOwn';
|
|
186
187
|
import ObjectKeys from './misc/ObjectKeys';
|
|
187
188
|
import ObjectValues from './misc/ObjectValues';
|
|
189
|
+
import ObjectFromEntries from './misc/ObjectFromEntries';
|
|
188
190
|
import createCustomEvent from './misc/createCustomEvent';
|
|
189
191
|
import passiveHandler from './misc/passiveHandler';
|
|
190
192
|
import reflow from './misc/reflow';
|
|
@@ -249,6 +251,8 @@ import getElementsByTagName from './selectors/getElementsByTagName';
|
|
|
249
251
|
import getElementsByClassName from './selectors/getElementsByClassName';
|
|
250
252
|
import matches from './selectors/matches';
|
|
251
253
|
|
|
254
|
+
import { version } from '../package.json';
|
|
255
|
+
|
|
252
256
|
export {
|
|
253
257
|
ariaChecked,
|
|
254
258
|
ariaDescription,
|
|
@@ -359,6 +363,7 @@ export {
|
|
|
359
363
|
keyControl,
|
|
360
364
|
keyDelete,
|
|
361
365
|
keyEnter,
|
|
366
|
+
keyNumpadEnter,
|
|
362
367
|
keyEscape,
|
|
363
368
|
keyInsert,
|
|
364
369
|
keyMeta,
|
|
@@ -453,6 +458,7 @@ export {
|
|
|
453
458
|
ObjectEntries,
|
|
454
459
|
ObjectKeys,
|
|
455
460
|
ObjectValues,
|
|
461
|
+
ObjectFromEntries,
|
|
456
462
|
getBoundingClientRect,
|
|
457
463
|
getDocument,
|
|
458
464
|
getDocumentBody,
|
|
@@ -468,4 +474,7 @@ export {
|
|
|
468
474
|
setAttributeNS,
|
|
469
475
|
removeAttribute,
|
|
470
476
|
removeAttributeNS,
|
|
477
|
+
version,
|
|
471
478
|
};
|
|
479
|
+
|
|
480
|
+
export * from './interface/event.d';
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions addapted from React 18.2
|
|
3
|
+
* Project: https://react.dev/
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
type NativeAnimationEvent = AnimationEvent;
|
|
7
|
+
type NativeClipboardEvent = ClipboardEvent;
|
|
8
|
+
type NativeCompositionEvent = CompositionEvent;
|
|
9
|
+
type NativeDragEvent = DragEvent;
|
|
10
|
+
type NativeFocusEvent = FocusEvent;
|
|
11
|
+
type NativeKeyboardEvent = KeyboardEvent;
|
|
12
|
+
type NativeMouseEvent = MouseEvent;
|
|
13
|
+
type NativeTouchEvent = TouchEvent;
|
|
14
|
+
type NativePointerEvent = PointerEvent;
|
|
15
|
+
type NativeTransitionEvent = TransitionEvent;
|
|
16
|
+
type NativeUIEvent = UIEvent;
|
|
17
|
+
type NativeWheelEvent = WheelEvent;
|
|
18
|
+
|
|
19
|
+
interface AbstractView {
|
|
20
|
+
styleMedia: StyleMedia;
|
|
21
|
+
document: Document;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//
|
|
25
|
+
// Event System
|
|
26
|
+
// ----------------------------------------------------------------------
|
|
27
|
+
// E = EventObject, C = e.currentTarget, T = e.target
|
|
28
|
+
interface BaseEvent<E = Event, C = unknown, T = unknown> {
|
|
29
|
+
nativeEvent: E;
|
|
30
|
+
currentTarget: C | null;
|
|
31
|
+
target: T & EventTarget;
|
|
32
|
+
bubbles: boolean;
|
|
33
|
+
cancelable: boolean;
|
|
34
|
+
defaultPrevented: boolean;
|
|
35
|
+
eventPhase: number;
|
|
36
|
+
isTrusted: boolean;
|
|
37
|
+
preventDefault(): void;
|
|
38
|
+
isDefaultPrevented(): boolean;
|
|
39
|
+
stopPropagation(): void;
|
|
40
|
+
isPropagationStopped(): boolean;
|
|
41
|
+
persist(): void;
|
|
42
|
+
timeStamp: number;
|
|
43
|
+
type: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* currentTarget - a reference to the element on which the event listener is registered.
|
|
48
|
+
*
|
|
49
|
+
* target - a reference to the element from which the event was originally dispatched.
|
|
50
|
+
* This might be a child element to the element on which the event listener is registered.
|
|
51
|
+
* If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11508#issuecomment-256045682
|
|
52
|
+
*/
|
|
53
|
+
type NativeEvent<T = Element, E = Event> = BaseEvent<E, T, T>;
|
|
54
|
+
|
|
55
|
+
interface ClipboardEvent<T = Element> extends NativeEvent<T, NativeClipboardEvent> {
|
|
56
|
+
clipboardData: DataTransfer;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface CompositionEvent<T = Element> extends NativeEvent<T, NativeCompositionEvent> {
|
|
60
|
+
data: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface DragEvent<T = Element> extends MouseEvent<T, NativeDragEvent> {
|
|
64
|
+
dataTransfer: DataTransfer;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface PointerEvent<T = Element> extends MouseEvent<T, NativePointerEvent> {
|
|
68
|
+
pointerId: number;
|
|
69
|
+
pressure: number;
|
|
70
|
+
tangentialPressure: number;
|
|
71
|
+
tiltX: number;
|
|
72
|
+
tiltY: number;
|
|
73
|
+
twist: number;
|
|
74
|
+
width: number;
|
|
75
|
+
height: number;
|
|
76
|
+
pointerType: 'mouse' | 'pen' | 'touch';
|
|
77
|
+
isPrimary: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface FocusEvent<T = Element, R = Element> extends NativeEvent<T, NativeFocusEvent> {
|
|
81
|
+
relatedTarget: (EventTarget & R) | null;
|
|
82
|
+
target: EventTarget & T;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
type FormControl = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
86
|
+
type FormEvent<T = FormControl> = NativeEvent<T>;
|
|
87
|
+
|
|
88
|
+
interface ChangeEvent<T = FormControl> extends FormEvent<T> {
|
|
89
|
+
target: EventTarget & T;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type ModifierKey =
|
|
93
|
+
| 'Alt'
|
|
94
|
+
| 'AltGraph'
|
|
95
|
+
| 'CapsLock'
|
|
96
|
+
| 'Control'
|
|
97
|
+
| 'Fn'
|
|
98
|
+
| 'FnLock'
|
|
99
|
+
| 'Hyper'
|
|
100
|
+
| 'Meta'
|
|
101
|
+
| 'NumLock'
|
|
102
|
+
| 'ScrollLock'
|
|
103
|
+
| 'Shift'
|
|
104
|
+
| 'Super'
|
|
105
|
+
| 'Symbol'
|
|
106
|
+
| 'SymbolLock';
|
|
107
|
+
|
|
108
|
+
interface KeyboardEvent<T = Element> extends UIEvent<T, NativeKeyboardEvent> {
|
|
109
|
+
altKey: boolean;
|
|
110
|
+
/** @deprecated */
|
|
111
|
+
charCode: number;
|
|
112
|
+
ctrlKey: boolean;
|
|
113
|
+
code: string;
|
|
114
|
+
/**
|
|
115
|
+
* See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
|
|
116
|
+
*/
|
|
117
|
+
getModifierState(key: ModifierKey): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* See the [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#named-key-attribute-values). for possible values
|
|
120
|
+
*/
|
|
121
|
+
key: string;
|
|
122
|
+
/** @deprecated */
|
|
123
|
+
keyCode: number;
|
|
124
|
+
locale: string;
|
|
125
|
+
location: number;
|
|
126
|
+
metaKey: boolean;
|
|
127
|
+
repeat: boolean;
|
|
128
|
+
shiftKey: boolean;
|
|
129
|
+
/** @deprecated */
|
|
130
|
+
which: number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
interface MouseEvent<T = Element, E = NativeMouseEvent> extends UIEvent<T, E> {
|
|
134
|
+
altKey: boolean;
|
|
135
|
+
button: number;
|
|
136
|
+
buttons: number;
|
|
137
|
+
clientX: number;
|
|
138
|
+
clientY: number;
|
|
139
|
+
ctrlKey: boolean;
|
|
140
|
+
/**
|
|
141
|
+
* See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
|
|
142
|
+
*/
|
|
143
|
+
getModifierState(key: ModifierKey): boolean;
|
|
144
|
+
metaKey: boolean;
|
|
145
|
+
movementX: number;
|
|
146
|
+
movementY: number;
|
|
147
|
+
pageX: number;
|
|
148
|
+
pageY: number;
|
|
149
|
+
relatedTarget: EventTarget | null;
|
|
150
|
+
screenX: number;
|
|
151
|
+
screenY: number;
|
|
152
|
+
shiftKey: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
interface TouchEvent<T = Element> extends UIEvent<T, NativeTouchEvent> {
|
|
156
|
+
altKey: boolean;
|
|
157
|
+
changedTouches: TouchList;
|
|
158
|
+
ctrlKey: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* See [DOM Level 3 Events spec](https://www.w3.org/TR/uievents-key/#keys-modifier). for a list of valid (case-sensitive) arguments to this method.
|
|
161
|
+
*/
|
|
162
|
+
getModifierState(key: ModifierKey): boolean;
|
|
163
|
+
metaKey: boolean;
|
|
164
|
+
shiftKey: boolean;
|
|
165
|
+
targetTouches: TouchList;
|
|
166
|
+
touches: TouchList;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface UIEvent<T = Element, E = NativeUIEvent> extends NativeEvent<T, E> {
|
|
170
|
+
detail: number;
|
|
171
|
+
view: AbstractView;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
interface WheelEvent<T = Element> extends MouseEvent<T, NativeWheelEvent> {
|
|
175
|
+
deltaMode: number;
|
|
176
|
+
deltaX: number;
|
|
177
|
+
deltaY: number;
|
|
178
|
+
deltaZ: number;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface AnimationEvent<T = Element> extends NativeEvent<T, NativeAnimationEvent> {
|
|
182
|
+
animationName: string;
|
|
183
|
+
elapsedTime: number;
|
|
184
|
+
pseudoElement: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
interface TransitionEvent<T = Element> extends NativeEvent<T, NativeTransitionEvent> {
|
|
188
|
+
elapsedTime: number;
|
|
189
|
+
propertyName: string;
|
|
190
|
+
pseudoElement: string;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
//
|
|
194
|
+
// Event Handler Types
|
|
195
|
+
// ----------------------------------------------------------------------
|
|
196
|
+
// (this: unknown & EventTarget, event: E): void;
|
|
197
|
+
type EventHandler<T = Element, E = Event | NativeEvent<T>> = (event: E) => void;
|
|
198
|
+
type NativeEventHandler<T = Element> = EventHandler<T, NativeEvent<T>>;
|
|
199
|
+
type ClipboardEventHandler<T = Element> = EventHandler<T, ClipboardEvent<T>>;
|
|
200
|
+
type CompositionEventHandler<T = Element> = EventHandler<T, CompositionEvent<T>>;
|
|
201
|
+
type DragEventHandler<T = Element> = EventHandler<T, DragEvent<T>>;
|
|
202
|
+
type FocusEventHandler<T = Element> = EventHandler<T, FocusEvent<T>>;
|
|
203
|
+
type FormEventHandler<T = Element> = EventHandler<T, FormEvent<T>>;
|
|
204
|
+
type ChangeEventHandler<T = Element> = EventHandler<T, ChangeEvent<T>>;
|
|
205
|
+
type KeyboardEventHandler<T = Element> = EventHandler<T, KeyboardEvent<T>>;
|
|
206
|
+
type MouseEventHandler<T = Element> = EventHandler<T, MouseEvent<T>>;
|
|
207
|
+
type TouchEventHandler<T = Element> = EventHandler<T, TouchEvent<T>>;
|
|
208
|
+
type PointerEventHandler<T = Element> = EventHandler<T, PointerEvent<T>>;
|
|
209
|
+
type UIEventHandler<T = Element> = EventHandler<T, UIEvent<T>>;
|
|
210
|
+
type WheelEventHandler<T = Element> = EventHandler<T, WheelEvent<T>>;
|
|
211
|
+
type AnimationEventHandler<T = Element> = EventHandler<T, AnimationEvent<T>>;
|
|
212
|
+
type TransitionEventHandler<T = Element> = EventHandler<T, TransitionEvent<T>>;
|
|
213
|
+
type PossibleEventTarget = EventTarget & (Element | Document | Window);
|
|
214
|
+
|
|
215
|
+
export {
|
|
216
|
+
NativeEvent,
|
|
217
|
+
ClipboardEvent,
|
|
218
|
+
CompositionEvent,
|
|
219
|
+
DragEvent,
|
|
220
|
+
FocusEvent,
|
|
221
|
+
FormEvent,
|
|
222
|
+
ChangeEvent,
|
|
223
|
+
KeyboardEvent,
|
|
224
|
+
MouseEvent,
|
|
225
|
+
TouchEvent,
|
|
226
|
+
PointerEvent,
|
|
227
|
+
UIEvent,
|
|
228
|
+
WheelEvent,
|
|
229
|
+
AnimationEvent,
|
|
230
|
+
TransitionEvent,
|
|
231
|
+
NativeEventHandler,
|
|
232
|
+
ClipboardEventHandler,
|
|
233
|
+
CompositionEventHandler,
|
|
234
|
+
DragEventHandler,
|
|
235
|
+
FocusEventHandler,
|
|
236
|
+
FormEventHandler,
|
|
237
|
+
ChangeEventHandler,
|
|
238
|
+
KeyboardEventHandler,
|
|
239
|
+
MouseEventHandler,
|
|
240
|
+
TouchEventHandler,
|
|
241
|
+
PointerEventHandler,
|
|
242
|
+
UIEventHandler,
|
|
243
|
+
WheelEventHandler,
|
|
244
|
+
AnimationEventHandler,
|
|
245
|
+
TransitionEventHandler,
|
|
246
|
+
PossibleEventTarget,
|
|
247
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Object.fromEntries()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param entries a target entries object
|
|
5
|
+
* @returns a new Object created from the specified entries in array format [key, value][]
|
|
6
|
+
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
|
+
*/
|
|
8
|
+
const ObjectFromEntries = <K extends string, V>(entries: [K, V][]) =>
|
|
9
|
+
Object.fromEntries(entries) as Record<K, V>;
|
|
10
|
+
|
|
11
|
+
export default ObjectFromEntries;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import isString from '../is/isString';
|
|
2
|
-
import setAttribute from '../attr/setAttribute';
|
|
3
2
|
import getDocument from '../get/getDocument';
|
|
4
|
-
import
|
|
3
|
+
import ObjectAssign from './ObjectAssign';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Shortie for `document.createElement` method
|
|
@@ -14,11 +13,11 @@ import ObjectEntries from './ObjectEntries';
|
|
|
14
13
|
* @param param `tagName` or object
|
|
15
14
|
* @return a new `HTMLElement`
|
|
16
15
|
*/
|
|
17
|
-
const createElement = (param?: string | Partial<
|
|
16
|
+
const createElement = <T extends HTMLElement>(param?: string | Partial<T>): T | undefined => {
|
|
18
17
|
if (!param) return undefined;
|
|
19
18
|
|
|
20
19
|
if (isString(param)) {
|
|
21
|
-
return getDocument().createElement(param);
|
|
20
|
+
return getDocument().createElement(param) as T;
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
const { tagName } = param;
|
|
@@ -29,12 +28,7 @@ const createElement = (param?: string | Partial<HTMLElement>): HTMLElement | und
|
|
|
29
28
|
const attr = { ...(param as Record<string, unknown>) };
|
|
30
29
|
delete attr.tagName;
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
if (isString(key as string) && isString(value as string)) {
|
|
34
|
-
setAttribute(newElement, key as string, value as string);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
return newElement;
|
|
31
|
+
return ObjectAssign(newElement, attr) as unknown as T;
|
|
38
32
|
};
|
|
39
33
|
|
|
40
34
|
export default createElement;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import getDocument from '../get/getDocument';
|
|
2
|
-
import
|
|
3
|
-
import setAttribute from '../attr/setAttribute';
|
|
2
|
+
import ObjectAssign from './ObjectAssign';
|
|
4
3
|
import isString from '../is/isString';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -8,6 +7,8 @@ import isString from '../is/isString';
|
|
|
8
7
|
* which allows you to create a new `HTMLElement` for a given `tagName`
|
|
9
8
|
* or based on an object with specific non-readonly attributes with string values:
|
|
10
9
|
* `id`, `className`, `textContent`, `style`, etc.
|
|
10
|
+
* Note: some elements resulted from this function call may not be compatible with
|
|
11
|
+
* some attributes.
|
|
11
12
|
*
|
|
12
13
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS
|
|
13
14
|
*
|
|
@@ -15,14 +16,14 @@ import isString from '../is/isString';
|
|
|
15
16
|
* @param param `tagName` or object
|
|
16
17
|
* @return a new `HTMLElement`
|
|
17
18
|
*/
|
|
18
|
-
const createElementNS = (
|
|
19
|
+
const createElementNS = <T extends HTMLElement>(
|
|
19
20
|
ns: string,
|
|
20
|
-
param?: string | Partial<
|
|
21
|
-
):
|
|
21
|
+
param?: string | Partial<T>,
|
|
22
|
+
): T | undefined => {
|
|
22
23
|
if (!ns || !param) return undefined;
|
|
23
24
|
|
|
24
25
|
if (isString(param)) {
|
|
25
|
-
return getDocument().createElementNS(ns, param) as
|
|
26
|
+
return getDocument().createElementNS(ns, param) as T;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
const { tagName } = param;
|
|
@@ -33,13 +34,7 @@ const createElementNS = (
|
|
|
33
34
|
const attr = { ...(param as Record<string, unknown>) };
|
|
34
35
|
delete attr.tagName;
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
if (isString(key as string) && isString(value as string)) {
|
|
38
|
-
setAttribute(newElement, key as string, value as string);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return newElement;
|
|
37
|
+
return ObjectAssign(newElement, attr) as unknown as T;
|
|
43
38
|
};
|
|
44
39
|
|
|
45
40
|
export default createElementNS;
|
package/src/misc/data.ts
CHANGED