Skip to content

Commit

Permalink
Add insertPipe and insertArrow shortcuts for exercise editor (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Jan 15, 2021
1 parent cee0e94 commit 6694842
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ learnr (development version)
* `question_text()` gains `rows` and `cols` parameters. If either is provided, a multi-line `textAreaInput()` is used for the text input. ([#460](https://github.com/rstudio/learnr/pull/460), [#455](https://github.com/rstudio/learnr/issues/455))
* Feedback messages can now be an htmltools tag or tagList, or a character message ([#458](https://github.com/rstudio/learnr/pull/458))
* Added an option to reveal [default] (or hide) the solution to an exercise. Set `exercise.reveal_solution` in the chunk options of a `*-solution` chunk to choose whether or not the solution is revealed to the user. The option can also be set globally with `tutorial_options()`. In a future version of learnr, the default will be changed to hide solutions. ([#402](https://github.com/rstudio/learnr/issue/402))
* Added shortcuts for pipe (`Command/Control+Shift+M`) and assignment (`Alt+-`) operators in exercise code boxes. ([#472](https://github.com/rstudio/learnr/pull/472))

## Bug fixes

Expand Down
22 changes: 22 additions & 0 deletions inst/lib/tutorial/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,28 @@ Tutorial.prototype.$initializeExerciseEditors = function() {
bindExecutionKey("execute1", "Ctrl+Enter");
bindExecutionKey("execute2", "Ctrl+Shift+Enter");

function bindInsertKey(name, keys, text) {
if (typeof keys === 'string') {
keys = {win: keys, mac: keys.replace('Ctrl+', 'Command+')};
}
if (typeof text === 'string') {
text = {r: text, fallback: text};
}
editor.commands.addCommand({
name: name,
bindKey: keys,
exec: function(editor) {
if (text[editor.tutorial.engine]) {
editor.insert(text[editor.tutorial.engine]);
} else if (text.fallback) {
editor.insert(text.fallback);
}
}
});
}
bindInsertKey('insertPipe', 'Ctrl+Shift+M', {r: ' %>% '});
bindInsertKey('insertArrow', 'Alt+-', {r: ' <- ', fallback: ' = '});

// re-focus the editor on run button click
run_button.on('click', function() {
editor.focus();
Expand Down

0 comments on commit 6694842

Please sign in to comment.