@alfalab/bridge-to-native 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,10 +6,12 @@ import { type NativeParamsService } from './native-params-service';
6
6
  */
7
7
  export declare class ExternalLinksService {
8
8
  private nativeParamsService;
9
+ private navigationByNativeAppInProgress;
9
10
  constructor(nativeParamsService: NativeParamsService);
10
11
  handleNativeDeeplink(deeplink: string, closeWebviewBeforeCallNativeDeeplinkHandler?: boolean): void;
11
12
  getHrefToOpenInBrowser(link: string): string;
12
13
  openInBrowser(link: string): void;
13
14
  openInNewWebview(link: string, nativeTitle?: string, closeCurrentWebview?: boolean): void;
14
15
  openPdf(url: string, type?: PdfType, title?: string): void;
16
+ private navigateByNativeApp;
15
17
  }
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExternalLinksService = void 0;
4
4
  const query_and_headers_keys_1 = require("../../query-and-headers-keys");
5
5
  const constants_1 = require("../constants");
6
- const close_webview_util_1 = require("./close-webview-util");
6
+ const utils_1 = require("./utils");
7
+ const CANCEL_NEW_CALLS_TO_NA_TIME = 150;
7
8
  const QUERY_OPEN_IN_BROWSER_KEY = 'openInBrowser';
8
9
  const QUERY_OPEN_IN_BROWSER_VALUE = 'true';
