Skip to content

Commit

Permalink
Fix a race condition with editing cells and clearing them on Enter
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Jun 28, 2024
1 parent bc5b870 commit 1ec9c0f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,19 @@
(fn [[el-id text]]
(rc/after-render
#(when-let [el (-> js/document (.getElementById el-id))]
(when text (set! (.-innerHTML el) text))
(set! (.-innerHTML el) (or text ""))
(.focus el)
(.selectAllChildren (.getSelection js/window) el)
(.collapseToEnd (.getSelection js/window))))))

(rf/reg-event-fx
::edit-cell
(fn edit-cell [{:keys [db]} [_ rc text]]
(let [rc* (util/merged-or-self rc (:sheet db))]
(let [rc* (util/merged-or-self rc (:sheet db))
content (get-in db (flatten [:sheet :grid rc* :content]))]
{: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" text]]]})))
[::focus-element ["cell-input" (or text content)]]]})))

(rf/reg-event-db
::clear-edit-cell
Expand Down
4 changes: 1 addition & 3 deletions src/bean/ui/views/sheet.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@

(defn- submit-cell-input []
(when-let [el (.getElementById js/document "cell-input")]
(rf/dispatch-sync [::events/submit-cell-input (str (.-textContent el))])
;; Reagent does not clear the element when input moves to a blank cell.
(set! (.-innerHTML el) nil)))
(rf/dispatch-sync [::events/submit-cell-input (str (.-textContent el))])))

(defn- selection->rect [^js g area row-heights col-widths]
(when (:start area)
Expand Down

0 comments on commit 1ec9c0f

Please sign in to comment.