Skip to content

Commit

Permalink
adds keyboard shortcuts for run now and save (#628)
Browse files Browse the repository at this point in the history
* adds keyboard shortcuts for `run now` and `save`

* adds deps for unit test

* using e-keyboard test helper

* minor tweaks
  • Loading branch information
gokatz authored and Gaurav0 committed Jul 1, 2018
1 parent 0e36028 commit d8f5bc3
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 16 deletions.
22 changes: 19 additions & 3 deletions app/components/main-gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import ColumnsMixin from "../mixins/columns";
import FilesMixin from "../mixins/files";
import TestFilesMixin from "../mixins/test-files";
import AppBuilderMixin from "../mixins/app-builder";
import { keyDown, EKMixin } from 'ember-keyboard';

const { inject, computed, run } = Ember;
const { inject, computed, run, on } = Ember;

export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin, TestFilesMixin, {
export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin, TestFilesMixin, EKMixin, {
emberCli: inject.service(),
dependencyResolver: inject.service(),
notify: inject.service(),
Expand All @@ -24,9 +25,24 @@ export default Ember.Component.extend(AppBuilderMixin, ColumnsMixin, FilesMixin,
isFastBoot: this.get('fastboot.isFastBoot')
}));
this.createColumns();
this.set('activeEditorCol', '1');
this.setProperties({
activeEditorCol: '1',
keyboardActivated: true
});
},

// eslint-disable-next-line ember/no-on-calls-in-components
onReloadCommand: on(keyDown('Enter+cmd'), function () {
this.send('runNow');
}),

// eslint-disable-next-line ember/no-on-calls-in-components
onSaveCommand: on(keyDown('cmd+KeyS'), function (event) {
this.saveGist(this.get('model'));
this.send('runNow');
event.preventDefault();
}),

