Skip to content

Commit

Permalink
Merge pull request #1746 from neotyk/fix/use-block-remove
Browse files Browse the repository at this point in the history
Fix: use `:block/remove`
  • Loading branch information
filipesilva authored Oct 19, 2021
2 parents ec8732a + ea39d0e commit 773f593
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 249 deletions.
35 changes: 0 additions & 35 deletions src/cljc/athens/common_events.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,6 @@
:drag-target drag-target}}))


(defn build-selected-delete-event
"Builds `:datascript/selected-delete` event with:
- uids : The uids of blocks to be deleted "
[last-tx uids]
(let [event-id (utils/gen-event-id)]
{:event/id event-id
:event/last-tx last-tx
:event/type :datascript/selected-delete
:event/args {:uids uids}}))


(defn build-block-open-event
"Builds `:datascript/block-open` event with:
- block-uid : The uid of block to be opened
Expand All @@ -545,30 +534,6 @@
:internal-representation internal-representation}}))


(defn build-delete-only-child-event
"Builds `:datascript/delete-only-child` event with:
- uid : The uid of block to delete"
[last-tx uid]
(let [event-id (utils/gen-event-id)]
{:event/id event-id
:event/last-tx last-tx
:event/type :datascript/delete-only-child
:event/args {:uid uid}}))


(defn build-delete-merge-block-event
"Builds `:datascript/delete-merge-block` event with:
- uid : The uid of block to delete
- value: The text content of the block"
[last-tx uid value]
(let [event-id (utils/gen-event-id)]
{:event/id event-id
:event/last-tx last-tx
:event/type :datascript/delete-merge-block
:event/args {:uid uid
:value value}}))


;; - presence events

(defn build-presence-hello-event
Expand Down
22 changes: 16 additions & 6 deletions src/cljc/athens/common_events/graph/schema.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
(def atomic-op-types
[:enum
:block/new ;
:block/save
:block/save ;
:block/open
:block/remove
:block/remove ;
:block/move
:page/new ;
:page/rename
Expand Down Expand Up @@ -50,6 +50,13 @@
[:old-string string?]]]])


(def op-block-remove
[:map
[:op/args
[:map
[:block-uid string?]]]])


(def op-page-new
[:map
[:op/args
Expand All @@ -65,18 +72,21 @@
[:block/new (mu/merge
op-type-atomic-common
op-block-new)]
[:page/new (mu/merge
op-type-atomic-common
op-page-new)]
[:block/save (mu/merge
op-type-atomic-common
op-block-save)]
[:block/remove (mu/merge
op-type-atomic-common
op-block-remove)]
[:page/new (mu/merge
op-type-atomic-common
op-page-new)]
[:composite/consequence [:ref ::composite-op]]]
::composite-op [:map
[:op/type [:enum :composite/consequence]]
[:op/atomic? false?]
[:op/trigger map?]
[:op/consequences [:vector [:ref ::atomic-op]]]]}}
[:op/consequences [:sequential [:ref ::atomic-op]]]]}}
::atomic-op])


Expand Down
3 changes: 2 additions & 1 deletion src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,8 @@
:backspace/delete-only-child
(fn [{:keys [db]} [_ uid]]
(log/debug ":backspace/delete-only-child:" (pr-str uid))
(let [event (common-events/build-delete-only-child-event (:remote/last-seen-tx db) uid)]
(let [op (graph-ops/build-block-remove-op @db/dsdb uid)
event (common-events/build-atomic-event (:remote/last-seen-tx db) op)]
{:fx [[:dispatch [:resolve-transact-forward event]]
[:dispatch [:editing/uid nil]]]})))

Expand Down
17 changes: 10 additions & 7 deletions src/cljs/athens/events/selection.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(ns athens.events.selection
(:require
[athens.common-events :as common-events]
[athens.db :as db]
[re-frame.core :as rf]))
[athens.common-events :as common-events]
[athens.common-events.graph.composite :as composite-ops]
[athens.common-events.graph.ops :as graph-ops]
[athens.db :as db]
[re-frame.core :as rf]))


