diff --git a/src/main/frontend/db/conn.cljs b/src/main/frontend/db/conn.cljs index 19ef0a24ac..8e9f311656 100644 --- a/src/main/frontend/db/conn.cljs +++ b/src/main/frontend/db/conn.cljs @@ -73,6 +73,10 @@ [] (d/create-conn db-schema/outline-schema)) +(defn get-outliner-conn + [] + nil) + (defn start! ([me repo] (start! me repo {})) diff --git a/src/main/frontend/db/outliner.cljs b/src/main/frontend/db/outliner.cljs index a38362491a..73f7a18b95 100644 --- a/src/main/frontend/db/outliner.cljs +++ b/src/main/frontend/db/outliner.cljs @@ -10,6 +10,16 @@ @conn id)] (ffirst r))) +(defn get-by-parent-&-left + [conn parent-id left-id] + (let [r (d/q '[:find (pull ?a [*]) + :in $ ?p ?l + :where + [?a :block/left-id ?l] + [?a :block/parent-id ?p]] + @conn parent-id left-id)] + (ffirst r))) + (defn get-by-parent-id [conn id] (let [r (d/q '[:find (pull ?a [*]) @@ -19,11 +29,7 @@ @conn id)] (flatten r))) -(defn get-by-left-id - [conn id] - (let [r (d/q '[:find (pull ?a [*]) - :in $ ?id - :where - [?a :block/left-id ?id]] - @conn id)] - (ffirst r))) \ No newline at end of file +(defn save-block + [conn block-m] + (d/transact! conn [block-m])) + diff --git a/src/main/frontend/modules/outliner/core.cljs b/src/main/frontend/modules/outliner/core.cljs index 59addebe15..196410187c 100644 --- a/src/main/frontend/modules/outliner/core.cljs +++ b/src/main/frontend/modules/outliner/core.cljs @@ -1,3 +1,62 @@ (ns frontend.modules.outliner.core - (:require [frontend.modules.outliner.tree :as tree])) + (:require [frontend.modules.outliner.tree :as tree] + [frontend.db.outliner :as db-outliner] + [frontend.db.conn :as conn])) +(defrecord Block [id data]) + +(defn get-block-from-db + ([id] + (let [c (conn/get-outliner-conn) + r (db-outliner/get-by-id c id)] + (when r (->Block (:block/id r) r)))) + ([parent-id left-id] + (let [c (conn/get-outliner-conn) + r (db-outliner/get-by-parent-&-left c parent-id left-id)] + (when r (->Block (:block/id r) r))))) + +(extend-type Block + tree/INode + (tree/-get-id [this] + (:id this)) + + (tree/-get-parent-id [this] + (get-in this [:data :block/parent-id])) + + (tree/-set-parent-id [this parent-id] + (update this :data assoc :block/parent-id parent-id)) + + (tree/-get-left-id [this] + (get-in this [:data :block/left-id])) + + (tree/-set-left-id [this left-id] + (update this :data assoc :block/left-id left-id)) + + (tree/-get-parent [this] + (let [parent-id (tree/-get-parent-id this)] + (get-block-from-db parent-id))) + + (tree/-get-left [this] + (let [left-id (tree/-get-left-id this)] + (get-block-from-db left-id))) + + (tree/-get-right [this] + (let [left-id (tree/-get-id this) + parent-id (tree/-get-parent-id this)] + (get-block-from-db parent-id left-id))) + + (tree/-get-down [this] + (let [parent-id (tree/-get-id this)] + (get-block-from-db parent-id parent-id))) + + (tree/-save [this] + (let [conn (conn/get-outliner-conn)] + (db-outliner/save-block conn (:data this)))) + + (tree/-get-children [this] + (let [first-child (tree/-get-down this)] + (loop [current first-child + children [first-child]] + (if-let [node (tree/-get-right current)] + (recur node (conj children node)) + children))))) \ No newline at end of file diff --git a/src/test/frontend/db/outliner_test.cljs b/src/test/frontend/db/outliner_test.cljs index 097d75fc4c..6131ae9978 100644 --- a/src/test/frontend/db/outliner_test.cljs +++ b/src/test/frontend/db/outliner_test.cljs @@ -19,6 +19,19 @@ (is (= parent-id (:block/parent-id result))) (is (= left-id (:block/left-id result))))) +(deftest test-get-by-parent-&-left + (let [conn (conn/create-outliner-db) + data [{:block/id "1"} + {:block/id "2" + :block/parent-id "1" + :block/left-id "1"} + {:block/id "3" + :block/parent-id "1" + :block/left-id "2"}] + _ (d/transact! conn data) + result (outliner/get-by-parent-&-left conn "1" "2")] + (is "3" (:block/id result)))) + (deftest test-get-by-parent-id (let [conn (conn/create-outliner-db) data [{:block/id "1"} @@ -31,19 +44,3 @@ _ (d/transact! conn data) result (outliner/get-by-parent-id conn "1")] (is (= ["2" "3"] (mapv :block/id result))))) - -(deftest test-get-by-left-id - (let [conn (conn/create-outliner-db) - data [{:block/id "1"} - {:block/id "2" - :block/parent-id "1" - :block/left-id "1"} - {:block/id "3" - :block/parent-id "1" - :block/left-id "2"}] - _ (d/transact! conn data) - result (outliner/get-by-left-id conn "1")] - (is "2" (:block/id result)))) - - -