Skip to content

Commit 0e36028

Browse files
gokatzGaurav0
authored andcommitted
Adds keyboard shortcut option to comment javascript code. (#622)
* 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
1 parent 1f0c682 commit 0e36028

File tree

9 files changed

+98
-5
lines changed

9 files changed

+98
-5
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ module.exports = {
3131
visit: true,
3232
waitForLoadedIFrame: true,
3333
waitForUnloadedIFrame: true,
34-
'$': true
34+
'$': true,
35+
CodeMirror: false
3536
},
3637
rules: {
3738
'ember/new-module-imports': 'off'

app/initializers/app-initializer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function initialize(/* application */) {
2+
// adds "toggleCommentIndented" command to codemirror for default keymap
3+
4+
if (CodeMirror) {
5+
CodeMirror.keyMap.default['Cmd-/'] = 'toggleCommentIndented';
6+
}
7+
}
8+
9+
export default {
10+
initialize
11+
};

app/templates/components/editor-mode-menu.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
2-
Keystroke Mode <b class="caret"></b>
2+
Keystroke - {{selectedKeyMap}} <b class="caret"></b>
33
</a>
44

55
<ul class="dropdown-menu dropdown-menu-right">

app/templates/components/gist-header.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
downloadProject=(route-action "downloadProject")
3838
}}
3939

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

4242
{{versions-menu versionSelected=(action this.attrs.versionSelected)
4343
emberVersions=emberVersions

app/templates/components/main-gist.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
activeEditorCol=activeEditorCol
55
activeFile=activeFile
66
isRevision=isRevision
7+
selectedKeyMap=settings.keyMap
78
titleChanged=(action "titleChanged")
89
addFile=(action "addFile")
910
addHelper=(action "addHelper")

ember-cli-build.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = function(defaults) {
4040
},
4141
codemirror: {
4242
modes: ['xml', 'javascript', 'handlebars', 'htmlmixed', 'css'],
43-
keyMaps: ['emacs', 'sublime', 'vim']
43+
keyMaps: ['emacs', 'sublime', 'vim'],
44+
addonFiles: ['comment/comment.js']
4445
},
4546
'ember-cli-bootstrap-sassy': {
4647
'js': ['dropdown', 'collapse']

tests/acceptance/code-comment-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptance';
3+
4+
moduleForAcceptance('Acceptance | code comment');
5+
6+
test('checks javascript comment option', async function(assert) {
7+
const files = [
8+
{
9+
filename: "application.controller.js",
10+
content: `import Ember from "ember";
11+
export default Ember.Controller.extend({
12+
appName: 'Ember Twiddle'
13+
});`
14+
},
15+
{
16+
filename: "application.template.hbs",
17+
content: "Welcome to {{appName}}"
18+
}
19+
];
20+
21+
22+
await runGist(files);
23+
24+
let textboxNode = 'textarea:eq(1)';
25+
textboxNode = '.CodeMirror textarea';
26+
await click(textboxNode);
27+
28+
await triggerEvent(textboxNode, 'keydown', {
29+
keyCode: 65, // 'A'
30+
metaKey: true
31+
});
32+
33+
await triggerEvent(textboxNode, 'keydown', {
34+
keyCode: 191, // '/'
35+
metaKey: true
36+
});
37+
38+
let [firstLine] = find('.CodeMirror-line');
39+
let content = firstLine.textContent;
40+
41+
assert.ok(content.startsWith('//'), 'Line has been commented');
42+
43+
await triggerEvent(textboxNode, 'keydown', {
44+
keyCode: 191, // '/'
45+
metaKey: true
46+
});
47+
48+
[firstLine] = find('.CodeMirror-line');
49+
content = firstLine.textContent;
50+
51+
assert.notOk(content.startsWith('//'), 'Line has been uncommented');
52+
53+
});

tests/helpers/destroy-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { run } from '@ember/runloop';
33
export default function destroyApp(application) {
44
run(() => {
55
application.destroy();
6-
window.server.shutdown();
6+
window.server && window.server.shutdown();
77
});
88
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Application from '@ember/application';
2+
import { run } from '@ember/runloop';
3+
4+
import { initialize } from 'ember-twiddle/initializers/app-initializer';
5+
import { module, test } from 'qunit';
6+
import destroyApp from '../../helpers/destroy-app';
7+
8+
module('Unit | Initializer | app initializer', {
9+
beforeEach() {
10+
run(() => {
11+
this.application = Application.create();
12+
this.application.deferReadiness();
13+
});
14+
},
15+
afterEach() {
16+
destroyApp(this.application);
17+
}
18+
});
19+
20+
// Replace this with your real tests.
21+
test('it works', function(assert) {
22+
initialize(this.application);
23+
24+
// you would normally confirm the results of the initializer here
25+
assert.ok(true);
26+
});

0 commit comments

Comments
 (0)