浏览代码

enhance: unify page and block references

Tienson Qin 6 月之前
父节点
当前提交
a70372ac88

+ 21 - 3
deps/db/src/logseq/db.cljs

@@ -465,11 +465,29 @@
     (when (seq ref-ids)
       (d/pull-many db '[*] ref-ids))))
 
+(defn hidden-ref?
+  "Whether ref-block (for block with the `id`) should be hidden."
+  [db ref-block id]
+  (let [db-based? (entity-plus/db-based-graph? db)]
+    (if db-based?
+      (let [entity (d/entity db id)]
+        (or
+         (= (:db/id ref-block) id)
+         (= id (:db/id (:block/page ref-block)))
+         (hidden? (:block/page ref-block))
+         (hidden? ref-block)
+         (contains? (set (map :db/id (:block/tags ref-block))) (:db/id entity))
+         (some? (get ref-block (:db/ident entity)))))
+      (or
+       (= (:db/id ref-block) id)
+       (= id (:db/id (:block/page ref-block)))))))
+
 (defn get-block-refs-count
   [db id]
-  (some-> (d/entity db id)
-          :block/_refs
-          count))
+  (some->> (d/entity db id)
+           :block/_refs
+           (remove (fn [ref-block] (hidden-ref? db ref-block id)))
+           count))
 
 (defn hidden-or-internal-tag?
   [e]

+ 2 - 14
deps/db/src/logseq/db/common/view.cljs

@@ -299,24 +299,12 @@
 
 (defn- get-linked-references
   [db id]
-  (let [db-based? (ldb/db-based-graph? db)
-        entity (d/entity db id)
+  (let [entity (d/entity db id)
         ids (set (cons id (ldb/get-block-alias db id)))
         refs (mapcat (fn [id] (:block/_refs (d/entity db id))) ids)
         page-filters (get-filters db entity)
         full-ref-blocks (->> refs
-                             (remove (fn [block]
-                                       (if db-based?
-                                         (or
-                                          (= (:db/id block) id)
-                                          (= id (:db/id (:block/page block)))
-                                          (ldb/hidden? (:block/page block))
-                                          (ldb/hidden? block)
-                                          (contains? (set (map :db/id (:block/tags block))) (:db/id entity))
-                                          (some? (get block (:db/ident entity))))
-                                         (or
-                                          (= (:db/id block) id)
-                                          (= id (:db/id (:block/page block)))))))
+                             (remove (fn [block] (ldb/hidden-ref? db block id)))
                              (common-util/distinct-by :db/id))
         ref-blocks (cond->> full-ref-blocks
                      (seq page-filters)

+ 34 - 32
src/main/frontend/components/block.cljs

@@ -2977,28 +2977,10 @@
 
 (rum/defcs ^:large-vars/cleanup-todo block-content-or-editor < rum/reactive
   (rum/local false ::hover?)
-  {:init (fn [state]
-           (let [block (second (:rum/args state))
-                 config (first (:rum/args state))
-                 current-block-page? (= (str (:block/uuid block)) (state/get-current-page))
-                 embed-self? (and (:embed? config)
-                                  (= (:block/uuid block) (:block/uuid (:block config))))
-                 default-hide? (or (not (and current-block-page? (not embed-self?) (state/auto-expand-block-refs?)))
-                                   (= (str (:id config)) (str (:block/uuid block))))
-                 *refs-count (atom nil)]
-             (when-let [id (:db/id block)]
-               (p/let [count (db-async/<get-block-refs-count (state/get-current-repo) id)]
-                 (reset! *refs-count count)))
-             (assoc state
-                    ::hide-block-refs? (atom default-hide?)
-                    ::refs-count *refs-count)))}
-  [state config {:block/keys [uuid] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count?]}]
+  [state config {:block/keys [uuid] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count? refs-count *hide-block-refs?]}]
   (let [format (if (config/db-based-graph? (state/get-current-repo))
                  :markdown
                  (or (:block/format block) :markdown))
-        *hide-block-refs? (get state ::hide-block-refs?)
-        *refs-count (get state ::refs-count)
-        hide-block-refs? (rum/react *hide-block-refs?)
         editor-box (state/get-component :editor/box)
         editor-id (str "editor-" edit-input-id)
         slide? (:slide? config)
@@ -3009,9 +2991,6 @@
         named? (some? (:block/name block))
         repo (state/get-current-repo)
         db-based? (config/db-based-graph? repo)
-        refs-count (if (seq (:block/_refs block))
-                     (count (remove :logseq.property/view-for (:block/_refs block)))
-                     (rum/react *refs-count))
         table? (:table? config)
         raw-mode-block (state/sub :editor/raw-mode-block)
         type-block-editor? (and (contains? #{:code} (:logseq.property.node/display-type block))
@@ -3076,14 +3055,7 @@
         (when-not (or (:block-ref? config) (:table? config) (:gallery-view? config)
                       (:property? config))
           (when (and db-based? (seq (:block/tags block)))
-            (tags-cp (assoc config :block/uuid (:block/uuid block)) block)))]]
-
-      (when (and (not (or (:table? config) (:property? config)))
-                 (not hide-block-refs?)
-                 (> refs-count 0)
-                 (not (:page-title? config)))
-        (when-let [refs-cp (state/get-component :block/linked-references)]
-          (refs-cp uuid)))]]))
+            (tags-cp (assoc config :block/uuid (:block/uuid block)) block)))]]]]))
 
 (rum/defcs single-block-cp < mixins/container-id
   [state _config block-uuid]
@@ -3432,10 +3404,30 @@
 
 (rum/defcs ^:large-vars/cleanup-todo block-container-inner-aux < rum/reactive db-mixins/query
   {:init (fn [state]
-           (let [*ref (atom nil)]
-             (assoc state ::ref *ref)))}
+           (let [*ref (atom nil)
+                 [_container-state _repo config block] (:rum/args state)
+                 current-block-page? (= (str (:block/uuid block)) (state/get-current-page))
+                 embed-self? (and (:embed? config)
+                                  (= (:block/uuid block) (:block/uuid (:block config))))
+                 default-hide? (or (not (and current-block-page? (not embed-self?) (state/auto-expand-block-refs?)))
+                                   (= (str (:id config)) (str (:block/uuid block))))
+                 *refs-count (atom nil)]
+             (when-not (:view? config)
+               (when-let [id (:db/id block)]
+                 (p/let [count (db-async/<get-block-refs-count (state/get-current-repo) id)]
+                   (reset! *refs-count count))))
+             (assoc state
+                    ::ref *ref
+                    ::hide-block-refs? (atom default-hide?)
+                    ::refs-count *refs-count)))}
   [state container-state repo config* block {:keys [navigating-block navigated? editing? selected?] :as opts}]
   (let [*ref (::ref state)
+        *hide-block-refs? (get state ::hide-block-refs?)
+        *refs-count (get state ::refs-count)
+        hide-block-refs? (rum/react *hide-block-refs?)
+        refs-count (if (seq (:block/_refs block))
+                     (count (remove :logseq.property/view-for (:block/_refs block)))
+                     (rum/react *refs-count))
         [original-block block] (build-block config* block {:navigating-block navigating-block :navigated? navigated?})
         config* (if original-block
                   (assoc config* :original-block original-block)
@@ -3613,6 +3605,8 @@
                                         {:edit-input-id edit-input-id
                                          :block-id block-id
                                          :edit? editing?
+                                         :refs-count refs-count
+                                         :*hide-block-refs? *hide-block-refs?
                                          :hide-block-refs-count? hide-block-refs-count?}))])]
 
          (when (and db-based? (not collapsed?) (not (or table? property?)))
@@ -3630,6 +3624,14 @@
        [:div (when-not (:page-title? config) {:style {:padding-left 45}})
         (db-properties-cp config block {:in-block-container? true})])
 
