Browse code

Add an ID to the prompt and store it in the database

This will later be used to get a reference to the prompt from
any part of the application.

Joseph Weston authored on 18/12/2021 23:07:20
Showing 3 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 []}
... ...
@@ -60,6 +60,11 @@
60 60
    (-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history)
61 61
    :else (append-history {:err (str "Unknown command '" cmdline "'")}))))
62 62
 
63
+(re-frame/reg-event-db
64
+ ::set-prompt-id
65
+ (fn [db [_ prompt-id]]
66
+     (assoc-in db [:cmdline :prompt-id] prompt-id)))
67
+
63 68
 (re-frame/reg-fx
64 69
  :window/scroll-to
65 70
  (fn [element]
... ...
@@ -45,7 +45,7 @@
45 45
             :on-click #(re-frame/dispatch [::events/execute-cmd text])}
46 46
      text])
47 47
 
48
-(defn prompt []
48
+(defn prompt [prompt-id]
49 49
   (let [cmdline (re-frame/subscribe [::subs/cmdline])
50 50
         suggestions (re-frame/subscribe [::subs/cmdline-suggestions])
51 51
         update-cmdline #(re-frame/dispatch-sync
... ...
@@ -53,8 +53,10 @@
53 53
         key-pressed #(re-frame/dispatch-sync [::events/prompt-keypress %])
54 54
         submit-cmd #(do (.preventDefault %)
55 55
                         (re-frame/dispatch [::events/submit-cmd (-> % .-target)]))]
56
+    (re-frame/dispatch [::events/set-prompt-id prompt-id])
56 57
     [:form {:class (styles/prompt-style)
57 58
             :on-submit submit-cmd}
59
+            :id prompt-id}
58 60
      [:input {:value @cmdline
59 61
               :on-change update-cmdline
60 62
               :on-keyDown key-pressed
... ...
@@ -328,4 +330,4 @@
328 330
 (defn main-panel []
329 331
   [:div {:class (styles/screen)}
330 332
    [:div {:class (styles/main-panel)}
331
-    [title] [history] [prompt] [footer]]])
333
+    [title] [history] [prompt "prompt"] [footer]]])