mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-07 23:16:52 +08:00
lint(eslint): migrate to eslint and prettier (#25046)
This commit is contained in:
@ -9,7 +9,10 @@ interface IsPlatformSignature {
|
||||
|
||||
export const getPlatforms = (win?: any) => setupPlatforms(win);
|
||||
|
||||
export const isPlatform: IsPlatformSignature = (winOrPlatform: Window | Platforms | undefined, platform?: Platforms) => {
|
||||
export const isPlatform: IsPlatformSignature = (
|
||||
winOrPlatform: Window | Platforms | undefined,
|
||||
platform?: Platforms
|
||||
) => {
|
||||
if (typeof winOrPlatform === 'string') {
|
||||
platform = winOrPlatform;
|
||||
winOrPlatform = undefined;
|
||||
@ -18,28 +21,29 @@ export const isPlatform: IsPlatformSignature = (winOrPlatform: Window | Platform
|
||||
};
|
||||
|
||||
export const setupPlatforms = (win: any = window) => {
|
||||
if (typeof win === 'undefined') { return []; }
|
||||
if (typeof win === 'undefined') {
|
||||
return [];
|
||||
}
|
||||
|
||||
win.Ionic = win.Ionic || {};
|
||||
|
||||
let platforms: Platforms[] | undefined | null = win.Ionic.platforms;
|
||||
if (platforms == null) {
|
||||
platforms = win.Ionic.platforms = detectPlatforms(win);
|
||||
platforms.forEach(p => win.document.documentElement.classList.add(`plt-${p}`));
|
||||
platforms.forEach((p) => win.document.documentElement.classList.add(`plt-${p}`));
|
||||
}
|
||||
return platforms;
|
||||
};
|
||||
|
||||
const detectPlatforms = (win: Window) => {
|
||||
const customPlatformMethods = config.get('platform');
|
||||
return (Object.keys(PLATFORMS_MAP) as Platforms[]).filter(p => {
|
||||
const customMethod = customPlatformMethods && customPlatformMethods[p];
|
||||
return (Object.keys(PLATFORMS_MAP) as Platforms[]).filter((p) => {
|
||||
const customMethod = customPlatformMethods?.[p];
|
||||
return typeof customMethod === 'function' ? customMethod(win) : PLATFORMS_MAP[p](win);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const isMobileWeb = (win: Window): boolean =>
|
||||
isMobile(win) && !isHybrid(win);
|
||||
const isMobileWeb = (win: Window): boolean => isMobile(win) && !isHybrid(win);
|
||||
|
||||
const isIpad = (win: Window) => {
|
||||
// iOS 12 and below
|
||||
@ -55,14 +59,11 @@ const isIpad = (win: Window) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
const isIphone = (win: Window) =>
|
||||
testUserAgent(win, /iPhone/i);
|
||||
const isIphone = (win: Window) => testUserAgent(win, /iPhone/i);
|
||||
|
||||
const isIOS = (win: Window) =>
|
||||
testUserAgent(win, /iPhone|iPod/i) || isIpad(win);
|
||||
const isIOS = (win: Window) => testUserAgent(win, /iPhone|iPod/i) || isIpad(win);
|
||||
|
||||
const isAndroid = (win: Window) =>
|
||||
testUserAgent(win, /android|sink/i);
|
||||
const isAndroid = (win: Window) => testUserAgent(win, /android|sink/i);
|
||||
|
||||
const isAndroidTablet = (win: Window) => {
|
||||
return isAndroid(win) && !testUserAgent(win, /mobile/i);
|
||||
@ -74,8 +75,7 @@ const isPhablet = (win: Window) => {
|
||||
const smallest = Math.min(width, height);
|
||||
const largest = Math.max(width, height);
|
||||
|
||||
return (smallest > 390 && smallest < 520) &&
|
||||
(largest > 620 && largest < 800);
|
||||
return smallest > 390 && smallest < 520 && largest > 620 && largest < 800;
|
||||
};
|
||||
|
||||
const isTablet = (win: Window) => {
|
||||
@ -84,60 +84,46 @@ const isTablet = (win: Window) => {
|
||||
const smallest = Math.min(width, height);
|
||||
const largest = Math.max(width, height);
|
||||
|
||||
return (
|
||||
isIpad(win) ||
|
||||
isAndroidTablet(win) ||
|
||||
(
|
||||
(smallest > 460 && smallest < 820) &&
|
||||
(largest > 780 && largest < 1400)
|
||||
)
|
||||
);
|
||||
return isIpad(win) || isAndroidTablet(win) || (smallest > 460 && smallest < 820 && largest > 780 && largest < 1400);
|
||||
};
|
||||
|
||||
const isMobile = (win: Window) =>
|
||||
matchMedia(win, '(any-pointer:coarse)');
|
||||
const isMobile = (win: Window) => matchMedia(win, '(any-pointer:coarse)');
|
||||
|
||||
const isDesktop = (win: Window) =>
|
||||
!isMobile(win);
|
||||
const isDesktop = (win: Window) => !isMobile(win);
|
||||
|
||||
const isHybrid = (win: Window) =>
|
||||
isCordova(win) || isCapacitorNative(win);
|
||||
const isHybrid = (win: Window) => isCordova(win) || isCapacitorNative(win);
|
||||
|
||||
const isCordova = (win: any): boolean =>
|
||||
!!(win['cordova'] || win['phonegap'] || win['PhoneGap']);
|
||||
const isCordova = (win: any): boolean => !!(win['cordova'] || win['phonegap'] || win['PhoneGap']);
|
||||
|
||||
const isCapacitorNative = (win: any): boolean => {
|
||||
const capacitor = win['Capacitor'];
|
||||
return !!(capacitor && capacitor.isNative);
|
||||
return !!capacitor?.isNative;
|
||||
};
|
||||
|
||||
const isElectron = (win: Window): boolean =>
|
||||
testUserAgent(win, /electron/i);
|
||||
const isElectron = (win: Window): boolean => testUserAgent(win, /electron/i);
|
||||
|
||||
const isPWA = (win: Window): boolean =>
|
||||
!!((win.matchMedia && win.matchMedia('(display-mode: standalone)').matches) || (win.navigator as any).standalone);
|
||||
!!(win.matchMedia?.('(display-mode: standalone)').matches || (win.navigator as any).standalone);
|
||||
|
||||
export const testUserAgent = (win: Window, expr: RegExp) =>
|
||||
expr.test(win.navigator.userAgent);
|
||||
export const testUserAgent = (win: Window, expr: RegExp) => expr.test(win.navigator.userAgent);
|
||||
|
||||
const matchMedia = (win: Window, query: string): boolean =>
|
||||
win.matchMedia && win.matchMedia(query).matches;
|
||||
const matchMedia = (win: Window, query: string): boolean => win.matchMedia?.(query).matches;
|
||||
|
||||
export type PlatformConfig = Partial<typeof PLATFORMS_MAP>;
|
||||
|
||||
const PLATFORMS_MAP = {
|
||||
'ipad': isIpad,
|
||||
'iphone': isIphone,
|
||||
'ios': isIOS,
|
||||
'android': isAndroid,
|
||||
'phablet': isPhablet,
|
||||
'tablet': isTablet,
|
||||
'cordova': isCordova,
|
||||
'capacitor': isCapacitorNative,
|
||||
'electron': isElectron,
|
||||
'pwa': isPWA,
|
||||
'mobile': isMobile,
|
||||
'mobileweb': isMobileWeb,
|
||||
'desktop': isDesktop,
|
||||
'hybrid': isHybrid
|
||||
ipad: isIpad,
|
||||
iphone: isIphone,
|
||||
ios: isIOS,
|
||||
android: isAndroid,
|
||||
phablet: isPhablet,
|
||||
tablet: isTablet,
|
||||
cordova: isCordova,
|
||||
capacitor: isCapacitorNative,
|
||||
electron: isElectron,
|
||||
pwa: isPWA,
|
||||
mobile: isMobile,
|
||||
mobileweb: isMobileWeb,
|
||||
desktop: isDesktop,
|
||||
hybrid: isHybrid,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user