+     (when (and (not (or (:table? config) (:property? config)))
+                (not hide-block-refs?)
+                (> refs-count 0)
+                (not (:page-title? config)))
+       (when-let [refs-cp (state/get-component :block/linked-references)]
+         [:div.px-4.py-2.border.rounded.my-2.shadow-xs {:style {:margin-left 42}}
+          (refs-cp block {})]))
+
      (when (and db-based? (not collapsed?) (not (or table? property?))
                 (ldb/class-instance? (entity-plus/entity-memoized (db/get-db) :logseq.class/Query) block))
        (let [query-block (:logseq.property/query (db/entity (:db/id block)))

+ 1 - 1
src/main/frontend/components/property.css

@@ -185,7 +185,7 @@
 
 .property-value-inner {
   .select-item {
-    @apply flex items-center shrink;
+    @apply flex items-center shrink whitespace-nowrap;
   }
 
   &[data-type] {

+ 3 - 40
src/main/frontend/components/reference.cljs

@@ -1,55 +1,18 @@
 (ns frontend.components.reference
   (:require [frontend.common.missionary :as c.m]
-            [frontend.components.block :as block]
-            [frontend.components.content :as content]
-            [frontend.components.editor :as editor]
             [frontend.components.reference-filters :as filters]
             [frontend.components.views :as views]
             [frontend.config :as config]
             [frontend.db :as db]
             [frontend.db-mixins :as db-mixins]
-            [frontend.db.async :as db-async]
-            [frontend.db.utils :as db-utils]
             [frontend.state :as state]
             [frontend.ui :as ui]
             [logseq.db.common.view :as db-view]
             [logseq.shui.hooks :as hooks]
             [logseq.shui.ui :as shui]
             [missionary.core :as m]
-            [promesa.core :as p]
             [rum.core :as rum]))
 
-;; TODO: merge both page and block linked refs
-(rum/defc block-linked-references-aux < rum/reactive db-mixins/query
-  [e]
-  (let [block-id (:block/uuid e)
-        ref-blocks (-> (db/get-referenced-blocks (:db/id e))
-                       db-utils/group-by-page)]
-    (when (> (count ref-blocks) 0)
-      (let [ref-hiccup (block/->hiccup ref-blocks
-                                       {:id (str block-id)
-                                        :ref? true
-                                        :breadcrumb-show? true
-                                        :group-by-page? true
-                                        :editor-box editor/box}
-                                       {})]
-        [:div.references-blocks
-         (content/content block-id
-                          {:hiccup ref-hiccup})]))))
-
-(rum/defc block-linked-references
-  [block-id]
-  (when-let [e (db/entity [:block/uuid block-id])]
-    (let [[loading? set-loading!] (hooks/use-state true)]
-      (hooks/use-effect!
-       (fn []
-         (p/do!
-          (db-async/<get-block-refs (state/get-current-repo) (:db/id e))
-          (set-loading! false)))
-       [])
-      (when-not loading?
-        (block-linked-references-aux e)))))
-
 (rum/defc references-aux
   [page-entity config]
   (let [filters (db-view/get-filters (db/get-db) page-entity)
@@ -85,9 +48,9 @@
       :config config})))
 
 (rum/defc references-cp < rum/reactive db-mixins/query
-  [page-entity config]
-  (let [page (db/sub-block (:db/id page-entity))]
-    (references-aux page config)))
+  [entity config]
+  (let [block (db/sub-block (:db/id entity))]
+    (references-aux block config)))
 
 (rum/defc references
   [entity config]

+ 22 - 13
src/main/frontend/components/views.cljs

@@ -1492,8 +1492,8 @@
       (db/entity [:block/uuid (:block/uuid result)]))))
 
 (rum/defc views-tab < rum/reactive db-mixins/query
-  [view-parent current-view {:keys [views data items-count set-view-entity! set-data! set-views! view-feature-type show-items-count?]} hover?]
-  [:div.views.flex.flex-row.items-center.flex-wrap.gap-2
+  [view-parent current-view {:keys [views data items-count set-view-entity! set-data! set-views! view-feature-type show-items-count? references? opacity]}]
+  [:div.views
    (for [view* views]
      (let [view (db/sub-block (:db/id view*))
            current-view? (= (:db/id current-view) (:db/id view))]
@@ -1529,11 +1529,12 @@
                        (do
                          (set-view-entity! view)
                          (set-data! nil))))}
-        (let [display-type (or (:db/ident (get view :logseq.property.view/type))
-                               :logseq.property.view/type.table)]
-          (when-let [icon (:logseq.property/icon (db/entity display-type))]
-            (icon-component/icon icon {:color? true
-                                       :size 15})))
+        (when-not references?
+          (let [display-type (or (:db/ident (get view :logseq.property.view/type))
+                                 :logseq.property.view/type.table)]
+            (when-let [icon (:logseq.property/icon (db/entity display-type))]
+              (icon-component/icon icon {:color? true
+                                         :size 15}))))
         (let [title (:block/title view)]
           (if (= title "")
             "New view"
@@ -1546,8 +1547,7 @@
     {:variant :text
      :size :sm
      :title "Add new view"
-     :class (str "!px-1 -ml-1 text-muted-foreground hover:text-foreground transition-opacity ease-in duration-300 "
-                 (if hover? "opacity-100" "opacity-75"))
+     :class (str "!px-1 -ml-1 text-muted-foreground hover:text-foreground transition-opacity ease-in duration-300 " opacity)
      :on-click (fn []
                  (p/let [view (create-view! view-parent view-feature-type)]
                    (set-views! (concat views [view]))))}
@@ -1559,8 +1559,14 @@
    {:keys [view-feature-type title-key additional-actions]
     :as option}]
   (let [[hover? set-hover?] (hooks/use-state nil)
-        db-based? (config/db-based-graph? (state/get-current-repo))]
-    [:div.flex.flex-1.flex-wrap.items-center.justify-between.gap-1
+        hover? true
+        db-based? (config/db-based-graph? (state/get-current-repo))
+        references? (contains? #{:linked-references :unlinked-references} view-feature-type)
+        opacity (cond
+                  (and references? (not hover?)) "opacity-0"
+                  hover? "opacity-100"
+                  :else "opacity-75")]
+    [:div.flex.flex-1.flex-nowrap.items-center.justify-between.gap-1.overflow-hidden
      {:on-mouse-over #(set-hover? true)
       :on-mouse-out #(set-hover? false)}
      [:div.flex.flex-row.items-center.gap-2
@@ -1569,7 +1575,10 @@
           [:div.font-medium.opacity-50.text-sm
            (t (or title-key :views.table/default-title)
               (count (:rows table)))]
-          (views-tab view-parent view-entity option hover?))
+          (views-tab view-parent view-entity (assoc option
+                                                    :hover? hover?
+                                                    :opacity opacity
+                                                    :references? references?)))
         [:div.font-medium.text-sm
          [:span
           (case view-feature-type
@@ -1579,7 +1588,7 @@
             "Nodes")]
          [:span.ml-1 (count (:rows table))]])]
      [:div.view-actions.flex.items-center.gap-1.transition-opacity.ease-in.duration-300
-      {:class (if hover? "opacity-100" "opacity-75")}
+      {:class opacity}
 
       (when (seq additional-actions)
         [:<> (for [action additional-actions]

+ 4 - 0
src/main/frontend/components/views.css

@@ -1,3 +1,7 @@
+.views {
+    @apply flex flex-row items-center flex-nowrap gap-2 overflow-hidden;
+}
+
 .ls-view-order-setting {
     button.order-button {
         width: 107px;

+ 1 - 2
src/main/frontend/components/whiteboard.cljs

@@ -66,7 +66,6 @@
      (let [*open? (::open? state)
            page-entity (model/get-page page-name-or-uuid)
            page (model/sub-block (:db/id page-entity))
-           block-uuid (:block/uuid page-entity)
            refs-count (count (:block/_refs page))]
        (when (> refs-count 0)
          (shui/popover
@@ -81,7 +80,7 @@
            {:on-open-auto-focus #(.preventDefault %)}
            (shui/popover-arrow {:class-name "popper-arrow"})
            [:div {:class classname}
-            (reference/block-linked-references block-uuid)])))))))
+            (reference/references page {})])))))))
 
 ;; This is not accurate yet
 (defn- get-page-human-update-time

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

@@ -120,6 +120,7 @@
     (cond
       (and (:block.temp/fully-loaded? e) ; children may not be fully loaded
            (not children-only?)
+           (not children?)
            (not nested-children?)
            (not (some #{:block.temp/refs-count} properties)))
       (p/promise e)

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

@@ -118,7 +118,7 @@
   []
   (state/set-page-blocks-cp! page/page-cp)
   (state/set-component! :block/->hiccup block/->hiccup)
-  (state/set-component! :block/linked-references reference/block-linked-references)
+  (state/set-component! :block/linked-references reference/references)
   (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
   (state/set-component! :block/single-block block/single-block-cp)
   (state/set-component! :block/container block/block-container)

+ 1 - 1
src/main/frontend/publishing.cljs

@@ -84,7 +84,7 @@
   []
   (state/set-page-blocks-cp! page-component/page-cp)
   (state/set-component! :block/->hiccup block/->hiccup)
-  (state/set-component! :block/linked-references reference/block-linked-references)
+  (state/set-component! :block/linked-references reference/references)
   (state/set-component! :whiteboard/tldraw-preview whiteboard/tldraw-preview)
   (state/set-component! :block/single-block block/single-block-cp)
   (state/set-component! :block/container block/block-container)