Browse code

Merge pull request #41 from jbweston/fix/viewport

Ensure window is scrolled to bottom after command execution

Closes #4.

Joseph Weston authored on 06/12/2021 03:27:47 • GitHub committed on 06/12/2021 03:27:47
Showing 3 changed files
... ...
@@ -30,17 +30,34 @@
30 30
    (contains? equipment cmd) (equipment cmd)
31 31
    :else {:err (str "Unknown command '" cmdline "'")})))
32 32
 
33
-(re-frame/reg-event-db
33
+(re-frame/reg-fx
34
+ :window/scroll-smoothly-to-bottom
35
+ (fn []
36
+   (let [x (.-scrollX js/window)
37
+         y (.-scrollMaxY js/window)]
38
+     (.scrollTo js/window (clj->js {:left x :top y :behavior :smooth})))))
39
+
40
+(re-frame/reg-event-fx
41
+ ::scroll-to-bottom
42
+ (fn []
43
+   {:window/scroll-smoothly-to-bottom []}))
44
+
45
+(re-frame/reg-event-fx
34 46
  ::submit-cmd
35
- (fn-traced [db _]
36
-  (let [cmdline (-> db (get-in [:cmdline :current]) trim)
47
+ (fn [cofx _]
48
+  (let [db (cofx :db)
49
+        cmdline (-> db (get-in [:cmdline :current]) trim)
37 50
         not-empty? seq]
38
-    ; TODO replace this with a "->when" macro so that we
39
-    ; don't always have to prefix with a conditional
40
-    (cond-> db
41
-     true (update-in [:cmdline :current] (constantly nil))
42
-     (not-empty? cmdline)
43
-     (as-> db
44
-       (let [output (execute db cmdline)]
45
-           (update-in db [:cmdline :history] conj
46
-               {:input cmdline :output output})))))))
51
+    {:db
52
+     ; TODO replace this with a "->when" macro so that we
53
+     ; don't always have to prefix with a conditional
54
+     (cond-> db
55
+      true (update-in [:cmdline :current] (constantly nil))
56
+      (not-empty? cmdline)
57
+      (as-> db
58
+        (let [output (execute db cmdline)]
59
+            (update-in db [:cmdline :history] conj
60
+                {:input cmdline :output output}))))
61
+     ; dispatch-later to ensure that the DOM has already been updated,
62
+     ; so that scrollMaxY has been changed appropriately.
63
+     :fx [[:dispatch-later {:ms 10 :dispatch [::scroll-to-bottom]}]]})))
... ...
@@ -137,6 +137,7 @@
137 137
 (defclass spell-list-panel []
138 138
     {}
139 139
     title-style
140
+    license-notice-style
140 141
     [:ul {:padding-left 0}]
141 142
     [:li {:list-style :inside
142 143
           :list-style-type "'* '"}
... ...
@@ -145,7 +146,8 @@
145 146
 
146 147
 (defclass item-list-panel []
147 148
     {}
148
-    title-style)
149
+    title-style
150
+    license-notice-style)
149 151
 
150 152
 
151 153
 (defclass error-message []
... ...
@@ -165,7 +167,8 @@
165 167
      (content-after ":")])
166 168
 
167 169
 (defclass property-table []
168
-    {:border-spacing 0}
170
+    {:border-spacing 0
171
+     :margin-bottom :1rem}
169 172
     [:td :th {:padding 0
170 173
               :padding-left :2ch}
171 174
      (s/& s/first-child) {:padding-left 0}]
... ...
@@ -9,7 +9,7 @@
9 9
 
10 10
 (defn prompt []
11 11
   (let [cmdline (re-frame/subscribe [::subs/cmdline])
12
-        update-cmdline #(re-frame/dispatch
12
+        update-cmdline #(re-frame/dispatch-sync
13 13
                          [::events/update-cmdline (-> % .-target .-value)])
14 14
         submit-cmd #(do (.preventDefault %)
15 15
                         (re-frame/dispatch [::events/submit-cmd]))]
... ...
@@ -87,6 +87,11 @@
87 87
         [:section.license-notice
88 88
          (spaced (x :name) "is" (l :content-kind) "subject to the" (l :license-name))])))
89 89
 
90
+(defn- listing-license-notice [listing]
91
+  [license-notice
92
+   {:name "This listing"
93
+    :license-notice (-> listing first :license-notice)}])
94
+
90 95
 (defn- description [x]
91 96
     (some->> x :desc (map #(vector :p %)) (into [:section.description])))
92 97
 
... ...
@@ -227,7 +232,8 @@
227 232
         ^{:key index}
228 233
          [:li
229 234
            [:span (spell :name)]
230
-           [:span (-> spell short-spell-description parenthesize)]])]])
235
+           [:span (-> spell short-spell-description parenthesize)]])]
236
+    [listing-license-notice spells]])
231 237
 
232 238
 (defn equipment-list-panel [items]
233 239
   [:article {:class (styles/item-list-panel)}
... ...
@@ -241,7 +247,8 @@
241 247
        cost
242 248
      :Weight
243 249
        {:align :right}
244
-       #(weight % :unit "lb" :no-quantity true)]])
250
+       #(weight % :unit "lb" :no-quantity true)]
251
+   [listing-license-notice items]])
245 252
 
246 253
 (defn error-message
247 254
     [{e :err}]