refactor: block && page properties

pull/1664/head
Tienson Qin 2021-04-15 12:02:35 +08:00
parent d0abccde62
commit da3970d531
4 changed files with 65 additions and 12 deletions

View File

@ -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?
}
)

View File

@ -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)

View File

@ -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))))

View File

@ -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)