From da3970d531411039c00be6d2bd672ef6d28d568b Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 15 Apr 2021 12:02:35 +0800 Subject: [PATCH] refactor: block && page properties --- src/main/frontend/db_schema.cljs | 19 +++++++--- src/main/frontend/format/block.cljs | 2 +- src/main/frontend/handler/editor.cljs | 37 ++++++++++++++++++-- src/main/frontend/modules/outliner/core.cljs | 19 +++++++--- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/main/frontend/db_schema.cljs b/src/main/frontend/db_schema.cljs index 2d84b0485..cf99914f2 100644 --- a/src/main/frontend/db_schema.cljs +++ b/src/main/frontend/db_schema.cljs @@ -76,11 +76,6 @@ ;; whether block is collapsed :block/collapsed? {} - ;; block's children - :block/children {:db/valueType :db.type/ref - :db/cardinality :db.cardinality/many - :db/unique :db.unique/identity} - ;; scheduled day :block/scheduled {} @@ -123,3 +118,17 @@ :git/error {} }) + +(def retract-attributes + #{ + :block/refs + :block/path-refs + :block/tags + :block/alias + :block/marker + :block/priority + :block/scheduled + :block/deadline + :block/repeated? + } + ) diff --git a/src/main/frontend/format/block.cljs b/src/main/frontend/format/block.cljs index 53dd06a8d..c8e9cff09 100644 --- a/src/main/frontend/format/block.cljs +++ b/src/main/frontend/format/block.cljs @@ -471,7 +471,7 @@ (recur headings block-body' (rest blocks) timestamps properties last-pos last-level children)))) (do (when (seq block-body) - (reset! pre-block-body block-body)) + (reset! pre-block-body (reverse block-body))) (-> (reverse headings) safe-blocks))))] (let [first-block (first blocks) diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 48649e652..776dd6a3a 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -251,6 +251,37 @@ (not= current-id (cljs.core/uuid block-id)) (db/entity [:block/uuid (cljs.core/uuid block-id)]))) +(defn- attach-page-properties-if-exists! + [block] + (if (and (:block/pre-block? block) + (= "Properties" (ffirst (:block/body block)))) ; page properties + (let [page-properties (second (first (:block/body block))) + str->page (fn [n] {:block/name (string/lower-case n) :block/original-name n}) + refs (->> page-properties + (filter (fn [[_ v]] (coll? v))) + (vals) + (apply concat) + (set) + (map str->page) + (concat (:block/refs block)) + (util/distinct-by :block/name)) + {:keys [tags alias]} page-properties + page-tx (let [id (:db/id (:block/page block)) + retract-attributes (mapv (fn [attribute] + [:db/retract id attribute]) + [:block/properties :block/tags :block/alias]) + tx (cond-> {:db/id id + :block/properties page-properties} + (seq tags) + (assoc :block/tags (map str->page tags)) + (seq alias) + (assoc :block/alias (map str->page alias)))] + (conj retract-attributes tx))] + (assoc block + :block/refs refs + :db/other-tx page-tx)) + block)) + (defn- wrap-parse-block [{:block/keys [content format] :as block}] (let [ast (mldoc/->edn (string/trim content) (mldoc/default-config format)) @@ -260,8 +291,10 @@ content (string/triml content) content' (if properties? content - (str (config/get-block-pattern format) (if heading? " " "\n") content))] - (-> (block/parse-block (assoc block :block/content content')) + (str (config/get-block-pattern format) (if heading? " " "\n") content)) + block (block/parse-block (assoc block :block/content content')) + block (attach-page-properties-if-exists! block)] + (-> block (dissoc :block/top? :block/block-refs-count) (assoc :block/content content)))) diff --git a/src/main/frontend/modules/outliner/core.cljs b/src/main/frontend/modules/outliner/core.cljs index a414bed9f..b691fbe6d 100644 --- a/src/main/frontend/modules/outliner/core.cljs +++ b/src/main/frontend/modules/outliner/core.cljs @@ -1,6 +1,7 @@ (ns frontend.modules.outliner.core (:require [frontend.modules.outliner.tree :as tree] [frontend.db :as db] + [frontend.db-schema :as db-schema] [frontend.db.outliner :as db-outliner] [frontend.db.conn :as conn] [frontend.modules.outliner.utils :as outliner-u] @@ -31,8 +32,6 @@ r (db-outliner/get-by-id c (outliner-u/->block-lookup-ref id))] (when r (->Block r)))) -(def block-id-size 9) - (defn- get-by-parent-&-left [parent-id left-id] (some-> @@ -133,8 +132,20 @@ "db should be satisfied outliner-tx-state?") (let [m (-> (:data this) (dissoc :block/children :block/dummy? :block/level :block/meta) - (util/remove-nils))] - (swap! txs-state conj m) + (util/remove-nils)) + other-tx (:db/other-tx m)] + (when (seq other-tx) + (swap! txs-state (fn [txs] + (vec (concat txs other-tx))))) + + (when-let [id (:db/id (:data this))] + (swap! txs-state (fn [txs] + (vec + (concat txs + (map (fn [attribute] + [:db/retract id attribute]) + db-schema/retract-attributes)))))) + (swap! txs-state conj (dissoc m :db/other-tx)) ;; TODO: enable for the database-only version ;; (let [[m page-tx] (with-timestamp (:data this))] ;; (swap! txs-state conj m page-tx)