From 6fe2dd4bf0ff866ef344cf9acc396bf329366c52 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Fri, 25 Oct 2019 15:49:10 -0700 Subject: [PATCH 1/3] Support cmd+s on macs for native editor --- news/2 Fixes/8151.md | 1 + .../native-editor/nativeCell.tsx | 2 +- .../native-editor/nativeEditor.tsx | 2 +- .../nativeEditor.functional.test.tsx | 20 ++++++++++++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 news/2 Fixes/8151.md diff --git a/news/2 Fixes/8151.md b/news/2 Fixes/8151.md new file mode 100644 index 000000000000..cc56d3b5bb86 --- /dev/null +++ b/news/2 Fixes/8151.md @@ -0,0 +1 @@ +Support `⌘+s` keyboard shortcut for saving `Notebooks`. diff --git a/src/datascience-ui/native-editor/nativeCell.tsx b/src/datascience-ui/native-editor/nativeCell.tsx index 1b6614790219..83e26e384b63 100644 --- a/src/datascience-ui/native-editor/nativeCell.tsx +++ b/src/datascience-ui/native-editor/nativeCell.tsx @@ -250,7 +250,7 @@ export class NativeCell extends React.Component { } break; case 's': - if (e.ctrlKey) { + if (e.ctrlKey || e.metaKey) { // This is save, save our cells this.props.stateController.save(); } diff --git a/src/datascience-ui/native-editor/nativeEditor.tsx b/src/datascience-ui/native-editor/nativeEditor.tsx index 18d1add62cc2..11f35c359b31 100644 --- a/src/datascience-ui/native-editor/nativeEditor.tsx +++ b/src/datascience-ui/native-editor/nativeEditor.tsx @@ -322,7 +322,7 @@ export class NativeEditor extends React.Component { + test('Test save using the key \'ctrl+s\'', async () => { clickCell(0); await addCell(wrapper, ioc, 'a=1\na', true); @@ -977,6 +977,24 @@ for _ in range(50): assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving'); }); + + test('Test save using the key \'cmd+s\'', async () => { + clickCell(0); + + await addCell(wrapper, ioc, 'a=1\na', true); + + const notebookProvider = ioc.get(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 waitForPromise(savedPromise.promise, 1_000); + + assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving'); + }); }); suite('Auto Save', () => { From 6cf5b729774c269d8aa550fc1f7e3db93422f050 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Mon, 28 Oct 2019 10:15:06 -0700 Subject: [PATCH 2/3] Code review fixes --- .../native-editor/nativeCell.tsx | 4 +++- .../native-editor/nativeEditor.tsx | 8 ++++--- src/datascience-ui/react-common/constants.ts | 14 +++++++++++ .../nativeEditor.functional.test.tsx | 23 +++++++++++++++++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/datascience-ui/native-editor/nativeCell.tsx b/src/datascience-ui/native-editor/nativeCell.tsx index 83e26e384b63..c18aa6c57040 100644 --- a/src/datascience-ui/native-editor/nativeCell.tsx +++ b/src/datascience-ui/native-editor/nativeCell.tsx @@ -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'; @@ -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'; @@ -250,7 +252,7 @@ export class NativeCell extends React.Component { } break; case 's': - if (e.ctrlKey || e.metaKey) { + if (e.ctrlKey || (e.metaKey && getOSType() === OSType.OSX)) { // This is save, save our cells this.props.stateController.save(); } diff --git a/src/datascience-ui/native-editor/nativeEditor.tsx b/src/datascience-ui/native-editor/nativeEditor.tsx index 11f35c359b31..d6898aaab5c6 100644 --- a/src/datascience-ui/native-editor/nativeEditor.tsx +++ b/src/datascience-ui/native-editor/nativeEditor.tsx @@ -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'; @@ -321,14 +323,14 @@ export class NativeEditor extends React.Component 0){ + return OSType.Linux; + } else { + return OSType.Unknown; + } +} diff --git a/src/test/datascience/nativeEditor.functional.test.tsx b/src/test/datascience/nativeEditor.functional.test.tsx index d7375d8711b9..1c4044ff81e8 100644 --- a/src/test/datascience/nativeEditor.functional.test.tsx +++ b/src/test/datascience/nativeEditor.functional.test.tsx @@ -571,7 +571,7 @@ for _ in range(50): // tslint:disable-next-line: no-invalid-this await setupFunction.call(this); }); - + teardown(() => sinon.restore()); 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. @@ -978,7 +978,8 @@ for _ in range(50): assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving'); }); - test('Test save using the key \'cmd+s\'', async () => { + test('Test save using the key \'cmd+s\' on a Mac', async () => { + sinon.stub(window, 'navigator').returns({ userAgent: 'Something Mac OS X Something', platform: ''}); clickCell(0); await addCell(wrapper, ioc, 'a=1\na', true); @@ -995,6 +996,24 @@ for _ in range(50): assert.ok(!editor!.isDirty, 'Editor should not be dirty after saving'); }); + test('Test save using the key \'cmd+s\' on a Windows', async () => { + sinon.stub(window, 'navigator').returns({ userAgent: 'Something', platform: 'Win'}); + clickCell(0); + + await addCell(wrapper, ioc, 'a=1\na', true); + + const notebookProvider = ioc.get(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(waitForPromise(savedPromise.promise, 1_000)).to.eventually.be.rejected; + assert.ok(editor!.isDirty, 'Editor be dirty as nothing got saved'); + }); }); suite('Auto Save', () => { From 494f57f218bc5cb19b413b02ef5f4a940f88a2ca Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Mon, 28 Oct 2019 11:32:15 -0700 Subject: [PATCH 3/3] Fixes to tests --- src/datascience-ui/react-common/constants.ts | 2 +- .../nativeEditor.functional.test.tsx | 24 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/datascience-ui/react-common/constants.ts b/src/datascience-ui/react-common/constants.ts index a95bcd5f53ea..e621dbee8ed8 100644 --- a/src/datascience-ui/react-common/constants.ts +++ b/src/datascience-ui/react-common/constants.ts @@ -13,7 +13,7 @@ export const KeyCodes = { }; export function getOSType() { - if (window.navigator.userAgent.startsWith('Mac')){ + if (window.navigator.platform.startsWith('Mac')){ return OSType.OSX; } else if (window.navigator.platform.startsWith('Win')){ return OSType.Windows; diff --git a/src/test/datascience/nativeEditor.functional.test.tsx b/src/test/datascience/nativeEditor.functional.test.tsx index 1c4044ff81e8..196aeb6fb368 100644 --- a/src/test/datascience/nativeEditor.functional.test.tsx +++ b/src/test/datascience/nativeEditor.functional.test.tsx @@ -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'; @@ -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(() => sinon.restore()); + 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. @@ -973,13 +981,14 @@ 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 () => { - sinon.stub(window, 'navigator').returns({ userAgent: 'Something Mac OS X Something', platform: ''}); + (window.navigator as any).platform = 'Mac'; + clickCell(0); await addCell(wrapper, ioc, 'a=1\na', true); @@ -992,12 +1001,13 @@ for _ in range(50): simulateKeyPressOnCell(1, { code: 's', metaKey: 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 Windows', async () => { - sinon.stub(window, 'navigator').returns({ userAgent: 'Something', platform: 'Win'}); + (window.navigator as any).platform = 'Win'; + clickCell(0); await addCell(wrapper, ioc, 'a=1\na', true); @@ -1011,7 +1021,7 @@ for _ in range(50): // CMD+s won't work on Windows. simulateKeyPressOnCell(1, { code: 's', metaKey: true }); - await expect(waitForPromise(savedPromise.promise, 1_000)).to.eventually.be.rejected; + 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'); }); });