aboutsummaryrefslogtreecommitdiff
path: root/src/ClientUtils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClientUtils.ts')
-rw-r--r--src/ClientUtils.ts114
1 files changed, 59 insertions, 55 deletions
diff --git a/src/ClientUtils.ts b/src/ClientUtils.ts
index d03ae1486..55801df81 100644
--- a/src/ClientUtils.ts
+++ b/src/ClientUtils.ts
@@ -7,17 +7,17 @@ import { CollectionViewType, DocumentType } from './client/documents/DocumentTyp
import { Colors } from './client/views/global/globalEnums';
import { CreateImage } from './client/views/nodes/WebBoxRenderer';
-export function DashColor(color: string) {
+export function DashColor(color: string | undefined) {
try {
return color ? Color(color.toLowerCase()) : Color('transparent');
} catch (e) {
- if (color.includes('gradient')) console.log("using color 'white' in place of :" + color);
+ if (color?.includes('gradient')) console.log("using color 'white' in place of :" + color);
else console.log('COLOR error:', e);
return Color('white');
}
}
-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;
@@ -82,10 +82,6 @@ export function returnEmptyFilter() {
return [] as string[];
}
-export function returnEmptyDoclist() {
- return [] as any[];
-}
-
export namespace ClientUtils {
export const CLICK_TIME = 300;
export const DRAG_THRESHOLD = 4;
@@ -350,9 +346,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];
@@ -368,8 +364,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];
});
@@ -449,40 +445,45 @@ export function smoothScrollHorizontal(duration: number, element: HTMLElement |
animateScroll();
}
-export function addStyleSheet(styleType: string = 'text/css') {
+export function addStyleSheet() {
const style = document.createElement('style');
- style.type = styleType;
const sheets = document.head.appendChild(style);
- return (sheets as any).sheet;
+ return sheets.sheet;
}
-export function addStyleSheetRule(sheet: any, selector: any, css: any, selectorPrefix = '.') {
+export function addStyleSheetRule(sheet: CSSStyleSheet | null, selector: string, css: string | { [key: string]: string }, selectorPrefix = '.') {
const propText =
typeof css === 'string'
? css
: Object.keys(css)
.map(p => p + ':' + (p === 'content' ? "'" + css[p] + "'" : css[p]))
.join(';');
- return sheet.insertRule(selectorPrefix + selector + '{' + propText + '}', sheet.cssRules.length);
+ return sheet?.insertRule(selectorPrefix + selector + '{' + propText + '}', sheet.cssRules.length);
}
-export function removeStyleSheetRule(sheet: any, rule: number) {
- if (sheet.rules.length) {
+export function removeStyleSheetRule(sheet: CSSStyleSheet | null, rule: number) {
+ if (sheet?.rules.length) {
sheet.removeRule(rule);
return true;
}
return false;
}
-export function clearStyleSheetRules(sheet: any) {
- if (sheet.rules.length) {
+export function clearStyleSheetRules(sheet: CSSStyleSheet | null) {
+ if (sheet?.rules.length) {
numberRange(sheet.rules.length).map(() => sheet.removeRule(0));
return true;
}
return false;
}
+export class simPointerEvent extends PointerEvent {
+ dash?: boolean;
+}
+export class simMouseEvent extends MouseEvent {
+ dash?: boolean;
+}
export function simulateMouseClick(element: Element | null | undefined, x: number, y: number, sx: number, sy: number, rightClick = true) {
if (!element) return;
['pointerdown', 'pointerup'].forEach(event => {
- const me = new PointerEvent(event, {
+ const me = new simPointerEvent(event, {
view: window,
bubbles: true,
cancelable: true,
@@ -493,12 +494,12 @@ export function simulateMouseClick(element: Element | null | undefined, x: numbe
screenX: sx,
screenY: sy,
});
- (me as any).dash = true;
+ me.dash = true;
element.dispatchEvent(me);
});
if (rightClick) {
- const me = new MouseEvent('contextmenu', {
+ const me = new simMouseEvent('contextmenu', {
view: window,
bubbles: true,
cancelable: true,
@@ -510,12 +511,12 @@ export function simulateMouseClick(element: Element | null | undefined, x: numbe
screenX: sx,
screenY: sy,
});
- (me as any).dash = true;
+ me.dash = true;
element.dispatchEvent(me);
}
}
-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) {
@@ -528,7 +529,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;
@@ -536,16 +537,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;
});
@@ -570,17 +573,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;
@@ -588,13 +592,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);
@@ -615,16 +619,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) {
@@ -636,11 +640,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) {
@@ -703,7 +707,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();
@@ -713,9 +717,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));
}