解决webview服务类问题
Some checks failed
Monaco Editor checks / Monaco Editor checks (push) Failing after 39s

This commit is contained in:
mxwj 2024-11-28 15:12:12 +08:00
parent ae05db9b66
commit 73e59d6517
2 changed files with 10 additions and 26 deletions

BIN
src.7z Normal file

Binary file not shown.

@ -5,10 +5,10 @@
import { EditorPane } from '../../../../browser/parts/editor/editorPane.js'; import { EditorPane } from '../../../../browser/parts/editor/editorPane.js';
import { Dimension } from '../../../../../base/browser/dom.js'; import { Dimension } from '../../../../../base/browser/dom.js';
import { StockDetailsInput } from './StockDetailsInput.js'; import { StockDetailsInput } from './StockDetailsInput.js';
import { IWebviewService, IOverlayWebview } from '../../../../contrib/webview/browser/webview.js';
import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.js'; import { ITelemetryService } from '../../../../../platform/telemetry/common/telemetry.js';
import { IThemeService } from '../../../../../platform/theme/common/themeService.js'; import { IThemeService } from '../../../../../platform/theme/common/themeService.js';
import { IStorageService } from '../../../../../platform/storage/common/storage.js'; import { IStorageService } from '../../../../../platform/storage/common/storage.js';
import { CancellationToken } from '../../../../../base/common/cancellation.js'; import { CancellationToken } from '../../../../../base/common/cancellation.js';
import { IEditorOptions } from '../../../../../platform/editor/common/editor.js'; import { IEditorOptions } from '../../../../../platform/editor/common/editor.js';
import { IEditorOpenContext } from '../../../../common/editor.js'; import { IEditorOpenContext } from '../../../../common/editor.js';
@ -18,19 +18,20 @@ import * as DOM from '../../../../../base/browser/dom.js';
export class StockDetailsEditor extends EditorPane { export class StockDetailsEditor extends EditorPane {
static readonly ID = 'workbench.editor.stockDetails'; static readonly ID = 'workbench.editor.stockDetails';
private container: HTMLElement | null = null; private webview: IOverlayWebview | null = null;
constructor( constructor(
group: IEditorGroup, group: IEditorGroup,
@ITelemetryService telemetryService: ITelemetryService, @ITelemetryService telemetryService: ITelemetryService,
@IThemeService themeService: IThemeService, @IThemeService themeService: IThemeService,
@IStorageService storageService: IStorageService @IStorageService storageService: IStorageService,
@IWebviewService private readonly webviewService: IWebviewService
) { ) {
super(StockDetailsEditor.ID, group, telemetryService, themeService, storageService); super(StockDetailsEditor.ID, group, telemetryService, themeService, storageService);
} }
/** /**
* * WebView
*/ */
protected createEditor(parent: HTMLElement): void { protected createEditor(parent: HTMLElement): void {
// 使用 WebView 服务创建 IOverlayWebview 实例 // 使用 WebView 服务创建 IOverlayWebview 实例
@ -41,38 +42,31 @@ export class StockDetailsEditor extends EditorPane {
contentOptions: { contentOptions: {
allowScripts: true, allowScripts: true,
localResourceRoots: [], // 根据需求设置本地资源根路径 localResourceRoots: [], // 根据需求设置本地资源根路径
localResourceRoots: [], // 根据需求设置本地资源根路径
}, },
extension: undefined, extension: undefined,
}); });
// 将 WebView 定位到父元素 // 将 WebView 定位到父元素
if (this.webview) { if (this.webview) {
// 获取当前激活的窗口
// 获取当前激活的窗口 // 获取当前激活的窗口
const targetWindow = DOM.getActiveWindow(); const targetWindow = DOM.getActiveWindow();
if (!targetWindow) { if (!targetWindow) {
console.error('无法获取活动窗口'); console.error('无法获取活动窗口');
return; return;
} }
// Claim WebView 所有权,绑定到目标窗口
// Claim WebView 所有权,绑定到目标窗口 // Claim WebView 所有权,绑定到目标窗口
this.webview.claim(this, targetWindow as any, undefined); this.webview.claim(this, targetWindow as any, undefined);
// 使用 layoutWebviewOverElement 方法,将 WebView 定位到 `parent`
// 使用 layoutWebviewOverElement 方法,将 WebView 定位到 `parent` // 使用 layoutWebviewOverElement 方法,将 WebView 定位到 `parent`
this.webview.layoutWebviewOverElement(parent, new Dimension(parent.offsetWidth, parent.offsetHeight)); this.webview.layoutWebviewOverElement(parent, new Dimension(parent.offsetWidth, parent.offsetHeight));
} }
} }
/** /**
* * WebView
*/ */
override async setInput( override async setInput(
input: StockDetailsInput, input: StockDetailsInput,
@ -82,8 +76,7 @@ export class StockDetailsEditor extends EditorPane {
): Promise<void> { ): Promise<void> {
await super.setInput(input, options, context, token); await super.setInput(input, options, context, token);
if (input.resource) { if (this.webview && input.resource) {
console.log(`setInput 接收到股票代码: ${input.getCode()}`);
const stockCode = input.getCode(); const stockCode = input.getCode();
if (!stockCode || stockCode.trim() === '') { if (!stockCode || stockCode.trim() === '') {
@ -169,7 +162,7 @@ export class StockDetailsEditor extends EditorPane {
setTimeout(() => { setTimeout(() => {
document.getElementById('price').textContent = '价格: ¥' + (Math.random() * 100).toFixed(2); document.getElementById('price').textContent = '价格: ¥' + (Math.random() * 100).toFixed(2);
document.getElementById('change').textContent = '涨跌幅: ' + (Math.random() * 10 - 5).toFixed(2) + '%'; document.getElementById('change').textContent = '涨跌幅: ' + (Math.random() * 10 - 5).toFixed(2) + '%';
}, 100); }, 1000);
</script> </script>
</body> </body>
</html> </html>
@ -205,13 +198,10 @@ export class StockDetailsEditor extends EditorPane {
if (this.webview && this.webview.container) { if (this.webview && this.webview.container) {
const webviewContainer = this.webview.container; const webviewContainer = this.webview.container;
// 设置 WebView 的宽高
// 设置 WebView 的宽高 // 设置 WebView 的宽高
webviewContainer.style.width = `${dimension.width}px`; webviewContainer.style.width = `${dimension.width}px`;
webviewContainer.style.height = `${dimension.height}px`; webviewContainer.style.height = `${dimension.height}px`;
// 限制 WebView 到当前编辑器区域
// 限制 WebView 到当前编辑器区域 // 限制 WebView 到当前编辑器区域
const clippingContainer = this.getContainer(); const clippingContainer = this.getContainer();
if (clippingContainer) { if (clippingContainer) {
@ -224,16 +214,10 @@ export class StockDetailsEditor extends EditorPane {
* *
*/ */
override clearInput(): void { override clearInput(): void {
if (this.container) { if (this.webview) {
this.container.textContent = ''; // 不使用 innerHTML this.webview.setHtml('<!DOCTYPE html><html><body></body></html>'); // 清空内容
} }
super.clearInput(); super.clearInput();
} }
} }