Skip to content

Commit

Permalink
Clear many cells with backspace/delete
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 19, 2024
1 parent 7683581 commit 903b010
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@

;; untested and slightly weird interface, exists for pasting
;; many cells and handling merged cells etc.
(defn update-cells-bulk [sheet start addressed-attrs]
(defn update-cells-bulk [sheet {:keys [start]} addressed-attrs]
(->> addressed-attrs
(map #(do [(offset (first %) start) (second %)]))
(reduce
Expand All @@ -313,6 +313,13 @@
(unmerge-cells sheet (map #(offset % start) (keys addressed-attrs))))
eval-sheet-a-few-times))

(defn clear-selection [sheet {:keys [start end]}]
(->> (util/addresses-matrix start end)
(mapcat identity)
(map #(do [% {:content ""}]))
(into {})
(update-cells-bulk sheet start)))

(defn eval-named
([name {:keys [bindings] :as sheet}]
(if-let [value (bindings name)]
Expand Down
10 changes: 7 additions & 3 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@
(rf/reg-event-fx
::handle-global-kbd
(fn handle-global-kbd [{:keys [db]} [_ e]]
(when-let [[r c] (:start (get-in db [:ui :grid :selection]))]
{:fx [[:dispatch [::edit-cell [r c]]]]})))
(let [selection (get-in db [:ui :grid :selection])]
(when-let [[r c] (:start selection)]
(if (or (= (.-key e) "Backspace")
(= (.-key e) "Delete"))
{:db (update-in db [:sheet] #(grid/clear-selection % selection))}
{:fx [[:dispatch [::edit-cell [r c]]]]})))))

(rf/reg-event-db
::paste-addressed-cells
(fn paste-addressed-cells [db [_ addressed-cells]]
(update-in db [:sheet] #(grid/update-cells-bulk %
(:start (get-in db [:ui :grid :selection]))
(get-in db [:ui :grid :selection])
addressed-cells))))

(rf/reg-fx
Expand Down
4 changes: 2 additions & 2 deletions src/bean/ui/paste.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@

(defn hiccup-matrix->html [matrix]
(hr/hiccup-to-html
[[:body {} [:table {}
(into [:tbody {}] matrix)]]]))
[[:table {}
(into [:tbody {}] matrix)]]))

(defn hickory-table->cells [hickory-table]
(loop [hiccup-cells (->>
Expand Down

0 comments on commit 903b010

Please sign in to comment.