Просмотр исходного кода

refactor: hl-color updates to closed values

hl-page changes to :raw-number
Tienson Qin 1 год назад
Родитель
Сommit
245b342654

+ 13 - 2
deps/db/src/logseq/db/frontend/property.cljs

@@ -94,8 +94,19 @@
                                       :hide? true}}
                                       :hide? true}}
 
 
    :logseq.property.pdf/hl-type {:schema {:type :keyword :hide? true}}
    :logseq.property.pdf/hl-type {:schema {:type :keyword :hide? true}}
-   :logseq.property.pdf/hl-color {:schema {:type :default :hide? true}}
-   :logseq.property.pdf/hl-page {:schema {:type :number :hide? true}}
+   :logseq.property.pdf/hl-color
+   {:schema {:type :default}
+    :closed-values
+    (mapv (fn [[db-ident value]]
+            {:db-ident db-ident
+             :value value
+             :uuid (common-uuid/gen-uuid :db-ident-block-uuid db-ident)})
+          [[:logseq.property/color.yellow "yellow"]
+           [:logseq.property/color.red "red"]
+           [:logseq.property/color.green "green"]
+           [:logseq.property/color.blue "blue"]
+           [:logseq.property/color.purple "purple"]])}
+   :logseq.property.pdf/hl-page {:schema {:type :raw-number}}
    :logseq.property.pdf/hl-image {:schema {:type :entity :hide? true}}
    :logseq.property.pdf/hl-image {:schema {:type :entity :hide? true}}
    :logseq.property.pdf/hl-value {:schema {:type :map :hide? true}}
    :logseq.property.pdf/hl-value {:schema {:type :map :hide? true}}
    :logseq.property/order-list-type {:name :logseq.order-list-type
    :logseq.property/order-list-type {:name :logseq.order-list-type

+ 14 - 10
deps/db/src/logseq/db/frontend/property/build.cljs

@@ -34,6 +34,19 @@
     true
     true
     sqlite-util/block-with-timestamps))
     sqlite-util/block-with-timestamps))
 
 
+(defn closed-values->blocks
+  [property]
+  (map (fn [{uuid' :uuid :keys [db-ident value icon]}]
+         (cond->
+          (build-closed-value-block
+           uuid'
+           value
+           property
+           {:db-ident db-ident :icon icon})
+           true
+           (assoc :block/order (db-order/gen-key))))
+       (:closed-values property)))
+
 (defn build-closed-values
 (defn build-closed-values
   "Builds all the tx needed for property with closed values including
   "Builds all the tx needed for property with closed values including
    the hidden page and closed value blocks as needed"
    the hidden page and closed value blocks as needed"
@@ -43,16 +56,7 @@
                                                                                      :ref-type? true})
                                                                                      :ref-type? true})
                            property-attributes)]
                            property-attributes)]
     (into [property-tx]
     (into [property-tx]
-          (map (fn [{uuid' :uuid :keys [db-ident value icon]}]
-                 (cond->
-                  (build-closed-value-block
-                   uuid'
-                   value
-                   property
-                   {:db-ident db-ident :icon icon})
-                   true
-                   (assoc :block/order (db-order/gen-key))))
-               (:closed-values property)))))
+          (closed-values->blocks property))))
 
 
 (defn build-property-value-block
 (defn build-property-value-block
   "Builds a property value entity given a block map/entity, a property entity or
   "Builds a property value entity given a block map/entity, a property entity or

+ 1 - 1
deps/db/src/logseq/db/frontend/schema.cljs

@@ -2,7 +2,7 @@
   "Main datascript schemas for the Logseq app"
   "Main datascript schemas for the Logseq app"
   (:require [clojure.set :as set]))
   (:require [clojure.set :as set]))
 
 
