Browse Source

Page property read and write works for property pages like progress.md

Block-level properties can also be set
Gabriel Horner 2 years ago
parent
commit
1b09efe04c

+ 33 - 31
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -526,18 +526,20 @@
                                                       (when (coll? refs)
                                                         refs))))
                                             (map :block/original-name))
-                         block {:uuid id
-                                :content content
-                                :level 1
-                                :properties properties
-                                :properties-order (vec properties-order)
-                                :properties-text-values properties-text-values
-                                :invalid-properties invalid-properties
-                                :refs property-refs
-                                :pre-block? true
-                                :unordered true
-                                :macros (extract-macros-from-ast body)
-                                :body body}
+                         block (merge
+                                {:uuid id
+                                 :content content
+                                 :level 1
+                                 :properties properties
+                                 :properties-order (vec properties-order)
+                                 :properties-text-values properties-text-values
+                                 :invalid-properties invalid-properties
+                                 :refs property-refs
+                                 :pre-block? true
+                                 :unordered true
+                                 :macros (extract-macros-from-ast body)
+                                 :body body}
+                                (select-keys pre-block-properties [:block/property-schema]))
                          block (with-page-block-refs block false supported-formats db date-formatter)]
                      (block-keywordize block))
                    (select-keys first-block [:block/format :block/page]))
@@ -567,21 +569,21 @@
                        :level (if unordered? (:level block) 1))
                 block)
         block (cond->
-                (-> (assoc block
-                           :uuid id
-                           :refs ref-pages-in-properties
-                           :format format
-                           :meta pos-meta)
-                    (dissoc :size))
-                (or (seq (:properties properties)) markdown-heading?)
-                (assoc :properties (with-heading-property (:properties properties) markdown-heading? (:size block) (:level block))
-                       :properties-text-values (:properties-text-values properties)
-                       :properties-order (vec (:properties-order properties)))
-
-                (seq (:invalid-properties properties))
-                (assoc :invalid-properties (:invalid-properties properties))
-                (some? (:block/property-schema properties))
-                (assoc :block/property-schema (:block/property-schema properties)))
+               (-> (assoc block
+                          :uuid id
+                          :refs ref-pages-in-properties
+                          :format format
+                          :meta pos-meta)
+                   (dissoc :size))
+               (or (seq (:properties properties)) markdown-heading?)
+               (assoc :properties (with-heading-property (:properties properties) markdown-heading? (:size block) (:level block))
+                      :properties-text-values (:properties-text-values properties)
+                      :properties-order (vec (:properties-order properties)))
+
+               (seq (:invalid-properties properties))
+               (assoc :invalid-properties (:invalid-properties properties))
+               (seq (select-keys properties [:block/property-schema]))
+               (merge (select-keys properties [:block/property-schema])))
         block (if (get-in block [:properties :collapsed])
                 (-> (assoc block :collapsed? true)
                     (update :properties (fn [m] (dissoc m :collapsed)))
@@ -636,20 +638,20 @@
 
                 (edn-comment-block? block)
                 (let [attributes (-> (second block)
-                                     (string/replace #"^<!--EDN\n|-->$" "")
+                                     (string/replace #"^<!--EDN|-->$" "")
                                      edn/read-string)]
-                  (prn :EDN block attributes)
+                  ; (prn :EDN block attributes)
                   (recur headings (rest blocks) timestamps attributes body))
 
                 (gp-property/properties-ast? block)
-                (let [_ (prn :PROP block properties)
+                (let [;_ (prn :PROP block properties)
                       properties (merge properties
                                         (extract-properties (second block) (assoc user-config :format format)))]
 
                   (recur headings (rest blocks) timestamps properties body))
 
                 (heading-block? block)
-                (let [_ (prn :HEADING block properties)
+                (let [;_ (prn :HEADING block properties)
                       block' (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)
                       block'' (assoc block' :macros (extract-macros-from-ast (cons block body)))]
                   (recur (conj headings block'') (rest blocks) {} {} []))

+ 3 - 1
src/main/frontend/components/property/schema.cljs

@@ -6,6 +6,8 @@
             [frontend.handler.property :as property-handler]
             [rum.core :as rum]
             [frontend.context.i18n :refer [t]]
+            [frontend.db :as db]
+            [frontend.state :as state]
             [frontend.handler.notification :as notification]))
 
 (rum/defc property-item
@@ -79,7 +81,7 @@
 
 (rum/defc schema
   [entity]
-  (let [schema (:block/property-schema entity)]
+  (let [schema (:block/property-schema (db/get-pre-block (state/get-current-repo) (:db/id entity)))]
     [:div.property-schema
      (schema-type entity schema)
      (schema-number-range entity schema)

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -2127,7 +2127,8 @@
 (defn property-value-on-chosen-handler
   [element-id q property]
   (fn [property-value]
-    (let [schema (:block/property-schema (db/pull [:block/name property]))
+    (let [schema (:block/property-schema (db/get-pre-block (state/get-current-repo)
+                                                           (:db/id (db/entity [:block/name property]))))
           final-value (or property-value q)
           [success? property-value-or-error] (property-handler/validate schema final-value)]
       (if success?

+ 14 - 3
src/main/frontend/handler/property.cljs

@@ -3,6 +3,7 @@
   (:require [frontend.state :as state]
             [frontend.db :as db]
             [clojure.string :as string]
+            [goog.string :as gstring]
             [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser.util.page-ref :as page-ref]))
 
@@ -13,10 +14,20 @@
 ;; TODO spec
 (defn set-property-schema!
   [entity key value]
-  (let [schema (assoc (:block/property-schema entity) key value)]
+  (let [pre-block (db/get-pre-block (state/get-current-repo) (:db/id entity))
+        schema (assoc (:block/property-schema pre-block) key value)
+        content (str (:block/content pre-block))
+        edn-comment (gstring/format "<!--EDN %s -->" (pr-str {:block/property-schema schema}))
+        new-content (cond
+                      (string/includes? content "<!--EDN")
+                      (string/replace-first content #"<!--EDN.*-->" edn-comment)
+                      (seq content)
+                      (str content "\n" edn-comment)
+                      :else
+                      edn-comment)]
     (db/transact! (state/get-current-repo)
-      [{:db/id (:db/id entity)
-        :block/property-schema schema}])))
+                  [{:db/id (:db/id pre-block)
+                    :block/content new-content}])))
 
 (defn validate
   "Check whether the `value` validate against the `schema`."