From c9f9d969b0a5eb8954ed0c1d080eefb6ebbb1b2b Mon Sep 17 00:00:00 2001 From: Rahman Usta Date: Thu, 18 Dec 2014 14:42:35 +0200 Subject: [PATCH] Make keyboard shortcuts configurable with shortcuts.yml --- conf/shortcuts.yml | 2 +- src/main/java/com/kodcu/bean/ShortCuts.java | 6 +-- .../kodcu/controller/AsciiDocController.java | 25 +++++++---- .../resources/public/js/editor-shortcuts.js | 44 +++++++------------ 4 files changed, 37 insertions(+), 40 deletions(-) diff --git a/conf/shortcuts.yml b/conf/shortcuts.yml index b29ecd076..175098ee0 100644 --- a/conf/shortcuts.yml +++ b/conf/shortcuts.yml @@ -1,3 +1,3 @@ !ShortCuts keys: -- 'AltGR-0+}' \ No newline at end of file + Ctrl-Alt-0: '}' \ No newline at end of file diff --git a/src/main/java/com/kodcu/bean/ShortCuts.java b/src/main/java/com/kodcu/bean/ShortCuts.java index 834b0b132..274e27edc 100644 --- a/src/main/java/com/kodcu/bean/ShortCuts.java +++ b/src/main/java/com/kodcu/bean/ShortCuts.java @@ -10,13 +10,13 @@ */ public class ShortCuts { - private String[] keys; + private Map keys; - public String[] getKeys() { + public Map getKeys() { return keys; } - public void setKeys(String[] keys) { + public void setKeys(Map keys) { this.keys = keys; } } diff --git a/src/main/java/com/kodcu/controller/AsciiDocController.java b/src/main/java/com/kodcu/controller/AsciiDocController.java index d5d088d4d..5eba2ca32 100644 --- a/src/main/java/com/kodcu/controller/AsciiDocController.java +++ b/src/main/java/com/kodcu/controller/AsciiDocController.java @@ -181,7 +181,6 @@ public class AsciiDocController extends TextWebSocketHandler implements Initiali private Optional workingDirectory = Optional.of(Paths.get(System.getProperty("user.home"))); private Optional initialDirectory = Optional.empty(); private List> closedPaths = new ArrayList<>(); - private String[] shortCuts; private List bookNames = Arrays.asList("book.asc", "book.txt", "book.asciidoc", "book.adoc", "book.ad"); @@ -198,6 +197,7 @@ public class AsciiDocController extends TextWebSocketHandler implements Initiali return Objects.nonNull(file) ? file.toPath() : null; }; + private Map shortCuts; private DirectoryChooser newDirectoryChooser(String title) { DirectoryChooser directoryChooser = new DirectoryChooser(); @@ -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); }); // @@ -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); }); @@ -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 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)) @@ -1499,7 +1511,4 @@ public TabPane getTabPane() { return tabPane; } - public String[] getShortCuts() { - return shortCuts; - } } diff --git a/src/main/resources/public/js/editor-shortcuts.js b/src/main/resources/public/js/editor-shortcuts.js index 11a99e498..782842879 100644 --- a/src/main/resources/public/js/editor-shortcuts.js +++ b/src/main/resources/public/js/editor-shortcuts.js @@ -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) { @@ -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