44 * N milliseconds. If `immediate` is passed, trigger the function on the
55 * leading edge, instead of the trailing.
66 * @attribution https://davidwalsh.name/javascript-debounce-function
7- * @param {Function } func
8- * @param {Number } waitMs
9- * @param {Boolean } immediate
10- * @returns {Function }
117 */
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 ;
1612 const later = function debouncedTimeout ( ) {
1713 timeout = null ;
1814 if ( ! immediate ) func . apply ( context , args ) ;
1915 } ;
2016 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 ) ;
2321 if ( callNow ) func . apply ( context , args ) ;
2422 } ;
2523}
2624
25+ function isDetailsElement ( element : HTMLElement ) : element is HTMLDetailsElement {
26+ return element . nodeName === 'DETAILS' ;
27+ }
28+
2729/**
28- * Scroll and highlight an element.
29- * @param {HTMLElement } element
30+ * Scroll-to and highlight an element.
3031 */
31- export function scrollAndHighlightElement ( element ) {
32+ export function scrollAndHighlightElement ( element : HTMLElement ) : void {
3233 if ( ! element ) return ;
3334
35+ // Open up parent <details> elements if within
3436 let parent = element ;
3537 while ( parent . parentElement ) {
3638 parent = parent . parentElement ;
37- if ( parent . nodeName === 'DETAILS' && ! parent . open ) {
39+ if ( isDetailsElement ( parent ) && ! parent . open ) {
3840 parent . open = true ;
3941 }
4042 }
@@ -44,15 +46,15 @@ export function scrollAndHighlightElement(element) {
4446 const highlight = getComputedStyle ( document . body ) . getPropertyValue ( '--color-link' ) ;
4547 element . style . outline = `2px dashed ${ highlight } ` ;
4648 element . style . outlineOffset = '5px' ;
47- element . style . transition = null ;
49+ element . style . removeProperty ( ' transition' ) ;
4850 setTimeout ( ( ) => {
4951 element . style . transition = 'outline linear 3s' ;
5052 element . style . outline = '2px dashed rgba(0, 0, 0, 0)' ;
5153 const listener = ( ) => {
5254 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' ) ;
5658 } ;
5759 element . addEventListener ( 'transitionend' , listener ) ;
5860 } , 1000 ) ;
@@ -61,10 +63,8 @@ export function scrollAndHighlightElement(element) {
6163/**
6264 * Escape any HTML in the given 'unsafe' string.
6365 * Take from https://stackoverflow.com/a/6234804.
64- * @param {String } unsafe
65- * @returns {string }
6666 */
67- export function escapeHtml ( unsafe ) {
67+ export function escapeHtml ( unsafe : string ) : string {
6868 return unsafe
6969 . replace ( / & / g, '&' )
7070 . replace ( / < / g, '<' )
@@ -75,32 +75,26 @@ export function escapeHtml(unsafe) {
7575
7676/**
7777 * Generate a random unique ID.
78- *
79- * @returns {string }
8078 */
81- export function uniqueId ( ) {
79+ export function uniqueId ( ) : string {
8280 // eslint-disable-next-line no-bitwise
8381 const S4 = ( ) => ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ;
8482 return ( `${ S4 ( ) + S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } -${ S4 ( ) } ${ S4 ( ) } ${ S4 ( ) } ` ) ;
8583}
8684
8785/**
8886 * Generate a random smaller unique ID.
89- *
90- * @returns {string }
9187 */
92- export function uniqueIdSmall ( ) {
88+ export function uniqueIdSmall ( ) : string {
9389 // eslint-disable-next-line no-bitwise
9490 const S4 = ( ) => ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ;
9591 return S4 ( ) ;
9692}
9793
9894/**
9995 * Create a promise that resolves after the given time.
100- * @param {int } timeMs
101- * @returns {Promise }
10296 */
103- export function wait ( timeMs ) {
97+ export function wait ( timeMs : number ) : Promise < any > {
10498 return new Promise ( res => {
10599 setTimeout ( res , timeMs ) ;
106100 } ) ;
0 commit comments