Skip to content

Commit 1a5a891

Browse files
authored
Merge pull request #1935 from filipesilva/idle-save
idle save
2 parents d6e1b84 + f97047f commit 1a5a891

File tree

7 files changed

+16
-13
lines changed

7 files changed

+16
-13
lines changed

src/cljs/athens/components.cljs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
to-str)]
2121
(dispatch [:block/save {:uid uid
2222
:string new-block-content
23-
:callback #()
2423
:add-time? true}])))
2524

2625

src/cljs/athens/db.cljs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,6 @@
653653
"uid -> Current block
654654
state -> Look at state atom in block-el"
655655
[uid state]
656-
(let [{:string/keys [local]} @state
657-
callback #(swap! state assoc :string/previous local)]
656+
(let [{:string/keys [local]} @state]
658657
(dispatch [:block/save {:uid uid
659-
:string local
660-
:callback callback}])))
658+
:string local}])))

src/cljs/athens/events.cljs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@
820820

821821
(reg-event-fx
822822
:block/save
823-
(fn [{:keys [db]} [_ {:keys [uid string callback] :as args}]]
823+
(fn [{:keys [db]} [_ {:keys [uid string] :as args}]]
824824
(log/debug ":block/save args" (pr-str args))
825825
(let [local? (not (db-picker/remote-db? db))
826826
block-eid (common-db/e-by-av @db/dsdb :block/uid uid)
@@ -831,8 +831,7 @@
831831
(log/debug ":block/save local?" local?
832832
", do-nothing?" do-nothing?)
833833
(when-not do-nothing?
834-
{:fx [[:dispatch [:resolve-transact-forward event]]
835-
[:invoke-callback callback]]}))))
834+
{:fx [[:dispatch [:resolve-transact-forward event]]]}))))
836835

837836

838837
(reg-event-fx

src/cljs/athens/views/blocks/content.cljs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@
333333

334334
(defn textarea-change
335335
[e _uid state]
336-
(swap! state assoc :string/local (.. e -target -value)))
336+
(swap! state assoc :string/local (.. e -target -value))
337+
((:string/idle-fn @state)))
337338

338339

339340
(defn textarea-click
@@ -410,7 +411,7 @@
410411
:on-change (fn [e] (textarea-change e uid state))
411412
:on-paste (fn [e] (textarea-paste e uid state))
412413
:on-key-down (fn [e] (textarea-keydown/textarea-key-down e uid state))
413-
:on-blur (fn [_] (db/transact-state-for-uid (or original-uid uid) state))
414+
:on-blur (:string/save-fn @state)
414415
:on-click (fn [e] (textarea-click e uid state))
415416
:on-mouse-enter (fn [e] (textarea-mouse-enter e uid state))
416417
:on-mouse-down (fn [e] (textarea-mouse-down e uid state))}])

src/cljs/athens/views/blocks/core.cljs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
[athens.views.blocks.context-menu :as context-menu]
2020
[athens.views.blocks.drop-area-indicator :as drop-area-indicator]
2121
[com.rpl.specter :as s]
22+
[goog.functions :as gfns]
2223
[re-frame.core :as rf]
2324
[reagent.core :as r]
2425
[stylefy.core :as stylefy]))
@@ -258,6 +259,7 @@
258259
[block-el block linked-ref-data {}])
259260
([block linked-ref-data _opts]
260261
(let [{:keys [linked-ref initial-open linked-ref-uid parent-uids]} linked-ref-data
262+
{:block/keys [uid original-uid]} block
261263
state (r/atom {:string/local nil
262264
:string/previous nil
263265
;; one of #{:page :block :slash :hashtag :template}
@@ -274,7 +276,12 @@
274276
:caret-position nil
275277
:show-editable-dom false
276278
:linked-ref/open (or (false? linked-ref) initial-open)
277-
:block/uid (:block/uid block)})]
279+
:block/uid uid})
280+
save-fn #(db/transact-state-for-uid (or original-uid uid) state)
281+
idle-fn (gfns/debounce save-fn 2000)]
282+
(swap! state assoc
283+
:string/save-fn save-fn
284+
:string/idle-fn idle-fn)
278285

279286
(fn [block linked-ref-data opts]
280287
(let [{:block/keys [uid

src/cljs/athens/views/blocks/textarea_keydown.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@
582582
(replace-first tail #"(?s)\)\)(.*)" ""))]
583583

584584
;; save block before navigating away
585-
(db/transact-state-for-uid uid state)
585+
((:string/save-fn @state))
586586

587587
(cond
588588
(and (re-find #"(?s)\[\[" head)

src/cljs/athens/views/pages/block_page.cljs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
[state block-uid]
6666
(dispatch [:block/save {:uid block-uid
6767
:string (:string/local state)
68-
:callback #()
6968
:add-time? true}]))
7069

7170

0 commit comments

Comments
 (0)