mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat(platform): add ability to override platform detection methods (#23915)
resolves #19737
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { AnimationBuilder, Mode, SpinnerTypes, TabButtonLayout } from '../interface';
|
||||
|
||||
import { PlatformConfig } from './platform';
|
||||
|
||||
export interface IonicConfig {
|
||||
/**
|
||||
* When it's set to `false`, disables all animation and transition across the app.
|
||||
@@ -175,6 +177,11 @@ export interface IonicConfig {
|
||||
*/
|
||||
sanitizerEnabled?: boolean;
|
||||
|
||||
/**
|
||||
* Overrides the default platform detection methods.
|
||||
*/
|
||||
platform?: PlatformConfig;
|
||||
|
||||
// PRIVATE configs
|
||||
keyboardHeight?: number;
|
||||
inputShims?: boolean;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { config } from '../global/config';
|
||||
|
||||
export type Platforms = keyof typeof PLATFORMS_MAP;
|
||||
|
||||
@@ -29,8 +30,13 @@ export const setupPlatforms = (win: any = window) => {
|
||||
return platforms;
|
||||
};
|
||||
|
||||
const detectPlatforms = (win: Window) =>
|
||||
(Object.keys(PLATFORMS_MAP) as Platforms[]).filter(p => PLATFORMS_MAP[p](win));
|
||||
const detectPlatforms = (win: Window) => {
|
||||
const customPlatformMethods = config.get('platform');
|
||||
return (Object.keys(PLATFORMS_MAP) as Platforms[]).filter(p => {
|
||||
const customMethod = customPlatformMethods && customPlatformMethods[p];
|
||||
return typeof customMethod === 'function' ? customMethod(win) : PLATFORMS_MAP[p](win);
|
||||
});
|
||||
}
|
||||
|
||||
const isMobileWeb = (win: Window): boolean =>
|
||||
isMobile(win) && !isHybrid(win);
|
||||
@@ -117,6 +123,8 @@ export const testUserAgent = (win: Window, expr: RegExp) =>
|
||||
const matchMedia = (win: Window, query: string): boolean =>
|
||||
win.matchMedia && win.matchMedia(query).matches;
|
||||
|
||||
export type PlatformConfig = Partial<typeof PLATFORMS_MAP>;
|
||||
|
||||
const PLATFORMS_MAP = {
|
||||
'ipad': isIpad,
|
||||
'iphone': isIphone,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import initialize from '../../global/ionic-global';
|
||||
|
||||
import { testUserAgent, getPlatforms, isPlatform } from '../platform';
|
||||
import { PlatformConfiguration, configureBrowser } from './platform.utils';
|
||||
|
||||
@@ -122,7 +124,7 @@ describe('Platform Tests', () => {
|
||||
expect(isPlatform(win, 'pwa')).toEqual(true);
|
||||
expect(isPlatform(win, 'cordova')).toEqual(false);
|
||||
});
|
||||
|
||||
|
||||
it('should return true for "ios", "ipad", and "tablet" and false for "iphone" and "android"', () => {
|
||||
const win = configureBrowser(PlatformConfiguration.iPadOS);
|
||||
expect(isPlatform(win, 'ios')).toEqual(true);
|
||||
@@ -132,4 +134,21 @@ describe('Platform Tests', () => {
|
||||
expect(isPlatform(win, 'android')).toEqual(false);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
describe('Custom Platform Config', () => {
|
||||
it('should use custom platform detection methods', () => {
|
||||
const win = configureBrowser(PlatformConfiguration.DesktopSafari);
|
||||
|
||||
initialize({
|
||||
'platform': {
|
||||
'desktop': (win) => false,
|
||||
'cordova': (win) => true
|
||||
}
|
||||
});
|
||||
|
||||
expect(isPlatform(win, 'desktop')).toEqual(false);
|
||||
expect(isPlatform(win, 'cordova')).toEqual(true);
|
||||
expect(getPlatforms(win).includes('cordova')).toEqual(true);
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user