Add simplier shortcuts implementation for the workspace.

This commit is contained in:
Andrey Antukh 2015-12-23 13:49:55 +02:00
parent 67842b39ea
commit 8201df7014
3 changed files with 56 additions and 50 deletions

View file

@ -4,7 +4,6 @@
[uxbox.router :as rt] [uxbox.router :as rt]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.ui :as ui] [uxbox.ui :as ui]
[uxbox.ui.keyboard :as kb]
[uxbox.data.load :as dl])) [uxbox.data.load :as dl]))
(enable-console-print!) (enable-console-print!)
@ -15,7 +14,6 @@
(rt/init) (rt/init)
(ui/init) (ui/init)
(kb/init)
(rs/emit! (dl/load-data)) (rs/emit! (dl/load-data))
(rx/on-value s/stream #(dl/persist-state %)))) (rx/on-value s/stream #(dl/persist-state %))))

View file

@ -1,14 +1,4 @@
(ns uxbox.ui.keyboard (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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn is-keycode? (defn is-keycode?
[keycode] [keycode]
@ -17,33 +7,3 @@
(def esc? (is-keycode? 27)) (def esc? (is-keycode? 27))
(def enter? (is-keycode? 13)) (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:" %))

View file

@ -1,14 +1,54 @@
(ns uxbox.ui.workspace.shortcuts (ns uxbox.ui.workspace.shortcuts
(:require [beicon.core :as rx] (:require [goog.events :as events]
[beicon.core :as rx]
[uxbox.rstore :as rs] [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) (defmulti -handle-event identity)
(defmethod -handle-event :default (defmethod -handle-event :default [ev] nil)
[ev]
(println "[warn]: shortcut" ev "not implemented"))
(defmethod -handle-event :ctrl+shift+l (defmethod -handle-event :ctrl+shift+l
[_] [_]
@ -22,13 +62,21 @@
[_] [_]
(rs/emit! (dw/toggle-tool :grid))) (rs/emit! (dw/toggle-tool :grid)))
(rx/on-value +stream+ #(-handle-event %))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keyboard Shortcuts Mixin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -will-mount (defn -will-mount
[own] [own]
(let [sub (rx/on-value kbd/+stream+ #(-handle-event %))] (println "shortcut-will-mount")
(let [sub (init-handler)]
(assoc own ::subscription sub))) (assoc own ::subscription sub)))
(defn -will-unmount (defn -will-unmount
[own] [own]
(println "shortcut-will-unmount")
(let [sub (::subscription own)] (let [sub (::subscription own)]
(sub) (sub)
(dissoc own ::subscription))) (dissoc own ::subscription)))