Skip to content

Commit

Permalink
Clear cell with pressed character if its filled, edit on enter
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 19, 2024
1 parent a59eb78 commit d2b3849
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
(if (or (= (.-key e) "Backspace")
(= (.-key e) "Delete"))
{:db (update-in db [:sheet] #(grid/clear-selection % selection))}
{:fx [[:dispatch [::edit-cell [r c]]]]})))))
(if (= (count (.-key e)) 1)
{:fx [[:dispatch [::edit-cell [r c] (.-key e)]]]}
{:fx [[:dispatch [::edit-cell [r c]]]]}))))))

(rf/reg-event-fx
::paste-addressed-cells
Expand Down Expand Up @@ -118,16 +120,21 @@

(rf/reg-fx
::focus-element
(fn [el-id]
(rc/after-render #(some-> js/document (.getElementById el-id) .focus))))
(fn [[el-id text]]
(rc/after-render
#(let [el (-> js/document (.getElementById el-id))]
(when text (set! (.-innerHTML el) text))
(.focus el)
(.selectAllChildren (.getSelection js/window) el)
(.collapseToEnd (.getSelection js/window))))))

(rf/reg-event-fx
::edit-cell
(fn edit-cell [{:keys [db]} [_ rc]]
(fn edit-cell [{:keys [db]} [_ rc text]]
(let [rc* (util/merged-or-self rc (:sheet db))]
{:db (assoc-in db [:ui :grid :editing-cell] rc*)
:fx [[:dispatch [::set-selection {:start rc* :end (util/merged-until-or-self rc* (:sheet db))}]]
[::focus-element "cell-input"]]})))
[::focus-element ["cell-input" text]]]})))

(rf/reg-event-db
::clear-edit-cell
Expand Down
4 changes: 3 additions & 1 deletion src/bean/ui/views/sheet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,9 @@
(or (.-ctrlKey e) (.-metaKey e)))

(defn handle-global-kbd [e]
(when (and (not (editing-text?)) (not (ctrl-or-meta? e)))
(when (and (not (editing-text?)) (not (or (ctrl-or-meta? e)
(= (.-key e) "Shift")
(= (.-key e) "Escape"))))
(rf/dispatch [::events/handle-global-kbd e])))

(defn handle-paste [e]
Expand Down

0 comments on commit d2b3849

Please sign in to comment.