-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add shortcut and cursor listeners #16
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,67 +23,113 @@ | |
#' of every keystroke as it happens. | ||
#' @param wordWrap If set to \code{TRUE}, Ace will enable word wrapping. | ||
#' Default value is \code{FALSE}. | ||
#' @param cursorId The ID associated with a cursor change. | ||
#' @param selectionId The ID associated with a change of selected text | ||
#' @param keyId A list whose names are ID names and whose elements are the shortcuts of keys. Shortcuts can either be a simple string or a list with elements 'win' and 'mac' that that specifies different shortcuts for win and mac (see example). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure
I personally like hotkeys best, but I'm not sure if that's internationally understood. What do you think? I'm open to alternatives. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, can you provide a reference to the syntax of these keyboard shortcuts in the @param documentation? I assume it's a jQuery convention? |
||
#' @import shiny | ||
#' @examples \dontrun{ | ||
#' aceEditor("myEditor", "Initial text for editor here", mode="r", | ||
#' theme="ambiance") | ||
#' | ||
#' aceEditor("myCodeEditor", "# Enter code", mode="r", | ||
#' keyId = list(helpKey="F1", | ||
#' runKey=list(win="Ctrl-R|Ctrl-Shift-Enter", | ||
#' mac="CMD-ENTER|CMD-SHIFT-ENTER") | ||
#' ), | ||
#' wordWrap=TRUE, debounce=10) | ||
#' } | ||
#' @author Jeff Allen \email{jeff@@trestletech.com} | ||
#' @export | ||
aceEditor <- function(outputId, value, mode, theme, vimKeyBinding = FALSE, | ||
readOnly=FALSE, height="400px", | ||
fontSize=12, debounce=1000, wordWrap=FALSE, selectionId=NULL){ | ||
js <- paste("var editor = ace.edit('",outputId,"');",sep="") | ||
fontSize=12, debounce=1000, wordWrap=FALSE,showLineNumbers = TRUE,highlightActiveLine=TRUE, selectionId=NULL, cursorId=NULL, keyId=NULL){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicky, but can I try to keep lines to < 80 chars wide. Can we wrap this one? |
||
editorVar = paste0("editor__",outputId) | ||
#restore.point("aceEditor") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extraneous comments can be trimmed. |
||
#editorVar = "editor" | ||
#editorIdVar = paste0("$('#", outputId, "')") | ||
js <- paste("var ", editorVar," = ace.edit('",outputId,"');",sep="") | ||
if (!missing(theme)){ | ||
js <- paste(js, "editor.setTheme('ace/theme/",theme,"');",sep="") | ||
js <- paste(js, "", editorVar,".setTheme('ace/theme/",theme,"');",sep="") | ||
} | ||
if (vimKeyBinding){ | ||
js <- paste(js, "editor.setKeyboardHandler('ace/keyboard/vim');",sep="") | ||
js <- paste(js, "", editorVar,".setKeyboardHandler('ace/keyboard/vim');",sep="") | ||
} | ||
if (!missing(mode)){ | ||
js <- paste(js, "editor.getSession().setMode('ace/mode/",mode,"');", sep="") | ||
js <- paste(js, "", editorVar,".getSession().setMode('ace/mode/",mode,"');", sep="") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this block still be in there? |
||
} | ||
if (!missing(value)){ | ||
js <- paste(js, "editor.setValue(", jsQuote(value), ", -1);", sep="") | ||
js <- paste(js, "", editorVar,".setValue(", jsQuote(value), ", -1);", sep="") | ||
} | ||
if (!showLineNumbers) { | ||
js <- paste(js, "", editorVar,".renderer.setShowGutter(false);", sep="") | ||
} | ||
if (!highlightActiveLine) { | ||
js <- paste(js, "", editorVar,".setHighlightActiveLine(false);", sep="") | ||
} | ||
|
||
if (readOnly){ | ||
js <- paste(js, "editor.setReadOnly(", jsQuote(readOnly), ");", sep="") | ||
js <- paste(js, "", editorVar,".setReadOnly(", jsQuote(readOnly), ");", sep="") | ||
} | ||
if (!is.null(fontSize) && !is.na(as.numeric(fontSize))){ | ||
js <- paste(js, "document.getElementById('",outputId,"').style.fontSize='", | ||
as.numeric(fontSize), "px'; ", sep="") | ||
} | ||
|
||
if (!is.null(debounce) && !is.na(as.numeric(debounce))){ | ||
# I certainly hope there's a more reasonable way to compare versions with an | ||
# extra field in them... | ||
re <- regexpr("^\\d+\\.\\d+\\.\\d+", packageVersion("shiny")) | ||
shinyVer <- substr(packageVersion("shiny"), 0, attr(re, "match.length")) | ||
|
||
minorVer <- as.integer(substr(packageVersion("shiny"), | ||
attr(re, "match.length")+2, | ||
nchar(packageVersion("shiny")))) | ||
comp <- compareVersion(shinyVer, "0.9.1") | ||
if (comp < 0 || (comp == 0 && minorVer < 9004)){ | ||
warning( | ||
"Shiny version 0.9.1.9004 required to use input debouncing in shinyAce.") | ||
} | ||
js <- paste(js, "$('#",outputId,"').data('debounce',",debounce,");", sep="") | ||
} | ||
|
||
if (wordWrap){ | ||
js <- paste(js, "editor.getSession().setUseWrapMode(true);", sep="") | ||
js <- paste(js, "", editorVar,".getSession().setUseWrapMode(true);", sep="") | ||
} | ||
|
||
js <- paste(js, "$('#", outputId, "').data('aceEditor',editor);", sep="") | ||
js <- paste(js, "$('#", outputId, "').data('aceEditor',", editorVar,");", sep="") | ||
|
||
if (!is.null(selectionId)){ | ||
selectJS <- paste("editor.getSelection().on(\"changeSelection\", function(){ | ||
selectJS <- paste("", editorVar,".getSelection().on(\"changeSelection\", function(){ | ||
Shiny.onInputChange(\"",selectionId, | ||
"\",editor.getCopyText());})", | ||
"\",", editorVar,".getCopyText());})", | ||
sep="") | ||
js <- paste(js, selectJS, sep="") | ||
} | ||
|
||
if (!is.null(cursorId)){ | ||
curJS <- paste("\n", editorVar,".getSelection().on(\"changeCursor\", function(){ | ||
Shiny.onInputChange(\"",cursorId, | ||
"\",", editorVar,".selection.getCursor() );}\n);", | ||
sep="") | ||
js <- paste(js, curJS, sep="") | ||
} | ||
|
||
for (i in seq_along(keyId)) { | ||
shortcut = keyId[[i]] | ||
if (is.list(shortcut)) { | ||
shortcut = paste0(names(shortcut),": '", shortcut,"'", collapse=", ") | ||
} else { | ||
shortcut = paste0("win: '",shortcut,"', mac: '",shortcut,"'") | ||
} | ||
|
||
id = names(keyId)[i] | ||
code = paste0(" | ||
",editorVar,".commands.addCommand({ | ||
name: '",id,"', | ||
bindKey: {", shortcut,"}, | ||
exec: function(",editorVar,") { | ||
Shiny.onInputChange(\"",id, | ||
"\",{ | ||
editorId : '",outputId,"', | ||
selection: ", editorVar,".session.getTextRange(",editorVar,".getSelectionRange()), | ||
cursor : ", editorVar,".selection.getCursor(), | ||
randNum : Math.random() | ||
}); | ||
}, | ||
readOnly: true // false if this command should not apply in readOnly mode | ||
}); | ||
") | ||
js = paste0(js, code) | ||
} | ||
|
||
|
||
|
||
tagList( | ||
singleton(tags$head( | ||
initResourcePaths(), | ||
|
@@ -101,4 +147,3 @@ aceEditor <- function(outputId, value, mode, theme, vimKeyBinding = FALSE, | |
tags$script(type="text/javascript", HTML(js)) | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ LaTeX: pdfLaTeX | |
|
||
BuildType: Package | ||
PackageInstallArgs: --no-multiarch --with-keep.source | ||
PackageRoxygenize: rd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know that CRAN would like this here. Can you remove that line and either add a contributors section to the README or I can after we merge?