refactor: switch back to store properties directly in plain-text

This commit is contained in:
Tienson Qin
2021-04-14 10:51:14 +08:00
parent 073e0cc554
commit e873690a8b
7 changed files with 74 additions and 44 deletions

View File

@@ -253,13 +253,9 @@
(defn- wrap-parse-block
[{:block/keys [content format] :as block}]
(let [ast (mldoc/->edn (string/trim content) (mldoc/default-config format))
properties? (contains? #{"properties" "property_drawer"}
(when-let [type (first (ffirst ast))]
(string/lower-case type)))
content' (if properties?
(string/trim content)
(str (config/get-block-pattern format) " "
(string/triml content)))]
heading? (= "Paragraph" (first (ffirst ast)))
content' (str (config/get-block-pattern format) (if heading? " " "\n")
(string/triml content))]
(-> (block/parse-block (assoc block :block/content content'))
(dissoc :block/top?
:block/block-refs-count)
@@ -749,12 +745,19 @@
repo (state/get-current-repo)]
(when repo
(when-let [block (db/entity [:block/uuid block-id])]
(let [properties (:block/properties block)
(let [format (:block/format block)
content (:block/content block)
markdown? (= format :markdown)
properties (:block/properties block)
properties (if value ; add
(assoc properties key value)
(dissoc properties key))
content (if value
(text/insert-property content key value)
(text/remove-property content key))
block (outliner-core/block {:block/uuid block-id
:block/properties properties})]
:block/properties properties
:block/content content})]
(outliner-core/save-node block)
(let [opts {:key :block/change
:data [block]}]

View File

@@ -66,26 +66,21 @@
;; instead of being attributes of properties.
;; which might improve the db performance, we can improve it later
(defn- with-timestamp
[data]
(prn {:data data})
[m]
(let [updated-at (util/time-ms)
m (-> data
(dissoc :block/children :block/dummy? :block/level :block/meta)
(util/remove-nils))
properties (assoc (:block/properties m)
:id (:block/uuid data)
:id (:block/uuid m)
:updated-at updated-at)
properties (if-let [created-at (get properties :created-at)]
properties
(assoc properties :created-at updated-at))
m (assoc m :block/properties properties)
page-id (or (get-in data [:block/page :db/id])
(:db/id (:block/page (db/entity (:db/id data)))))
page-id (or (get-in m [:block/page :db/id])
(:db/id (:block/page (db/entity (:db/id m)))))
page (db/entity page-id)
page-properties (:block/properties page)
page-tx {:db/id page-id
:block/properties (assoc page-properties
:id (:block/uuid page)
:updated-at updated-at
:created-at (get page-properties :created-at updated-at))}]
[m page-tx]))
@@ -135,9 +130,16 @@
(-save [this txs-state]
(assert (ds/outliner-txs-state? txs-state)
"db should be satisfied outliner-tx-state?")
(let [[m page-tx] (with-timestamp (:data this))]
(swap! txs-state conj m page-tx)
m))
(let [m (-> (:data this)
(dissoc :block/children :block/dummy? :block/level :block/meta)
(util/remove-nils))]
(swap! txs-state conj m)
;; TODO: enable for the database-only version
;; (let [[m page-tx] (with-timestamp (:data this))]
;; (swap! txs-state conj m page-tx)
;; m)
)
)
(-del [this txs-state]
(assert (ds/outliner-txs-state? txs-state)

View File

@@ -13,16 +13,17 @@
[page]
(file/sync-to-file page))
(defn updated-properties-hook
[properties]
(metadata-handler/update-properties! properties))
;; (defn updated-properties-hook
;; [properties]
;; (metadata-handler/update-properties! properties))
(defn invoke-hooks
[tx-report]
(let [{:keys [blocks pages properties]} (ds-report/get-blocks-and-pages tx-report)]
(doseq [p (seq pages)] (updated-page-hook p))
(doseq [b (seq blocks)] (updated-block-hook b))
(when (seq properties) (updated-properties-hook properties))))
;; (when (seq properties) (updated-properties-hook properties))
))
(defn after-transact-pipelines
[{:keys [_db-before _db-after _tx-data _tempids _tx-meta] :as tx-report}]

View File

@@ -122,8 +122,7 @@
(def hidden-properties
(set/union
#{:id :custom-id :background-color
:created-at :updated-at}
#{:id :custom-id :background-color :heading}
config/markers))
(defn properties-hidden?
@@ -239,9 +238,10 @@
(defn insert-property
[content key value]
(when (and (not (string/blank? key))
(not (string/blank? value)))
(let [key (string/lower-case key)
(when (and (not (string/blank? (name key)))
(not (string/blank? (str value))))
(let [key (string/lower-case (name key))
value (str value)
[title body] (util/safe-split-first "\n" content)]
(if-not (contains-properties? content)
(let [properties (build-properties-str {key value})]
@@ -254,7 +254,6 @@
(string/starts-with? (string/upper-case (string/triml l)) ":end:")))
properties (take-while properties? properties-and-body)
exists? (atom false)
new-line (util/format ":%s: %s" key value)
new-properties (doall
(map (fn [l]
(if (string/starts-with? (string/triml l) (str ":" key ":"))
@@ -272,6 +271,30 @@
(->> (concat title-lines new-properties body)
(string/join "\n")))))))
(defn remove-property
[content key]
(when (not (string/blank? (name key)))
(let [key (string/lower-case (name key))
[title body] (util/safe-split-first "\n" content)]
(if-not (contains-properties? content)
content
(let [lines (string/split-lines content)
[title-lines properties-and-body] (split-with (fn [l] (not (string/starts-with? (string/upper-case (string/triml l)) ":PROPERTIES:"))) lines)
properties? (fn [l]
(or
(string/starts-with? (string/triml l) ":") ; kv
(string/starts-with? (string/upper-case (string/triml l)) ":end:")))
properties (take-while properties? properties-and-body)
exists? (atom false)
new-properties (->> (map (fn [l]
(if (string/starts-with? (string/triml l) (str ":" key ":"))
nil
l)) properties)
(remove nil?))
body (drop-while properties? properties-and-body)]
(->> (concat title-lines new-properties body)
(string/join "\n")))))))
(defn build-data-value
[col]
(let [items (map (fn [item] (str "\"" item "\"")) col)]

View File

@@ -317,14 +317,15 @@
(.setSelectionRange input pos pos)))
#?(:cljs
(defn get-caret-pos
[input]
(try
(let [pos ((gobj/get caret-pos "position") input)]
(set! pos -rect (.. input (getBoundingClientRect) (toJSON)))
(bean/->clj pos))
(catch js/Error e
(js/console.error e)))))
(defn get-caret-pos
[input]
(when input
(try
(let [pos ((gobj/get caret-pos "position") input)]
(set! pos -rect (.. input (getBoundingClientRect) (toJSON)))
(bean/->clj pos))
(catch js/Error e
(js/console.error e))))))
(defn get-first-or-last-line-pos
[input]