4
4
* N milliseconds. If `immediate` is passed, trigger the function on the
5
5
* leading edge, instead of the trailing.
6
6
* @attribution https://davidwalsh.name/javascript-debounce-function
7
- * @param {Function } func
8
- * @param {Number } waitMs
9
- * @param {Boolean } immediate
10
- * @returns {Function }
11
7
*/
12
- export function debounce ( func , waitMs , immediate ) {
13
- let timeout ;
14
- return function debouncedWrapper ( ...args ) {
15
- const context = this ;
8
+ export function debounce ( func : Function , waitMs : number , immediate : boolean ) : Function {
9
+ let timeout : number | null = null ;
10
+ return function debouncedWrapper ( this : any , ...args : any [ ] ) {
11
+ const context : any = this ;
16
12
const later = function debouncedTimeout ( ) {
17
13
timeout = null ;
18
14
if ( ! immediate ) func . apply ( context , args ) ;
19
15
} ;
20
16
const callNow = immediate && ! timeout ;
21
- clearTimeout ( timeout ) ;
22
- timeout = setTimeout ( later , waitMs ) ;
17
+ if ( timeout ) {
18
+ clearTimeout ( timeout ) ;
19
+ }
20
+ timeout = window . setTimeout ( later , waitMs ) ;
23
21
if ( callNow ) func . apply ( context , args ) ;
24
22
} ;
25
23
}
26
24
25
+ function isDetailsElement ( element : HTMLElement ) : element is HTMLDetailsElement {
26
+ return element . nodeName === 'DETAILS' ;
27
+ }
28
+
27
29
/**
28
- * Scroll and highlight an element.
29
- * @param {HTMLElement } element
30
+ * Scroll-to and highlight an element.
30
31
*/
31
- export function scrollAndHighlightElement ( element ) {
32
+ export function scrollAndHighlightElement ( element : HTMLElement ) : void {
32
33
if ( ! element ) return ;
33
34
35
+ // Open up parent <details> elements if within
34
36
let parent = element ;
35
37
while ( parent . parentElement ) {
36
38
parent = parent . parentElement ;
37
- if ( parent . nodeName === 'DETAILS' && ! parent . open ) {
39
+ if ( isDetailsElement ( parent ) && ! parent . open ) {
38
40
parent . open = true ;
39
41
}
40
42
}
@@ -44,15 +46,15 @@ export function scrollAndHighlightElement(element) {
44
46
const highlight = getComputedStyle ( document . body ) . getPropertyValue ( '--color-link' ) ;
45
47
element . style . outline = `2px dashed ${ highlight } ` ;
46
48
element . style . outlineOffset = '5px' ;
47
- element . style . transition = null ;
49
+ element . style . removeProperty ( ' transition' ) ;
48
50
setTimeout ( ( ) => {
49
51
element . style . transition = 'outline linear 3s' ;
50
52
element . style . outline = '2px dashed rgba(0, 0, 0, 0)' ;
51
53
const listener = ( ) => {
52
54
element . removeEventListener ( 'transitionend' , listener ) ;
53
- element . style . transition = null ;
54
- element . style . outline = null ;
55
- element . style . outlineOffset = null ;
55
+ element . style . removeProperty ( ' transition' ) ;
56
+ element . style . removeProperty ( ' outline' ) ;
57
+ element . style . removeProperty ( ' outlineOffset' ) ;
56
58
} ;
57
59
element . addEventListener ( 'transitionend' , listener ) ;
58
60
} , 1000 ) ;
@@ -61,10 +63,8 @@ export function scrollAndHighlightElement(element) {
61
63
/**
62
64
* Escape any HTML in the given 'unsafe' string.
63
65
* Take from https://stackoverflow.com/a/6234804.
64
- * @param {String } unsafe
65
- * @returns {string }
66
66
*/
67
- export function escapeHtml ( unsafe ) {
67
+ export function escapeHtml ( unsafe : string ) : string {
68
68
return unsafe
69
69
. replace ( / & / g, '&' )
70
70
. replace ( / < / g, '<' )
@@ -75,32 +75,26 @@ export function escapeHtml(unsafe) {
75
75
76
76
/**
77
77
* Generate a random unique ID.
78
- *
79
- * @returns {string }
80
78
*/
81
- export function uniqueId ( ) {
79
+ export function uniqueId ( ) : string {
82
80
// eslint-disable-next-line no-bitwise
83
81
const S4 = ( ) => ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ;
84
82
return ( `${ S4 ( ) + S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } ${ S4 ( ) } ${ S4 ( ) } ` ) ;
85
83
}
86
84
87
85
/**
88
86
* Generate a random smaller unique ID.
89
- *
90
- * @returns {string }
91
87
*/
92
- export function uniqueIdSmall ( ) {
88
+ export function uniqueIdSmall ( ) : string {
93
89
// eslint-disable-next-line no-bitwise
94
90
const S4 = ( ) => ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ;
95
91
return S4 ( ) ;
96
92
}
97
93
98
94
/**
99
95
* Create a promise that resolves after the given time.
100
- * @param {int } timeMs
101
- * @returns {Promise }
102
96
*/
103
- export function wait ( timeMs ) {
97
+ export function wait ( timeMs : number ) : Promise < any > {
104
98
return new Promise ( res => {
105
99
setTimeout ( res , timeMs ) ;
106
100
} ) ;
0 commit comments