0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-13 02:28:18 -05:00
penpot/frontend/uxbox/ui/keyboard.cljs

50 lines
1.4 KiB
Text
Raw Normal View History

2015-12-14 09:50:19 +02:00
(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))
2015-12-14 09:50:19 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-14 09:50:19 +02:00
(defn is-keycode?
[keycode]
(fn [e]
(= (.-keyCode e) keycode)))
(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:" %))