Browse code

Merge pull request #54 from jbweston/feat/click-to-submit

Add clickable text that is submitted as a command.

Closes #52

Joseph Weston authored on 18/12/2021 23:15:09 • GitHub committed on 18/12/2021 23:15:09
Showing 4 changed files
... ...
@@ -31,7 +31,8 @@
31 31
 
32 32
 (def default-db
33 33
   {:name "DMR"
34
-   :cmdline {:current ""
34
+   :cmdline {:prompt-id nil
35
+             :current ""
35 36
              :suggestions {:options nil :selected nil}
36 37
              :selected-history nil
37 38
              :history []}
... ...
@@ -11,6 +11,9 @@
11 11
  (fn-traced [_ _]
12 12
    db/default-db))
13 13
 
14
+(defn prompt [db]
15
+  (->> db :cmdline :prompt-id (.getElementById js/document)))
16
+
14 17
 (defn single-space [s]
15 18
   (as-> s x
16 19
         (split x " ")
... ...
@@ -60,6 +63,11 @@
60 63
    (-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history)
61 64
    :else (append-history {:err (str "Unknown command '" cmdline "'")}))))
62 65
 
66
+(re-frame/reg-event-db
67
+ ::set-prompt-id
68
+ (fn [db [_ prompt-id]]
69
+     (assoc-in db [:cmdline :prompt-id] prompt-id)))
70
+
63 71
 (re-frame/reg-fx
64 72
  :window/scroll-to
65 73
  (fn [element]
... ...
@@ -158,17 +166,22 @@
158 166
       :else     cmd))))
159 167
 
160 168
 (re-frame/reg-event-fx
161
- ::submit-cmd
162
- (fn [cofx [_ prompt]]
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
+
177
+(re-frame/reg-event-fx
178
+ ::submit-cmdline
179
+ (fn [cofx [_]]
163 180
   (let [db (cofx :db)
164 181
         cmdline (some-> db :cmdline :current)]
165 182
     {:db
166 183
      (-> db
167 184
       (assoc-in [:cmdline :current] nil)
168 185
       (assoc-in [:cmdline :selected-history] nil)
169
-      (assoc-in [:cmdline :suggestions] {:options nil :selected nil})
170
-      (execute cmdline))
171
-
172
-     ; dispatch-later to ensure that the DOM has already been updated,
173
-     ; so that scrollMaxY has been changed appropriately.
174
-     :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to prompt]}]]})))
186
+      (assoc-in [:cmdline :suggestions] {:options nil :selected nil}))
187
+     :fx [[:dispatch [::execute-cmd cmdline]]]})))
... ...
@@ -100,6 +100,11 @@
100 100
         ; letters that hang down touch the lower edge of the box without this
101 101
         :padding-bottom :0.4rem}])
102 102
 
103
+(defclass code []
104
+    {:font-size :1rem
105
+     :text-decoration :underline
106
+     :cursor :pointer})
107
+
103 108
 (def kind-style [:span.kind {:font-style :italic}])
104 109
 (def description-style [:section.description {:font-style :italic}])
105 110
 (def license-notice-style
... ...
@@ -40,17 +40,23 @@
40 40
 
41 41
 (defn- enumerated [& colls] (apply zip (range) colls))
42 42
 
43
+(defn code [text]
44
+    [:code {:class (styles/code)
45
+            :on-click #(re-frame/dispatch [::events/execute-cmd text])}
46
+     text])
43 47
 
44
-(defn prompt []
48
+(defn prompt [prompt-id]
45 49
   (let [cmdline (re-frame/subscribe [::subs/cmdline])
46 50
         suggestions (re-frame/subscribe [::subs/cmdline-suggestions])
47 51
         update-cmdline #(re-frame/dispatch-sync
48 52
                          [::events/update-cmdline (-> % .-target .-value)])
49 53
         key-pressed #(re-frame/dispatch-sync [::events/prompt-keypress %])
50
-        submit-cmd #(do (.preventDefault %)
51
-                        (re-frame/dispatch [::events/submit-cmd (-> % .-target)]))]
54
+        submit #(do (.preventDefault %)
55
+                    (re-frame/dispatch [::events/submit-cmdline]))]
56
+    (re-frame/dispatch [::events/set-prompt-id prompt-id])
52 57
     [:form {:class (styles/prompt-style)
53
-            :on-submit submit-cmd}
58
+            :on-submit submit
59
+            :id prompt-id}
54 60
      [:input {:value @cmdline
55 61
               :on-change update-cmdline
56 62
               :on-keyDown key-pressed
... ...
@@ -249,7 +255,7 @@
249 255
      (for [[index spell] (enumerated (sort-by :name spells))]
250 256
         ^{:key index}
251 257
          [:li
252
-           [:span (spell :name)]
258
+           [:span [code (spell :name)]]
253 259
            [:span (-> spell short-spell-description parenthesize)]])]
254 260
     [listing-license-notice spells]])
255 261
 
... ...
@@ -259,7 +265,7 @@
259 265
    [property-table items
260 266
      :Name
261 267
        {}
262
-       :name
268
+       #(-> % :name code)
263 269
      :Cost
264 270
        {:align :right}
265 271
        cost
... ...
@@ -324,4 +330,4 @@
324 330
 (defn main-panel []
325 331
   [:div {:class (styles/screen)}
326 332
    [:div {:class (styles/main-panel)}
327
-    [title] [history] [prompt] [footer]]])
333
+    [title] [history] [prompt "prompt"] [footer]]])