Browse code

Refactor commandline submission to tail call the 'execute-cmd' event

This way scrolling to the prompt happens both on commandline
submission, and when clicking 'code' elements.

Joseph Weston authored on 18/12/2021 23:09:56
Showing 2 changed files
... ...
@@ -165,22 +165,23 @@
165 165
                                                           (merge cmd {:suggestions null-suggestions}))
166 166
       :else     cmd))))
167 167
 
168
-(re-frame/reg-event-db
168
+(re-frame/reg-event-fx
169 169
  ::execute-cmd
170
- (fn [db [_ cmd]] (execute db 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)]}]]})))
171 176
 
172 177
 (re-frame/reg-event-fx
173
- ::submit-cmd
174
- (fn [cofx [_ prompt]]
178
+ ::submit-cmdline
179
+ (fn [cofx [_]]
175 180
   (let [db (cofx :db)
176 181
         cmdline (some-> db :cmdline :current)]
177 182
     {:db
178 183
      (-> db
179 184
       (assoc-in [:cmdline :current] nil)
180 185
       (assoc-in [:cmdline :selected-history] nil)
181
-      (assoc-in [:cmdline :suggestions] {:options nil :selected nil})
182
-      (execute cmdline))
183
-
184
-     ; dispatch-later to ensure that the DOM has already been updated,
185
-     ; so that scrollMaxY has been changed appropriately.
186
-     :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to prompt]}]]})))
186
+      (assoc-in [:cmdline :suggestions] {:options nil :selected nil}))
187
+     :fx [[:dispatch [::execute-cmd cmdline]]]})))
... ...
@@ -51,11 +51,11 @@
51 51
         update-cmdline #(re-frame/dispatch-sync
52 52
                          [::events/update-cmdline (-> % .-target .-value)])
53 53
         key-pressed #(re-frame/dispatch-sync [::events/prompt-keypress %])
54
-        submit-cmd #(do (.preventDefault %)
55
-                        (re-frame/dispatch [::events/submit-cmd (-> % .-target)]))]
54
+        submit #(do (.preventDefault %)
55
+                    (re-frame/dispatch [::events/submit-cmdline]))]
56 56
     (re-frame/dispatch [::events/set-prompt-id prompt-id])
57 57
     [:form {:class (styles/prompt-style)
58
-            :on-submit submit-cmd}
58
+            :on-submit submit
59 59
             :id prompt-id}
60 60
      [:input {:value @cmdline
61 61
               :on-change update-cmdline