-(def version 43)
+(def version 44)
 
 
 ;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
 ;; A page is a special block, a page can corresponds to multiple files with the same ":block/name".
 (def ^:large-vars/data-var schema
 (def ^:large-vars/data-var schema

+ 28 - 19
src/main/frontend/extensions/pdf/assets.cljs

@@ -158,23 +158,27 @@
           (println "[existed ref block]" ref-block)
           (println "[existed ref block]" ref-block)
           ref-block)
           ref-block)
         (let [text       (:text content)
         (let [text       (:text content)
-              properties (cond->
-                          {:block/tags :logseq.class/Pdf-annotation
-                           :logseq.property/ls-type  :annotation
-                           :logseq.property.pdf/hl-color (:color properties)
-                           :logseq.property/asset (:db/id pdf-block)
-                           :logseq.property.pdf/hl-page  page
-                           :logseq.property.pdf/hl-value hl}
-                           (:image content)
-                           (assoc :logseq.property.pdf/hl-type :area
-                                  :logseq.property.pdf/hl-image (:image content)))]
-          (when (string? text)
-            (editor-handler/api-insert-new-block!
-             text (merge {:block-uuid (:block/uuid pdf-block)
-                          :sibling? false
-                          :custom-uuid id
-                          :properties properties}
-                         (assoc insert-opts :edit-block? false)))))))))
+              colors     (:property/closed-values (db/entity :logseq.property.pdf/hl-color))
+              color-id   (some (fn [color] (when (= (:block/title color) (:color properties))
+                                             (:db/id color))) colors)]
+          (when color-id
+            (let [properties (cond->
+                              {:block/tags :logseq.class/Pdf-annotation
+                               :logseq.property/ls-type  :annotation
+                               :logseq.property.pdf/hl-color color-id
+                               :logseq.property/asset (:db/id pdf-block)
+                               :logseq.property.pdf/hl-page  page
+                               :logseq.property.pdf/hl-value hl}
+                               (:image content)
+                               (assoc :logseq.property.pdf/hl-type :area
+                                      :logseq.property.pdf/hl-image (:image content)))]
+              (when (string? text)
+                (editor-handler/api-insert-new-block!
+                 text (merge {:block-uuid (:block/uuid pdf-block)
+                              :sibling? false
+                              :custom-uuid id
+                              :properties properties}
+                             (assoc insert-opts :edit-block? false)))))))))))
 
 
 (defn ensure-ref-block!
 (defn ensure-ref-block!
   [pdf-current hl insert-opts]
   [pdf-current hl insert-opts]
@@ -285,8 +289,13 @@
   [highlight]
   [highlight]
   (when-let [block (db-model/get-block-by-uuid (:id highlight))]
   (when-let [block (db-model/get-block-by-uuid (:id highlight))]
     (when-let [color (get-in highlight [:properties :color])]
     (when-let [color (get-in highlight [:properties :color])]
-      (let [k (pu/get-pid :logseq.property.pdf/hl-color)]
-        (property-handler/set-block-property! (state/get-current-repo) (:block/uuid block) k color)))))
+      (let [k (pu/get-pid :logseq.property.pdf/hl-color)
+            color' (if (config/db-based-graph?)
+                     (let [colors     (:property/closed-values (db/entity :logseq.property.pdf/hl-color))]
+                       (some (fn [color-block] (when (= (:block/title color-block) color)
+                                                 (:db/id color-block))) colors))
+                     color)]
+        (property-handler/set-block-property! (state/get-current-repo) (:block/uuid block) k color')))))
 
 
 (defn unlink-hl-area-image$
 (defn unlink-hl-area-image$
   [^js _viewer current hl]
   [^js _viewer current hl]

+ 40 - 1
src/main/frontend/worker/db/migrate.cljs

@@ -144,6 +144,44 @@
       :logseq.property/hide-empty-value true})
       :logseq.property/hide-empty-value true})
    [:logseq.task/status :logseq.task/priority :logseq.task/deadline]))
    [:logseq.task/status :logseq.task/priority :logseq.task/deadline]))
 
 
+(defn- update-hl-color-and-page
+  [conn _search-db]
+  (let [db @conn
+        hl-color (d/entity db :logseq.property.pdf/hl-color)
+        hl-page (d/entity db :logseq.property.pdf/hl-page)
+        existing-colors (d/datoms db :avet :logseq.property.pdf/hl-color)
+        color-update-tx (mapcat
+                         (fn [datom]
+                           (let [block (d/entity db (:v datom))
+                                 color-ident (keyword "logseq.property" (str "color." (:block/title block)))]
+                             (if block
+                               [[:db/add (:e datom) :logseq.property.pdf/hl-color color-ident]
+                                [:db/retractEntity (:db/id block)]]
+                               [[:db/retract (:e datom) :logseq.property.pdf/hl-color]])))
+                         existing-colors)
+        page-datoms (d/datoms db :avet :logseq.property.pdf/hl-page)
+        page-update-tx (mapcat
+                        (fn [datom]
+                          (let [block (d/entity db (:v datom))
+                                value (db-property/property-value-content block)]
+                            (if (integer? value)
+                              [[:db/add (:e datom) :logseq.property.pdf/hl-page value]
+                               [:db/retractEntity (:db/id block)]]
+                              [[:db/retract (:e datom) :logseq.property.pdf/hl-page]])))
+                        page-datoms)]
+    ;; update schema first
+    (d/transact! conn
+                 (concat
+                  [{:db/ident :logseq.property.pdf/hl-page
+                    :block/schema {:type :raw-number}}
+                   [:db/retract (:db/id hl-page) :db/valueType]
+                   {:db/ident :logseq.property.pdf/hl-color
+                    :block/schema {:type :default}}]
+                  (db-property-build/closed-values->blocks
+                   (assoc hl-color :closed-values (get-in db-property/built-in-properties [:logseq.property.pdf/hl-color :closed-values])))))
+    ;; migrate data
+    (concat color-update-tx page-update-tx)))
+
 (defn- update-block-type-many->one
 (defn- update-block-type-many->one
   [conn _search-db]
   [conn _search-db]
   (let [db @conn
   (let [db @conn
@@ -378,7 +416,8 @@
    [42 {:fix (rename-properties {:logseq.property/hl-color :logseq.property.pdf/hl-color
    [42 {:fix (rename-properties {:logseq.property/hl-color :logseq.property.pdf/hl-color
                                  :logseq.property/hl-type :logseq.property.pdf/hl-type})}]
                                  :logseq.property/hl-type :logseq.property.pdf/hl-type})}]
    [43 {:properties [:logseq.property/hide-empty-value]
    [43 {:properties [:logseq.property/hide-empty-value]
-        :fix set-hide-empty-value}]])
+        :fix set-hide-empty-value}]
+   [44 {:fix update-hl-color-and-page}]])
 
 
 (let [max-schema-version (apply max (map first schema-version->updates))]
 (let [max-schema-version (apply max (map first schema-version->updates))]
   (assert (<= db-schema/version max-schema-version))
   (assert (<= db-schema/version max-schema-version))