(rf/reg-event-db
Expand Down Expand Up @@ -46,10 +48,11 @@
(let [selected-uids (get-in db [:selection :items])
sanitized-uids (map (comp first db/uid-and-embed-id) selected-uids)]
(js/console.debug ::delete "args" selected-uids)
(let [event (common-events/build-selected-delete-event (:remote/last-seen-tx db)
sanitized-uids)]
{:fx [[:dispatch-n [[:transact-and-forward event]
[:editing/uid nil]]]]
(let [ops (map #(graph-ops/build-block-remove-op @db/dsdb %) sanitized-uids)
composite-op (composite-ops/make-consequence-op {:op/type :selection/delete} ops)
event (common-events/build-atomic-event (:remote/last-seen-tx db) composite-op)]
{:fx [[:dispatch-n [[:resolve-transact-forward event]
[:editing/uid nil]]]]
:db (assoc-in db [:selection :items] [])}))))


Expand Down
200 changes: 0 additions & 200 deletions test/athens/common_events/block_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1268,113 +1268,6 @@
(t/is (= 2 (count union-set))))))))


(t/deftest selected-delete-test
"Basic Case:
Start with :
-a
-b
-c
-d
End:
-a
-d"

(t/testing "Delete some blocks"
(let [block-1-uid "block-1-uid"
block-1-text "a"
block-2-uid "block-2-uid"
block-2-text "b"
block-3-uid "block-3-uid"
block-3-text "c"
block-4-uid "block-4-uid"
block-4-text "d"
setup-txs [{:node/title "test page"
:block/uid "page-uid"
:block/children [{:block/uid block-1-uid
:block/string block-1-text
:block/order 0
:block/children {:block/uid block-2-uid
:block/string block-2-text
:block/order 0
:block/children []}}
{:block/uid block-3-uid
:block/string block-3-text
:block/order 1
:block/children []}
{:block/uid block-4-uid
:block/string block-4-text
:block/order 2
:block/children []}]}]]


(d/transact! @fixture/connection setup-txs)
(let [uids [block-2-uid block-3-uid]
parent-block (common-db/get-block @@fixture/connection [:block/uid "page-uid"])
block-1 (common-db/get-block @@fixture/connection [:block/uid block-1-uid])
block-4 (common-db/get-block @@fixture/connection [:block/uid block-4-uid])
selected-delete-event (common-events/build-selected-delete-event -1
uids)
selected-delete-txs (resolver/resolve-event-to-tx @@fixture/connection selected-delete-event)]
(t/is (= 3 (-> parent-block :block/children count)))
(t/is (= 0 (:block/order block-1)))
(t/is (= 2 (:block/order block-4)))

(d/transact! @fixture/connection selected-delete-txs)
(let [parent-block (common-db/get-block @@fixture/connection [:block/uid "page-uid"])
block-1 (common-db/get-block @@fixture/connection [:block/uid block-1-uid])
block-4 (common-db/get-block @@fixture/connection [:block/uid block-4-uid])]
(t/is (= 2 (-> parent-block :block/children count)))
(t/is (= 0 (:block/order block-1)))
(t/is (= 1 (:block/order block-4)))))))

(t/testing "Replace block refs"
(let [block-1-uid "block-1-uid"
block-1-text "a"
block-2-uid "block-2-uid"
block-2-text "b"
block-3-uid "block-3-ui"
block-3-text (str "((" block-1-uid "))")
block-4-uid "block-4-uid"
block-4-text (str "((" block-2-uid "))")
block-5-uid "block-5-uid"
block-5-text (str "((" block-1-uid "))" " test " "((" block-2-uid "))")
setup-txs [{:node/title "test page"
:block/uid "page-uid"
:block/children [{:block/uid block-1-uid
:block/string block-1-text
:block/order 0
:block/children {:block/uid block-2-uid
:block/string block-2-text
:block/order 0
:block/children []}}
{:block/uid block-3-uid
:block/string block-3-text
:block/order 1
:block/children []}
{:block/uid block-4-uid
:block/string block-4-text
:block/order 2
:block/children []}
{:block/uid block-5-uid
:block/string block-5-text
:block/order 3
:block/children []}]}]]


(d/transact! @fixture/connection setup-txs)
(let [uids [block-1-uid block-2-uid]
selected-delete-event (common-events/build-selected-delete-event -1
uids)
selected-delete-txs (resolver/resolve-event-to-tx @@fixture/connection selected-delete-event)]
(d/transact! @fixture/connection selected-delete-txs)
(let [block-3 (common-db/get-block @@fixture/connection [:block/uid block-3-uid])
block-4 (common-db/get-block @@fixture/connection [:block/uid block-4-uid])
block-5 (common-db/get-block @@fixture/connection [:block/uid block-5-uid])]
(t/is (= "a" (:block/string block-3)))
(t/is (= "b" (:block/string block-4)))
(t/is (= "a test b" (:block/string block-5))))))))