9
10
  /**
@@ -13,18 +14,26 @@ const QUERY_OPEN_IN_BROWSER_VALUE = 'true';
13
14
  class ExternalLinksService {
14
15
  constructor(nativeParamsService) {
15
16
  this.nativeParamsService = nativeParamsService;
17
+ this.navigationByNativeAppInProgress = false;
16
18
  }
17
19
  handleNativeDeeplink(deeplink, closeWebviewBeforeCallNativeDeeplinkHandler = false) {
20
+ if (this.navigationByNativeAppInProgress) {
21
+ return;
22
+ }
18
23
  const clearedDeeplinkPath = deeplink.replace(constants_1.DEEP_LINK_PATTERN, '');
24
+ const originalNativeUrl = `${this.nativeParamsService.appId}://${clearedDeeplinkPath}`;
25
+ const preparedNativeUrl = this.nativeParamsService.environment === 'ios'
26
+ ? (0, utils_1.appendFromCurrentQueryParamForIos)(originalNativeUrl)
27
+ : originalNativeUrl;
19
28
  if (closeWebviewBeforeCallNativeDeeplinkHandler &&
20
29
  this.nativeParamsService.canUseNativeFeature('savedBackStack')) {
21
- (0, close_webview_util_1.closeWebviewUtil)();
30
+ (0, utils_1.closeWebviewUtil)();
22
31
  // Проверено, ОС получает диплинк и передаёт его NA, не смотря на то,
23
32
  // что это происходит в следующей макрозадаче после команды на закрытие WV.
24
- setTimeout(() => window.location.replace(`${this.nativeParamsService.appId}://${clearedDeeplinkPath}`), 0);
33
+ setTimeout(() => window.location.replace(preparedNativeUrl), 0);
25
34
  return;
26
35
  }
27
- window.location.replace(`${this.nativeParamsService.appId}://${clearedDeeplinkPath}`);
36
+ this.navigateByNativeApp(preparedNativeUrl);
28
37
  }
29
38
  getHrefToOpenInBrowser(link) {
30
39
  if (!this.nativeParamsService.canUseNativeFeature('linksInBrowser')) {
@@ -35,13 +44,16 @@ class ExternalLinksService {
35
44
  return url.href;
36
45
  }
37
46
  openInBrowser(link) {
47
+ if (this.navigationByNativeAppInProgress) {
48
+ return;
49
+ }
38
50
  if (!this.nativeParamsService.canUseNativeFeature('linksInBrowser')) {
39
51
  this.openInNewWebview(link);
40
52
  return;
41
53
  }
42
54
  const url = new URL(link);
43
55
  url.searchParams.append(QUERY_OPEN_IN_BROWSER_KEY, QUERY_OPEN_IN_BROWSER_VALUE);
44
- window.location.replace(url.href);
56
+ this.navigateByNativeApp(url.href);
45
57
  }
46
58
  openInNewWebview(link, nativeTitle = '', closeCurrentWebview = false) {
47
59
  const url = new URL(link);
@@ -51,6 +63,9 @@ class ExternalLinksService {
51
63
  this.handleNativeDeeplink(`/webFeature?type=recommendation&url=${encodeURIComponent(url.toString())}`, closeCurrentWebview);
52
64
  }
53
65
  openPdf(url, type = 'pdfFile', title) {
66
+ if (this.navigationByNativeAppInProgress) {
67
+ return;
68
+ }
54
69
  let replaceUrl = url;
55
70
  if (this.nativeParamsService.environment === 'ios') {
56
71
  const params = new URLSearchParams();
@@ -62,10 +77,18 @@ class ExternalLinksService {
62
77
  const paramsStr = params.toString();
63
78
  replaceUrl = `${this.nativeParamsService.appId}:///dashboard/pdf_viewer?${paramsStr}`;
64
79
  }
65
- const windowObjectReference = window.open(replaceUrl);
66
- if (windowObjectReference === null) {
67
- window.location.replace(replaceUrl);
68
- }
80
+ replaceUrl =
81
+ this.nativeParamsService.environment === 'ios'
82
+ ? (0, utils_1.appendFromCurrentQueryParamForIos)(replaceUrl)
83
+ : replaceUrl;
84
+ this.navigateByNativeApp(replaceUrl);
85
+ }
86
+ navigateByNativeApp(url) {
87
+ this.navigationByNativeAppInProgress = true;
88
+ window.location.replace(url);
89
+ setTimeout(() => {
90
+ this.navigationByNativeAppInProgress = false;
91
+ }, CANCEL_NEW_CALLS_TO_NA_TIME);
69
92
  }
70
93
  }
71
94
  exports.ExternalLinksService = ExternalLinksService;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.NativeNavigationAndTitleService = void 0;
5
5
  const query_and_headers_keys_1 = require("../../query-and-headers-keys");
6
- const close_webview_util_1 = require("./close-webview-util");
6
+ const utils_1 = require("./utils");
7
7
  const NativeHistoryStackStub = 0;
8
8
  /**
9
9
  * Сервис, отвечающий за взаимодействие WA с WV компонентами NA —
@@ -39,7 +39,7 @@ class NativeNavigationAndTitleService {
39
39
  }
40
40
  // eslint-disable-next-line class-methods-use-this -- удобней использовать метод в контексте экземпляра.
41
41
  closeWebview() {
42
- (0, close_webview_util_1.closeWebviewUtil)();
42
+ (0, utils_1.closeWebviewUtil)();
43
43
  }
44
44
  goBack() {
45
45
  if (this.isGoBackLocked) {
@@ -57,7 +57,7 @@ class NativeNavigationAndTitleService {
57
57
  const maxStepsToBack = this.nativeHistoryStack.length - 1;
58
58
  if (stepsToBack > maxStepsToBack) {
59
59
  if (autoCloseWebview) {
60
- (0, close_webview_util_1.closeWebviewUtil)();
60
+ (0, utils_1.closeWebviewUtil)();
61
61
  return;
62
62
  }
63
63
  this.numOfBackSteps = maxStepsToBack;
@@ -154,7 +154,7 @@ class NativeNavigationAndTitleService {
154
154
  }
155
155
  this.numOfBackSteps = 1;
156
156
  if (this.nativeHistoryStack.length < 1) {
157
- (0, close_webview_util_1.closeWebviewUtil)();
157
+ (0, utils_1.closeWebviewUtil)();
158
158
  return;
159
159
  }
160
160
  this.saveNativeHistoryStack();
@@ -0,0 +1,2 @@
1
+ export declare function appendFromCurrentQueryParamForIos(nativeUrl: string): string;
2
+ export declare const closeWebviewUtil: () => void;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.closeWebviewUtil = void 0;
4
+ exports.appendFromCurrentQueryParamForIos = appendFromCurrentQueryParamForIos;
5
+ const QUERY_CLOSE_WEBVIEW_KEY = 'closeWebView';
6
+ const QUERY_CLOSE_WEBVIEW_VALUE = 'true';
7
+ const QUERY_FROM_CURRENT_KEY = 'fromCurrent';
8
+ const QUERY_FROM_CURRENT_VALUE = 'true';
9
+ function appendFromCurrentQueryParamForIos(nativeUrl) {
10
+ const qIndex = nativeUrl.indexOf('?');
11
+ if (qIndex === -1) {
12
+ return `${nativeUrl}?${QUERY_FROM_CURRENT_KEY}=${QUERY_FROM_CURRENT_VALUE}`;
13
+ }
14
+ const base = nativeUrl.slice(0, qIndex);
15
+ const query = nativeUrl.slice(qIndex + 1);
16
+ const params = new URLSearchParams(query);
17
+ params.set(QUERY_FROM_CURRENT_KEY, QUERY_FROM_CURRENT_VALUE);
18
+ return `${base}?${params.toString()}`;
19
+ }
20
+ const closeWebviewUtil = () => {
21
+ const originalPageUrl = new URL(window.location.href);
22
+ originalPageUrl.searchParams.set(QUERY_CLOSE_WEBVIEW_KEY, QUERY_CLOSE_WEBVIEW_VALUE);
23
+ window.location.href = originalPageUrl.toString();
24
+ };
25
+ exports.closeWebviewUtil = closeWebviewUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfalab/bridge-to-native",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "license": "MIT",
5
5
  "description": "Утилита для удобной работы веб приложения внутри нативного приложения и коммуникации с ним.",
6
6
  "engines": {
@@ -1 +0,0 @@
1
- export declare const closeWebviewUtil: () => void;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.closeWebviewUtil = void 0;
4
- const QUERY_CLOSE_WEBVIEW_KEY = 'closeWebView';
5
- const QUERY_CLOSE_WEBVIEW_VALUE = 'true';
6
- const closeWebviewUtil = () => {
7
- const originalPageUrl = new URL(window.location.href);
8
- originalPageUrl.searchParams.set(QUERY_CLOSE_WEBVIEW_KEY, QUERY_CLOSE_WEBVIEW_VALUE);
9
- window.location.href = originalPageUrl.toString();
10
- };
11
- exports.closeWebviewUtil = closeWebviewUtil;