Skip to content

Commit d8f5bc3

Browse files
gokatzGaurav0
authored andcommitted
adds keyboard shortcuts for run now and save (#628)
* adds keyboard shortcuts for `run now` and `save` * adds deps for unit test * using e-keyboard test helper * minor tweaks
1 parent 0e36028 commit d8f5bc3

File tree

13 files changed

+82
-16
lines changed

13 files changed

+82
-16
lines changed

app/components/main-gist.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import ColumnsMixin from "../mixins/columns";
44
import FilesMixin from "../mixins/files";
55
import TestFilesMixin from "../mixins/test-files";
66
import AppBuilderMixin from "../mixins/app-builder";
7+
import { keyDown, EKMixin } from 'ember-keyboard';
78

8-
const { inject, computed, run } = Ember;
9+
const { inject, computed, run, on } = Ember;
910

10-
export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin, TestFilesMixin, {
11+
export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin, TestFilesMixin, EKMixin, {
1112
emberCli: inject.service(),
1213
dependencyResolver: inject.service(),
1314
notify: inject.service(),
@@ -24,9 +25,24 @@ export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin,
2425
isFastBoot: this.get('fastboot.isFastBoot')
2526
}));
2627
this.createColumns();
27-
this.set('activeEditorCol', '1');
28+
this.setProperties({
29+
activeEditorCol: '1',
30+
keyboardActivated: true
31+
});
2832
},
2933

34+
// eslint-disable-next-line ember/no-on-calls-in-components
35+
onReloadCommand: on(keyDown('Enter+cmd'), function () {
36+
this.send('runNow');
37+
}),
38+
39+
// eslint-disable-next-line ember/no-on-calls-in-components
40+
onSaveCommand: on(keyDown('cmd+KeyS'), function (event) {
41+
this.saveGist(this.get('model'));
42+
this.send('runNow');
43+
event.preventDefault();
44+
}),
45+
3046
/**
3147
* Output from the build, sets the `code` attr on the component
3248
* @type {String}

app/routes/gist.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,21 @@ export default Ember.Route.extend({
4949
actions: {
5050
saveGist(gist) {
5151
var newGist = gist.get('isNew');
52+
let controller = this.get('controller');
5253
if (!newGist && gist.get('ownerLogin') !== this.get('session.currentUser.login')) {
5354
this.send('fork', gist);
5455
return;
5556
}
57+
controller.set('isGistSaving', true);
5658
gist.save().then(() => {
5759
this.get('notify').info(`Saved to Gist ${gist.get('id')} on Github`);
5860
this.send('setSaved');
5961
if(newGist) {
6062
gist.set('gistId', gist.get('id'));
6163
this.transitionTo('gist.edit', gist);
6264
}
63-
}).catch((this.catchSaveError.bind(this)));
65+
}).catch((this.catchSaveError.bind(this)))
66+
.finally(() => controller.set('isGistSaving', false));
6467
},
6568

6669
deleteGist(gist) {

app/templates/components/gist-header.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="row toolbar visible-toolbar">
33
<div class="title">
44
{{title-input value=model.description titleChanged=(action this.attrs.titleChanged)}}
5-
{{saved-state-indicator model=model unsaved=unsaved}}
5+
{{saved-state-indicator model=model unsaved=unsaved isGistSaving=isGistSaving}}
66
</div>
77

88
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapsed-menu" aria-expanded="false">

app/templates/components/main-gist.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{gist-header model=model
22
session=session
33
unsaved=unsaved
4+
isGistSaving=isGistSaving
45
activeEditorCol=activeEditorCol
56
activeFile=activeFile
67
isRevision=isRevision

app/templates/components/saved-state-indicator.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{{#if unsaved}}
1+
{{#if isGistSaving}}
2+
<span>Saving...</span>
3+
{{else if unsaved}}
24
<span class="test-unsaved-indicator">Unsaved</span>
35
{{else}}
46
<span>{{model.files.length}} files</span>

app/templates/gist.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
route=route
88
applicationUrl=applicationUrl
99
unsaved=unsaved
10+
isGistSaving=isGistSaving
11+
saveGist=(route-action "saveGist")
1012
isRevision=isRevision
1113
transitionQueryParams=(action "transitionQueryParams")
1214
titleUpdated=(route-action "titleUpdated")

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"ember-export-application-global": "^2.0.0",
7272
"ember-git-version": "~0.1.2",
7373
"ember-inflector": "~2.0.0",
74+
"ember-keyboard": "^3.0.2",
7475
"ember-load": "~0.0.12",
7576
"ember-load-initializers": "~1.0.0",
7677
"ember-lodash": "^4.18.0",

tests/acceptance/code-comment-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ test('checks javascript comment option', async function(assert) {
2121

2222
await runGist(files);
2323

24-
let textboxNode = 'textarea:eq(1)';
25-
textboxNode = '.CodeMirror textarea';
24+
let textboxNode = '.CodeMirror textarea';
2625
await click(textboxNode);
2726

2827
await triggerEvent(textboxNode, 'keydown', {

tests/acceptance/run-now-test.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptan
33

44
moduleForAcceptance('Acceptance | run now');
55

6-
test('Able to reload the Twiddle', function(assert) {
6+
const files = [
7+
{
8+
filename: "application.template.hbs",
9+
content: `{{input value="initial value"}}`
10+
}
11+
];
712

8-
const files = [
9-
{
10-
filename: "application.template.hbs",
11-
content: `{{input value="initial value"}}`
12-
}
13-
];
13+
test('Able to reload the Twiddle', function(assert) {
1414

1515
runGist(files);
1616

@@ -34,3 +34,21 @@ test('Able to reload the Twiddle', function(assert) {
3434
assert.equal(outputPane().find('input').val(), 'initial value');
3535
});
3636
});
37+
38+
test('Reload the Twiddle on command (Cmd+Enter)', async(assert) => {
39+
40+
runGist(files);
41+
42+
await click("#live-reload");
43+
assert.equal(outputPane().find('input').val(), 'initial value');
44+
45+
await outputPane().find('input').val('new value');
46+
assert.equal(outputPane().find('input').val(), 'new value');
47+
48+
await keyDown('Enter+cmd'); // eslint-disable-line no-undef
49+
50+
await waitForUnloadedIFrame();
51+
await waitForLoadedIFrame();
52+
53+
assert.equal(outputPane().find('input').val(), 'initial value');
54+
});

tests/acceptance/save-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,18 @@ test('can save a gist without an id', function(assert) {
7575
assert.equal(find('.test-unsaved-indicator').length, 0, "No unsaved indicator shown");
7676
});
7777
});
78+
79+
test('gist save on cmd+s shortcut', async function(assert) {
80+
// set owner of gist as currently logged in user
81+
stubValidSession(this.application, {
82+
currentUser: { login: "gokatz" },
83+
"github-oauth2": {}
84+
});
85+
86+
await visit('/');
87+
assert.equal(find('.gist-link').length, 0, "No gist link is displayed for unsaved twiddle");
88+
89+
await keyDown('cmd+KeyS'); // eslint-disable-line no-undef
90+
91+
assert.equal(find('.gist-link').length, 1, "Gist link is shown for saved twiddle");
92+
});

0 commit comments

Comments
 (0)