From 8201df7014304fd08a38548f88b36bb78bc6d433 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 23 Dec 2015 13:49:55 +0200 Subject: [PATCH] Add simplier shortcuts implementation for the workspace. --- frontend/uxbox/core.cljs | 2 - frontend/uxbox/ui/keyboard.cljs | 42 +-------------- frontend/uxbox/ui/workspace/shortcuts.cljs | 62 +++++++++++++++++++--- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/frontend/uxbox/core.cljs b/frontend/uxbox/core.cljs index f342b4d69..d8a82c8df 100644 --- a/frontend/uxbox/core.cljs +++ b/frontend/uxbox/core.cljs @@ -4,7 +4,6 @@ [uxbox.router :as rt] [uxbox.rstore :as rs] [uxbox.ui :as ui] - [uxbox.ui.keyboard :as kb] [uxbox.data.load :as dl])) (enable-console-print!) @@ -15,7 +14,6 @@ (rt/init) (ui/init) - (kb/init) (rs/emit! (dl/load-data)) (rx/on-value s/stream #(dl/persist-state %)))) diff --git a/frontend/uxbox/ui/keyboard.cljs b/frontend/uxbox/ui/keyboard.cljs index 400bf9ea2..56491066e 100644 --- a/frontend/uxbox/ui/keyboard.cljs +++ b/frontend/uxbox/ui/keyboard.cljs @@ -1,14 +1,4 @@ -(ns uxbox.ui.keyboard - (:require [goog.events :as events] - [beicon.core :as rx]) - (:import goog.events.EventType - goog.events.KeyCodes - goog.ui.KeyboardShortcutHandler - goog.ui.KeyboardShortcutHandler)) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Public Api -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(ns uxbox.ui.keyboard) (defn is-keycode? [keycode] @@ -17,33 +7,3 @@ (def esc? (is-keycode? 27)) (def enter? (is-keycode? 13)) - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Shortcuts -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -(defonce ^:static +shortcuts+ - #{:ctrl+g - :esc - :ctrl+shift+f - :ctrl+shift+l}) - -(defonce ^:static +handler+ - (KeyboardShortcutHandler. js/document)) - -(defonce ^:static ^:private +bus+ - (rx/bus)) - -(defonce ^:static +stream+ - (rx/to-observable +bus+)) - -(defn init - "Initialize the shortcuts handler." - [] - (doseq [item +shortcuts+] - (let [identifier (name item)] - (.registerShortcut +handler+ identifier identifier))) - (let [event KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED] - (events/listen +handler+ event #(rx/push! +bus+ (keyword (.-identifier %)))))) - -(rx/on-value +stream+ #(println "[debug]: shortcut:" %)) diff --git a/frontend/uxbox/ui/workspace/shortcuts.cljs b/frontend/uxbox/ui/workspace/shortcuts.cljs index 66b67a495..165087d06 100644 --- a/frontend/uxbox/ui/workspace/shortcuts.cljs +++ b/frontend/uxbox/ui/workspace/shortcuts.cljs @@ -1,14 +1,54 @@ (ns uxbox.ui.workspace.shortcuts - (:require [beicon.core :as rx] + (:require [goog.events :as events] + [beicon.core :as rx] [uxbox.rstore :as rs] - [uxbox.ui.keyboard :as kbd] - [uxbox.data.workspace :as dw])) + [uxbox.data.workspace :as dw]) + (:import goog.events.EventType + goog.events.KeyCodes + goog.ui.KeyboardShortcutHandler + goog.ui.KeyboardShortcutHandler)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Keyboard Shortcuts Watcher +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defonce ^:static +shortcuts+ + #{:ctrl+g + :ctrl+shift+f + :ctrl+shift+l}) + +(defonce ^:static ^:private +bus+ + (rx/bus)) + +(defonce ^:static +stream+ + (rx/to-observable +bus+)) + +(defn- init-handler + [] + (let [handler (KeyboardShortcutHandler. js/document)] + ;; Register shortcuts. + (doseq [item +shortcuts+] + (let [identifier (name item)] + (.registerShortcut handler identifier identifier))) + + ;; Initialize shortcut listener. + (let [event KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED + callback #(rx/push! +bus+ (keyword (.-identifier %))) + key (events/listen handler event callback)] + (fn [] + (events/unlistenByKey key) + (.clearKeyListener handler))))) + +;; DEBUG +(rx/on-value +stream+ #(println "[debug]: shortcut:" %)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Keyboard Shortcuts Handlers +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defmulti -handle-event identity) -(defmethod -handle-event :default - [ev] - (println "[warn]: shortcut" ev "not implemented")) +(defmethod -handle-event :default [ev] nil) (defmethod -handle-event :ctrl+shift+l [_] @@ -22,13 +62,21 @@ [_] (rs/emit! (dw/toggle-tool :grid))) +(rx/on-value +stream+ #(-handle-event %)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Keyboard Shortcuts Mixin +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defn -will-mount [own] - (let [sub (rx/on-value kbd/+stream+ #(-handle-event %))] + (println "shortcut-will-mount") + (let [sub (init-handler)] (assoc own ::subscription sub))) (defn -will-unmount [own] + (println "shortcut-will-unmount") (let [sub (::subscription own)] (sub) (dissoc own ::subscription)))