index.uts 1.2 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
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
import { ComponentPublicInstance } from 'vue';
import { CreateWebviewContext, WebviewContext } from '../interface.uts';

export const createWebviewContext : CreateWebviewContext = function (webviewId : string.WebviewIdString, component ?: ComponentPublicInstance | null) : WebviewContext | null {
	let webviewElement : UniElement | null = null;
	if (component == null) {
		const pages = getCurrentPages();
		if (pages.length > 0) {
			webviewElement = pages[pages.length - 1].$el?.parentNode?.querySelector('#' + webviewId);
		}
	} else {
		webviewElement = component.$el?.parentNode?.querySelector('#' + webviewId);
	}
	if (webviewElement == null) return null;
	return new WebviewContextImpl(webviewElement as UniWebViewElement);
}

class WebviewContextImpl implements WebviewContext {

	private webviewElement : UniWebViewElement | null = null;

	constructor(webviewElement : UniWebViewElement) {
		this.webviewElement = webviewElement;
	}

	override back() {
		this.webviewElement?.back();
	}

	override forward() {
		this.webviewElement?.forward();
	}

	override reload() {
		this.webviewElement?.reload();
	}

	override stop() {
		this.webviewElement?.stop();
	}

	override evalJS(js : string) {
		this.webviewElement?.evalJS(js);
	}
}