Skip to content

Commit 60f8583

Browse files
committed
Adding setting to enable a test command for making a fake "edit" to an image preview
This will be use for testing custom editors
1 parent 06a4836 commit 60f8583

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

extensions/image-preview/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
"command": "imagePreview.zoomOut",
4646
"title": "%command.zoomOut%",
4747
"category": "Image Preview"
48+
},
49+
{
50+
"command": "imagePreview.testing.makeEdit",
51+
"title": "Make test edit",
52+
"category": "Image Preview"
4853
}
4954
],
5055
"menus": {
@@ -58,6 +63,11 @@
5863
"command": "imagePreview.zoomOut",
5964
"when": "imagePreviewFocus",
6065
"group": "1_imagePreview"
66+
},
67+
{
68+
"command": "imagePreview.testing.makeEdit",
69+
"when": "imagePreviewTestMode",
70+
"group": "1_imagePreview"
6171
}
6272
]
6373
}

extensions/image-preview/src/extension.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,20 @@ export function activate(context: vscode.ExtensionContext) {
3434
context.subscriptions.push(vscode.commands.registerCommand('imagePreview.zoomOut', () => {
3535
previewManager.activePreview?.zoomOut();
3636
}));
37+
38+
context.subscriptions.push(vscode.commands.registerCommand('imagePreview.testing.makeEdit', () => {
39+
previewManager.activePreview?.test_makeEdit();
40+
}));
41+
42+
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {
43+
if (e.affectsConfiguration('imagePreview.customEditorTestMode')) {
44+
updateTestMode();
45+
}
46+
}));
47+
updateTestMode();
3748
}
3849

50+
function updateTestMode() {
51+
const isInTestMode = vscode.workspace.getConfiguration('imagePreview').get<boolean>('customEditorTestMode', false);
52+
vscode.commands.executeCommand('setContext', 'imagePreviewTestMode', isInTestMode);
53+
}

extensions/image-preview/src/preview.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ export class PreviewManager {
4343
}
4444
});
4545

46-
const onEdit = new vscode.EventEmitter<{ now: number }>();
4746
return {
48-
editingCapability: {
49-
onEdit: onEdit.event,
50-
save: async () => { },
51-
hotExit: async () => { },
52-
applyEdits: async () => { },
53-
undoEdits: async (edits) => { console.log('undo', edits); },
54-
}
47+
editingCapability: preview
5548
};
5649
}
5750

@@ -73,7 +66,7 @@ const enum PreviewState {
7366
Active,
7467
}
7568

76-
class Preview extends Disposable {
69+
class Preview extends Disposable implements vscode.WebviewEditorEditingCapability {
7770

7871
private readonly id: string = `${Date.now()}-${Math.random().toString()}`;
7972

@@ -246,6 +239,24 @@ class Preview extends Disposable {
246239
path: this.extensionRoot.path + path
247240
}));
248241
}
242+
243+
//#region WebviewEditorCapabilities
244+
private readonly _onEdit = this._register(new vscode.EventEmitter<{ now: number }>());
245+
public readonly onEdit = this._onEdit.event;
246+
247+
async save() { }
248+
249+
async hotExit() { }
250+
251+
async applyEdits(_edits: any[]) { }
252+
253+
async undoEdits(edits: any[]) { console.log('undo', edits); }
254+
255+
//#endregion
256+
257+
public test_makeEdit() {
258+
this._onEdit.fire({ now: Date.now() });
259+
}
249260
}
250261

251262
function escapeAttribute(value: string | vscode.Uri): string {

0 commit comments

Comments
 (0)