From dfa7c4b219a7f75c9f7eb34a8470b1d6071d58ad Mon Sep 17 00:00:00 2001 From: Rahman Usta Date: Thu, 18 Dec 2014 01:10:27 +0200 Subject: [PATCH] Configurable Shortcut --- conf/shortcuts.yml | 3 ++ src/main/java/com/kodcu/bean/ShortCuts.java | 22 ++++++++++++ .../kodcu/controller/AsciiDocController.java | 19 ++++++++++ .../resources/public/js/editor-shortcuts.js | 36 ++++++++++++++----- 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 conf/shortcuts.yml create mode 100644 src/main/java/com/kodcu/bean/ShortCuts.java diff --git a/conf/shortcuts.yml b/conf/shortcuts.yml new file mode 100644 index 000000000..b29ecd076 --- /dev/null +++ b/conf/shortcuts.yml @@ -0,0 +1,3 @@ +!ShortCuts +keys: +- 'AltGR-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 new file mode 100644 index 000000000..834b0b132 --- /dev/null +++ b/src/main/java/com/kodcu/bean/ShortCuts.java @@ -0,0 +1,22 @@ +package com.kodcu.bean; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by usta on 17.12.2014. + */ +public class ShortCuts { + + private String[] keys; + + public String[] getKeys() { + return keys; + } + + public void setKeys(String[] 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 6023b3474..d5d088d4d 100644 --- a/src/main/java/com/kodcu/controller/AsciiDocController.java +++ b/src/main/java/com/kodcu/controller/AsciiDocController.java @@ -6,6 +6,7 @@ import com.esotericsoftware.yamlbeans.YamlWriter; import com.kodcu.bean.Config; import com.kodcu.bean.RecentFiles; +import com.kodcu.bean.ShortCuts; import com.kodcu.component.MyTab; import com.kodcu.other.Current; import com.kodcu.other.IOHelper; @@ -180,6 +181,7 @@ 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"); @@ -449,6 +451,7 @@ public void initialize(URL url, ResourceBundle rb) { loadConfigurations(); loadRecentFileList(); + loadShortCuts(); recentListView.setItems(recentFiles); recentFiles.addListener((ListChangeListener) c -> { @@ -586,6 +589,18 @@ public void initialize(URL url, ResourceBundle rb) { } + private void loadShortCuts() { + try { + YamlReader yamlReader = + new YamlReader(new FileReader(configPath.resolve("shortcuts.yml").toFile())); + yamlReader.getConfig().setClassTag("ShortCuts", ShortCuts.class); + shortCuts = yamlReader.read(ShortCuts.class).getKeys(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + private void addImageTab(Path imagePath) { MyTab tab = createTab(); Label label = (Label) tab.getGraphic(); @@ -1483,4 +1498,8 @@ public ObservableList getRecentFiles() { 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 947867f44..11a99e498 100644 --- a/src/main/resources/public/js/editor-shortcuts.js +++ b/src/main/resources/public/js/editor-shortcuts.js @@ -63,14 +63,34 @@ 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 -}); +//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 formatText(editor, matcher, firstCharacter, lastCharacter) { var range = editor.getSelectionRange();