1 | 'use strict';
|
2 |
|
3 | const helpers = require('./helpers-d381ec4d.js');
|
4 |
|
5 | const cloneMap = new WeakMap();
|
6 | const relocateInput = (componentEl, inputEl, shouldRelocate, inputRelativeY = 0) => {
|
7 | if (cloneMap.has(componentEl) === shouldRelocate) {
|
8 | return;
|
9 | }
|
10 | if (shouldRelocate) {
|
11 | addClone(componentEl, inputEl, inputRelativeY);
|
12 | }
|
13 | else {
|
14 | removeClone(componentEl, inputEl);
|
15 | }
|
16 | };
|
17 | const isFocused = (input) => {
|
18 | return input === input.getRootNode().activeElement;
|
19 | };
|
20 | const addClone = (componentEl, inputEl, inputRelativeY) => {
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const parentEl = inputEl.parentNode;
|
31 |
|
32 | const clonedEl = inputEl.cloneNode(false);
|
33 | clonedEl.classList.add('cloned-input');
|
34 | clonedEl.tabIndex = -1;
|
35 | parentEl.appendChild(clonedEl);
|
36 | cloneMap.set(componentEl, clonedEl);
|
37 | const doc = componentEl.ownerDocument;
|
38 | const tx = doc.dir === 'rtl' ? 9999 : -9999;
|
39 | componentEl.style.pointerEvents = 'none';
|
40 | inputEl.style.transform = `translate3d(${tx}px,${inputRelativeY}px,0) scale(0)`;
|
41 | };
|
42 | const removeClone = (componentEl, inputEl) => {
|
43 | const clone = cloneMap.get(componentEl);
|
44 | if (clone) {
|
45 | cloneMap.delete(componentEl);
|
46 | clone.remove();
|
47 | }
|
48 | componentEl.style.pointerEvents = '';
|
49 | inputEl.style.transform = '';
|
50 | };
|
51 |
|
52 | const enableHideCaretOnScroll = (componentEl, inputEl, scrollEl) => {
|
53 | if (!scrollEl || !inputEl) {
|
54 | return () => { return; };
|
55 | }
|
56 | const scrollHideCaret = (shouldHideCaret) => {
|
57 | if (isFocused(inputEl)) {
|
58 | relocateInput(componentEl, inputEl, shouldHideCaret);
|
59 | }
|
60 | };
|
61 | const onBlur = () => relocateInput(componentEl, inputEl, false);
|
62 | const hideCaret = () => scrollHideCaret(true);
|
63 | const showCaret = () => scrollHideCaret(false);
|
64 | helpers.addEventListener(scrollEl, 'ionScrollStart', hideCaret);
|
65 | helpers.addEventListener(scrollEl, 'ionScrollEnd', showCaret);
|
66 | inputEl.addEventListener('blur', onBlur);
|
67 | return () => {
|
68 | helpers.removeEventListener(scrollEl, 'ionScrollStart', hideCaret);
|
69 | helpers.removeEventListener(scrollEl, 'ionScrollEnd', showCaret);
|
70 | inputEl.addEventListener('ionBlur', onBlur);
|
71 | };
|
72 | };
|
73 |
|
74 | const SKIP_SELECTOR = 'input, textarea, [no-blur], [contenteditable]';
|
75 | const enableInputBlurring = () => {
|
76 | let focused = true;
|
77 | let didScroll = false;
|
78 | const doc = document;
|
79 | const onScroll = () => {
|
80 | didScroll = true;
|
81 | };
|
82 | const onFocusin = () => {
|
83 | focused = true;
|
84 | };
|
85 | const onTouchend = (ev) => {
|
86 |
|
87 | if (didScroll) {
|
88 | didScroll = false;
|
89 | return;
|
90 | }
|
91 | const active = doc.activeElement;
|
92 | if (!active) {
|
93 | return;
|
94 | }
|
95 |
|
96 | if (active.matches(SKIP_SELECTOR)) {
|
97 | return;
|
98 | }
|
99 |
|
100 | const tapped = ev.target;
|
101 | if (tapped === active) {
|
102 | return;
|
103 | }
|
104 | if (tapped.matches(SKIP_SELECTOR) || tapped.closest(SKIP_SELECTOR)) {
|
105 | return;
|
106 | }
|
107 | focused = false;
|
108 |
|
109 | setTimeout(() => {
|
110 | if (!focused) {
|
111 | active.blur();
|
112 | }
|
113 | }, 50);
|
114 | };
|
115 | helpers.addEventListener(doc, 'ionScrollStart', onScroll);
|
116 | doc.addEventListener('focusin', onFocusin, true);
|
117 | doc.addEventListener('touchend', onTouchend, false);
|
118 | return () => {
|
119 | helpers.removeEventListener(doc, 'ionScrollStart', onScroll, true);
|
120 | doc.removeEventListener('focusin', onFocusin, true);
|
121 | doc.removeEventListener('touchend', onTouchend, false);
|
122 | };
|
123 | };
|
124 |
|
125 | const SCROLL_ASSIST_SPEED = 0.3;
|
126 | const getScrollData = (componentEl, contentEl, keyboardHeight) => {
|
127 | const itemEl = componentEl.closest('ion-item,[ion-item]') || componentEl;
|
128 | return calcScrollData(itemEl.getBoundingClientRect(), contentEl.getBoundingClientRect(), keyboardHeight, componentEl.ownerDocument.defaultView.innerHeight);
|
129 | };
|
130 | const calcScrollData = (inputRect, contentRect, keyboardHeight, platformHeight) => {
|
131 |
|
132 | const inputTop = inputRect.top;
|
133 | const inputBottom = inputRect.bottom;
|
134 |
|
135 | const visibleAreaTop = contentRect.top;
|
136 | const visibleAreaBottom = Math.min(contentRect.bottom, platformHeight - keyboardHeight);
|
137 |
|
138 | const safeAreaTop = visibleAreaTop + 15;
|
139 | const safeAreaBottom = visibleAreaBottom * 0.75;
|
140 |
|
141 | const distanceToBottom = safeAreaBottom - inputBottom;
|
142 | const distanceToTop = safeAreaTop - inputTop;
|
143 |
|
144 | const desiredScrollAmount = Math.round((distanceToBottom < 0)
|
145 | ? -distanceToBottom
|
146 | : (distanceToTop > 0)
|
147 | ? -distanceToTop
|
148 | : 0);
|
149 |
|
150 |
|
151 | const scrollAmount = Math.min(desiredScrollAmount, inputTop - visibleAreaTop);
|
152 | const distance = Math.abs(scrollAmount);
|
153 | const duration = distance / SCROLL_ASSIST_SPEED;
|
154 | const scrollDuration = Math.min(400, Math.max(150, duration));
|
155 | return {
|
156 | scrollAmount,
|
157 | scrollDuration,
|
158 | scrollPadding: keyboardHeight,
|
159 | inputSafeY: -(inputTop - safeAreaTop) + 4
|
160 | };
|
161 | };
|
162 |
|
163 | const enableScrollAssist = (componentEl, inputEl, contentEl, footerEl, keyboardHeight) => {
|
164 | let coord;
|
165 | const touchStart = (ev) => {
|
166 | coord = helpers.pointerCoord(ev);
|
167 | };
|
168 | const touchEnd = (ev) => {
|
169 |
|
170 | if (!coord) {
|
171 | return;
|
172 | }
|
173 |
|
174 | const endCoord = helpers.pointerCoord(ev);
|
175 |
|
176 |
|
177 | if (!hasPointerMoved(6, coord, endCoord) && !isFocused(inputEl)) {
|
178 | ev.stopPropagation();
|
179 |
|
180 | jsSetFocus(componentEl, inputEl, contentEl, footerEl, keyboardHeight);
|
181 | }
|
182 | };
|
183 | componentEl.addEventListener('touchstart', touchStart, true);
|
184 | componentEl.addEventListener('touchend', touchEnd, true);
|
185 | return () => {
|
186 | componentEl.removeEventListener('touchstart', touchStart, true);
|
187 | componentEl.removeEventListener('touchend', touchEnd, true);
|
188 | };
|
189 | };
|
190 | const jsSetFocus = async (componentEl, inputEl, contentEl, footerEl, keyboardHeight) => {
|
191 | if (!contentEl && !footerEl) {
|
192 | return;
|
193 | }
|
194 | const scrollData = getScrollData(componentEl, (contentEl || footerEl), keyboardHeight);
|
195 | if (contentEl && Math.abs(scrollData.scrollAmount) < 4) {
|
196 |
|
197 |
|
198 | inputEl.focus();
|
199 | return;
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 | relocateInput(componentEl, inputEl, true, scrollData.inputSafeY);
|
205 | inputEl.focus();
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 | helpers.raf(() => componentEl.click());
|
212 |
|
213 | if (typeof window !== 'undefined') {
|
214 | let scrollContentTimeout;
|
215 | const scrollContent = async () => {
|
216 |
|
217 | if (scrollContentTimeout !== undefined) {
|
218 | clearTimeout(scrollContentTimeout);
|
219 | }
|
220 | window.removeEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
|
221 | window.removeEventListener('ionKeyboardDidShow', scrollContent);
|
222 |
|
223 | if (contentEl) {
|
224 | await contentEl.scrollByPoint(0, scrollData.scrollAmount, scrollData.scrollDuration);
|
225 | }
|
226 |
|
227 |
|
228 | relocateInput(componentEl, inputEl, false, scrollData.inputSafeY);
|
229 |
|
230 | inputEl.focus();
|
231 | };
|
232 | const doubleKeyboardEventListener = () => {
|
233 | window.removeEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
|
234 | window.addEventListener('ionKeyboardDidShow', scrollContent);
|
235 | };
|
236 | if (contentEl) {
|
237 | const scrollEl = await contentEl.getScrollElement();
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | const totalScrollAmount = scrollEl.scrollHeight - scrollEl.clientHeight;
|
251 | if (scrollData.scrollAmount > (totalScrollAmount - scrollEl.scrollTop)) {
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 | if (inputEl.type === 'password') {
|
258 |
|
259 | scrollData.scrollAmount += 50;
|
260 | window.addEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
|
261 | }
|
262 | else {
|
263 | window.addEventListener('ionKeyboardDidShow', scrollContent);
|
264 | }
|
265 | |
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | scrollContentTimeout = setTimeout(scrollContent, 1000);
|
272 | return;
|
273 | }
|
274 | }
|
275 | scrollContent();
|
276 | }
|
277 | };
|
278 | const hasPointerMoved = (threshold, startCoord, endCoord) => {
|
279 | if (startCoord && endCoord) {
|
280 | const deltaX = (startCoord.x - endCoord.x);
|
281 | const deltaY = (startCoord.y - endCoord.y);
|
282 | const distance = deltaX * deltaX + deltaY * deltaY;
|
283 | return distance > (threshold * threshold);
|
284 | }
|
285 | return false;
|
286 | };
|
287 |
|
288 | const PADDING_TIMER_KEY = '$ionPaddingTimer';
|
289 | const enableScrollPadding = (keyboardHeight) => {
|
290 | const doc = document;
|
291 | const onFocusin = (ev) => {
|
292 | setScrollPadding(ev.target, keyboardHeight);
|
293 | };
|
294 | const onFocusout = (ev) => {
|
295 | setScrollPadding(ev.target, 0);
|
296 | };
|
297 | doc.addEventListener('focusin', onFocusin);
|
298 | doc.addEventListener('focusout', onFocusout);
|
299 | return () => {
|
300 | doc.removeEventListener('focusin', onFocusin);
|
301 | doc.removeEventListener('focusout', onFocusout);
|
302 | };
|
303 | };
|
304 | const setScrollPadding = (input, keyboardHeight) => {
|
305 | if (input.tagName !== 'INPUT') {
|
306 | return;
|
307 | }
|
308 | if (input.parentElement && input.parentElement.tagName === 'ION-INPUT') {
|
309 | return;
|
310 | }
|
311 | if (input.parentElement &&
|
312 | input.parentElement.parentElement &&
|
313 | input.parentElement.parentElement.tagName === 'ION-SEARCHBAR') {
|
314 | return;
|
315 | }
|
316 | const el = input.closest('ion-content');
|
317 | if (el === null) {
|
318 | return;
|
319 | }
|
320 | const timer = el[PADDING_TIMER_KEY];
|
321 | if (timer) {
|
322 | clearTimeout(timer);
|
323 | }
|
324 | if (keyboardHeight > 0) {
|
325 | el.style.setProperty('--keyboard-offset', `${keyboardHeight}px`);
|
326 | }
|
327 | else {
|
328 | el[PADDING_TIMER_KEY] = setTimeout(() => {
|
329 | el.style.setProperty('--keyboard-offset', '0px');
|
330 | }, 120);
|
331 | }
|
332 | };
|
333 |
|
334 | const INPUT_BLURRING = true;
|
335 | const SCROLL_PADDING = true;
|
336 | const startInputShims = (config) => {
|
337 | const doc = document;
|
338 | const keyboardHeight = config.getNumber('keyboardHeight', 290);
|
339 | const scrollAssist = config.getBoolean('scrollAssist', true);
|
340 | const hideCaret = config.getBoolean('hideCaretOnScroll', true);
|
341 | const inputBlurring = config.getBoolean('inputBlurring', true);
|
342 | const scrollPadding = config.getBoolean('scrollPadding', true);
|
343 | const inputs = Array.from(doc.querySelectorAll('ion-input, ion-textarea'));
|
344 | const hideCaretMap = new WeakMap();
|
345 | const scrollAssistMap = new WeakMap();
|
346 | const registerInput = async (componentEl) => {
|
347 | await new Promise(resolve => helpers.componentOnReady(componentEl, resolve));
|
348 | const inputRoot = componentEl.shadowRoot || componentEl;
|
349 | const inputEl = inputRoot.querySelector('input') || inputRoot.querySelector('textarea');
|
350 | const scrollEl = componentEl.closest('ion-content');
|
351 | const footerEl = (!scrollEl) ? componentEl.closest('ion-footer') : null;
|
352 | if (!inputEl) {
|
353 | return;
|
354 | }
|
355 | if (!!scrollEl && hideCaret && !hideCaretMap.has(componentEl)) {
|
356 | const rmFn = enableHideCaretOnScroll(componentEl, inputEl, scrollEl);
|
357 | hideCaretMap.set(componentEl, rmFn);
|
358 | }
|
359 | if ((!!scrollEl || !!footerEl) && scrollAssist && !scrollAssistMap.has(componentEl)) {
|
360 | const rmFn = enableScrollAssist(componentEl, inputEl, scrollEl, footerEl, keyboardHeight);
|
361 | scrollAssistMap.set(componentEl, rmFn);
|
362 | }
|
363 | };
|
364 | const unregisterInput = (componentEl) => {
|
365 | if (hideCaret) {
|
366 | const fn = hideCaretMap.get(componentEl);
|
367 | if (fn) {
|
368 | fn();
|
369 | }
|
370 | hideCaretMap.delete(componentEl);
|
371 | }
|
372 | if (scrollAssist) {
|
373 | const fn = scrollAssistMap.get(componentEl);
|
374 | if (fn) {
|
375 | fn();
|
376 | }
|
377 | scrollAssistMap.delete(componentEl);
|
378 | }
|
379 | };
|
380 | if (inputBlurring && INPUT_BLURRING) {
|
381 | enableInputBlurring();
|
382 | }
|
383 | if (scrollPadding && SCROLL_PADDING) {
|
384 | enableScrollPadding(keyboardHeight);
|
385 | }
|
386 |
|
387 |
|
388 |
|
389 | for (const input of inputs) {
|
390 | registerInput(input);
|
391 | }
|
392 | doc.addEventListener('ionInputDidLoad', ((ev) => {
|
393 | registerInput(ev.detail);
|
394 | }));
|
395 | doc.addEventListener('ionInputDidUnload', ((ev) => {
|
396 | unregisterInput(ev.detail);
|
397 | }));
|
398 | };
|
399 |
|
400 | exports.startInputShims = startInputShims;
|