Skip to content

Commit

Permalink
Check if area within bounds when setting selection
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 20, 2024
1 parent 2bf4437 commit ce52b91
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
{:start (offset start pasted-at)
:end (offset end pasted-at)}))

(defn clear-selection [sheet {:keys [start end]}]
(defn clear-area [sheet {:keys [start end]}]
(->> (util/addresses-matrix start end)
(mapcat identity)
(map #(do [% {:content ""}]))
Expand Down
6 changes: 4 additions & 2 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
(let [[mr mc] (get-in db [:sheet :grid r c :style :merged-until])]
(if (or (= (.-key e) "Backspace")
(= (.-key e) "Delete"))
{:db (update-in db [:sheet] #(grid/clear-selection % selection))}
{:db (update-in db [:sheet] #(grid/clear-area % selection))}
(if-let [move-to (cond
(= (.-key e) "ArrowUp") [(dec r) c]
(= (.-key e) "ArrowLeft") [r (dec c)]
Expand Down Expand Up @@ -151,7 +151,9 @@
(rf/reg-event-db
::set-selection
(fn [db [_ selection]]
(assoc-in db [:ui :grid :selection] selection)))
(if (util/area-inside? (:sheet db) selection)
(assoc-in db [:ui :grid :selection] selection)
db)))

(rf/reg-event-fx
::select-frame
Expand Down
5 changes: 5 additions & 0 deletions src/bean/ui/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@
(let [[r* c*] (merged-or-self rc sheet)]
(or (get-in sheet [:grid r* c* :style :merged-until]) rc)))

(defn area-inside? [sheet {:keys [start end]}]
(and
(nat-int? (first start)) (nat-int? (second start))
(< (first end) (get-in sheet [:grid-dimensions :num-rows]))
(< (second end) (get-in sheet [:grid-dimensions :num-cols]))))

0 comments on commit ce52b91

Please sign in to comment.