ソースを参照

fix: make table row reactive

Tienson Qin 9 ヶ月 前
コミット
591c5e391f

+ 4 - 4
src/main/frontend/components/block.cljs

@@ -919,7 +919,9 @@
                (:skip-async-load? (first args))
                (reset! *result page)
                :else
-               (p/let [result (db-async/<get-block (state/get-current-repo) page-id-or-name {:children? false :block-only? true})]
+               (p/let [result (db-async/<get-block (state/get-current-repo) page-id-or-name {:children? false :block-only? true
+                                                                                             :skip-refresh? true
+                                                                                             :including-property-vals? false})]
                  (reset! *result result)))
 
              (assoc state :*entity *result)))}
@@ -3690,9 +3692,7 @@
 
 (rum/defc block-container
   [config block* & {:as opts}]
-  (let [[block set-block!] (hooks/use-state
-                            (or (some-> (:db/id block*) db/entity)
-                                block*))]
+  (let [[block set-block!] (hooks/use-state block*)]
     (when-not (or (:page-title? config)
                   (:property-block? config)
                   (:table-view? config))

+ 68 - 65
src/main/frontend/components/property/value.cljs

@@ -4,7 +4,6 @@
             [cljs-time.local :as local]
             [clojure.set :as set]
             [clojure.string :as string]
-            [datascript.impl.entity :as de]
             [dommy.core :as d]
             [frontend.components.icon :as icon-component]
             [frontend.components.select :as select]
@@ -42,6 +41,10 @@
             [promesa.core :as p]
             [rum.core :as rum]))
 
+(defn- entity-map?
+  [m]
+  (and (map? m) (:db/id m)))
+
 (rum/defc property-empty-btn-value
   [property & opts]
   (let [text (cond
@@ -235,13 +238,15 @@
 (defn- add-or-remove-property-value
   [block property value selected? {:keys [refresh-result-f] :as opts}]
   (let [many? (db-property/many? property)
-        blocks (get-operating-blocks block)]
-    (when (and selected?
-               (= :db.type/ref (:db/valueType property))
-               (number? value)
-               (not (db/entity value)))
-      (db-async/<get-block (state/get-current-repo) value {:children? false}))
+        blocks (get-operating-blocks block)
+        repo (state/get-current-repo)]
     (p/do!
+     (db-async/<get-block repo (:db/id block) {:children? false})
+     (when (and selected?
+                (= :db.type/ref (:db/valueType property))
+                (number? value)
+                (not (db/entity value)))
+       (db-async/<get-block repo value {:children? false}))
      (if selected?
        (<add-property! block (:db/ident property) value
                        {:selected? selected?
@@ -637,10 +642,10 @@
         tags? (= :block/tags (:db/ident property))
         alias? (= :block/alias (:db/ident property))
         tags-or-alias? (or tags? alias?)
-        block (db/entity (:db/id block))
+        block (or (db/entity (:db/id block)) block)
         selected-choices (when block
                            (when-let [v (get block (:db/ident property))]
-                             (if (every? de/entity? v)
+                             (if (every? entity-map? v)
                                (map :db/id v)
                                [(:db/id v)])))
         parent-property? (= (:db/ident property) :logseq.property/parent)
@@ -649,57 +654,55 @@
         get-all-classes-f (fn []
                             (model/get-all-classes repo {:except-root-class? true
                                                          :except-private-tags? (not (contains? #{:logseq.property/template-applied-to} (:db/ident property)))}))
-        nodes
-        (->>
-         (cond
-           parent-property?
-           (let [;; Disallows cyclic hierarchies
-                 exclude-ids (-> (set (map (fn [id] (:block/uuid (db/entity id))) children-pages))
-                                 (conj (:block/uuid block))) ; break cycle
-                 options (if (ldb/class? block)
-                           (model/get-all-classes repo)
-                           (when (ldb/internal-page? block)
-                             (cond->>
-                              (->> (model/get-all-pages repo)
-                                   (filter ldb/internal-page?)
-                                   (remove ldb/built-in?)))))
-                 excluded-options (remove (fn [e] (contains? exclude-ids (:block/uuid e))) options)]
-             excluded-options)
-
-           (= property-type :class)
-           (get-all-classes-f)
-
-           (seq classes)
-           (->>
-            (mapcat
-             (fn [class]
-               (model/get-class-objects repo (:db/id class)))
-             classes)
-            distinct)
-
-           :else
-           (if (empty? result)
-             (let [v (get block (:db/ident property))]
-               (remove #(= :logseq.property/empty-placeholder (:db/ident %))
-                       (if (every? de/entity? v) v [v])))
-             (remove (fn [node]
-                       (or (= (:db/id block) (:db/id node))
+        nodes (cond
+                parent-property?
+                (let [;; Disallows cyclic hierarchies
+                      exclude-ids (-> (set (map (fn [id] (:block/uuid (db/entity id))) children-pages))
+                                      (conj (:block/uuid block))) ; break cycle
+                      options (if (ldb/class? block)
+                                (model/get-all-classes repo)
+                                (when (ldb/internal-page? block)
+                                  (cond->>
+                                   (->> (model/get-all-pages repo)
+                                        (filter ldb/internal-page?)
+                                        (remove ldb/built-in?)))))
+                      excluded-options (remove (fn [e] (contains? exclude-ids (:block/uuid e))) options)]
+                  excluded-options)
+
+                (= property-type :class)
+                (get-all-classes-f)
+
+                (seq classes)
+                (->>
+                 (mapcat
+                  (fn [class]
+                    (model/get-class-objects repo (:db/id class)))
+                  classes)
+                 distinct)
+
+                :else
+                (if (empty? result)
+                  (let [v (get block (:db/ident property))]
+                    (remove #(= :logseq.property/empty-placeholder (:db/ident %))
+                            (if (every? entity-map? v) v [v])))
+                  (remove (fn [node]
+                            (or (= (:db/id block) (:db/id node))
                              ;; A page's alias can't be itself
-                           (and alias? (= (or (:db/id (:block/page block))
-                                              (:db/id block))
-                                          (:db/id node)))
-                           (cond
-                             (= property-type :class)
-                             (ldb/private-tags (:db/ident node))
-
-                             (and property-type (not= property-type :node))
-                             (if (= property-type :page)
-                               (not (db/page? node))
-                               (not (contains? (ldb/get-entity-types node) property-type)))
-
-                             :else
-                             false)))
-                     result))))
+                                (and alias? (= (or (:db/id (:block/page block))
+                                                   (:db/id block))
+                                               (:db/id node)))
+                                (cond
+                                  (= property-type :class)
+                                  (ldb/private-tags (:db/ident node))
+
+                                  (and property-type (not= property-type :node))
+                                  (if (= property-type :page)
+                                    (not (db/page? node))
+                                    (not (contains? (ldb/get-entity-types node) property-type)))
+
+                                  :else
+                                  false)))
+                          result)))
 
         options (map (fn [node]
                        (let [id (or (:value node) (:db/id node))
@@ -917,7 +920,7 @@
         multiple-values? (db-property/many? property)
         block-container (state/get-component :block/container)
         blocks-container (state/get-component :block/blocks-container)
-        value-block (if (and (coll? value-block) (every? de/entity? value-block))
+        value-block (if (and (coll? value-block) (every? entity-map? value-block))
                       (set (remove #(= (:db/ident %) :logseq.property/empty-placeholder) value-block))
                       value-block)
         default-value (:logseq.property/default-value property)
@@ -976,7 +979,7 @@
 (rum/defc closed-value-item < rum/reactive db-mixins/query
   [value {:keys [inline-text icon?]}]
   (when value
-    (let [eid (if (de/entity? value) (:db/id value) [:block/uuid value])
+    (let [eid (if (entity-map? value) (:db/id value) [:block/uuid value])
           block (or (db/sub-block (:db/id (db/entity eid))) value)
           property-block? (db-property/property-created-block? block)
           value' (db-property/closed-value-content block)
@@ -1122,7 +1125,7 @@
         select-type?' (or (select-type? block property)
                           (and editing? batch? (contains? #{:default :url} type) (not closed-values?)))
         select-opts {:on-chosen on-chosen}
-        value (if (and (de/entity? value*) (= (:db/ident value*) :logseq.property/empty-placeholder))
+        value (if (and (entity-map? value*) (= (:db/ident value*) :logseq.property/empty-placeholder))
                 nil
                 value*)]
     (if (= :logseq.property/icon (:db/ident property))
@@ -1180,7 +1183,7 @@
   (let [type (:logseq.property/type property)
         date? (= type :date)
         *el (rum/use-ref nil)
-        items (cond->> (if (de/entity? v) #{v} v)
+        items (cond->> (if (entity-map? v) #{v} v)
                 (= (:db/ident property) :block/tags)
                 (remove (fn [v] (contains? ldb/hidden-tags (:db/ident v)))))
         select-cp (fn [select-opts]
@@ -1267,8 +1270,8 @@
                                    (or (= (:db/id v) (:db/id block))
                                        ;; property value self embedded
                                        (= (:db/id (:block/link v)) (:db/id block))))]
-     (if (and (or (and (de/entity? v) (self-value-or-embedded? v))
-                  (and (coll? v) (every? de/entity? v)
+     (if (and (or (and (entity-map? v) (self-value-or-embedded? v))
+                  (and (coll? v) (every? entity-map? v)
                        (some self-value-or-embedded? v))
                   (and (= p-block (:db/id block)) (= p-property (:db/id property))))
               (not= :logseq.class/Tag (:db/ident block)))

+ 9 - 7
src/main/frontend/components/views.cljs

@@ -600,8 +600,10 @@
 
 (rum/defc table-row < rum/reactive db-mixins/query
   [table row props option]
-  (let [row' (or (db/sub-block (:db/id row)) row)]
-    (table-row-inner table row' props option)))
+  (let [block (db/sub-block (:db/id row))
+        row' (or block row)]
+    (table-row-inner table row' props
+                     (assoc option :init-row row))))
 
 (rum/defc search
   [input {:keys [on-change set-input!]}]
@@ -1156,15 +1158,15 @@
                     (number? item) item
                     :else nil)]
     (when db-id
-      (let [block (db/entity db-id)
-            [item set-item!] (hooks/use-state block)
+      (let [block-entity (db/entity db-id)
+            [item set-item!] (hooks/use-state (when (:block.temp/fully-loaded? block-entity) block-entity))
             opts {:block-only? true
                   :properties properties
-                  :skip-transact? true
-                  :skip-refresh? true}]
+                  :skip-transact? (not block-entity)
+                  :skip-refresh? (not block-entity)}]
         (hooks/use-effect!
          (fn []
-           (when (and db-id (not (:block.temp/fully-loaded? block)))
+           (when (and db-id (not item))
              (p/let [block (db-async/<get-block (state/get-current-repo) db-id opts)]
                (set-item! block))))
          [db-id])

+ 1 - 1
src/main/frontend/db/async.cljs

@@ -155,7 +155,7 @@
                 (react/refresh-affected-queries! graph affected-keys))))
 
           (if (or children? block-only?)
-            (or (db/entity (:db/id block)) block)
+            block
             result'))))))
 
 (defn <get-blocks