Skip to content

Commit

Permalink
Merge pull request #2030 from filipesilva/undo-textarea
Browse files Browse the repository at this point in the history
undo textarea
  • Loading branch information
filipesilva committed Feb 21, 2022
2 parents 1a1deeb + efef7b5 commit d59ab43
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 546 deletions.
11 changes: 0 additions & 11 deletions src/cljc/athens/common_events.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@
:event/args {:datoms datoms}}))


;; undo-redo events

(defn build-undo-redo-event
"Builds `:datascript/undo-redo`"
[redo?]
(let [event-id (utils/gen-event-id)]
{:event/id event-id
:event/type :datascript/undo-redo
:event/args {:redo? redo?}}))


;; - presence events

(defn build-presence-hello-event
Expand Down
56 changes: 0 additions & 56 deletions src/cljc/athens/common_events/resolver.cljc

This file was deleted.

3 changes: 1 addition & 2 deletions src/cljc/athens/common_events/resolver/atomic.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
(:require
[athens.common-db :as common-db]
[athens.common-events.graph.ops :as graph-ops]
[athens.common-events.resolver :as resolver]
[athens.common-events.resolver.order :as order]
[athens.common.logging :as log]
[athens.common.utils :as utils]
Expand Down Expand Up @@ -350,7 +349,7 @@
(resolve-atomic-op-to-tx db (if (:op/atomic? event)
event
op))
(resolver/resolve-event-to-tx db event)))
(throw (ex-info "Can't resolve event, only Atomic Graph Ops are allowed." event))))


(defn resolve-transact!
Expand Down
83 changes: 32 additions & 51 deletions src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
[athens.common-events.graph.atomic :as atomic-graph-ops]
[athens.common-events.graph.composite :as composite-ops]
[athens.common-events.graph.ops :as graph-ops]
[athens.common-events.resolver :as resolver]
[athens.common-events.resolver.atomic :as atomic-resolver]
[athens.common-events.resolver.undo :as undo-resolver]
[athens.common-events.schema :as schema]
Expand Down Expand Up @@ -625,7 +624,6 @@
;; TODO: remove this event and also :transact! when the following are converted to events:
;; - athens.electron.images/dnd-image (needs file upload)
;; - :upload/roam-edn (needs internal representation)
;; - :undo / :redo
;; No other reframe events should be calling this event.
(reg-event-fx
:transact
Expand Down Expand Up @@ -851,65 +849,48 @@
{:fs/write! nil}))


(def USE_OLD_UNDO_FOR_LOCAL? true)


