Skip to content

Commit 04da82c

Browse files
committed
Let webview editors show in diff original view too
Fixes microsoft#80740 The original view in diff views uses a data uri instead of a resource on disk. Make sure our webview editors can register themselves to handle this case too
1 parent 1961739 commit 04da82c

4 files changed

Lines changed: 47 additions & 19 deletions

File tree

extensions/image-preview/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"displayName": "%webviewEditors.displayName%",
2525
"selector": [
2626
{
27-
"filenamePattern": "*.{jpg,jpe,jpeg,png,bmp,gif,ico,tga,tif,tiff,webp}"
27+
"filenamePattern": "*.{jpg,jpe,jpeg,png,bmp,gif,ico,tga,tif,tiff,webp}",
28+
"mime": "image/*"
2829
}
2930
]
3031
}

extensions/image-preview/src/preview.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class Preview extends Disposable {
8181
private getWebiewContents(webviewEditor: vscode.WebviewEditor, resource: vscode.Uri): string {
8282
const settings = {
8383
isMac: process.platform === 'darwin',
84-
src: encodeURI(webviewEditor.webview.asWebviewUri(resource).toString(true))
84+
src: this.getResourcePath(webviewEditor, resource)
8585
};
8686

8787
return /* html */`<!DOCTYPE html>
@@ -101,6 +101,14 @@ export class Preview extends Disposable {
101101
</html>`;
102102
}
103103

104+
private getResourcePath(webviewEditor: vscode.WebviewEditor, resource: vscode.Uri) {
105+
if (resource.scheme === 'data') {
106+
return encodeURI(resource.toString(true));
107+
}
108+
109+
return encodeURI(webviewEditor.webview.asWebviewUri(resource).toString(true));
110+
}
111+
104112
private extensionResource(path: string) {
105113
return this.webviewEditor.webview.asWebviewUri(this.extensionRoot.with({
106114
path: this.extensionRoot.path + path

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

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import { coalesce, distinct } from 'vs/base/common/arrays';
77
import * as glob from 'vs/base/common/glob';
88
import { UnownedDisposable } from 'vs/base/common/lifecycle';
9-
import { basename } from 'vs/base/common/resources';
9+
import { Schemas } from 'vs/base/common/network';
10+
import { basename, DataUri } from 'vs/base/common/resources';
1011
import { withNullAsUndefined } from 'vs/base/common/types';
1112
import { URI } from 'vs/base/common/uri';
1213
import { generateUuid } from 'vs/base/common/uuid';
@@ -16,7 +17,7 @@ import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/ed
1617
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1718
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
1819
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
19-
import { EditorOptions, IEditor, IEditorInput } from 'vs/workbench/common/editor';
20+
import { EditorInput, EditorOptions, IEditor, IEditorInput } from 'vs/workbench/common/editor';
2021
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
2122
import { webviewEditorsExtensionPoint } from 'vs/workbench/contrib/customEditor/browser/extensionPoint';
2223
import { CustomEditorDiscretion, CustomEditorInfo, CustomEditorSelector, ICustomEditorService } from 'vs/workbench/contrib/customEditor/common/customEditor';
@@ -203,25 +204,33 @@ export class CustomEditorContribution implements IWorkbenchContribution {
203204
}
204205

205206
if (editor instanceof DiffEditorInput) {
206-
if (editor.modifiedInput instanceof CustomFileEditorInput) {
207-
return;
208-
}
209-
const resource = editor.modifiedInput.getResource();
210-
if (!resource) {
211-
return;
212-
}
207+
const getCustomEditorOverrideForSubInput = (subInput: IEditorInput): EditorInput | undefined => {
208+
if (subInput instanceof CustomFileEditorInput) {
209+
return undefined;
210+
}
211+
const resource = subInput.getResource();
212+
if (!resource) {
213+
return undefined;
214+
}
213215

214-
const customEditors = distinct([
215-
...this.customEditorService.getUserConfiguredCustomEditors(resource),
216-
...this.customEditorService.getContributedCustomEditors(resource),
217-
], editor => editor.id);
216+
const editors = distinct([
217+
...this.customEditorService.getUserConfiguredCustomEditors(resource),
218+
...this.customEditorService.getContributedCustomEditors(resource),
219+
], editor => editor.id);
218220

219-
// Always prefer the first editor in the diff editor case
220-
if (customEditors.length > 0) {
221+
// Always prefer the first editor in the diff editor case
222+
return editors.length
223+
? this.customEditorService.createInput(resource, editors[0].id, group)
224+
: undefined;
225+
};
226+
227+
const modifiedOverride = getCustomEditorOverrideForSubInput(editor.modifiedInput);
228+
const originalOverride = getCustomEditorOverrideForSubInput(editor.originalInput);
229+
230+
if (modifiedOverride || originalOverride) {
221231
return {
222232
override: (async () => {
223-
const newModified = this.customEditorService.createInput(resource, customEditors[0].id, group);
224-
const input = new DiffEditorInput(editor.getName(), editor.getDescription(), editor.originalInput, newModified);
233+
const input = new DiffEditorInput(editor.getName(), editor.getDescription(), originalOverride || editor.originalInput, modifiedOverride || editor.modifiedInput);
225234
return this.editorService.openEditor(input, { ...options, ignoreOverrides: true }, group);
226235
})(),
227236
};
@@ -285,6 +294,15 @@ export class CustomEditorContribution implements IWorkbenchContribution {
285294
}
286295

287296
function matches(selector: CustomEditorSelector, resource: URI): boolean {
297+
if (resource.scheme === Schemas.data) {
298+
const metadata = DataUri.parseMetaData(resource);
299+
const mime = metadata.get(DataUri.META_DATA_MIME);
300+
if (!selector.mime || !mime) {
301+
return false;
302+
}
303+
return glob.match(selector.mime, mime.toLowerCase());
304+
}
305+
288306
if (!selector.filenamePattern && !selector.scheme) {
289307
return false;
290308
}

src/vs/workbench/contrib/customEditor/common/customEditor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const enum CustomEditorDiscretion {
3131
export interface CustomEditorSelector {
3232
readonly scheme?: string;
3333
readonly filenamePattern?: string;
34+
readonly mime?: string;
3435
}
3536

3637
export interface CustomEditorInfo {

0 commit comments

Comments
 (0)