Browse code

Refactor execution events

Joseph Weston authored on 19/12/2021 02:13:59
Showing 2 changed files
... ...
@@ -63,6 +63,32 @@
63 63
    (-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history)
64 64
    :else (append-history {:err (str "Unknown command '" cmdline "'")}))))
65 65
 
66
+(re-frame/reg-event-fx
67
+ ::execute
68
+ (fn [cofx [_ cmd]]
69
+    (let [db (cofx :db)]
70
+      {:db (execute db cmd)
71
+       ; dispatch-later to ensure that the DOM has already been updated,
72
+       ; so that scrollMaxY has been changed appropriately.
73
+       :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to (prompt db)]}]]})))
74
+
75
+
76
+(re-frame/reg-event-db
77
+ ::clear-cmdline
78
+ (fn [db _]
79
+   (-> db
80
+     (assoc-in [:cmdline :current] nil)
81
+     (assoc-in [:cmdline :selected-history] nil)
82
+     (assoc-in [:cmdline :suggestions] {:options nil :selected nil}))))
83
+
84
+
85
+(re-frame/reg-event-fx
86
+ ::submit-cmdline
87
+ (fn [cofx _]
88
+   {:fx [[:dispatch [::execute (-> cofx :db :cmdline :current)]]
89
+         [:dispatch [::clear-cmdline]]]}))
90
+
91
+
66 92
 (re-frame/reg-event-db
67 93
  ::set-prompt-id
68 94
  (fn [db [_ prompt-id]]
... ...
@@ -129,13 +155,6 @@
129 155
  (fn [cmd [_ idx]]
130 156
      (assoc-in cmd [:suggestions :selected] idx)))
131 157
 
132
-(re-frame/reg-event-fx
133
- ::click-suggestion
134
- (fn [cofx [_ prompt cmd]]
135
-   (let [db (cofx :db)]
136
-     {:db (assoc-in db [:cmdline :current] cmd)
137
-      :fx [[:dispatch [::submit-cmd prompt]]]})))
138
-
139 158
 (re-frame/reg-event-db
140 159
  ::prompt-keypress
141 160
  (re-frame/path :cmdline)
... ...
@@ -165,23 +184,5 @@
165 184
                                                           (merge cmd {:suggestions null-suggestions}))
166 185
       :else     cmd))))
167 186
 
168
-(re-frame/reg-event-fx
169
- ::execute-cmd
170
- (fn [cofx [_ cmd]]
171
-    (let [db (cofx :db)]
172
-      {:db (execute db cmd)
173
-       ; dispatch-later to ensure that the DOM has already been updated,
174
-       ; so that scrollMaxY has been changed appropriately.
175
-       :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to (prompt db)]}]]})))
176 187
 
177
-(re-frame/reg-event-fx
178
- ::submit-cmdline
179
- (fn [cofx [_]]
180
-  (let [db (cofx :db)
181
-        cmdline (some-> db :cmdline :current)]
182
-    {:db
183
-     (-> db
184
-      (assoc-in [:cmdline :current] nil)
185
-      (assoc-in [:cmdline :selected-history] nil)
186
-      (assoc-in [:cmdline :suggestions] {:options nil :selected nil}))
187
-     :fx [[:dispatch [::execute-cmd cmdline]]]})))
188
+
... ...
@@ -42,7 +42,7 @@
42 42
 
43 43
 (defn code [text]
44 44
     [:code {:class (styles/code)
45
-            :on-click #(re-frame/dispatch [::events/execute-cmd text])}
45
+            :on-click #(re-frame/dispatch [::events/execute text])}
46 46
      text])
47 47
 
48 48
 (defn prompt [prompt-id]
... ...
@@ -69,10 +69,8 @@
69 69
             [:li
70 70
              {:class (when (= idx selected) :selected)
71 71
               :on-mouse-enter #(re-frame/dispatch [::events/hover-suggestion idx])
72
-              :on-click #(do (.preventDefault %)
73
-                             (re-frame/dispatch [::events/click-suggestion
74
-                                                 (-> % .-target .-parentElement .-parentElement)
75
-                                                 x]))}
72
+              :on-click #(do (re-frame/dispatch [::events/execute x])
73
+                             (re-frame/dispatch [::events/clear-cmdline]))}
76 74
              x]))]]))
77 75
 
78 76
 (defn title []