mirror of
https://github.com/penpot/penpot.git
synced 2025-03-18 02:32:13 -05:00
Add simplier shortcuts implementation for the workspace.
This commit is contained in:
parent
67842b39ea
commit
8201df7014
3 changed files with 56 additions and 50 deletions
|
@ -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 %))))
|
||||
|
|
|
@ -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:" %))
|
||||
|
|
|
@ -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)))
|
||||
|
|
Loading…
Add table
Reference in a new issue