/**
* Output from the build, sets the `code` attr on the component
* @type {String}
Expand Down
5 changes: 4 additions & 1 deletion app/routes/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ export default Ember.Route.extend({
actions: {
saveGist(gist) {
var newGist = gist.get('isNew');
let controller = this.get('controller');
if (!newGist && gist.get('ownerLogin') !== this.get('session.currentUser.login')) {
this.send('fork', gist);
return;
}
controller.set('isGistSaving', true);
gist.save().then(() => {
this.get('notify').info(`Saved to Gist ${gist.get('id')} on Github`);
this.send('setSaved');
if(newGist) {
gist.set('gistId', gist.get('id'));
this.transitionTo('gist.edit', gist);
}
}).catch((this.catchSaveError.bind(this)));
}).catch((this.catchSaveError.bind(this)))
.finally(() => controller.set('isGistSaving', false));
},

deleteGist(gist) {
Expand Down
2 changes: 1 addition & 1 deletion app/templates/components/gist-header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="row toolbar visible-toolbar">
<div class="title">
{{title-input value=model.description titleChanged=(action this.attrs.titleChanged)}}
{{saved-state-indicator model=model unsaved=unsaved}}
{{saved-state-indicator model=model unsaved=unsaved isGistSaving=isGistSaving}}
</div>

<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#collapsed-menu" aria-expanded="false">
Expand Down
1 change: 1 addition & 0 deletions app/templates/components/main-gist.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{gist-header model=model
session=session
unsaved=unsaved
isGistSaving=isGistSaving
activeEditorCol=activeEditorCol
activeFile=activeFile
isRevision=isRevision
Expand Down
4 changes: 3 additions & 1 deletion app/templates/components/saved-state-indicator.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{#if unsaved}}
{{#if isGistSaving}}
<span>Saving...</span>
{{else if unsaved}}
<span class="test-unsaved-indicator">Unsaved</span>
{{else}}
<span>{{model.files.length}} files</span>
Expand Down
2 changes: 2 additions & 0 deletions app/templates/gist.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
route=route
applicationUrl=applicationUrl
unsaved=unsaved
isGistSaving=isGistSaving
saveGist=(route-action "saveGist")
isRevision=isRevision
transitionQueryParams=(action "transitionQueryParams")
titleUpdated=(route-action "titleUpdated")
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"ember-export-application-global": "^2.0.0",
"ember-git-version": "~0.1.2",
"ember-inflector": "~2.0.0",
"ember-keyboard": "^3.0.2",
"ember-load": "~0.0.12",
"ember-load-initializers": "~1.0.0",
"ember-lodash": "^4.18.0",
Expand Down
3 changes: 1 addition & 2 deletions tests/acceptance/code-comment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ test('checks javascript comment option', async function(assert) {

await runGist(files);

let textboxNode = 'textarea:eq(1)';
textboxNode = '.CodeMirror textarea';
let textboxNode = '.CodeMirror textarea';
await click(textboxNode);

await triggerEvent(textboxNode, 'keydown', {
Expand Down
32 changes: 25 additions & 7 deletions tests/acceptance/run-now-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptan

moduleForAcceptance('Acceptance | run now');

test('Able to reload the Twiddle', function(assert) {
const files = [
{
filename: "application.template.hbs",
content: `{{input value="initial value"}}`
}
];

const files = [
{
filename: "application.template.hbs",
content: `{{input value="initial value"}}`
}
];
test('Able to reload the Twiddle', function(assert) {

runGist(files);

Expand All @@ -34,3 +34,21 @@ test('Able to reload the Twiddle', function(assert) {
assert.equal(outputPane().find('input').val(), 'initial value');
});
});

test('Reload the Twiddle on command (Cmd+Enter)', async(assert) => {

runGist(files);

await click("#live-reload");
assert.equal(outputPane().find('input').val(), 'initial value');

await outputPane().find('input').val('new value');
assert.equal(outputPane().find('input').val(), 'new value');

await keyDown('Enter+cmd'); // eslint-disable-line no-undef

await waitForUnloadedIFrame();
await waitForLoadedIFrame();

assert.equal(outputPane().find('input').val(), 'initial value');
});
15 changes: 15 additions & 0 deletions tests/acceptance/save-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,18 @@ test('can save a gist without an id', function(assert) {
assert.equal(find('.test-unsaved-indicator').length, 0, "No unsaved indicator shown");
});
});

test('gist save on cmd+s shortcut', async function(assert) {
// set owner of gist as currently logged in user
stubValidSession(this.application, {
currentUser: { login: "gokatz" },
"github-oauth2": {}
});

await visit('/');
assert.equal(find('.gist-link').length, 0, "No gist link is displayed for unsaved twiddle");

await keyDown('cmd+KeyS'); // eslint-disable-line no-undef

assert.equal(find('.gist-link').length, 1, "Gist link is shown for saved twiddle");
});
2 changes: 2 additions & 0 deletions tests/helpers/start-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Application from '../../app';
import config from '../../config/environment';
import { run } from '@ember/runloop';
import { assign } from '@ember/polyfills';
import keyboardRegisterTestHelpers from './ember-keyboard/register-test-helpers';

export default function startApp(attrs) {
let application;
Expand All @@ -12,6 +13,7 @@ export default function startApp(attrs) {
run(() => {
application = Application.create(attributes);
application.setupForTesting();
keyboardRegisterTestHelpers();
application.injectTestHelpers();
});

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/components/main-gist-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ moduleForComponent('main-gist', 'Unit | Component | main gist', {
'service:ember-cli',
'service:dependency-resolver',
'service:notify',
'service:fastboot'
'service:fastboot',
'service:keyboard'
]
});

Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3771,6 +3771,12 @@ ember-inflector@~2.0.0:
dependencies:
ember-cli-babel "^6.0.0"

ember-keyboard@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/ember-keyboard/-/ember-keyboard-3.0.2.tgz#839ab5c32a6b3d3ce0b735728c1e1bc362107464"
dependencies:
ember-cli-babel "^6.6.0"

ember-load-initializers@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ember-load-initializers/-/ember-load-initializers-1.0.0.tgz#4919eaf06f6dfeca7e134633d8c05a6c9921e6e7"
Expand Down

0 comments on commit d8f5bc3

Please sign in to comment.