Skip to content

Commit f76a0ed

Browse files
committed
Initial support for user specified shortcuts
1 parent 9a4bda5 commit f76a0ed

File tree

4 files changed

+36
-28
lines changed

4 files changed

+36
-28
lines changed

dev/portal/setup.cljs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
(section "Viewers" api/viewers)
8585
["Shortcuts"
8686
(section "Key Log" @#'shortcuts/log)
87-
(section "Key Map" commands/keymap)]
87+
(section "Key Map" @#'commands/client-keymap)]
8888
["Commands"
8989
(section "UI" commands/registry)
9090
(section "Runtime" commands/runtime-registry)]

src/portal/runtime.cljc

+15-6
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@
289289
(error->data
290290
(ex-info "Failed to receive value." {:value-type (type new-value)} e))))))
291291

292+
(def ^:private runtime-keymap (atom ^::no-cache {}))
293+
292294
(defn- get-options []
293295
(let [{:keys [options watch-registry]} *session*]
294296
(with-meta
@@ -309,6 +311,7 @@
309311
(exists? js/PLANCK_VERSION) "planck"
310312
:else "web"))
311313
:value tap-list
314+
:keymap runtime-keymap
312315
:watch-registry watch-registry}
313316
options)
314317
{::no-cache true})))
@@ -405,10 +408,14 @@
405408
(defn register!
406409
([var] (register! var {}))
407410
([var opts]
408-
(swap! registry
409-
assoc
410-
(or (:name opts) (var->name var))
411-
(merge {:var var} opts))))
411+
(let [m (meta var)
412+
name (or (:name opts) (var->name var))]
413+
(doseq [shortcut (concat (:shortcuts m) (:shortcuts opts))]
414+
(swap! runtime-keymap assoc shortcut name))
415+
(swap! registry
416+
assoc
417+
name
418+
(merge {:var var} opts)))))
412419

413420
(doseq [var [#'ping
414421
#'cache-evict
@@ -422,8 +429,10 @@
422429
#'meta {:predicate can-meta?}
423430
#'update-selected {:private true}
424431
#'clear-values {:private true}
425-
#'nav {:private true}
426-
#'datafy {:name 'clojure.datafy/datafy}
432+
#'nav {:private true
433+
:shortcuts [#{"enter"}]}
434+
#'datafy {:name 'clojure.datafy/datafy
435+
:shortcuts [#{"shift" "enter"}]}
427436
#'toggle-watch {:private false
428437
:predicate atom?
429438
:name 'portal.api/toggle-watch}}]

src/portal/runtime/jvm/editor.clj

+3-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@
152152

153153
(defn goto-definition
154154
"Goto the definition of a value in an editor."
155-
{:predicate can-goto :command true}
155+
{:command true
156+
:predicate can-goto
157+
:shortcuts [["g" "d"]]}
156158
[input]
157159
(when-let [location (can-goto input)]
158160
(let [{:keys [options]} rt/*session*]

src/portal/ui/commands.cljs

+17-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
[portal.ui.drag-and-drop :as dnd]
1212
[portal.ui.icons :as icons]
1313
[portal.ui.inspector :as ins]
14+
[portal.ui.options :as options]
1415
[portal.ui.state :as state]
1516
[portal.ui.styled :as s]
1617
[portal.ui.theme :as theme]
@@ -207,12 +208,7 @@
207208
[ins/inspector option]])))
208209
doall)]]]))))
209210

210-
(def default-map
211-
{#{"enter"} 'clojure.datafy/nav
212-
#{"shift" "enter"} 'clojure.datafy/datafy
213-
["g" "d"] `portal.runtime.jvm.editor/goto-definition})
214-
215-
(def keymap (r/atom default-map))
211+
(def ^:private client-keymap (r/atom {}))
216212

217213
(def ^:private aliases {"cljs.core" "clojure.core"})
218214

@@ -221,16 +217,14 @@
221217
ns (str ns)]
222218
(symbol (aliases ns ns) (str name))))
223219

224-
(defn- find-combos [command]
220+
(defn- find-combos [keymap command]
225221
(let [command-name (:name command)]
226222
(keep
227-
#(into [] (sort %))
228-
(keep
229-
(fn [[combo f]]
230-
(when (and (= f command-name)
231-
(shortcuts/platform-supported? combo))
232-
combo))
233-
@keymap))))
223+
(fn [[combo f]]
224+
(when (and (= f command-name)
225+
(shortcuts/platform-supported? combo))
226+
combo))
227+
keymap)))
234228

235229
(def ^:private shortcut->symbol
236230
{"arrowright" [icons/arrow-right {:size "xs"}]
@@ -260,13 +254,15 @@
260254
:border-right [1 :solid (::c/border theme)]}}]])))]))
261255

262256
(defn shortcut [command]
263-
(let [theme (theme/use-theme)]
257+
(let [theme (theme/use-theme)
258+
opts (options/use-options)]
264259
[s/div {:style
265260
{:display :flex
266261
:align-items :stretch
267262
:white-space :nowrap}}
268263
[separate
269-
(for [combo (find-combos command)]
264+
(for [combo (concat (find-combos @client-keymap command)
265+
(find-combos (some-> opts :keymap deref) command))]
270266
[:<>
271267
{:key (hash combo)}
272268
(map-indexed
@@ -903,7 +899,7 @@
903899
(let [m (meta var)
904900
name (or (:name opts) (var->name var))]
905901
(doseq [shortcut (concat (:shortcuts m) (:shortcuts opts))]
906-
(swap! keymap assoc shortcut name))
902+
(swap! client-keymap assoc shortcut name))
907903
(swap! registry
908904
assoc name (merge {:name name :run var}
909905
(when-let [doc (or (:doc m) (:doc opts))] {:doc doc})
@@ -1065,7 +1061,8 @@
10651061

10661062
(defn palette [{:keys [container]}]
10671063
(let [state (state/use-state)
1068-
value (state/get-selected-value @state)]
1064+
value (state/get-selected-value @state)
1065+
opts (options/use-options)]
10691066
(react/useEffect
10701067
(fn []
10711068
(a/let [fns (state/invoke 'portal.runtime/get-functions value)]
@@ -1080,10 +1077,10 @@
10801077
[with-shortcuts
10811078
(fn [log]
10821079
(when-not (shortcuts/input? log)
1083-
(when-let [f (shortcuts/match @keymap log)]
1080+
(when-let [f (or (shortcuts/match @client-keymap log)
1081+
(shortcuts/match (some-> opts :keymap deref) log))]
10841082
(when-let [{:keys [run]} (or (get @registry f)
10851083
(get @runtime-registry f))]
1086-
10871084
(shortcuts/matched! log)
10881085
(run state)))))
10891086
(when-let [component (::input @state)]

0 commit comments

Comments
 (0)