diff options
author | bobzel <zzzman@gmail.com> | 2024-08-08 12:27:40 -0400 |
---|---|---|
committer | bobzel <zzzman@gmail.com> | 2024-08-08 12:27:40 -0400 |
commit | 4574b7f03ccc85c4bebdbfd9475788456086704f (patch) | |
tree | d23d30343541b9af029ef418492d629d3cc710d7 /src/ClientUtils.ts | |
parent | e1db06d59d580aa640212a0d3a6aeecb9122bdf0 (diff) |
many changes to add typing in place of 'any's etc
Diffstat (limited to 'src/ClientUtils.ts')
-rw-r--r-- | src/ClientUtils.ts | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/src/ClientUtils.ts b/src/ClientUtils.ts index fc415d589..f9e282993 100644 --- a/src/ClientUtils.ts +++ b/src/ClientUtils.ts @@ -17,7 +17,7 @@ export function DashColor(color: string | undefined) { } } -export function lightOrDark(color: any) { +export function lightOrDark(color: string | undefined) { if (color === 'transparent' || !color) return Colors.BLACK; if (color.startsWith?.('linear')) return Colors.BLACK; if (DashColor(color).isLight()) return Colors.BLACK; @@ -351,9 +351,9 @@ export namespace ClientUtils { } } -export function OmitKeys(obj: any, keys: string[], pattern?: string, addKeyFunc?: (dup: any) => void): { omit: any; extract: any } { - const omit: any = { ...obj }; - const extract: any = {}; +export function OmitKeys(obj: object, keys: string[], pattern?: string, addKeyFunc?: (dup: object) => void): { omit: { [key: string]: unknown }; extract: { [key: string]: unknown } } { + const omit: { [key: string]: unknown } = { ...obj }; + const extract: { [key: string]: unknown } = {}; keys.forEach(key => { extract[key] = omit[key]; delete omit[key]; @@ -369,8 +369,8 @@ export function OmitKeys(obj: any, keys: string[], pattern?: string, addKeyFunc? return { omit, extract }; } -export function WithKeys(obj: any, keys: string[], addKeyFunc?: (dup: any) => void) { - const dup: any = {}; +export function WithKeys(obj: object & { [key: string]: unknown }, keys: string[], addKeyFunc?: (dup: unknown) => void) { + const dup: { [key: string]: unknown } = {}; keys.forEach(key => { dup[key] = obj[key]; }); @@ -521,7 +521,7 @@ export function simulateMouseClick(element: Element | null | undefined, x: numbe } } -export function getWordAtPoint(elem: any, x: number, y: number): string | undefined { +export function getWordAtPoint(elem: Element, x: number, y: number): string | undefined { if (elem.tagName === 'INPUT') return 'input'; if (elem.tagName === 'TEXTAREA') return 'textarea'; if (elem.nodeType === elem.TEXT_NODE || elem.textContent) { @@ -534,7 +534,7 @@ export function getWordAtPoint(elem: any, x: number, y: number): string | undefi range.setEnd(elem, currentPos + 1); const rangeRect = range.getBoundingClientRect(); if (rangeRect.left <= x && rangeRect.right >= x && rangeRect.top <= y && rangeRect.bottom >= y) { - range.expand?.('word'); // doesn't exist in firefox + 'expand' in range && (range.expand as (val: string) => void)('word'); // doesn't exist in firefox const ret = range.toString(); range.detach(); return ret; @@ -542,16 +542,18 @@ export function getWordAtPoint(elem: any, x: number, y: number): string | undefi currentPos += 1; } } else { - Array.from(elem.childNodes).forEach((childNode: any) => { - const range = childNode.ownerDocument.createRange(); - range.selectNodeContents(childNode); - const rangeRect = range.getBoundingClientRect(); - if (rangeRect.left <= x && rangeRect.right >= x && rangeRect.top <= y && rangeRect.bottom >= y) { - range.detach(); - const word = getWordAtPoint(childNode, x, y); - if (word) return word; - } else { - range.detach(); + Array.from(elem.children).forEach(childNode => { + const range = childNode.ownerDocument?.createRange(); + if (range) { + range.selectNodeContents(childNode); + const rangeRect = range.getBoundingClientRect(); + if (rangeRect.left <= x && rangeRect.right >= x && rangeRect.top <= y && rangeRect.bottom >= y) { + range.detach(); + const word = getWordAtPoint(childNode, x, y); + if (word) return word; + } else { + range.detach(); + } } return undefined; }); @@ -576,17 +578,18 @@ export function setupMoveUpEvents( target: object, e: React.PointerEvent, moveEvent: (e: PointerEvent, down: number[], delta: number[]) => boolean, - upEvent: (e: PointerEvent, movement: number[], isClick: boolean) => any, - clickEvent: (e: PointerEvent, doubleTap?: boolean) => any, + upEvent: (e: PointerEvent, movement: number[], isClick: boolean) => void, + clickEvent: (e: PointerEvent, doubleTap?: boolean) => unknown, // eslint-disable-next-line default-param-last stopPropagation: boolean = true, // eslint-disable-next-line default-param-last stopMovePropagation: boolean = true, noDoubleTapTimeout?: () => void ) { - const targetAny = target as any; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const targetAny: object & { _downX: number; _downY: number; _lastX: number; _lastY: number; _doubleTap: boolean; _doubleTime?: NodeJS.Timeout; _lastTap: number; _noClick: boolean } = target as any; const doubleTapTimeout = 300; - targetAny._doubleTap = Date.now() - (target as any)._lastTap < doubleTapTimeout; + targetAny._doubleTap = Date.now() - targetAny._lastTap < doubleTapTimeout; targetAny._lastTap = Date.now(); targetAny._downX = targetAny._lastX = e.clientX; targetAny._downY = targetAny._lastY = e.clientY; @@ -594,13 +597,13 @@ export function setupMoveUpEvents( let moving = false; const _moveEvent = (moveEv: PointerEvent): void => { - if (moving || Math.abs(moveEv.clientX - (target as any)._downX) > ClientUtils.DRAG_THRESHOLD || Math.abs(moveEv.clientY - (target as any)._downY) > ClientUtils.DRAG_THRESHOLD) { + if (moving || Math.abs(moveEv.clientX - targetAny._downX) > ClientUtils.DRAG_THRESHOLD || Math.abs(moveEv.clientY - targetAny._downY) > ClientUtils.DRAG_THRESHOLD) { moving = true; - if ((target as any)._doubleTime) { - clearTimeout((target as any)._doubleTime); + if (targetAny._doubleTime) { + targetAny._doubleTime && clearTimeout(targetAny._doubleTime); targetAny._doubleTime = undefined; } - if (moveEvent(moveEv, [(target as any)._downX, (target as any)._downY], [moveEv.clientX - (target as any)._lastX, moveEv.clientY - (target as any)._lastY])) { + if (moveEvent(moveEv, [targetAny._downX, targetAny._downY], [moveEv.clientX - targetAny._lastX, moveEv.clientY - targetAny._lastY])) { document.removeEventListener('pointermove', _moveEvent); // eslint-disable-next-line no-use-before-define document.removeEventListener('pointerup', _upEvent); @@ -621,16 +624,16 @@ export function setupMoveUpEvents( }, doubleTapTimeout); } if (targetAny._doubleTime && targetAny._doubleTap) { - clearTimeout(targetAny._doubleTime); + targetAny._doubleTime && clearTimeout(targetAny._doubleTime); targetAny._doubleTime = undefined; } - targetAny._noClick = clickEvent(upEv, targetAny._doubleTap); + targetAny._noClick = clickEvent(upEv, targetAny._doubleTap) ? true : false; } document.removeEventListener('pointermove', _moveEvent); document.removeEventListener('pointerup', _upEvent, true); }; const _clickEvent = (clickev: MouseEvent): void => { - if ((target as any)._noClick) clickev.stopPropagation(); + if (targetAny._noClick) clickev.stopPropagation(); document.removeEventListener('click', _clickEvent, true); }; if (stopPropagation) { @@ -642,11 +645,11 @@ export function setupMoveUpEvents( document.addEventListener('click', _clickEvent, true); } -export function DivHeight(ele: HTMLElement): number { - return Number(getComputedStyle(ele).height.replace('px', '')); +export function DivHeight(ele: HTMLElement | null): number { + return ele ? Number(getComputedStyle(ele).height.replace('px', '')) : 0; } -export function DivWidth(ele: HTMLElement): number { - return Number(getComputedStyle(ele).width.replace('px', '')); +export function DivWidth(ele: HTMLElement | null): number { + return ele ? Number(getComputedStyle(ele).width.replace('px', '')) : 0; } export function dateRangeStrToDates(dateStr: string) { @@ -709,7 +712,7 @@ export function UpdateIcon( realNativeHeight: number, noSuffix: boolean, replaceRootFilename: string | undefined, - cb: (iconFile: string, nativeWidth: number, nativeHeight: number) => any + cb: (iconFile: string, nativeWidth: number, nativeHeight: number) => void ) { const newDiv = docViewContent.cloneNode(true) as HTMLDivElement; newDiv.style.width = width.toString(); @@ -719,9 +722,9 @@ export function UpdateIcon( const nativeWidth = width; const nativeHeight = height; return CreateImage(ClientUtils.prepend(''), document.styleSheets, htmlString, nativeWidth, (nativeWidth * panelHeight) / panelWidth, (scrollTop * panelHeight) / realNativeHeight) - .then(async (dataUrl: any) => { + .then(async dataUrl => { const returnedFilename = await ClientUtils.convertDataUri(dataUrl, filename, noSuffix, replaceRootFilename); cb(returnedFilename as string, nativeWidth, nativeHeight); }) - .catch((error: any) => console.error('oops, something went wrong!', error)); + .catch(error => console.error('oops, something went wrong!', error)); } |