(reg-event-fx
:undo
[(interceptors/sentry-span "undo")]
(fn [{:keys [db]} _]
(let [local? (not (db-picker/remote-db? db))]
(log/debug ":undo: local?" local?)
(if (and local? USE_OLD_UNDO_FOR_LOCAL?)

(let [undo-event (common-events/build-undo-redo-event false)
tx-data (resolver/resolve-event-to-tx db/history undo-event)]
{:fx [[:dispatch [:transact tx-data]]]})

(try
(log/debug ":undo count" (undo/count-undo db))
(if-some [[undo db'] (undo/pop-undo db)]
(let [[evt-dsdb evt] undo
evt-id (:event/id evt)
dsdb @db/dsdb
undo-evt (undo-resolver/build-undo-event dsdb evt-dsdb evt)
undo-evt-id (:event/id undo-evt)
db'' (undo/push-redo db' undo-evt-id [dsdb undo-evt])]
(log/debug ":undo evt" (pr-str evt-id) "as" (pr-str undo-evt-id))
{:db db''
:fx [[:dispatch [:resolve-transact-forward undo-evt]]]})
{})
(catch :default _
{:fx [[:dispatch [:alert/js "Undo for this operation not supported in Lan-Party, yet."]]]}))))))
(log/debug ":undo")
(try
(log/debug ":undo count" (undo/count-undo db))
(if-some [[undo db'] (undo/pop-undo db)]
(let [[evt-dsdb evt] undo
evt-id (:event/id evt)
dsdb @db/dsdb
undo-evt (undo-resolver/build-undo-event dsdb evt-dsdb evt)
undo-evt-id (:event/id undo-evt)
db'' (undo/push-redo db' undo-evt-id [dsdb undo-evt])]
(log/debug ":undo evt" (pr-str evt-id) "as" (pr-str undo-evt-id))
{:db db''
:fx [[:dispatch [:resolve-transact-forward undo-evt]]]})
{})
(catch :default _
{:fx [[:dispatch [:alert/js "Undo for this operation not supported in Lan-Party, yet."]]]}))))


(reg-event-fx
:redo
[(interceptors/sentry-span "redo")]
(fn [{:keys [db]} _]
(let [local? (not (db-picker/remote-db? db))]
(log/debug ":redo local?" local?)
(if (and local? USE_OLD_UNDO_FOR_LOCAL?)

(let [redo-event (common-events/build-undo-redo-event true)
tx-data (resolver/resolve-event-to-tx db/history redo-event)]
{:fx [[:dispatch [:transact tx-data]]]})

(try
(log/debug ":redo count" (undo/count-redo db))
(if-some [[redo db'] (undo/pop-redo db)]
(let [[evt-dsdb evt] redo
evt-id (:event/id evt)
dsdb @db/dsdb
undo-evt (undo-resolver/build-undo-event dsdb evt-dsdb evt)
undo-evt-id (:event/id undo-evt)
db'' (undo/push-undo db' undo-evt-id [dsdb undo-evt])]
(log/debug ":redo evt" (pr-str evt-id) "as" (pr-str undo-evt-id))
{:db db''
:fx [[:dispatch [:resolve-transact-forward undo-evt]]]})
{})
(catch :default _
{:fx [[:dispatch [:alert/js "Redo for this operation not supported in Lan-Party, yet."]]]}))))))
(log/debug ":redo")
(try
(log/debug ":redo count" (undo/count-redo db))
(if-some [[redo db'] (undo/pop-redo db)]
(let [[evt-dsdb evt] redo
evt-id (:event/id evt)
dsdb @db/dsdb
undo-evt (undo-resolver/build-undo-event dsdb evt-dsdb evt)
undo-evt-id (:event/id undo-evt)
db'' (undo/push-undo db' undo-evt-id [dsdb undo-evt])]
(log/debug ":redo evt" (pr-str evt-id) "as" (pr-str undo-evt-id))
{:db db''
:fx [[:dispatch [:resolve-transact-forward undo-evt]]]})
{})
(catch :default _
{:fx [[:dispatch [:alert/js "Redo for this operation not supported in Lan-Party, yet."]]]}))))


(reg-event-fx
Expand Down
17 changes: 9 additions & 8 deletions src/cljs/athens/listeners.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@
KeyCodes.ZERO (dispatch [:zoom/reset])
KeyCodes.K (dispatch [:athena/toggle])
KeyCodes.G (dispatch [:devtool/toggle])
KeyCodes.Z (let [editing-uid @(subscribe [:editing/uid])
selected-items @(subscribe [::select-subs/items])]
;; editing/uid must be nil or selected-items must be non-empty
(when (or (nil? editing-uid)
(not-empty selected-items))
(if shift
(dispatch [:redo])
(dispatch [:undo]))))
KeyCodes.Z (do
;; Disable the default undo behaviour.
;; Chrome has a textarea undo that does not behave like
;; we want undo to behave.
(.. e preventDefault)
;; Dispatch our custom undo/redo.
(if shift
(dispatch [:redo])
(dispatch [:undo])))
KeyCodes.BACKSLASH (if shift
(dispatch [:right-sidebar/toggle])
(dispatch [:left-sidebar/toggle]))
Expand Down
7 changes: 0 additions & 7 deletions src/cljs/athens/views/blocks/textarea_keydown.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,6 @@
(mapv :block/uid))]
(dispatch [::select-events/set-items children]))

;; When undo no longer makes changes for local textarea, do datascript undo.
(= key-code KeyCodes.Z) (let [{:string/keys [local previous]} @state]
(when (= local previous)
(if shift
(dispatch [:redo])
(dispatch [:undo]))))

(= key-code KeyCodes.B) (surround-and-set e state "**")

(= key-code KeyCodes.I) (surround-and-set e state "*" 1)
Expand Down

0 comments on commit d59ab43

Please sign in to comment.