(t/deftest paste-internal-event
(let [block-1-uid "test-block-1-uid"
block-2-uid "test-block-2-uid"
Expand Down Expand Up @@ -1408,96 +1301,3 @@
(:block/order reindexed-block)))
(t/is (= "Copy-Paste test block"
(:block/string pasted-block)))))))


(t/deftest delete-only-child
(let [block-uid "block-uid"
page-uid "page-uid"
setup-tx [{:db/id -1
:node/title "test page"
:block/uid page-uid
:block/children [{:db/id -2
:block/uid block-uid
:block/string ""
:block/order 0}]}]]

(d/transact! @fixture/connection setup-tx)
(let [{uid :block/uid} (common-db/get-block @@fixture/connection [:block/uid page-uid])]
(t/is (= page-uid uid)
"check if setup-tx is added"))

(let [delete-only-child-event (common-events/build-delete-only-child-event -1 block-uid)
delete-only-child-tx (resolver/resolve-event-to-tx @@fixture/connection delete-only-child-event)]
(d/transact! @fixture/connection delete-only-child-tx)
(let [page (common-db/get-block @@fixture/connection [:block/uid page-uid])]
(t/is (empty? (:block/children page))
"check if the empty, only child is deleted")))))


(t/deftest delete-merge-block
(t/testing "Deleting and merging blocks"
(let [block-uid "block-uid-2"
page-uid "page-uid"
value "next"
setup-tx [{:db/id -1
:node/title "test page"
:block/order 0
:block/uid page-uid
:block/open true
:block/children [{:db/id -2
:block/uid "block-uid-1"
:block/string "previous"
:block/open true
:block/order 0}
{:db/id -3
:block/uid block-uid
:block/string value
:block/order 1}]}]]

(d/transact! @fixture/connection setup-tx)
(let [{uid :block/uid} (common-db/get-block @@fixture/connection [:block/uid page-uid])]
(t/is (= page-uid uid)
"check if setup-tx is added"))

(let [delete-merge-block-event (common-events/build-delete-merge-block-event -1 block-uid value)
delete-merge-block-tx (resolver/resolve-event-to-tx @@fixture/connection delete-merge-block-event)]
(d/transact! @fixture/connection delete-merge-block-tx)
(let [{children :block/children} (d/pull @@fixture/connection '[{:block/children [:block/string]}] [:block/uid page-uid])]
(t/is (= 1 (count children))
"check if the second child is deleted")
(t/is (= "previousnext" (-> children
first
:block/string))
"check if the content of the two blocks are merged")))))

(t/testing "Replacing block refs"
(let [page-uid "page-uid"
block-uid-1 "block-uid-1"
block-str-1 "a"
block-uid-2 "block-uid-2"
block-str-2 (str "((" block-uid-1 "))")
setup-tx [{:db/id -1
:node/title "test page"
:block/order 0
:block/uid page-uid
:block/children [{:db/id -2
:block/uid block-uid-1
:block/string block-str-1
:block/order 0}
{:db/id -3
:block/uid block-uid-2
:block/string block-str-2
:block/order 1}]}]]

(d/transact! @fixture/connection setup-tx)
(let [{uid :block/uid} (common-db/get-block @@fixture/connection [:block/uid page-uid])]
(t/is (= page-uid uid)
"check if setup-tx is added"))

(let [delete-merge-block-event (common-events/build-delete-merge-block-event -1 block-uid-1 block-str-1)
delete-merge-block-tx (resolver/resolve-event-to-tx @@fixture/connection delete-merge-block-event)]
(d/transact! @fixture/connection delete-merge-block-tx)

(let [{string :block/string} (common-db/get-block @@fixture/connection [:block/uid block-uid-2])]
(t/is (= "a" string)
"check if the block refs is replaced with text content"))))))

1 comment on commit 773f593

@vercel
Copy link

@vercel vercel bot commented on 773f593 Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.