|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as pLimit from 'p-limit'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as vscode from 'vscode'; |
| 9 | +import { Disposable } from './dispose'; |
| 10 | + |
| 11 | +export namespace Testing { |
| 12 | + export const abcEditorContentChangeCommand = '_abcEditor.contentChange'; |
| 13 | + export const abcEditorTypeCommand = '_abcEditor.type'; |
| 14 | + |
| 15 | + export interface CustomEditorContentChangeEvent { |
| 16 | + readonly content: string; |
| 17 | + readonly source: vscode.Uri; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export class AbcTextEditorProvider implements vscode.CustomTextEditorProvider { |
| 22 | + |
| 23 | + public static readonly viewType = 'testWebviewEditor.abc'; |
| 24 | + |
| 25 | + private activeEditor?: AbcEditor; |
| 26 | + |
| 27 | + public constructor( |
| 28 | + private readonly context: vscode.ExtensionContext, |
| 29 | + ) { } |
| 30 | + |
| 31 | + public register(): vscode.Disposable { |
| 32 | + const provider = vscode.window.registerCustomEditorProvider(AbcTextEditorProvider.viewType, this); |
| 33 | + |
| 34 | + const commands: vscode.Disposable[] = []; |
| 35 | + commands.push(vscode.commands.registerCommand(Testing.abcEditorTypeCommand, (content: string) => { |
| 36 | + this.activeEditor?.testing_fakeInput(content); |
| 37 | + })); |
| 38 | + |
| 39 | + return vscode.Disposable.from(provider, ...commands); |
| 40 | + } |
| 41 | + |
| 42 | + public async resolveCustomTextEditor(document: vscode.TextDocument, panel: vscode.WebviewPanel) { |
| 43 | + const editor = new AbcEditor(document, this.context.extensionPath, panel); |
| 44 | + |
| 45 | + this.activeEditor = editor; |
| 46 | + |
| 47 | + panel.onDidChangeViewState(({ webviewPanel }) => { |
| 48 | + if (this.activeEditor === editor && !webviewPanel.active) { |
| 49 | + this.activeEditor = undefined; |
| 50 | + } |
| 51 | + if (webviewPanel.active) { |
| 52 | + this.activeEditor = editor; |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class AbcEditor extends Disposable { |
| 59 | + |
| 60 | + public readonly _onDispose = this._register(new vscode.EventEmitter<void>()); |
| 61 | + public readonly onDispose = this._onDispose.event; |
| 62 | + |
| 63 | + private readonly limit = pLimit(1); |
| 64 | + private syncedVersion: number = -1; |
| 65 | + private currentWorkspaceEdit?: Thenable<void>; |
| 66 | + |
| 67 | + constructor( |
| 68 | + private readonly document: vscode.TextDocument, |
| 69 | + private readonly _extensionPath: string, |
| 70 | + private readonly panel: vscode.WebviewPanel, |
| 71 | + ) { |
| 72 | + super(); |
| 73 | + |
| 74 | + panel.webview.options = { |
| 75 | + enableScripts: true, |
| 76 | + }; |
| 77 | + panel.webview.html = this.html; |
| 78 | + |
| 79 | + this._register(vscode.workspace.onDidChangeTextDocument(e => { |
| 80 | + if (e.document === this.document) { |
| 81 | + this.update(); |
| 82 | + } |
| 83 | + })); |
| 84 | + |
| 85 | + this._register(panel.webview.onDidReceiveMessage(message => { |
| 86 | + switch (message.type) { |
| 87 | + case 'edit': |
| 88 | + this.doEdit(message.value); |
| 89 | + break; |
| 90 | + |
| 91 | + case 'didChangeContent': |
| 92 | + vscode.commands.executeCommand(Testing.abcEditorContentChangeCommand, { |
| 93 | + content: message.value, |
| 94 | + source: document.uri, |
| 95 | + } as Testing.CustomEditorContentChangeEvent); |
| 96 | + break; |
| 97 | + } |
| 98 | + })); |
| 99 | + |
| 100 | + this._register(panel.onDidDispose(() => { this.dispose(); })); |
| 101 | + |
| 102 | + this.update(); |
| 103 | + } |
| 104 | + |
| 105 | + public testing_fakeInput(value: string) { |
| 106 | + this.panel.webview.postMessage({ |
| 107 | + type: 'fakeInput', |
| 108 | + value: value, |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + private async doEdit(value: string) { |
| 113 | + const edit = new vscode.WorkspaceEdit(); |
| 114 | + edit.replace(this.document.uri, this.document.validateRange(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(999999, 999999))), value); |
| 115 | + this.limit(() => { |
| 116 | + this.currentWorkspaceEdit = vscode.workspace.applyEdit(edit).then(() => { |
| 117 | + this.syncedVersion = this.document.version; |
| 118 | + this.currentWorkspaceEdit = undefined; |
| 119 | + }); |
| 120 | + return this.currentWorkspaceEdit; |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + public dispose() { |
| 125 | + if (this.isDisposed) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + this._onDispose.fire(); |
| 130 | + super.dispose(); |
| 131 | + } |
| 132 | + |
| 133 | + private get html() { |
| 134 | + const contentRoot = path.join(this._extensionPath, 'customEditorMedia'); |
| 135 | + const scriptUri = vscode.Uri.file(path.join(contentRoot, 'textEditor.js')); |
| 136 | + const nonce = Date.now() + ''; |
| 137 | + return /* html */`<!DOCTYPE html> |
| 138 | + <html lang="en"> |
| 139 | + <head> |
| 140 | + <meta charset="UTF-8"> |
| 141 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 142 | + <meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'nonce-${nonce}'; style-src 'unsafe-inline';"> |
| 143 | + <title>Document</title> |
| 144 | + </head> |
| 145 | + <body> |
| 146 | + <textarea style="width: 300px; height: 300px;"></textarea> |
| 147 | + <script nonce=${nonce} src="${this.panel.webview.asWebviewUri(scriptUri)}"></script> |
| 148 | + </body> |
| 149 | + </html>`; |
| 150 | + } |
| 151 | + |
| 152 | + public async update() { |
| 153 | + await this.currentWorkspaceEdit; |
| 154 | + |
| 155 | + if (this.isDisposed || this.syncedVersion >= this.document.version) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + this.panel.webview.postMessage({ |
| 160 | + type: 'setValue', |
| 161 | + value: this.document.getText(), |
| 162 | + }); |
| 163 | + this.syncedVersion = this.document.version; |
| 164 | + } |
| 165 | +} |
0 commit comments