From 19893994d57e87d0cadbaef36c1734b4b94b85fb Mon Sep 17 00:00:00 2001 From: "maikai (A)" Date: Mon, 8 Jul 2024 19:58:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=86refresh=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E4=B8=BAloadUrl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rn_webview/src/main/ets/RNCWebView.ets | 17 +- .../src/main/ets/WebViewBaseOperate.ets | 199 ++++++++++++++++++ .../src/main/ets/webViewBaseOperate.ets | 171 --------------- 3 files changed, 204 insertions(+), 183 deletions(-) create mode 100644 harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets delete mode 100644 harmony/rn_webview/src/main/ets/webViewBaseOperate.ets diff --git a/harmony/rn_webview/src/main/ets/RNCWebView.ets b/harmony/rn_webview/src/main/ets/RNCWebView.ets index a03477e80..76e3a356d 100644 --- a/harmony/rn_webview/src/main/ets/RNCWebView.ets +++ b/harmony/rn_webview/src/main/ets/RNCWebView.ets @@ -27,7 +27,7 @@ import webview from '@ohos.web.webview'; import { url as OSUrl } from '@kit.ArkTS'; import { RNC } from '@rnoh/react-native-openharmony/generated'; import Logger from './Logger'; -import { BaseOperate } from './webViewBaseOperate'; +import { BaseOperate } from './WebViewBaseOperate'; import { AlertEvent, CACHE_MODE, @@ -377,8 +377,8 @@ export struct RNCWebView { .overScrollMode(this.overScrollMode) .onProgressChange((event) => { if (event) { - this.progress = event.newProgress - this.onProgressChange() + this.progress = event.newProgress + this.onProgressChange() } }) .onScroll((event) => { @@ -453,14 +453,6 @@ export struct RNCWebView { if (!this.hasRegisterJavaScriptProxy) { this.registerPostMessage() } - if (this.descriptorWrapper.props.userAgent) { - try { - this.controller.setCustomUserAgent(this.descriptorWrapper.props.userAgent); - } catch (error) { - Logger.debug(TAG, - `[RNOH] setCustomUserAgent ErrorCode: ${error.code}, Message: ${error.message}, userAgent: ${this.descriptorWrapper.props.userAgent}`); - } - } }) .onAlert((event) => this.onJavascriptAlert(event)) .onConfirm((event) => this.onJavascriptConfirm(event)) @@ -570,7 +562,8 @@ export struct RNCWebView { } }; this.controller.registerJavaScriptProxy(bridge, JAVASCRIPT_INTERFACE, ["postMessage"]) - this.controller.refresh() + // this.controller.refresh() + this.controller.loadUrl(this.source.uri, this.headers); this.hasRegisterJavaScriptProxy = true } } diff --git a/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets new file mode 100644 index 000000000..036220cd4 --- /dev/null +++ b/harmony/rn_webview/src/main/ets/WebViewBaseOperate.ets @@ -0,0 +1,199 @@ +import { RNC } from '@rnoh/react-native-openharmony/generated/ts' +import { webview } from '@kit.ArkWeb' +import { CACHE_MODE, ONE_HUNDRED, WebViewEventParams } from './Magic'; +import Logger from './Logger'; + +export const TAG = "WebView" + + +interface ProgressInterface { + progress: number; +} + +interface LoadingErrorInterface { + code: number; + description: string +} + +interface ScrollInterface { + x: number; + y: number +} + +interface CreateWebViewEventInterface { + type: string; + progress: number; +} + +export class BaseOperate { + static instance: BaseOperate | null = null + private eventEmitter: RNC.RNCWebView.EventEmitter + private controller: webview.WebviewController + + constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { + this.eventEmitter = eventEmitter + this.controller = controller + } + + static getInstance(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController): BaseOperate { + if (BaseOperate.instance === null) { + BaseOperate.instance = new BaseOperate(eventEmitter, controller) + } + return BaseOperate.instance + } + + emitProgressChange(params: ProgressInterface) { + try { + this.eventEmitter!.emit('loadingProgress', { + url: this.controller.getUrl(), + loading: params.progress != ONE_HUNDRED, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + progress: params.progress / ONE_HUNDRED + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + emitLoadingStart(params: ProgressInterface) { + try { + this.eventEmitter!.emit('loadingStart', { + url: this.controller.getUrl(), + loading: params.progress != ONE_HUNDRED, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + navigationType: "other", + mainDocumentURL: "" + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + emitLoadingFinish(params: ProgressInterface) { + try { + this.eventEmitter!.emit('loadingFinish', { + url: this.controller.getUrl(), + loading: params.progress != ONE_HUNDRED, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + navigationType: "other", + mainDocumentURL: "" + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + emitLoadingError(params: LoadingErrorInterface) { + try { + this.eventEmitter!.emit('loadingError', { + url: this.controller.getUrl(), + loading: false, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + domain: "", + code: params.code, + description: params.description + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + emitHttpError(params: LoadingErrorInterface) { + try { + this.eventEmitter!.emit('httpError', { + url: this.controller.getUrl(), + loading: false, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + description: params.description, + statusCode: params.code + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + emitScroll(params: ScrollInterface) { + this.eventEmitter!.emit('scroll', { + contentInset: { + bottom: 0, + left: 0, + right: 0, + top: 0 + }, + contentOffset: { y: params.y, x: params.x }, + contentSize: { height: 0, width: 0 }, + layoutMeasurement: { height: 0, width: 0 } + }) + } + + emitShouldStartLoadWithRequest(params: ProgressInterface) { + try { + this.eventEmitter!.emit('shouldStartLoadWithRequest', { + url: this.controller.getUrl(), + loading: params.progress != ONE_HUNDRED, + title: this.controller.getTitle(), + canGoBack: this.controller.accessBackward(), + canGoForward: this.controller.accessForward(), + lockIdentifier: 0, + navigationType: "other", + mainDocumentURL: "", + isTopFrame: false + }) + } catch (error) { + Logger.error(TAG, `[RNOH]Errorcode: ${JSON.stringify(error)}`); + } + } + + transCacheMode(cacheMode: CACHE_MODE): CacheMode { + let mode = CacheMode.Default + switch (cacheMode) { + case CACHE_MODE.LOAD_DEFAULT: + mode = CacheMode.Default + break; + case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK: + mode = CacheMode.None + break; + case CACHE_MODE.LOAD_NO_CACHE: + mode = CacheMode.Online + break; + case CACHE_MODE.LOAD_CACHE_ONLY: + mode = CacheMode.Only + break; + default: + break; + } + return mode + } + + createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams { + let result: WebViewEventParams = new WebViewEventParams(param.type); + result.loading = param.progress != ONE_HUNDRED + try { + result.url = this.controller.getUrl(); + result.title = this.controller.getTitle(); + result.canGoBack = this.controller.accessBackward(); + result.canGoForward = this.controller.accessForward(); + } catch (error) { + result.url = ""; + result.title = ""; + result.canGoBack = false; + result.canGoForward = false; + } + return result; + } +} diff --git a/harmony/rn_webview/src/main/ets/webViewBaseOperate.ets b/harmony/rn_webview/src/main/ets/webViewBaseOperate.ets deleted file mode 100644 index 427300ef6..000000000 --- a/harmony/rn_webview/src/main/ets/webViewBaseOperate.ets +++ /dev/null @@ -1,171 +0,0 @@ -import { RNC } from '@rnoh/react-native-openharmony/generated/ts' -import { webview } from '@kit.ArkWeb' -import { CACHE_MODE, ONE_HUNDRED, WebViewEventParams } from './Magic'; - -interface ProgressInterface { - progress: number; -} - -interface LoadingErrorInterface { - code: number; - description: string -} - -interface ScrollInterface { - x: number; - y: number -} - -interface CreateWebViewEventInterface { - type: string; - progress: number; -} - -export class BaseOperate { - static instance: BaseOperate | null = null - private eventEmitter: RNC.RNCWebView.EventEmitter - private controller: webview.WebviewController - - constructor(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController) { - this.eventEmitter = eventEmitter - this.controller = controller - } - - static getInstance(eventEmitter: RNC.RNCWebView.EventEmitter, controller: webview.WebviewController): BaseOperate { - if (BaseOperate.instance === null) { - BaseOperate.instance = new BaseOperate(eventEmitter, controller) - } - return BaseOperate.instance - } - - emitProgressChange(params: ProgressInterface) { - this.eventEmitter!.emit('loadingProgress', { - url: this.controller.getUrl(), - loading: params.progress != ONE_HUNDRED, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - progress: params.progress / ONE_HUNDRED - }) - } - - emitLoadingStart(params: ProgressInterface) { - this.eventEmitter!.emit('loadingStart', { - url: this.controller.getUrl(), - loading: params.progress != ONE_HUNDRED, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - navigationType: "other", - mainDocumentURL: "" - }) - } - - emitLoadingFinish(params: ProgressInterface) { - this.eventEmitter!.emit('loadingFinish', { - url: this.controller.getUrl(), - loading: params.progress != ONE_HUNDRED, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - navigationType: "other", - mainDocumentURL: "" - }) - } - - emitLoadingError(params: LoadingErrorInterface) { - this.eventEmitter!.emit('loadingError', { - url: this.controller.getUrl(), - loading: false, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - domain: "", - code: params.code, - description: params.description - }) - } - - emitHttpError(params: LoadingErrorInterface) { - this.eventEmitter!.emit('httpError', { - url: this.controller.getUrl(), - loading: false, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - description: params.description, - statusCode: params.code - }) - } - - emitScroll(params: ScrollInterface) { - this.eventEmitter!.emit('scroll', { - contentInset: { - bottom: 0, - left: 0, - right: 0, - top: 0 - }, - contentOffset: { y: params.y, x: params.x }, - contentSize: { height: 0, width: 0 }, - layoutMeasurement: { height: 0, width: 0 } - }) - } - - emitShouldStartLoadWithRequest(params: ProgressInterface) { - this.eventEmitter!.emit('shouldStartLoadWithRequest', { - url: this.controller.getUrl(), - loading: params.progress != ONE_HUNDRED, - title: this.controller.getTitle(), - canGoBack: this.controller.accessBackward(), - canGoForward: this.controller.accessForward(), - lockIdentifier: 0, - navigationType: "other", - mainDocumentURL: "", - isTopFrame: false - }) - } - - transCacheMode(cacheMode: CACHE_MODE): CacheMode { - let mode = CacheMode.Default - switch (cacheMode) { - case CACHE_MODE.LOAD_DEFAULT: - mode = CacheMode.Default - break; - case CACHE_MODE.LOAD_CACHE_ELSE_NETWORK: - mode = CacheMode.None - break; - case CACHE_MODE.LOAD_NO_CACHE: - mode = CacheMode.Online - break; - case CACHE_MODE.LOAD_CACHE_ONLY: - mode = CacheMode.Only - break; - default: - break; - } - return mode - } - - createWebViewEvent(param: CreateWebViewEventInterface): WebViewEventParams { - let result: WebViewEventParams = new WebViewEventParams(param.type); - result.loading = param.progress != ONE_HUNDRED - try { - result.url = this.controller.getUrl(); - result.title = this.controller.getTitle(); - result.canGoBack = this.controller.accessBackward(); - result.canGoForward = this.controller.accessForward(); - } catch (error) { - result.url = ""; - result.title = ""; - result.canGoBack = false; - result.canGoForward = false; - } - return result; - } -}