Skip to content

Commit

Permalink
Make keyboard shortcuts configurable with shortcuts.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Dec 18, 2014
1 parent dfa7c4b commit c9f9d96
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion conf/shortcuts.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
!ShortCuts
keys:
- 'AltGR-0+}'
Ctrl-Alt-0: '}'
6 changes: 3 additions & 3 deletions src/main/java/com/kodcu/bean/ShortCuts.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*/
public class ShortCuts {

private String[] keys;
private Map<String,String> keys;

public String[] getKeys() {
public Map<String, String> getKeys() {
return keys;
}

public void setKeys(String[] keys) {
public void setKeys(Map<String, String> keys) {
this.keys = keys;
}
}
25 changes: 17 additions & 8 deletions src/main/java/com/kodcu/controller/AsciiDocController.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ public class AsciiDocController extends TextWebSocketHandler implements Initiali
private Optional<Path> workingDirectory = Optional.of(Paths.get(System.getProperty("user.home")));
private Optional<File> initialDirectory = Optional.empty();
private List<Optional<Path>> closedPaths = new ArrayList<>();
private String[] shortCuts;

private List<String> bookNames = Arrays.asList("book.asc", "book.txt", "book.asciidoc", "book.adoc", "book.ad");

Expand All @@ -198,6 +197,7 @@ public class AsciiDocController extends TextWebSocketHandler implements Initiali

return Objects.nonNull(file) ? file.toPath() : null;
};
private Map<String, String> shortCuts;

private DirectoryChooser newDirectoryChooser(String title) {
DirectoryChooser directoryChooser = new DirectoryChooser();
Expand Down Expand Up @@ -488,7 +488,7 @@ public void initialize(URL url, ResourceBundle rb) {
WebEngine mathjaxEngine = mathjaxView.getEngine();
mathjaxEngine.getLoadWorker().stateProperty().addListener((observableValue1, state, state2) -> {
JSObject window = (JSObject) mathjaxEngine.executeScript("window");
if (Objects.isNull(window.getMember("app"))) ;
if (window.getMember("app").equals("undefined"))
window.setMember("app", this);
});
//
Expand All @@ -500,7 +500,7 @@ public void initialize(URL url, ResourceBundle rb) {

previewEngine.getLoadWorker().stateProperty().addListener((observableValue1, state, state2) -> {
JSObject window = (JSObject) previewEngine.executeScript("window");
if (Objects.isNull(window.getMember("app"))) ;
if (window.getMember("app").equals("undefined"))
window.setMember("app", this);
});

Expand Down Expand Up @@ -1108,13 +1108,25 @@ private WebView createWebView() {

webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
JSObject window = (JSObject) webEngine.executeScript("window");
if (Objects.isNull(window.getMember("app"))) ;
window.setMember("app", this);
if (window.getMember("app").equals("undefined"))
window.setMember("app", this);

if(newValue== Worker.State.SUCCEEDED)
applySohrtCuts();

});

webEngine.load(String.format("http://localhost:%d/editor.html", tomcatPort));
return webView;
}

private void applySohrtCuts() {
Set<String> keySet = shortCuts.keySet();
for (String key : keySet) {
current.currentEngine().executeScript(String.format("addNewCommand('%s','%s')",key,shortCuts.get(key)));
}
}


public void onscroll(Object pos, Object max) {
if (Objects.isNull(pos) || Objects.isNull(max))
Expand Down Expand Up @@ -1499,7 +1511,4 @@ public TabPane getTabPane() {
return tabPane;
}

public String[] getShortCuts() {
return shortCuts;
}
}
44 changes: 16 additions & 28 deletions src/main/resources/public/js/editor-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,21 @@ editor.commands.addCommand({
readOnly: true
});

//editor.commands.addCommand({
// name: 'right-brace-1',
// bindKey: {win: 'Ctrl-Alt-0', mac: 'Command-Alt-0'},
// exec: function (editor) {
// editor.insert("}");
// },
// readOnly: true
//});

// loads from shortcuts.yml
var shortCuts = app.getShortCuts();
for (var i = 0; i < shortCuts.length; i++) {
var key = shortCuts[i].toString();
key = key.replace("AltGR", "Ctrl-Alt");
var split = key.split("+");

editor.commands.addCommand({
name: key,
bindKey: {
win: split[0].replace("Command", "Ctrl"),
mac: split[0].replace("Ctrl", "Command")
},
exec: function (editor) {
editor.insert(split[1]);
},
readOnly: true
});
function addNewCommand(key,value){
var name = key + value;
console.log(name);
editor.commands.addCommand({
name: name,
bindKey: {
win: key,
mac: key
},
exec: function (editor) {
console.log(value);
editor.insert(value);
},
readOnly: true
});
}

function formatText(editor, matcher, firstCharacter, lastCharacter) {
Expand Down Expand Up @@ -234,7 +222,7 @@ editor.commands.addCommand({
name: 'codify-selected',
bindKey: {win: 'Ctrl-Shift-C', mac: 'Command-Shift-C'},
exec: function (editor) {

console.log("in old");
formatText(editor, matchCode, "`", "`");
},
readOnly: true
Expand Down

0 comments on commit c9f9d96

Please sign in to comment.