-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
notebook.contribution.ts
61 lines (49 loc) · 2.16 KB
/
notebook.contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { CopyAction, CutAction, PasteAction } from 'vs/editor/contrib/clipboard/clipboard';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { getActiveNotebookEditor } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions';
import { ElectronWebviewBasedWebview } from 'vs/workbench/contrib/webview/electron-browser/webviewElement';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { UndoCommand, RedoCommand } from 'vs/editor/browser/editorExtensions';
function getFocusedElectronBasedWebviewDelegate(accessor: ServicesAccessor): ElectronWebviewBasedWebview | undefined {
const editorService = accessor.get(IEditorService);
const editor = getActiveNotebookEditor(editorService);
if (!editor?.hasFocus()) {
return;
}
if (!editor?.hasWebviewFocus()) {
return;
}
const webview = editor?.getInnerWebview();
if (webview && webview instanceof ElectronWebviewBasedWebview) {
return webview;
}
return;
}
function withWebview(accessor: ServicesAccessor, f: (webviewe: ElectronWebviewBasedWebview) => void) {
const webview = getFocusedElectronBasedWebviewDelegate(accessor);
if (webview) {
f(webview);
return true;
}
return false;
}
const PRIORITY = 100;
UndoCommand.addImplementation(PRIORITY, accessor => {
return withWebview(accessor, webview => webview.undo());
});
RedoCommand.addImplementation(PRIORITY, accessor => {
return withWebview(accessor, webview => webview.redo());
});
CopyAction?.addImplementation(PRIORITY, accessor => {
return withWebview(accessor, webview => webview.copy());
});
PasteAction?.addImplementation(PRIORITY, accessor => {
return withWebview(accessor, webview => webview.paste());
});
CutAction?.addImplementation(PRIORITY, accessor => {
return withWebview(accessor, webview => webview.cut());
});