Skip to content

Commit

Permalink
Adds keyboard shortcut option to comment javascript code. (#622)
Browse files Browse the repository at this point in the history
* adds shortcut comment option for javascript code

* Adds test

* change from `keypress` to `keydown` to trigger events

* comment logic moved to initializer

* fix test server shutdown
  • Loading branch information
gokatz authored and Gaurav0 committed Jun 22, 2018
1 parent 1f0c682 commit 0e36028
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
visit: true,
waitForLoadedIFrame: true,
waitForUnloadedIFrame: true,
'$': true
'$': true,
CodeMirror: false
},
rules: {
'ember/new-module-imports': 'off'
Expand Down
11 changes: 11 additions & 0 deletions app/initializers/app-initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function initialize(/* application */) {
// adds "toggleCommentIndented" command to codemirror for default keymap

if (CodeMirror) {
CodeMirror.keyMap.default['Cmd-/'] = 'toggleCommentIndented';
}
}

export default {
initialize
};
2 changes: 1 addition & 1 deletion app/templates/components/editor-mode-menu.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Keystroke Mode <b class="caret"></b>
Keystroke - {{selectedKeyMap}} <b class="caret"></b>
</a>

<ul class="dropdown-menu dropdown-menu-right">
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 @@ -37,7 +37,7 @@
downloadProject=(route-action "downloadProject")
}}

{{editor-mode-menu setKeyMap=(action this.attrs.setEditorKeyMap)}}
{{editor-mode-menu selectedKeyMap=selectedKeyMap setKeyMap=(action this.attrs.setEditorKeyMap)}}

{{versions-menu versionSelected=(action this.attrs.versionSelected)
emberVersions=emberVersions
Expand Down
1 change: 1 addition & 0 deletions app/templates/components/main-gist.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
activeEditorCol=activeEditorCol
activeFile=activeFile
isRevision=isRevision
selectedKeyMap=settings.keyMap
titleChanged=(action "titleChanged")
addFile=(action "addFile")
addHelper=(action "addHelper")
Expand Down
3 changes: 2 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module.exports = function(defaults) {
},
codemirror: {
modes: ['xml', 'javascript', 'handlebars', 'htmlmixed', 'css'],
keyMaps: ['emacs', 'sublime', 'vim']
keyMaps: ['emacs', 'sublime', 'vim'],
addonFiles: ['comment/comment.js']
},
'ember-cli-bootstrap-sassy': {
'js': ['dropdown', 'collapse']
Expand Down
53 changes: 53 additions & 0 deletions tests/acceptance/code-comment-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { test } from 'qunit';
import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptance';

moduleForAcceptance('Acceptance | code comment');

test('checks javascript comment option', async function(assert) {
const files = [
{
filename: "application.controller.js",
content: `import Ember from "ember";
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});`
},
{
filename: "application.template.hbs",
content: "Welcome to {{appName}}"
}
];


await runGist(files);

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

await triggerEvent(textboxNode, 'keydown', {
keyCode: 65, // 'A'
metaKey: true
});

await triggerEvent(textboxNode, 'keydown', {
keyCode: 191, // '/'
metaKey: true
});

let [firstLine] = find('.CodeMirror-line');
let content = firstLine.textContent;

assert.ok(content.startsWith('//'), 'Line has been commented');

await triggerEvent(textboxNode, 'keydown', {
keyCode: 191, // '/'
metaKey: true
});

[firstLine] = find('.CodeMirror-line');
content = firstLine.textContent;

assert.notOk(content.startsWith('//'), 'Line has been uncommented');

});
2 changes: 1 addition & 1 deletion tests/helpers/destroy-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { run } from '@ember/runloop';
export default function destroyApp(application) {
run(() => {
application.destroy();
window.server.shutdown();
window.server && window.server.shutdown();
});
}
26 changes: 26 additions & 0 deletions tests/unit/initializers/app-initializer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Application from '@ember/application';
import { run } from '@ember/runloop';

import { initialize } from 'ember-twiddle/initializers/app-initializer';
import { module, test } from 'qunit';
import destroyApp from '../../helpers/destroy-app';

module('Unit | Initializer | app initializer', {
beforeEach() {
run(() => {
this.application = Application.create();
this.application.deferReadiness();
});
},
afterEach() {
destroyApp(this.application);
}
});

// Replace this with your real tests.
test('it works', function(assert) {
initialize(this.application);

// you would normally confirm the results of the initializer here
assert.ok(true);
});

0 comments on commit 0e36028

Please sign in to comment.