mirror of
https://github.com/penpot/penpot.git
synced 2026-08-06 12:58:55 +00:00
Minor refactor on shortcuts mixin.
This commit is contained in:
parent
3b77286c77
commit
ac4e090b96
@ -15,7 +15,7 @@
|
|||||||
[uxbox.ui.keyboard :as kbd]
|
[uxbox.ui.keyboard :as kbd]
|
||||||
[uxbox.ui.workspace.scroll :as scroll]
|
[uxbox.ui.workspace.scroll :as scroll]
|
||||||
[uxbox.ui.workspace.base :as wb]
|
[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.header :refer (header)]
|
||||||
[uxbox.ui.workspace.rules :refer (horizontal-rule vertical-rule)]
|
[uxbox.ui.workspace.rules :refer (horizontal-rule vertical-rule)]
|
||||||
[uxbox.ui.workspace.sidebar :refer (left-sidebar right-sidebar)]
|
[uxbox.ui.workspace.sidebar :refer (left-sidebar right-sidebar)]
|
||||||
@ -142,5 +142,5 @@
|
|||||||
:name "workspace"
|
:name "workspace"
|
||||||
:mixins [mx/static
|
:mixins [mx/static
|
||||||
rum/reactive
|
rum/reactive
|
||||||
shortcuts/mixin
|
shortcuts-mixin
|
||||||
(mx/local)]}))
|
(mx/local)]}))
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||||
|
|
||||||
(ns uxbox.ui.workspace.shortcuts
|
(ns uxbox.ui.workspace.shortcuts
|
||||||
(:require-macros [uxbox.util.syntax :refer [define-once]])
|
|
||||||
(:require [goog.events :as events]
|
(:require [goog.events :as events]
|
||||||
[beicon.core :as rx]
|
[beicon.core :as rx]
|
||||||
[uxbox.rstore :as rs]
|
[uxbox.rstore :as rs]
|
||||||
@ -18,15 +17,9 @@
|
|||||||
goog.ui.KeyboardShortcutHandler
|
goog.ui.KeyboardShortcutHandler
|
||||||
goog.ui.KeyboardShortcutHandler))
|
goog.ui.KeyboardShortcutHandler))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
(declare move-selected)
|
||||||
;; Keyboard Shortcuts Handlers
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
|
|
||||||
(defn move-selected
|
;; --- Shortcuts
|
||||||
[dir speed]
|
|
||||||
(case speed
|
|
||||||
:std (rs/emit! (dw/move-selected dir 1))
|
|
||||||
:fast (rs/emit! (dw/move-selected dir 20))))
|
|
||||||
|
|
||||||
(defonce ^:const +shortcuts+
|
(defonce ^:const +shortcuts+
|
||||||
{:ctrl+g #(rs/emit! (dw/toggle-flag :grid))
|
{:ctrl+g #(rs/emit! (dw/toggle-flag :grid))
|
||||||
@ -53,18 +46,12 @@
|
|||||||
:right #(move-selected :right :std)
|
:right #(move-selected :right :std)
|
||||||
:left #(move-selected :left :std)})
|
:left #(move-selected :left :std)})
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;; --- Shortcuts Setup Functions
|
||||||
;; Keyboard Shortcuts Watcher
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
|
|
||||||
(defonce ^:const ^:private +bus+
|
(defn- watch-shortcuts
|
||||||
(rx/bus))
|
[sink]
|
||||||
|
|
||||||
(defonce ^:const +stream+ +bus+)
|
|
||||||
|
|
||||||
(defn- init-handler
|
|
||||||
[]
|
|
||||||
(let [handler (KeyboardShortcutHandler. js/document)]
|
(let [handler (KeyboardShortcutHandler. js/document)]
|
||||||
|
|
||||||
;; Register shortcuts.
|
;; Register shortcuts.
|
||||||
(doseq [item (keys +shortcuts+)]
|
(doseq [item (keys +shortcuts+)]
|
||||||
(let [identifier (name item)]
|
(let [identifier (name item)]
|
||||||
@ -72,38 +59,44 @@
|
|||||||
|
|
||||||
;; Initialize shortcut listener.
|
;; Initialize shortcut listener.
|
||||||
(let [event KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED
|
(let [event KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED
|
||||||
callback #(rx/push! +bus+ (keyword (.-identifier %)))
|
callback #(sink (keyword (.-identifier %)))
|
||||||
key (events/listen handler event callback)]
|
key (events/listen handler event callback)]
|
||||||
(fn []
|
(fn []
|
||||||
(events/unlistenByKey key)
|
(events/unlistenByKey key)
|
||||||
(.clearKeyListener handler)))))
|
(.clearKeyListener handler)))))
|
||||||
|
|
||||||
(define-once :subscriptions
|
(defn- initialize
|
||||||
(rx/on-value +stream+ #(println "[debug]: shortcut:" %))
|
[]
|
||||||
(rx/on-value +stream+ (fn [event]
|
(let [stream (->> (rx/create watch-shortcuts)
|
||||||
|
(rx/pr-log "[debug]: shortcut:"))]
|
||||||
|
(rx/on-value stream (fn [event]
|
||||||
(when-let [handler (get +shortcuts+ event)]
|
(when-let [handler (get +shortcuts+ event)]
|
||||||
(handler)))))
|
(handler))))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;; --- Helpers
|
||||||
;; Keyboard Shortcuts Mixin
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
||||||
|
|
||||||
(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]
|
[own]
|
||||||
(let [sub (init-handler)]
|
(assoc own ::sub (initialize)))
|
||||||
(assoc own ::subscription sub)))
|
|
||||||
|
|
||||||
(defn -will-unmount
|
(defn- will-unmount
|
||||||
[own]
|
[own]
|
||||||
(let [sub (::subscription own)]
|
(.close (::sub own))
|
||||||
(sub)
|
(dissoc own ::sub))
|
||||||
(dissoc own ::subscription)))
|
|
||||||
|
|
||||||
(defn -transfer-state
|
(defn- transfer-state
|
||||||
[old-own own]
|
[oldown own]
|
||||||
(assoc own ::subscription (::subscription old-own)))
|
(assoc own ::sub (::sub oldown)))
|
||||||
|
|
||||||
(def mixin
|
(def shortcuts-mixin
|
||||||
{:will-mount -will-mount
|
{:will-mount will-mount
|
||||||
:will-unmount -will-unmount
|
:will-unmount will-unmount
|
||||||
:transfer-state -transfer-state})
|
:transfer-state transfer-state})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user