Skip to content

Commit 2e8d6ce

Browse files
committed
TS: Coverted util service
1 parent a58102d commit 2e8d6ce

File tree

9 files changed

+32
-38
lines changed

9 files changed

+32
-38
lines changed

resources/js/components/add-remove-rows.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {onChildEvent} from '../services/dom';
2-
import {uniqueId} from '../services/util';
2+
import {uniqueId} from '../services/util.ts';
33
import {Component} from './component';
44

55
/**

resources/js/components/auto-suggest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {escapeHtml} from '../services/util';
1+
import {escapeHtml} from '../services/util.ts';
22
import {onChildEvent} from '../services/dom';
33
import {Component} from './component';
44
import {KeyboardNavigationHandler} from '../services/keyboard-navigation';

resources/js/components/dropdown-search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {debounce} from '../services/util';
1+
import {debounce} from '../services/util.ts';
22
import {transitionHeight} from '../services/animations';
33
import {Component} from './component';
44

resources/js/components/global-search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {htmlToDom} from '../services/dom';
2-
import {debounce} from '../services/util';
2+
import {debounce} from '../services/util.ts';
33
import {KeyboardNavigationHandler} from '../services/keyboard-navigation';
44
import {Component} from './component';
55

resources/js/components/page-display.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as DOM from '../services/dom';
2-
import {scrollAndHighlightElement} from '../services/util';
2+
import {scrollAndHighlightElement} from '../services/util.ts';
33
import {Component} from './component';
44

55
function toggleAnchorHighlighting(elementId, shouldHighlight) {

resources/js/components/page-editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {onSelect} from '../services/dom';
2-
import {debounce} from '../services/util';
2+
import {debounce} from '../services/util.ts';
33
import {Component} from './component';
44
import {utcTimeStampToLocalTime} from '../services/dates.ts';
55

resources/js/markdown/codemirror.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {provideKeyBindings} from './shortcuts';
2-
import {debounce} from '../services/util';
2+
import {debounce} from '../services/util.ts';
33
import {Clipboard} from '../services/clipboard.ts';
44

55
/**

resources/js/services/util.js renamed to resources/js/services/util.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,39 @@
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, '&amp;')
7070
.replace(/</g, '&lt;')
@@ -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
});

resources/js/wysiwyg-tinymce/plugin-drawio.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as DrawIO from '../services/drawio.ts';
2-
import {wait} from '../services/util';
2+
import {wait} from '../services/util.ts';
33

44
let pageEditor = null;
55
let currentNode = null;

0 commit comments

Comments
 (0)