/
ExpoScreenOrientation.web.ts
164 lines (152 loc) · 5.52 KB
/
ExpoScreenOrientation.web.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { SyntheticPlatformEmitter } from '@unimodules/core';
import { canUseViewport, canUseEventListeners } from 'fbjs/lib/ExecutionEnvironment';
import {
OrientationInfo,
Orientation,
OrientationLock,
WebOrientationLock,
WebOrientation,
} from './ScreenOrientation.types';
import { getOrientationLockAsync, getOrientationAsync } from './ScreenOrientation';
const OrientationLockAPIToWeb: {
[lock: string]: WebOrientationLock;
} = {
DEFAULT: WebOrientationLock.NATURAL,
ALL: WebOrientationLock.ANY,
PORTRAIT: WebOrientationLock.PORTRAIT,
PORTRAIT_UP: WebOrientationLock.PORTRAIT_PRIMARY,
PORTRAIT_DOWN: WebOrientationLock.PORTRAIT_SECONDARY,
LANDSCAPE: WebOrientationLock.LANDSCAPE,
LANDSCAPE_LEFT: WebOrientationLock.LANDSCAPE_PRIMARY,
LANDSCAPE_RIGHT: WebOrientationLock.LANDSCAPE_SECONDARY,
};
const OrientationWebToAPI: {
[orientationWeb: string]: Orientation;
} = {
[WebOrientation.PORTRAIT_PRIMARY]: Orientation.PORTRAIT_UP,
[WebOrientation.PORTRAIT_SECONDARY]: Orientation.PORTRAIT_DOWN,
[WebOrientation.LANDSCAPE_PRIMARY]: Orientation.LANDSCAPE_LEFT,
[WebOrientation.LANDSCAPE_SECONDARY]: Orientation.LANDSCAPE_RIGHT,
};
declare const window: Window;
const { screen } = canUseViewport && window;
const orientation: ScreenOrientation | null =
canUseViewport && (screen.orientation || (screen as any).msOrientation || null);
async function emitOrientationEvent() {
const [orientationLock, orientationInfo] = await Promise.all([
getOrientationLockAsync(),
getOrientationAsync(),
]);
SyntheticPlatformEmitter.emit('expoDidUpdateDimensions', {
orientationLock,
orientationInfo,
});
}
if (canUseEventListeners) {
if (orientation && orientation.addEventListener) {
orientation.addEventListener('change', emitOrientationEvent);
} else {
window.addEventListener('orientationchange', emitOrientationEvent);
}
}
function _convertToLegacyOrientationLock(orientationLock: WebOrientationLock): string | string[] {
switch (orientationLock) {
case WebOrientationLock.UNKNOWN:
throw new Error(
`expo-screen-orientation: WebOrientationLock.UNKNOWN is not a valid lock to be converted.`
);
case WebOrientationLock.ANY:
return ['portrait', 'landscape'];
case WebOrientationLock.NATURAL:
return 'default';
default:
return orientationLock;
}
}
async function _lockAsync(webOrientationLock: WebOrientationLock): Promise<void> {
if (webOrientationLock === WebOrientationLock.UNKNOWN) {
throw new Error(
`expo-screen-orientation: WebOrientationLock.UNKNOWN is not a valid lock that can be applied to the device.`
);
}
if (screen.orientation && screen.orientation.lock) {
await screen.orientation.lock(webOrientationLock);
} else if (
screen['lockOrientation'] ||
screen['mozLockOrientation'] ||
screen['msLockOrientation']
) {
const legacyLock = _convertToLegacyOrientationLock(webOrientationLock);
const lockOrientation =
screen['lockOrientation'] || screen['mozLockOrientation'] || screen['msLockOrientation'];
// correct `this` context must be passed in otherwise method call is disallowed by browser
const isSuccess = lockOrientation.call(screen, legacyLock);
if (!isSuccess) {
throw new Error(
`Applying orientation lock: ${JSON.stringify(webOrientationLock)} to device was denied`
);
}
} else {
throw new Error(
`expo-screen-orientation: The browser doesn't support locking screen orientation.`
);
}
}
let _lastWebOrientationLock: WebOrientationLock = WebOrientationLock.UNKNOWN;
export default {
get name(): string {
return 'ExpoScreenOrientation';
},
async supportsOrientationLockAsync(orientationLock: OrientationLock): Promise<boolean> {
return orientationLock in OrientationLockAPIToWeb;
},
async getPlatformOrientationLockAsync(): Promise<WebOrientationLock> {
return _lastWebOrientationLock;
},
async getOrientationAsync(): Promise<OrientationInfo> {
const webOrientation =
screen['msOrientation'] || (screen.orientation || screen['mozOrientation'] || {}).type;
if (!webOrientation) {
return {
orientation: Orientation.UNKNOWN,
};
}
return {
orientation: OrientationWebToAPI[webOrientation],
};
},
async lockAsync(orientationLock: OrientationLock): Promise<void> {
const webOrientationLock = OrientationLockAPIToWeb[orientationLock];
if (!webOrientationLock) {
throw new TypeError(`Invalid Orientation Lock: ${orientationLock}`);
}
await _lockAsync(webOrientationLock);
},
async lockPlatformAsync(webOrientationLock: WebOrientationLock): Promise<void> {
await _lockAsync(webOrientationLock);
_lastWebOrientationLock = webOrientationLock;
},
async unlockAsync(): Promise<void> {
if (screen.orientation && screen.orientation.unlock) {
screen.orientation.unlock();
} else if (
screen['unlockOrientation'] ||
screen['mozUnlockOrientation'] ||
screen['msUnlockOrientation']
) {
const unlockOrientation =
screen['unlockOrientation'] ||
screen['mozUnlockOrientation'] ||
screen['msUnlockOrientation'];
// correct `this` context must be passed in otherwise method call is disallowed by browser
const isSuccess = unlockOrientation.call(screen);
if (!isSuccess) {
throw new Error(`Unlocking screen orientation on device was denied`);
}
} else {
throw new Error(
`expo-screen-orientation: The browser doesn't support unlocking screen orientation.`
);
}
},
};