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

Minor refactor on shortcuts mixin.

This commit is contained in:
Andrey Antukh 2016-04-09 11:50:29 +03:00
parent 3b77286c77
commit ac4e090b96
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
2 changed files with 36 additions and 43 deletions

View file

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

View file

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