diff --git a/src/uxbox/ui/workspace.cljs b/src/uxbox/ui/workspace.cljs index eb522c31c..95c1232d8 100644 --- a/src/uxbox/ui/workspace.cljs +++ b/src/uxbox/ui/workspace.cljs @@ -15,7 +15,7 @@ [uxbox.ui.keyboard :as kbd] [uxbox.ui.workspace.scroll :as scroll] [uxbox.ui.workspace.base :as wb] - [uxbox.ui.workspace.shortcuts :as shortcuts] + [uxbox.ui.workspace.shortcuts :refer (shortcuts-mixin)] [uxbox.ui.workspace.header :refer (header)] [uxbox.ui.workspace.rules :refer (horizontal-rule vertical-rule)] [uxbox.ui.workspace.sidebar :refer (left-sidebar right-sidebar)] @@ -142,5 +142,5 @@ :name "workspace" :mixins [mx/static rum/reactive - shortcuts/mixin + shortcuts-mixin (mx/local)]})) diff --git a/src/uxbox/ui/workspace/shortcuts.cljs b/src/uxbox/ui/workspace/shortcuts.cljs index 9439629f6..4e543be7f 100644 --- a/src/uxbox/ui/workspace/shortcuts.cljs +++ b/src/uxbox/ui/workspace/shortcuts.cljs @@ -6,7 +6,6 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com> (ns uxbox.ui.workspace.shortcuts - (:require-macros [uxbox.util.syntax :refer [define-once]]) (:require [goog.events :as events] [beicon.core :as rx] [uxbox.rstore :as rs] @@ -18,15 +17,9 @@ goog.ui.KeyboardShortcutHandler goog.ui.KeyboardShortcutHandler)) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Keyboard Shortcuts Handlers -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(declare move-selected) -(defn move-selected - [dir speed] - (case speed - :std (rs/emit! (dw/move-selected dir 1)) - :fast (rs/emit! (dw/move-selected dir 20)))) +;; --- Shortcuts (defonce ^:const +shortcuts+ {:ctrl+g #(rs/emit! (dw/toggle-flag :grid)) @@ -53,18 +46,12 @@ :right #(move-selected :right :std) :left #(move-selected :left :std)}) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Keyboard Shortcuts Watcher -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; --- Shortcuts Setup Functions -(defonce ^:const ^:private +bus+ - (rx/bus)) - -(defonce ^:const +stream+ +bus+) - -(defn- init-handler - [] +(defn- watch-shortcuts + [sink] (let [handler (KeyboardShortcutHandler. js/document)] + ;; Register shortcuts. (doseq [item (keys +shortcuts+)] (let [identifier (name item)] @@ -72,38 +59,44 @@ ;; Initialize shortcut listener. (let [event KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED - callback #(rx/push! +bus+ (keyword (.-identifier %))) + callback #(sink (keyword (.-identifier %))) key (events/listen handler event callback)] (fn [] (events/unlistenByKey key) (.clearKeyListener handler))))) -(define-once :subscriptions - (rx/on-value +stream+ #(println "[debug]: shortcut:" %)) - (rx/on-value +stream+ (fn [event] +(defn- initialize + [] + (let [stream (->> (rx/create watch-shortcuts) + (rx/pr-log "[debug]: shortcut:"))] + (rx/on-value stream (fn [event] (when-let [handler (get +shortcuts+ event)] - (handler))))) + (handler)))))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Keyboard Shortcuts Mixin -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; --- Helpers -(defn -will-mount +(defn- move-selected + [dir speed] + (case speed + :std (rs/emit! (dw/move-selected dir 1)) + :fast (rs/emit! (dw/move-selected dir 20)))) + +;; --- Mixin + +(defn- will-mount [own] - (let [sub (init-handler)] - (assoc own ::subscription sub))) + (assoc own ::sub (initialize))) -(defn -will-unmount +(defn- will-unmount [own] - (let [sub (::subscription own)] - (sub) - (dissoc own ::subscription))) + (.close (::sub own)) + (dissoc own ::sub)) -(defn -transfer-state - [old-own own] - (assoc own ::subscription (::subscription old-own))) +(defn- transfer-state + [oldown own] + (assoc own ::sub (::sub oldown))) -(def mixin - {:will-mount -will-mount - :will-unmount -will-unmount - :transfer-state -transfer-state}) +(def shortcuts-mixin + {:will-mount will-mount + :will-unmount will-unmount + :transfer-state transfer-state})