0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-17 18:21:23 -05:00

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.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 %))))

View file

@ -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:" %))

View file

@ -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)))