Skip to content

Commit e2085fa

Browse files
authored
Merge pull request microsoft#191846 from microsoft/aamunger/flakyImageCopy
look for re-used output id containing the image
2 parents 08631fa + 9fbe99c commit e2085fa

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

src/vs/workbench/contrib/notebook/browser/controller/cellOutputActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ registerAction2(class CopyCellOutputAction extends Action2 {
5555
const mimeType = outputViewModel.pickedMimeType?.mimeType;
5656

5757
if (mimeType?.startsWith('image/')) {
58-
const focusOptions = { skipReveal: true, outputId: outputViewModel.model.outputId };
58+
const focusOptions = { skipReveal: true, outputId: outputViewModel.model.outputId, altOutputId: outputViewModel.model.alternativeOutputId };
5959
await notebookEditor.focusNotebookCell(outputViewModel.cellViewModel as ICellViewModel, 'output', focusOptions);
6060
notebookEditor.copyOutputImage(outputViewModel);
6161
} else {

src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export interface IFocusNotebookCellOptions {
152152
readonly focusEditorLine?: number;
153153
readonly minimalScrolling?: boolean;
154154
readonly outputId?: string;
155+
readonly altOutputId?: string;
155156
}
156157

157158
//#endregion

src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
23502350
}
23512351

23522352
const focusElementId = options?.outputId ?? cell.id;
2353-
this._webview.focusOutput(focusElementId, this._webviewFocused);
2353+
this._webview.focusOutput(focusElementId, options?.altOutputId, this._webviewFocused);
23542354

23552355
cell.updateEditState(CellEditState.Preview, 'focusNotebookCell');
23562356
cell.focusMode = CellFocusMode.Output;

src/vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,8 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
15481548
async copyImage(output: ICellOutputViewModel): Promise<void> {
15491549
this._sendMessageToWebview({
15501550
type: 'copyImage',
1551-
outputId: output.model.outputId
1551+
outputId: output.model.outputId,
1552+
altOutputId: output.model.alternativeOutputId
15521553
});
15531554
}
15541555

@@ -1608,7 +1609,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
16081609
this.webview?.focus();
16091610
}
16101611

1611-
focusOutput(cellOrOutputId: string, viewFocused: boolean) {
1612+
focusOutput(cellOrOutputId: string, alternateId: string | undefined, viewFocused: boolean) {
16121613
if (this._disposed) {
16131614
return;
16141615
}
@@ -1620,6 +1621,7 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
16201621
this._sendMessageToWebview({
16211622
type: 'focus-output',
16221623
cellOrOutputId: cellOrOutputId,
1624+
alternateId: alternateId
16231625
});
16241626
}
16251627

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewMessages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,13 @@ export interface IShowOutputMessage {
269269
export interface ICopyImageMessage {
270270
readonly type: 'copyImage';
271271
readonly outputId: string;
272+
readonly altOutputId: string;
272273
}
273274

274275
export interface IFocusOutputMessage {
275276
readonly type: 'focus-output';
276277
readonly cellOrOutputId: string;
278+
readonly alternateId?: string;
277279
}
278280

279281
export interface IAckOutputHeight {

src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,9 @@ async function webviewPreloads(ctx: PreloadContext) {
474474
});
475475
};
476476

477-
function focusFirstFocusableOrContainerInOutput(cellOrOutputId: string) {
478-
const cellOutputContainer = document.getElementById(cellOrOutputId);
477+
function focusFirstFocusableOrContainerInOutput(cellOrOutputId: string, alternateId?: string) {
478+
const cellOutputContainer = document.getElementById(cellOrOutputId) ??
479+
alternateId ? document.getElementById(alternateId!) : undefined;
479480
if (cellOutputContainer) {
480481
if (cellOutputContainer.contains(document.activeElement)) {
481482
return;
@@ -1362,17 +1363,18 @@ async function webviewPreloads(ctx: PreloadContext) {
13621363
});
13631364
};
13641365

1365-
const copyOutputImage = async (outputId: string, retries = 5) => {
1366+
const copyOutputImage = async (outputId: string, altOutputId: string, retries = 5) => {
13661367
if (!document.hasFocus() && retries > 0) {
13671368
// copyImage can be called from outside of the webview, which means this function may be running whilst the webview is gaining focus.
13681369
// Since navigator.clipboard.write requires the document to be focused, we need to wait for focus.
13691370
// We cannot use a listener, as there is a high chance the focus is gained during the setup of the listener resulting in us missing it.
1370-
setTimeout(() => { copyOutputImage(outputId, retries - 1); }, 20);
1371+
setTimeout(() => { copyOutputImage(outputId, altOutputId, retries - 1); }, 20);
13711372
return;
13721373
}
13731374

13741375
try {
1375-
const image = document.getElementById(outputId)?.querySelector('img');
1376+
const image = document.getElementById(outputId)?.querySelector('img')
1377+
?? document.getElementById(altOutputId)?.querySelector('img');
13761378
if (image) {
13771379
await navigator.clipboard.write([new ClipboardItem({
13781380
'image/png': new Promise((resolve) => {
@@ -1500,9 +1502,7 @@ async function webviewPreloads(ctx: PreloadContext) {
15001502
break;
15011503
}
15021504
case 'copyImage': {
1503-
1504-
await copyOutputImage(event.data.outputId);
1505-
1505+
await copyOutputImage(event.data.outputId, event.data.altOutputId);
15061506
break;
15071507
}
15081508
case 'ack-dimension': {
@@ -1524,7 +1524,7 @@ async function webviewPreloads(ctx: PreloadContext) {
15241524
break;
15251525
}
15261526
case 'focus-output':
1527-
focusFirstFocusableOrContainerInOutput(event.data.cellOrOutputId);
1527+
focusFirstFocusableOrContainerInOutput(event.data.cellOrOutputId, event.data.alternateId);
15281528
break;
15291529
case 'decorations': {
15301530
let outputContainer = document.getElementById(event.data.cellId);

0 commit comments

Comments
 (0)