mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
Switch webview api back to use delegate model
For #77131 Going back the the delegate based model for a few reasons: - It gives us a better approach to add additional API hooks in the future (such as for rename) - In practive, the capabilities were almost always the same as the `userData` on the document. It is rather confusing to have both `userData` and the capabilities 'on' the document
This commit is contained in:
parent
c65ea4300d
commit
9dae408dc3
|
@ -27,8 +27,8 @@ export class PreviewManager implements vscode.CustomEditorProvider {
|
|||
private readonly zoomStatusBarEntry: ZoomStatusBarEntry,
|
||||
) { }
|
||||
|
||||
public async resolveCustomDocument(_document: vscode.CustomDocument): Promise<vscode.CustomEditorCapabilities> {
|
||||
return {};
|
||||
public async resolveCustomDocument(_document: vscode.CustomDocument): Promise<void> {
|
||||
// noop
|
||||
}
|
||||
|
||||
public async resolveCustomEditor(
|
||||
|
|
|
@ -148,8 +148,8 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview
|
|||
this.registerDynamicPreview(preview);
|
||||
}
|
||||
|
||||
public async resolveCustomDocument(_document: vscode.CustomDocument): Promise<vscode.CustomEditorCapabilities> {
|
||||
return {};
|
||||
public async resolveCustomDocument(_document: vscode.CustomDocument): Promise<void> {
|
||||
// noop
|
||||
}
|
||||
|
||||
public async resolveCustomTextEditor(
|
||||
|
|
60
src/vs/vscode.proposed.d.ts
vendored
60
src/vs/vscode.proposed.d.ts
vendored
|
@ -1222,78 +1222,88 @@ declare module 'vscode' {
|
|||
// - Should we expose edits?
|
||||
// - More properties from `TextDocument`?
|
||||
|
||||
/**
|
||||
* Defines the capabilities of a custom webview editor.
|
||||
*/
|
||||
interface CustomEditorCapabilities {
|
||||
/**
|
||||
* Defines the editing capability of a custom webview document.
|
||||
*
|
||||
* When not provided, the document is considered readonly.
|
||||
*/
|
||||
readonly editing?: CustomEditorEditingCapability;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the editing capability of a custom webview editor. This allows the webview editor to hook into standard
|
||||
* editor events such as `undo` or `save`.
|
||||
*
|
||||
* @param EditType Type of edits.
|
||||
*/
|
||||
interface CustomEditorEditingCapability<EditType = unknown> {
|
||||
interface CustomEditorEditingDelegate<EditType = unknown> {
|
||||
/**
|
||||
* Save the resource.
|
||||
*
|
||||
* @param document Document to save.
|
||||
* @param cancellation Token that signals the save is no longer required (for example, if another save was triggered).
|
||||
*
|
||||
* @return Thenable signaling that the save has completed.
|
||||
*/
|
||||
save(cancellation: CancellationToken): Thenable<void>;
|
||||
save(document: CustomDocument, cancellation: CancellationToken): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Save the existing resource at a new path.
|
||||
*
|
||||
* @param document Document to save.
|
||||
* @param targetResource Location to save to.
|
||||
*
|
||||
* @return Thenable signaling that the save has completed.
|
||||
*/
|
||||
saveAs(targetResource: Uri): Thenable<void>;
|
||||
saveAs(document: CustomDocument, targetResource: Uri): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Event triggered by extensions to signal to VS Code that an edit has occurred.
|
||||
*/
|
||||
readonly onDidEdit: Event<EditType>;
|
||||
readonly onDidEdit: Event<{
|
||||
/**
|
||||
* Document the edit is for.
|
||||
*/
|
||||
readonly document: CustomDocument;
|
||||
|
||||
/**
|
||||
* Object that describes the edit.
|
||||
*
|
||||
* Edit objects are passed back to your extension in `undoEdits`, `applyEdits`, and `revert`.
|
||||
*/
|
||||
readonly edit: EditType;
|
||||
|
||||
/**
|
||||
* Display name describing the edit.
|
||||
*/
|
||||
readonly label?: string;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Apply a set of edits.
|
||||
*
|
||||
* Note that is not invoked when `onDidEdit` is called because `onDidEdit` implies also updating the view to reflect the edit.
|
||||
*
|
||||
* @param document Document to apply edits to.
|
||||
* @param edit Array of edits. Sorted from oldest to most recent.
|
||||
*
|
||||
* @return Thenable signaling that the change has completed.
|
||||
*/
|
||||
applyEdits(edits: readonly EditType[]): Thenable<void>;
|
||||
applyEdits(document: CustomDocument, edits: readonly EditType[]): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Undo a set of edits.
|
||||
*
|
||||
* This is triggered when a user undoes an edit.
|
||||
*
|
||||
* @param document Document to undo edits from.
|
||||
* @param edit Array of edits. Sorted from most recent to oldest.
|
||||
*
|
||||
* @return Thenable signaling that the change has completed.
|
||||
*/
|
||||
undoEdits(edits: readonly EditType[]): Thenable<void>;
|
||||
undoEdits(document: CustomDocument, edits: readonly EditType[]): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Revert the file to its last saved state.
|
||||
*
|
||||
* @param document Document to revert.
|
||||
* @param change Added or applied edits.
|
||||
*
|
||||
* @return Thenable signaling that the change has completed.
|
||||
*/
|
||||
revert(change: {
|
||||
revert(document: CustomDocument, change: {
|
||||
readonly undoneEdits: readonly EditType[];
|
||||
readonly appliedEdits: readonly EditType[];
|
||||
}): Thenable<void>;
|
||||
|
@ -1311,12 +1321,13 @@ declare module 'vscode' {
|
|||
* made in quick succession, `backup` is only triggered after the last one. `backup` is not invoked when
|
||||
* `auto save` is enabled (since auto save already persists resource ).
|
||||
*
|
||||
* @param document Document to revert.
|
||||
* @param cancellation Token that signals the current backup since a new backup is coming in. It is up to your
|
||||
* extension to decided how to respond to cancellation. If for example your extension is backing up a large file
|
||||
* in an operation that takes time to complete, your extension may decide to finish the ongoing backup rather
|
||||
* than cancelling it to ensure that VS Code has some valid backup.
|
||||
*/
|
||||
backup(cancellation: CancellationToken): Thenable<void>;
|
||||
backup(document: CustomDocument, cancellation: CancellationToken): Thenable<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1375,7 +1386,7 @@ declare module 'vscode' {
|
|||
*
|
||||
* @return The capabilities of the resolved document.
|
||||
*/
|
||||
resolveCustomDocument(document: CustomDocument): Thenable<CustomEditorCapabilities>;
|
||||
resolveCustomDocument(document: CustomDocument): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Resolve a webview editor for a given resource.
|
||||
|
@ -1393,6 +1404,13 @@ declare module 'vscode' {
|
|||
* @return Thenable indicating that the webview editor has been resolved.
|
||||
*/
|
||||
resolveCustomEditor(document: CustomDocument, webviewPanel: WebviewPanel): Thenable<void>;
|
||||
|
||||
/**
|
||||
* Defines the editing capability of a custom webview document.
|
||||
*
|
||||
* When not provided, the document is considered readonly.
|
||||
*/
|
||||
readonly editingDelegate?: CustomEditorEditingDelegate;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -366,14 +366,14 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
|
|||
return this._customEditorService.models.add(resource, viewType, model);
|
||||
}
|
||||
|
||||
public async $onDidEdit(resourceComponents: UriComponents, viewType: string, editId: number): Promise<void> {
|
||||
public async $onDidEdit(resourceComponents: UriComponents, viewType: string, editId: number, label: string | undefined): Promise<void> {
|
||||
const resource = URI.revive(resourceComponents);
|
||||
const model = await this._customEditorService.models.get(resource, viewType);
|
||||
if (!model || !(model instanceof MainThreadCustomEditorModel)) {
|
||||
throw new Error('Could not find model for webview editor');
|
||||
}
|
||||
|
||||
model.pushEdit(editId);
|
||||
model.pushEdit(editId, label);
|
||||
}
|
||||
|
||||
private hookupWebviewEventDelegate(handle: extHostProtocol.WebviewPanelHandle, input: WebviewInput) {
|
||||
|
@ -604,7 +604,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
|
|||
return this._viewType;
|
||||
}
|
||||
|
||||
public pushEdit(editId: number) {
|
||||
public pushEdit(editId: number, label: string | undefined) {
|
||||
if (!this._editable) {
|
||||
throw new Error('Document is not editable');
|
||||
}
|
||||
|
@ -617,7 +617,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
|
|||
this._undoService.pushElement({
|
||||
type: UndoRedoElementType.Resource,
|
||||
resource: this.resource,
|
||||
label: 'Edit', // TODO: get this from extensions?
|
||||
label: label ?? 'Edit',
|
||||
undo: async () => {
|
||||
if (!this._editable) {
|
||||
return;
|
||||
|
|
|
@ -592,7 +592,7 @@ export interface MainThreadWebviewsShape extends IDisposable {
|
|||
$registerCustomEditorProvider(extension: WebviewExtensionDescription, viewType: string, options: modes.IWebviewPanelOptions): void;
|
||||
$unregisterEditorProvider(viewType: string): void;
|
||||
|
||||
$onDidEdit(resource: UriComponents, viewType: string, editId: number): void;
|
||||
$onDidEdit(resource: UriComponents, viewType: string, editId: number, label: string | undefined): void;
|
||||
}
|
||||
|
||||
export interface WebviewPanelViewStateData {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { Emitter, Event } from 'vs/base/common/event';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
|
||||
import { URI, UriComponents } from 'vs/base/common/uri';
|
||||
import { generateUuid } from 'vs/base/common/uuid';
|
||||
import * as modes from 'vs/editor/common/modes';
|
||||
|
@ -248,25 +248,31 @@ export class ExtHostWebviewEditor extends Disposable implements vscode.WebviewPa
|
|||
|
||||
class CustomDocument extends Disposable implements vscode.CustomDocument {
|
||||
|
||||
public static create(proxy: MainThreadWebviewsShape, viewType: string, uri: vscode.Uri) {
|
||||
return Object.seal(new CustomDocument(proxy, viewType, uri));
|
||||
public static create(
|
||||
viewType: string,
|
||||
uri: vscode.Uri,
|
||||
editingDelegate: vscode.CustomEditorEditingDelegate | undefined
|
||||
) {
|
||||
return Object.seal(new CustomDocument(viewType, uri, editingDelegate));
|
||||
}
|
||||
|
||||
// Explicitly initialize all properties as we seal the object after creation!
|
||||
|
||||
readonly #_edits = new Cache<unknown>('edits');
|
||||
|
||||
readonly #proxy: MainThreadWebviewsShape;
|
||||
readonly #viewType: string;
|
||||
readonly #uri: vscode.Uri;
|
||||
readonly #editingDelegate: vscode.CustomEditorEditingDelegate | undefined;
|
||||
|
||||
#capabilities: vscode.CustomEditorCapabilities | undefined = undefined;
|
||||
|
||||
private constructor(proxy: MainThreadWebviewsShape, viewType: string, uri: vscode.Uri) {
|
||||
private constructor(
|
||||
viewType: string,
|
||||
uri: vscode.Uri,
|
||||
editingDelegate: vscode.CustomEditorEditingDelegate | undefined,
|
||||
) {
|
||||
super();
|
||||
this.#proxy = proxy;
|
||||
this.#viewType = viewType;
|
||||
this.#uri = uri;
|
||||
this.#editingDelegate = editingDelegate;
|
||||
}
|
||||
|
||||
dispose() {
|
||||
|
@ -289,47 +295,35 @@ class CustomDocument extends Disposable implements vscode.CustomDocument {
|
|||
|
||||
//#region Internal
|
||||
|
||||
/** @internal*/ _setCapabilities(capabilities: vscode.CustomEditorCapabilities) {
|
||||
if (this.#capabilities) {
|
||||
throw new Error('Capabilities already provided');
|
||||
}
|
||||
|
||||
this.#capabilities = capabilities;
|
||||
capabilities.editing?.onDidEdit(edit => {
|
||||
const id = this.#_edits.add([edit]);
|
||||
this.#proxy.$onDidEdit(this.uri, this.viewType, id);
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal*/ async _revert(changes: { undoneEdits: number[], redoneEdits: number[] }) {
|
||||
const editing = this.getEditingCapability();
|
||||
const editing = this.getEditingDelegate();
|
||||
const undoneEdits = changes.undoneEdits.map(id => this.#_edits.get(id, 0));
|
||||
const appliedEdits = changes.redoneEdits.map(id => this.#_edits.get(id, 0));
|
||||
return editing.revert({ undoneEdits, appliedEdits });
|
||||
return editing.revert(this, { undoneEdits, appliedEdits });
|
||||
}
|
||||
|
||||
/** @internal*/ _undo(editId: number) {
|
||||
const editing = this.getEditingCapability();
|
||||
const editing = this.getEditingDelegate();
|
||||
const edit = this.#_edits.get(editId, 0);
|
||||
return editing.undoEdits([edit]);
|
||||
return editing.undoEdits(this, [edit]);
|
||||
}
|
||||
|
||||
/** @internal*/ _redo(editId: number) {
|
||||
const editing = this.getEditingCapability();
|
||||
const editing = this.getEditingDelegate();
|
||||
const edit = this.#_edits.get(editId, 0);
|
||||
return editing.applyEdits([edit]);
|
||||
return editing.applyEdits(this, [edit]);
|
||||
}
|
||||
|
||||
/** @internal*/ _save(cancellation: CancellationToken) {
|
||||
return this.getEditingCapability().save(cancellation);
|
||||
return this.getEditingDelegate().save(this, cancellation);
|
||||
}
|
||||
|
||||
/** @internal*/ _saveAs(target: vscode.Uri) {
|
||||
return this.getEditingCapability().saveAs(target);
|
||||
return this.getEditingDelegate().saveAs(this, target);
|
||||
}
|
||||
|
||||
/** @internal*/ _backup(cancellation: CancellationToken) {
|
||||
return this.getEditingCapability().backup(cancellation);
|
||||
return this.getEditingDelegate().backup(this, cancellation);
|
||||
}
|
||||
|
||||
/** @internal*/ _disposeEdits(editIds: number[]) {
|
||||
|
@ -338,13 +332,17 @@ class CustomDocument extends Disposable implements vscode.CustomDocument {
|
|||
}
|
||||
}
|
||||
|
||||
/** @internal*/ _pushEdit(edit: unknown): number {
|
||||
return this.#_edits.add([edit]);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
private getEditingCapability(): vscode.CustomEditorEditingCapability {
|
||||
if (!this.#capabilities?.editing) {
|
||||
private getEditingDelegate(): vscode.CustomEditorEditingDelegate {
|
||||
if (!this.#editingDelegate) {
|
||||
throw new Error('Document is not editable');
|
||||
}
|
||||
return this.#capabilities.editing;
|
||||
return this.#editingDelegate;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -487,17 +485,24 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
|
|||
provider: vscode.CustomEditorProvider | vscode.CustomTextEditorProvider,
|
||||
options: vscode.WebviewPanelOptions | undefined = {}
|
||||
): vscode.Disposable {
|
||||
let disposable: vscode.Disposable;
|
||||
const disposables = new DisposableStore();
|
||||
if ('resolveCustomTextEditor' in provider) {
|
||||
disposable = this._editorProviders.addTextProvider(viewType, extension, provider);
|
||||
disposables.add(this._editorProviders.addTextProvider(viewType, extension, provider));
|
||||
this._proxy.$registerTextEditorProvider(toExtensionData(extension), viewType, options);
|
||||
} else {
|
||||
disposable = this._editorProviders.addCustomProvider(viewType, extension, provider);
|
||||
disposables.add(this._editorProviders.addCustomProvider(viewType, extension, provider));
|
||||
this._proxy.$registerCustomEditorProvider(toExtensionData(extension), viewType, options);
|
||||
if (provider.editingDelegate) {
|
||||
disposables.add(provider.editingDelegate.onDidEdit(e => {
|
||||
const document = e.document;
|
||||
const editId = (document as CustomDocument)._pushEdit(e.edit);
|
||||
this._proxy.$onDidEdit(document.uri, document.viewType, editId, e.label);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
return VSCodeDisposable.from(
|
||||
disposable,
|
||||
disposables,
|
||||
new VSCodeDisposable(() => {
|
||||
this._proxy.$unregisterEditorProvider(viewType);
|
||||
}));
|
||||
|
@ -592,12 +597,11 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
|
|||
}
|
||||
|
||||
const revivedResource = URI.revive(resource);
|
||||
const document = CustomDocument.create(this._proxy, viewType, revivedResource);
|
||||
const capabilities = await entry.provider.resolveCustomDocument(document);
|
||||
document._setCapabilities(capabilities);
|
||||
const document = CustomDocument.create(viewType, revivedResource, entry.provider.editingDelegate);
|
||||
await entry.provider.resolveCustomDocument(document);
|
||||
this._documents.add(document);
|
||||
return {
|
||||
editable: !!capabilities.editing
|
||||
editable: !!entry.provider.editingDelegate,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue