diff options
Diffstat (limited to 'src/shared/utils')
-rw-r--r-- | src/shared/utils/dom.ts (renamed from src/shared/utils/dom.js) | 41 | ||||
-rw-r--r-- | src/shared/utils/keys.ts (renamed from src/shared/utils/keys.js) | 22 | ||||
-rw-r--r-- | src/shared/utils/re.ts (renamed from src/shared/utils/re.js) | 2 |
3 files changed, 45 insertions, 20 deletions
diff --git a/src/shared/utils/dom.js b/src/shared/utils/dom.ts index 974d534..c1f2190 100644 --- a/src/shared/utils/dom.js +++ b/src/shared/utils/dom.ts @@ -1,16 +1,24 @@ -const isContentEditable = (element) => { - return element.hasAttribute('contenteditable') && ( - element.getAttribute('contenteditable').toLowerCase() === 'true' || - element.getAttribute('contenteditable').toLowerCase() === '' - ); +const isContentEditable = (element: Element): boolean => { + let value = element.getAttribute('contenteditable'); + if (value === null) { + return false; + } + return value.toLowerCase() === 'true' || value.toLowerCase() === ''; }; -const rectangleCoordsRect = (coords) => { +interface Rect { + left: number; + top: number; + right: number; + bottom: number; +} + +const rectangleCoordsRect = (coords: string): Rect => { let [left, top, right, bottom] = coords.split(',').map(n => Number(n)); return { left, top, right, bottom }; }; -const circleCoordsRect = (coords) => { +const circleCoordsRect = (coords: string): Rect => { let [x, y, r] = coords.split(',').map(n => Number(n)); return { left: x - r, @@ -20,7 +28,7 @@ const circleCoordsRect = (coords) => { }; }; -const polygonCoordsRect = (coords) => { +const polygonCoordsRect = (coords: string): Rect => { let params = coords.split(','); let minx = Number(params[0]), maxx = Number(params[0]), @@ -46,18 +54,24 @@ const polygonCoordsRect = (coords) => { return { left: minx, top: miny, right: maxx, bottom: maxy }; }; -const viewportRect = (e) => { +const viewportRect = (e: Element): Rect => { if (e.tagName !== 'AREA') { return e.getBoundingClientRect(); } - let mapElement = e.parentNode; - let imgElement = document.querySelector(`img[usemap="#${mapElement.name}"]`); + let mapElement = e.parentNode as HTMLMapElement; + let imgElement = document.querySelector( + `img[usemap="#${mapElement.name}"]` + ) as HTMLImageElement; let { left: mapLeft, top: mapTop } = imgElement.getBoundingClientRect(); let coords = e.getAttribute('coords'); + if (!coords) { + return e.getBoundingClientRect(); + } + let rect = { left: 0, top: 0, right: 0, bottom: 0 }; switch (e.getAttribute('shape')) { case 'rect': @@ -81,7 +95,7 @@ const viewportRect = (e) => { }; }; -const isVisible = (element) => { +const isVisible = (element: Element): boolean => { let rect = element.getBoundingClientRect(); let style = window.getComputedStyle(element); @@ -94,7 +108,8 @@ const isVisible = (element) => { if (window.innerWidth < rect.left && window.innerHeight < rect.top) { return false; } - if (element.nodeName === 'INPUT' && element.type.toLowerCase() === 'hidden') { + if (element instanceof HTMLInputElement && + element.type.toLowerCase() === 'hidden') { return false; } diff --git a/src/shared/utils/keys.js b/src/shared/utils/keys.ts index f024069..e9b0365 100644 --- a/src/shared/utils/keys.js +++ b/src/shared/utils/keys.ts @@ -1,4 +1,12 @@ -const modifiedKeyName = (name) => { +export interface Key { + key: string; + shiftKey: boolean | undefined; + ctrlKey: boolean | undefined; + altKey: boolean | undefined; + metaKey: boolean | undefined; +} + +const modifiedKeyName = (name: string): string => { if (name === ' ') { return 'Space'; } @@ -10,7 +18,7 @@ const modifiedKeyName = (name) => { return name; }; -const fromKeyboardEvent = (e) => { +const fromKeyboardEvent = (e: KeyboardEvent): Key => { let key = modifiedKeyName(e.key); let shift = e.shiftKey; if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) { @@ -28,7 +36,7 @@ const fromKeyboardEvent = (e) => { }; }; -const fromMapKey = (key) => { +const fromMapKey = (key: string): Key => { if (key.startsWith('<') && key.endsWith('>')) { let inner = key.slice(1, -1); let shift = inner.includes('S-'); @@ -55,8 +63,10 @@ const fromMapKey = (key) => { }; }; -const fromMapKeys = (keys) => { - const fromMapKeysRecursive = (remainings, mappedKeys) => { +const fromMapKeys = (keys: string): Key[] => { + const fromMapKeysRecursive = ( + remainings: string, mappedKeys: Key[], + ): Key[] => { if (remainings.length === 0) { return mappedKeys; } @@ -78,7 +88,7 @@ const fromMapKeys = (keys) => { return fromMapKeysRecursive(keys, []); }; -const equals = (e1, e2) => { +const equals = (e1: Key, e2: Key): boolean => { return e1.key === e2.key && e1.ctrlKey === e2.ctrlKey && e1.metaKey === e2.metaKey && diff --git a/src/shared/utils/re.js b/src/shared/utils/re.ts index 7db9091..34f4fa6 100644 --- a/src/shared/utils/re.js +++ b/src/shared/utils/re.ts @@ -1,4 +1,4 @@ -const fromWildcard = (pattern) => { +const fromWildcard = (pattern: string): RegExp => { let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; return new RegExp(regexStr); }; |