Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/8151.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support `⌘+s` keyboard shortcut for saving `Notebooks`.
4 changes: 3 additions & 1 deletion src/datascience-ui/native-editor/nativeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { nbformat } from '@jupyterlab/coreutils';
import * as fastDeepEqual from 'fast-deep-equal';
import * as React from 'react';

import { OSType } from '../../client/common/utils/platform';
import { concatMultilineStringInput } from '../../client/datascience/common';
import { Identifiers } from '../../client/datascience/constants';
import { NativeCommandType } from '../../client/datascience/interactive-common/interactiveWindowTypes';
Expand All @@ -16,6 +17,7 @@ import { CellOutput } from '../interactive-common/cellOutput';
import { ExecutionCount } from '../interactive-common/executionCount';
import { InformationMessages } from '../interactive-common/informationMessages';
import { CursorPos, ICellViewModel, IFont } from '../interactive-common/mainState';
import { getOSType } from '../react-common/constants';
import { IKeyboardEvent } from '../react-common/event';
import { Image, ImageName } from '../react-common/image';
import { ImageButton } from '../react-common/imageButton';
Expand Down Expand Up @@ -250,7 +252,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
}
break;
case 's':
if (e.ctrlKey) {
if (e.ctrlKey || (e.metaKey && getOSType() === OSType.OSX)) {
// This is save, save our cells
this.props.stateController.save();
}
Expand Down
8 changes: 5 additions & 3 deletions src/datascience-ui/native-editor/nativeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import './nativeEditor.less';
import * as React from 'react';

import { noop } from '../../client/common/utils/misc';
import { OSType } from '../../client/common/utils/platform';
import { NativeCommandType } from '../../client/datascience/interactive-common/interactiveWindowTypes';
import { ContentPanel, IContentPanelProps } from '../interactive-common/contentPanel';
import { CursorPos, ICellViewModel, IMainState } from '../interactive-common/mainState';
import { IVariablePanelProps, VariablePanel } from '../interactive-common/variablePanel';
import { getOSType } from '../react-common/constants';
import { ErrorBoundary } from '../react-common/errorBoundary';
import { Image, ImageName } from '../react-common/image';
import { ImageButton } from '../react-common/imageButton';
Expand Down Expand Up @@ -321,14 +323,14 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
switch (event.key) {
// tslint:disable-next-line: no-suspicious-comment
// TODO: How to have this work for when the keyboard shortcuts are changed?
case 's':
if (event.ctrlKey) {
case 's': {
if (event.ctrlKey || (event.metaKey && getOSType() === OSType.OSX)) {
// This is save, save our cells
this.stateController.save();
this.stateController.sendCommand(NativeCommandType.Save, 'keyboard');
}
break;

}
default:
break;
}
Expand Down
14 changes: 14 additions & 0 deletions src/datascience-ui/react-common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { OSType } from '../../client/common/utils/platform';

// Javascript keyCodes
export const KeyCodes = {
LeftArrow: 37,
Expand All @@ -9,3 +11,15 @@ export const KeyCodes = {
End: 35,
Home: 36
};

export function getOSType() {
if (window.navigator.platform.startsWith('Mac')){
return OSType.OSX;
} else if (window.navigator.platform.startsWith('Win')){
return OSType.Windows;
} else if (window.navigator.userAgent.indexOf('Linux') > 0){
return OSType.Linux;
} else {
return OSType.Unknown;
}
}
55 changes: 51 additions & 4 deletions src/test/datascience/nativeEditor.functional.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Disposable, TextDocument, TextEditor, Uri, WindowState } from 'vscode';

import { IApplicationShell, IDocumentManager } from '../../client/common/application/types';
import { IFileSystem } from '../../client/common/platform/types';
import { createDeferred, waitForPromise } from '../../client/common/utils/async';
import { createDeferred } from '../../client/common/utils/async';
import { createTemporaryFile } from '../../client/common/utils/fs';
import { noop } from '../../client/common/utils/misc';
import { Identifiers } from '../../client/datascience/constants';
Expand Down Expand Up @@ -566,12 +566,20 @@ for _ in range(50):
});

suite('Keyboard Shortcuts', () => {
const originalPlatform = window.navigator.platform;
Object.defineProperty(window.navigator, 'platform', ((value: string) => {
return {
get: () => value,
set: (v: string) => value = v
};
})(originalPlatform));
setup(async function() {
(window.navigator as any).platform = originalPlatform;
initIoc();
// tslint:disable-next-line: no-invalid-this
await setupFunction.call(this);
});

teardown(() => (window.navigator as any).platform = originalPlatform);
test('Traverse cells by using ArrowUp and ArrowDown, k and j', async () => {
const keyCodesAndPositions = [
// When we press arrow down in the first cell, then second cell gets selected.
Expand Down Expand Up @@ -960,7 +968,7 @@ for _ in range(50):
}
});

test('Test save using the key \'s\'', async () => {
test('Test save using the key \'ctrl+s\'', async () => {
clickCell(0);

await addCell(wrapper, ioc, 'a=1\na', true);
Expand All @@ -973,10 +981,49 @@ for _ in range(50):

simulateKeyPressOnCell(1, { code: 's', ctrlKey: true });

await waitForPromise(savedPromise.promise, 1_000);
await waitForCondition(() => savedPromise.promise.then(() => true).catch(() => false), 1_000, 'Timedout');

assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving');
});

test('Test save using the key \'cmd+s\' on a Mac', async () => {
(window.navigator as any).platform = 'Mac';

clickCell(0);

await addCell(wrapper, ioc, 'a=1\na', true);

const notebookProvider = ioc.get<INotebookEditorProvider>(INotebookEditorProvider);
const editor = notebookProvider.editors[0];
assert.ok(editor, 'No editor when saving');
const savedPromise = createDeferred();
editor.saved(() => savedPromise.resolve());

simulateKeyPressOnCell(1, { code: 's', metaKey: true });

await waitForCondition(() => savedPromise.promise.then(() => true).catch(() => false), 1_000, 'Timedout');

assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving');
});
test('Test save using the key \'cmd+s\' on a Windows', async () => {
(window.navigator as any).platform = 'Win';

clickCell(0);

await addCell(wrapper, ioc, 'a=1\na', true);

const notebookProvider = ioc.get<INotebookEditorProvider>(INotebookEditorProvider);
const editor = notebookProvider.editors[0];
assert.ok(editor, 'No editor when saving');
const savedPromise = createDeferred();
editor.saved(() => savedPromise.resolve());

// CMD+s won't work on Windows.
simulateKeyPressOnCell(1, { code: 's', metaKey: true });

await expect(waitForCondition(() => savedPromise.promise.then(() => true).catch(() => false), 1_000, 'Timedout')).to.eventually.be.rejected;
assert.ok(editor!.isDirty, 'Editor be dirty as nothing got saved');
});
});

suite('Auto Save', () => {
Expand Down