octicon-rss(16/)
You've already forked ionic-framework
mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-10 22:44:13 +08:00
perf(all): minify better by using arrow functions (#18730)
This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
committed by
Brandy Carney
gitea-unlock(16/)
parent
8beeff2c52
commit
03c1d19e07
octicon-diff(16/tw-mr-1) 99 changed files with 653 additions and 679 deletions
@@ -1,11 +1,11 @@
|
||||
const cloneMap = new WeakMap<HTMLElement, HTMLElement>();
|
||||
|
||||
export function relocateInput(
|
||||
export const relocateInput = (
|
||||
componentEl: HTMLElement,
|
||||
inputEl: HTMLInputElement | HTMLTextAreaElement,
|
||||
shouldRelocate: boolean,
|
||||
inputRelativeY = 0
|
||||
) {
|
||||
) => {
|
||||
if (cloneMap.has(componentEl) === shouldRelocate) {
|
||||
return;
|
||||
}
|
||||
@@ -15,13 +15,13 @@ export function relocateInput(
|
||||
} else {
|
||||
removeClone(componentEl, inputEl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export function isFocused(input: HTMLInputElement | HTMLTextAreaElement): boolean {
|
||||
export const isFocused = (input: HTMLInputElement | HTMLTextAreaElement): boolean => {
|
||||
return input === (input as any).getRootNode().activeElement;
|
||||
}
|
||||
};
|
||||
|
||||
function addClone(componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLTextAreaElement, inputRelativeY: number) {
|
||||
const addClone = (componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLTextAreaElement, inputRelativeY: number) => {
|
||||
// this allows for the actual input to receive the focus from
|
||||
// the user's touch event, but before it receives focus, it
|
||||
// moves the actual input to a location that will not screw
|
||||
@@ -44,9 +44,9 @@ function addClone(componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLText
|
||||
const tx = doc.dir === 'rtl' ? 9999 : -9999;
|
||||
componentEl.style.pointerEvents = 'none';
|
||||
inputEl.style.transform = `translate3d(${tx}px,${inputRelativeY}px,0) scale(0)`;
|
||||
}
|
||||
};
|
||||
|
||||
function removeClone(componentEl: HTMLElement, inputEl: HTMLElement) {
|
||||
const removeClone = (componentEl: HTMLElement, inputEl: HTMLElement) => {
|
||||
const clone = cloneMap.get(componentEl);
|
||||
if (clone) {
|
||||
cloneMap.delete(componentEl);
|
||||
@@ -54,4 +54,4 @@ function removeClone(componentEl: HTMLElement, inputEl: HTMLElement) {
|
||||
}
|
||||
componentEl.style.pointerEvents = '';
|
||||
inputEl.style.transform = '';
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { isFocused, relocateInput } from './common';
|
||||
|
||||
export function enableHideCaretOnScroll(componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLTextAreaElement | undefined, scrollEl: HTMLIonContentElement | undefined) {
|
||||
export const enableHideCaretOnScroll = (componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLTextAreaElement | undefined, scrollEl: HTMLIonContentElement | undefined) => {
|
||||
if (!scrollEl || !inputEl) {
|
||||
return () => { return; };
|
||||
}
|
||||
@@ -24,4 +24,4 @@ export function enableHideCaretOnScroll(componentEl: HTMLElement, inputEl: HTMLI
|
||||
scrollEl.removeEventListener('ionScrollEnd', showCaret);
|
||||
inputEl.addEventListener('ionBlur', onBlur);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
|
||||
const SKIP_SELECTOR = 'input, textarea, [no-blur]';
|
||||
|
||||
export function enableInputBlurring() {
|
||||
const doc = document;
|
||||
export const enableInputBlurring = () => {
|
||||
let focused = true;
|
||||
let didScroll = false;
|
||||
|
||||
function onScroll() {
|
||||
const doc = document;
|
||||
const onScroll = () => {
|
||||
didScroll = true;
|
||||
}
|
||||
};
|
||||
|
||||
function onFocusin() {
|
||||
const onFocusin = () => {
|
||||
focused = true;
|
||||
}
|
||||
};
|
||||
|
||||
function onTouchend(ev: any) {
|
||||
const onTouchend = (ev: any) => {
|
||||
// if app did scroll return early
|
||||
if (didScroll) {
|
||||
didScroll = false;
|
||||
@@ -46,7 +46,7 @@ export function enableInputBlurring() {
|
||||
active.blur();
|
||||
}
|
||||
}, 50);
|
||||
}
|
||||
};
|
||||
|
||||
doc.addEventListener('ionScrollStart', onScroll);
|
||||
doc.addEventListener('focusin', onFocusin, true);
|
||||
@@ -57,4 +57,4 @@ export function enableInputBlurring() {
|
||||
doc.removeEventListener('focusin', onFocusin, true);
|
||||
doc.removeEventListener('touchend', onTouchend, false);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,12 +3,12 @@ import { pointerCoord } from '../../helpers';
|
||||
import { isFocused, relocateInput } from './common';
|
||||
import { getScrollData } from './scroll-data';
|
||||
|
||||
export function enableScrollAssist(
|
||||
export const enableScrollAssist = (
|
||||
componentEl: HTMLElement,
|
||||
inputEl: HTMLInputElement | HTMLTextAreaElement,
|
||||
contentEl: HTMLIonContentElement,
|
||||
keyboardHeight: number
|
||||
) {
|
||||
) => {
|
||||
let coord: any;
|
||||
const touchStart = (ev: Event) => {
|
||||
coord = pointerCoord(ev);
|
||||
@@ -39,14 +39,14 @@ export function enableScrollAssist(
|
||||
componentEl.removeEventListener('touchstart', touchStart, true);
|
||||
componentEl.removeEventListener('touchend', touchEnd, true);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
function jsSetFocus(
|
||||
const jsSetFocus = (
|
||||
componentEl: HTMLElement,
|
||||
inputEl: HTMLInputElement | HTMLTextAreaElement,
|
||||
contentEl: HTMLIonContentElement,
|
||||
keyboardHeight: number
|
||||
) {
|
||||
) => {
|
||||
const scrollData = getScrollData(componentEl, contentEl, keyboardHeight);
|
||||
if (Math.abs(scrollData.scrollAmount) < 4) {
|
||||
// the text input is in a safe position that doesn't
|
||||
@@ -70,9 +70,9 @@ function jsSetFocus(
|
||||
// ensure this is the focused input
|
||||
inputEl.focus();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function hasPointerMoved(threshold: number, startCoord: PointerCoordinates | undefined, endCoord: PointerCoordinates | undefined) {
|
||||
const hasPointerMoved = (threshold: number, startCoord: PointerCoordinates | undefined, endCoord: PointerCoordinates | undefined) => {
|
||||
if (startCoord && endCoord) {
|
||||
const deltaX = (startCoord.x - endCoord.x);
|
||||
const deltaY = (startCoord.y - endCoord.y);
|
||||
@@ -80,7 +80,7 @@ function hasPointerMoved(threshold: number, startCoord: PointerCoordinates | und
|
||||
return distance > (threshold * threshold);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export interface PointerCoordinates {
|
||||
x: number;
|
||||
|
||||
@@ -8,7 +8,7 @@ export interface ScrollData {
|
||||
inputSafeY: number;
|
||||
}
|
||||
|
||||
export function getScrollData(componentEl: HTMLElement, contentEl: HTMLElement, keyboardHeight: number): ScrollData {
|
||||
export const getScrollData = (componentEl: HTMLElement, contentEl: HTMLElement, keyboardHeight: number): ScrollData => {
|
||||
const itemEl = componentEl.closest('ion-item,[ion-item]') as HTMLElement || componentEl;
|
||||
return calcScrollData(
|
||||
itemEl.getBoundingClientRect(),
|
||||
@@ -16,14 +16,14 @@ export function getScrollData(componentEl: HTMLElement, contentEl: HTMLElement,
|
||||
keyboardHeight,
|
||||
(componentEl as any).ownerDocument.defaultView.innerHeight
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
function calcScrollData(
|
||||
const calcScrollData = (
|
||||
inputRect: ClientRect,
|
||||
contentRect: ClientRect,
|
||||
keyboardHeight: number,
|
||||
platformHeight: number
|
||||
): ScrollData {
|
||||
): ScrollData => {
|
||||
// compute input's Y values relative to the body
|
||||
const inputTop = inputRect.top;
|
||||
const inputBottom = inputRect.bottom;
|
||||
@@ -44,8 +44,8 @@ function calcScrollData(
|
||||
const desiredScrollAmount = Math.round((distanceToBottom < 0)
|
||||
? -distanceToBottom
|
||||
: (distanceToTop > 0)
|
||||
? -distanceToTop
|
||||
: 0);
|
||||
? -distanceToTop
|
||||
: 0);
|
||||
|
||||
// our calculations make some assumptions that aren't always true, like the keyboard being closed when an input
|
||||
// gets focus, so make sure we don't scroll the input above the visible area
|
||||
@@ -61,4 +61,4 @@ function calcScrollData(
|
||||
scrollPadding: keyboardHeight,
|
||||
inputSafeY: -(inputTop - safeAreaTop) + 4
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
const PADDING_TIMER_KEY = '$ionPaddingTimer';
|
||||
|
||||
export function enableScrollPadding(keyboardHeight: number) {
|
||||
export const enableScrollPadding = (keyboardHeight: number) => {
|
||||
const doc = document;
|
||||
|
||||
function onFocusin(ev: any) {
|
||||
const onFocusin = (ev: any) => {
|
||||
setScrollPadding(ev.target, keyboardHeight);
|
||||
}
|
||||
function onFocusout(ev: any) {
|
||||
};
|
||||
const onFocusout = (ev: any) => {
|
||||
setScrollPadding(ev.target, 0);
|
||||
}
|
||||
};
|
||||
|
||||
doc.addEventListener('focusin', onFocusin);
|
||||
doc.addEventListener('focusout', onFocusout);
|
||||
@@ -17,9 +17,9 @@ export function enableScrollPadding(keyboardHeight: number) {
|
||||
doc.removeEventListener('focusin', onFocusin);
|
||||
doc.removeEventListener('focusout', onFocusout);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
function setScrollPadding(input: HTMLElement, keyboardHeight: number) {
|
||||
const setScrollPadding = (input: HTMLElement, keyboardHeight: number) => {
|
||||
if (input.tagName !== 'INPUT') {
|
||||
return;
|
||||
}
|
||||
@@ -50,4 +50,4 @@ function setScrollPadding(input: HTMLElement, keyboardHeight: number) {
|
||||
el.style.setProperty('--keyboard-offset', '0px');
|
||||
}, 120);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,20 +11,19 @@ const SCROLL_ASSIST = true;
|
||||
const SCROLL_PADDING = true;
|
||||
const HIDE_CARET = true;
|
||||
|
||||
export function startInputShims(
|
||||
config: Config,
|
||||
) {
|
||||
export const startInputShims = (config: Config) => {
|
||||
const doc = document;
|
||||
const keyboardHeight = config.getNumber('keyboardHeight', 290);
|
||||
const scrollAssist = config.getBoolean('scrollAssist', true);
|
||||
const hideCaret = config.getBoolean('hideCaretOnScroll', true);
|
||||
const inputBlurring = config.getBoolean('inputBlurring', true);
|
||||
const scrollPadding = config.getBoolean('scrollPadding', true);
|
||||
const inputs = Array.from(doc.querySelectorAll('ion-input, ion-textarea')) as HTMLElement[];
|
||||
|
||||
const hideCaretMap = new WeakMap<HTMLElement, () => void>();
|
||||
const scrollAssistMap = new WeakMap<HTMLElement, () => void>();
|
||||
|
||||
function registerInput(componentEl: HTMLElement) {
|
||||
const registerInput = (componentEl: HTMLElement) => {
|
||||
const inputEl = (componentEl.shadowRoot || componentEl).querySelector('input') || (componentEl.shadowRoot || componentEl).querySelector('textarea');
|
||||
const scrollEl = componentEl.closest('ion-content');
|
||||
|
||||
@@ -41,9 +40,9 @@ export function startInputShims(
|
||||
const rmFn = enableScrollAssist(componentEl, inputEl, scrollEl, keyboardHeight);
|
||||
scrollAssistMap.set(componentEl, rmFn);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function unregisterInput(componentEl: HTMLElement) {
|
||||
const unregisterInput = (componentEl: HTMLElement) => {
|
||||
if (HIDE_CARET && hideCaret) {
|
||||
const fn = hideCaretMap.get(componentEl);
|
||||
if (fn) {
|
||||
@@ -59,7 +58,7 @@ export function startInputShims(
|
||||
}
|
||||
scrollAssistMap.delete(componentEl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (inputBlurring && INPUT_BLURRING) {
|
||||
enableInputBlurring();
|
||||
@@ -72,7 +71,6 @@ export function startInputShims(
|
||||
// Input might be already loaded in the DOM before ion-device-hacks did.
|
||||
// At this point we need to look for all of the inputs not registered yet
|
||||
// and register them.
|
||||
const inputs = Array.from(doc.querySelectorAll('ion-input, ion-textarea')) as HTMLElement[];
|
||||
for (const input of inputs) {
|
||||
registerInput(input);
|
||||
}
|
||||
@@ -84,4 +82,4 @@ export function startInputShims(
|
||||
doc.body.addEventListener('ionInputDidUnload', event => {
|
||||
unregisterInput(event.target as any);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user