Procházet zdrojové kódy

enhance: import float values with type :number

They were imported as :default b/c graph-parser didn't handle them.
Part of LOG-2985
Gabriel Horner před 1 rokem
rodič
revize
487c86ac83

+ 45 - 33
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -93,13 +93,6 @@
                     (keep #(convert-tag-to-class % tag-classes) tags)))))
     block))
 
-(def ignored-built-in-properties
-  "Ignore built-in properties that are already imported or not supported in db graphs"
-  ;; Already imported via a datascript attribute i.e. have :attribute on property config
-  [:tags :alias :collapsed
-   ;; Not supported as they have been ignored for a long time and cause invalid built-in pages
-   :now :later :doing :done :canceled :cancelled :in-progress :todo :wait :waiting])
-
 (defn- text-with-refs?
   "Detects if a property value has text with refs e.g. `#Logseq is #awesome`
   instead of `#Logseq #awesome`. If so the property type is :default instead of :page"
@@ -220,11 +213,6 @@
         (swap! ignored-properties conj {:property prop :value val :schema (get property-changes prop)})
         nil))))
 
-(defn- user-property-value-to-ignore?
-  [val]
-  ;; Ignore blank values as they were usually generated by templates
-  (and (string? val) (string/blank? val)))
-
 (defn- update-user-property-values
   [props prop-name->uuid properties-text-values
    {:keys [property-schemas] :as import-state}
@@ -234,14 +222,13 @@
                (if (get-in property-changes [prop :type])
                  (when-let [val' (handle-changed-property val prop prop-name->uuid properties-text-values import-state options)]
                    [prop val'])
-                 (when-not (user-property-value-to-ignore? val)
-                   [prop
-                    (if (set? val)
-                      (if (= :default (get-in @property-schemas [prop :type]))
-                        (get properties-text-values prop)
+                 [prop
+                  (if (set? val)
+                    (if (= :default (get-in @property-schemas [prop :type]))
+                      (get properties-text-values prop)
                       ;; assume for now a ref's :block/name can always be translated by lc helper
-                        (set (map (comp prop-name->uuid common-util/page-name-sanity-lc) val)))
-                      val)]))))
+                      (set (map (comp prop-name->uuid common-util/page-name-sanity-lc) val)))
+                    val)])))
        (into {})))
 
 (defn- cached-prop-name->uuid [db page-names-to-uuids k]
@@ -280,23 +267,49 @@
           (merge (update-user-property-values user-properties prop-name->uuid properties-text-values import-state options))
           (update-keys prop-name->uuid)))))
 
+(def ignored-built-in-properties
+  "Ignore built-in properties that are already imported or not supported in db graphs"
+  ;; Already imported via a datascript attribute i.e. have :attribute on property config
+  [:tags :alias :collapsed
+   ;; Not supported as they have been ignored for a long time and cause invalid built-in pages
+   :now :later :doing :done :canceled :cancelled :in-progress :todo :wait :waiting])
+
+(defn- pre-update-properties
+  "Updates page and block properties before their property types are inferred"
+  [properties property-classes]
+  (let [dissoced-props (concat ignored-built-in-properties
+                               ;; TODO: Add import support for these dissoced built-in properties
+                               [:title :id :created-at :updated-at
+                                :card-last-interval :card-repeats :card-last-reviewed :card-next-schedule
+                                :card-ease-factor :card-last-score]
+                               property-classes)]
+    (->> (apply dissoc properties dissoced-props)
+         (keep (fn [[prop val]]
+                 (if (not (contains? db-property/built-in-properties-keys prop))
+                  ;; only update user properties
+                   (if (string? val)
+                    ;; Ignore blank values as they were usually generated by templates
+                     (when-not (string/blank? val)
+                       [prop
+                       ;; handle float strings b/c graph-parser doesn't
+                        (or (parse-double val) val)])
+                     [prop val])
+                   [prop val])))
+         (into {}))))
+
 (defn- handle-page-properties
-  "Infers property schemas, update :block/properties and remove deprecated
-  property attributes. Only infers property schemas on user properties as
-  built-in ones shouldn't change"
+  "Handles modifying :block/properties, updating classes from property-classes
+  and removing any deprecated property related attributes. Before updating most
+  :block/properties, their property schemas are inferred as that can affect how
+  a property is updated. Only infers property schemas on user properties as
+  built-in ones must not change"
   [{:block/keys [properties] :as block} db page-names-to-uuids refs
-   {:keys [import-state macros property-classes log-fn] :as options}]
+   {:keys [import-state macros property-classes] :as options}]
   (-> (if (seq properties)
         (let [classes-from-properties (->> (select-keys properties property-classes)
                                            (mapcat (fn [[_k v]] (if (coll? v) v [v])))
                                            distinct)
-              dissoced-props (concat ignored-built-in-properties
-                                     ;; TODO: Add import support for these dissoced built-in properties
-                                     [:title :id :created-at :updated-at
-                                      :card-last-interval :card-repeats :card-last-reviewed :card-next-schedule
-                                      :card-ease-factor :card-last-score]
-                                     property-classes)
-              properties' (apply dissoc properties dissoced-props)
+              properties' (pre-update-properties properties property-classes)
               properties-to-infer (if (:template properties')
                                     ;; Ignore template properties as they don't consistently have representative property values
                                     {}
@@ -305,11 +318,10 @@
               (->> properties-to-infer
                    (keep (fn [[prop val]]
                            (when-let [property-change
-                                      (and (not (user-property-value-to-ignore? val))
-                                           (infer-property-schema-and-get-property-change val prop (get (:block/properties-text-values block) prop) refs (:property-schemas import-state) macros))]
+                                      (infer-property-schema-and-get-property-change val prop (get (:block/properties-text-values block) prop) refs (:property-schemas import-state) macros)]
                              [prop property-change])))
                    (into {}))
-              _ (when (seq property-changes) (log-fn :prop-changes property-changes))
+              ;; _ (when (seq property-changes) (prn :prop-changes property-changes))
               options' (assoc options :property-changes property-changes)]
           (cond-> (assoc-in block [:block/properties]
                             (update-properties properties' db page-names-to-uuids