Skip to content

Commit e0762af

Browse files
committed
Move webview content state into webviewPanel instead of webviewEditor
Split out from #77131 The current webview editor api is very procedural. This model has some problems when it comes to supporting editing resources, but actually does make a lot of sense for webviews that aren't backed by real file system resources. For example, if you have a webview that edits some document in the cloud, you should not be required to implement a custom file system provider just to enable basic saving. This change moves the `onWillSave` and `webviewEditorState` properties back onto `WebviewPanel` instead of keeping them specific to `WebviewEditor`. The save implementation does not fully work yet, as the will require #81521
1 parent e5efdb4 commit e0762af

File tree

9 files changed

+39
-29
lines changed

9 files changed

+39
-29
lines changed

src/vs/editor/common/modes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ export interface IWebviewPanelOptions {
14051405
/**
14061406
* @internal
14071407
*/
1408-
export const enum WebviewEditorState {
1408+
export const enum WebviewContentState {
14091409
Readonly = 1,
14101410
Unchanged = 2,
14111411
Dirty = 3,

src/vs/vscode.proposed.d.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,30 +1085,34 @@ declare module 'vscode' {
10851085

10861086
//#region Custom editors, mjbvz
10871087

1088-
export enum WebviewEditorState {
1088+
export enum WebviewContentState {
10891089
/**
1090-
* The webview editor's content cannot be modified.
1090+
* The webview content cannot be modified.
10911091
*
1092-
* This disables save
1092+
* This disables save.
10931093
*/
10941094
Readonly = 1,
10951095

10961096
/**
1097-
* The webview editor's content has not been changed but they can be modified and saved.
1097+
* The webview content has not been changed but they can be modified and saved.
10981098
*/
10991099
Unchanged = 2,
11001100

11011101
/**
1102-
* The webview editor's content has been changed and can be saved.
1102+
* The webview content has been changed and can be saved.
11031103
*/
11041104
Dirty = 3,
11051105
}
11061106

1107-
export interface WebviewEditor extends WebviewPanel {
1108-
state: WebviewEditorState;
1107+
export interface WebviewEditorState {
1108+
readonly contentState: WebviewContentState;
1109+
}
1110+
1111+
export interface WebviewPanel {
1112+
editorState: WebviewEditorState;
11091113

11101114
/**
1111-
* Fired when the webview editor is saved.
1115+
* Fired when the webview is being saved.
11121116
*
11131117
* Both `Unchanged` and `Dirty` editors can be saved.
11141118
*
@@ -1117,6 +1121,11 @@ declare module 'vscode' {
11171121
readonly onWillSave: Event<{ waitUntil: (thenable: Thenable<boolean>) => void }>;
11181122
}
11191123

1124+
export interface WebviewEditor extends WebviewPanel {
1125+
// TODO: We likely do not want `editorState` and `onWillSave` enabled for
1126+
// resource backed webviews
1127+
}
1128+
11201129
export interface WebviewEditorProvider {
11211130
/**
11221131
* Fills out a `WebviewEditor` for a given resource.

src/vs/workbench/api/browser/mainThreadWebview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
150150
webview.setName(value);
151151
}
152152

153-
public $setState(handle: WebviewPanelHandle, state: modes.WebviewEditorState): void {
153+
public $setState(handle: WebviewPanelHandle, state: modes.WebviewContentState): void {
154154
const webview = this.getWebviewEditorInput(handle);
155155
if (webview instanceof CustomFileEditorInput) {
156156
webview.setState(state);

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
909909
CallHierarchyItem: extHostTypes.CallHierarchyItem,
910910
DebugConsoleMode: extHostTypes.DebugConsoleMode,
911911
Decoration: extHostTypes.Decoration,
912-
WebviewEditorState: extHostTypes.WebviewEditorState,
912+
WebviewContentState: extHostTypes.WebviewContentState,
913913
UIKind: UIKind
914914
};
915915
};

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ export interface MainThreadWebviewsShape extends IDisposable {
545545
$disposeWebview(handle: WebviewPanelHandle): void;
546546
$reveal(handle: WebviewPanelHandle, showOptions: WebviewPanelShowOptions): void;
547547
$setTitle(handle: WebviewPanelHandle, value: string): void;
548-
$setState(handle: WebviewPanelHandle, state: modes.WebviewEditorState): void;
548+
$setState(handle: WebviewPanelHandle, state: modes.WebviewContentState): void;
549549
$setIconPath(handle: WebviewPanelHandle, value: { light: UriComponents, dark: UriComponents } | undefined): void;
550550

551551
$setHtml(handle: WebviewPanelHandle, value: string): void;

src/vs/workbench/api/common/extHostTypeConverters.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,13 +1163,13 @@ export namespace LogLevel {
11631163
return types.LogLevel.Info;
11641164
}
11651165
}
1166-
export namespace WebviewEditorState {
1167-
export function from(state: vscode.WebviewEditorState): modes.WebviewEditorState {
1166+
export namespace WebviewContentState {
1167+
export function from(state: vscode.WebviewContentState): modes.WebviewContentState {
11681168
switch (state) {
1169-
case types.WebviewEditorState.Readonly: return modes.WebviewEditorState.Readonly;
1170-
case types.WebviewEditorState.Unchanged: return modes.WebviewEditorState.Unchanged;
1171-
case types.WebviewEditorState.Dirty: return modes.WebviewEditorState.Dirty;
1172-
default: throw new Error('Unknown vscode.WebviewEditorState');
1169+
case types.WebviewContentState.Readonly: return modes.WebviewContentState.Readonly;
1170+
case types.WebviewContentState.Unchanged: return modes.WebviewContentState.Unchanged;
1171+
case types.WebviewContentState.Dirty: return modes.WebviewContentState.Dirty;
1172+
default: throw new Error('Unknown vscode.WebviewContentState');
11731173
}
11741174
}
11751175
}

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2399,7 +2399,7 @@ export class Decoration {
23992399
bubble?: boolean;
24002400
}
24012401

2402-
export enum WebviewEditorState {
2402+
export enum WebviewContentState {
24032403
Readonly = 1,
24042404
Unchanged = 2,
24052405
Dirty = 3,

src/vs/workbench/api/common/extHostWebview.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { EditorViewColumn } from 'vs/workbench/api/common/shared/editor';
1313
import { asWebviewUri, WebviewInitData } from 'vs/workbench/api/common/shared/webview';
1414
import * as vscode from 'vscode';
1515
import { ExtHostWebviewsShape, IMainContext, MainContext, MainThreadWebviewsShape, WebviewPanelHandle, WebviewPanelViewStateData } from './extHost.protocol';
16-
import { Disposable, WebviewEditorState } from './extHostTypes';
16+
import { Disposable, WebviewContentState } from './extHostTypes';
1717

1818
type IconPath = URI | { light: URI, dark: URI };
1919

@@ -93,7 +93,9 @@ export class ExtHostWebviewEditor implements vscode.WebviewEditor {
9393
private _viewColumn: vscode.ViewColumn | undefined;
9494
private _visible: boolean = true;
9595
private _active: boolean = true;
96-
private _state = WebviewEditorState.Readonly;
96+
private _state: vscode.WebviewEditorState = {
97+
contentState: WebviewContentState.Readonly,
98+
};
9799

98100
_isDisposed: boolean = false;
99101

@@ -103,7 +105,6 @@ export class ExtHostWebviewEditor implements vscode.WebviewEditor {
103105
readonly _onDidChangeViewStateEmitter = new Emitter<vscode.WebviewPanelOnDidChangeViewStateEvent>();
104106
public readonly onDidChangeViewState: Event<vscode.WebviewPanelOnDidChangeViewStateEvent> = this._onDidChangeViewStateEmitter.event;
105107

106-
107108
constructor(
108109
handle: WebviewPanelHandle,
109110
proxy: MainThreadWebviewsShape,
@@ -214,13 +215,13 @@ export class ExtHostWebviewEditor implements vscode.WebviewEditor {
214215
this._visible = value;
215216
}
216217

217-
public get state(): vscode.WebviewEditorState {
218+
public get editorState(): vscode.WebviewEditorState {
218219
return this._state;
219220
}
220221

221-
public set state(newState: vscode.WebviewEditorState) {
222+
public set editorState(newState: vscode.WebviewEditorState) {
222223
this._state = newState;
223-
this._proxy.$setState(this._handle, typeConverters.WebviewEditorState.from(newState));
224+
this._proxy.$setState(this._handle, typeConverters.WebviewContentState.from(newState.contentState));
224225
}
225226

226227
private readonly _onWillSave = new Emitter<{ waitUntil: (thenable: Thenable<boolean>) => void }>();

src/vs/workbench/contrib/customEditor/browser/customEditorInput.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Emitter } from 'vs/base/common/event';
88
import { UnownedDisposable } from 'vs/base/common/lifecycle';
99
import { basename } from 'vs/base/common/path';
1010
import { URI } from 'vs/base/common/uri';
11-
import { WebviewEditorState } from 'vs/editor/common/modes';
11+
import { WebviewContentState } from 'vs/editor/common/modes';
1212
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
1313
import { IEditorModel } from 'vs/platform/editor/common/editor';
1414
import { ILabelService } from 'vs/platform/label/common/label';
@@ -26,7 +26,7 @@ export class CustomFileEditorInput extends WebviewInput {
2626
private name?: string;
2727
private _hasResolved = false;
2828
private readonly _editorResource: URI;
29-
private _state = WebviewEditorState.Readonly;
29+
private _state = WebviewContentState.Readonly;
3030

3131
constructor(
3232
resource: URI,
@@ -99,13 +99,13 @@ export class CustomFileEditorInput extends WebviewInput {
9999
return super.resolve();
100100
}
101101

102-
public setState(newState: WebviewEditorState): void {
102+
public setState(newState: WebviewContentState): void {
103103
this._state = newState;
104104
this._onDidChangeDirty.fire();
105105
}
106106

107107
public isDirty() {
108-
return this._state === WebviewEditorState.Dirty;
108+
return this._state === WebviewContentState.Dirty;
109109
}
110110

111111
public async confirmSave(): Promise<ConfirmResult> {

0 commit comments

Comments
 (0)