1 | import { Observable } from '../../data/observable';
|
2 | import { Screen } from '../../platform';
|
3 | import { isNumber, CORE_ANIMATION_DEFAULTS } from '../../utils';
|
4 | import { querySelectorAll } from '../core/view-base';
|
5 |
|
6 | export var SharedTransitionAnimationType;
|
7 | (function (SharedTransitionAnimationType) {
|
8 | SharedTransitionAnimationType[SharedTransitionAnimationType["present"] = 0] = "present";
|
9 | SharedTransitionAnimationType[SharedTransitionAnimationType["dismiss"] = 1] = "dismiss";
|
10 | })(SharedTransitionAnimationType || (SharedTransitionAnimationType = {}));
|
11 | class SharedTransitionObservable extends Observable {
|
12 |
|
13 | on(eventNames, callback, thisArg) {
|
14 | super.on(eventNames, callback, thisArg);
|
15 | }
|
16 | }
|
17 | let sharedTransitionEvents;
|
18 | let currentStack;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export class SharedTransition {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | static custom(transition, options) {
|
32 | SharedTransition.updateState(transition.id, {
|
33 | ...(options || {}),
|
34 | instance: transition,
|
35 | activeType: SharedTransitionAnimationType.present,
|
36 | });
|
37 | const pageEnd = options?.pageEnd;
|
38 | if (isNumber(pageEnd?.duration)) {
|
39 |
|
40 |
|
41 | transition.setDuration(__APPLE__ ? pageEnd?.duration / 1000 : pageEnd?.duration);
|
42 | }
|
43 | return { instance: transition };
|
44 | }
|
45 | |
46 |
|
47 |
|
48 |
|
49 | static events() {
|
50 | if (!sharedTransitionEvents) {
|
51 | sharedTransitionEvents = new SharedTransitionObservable();
|
52 | }
|
53 | return sharedTransitionEvents;
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | static notifyEvent(eventName, data) {
|
63 | switch (eventName) {
|
64 | case SharedTransition.startedEvent:
|
65 | case SharedTransition.interactiveUpdateEvent:
|
66 | SharedTransition.inProgress = true;
|
67 | break;
|
68 | default:
|
69 | SharedTransition.inProgress = false;
|
70 | break;
|
71 | }
|
72 | SharedTransition.events().notify({
|
73 | eventName,
|
74 | data,
|
75 | });
|
76 | }
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | static updateState(id, state) {
|
83 | if (!currentStack) {
|
84 | currentStack = [];
|
85 | }
|
86 | const existingTransition = SharedTransition.getState(id);
|
87 | if (existingTransition) {
|
88 |
|
89 | for (const key in state) {
|
90 | existingTransition[key] = state[key];
|
91 |
|
92 | }
|
93 | }
|
94 | else {
|
95 | currentStack.push(state);
|
96 | }
|
97 | }
|
98 | |
99 |
|
100 |
|
101 |
|
102 | static getState(id) {
|
103 | return currentStack?.find((t) => t.instance?.id === id);
|
104 | }
|
105 | |
106 |
|
107 |
|
108 |
|
109 | static finishState(id) {
|
110 | const index = currentStack?.findIndex((t) => t.instance?.id === id);
|
111 | if (index > -1) {
|
112 | currentStack.splice(index, 1);
|
113 | }
|
114 | }
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | static getSharedElements(fromPage, toPage) {
|
122 |
|
123 | const presentedSharedElements = querySelectorAll(toPage, 'sharedTransitionTag').filter((v) => !v.sharedTransitionIgnore && typeof v.sharedTransitionTag === 'string');
|
124 |
|
125 |
|
126 | const presentingSharedElements = querySelectorAll(fromPage, 'sharedTransitionTag').filter((v) => !v.sharedTransitionIgnore && typeof v.sharedTransitionTag === 'string');
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | const presentedTags = presentedSharedElements.map((v) => v.sharedTransitionTag);
|
133 | return {
|
134 | sharedElements: presentingSharedElements.filter((v) => presentedTags.includes(v.sharedTransitionTag)),
|
135 | presented: presentedSharedElements,
|
136 | presenting: presentingSharedElements,
|
137 | };
|
138 | }
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 | SharedTransition.startedEvent = 'SharedTransitionStartedEvent';
|
144 |
|
145 |
|
146 |
|
147 | SharedTransition.finishedEvent = 'SharedTransitionFinishedEvent';
|
148 |
|
149 |
|
150 |
|
151 | SharedTransition.interactiveCancelledEvent = 'SharedTransitionInteractiveCancelledEvent';
|
152 |
|
153 |
|
154 |
|
155 | SharedTransition.interactiveUpdateEvent = 'SharedTransitionInteractiveUpdateEvent';
|
156 |
|
157 |
|
158 |
|
159 | SharedTransition.DEBUG = false;
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | export function getRectFromProps(props, defaults) {
|
167 | defaults = {
|
168 | x: 0,
|
169 | y: 0,
|
170 | width: getPlatformWidth(),
|
171 | height: getPlatformHeight(),
|
172 | ...(defaults || {}),
|
173 | };
|
174 | return {
|
175 | x: isNumber(props?.x) ? props?.x : defaults.x,
|
176 | y: isNumber(props?.y) ? props?.y : defaults.y,
|
177 | width: isNumber(props?.width) ? props?.width : defaults.width,
|
178 | height: isNumber(props?.height) ? props?.height : defaults.height,
|
179 | };
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export function getSpringFromProps(props) {
|
187 | return {
|
188 | tension: isNumber(props?.tension) ? props?.tension : CORE_ANIMATION_DEFAULTS.spring.tension,
|
189 | friction: isNumber(props?.friction) ? props?.friction : CORE_ANIMATION_DEFAULTS.spring.friction,
|
190 | mass: isNumber(props?.mass) ? props?.mass : CORE_ANIMATION_DEFAULTS.spring.mass,
|
191 | velocity: isNumber(props?.velocity) ? props?.velocity : CORE_ANIMATION_DEFAULTS.spring.velocity,
|
192 | delay: isNumber(props?.delay) ? props?.delay : 0,
|
193 | };
|
194 | }
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | export function getPageStartDefaultsForType(type) {
|
201 | return {
|
202 | x: type === 'page' ? getPlatformWidth() : 0,
|
203 | y: type === 'page' ? 0 : getPlatformHeight(),
|
204 | width: getPlatformWidth(),
|
205 | height: getPlatformHeight(),
|
206 | };
|
207 | }
|
208 | function getPlatformWidth() {
|
209 | return __ANDROID__ ? Screen.mainScreen.widthPixels : Screen.mainScreen.widthDIPs;
|
210 | }
|
211 | function getPlatformHeight() {
|
212 | return __ANDROID__ ? Screen.mainScreen.heightPixels : Screen.mainScreen.heightDIPs;
|
213 | }
|
214 |
|
\ | No newline at end of file |