Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added KeyBindings #103

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 119 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,126 @@ function selectionChanger(selection,operator,endoperator){
}

editor.addKeyMap({
// bold
'Ctrl-B': function(cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(),'**'));
},
// italic
'Ctrl-I': function(cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(),'_'));
},
// code
'Ctrl-K': function(cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(),'`'));
},
// keyboard shortcut
'Ctrl-L': function(cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(),'<kbd>','</kbd>'));
// bold
'Ctrl-B': function(cm) {
var selection = cm.getSelection();
cm.replaceSelection('**' + selection + '**');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change takes away the existing functionality where you could use CTRL+B to toggle bold. This would always add more text. For example, pressing the hotkey twice on 'hello' would result in:

=> hello
=> **hello**
=> ****hello****

Arguably, the first approach wasn't foolproof either, but this implementation is immediately broken.

if (!selection) {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line, cursorPos.ch - 2);
}
},
// italic
'Ctrl-I': function(cm) {
var selection = cm.getSelection();
cm.replaceSelection('_' + selection + '_');
if (!selection) {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line, cursorPos.ch - 1);
}
},
// code
'Ctrl-K': function(cm) {
var selection = cm.getSelection();
cm.replaceSelection('`' + selection + '`');
if (!selection) {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line, cursorPos.ch - 1);
}
},
// keyboard shortcut
'Ctrl-L': function(cm) {
cm.replaceSelection(selectionChanger(cm.getSelection(), '<kbd>', '</kbd>'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your changes appear to change the code to be indented with 2 characters instead of 4, which is the current convention of the file. I'd appreciate if you could make sure the file remains consistent.

},
//Heading 1
'Ctrl-Alt-1': function(cm) {
cm.replaceSelection('# ' + cm.getSelection());
},
//Heading 2
'Ctrl-Alt-2': function(cm) {
cm.replaceSelection('## ' + cm.getSelection());
},
//Heading 3
'Ctrl-Alt-3': function(cm) {
cm.replaceSelection('### ' + cm.getSelection());
},
//Heading 4
'Ctrl-Alt-4': function(cm) {
cm.replaceSelection('#### ' + cm.getSelection());
},
//Heading 5
'Ctrl-Alt-5': function(cm) {
cm.replaceSelection('##### ' + cm.getSelection());
},
//Heading 6
'Ctrl-Alt-6': function(cm) {
cm.replaceSelection('###### ' + cm.getSelection());
},
// Links
'Shift-Ctrl-L': function(cm) {
var selection = cm.getSelection();
var text = '';
var link = '';

if (selection.match(/^https?:\/\//)) {
link = selection;
} else {
text = selection;
}
cm.replaceSelection('[' + text + '](' + link + ')');

var cursorPos = cm.getCursor();
if (!selection) {
cm.setCursor(cursorPos.line, cursorPos.ch - 3);
} else if (link) {
cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length));
} else {
cm.setCursor(cursorPos.line, cursorPos.ch - 1);
}
},
// Insert Image
'Shift-Ctrl-I': function(cm) {
var selection = cm.getSelection();
var text = '';
var link = '';

if (selection.match(/^https?:\/\//)) {
link = selection;
} else {
text = selection;
}
cm.replaceSelection('![' + text + '](' + link + ')');

var cursorPos = cm.getCursor();
if (!selection) {
cm.setCursor(cursorPos.line, cursorPos.ch - 3);
} else if (link) {
cm.setCursor(cursorPos.line, cursorPos.ch - (3 + link.length));
} else {
cm.setCursor(cursorPos.line, cursorPos.ch - 1);
}
},
//Unordered list
'Shift-U': function(cm) {
cm.replaceSelection('* ' + cm.getSelection());
},
//Ordered list
'Shift-O': function(cm) {
cm.replaceSelection('1. ' + cm.getSelection());
},
//Blockquote
'Shift-Ctrl-.': function(cm) {
cm.replaceSelection('> ' + cm.getSelection());
},
//codeblock
"Shift-Ctrl-'": function(cm) {
var selection = cm.getSelection();
cm.replaceSelection('```javascript' + '\n' + selection+'\n' +'```');
if (!selection) {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line -1, cursorPos.ch);
}
},
});

document.addEventListener('drop', function(e) {
Expand Down