Browse Source

move graph view data handler to deps/db

Tienson Qin 9 months ago
parent
commit
4c54427640

+ 48 - 0
deps/db/src/logseq/db.cljs

@@ -437,6 +437,13 @@
     (:alias rules/rules))
    distinct))
 
+(defn page-alias-set
+  [db page-id]
+  (->>
+   (get-block-alias db page-id)
+   (set)
+   (set/union #{page-id})))
+
 (defn get-block-refs
   [db id]
   (let [entity (d/entity db id)
@@ -603,3 +610,44 @@
 (def get-recent-updated-pages sqlite-common-db/get-recent-updated-pages)
 
 (def get-latest-journals sqlite-common-db/get-latest-journals)
+
+(defn get-all-namespace-relation
+  [db]
+  (d/q '[:find ?page ?parent
+         :where
+         [?page :block/namespace ?parent]]
+       db))
+
+(defn get-pages-relation
+  [db with-journal?]
+  (if (entity-plus/db-based-graph? db)
+    (let [q (if with-journal?
+              '[:find ?p ?ref-page
+                :where
+                [?block :block/page ?p]
+                [?block :block/refs ?ref-page]]
+              '[:find ?p ?ref-page
+                :where
+                [?block :block/page ?p]
+                [?p :block/tags]
+                (not [?p :block/tags :logseq.class/Journal])
+                [?block :block/refs ?ref-page]])]
+      (d/q q db))
+    (let [q (if with-journal?
+              '[:find ?p ?ref-page
+                :where
+                [?block :block/page ?p]
+                [?block :block/refs ?ref-page]]
+              '[:find ?p ?ref-page
+                :where
+                [?block :block/page ?p]
+                (not [?p :block/type "journal"])
+                [?block :block/refs ?ref-page]])]
+      (d/q q db))))
+
+(defn get-all-tagged-pages
+  [db]
+  (d/q '[:find ?page ?tag
+         :where
+         [?page :block/tags ?tag]]
+       db))

+ 15 - 0
deps/db/src/logseq/db/file_based/builtins.cljs

@@ -0,0 +1,15 @@
+(ns logseq.db.file-based.builtins
+  "File graph built-ins"
+  (:require [clojure.set :as set]))
+
+(defonce built-in-markers
+  ["NOW" "LATER" "DOING" "DONE" "CANCELED" "CANCELLED" "IN-PROGRESS" "TODO" "WAIT" "WAITING"])
+
+(defonce built-in-priorities
+  ["A" "B" "C"])
+
+(defonce built-in-pages-names
+  (set/union
+   (set built-in-markers)
+   (set built-in-priorities)
+   #{"Favorites" "Contents" "card"}))

+ 237 - 0
deps/db/src/logseq/db/frontend/graph.cljs

@@ -0,0 +1,237 @@
+(ns logseq.db.frontend.graph
+  "Main namespace for graph view fns."
+  (:require [clojure.set :as set]
+            [clojure.string :as string]
+            [datascript.core :as d]
+            [logseq.common.util :as common-util]
+            [logseq.db :as ldb]
+            [logseq.db.file-based.builtins :as file-builtins]
+            [logseq.db.frontend.entity-plus :as entity-plus]
+            [logseq.db.sqlite.create-graph :as sqlite-create-graph]))
+
+(defn- build-links
+  [links]
+  (keep (fn [[from to]]
+          (when (and from to)
+            {:source (str from)
+             :target (str to)}))
+        links))
+
+(defn- build-nodes
+  [dark? current-page page-links tags nodes namespaces]
+  (let [parents (set (map last namespaces))
+        current-page (or current-page "")
+        pages (common-util/distinct-by :db/id nodes)]
+    (->>
+     pages
+     (remove ldb/hidden?)
+     (remove nil?)
+     (mapv (fn [p]
+             (let [page-title (:block/title p)
+                   current-page? (= page-title current-page)
+                   color (case [dark? current-page?] ; FIXME: Put it into CSS
+                           [false false] "#999"
+                           [false true]  "#045591"
+                           [true false]  "#93a1a1"
+                           [true true]   "#ffffff")
+                   color (if (contains? tags (:db/id p))
+                           (if dark? "orange" "green")
+                           color)
+                   n (get page-links page-title 1)
+                   size (int (* 8 (max 1.0 (js/Math.cbrt n))))]
+               (cond->
+                {:id (str (:db/id p))
+                 :label page-title
+                 :size size
+                 :color color
+                 :block/created-at (:block/created-at p)}
+                 (contains? parents (:db/id p))
+                 (assoc :parent true))))))))
+
+                  ;; slow
+(defn- uuid-or-asset?
+  [label]
+  (or (common-util/uuid-string? label)
+      (string/starts-with? label "../assets/")
+      (= label "..")
+      (string/starts-with? label "assets/")
+      (string/ends-with? label ".gif")
+      (string/ends-with? label ".jpg")
+      (string/ends-with? label ".png")))
+
+(defn- remove-uuids-and-files!
+  [nodes]
+  (remove
+   (fn [node] (uuid-or-asset? (:label node)))
+   nodes))
+
+(defn- normalize-page-name
+  [{:keys [nodes links]}]
+  (let [nodes' (->> (remove-uuids-and-files! nodes)
+                    (common-util/distinct-by (fn [node] (:id node)))
+                    (remove nil?))]
+    {:nodes nodes'
+     :links links}))
+
+(defn- build-global-graph
+  [db {:keys [theme journal? orphan-pages? builtin-pages? excluded-pages? created-at-filter]}]
+  (let [dark? (= "dark" theme)
+        relation (ldb/get-pages-relation db journal?)
+        tagged-pages (ldb/get-all-tagged-pages db)
+        namespaces (ldb/get-all-namespace-relation db)
+        tags (set (map second tagged-pages))
+        full-pages (ldb/get-all-pages db)
+        db-based? (entity-plus/db-based-graph? db)
+        created-ats (map :block/created-at full-pages)
+
+            ;; build up nodes
+        full-pages'
+        (cond->> full-pages
+          created-at-filter
+          (filter #(<= (:block/created-at %) (+ (apply min created-ats) created-at-filter)))
+          (not journal?)
+          (remove ldb/journal?)
+          (not excluded-pages?)
+          (remove (fn [p] (true?
+                           (or (:logseq.property/exclude-from-graph-view p)
+                               (get-in p [:block/properties :exclude-from-graph-view]))))))
+        links (concat relation tagged-pages namespaces)
+        linked (set (mapcat identity links))
+        build-in-pages (->> (if db-based? sqlite-create-graph/built-in-pages-names file-builtins/built-in-pages-names)
+                            (map string/lower-case)
+                            set)
+        nodes (cond->> full-pages'
+                (not builtin-pages?)
+                (remove #(contains? build-in-pages (:block/name %)))
+                (not orphan-pages?)
+                (filter #(contains? linked (:db/id %))))
+        links (map (fn [[x y]] [(str x) (str y)]) links)
+        page-links (reduce (fn [m [k v]] (-> (update m k inc)
+                                             (update v inc))) {} links)
+        links (build-links links)
+        nodes (build-nodes dark? nil page-links tags nodes namespaces)]
+    (-> {:nodes nodes
+         :links links}
+        normalize-page-name
+        (assoc :all-pages
+               {:created-at-min (apply min created-ats)
+                :created-at-max (apply max created-ats)}))))
+
+(defn get-pages-that-mentioned-page
+  [db page-id include-journals?]
+  (let [pages (ldb/page-alias-set db page-id)
+        mentioned-pages (->>
+                         (mapcat
+                          (fn [id]
+                            (let [page (d/entity db id)]
+                              (->> (:block/_refs page)
+                                   (keep (fn [ref]
+                                           (if (ldb/page? ref)
+                                             page
+                                             (:block/page ref)))))))
+                          pages)
+                         (common-util/distinct-by :db/id))]
+    (keep (fn [page]
+            (when-not (and (not include-journals?) (ldb/journal? page))
+              (:db/id page)))
+          mentioned-pages)))
+
+(defn get-page-referenced-pages
+  [db page-id]
+  (let [pages (ldb/page-alias-set db page-id)
+        ref-pages (d/q
+                   '[:find [?ref-page ...]
+                     :in $ ?pages
+                     :where
+                     [(untuple ?pages) [?page ...]]
+                     [?block :block/page ?page]
+                     [?block :block/refs ?ref-page]]
+                   db
+                   pages)]
+    ref-pages))
+
+(defn- build-page-graph
+  [db page-uuid theme show-journal]
+  (let [dark? (= "dark" theme)
+        page-entity (d/entity db [:block/uuid page-uuid])
+        db-based? (entity-plus/db-based-graph? db)
+        page-id (:db/id page-entity)
+        tags (when db-based?
+               (set (map :db/id (:block/tags page-entity))))
+        tags (set (remove #(= page-id %) tags))
+        ref-pages (get-page-referenced-pages db page-id)
+        mentioned-pages (get-pages-that-mentioned-page db page-id show-journal)
+        namespaces (ldb/get-all-namespace-relation db)
+        links (concat
+               namespaces
+               (map (fn [ref-page]
+                      [page-id ref-page]) ref-pages)
+               (map (fn [page]
+                      [page-id page]) mentioned-pages)
+               (map (fn [tag]
+                      [page-id tag])
+                    tags))
+        other-pages (->> (concat ref-pages mentioned-pages)
+                         (remove nil?)
+                         (set))
+        other-pages-links (mapcat
+                           (fn [page-id]
+                             (let [ref-pages (-> (get-page-referenced-pages db page-id)
+                                                 (set)
+                                                 (set/intersection other-pages))
+                                   mentioned-pages (-> (get-pages-that-mentioned-page db page-id show-journal)
+                                                       (set)
+                                                       (set/intersection other-pages))]
+                               (concat
+                                (map (fn [p] [page-id p]) ref-pages)
+                                (map (fn [p] [p page-id]) mentioned-pages))))
+                           other-pages)
+        links (->> (concat links other-pages-links)
+                   (remove nil?)
+                   (distinct)
+                   (build-links))
+        nodes (->> (concat
+                    [page-id]
+                    ref-pages
+                    mentioned-pages
+                    tags)
+                   (remove nil?)
+                   (map #(d/entity db %))
+                   (common-util/distinct-by :db/id))
+        nodes (build-nodes dark? (:block/title page-entity) links tags nodes namespaces)]
+    (normalize-page-name
+     {:nodes nodes
+      :links links})))
+
+(defn- build-block-graph
+  "Builds a citation/reference graph for a given block uuid."
+  [db block-uuid theme]
+  (when-let [block (and (uuid? block-uuid) (d/entity db [:block/uuid block-uuid]))]
+    (let [dark? (= "dark" theme)
+          ref-blocks (->> (concat (:block/_refs block) (:block/refs block))
+                          (map (fn [b]
+                                 (if (ldb/page? b) b (:block/page b))))
+                          (remove (fn [node] (= (:db/id block) (:db/id node))))
+                          (common-util/distinct-by :db/id))
+          namespaces (ldb/get-all-namespace-relation db)
+          links (->> (concat
+                      namespaces
+                      (map (fn [p] [(:db/id block) (:db/id p)]) ref-blocks))
+                     (remove nil?)
+                     (distinct)
+                     (build-links))
+          nodes (->> (cons block ref-blocks)
+                     distinct
+                       ;; FIXME: get block tags
+                     )
+          nodes (build-nodes dark? block links #{} nodes namespaces)]
+      (normalize-page-name
+       {:nodes nodes
+        :links links}))))
+
+(defn build-graph
+  [db opts]
+  (case (:type opts)
+    :global (build-global-graph db opts)
+    :block (build-block-graph db (:block/uuid opts) (:theme opts))
+    :page (build-page-graph db (:block/uuid opts) (:theme opts) (:show-journal? opts))))

+ 5 - 13
deps/graph-parser/src/logseq/graph_parser/db.cljs

@@ -1,24 +1,16 @@
 (ns logseq.graph-parser.db
   "File graph specific db fns"
-  (:require [clojure.set :as set]
-            [clojure.string :as string]
+  (:require [clojure.string :as string]
             [datascript.core :as d]
             [logseq.common.util :as common-util]
             [logseq.common.uuid :as common-uuid]
             [logseq.db :as ldb]
+            [logseq.db.file-based.builtins :as file-builtins]
             [logseq.db.file-based.schema :as file-schema]))
 
-(defonce built-in-markers
-  ["NOW" "LATER" "DOING" "DONE" "CANCELED" "CANCELLED" "IN-PROGRESS" "TODO" "WAIT" "WAITING"])
-
-(defonce built-in-priorities
-  ["A" "B" "C"])
-
-(defonce built-in-pages-names
-  (set/union
-   (set built-in-markers)
-   (set built-in-priorities)
-   #{"Favorites" "Contents" "card"}))
+(defonce built-in-markers file-builtins/built-in-markers)
+(defonce built-in-priorities file-builtins/built-in-priorities)
+(defonce built-in-pages-names file-builtins/built-in-pages-names)
 
 (defn- page-title->block
   [title]

+ 37 - 11
src/main/frontend/components/page.cljs

@@ -1086,6 +1086,22 @@
       (filter (fn [node] (some #(re-find % (:label node)) filter-patterns)) nodes))
     nodes))
 
+(rum/defc graph-aux
+  [settings forcesettings theme search-graph-filters]
+  (let [[graph set-graph!] (hooks/use-state nil)]
+    (hooks/use-effect!
+     (fn []
+       (p/let [data-str (.build-graph ^js @state/*db-worker (state/get-current-repo)
+                                      (ldb/write-transit-str (assoc settings
+                                                                    :type :global
+                                                                    :theme theme)))
+               result (ldb/read-transit-str data-str)]
+         (set-graph! result)))
+     [theme settings])
+    (when graph
+      (let [graph' (update graph :nodes #(filter-graph-nodes % search-graph-filters))]
+        (global-graph-inner graph' settings forcesettings theme)))))
+
 (rum/defcs global-graph < rum/reactive
   (mixins/event-mixin
    (fn [state]
@@ -1103,10 +1119,8 @@
         theme (state/sub :ui/theme)
         ;; Needed for query to retrigger after reset
         _reset? (rum/react *graph-reset?)
-        graph (graph-handler/build-global-graph theme settings)
-        search-graph-filters (state/sub :search/graph-filters)
-        graph (update graph :nodes #(filter-graph-nodes % search-graph-filters))]
-    (global-graph-inner graph settings forcesettings theme)))
+        search-graph-filters (state/sub :search/graph-filters)]
+    (graph-aux settings forcesettings theme search-graph-filters)))
 
 (rum/defc page-graph-inner < rum/reactive
   [_page graph dark?]
@@ -1130,6 +1144,19 @@
                       (fn [graph]
                         (graph-register-handlers graph (atom nil) (atom nil) dark?))})]))
 
+(rum/defc page-graph-aux
+  [page opts]
+  (let [[graph set-graph!] (hooks/use-state nil)
+        dark? (= (:theme opts) "dark")]
+    (hooks/use-effect!
+     (fn []
+       (p/let [data-str (.build-graph ^js @state/*db-worker (state/get-current-repo) (ldb/write-transit-str opts))
+               result (ldb/read-transit-str data-str)]
+         (set-graph! result)))
+     [opts])
+    (when (seq (:nodes graph))
+      (page-graph-inner page graph dark?))))
+
 (rum/defc page-graph < db-mixins/query rum/reactive
   []
   (let [page (or
@@ -1137,14 +1164,13 @@
                    (state/sub [:route-match :path-params :name]))
               (date/today))
         theme (:ui/theme @state/state)
-        dark? (= theme "dark")
         show-journals-in-page-graph (rum/react *show-journals-in-page-graph?)
-        page-entity (db/get-page page)
-        graph (if (ldb/page? page-entity)
-                (graph-handler/build-page-graph page theme show-journals-in-page-graph)
-                (graph-handler/build-block-graph (uuid page) theme))]
-    (when (seq (:nodes graph))
-      (page-graph-inner page graph dark?))))
+        page-entity (db/get-page page)]
+    (page-graph-aux page
+                    {:type (if (ldb/page? page-entity) :page :block)
+                     :block/uuid (:block/uuid page-entity)
+                     :theme theme
+                     :show-journals? show-journals-in-page-graph})))
 
 (defn batch-delete-dialog
   [pages orphaned-pages? refresh-fn]

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

@@ -35,7 +35,7 @@
   get-files-blocks get-files-full get-journals-length
   get-latest-journals get-page get-case-page get-page-alias-names
   get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format
-  get-referenced-blocks get-page-referenced-blocks-full get-page-referenced-pages
+  get-referenced-blocks get-page-referenced-blocks-full
   get-all-pages get-pages-relation get-pages-that-mentioned-page
   journal-page? page? page-alias-set sub-block sub-entity
   page-empty? page-exists? get-alias-source-page

+ 4 - 37
src/main/frontend/db/model.cljs

@@ -20,6 +20,7 @@
             [logseq.db :as ldb]
             [logseq.db.frontend.class :as db-class]
             [logseq.db.frontend.content :as db-content]
+            [logseq.db.frontend.graph :as db-graph]
             [logseq.db.frontend.rules :as rules]
             [logseq.graph-parser.db :as gp-db]))
 
@@ -216,10 +217,7 @@ independent of format as format specific heading characters are stripped"
 
 (defn page-alias-set
   [repo-url page-id]
-  (->>
-   (ldb/get-block-alias (conn/get-db repo-url) page-id)
-   (set)
-   (set/union #{page-id})))
+  (ldb/page-alias-set (conn/get-db repo-url) page-id))
 
 (defn get-page-alias-names
   [repo page-id]
@@ -576,42 +574,11 @@ independent of format as format specific heading characters are stripped"
    (when-let [db (conn/get-db repo-url)]
      (take n (ldb/get-latest-journals db)))))
 
-;; get pages that this page referenced
-(defn get-page-referenced-pages
-  [repo page-id]
-  (when-let [db (conn/get-db repo)]
-    (let [pages (page-alias-set repo page-id)
-          ref-pages (d/q
-                     '[:find [?ref-page ...]
-                       :in $ ?pages
-                       :where
-                       [(untuple ?pages) [?page ...]]
-                       [?block :block/page ?page]
-                       [?block :block/refs ?ref-page]]
-                     db
-                     pages)]
-      ref-pages)))
-
 ;; get pages who mentioned this page
 (defn get-pages-that-mentioned-page
   [repo page-id include-journals?]
-  (when (conn/get-db repo)
-    (let [pages (page-alias-set repo page-id)
-          mentioned-pages (->>
-                           (mapcat
-                            (fn [id]
-                              (let [page (db-utils/entity repo id)]
-                                (->> (:block/_refs page)
-                                     (keep (fn [ref]
-                                             (if (ldb/page? ref)
-                                               page
-                                               (:block/page ref)))))))
-                            pages)
-                           (util/distinct-by :db/id))]
-      (keep (fn [page]
-              (when-not (and (not include-journals?) (ldb/journal? page))
-                (:db/id page)))
-            mentioned-pages))))
+  (when-let [db (conn/get-db repo)]
+    (db-graph/get-pages-that-mentioned-page db page-id include-journals?)))
 
 (defn get-page-referenced-blocks-full
   ([page-id]

+ 2 - 199
src/main/frontend/handler/graph.cljs

@@ -1,204 +1,7 @@
 (ns frontend.handler.graph
   "Provides util handler fns for graph view"
-  (:require [clojure.set :as set]
-            [clojure.string :as string]
-            [frontend.db :as db]
-            [frontend.db.model :as db-model]
-            [frontend.state :as state]
-            [frontend.util :as util]
-            [frontend.handler.property.util :as pu]
-            [frontend.config :as config]
-            [frontend.storage :as storage]
-            [logseq.graph-parser.db :as gp-db]
-            [logseq.db.sqlite.create-graph :as sqlite-create-graph]
-            [logseq.db :as ldb]))
-
-(defn- build-links
-  [links]
-  (keep (fn [[from to]]
-          (when (and from to)
-            {:source (str from)
-             :target (str to)}))
-        links))
-
-(defn- build-nodes
-  [dark? current-page page-links tags nodes namespaces]
-  (let [parents (set (map last namespaces))
-        current-page (or current-page "")
-        pages (util/distinct-by :db/id nodes)]
-    (->>
-     pages
-     (remove nil?)
-     (mapv (fn [p]
-             (let [page-title (:block/title p)
-                   current-page? (= page-title current-page)
-                   color (case [dark? current-page?] ; FIXME: Put it into CSS
-                           [false false] "#999"
-                           [false true]  "#045591"
-                           [true false]  "#93a1a1"
-                           [true true]   "#ffffff")
-                   color (if (contains? tags (:db/id p))
-                           (if dark? "orange" "green")
-                           color)
-                   n (get page-links page-title 1)
-                   size (int (* 8 (max 1.0 (js/Math.cbrt n))))]
-               (cond->
-                {:id (str (:db/id p))
-                 :label page-title
-                 :size size
-                 :color color
-                 :block/created-at (:block/created-at p)}
-                 (contains? parents (:db/id p))
-                 (assoc :parent true))))))))
-
-                  ;; slow
-(defn- uuid-or-asset?
-  [label]
-  (or (util/uuid-string? label)
-      (string/starts-with? label "../assets/")
-      (= label "..")
-      (string/starts-with? label "assets/")
-      (string/ends-with? label ".gif")
-      (string/ends-with? label ".jpg")
-      (string/ends-with? label ".png")))
-
-(defn- remove-uuids-and-files!
-  [nodes]
-  (remove
-   (fn [node] (uuid-or-asset? (:label node)))
-   nodes))
-
-(defn- normalize-page-name
-  [{:keys [nodes links]}]
-  (let [nodes' (->> (remove-uuids-and-files! nodes)
-                    (util/distinct-by (fn [node] (:id node)))
-                    (remove nil?))]
-    {:nodes nodes'
-     :links links}))
-
-(defn build-global-graph
-  [theme {:keys [journal? orphan-pages? builtin-pages? excluded-pages? created-at-filter]}]
-  (let [dark? (= "dark" theme)
-        current-page (or (:block/name (db/get-current-page)) "")]
-    (when-let [repo (state/get-current-repo)]
-      (let [relation (db/get-pages-relation repo journal?)
-            tagged-pages (db-model/get-all-tagged-pages repo)
-            namespaces (db/get-all-namespace-relation repo)
-            tags (set (map second tagged-pages))
-            full-pages (db/get-all-pages repo)
-            db-based? (config/db-based-graph? repo)
-            created-ats (map :block/created-at full-pages)
-
-            ;; build up nodes
-            full-pages'
-            (cond->> full-pages
-              created-at-filter
-              (filter #(<= (:block/created-at %) (+ (apply min created-ats) created-at-filter)))
-              (not journal?)
-              (remove ldb/journal?)
-              (not excluded-pages?)
-              (remove (fn [p] (true? (pu/get-block-property-value p :logseq.property/exclude-from-graph-view)))))
-            links (concat relation tagged-pages namespaces)
-            linked (set (mapcat identity links))
-            build-in-pages (->> (if db-based? sqlite-create-graph/built-in-pages-names gp-db/built-in-pages-names)
-                                (map string/lower-case)
-                                set)
-            nodes (cond->> full-pages'
-                    (not builtin-pages?)
-                    (remove #(contains? build-in-pages (:block/name %)))
-                    (not orphan-pages?)
-                    (filter #(contains? linked (:db/id %))))
-            links (map (fn [[x y]] [(str x) (str y)]) links)
-            page-links (reduce (fn [m [k v]] (-> (update m k inc)
-                                                 (update v inc))) {} links)
-            links (build-links links)
-            nodes (build-nodes dark? (string/lower-case current-page) page-links tags nodes namespaces)]
-        (-> {:nodes nodes
-             :links links}
-            normalize-page-name
-            (assoc :all-pages
-                   {:created-at-min (apply min created-ats)
-                    :created-at-max (apply max created-ats)}))))))
-
-(defn build-page-graph
-  [page theme show-journal]
-  (let [dark? (= "dark" theme)]
-    (when-let [repo (state/get-current-repo)]
-      (let [page-entity (db/get-page page)
-            page-id (:db/id page-entity)
-            tags (when (config/db-based-graph? repo)
-                   (set (map :db/id (:block/tags page-entity))))
-            tags (set (remove #(= page-id %) tags))
-            ref-pages (db/get-page-referenced-pages repo page-id)
-            mentioned-pages (db/get-pages-that-mentioned-page repo page-id show-journal)
-            namespaces (db/get-all-namespace-relation repo)
-            links (concat
-                   namespaces
-                   (map (fn [ref-page]
-                          [page-id ref-page]) ref-pages)
-                   (map (fn [page]
-                          [page-id page]) mentioned-pages)
-                   (map (fn [tag]
-                          [page-id tag])
-                        tags))
-            other-pages (->> (concat ref-pages mentioned-pages)
-                             (remove nil?)
-                             (set))
-            other-pages-links (mapcat
-                               (fn [page-id]
-                                 (let [ref-pages (-> (db/get-page-referenced-pages repo page-id)
-                                                     (set)
-                                                     (set/intersection other-pages))
-                                       mentioned-pages (-> (db/get-pages-that-mentioned-page repo page-id show-journal)
-                                                           (set)
-                                                           (set/intersection other-pages))]
-                                   (concat
-                                    (map (fn [p] [page-id p]) ref-pages)
-                                    (map (fn [p] [p page-id]) mentioned-pages))))
-                               other-pages)
-            links (->> (concat links other-pages-links)
-                       (remove nil?)
-                       (distinct)
-                       (build-links))
-            nodes (->> (concat
-                        [page-id]
-                        ref-pages
-                        mentioned-pages
-                        tags)
-                       (remove nil?)
-                       (map db/entity)
-                       (util/distinct-by :db/id))
-            nodes (build-nodes dark? page links tags nodes namespaces)]
-        (normalize-page-name
-         {:nodes nodes
-          :links links})))))
-
-(defn build-block-graph
-  "Builds a citation/reference graph for a given block uuid."
-  [block-uuid theme]
-  (when-let [repo (state/get-current-repo)]
-    (when-let [block (and (uuid? block-uuid) (db/entity [:block/uuid block-uuid]))]
-      (let [dark? (= "dark" theme)
-            ref-blocks (->> (concat (:block/_refs block) (:block/refs block))
-                            (map (fn [b]
-                                   (if (ldb/page? b) b (:block/page b))))
-                            (remove (fn [node] (= (:db/id block) (:db/id node))))
-                            (util/distinct-by :db/id))
-            namespaces (db/get-all-namespace-relation repo)
-            links (->> (concat
-                        namespaces
-                        (map (fn [p] [(:db/id block) (:db/id p)]) ref-blocks))
-                       (remove nil?)
-                       (distinct)
-                       (build-links))
-            nodes (->> (cons block ref-blocks)
-                       distinct
-                       ;; FIXME: get block tags
-                       )
-            nodes (build-nodes dark? block links #{} nodes namespaces)]
-        (normalize-page-name
-         {:nodes nodes
-          :links links})))))
+  (:require [frontend.state :as state]
+            [frontend.storage :as storage]))
 
 (defn n-hops
   "Get all nodes that are n hops from nodes (a collection of node ids)"

+ 0 - 16
src/main/frontend/handler/page.cljs

@@ -217,22 +217,6 @@
       (graph-handler/settle-metadata-to-local! {:created-at (js/Date.now)}))
     opts)))
 
-(comment
-  (defn get-all-pages
-    [repo]
-    (let [db-based? (config/db-based-graph? repo)
-          graph-specific-hidden?
-          (if db-based?
-            (fn [p]
-              (and (ldb/property? p) (ldb/built-in? p)))
-            (fn [p]
-              (gp-db/built-in-pages-names (string/upper-case (:block/name p)))))]
-      (cond->>
-       (->> (db/get-all-pages repo)
-            (remove graph-specific-hidden?))
-        (not db-based?)
-        (common-handler/fix-pages-timestamps)))))
-
 (defn file-based-save-filter!
   [page filter-state]
   (property-handler/add-page-property! page :filters filter-state))

+ 7 - 0
src/main/frontend/worker/db_worker.cljs

@@ -34,6 +34,7 @@
             [logseq.db :as ldb]
             [logseq.db.common.order :as db-order]
             [logseq.db.common.sqlite :as sqlite-common-db]
+            [logseq.db.frontend.graph :as db-graph]
             [logseq.db.frontend.schema :as db-schema]
             [logseq.db.frontend.view :as db-view]
             [logseq.db.sqlite.create-graph :as sqlite-create-graph]
@@ -922,6 +923,12 @@
          data (db-view/get-property-values @conn property-ident opts)]
      (ldb/write-transit-str data)))
 
+  (build-graph
+   [_this repo opts-str]
+   (let [conn (worker-state/get-datascript-conn repo)
+         data (db-graph/build-graph @conn (ldb/read-transit-str opts-str))]
+     (ldb/write-transit-str data)))
+
   (dangerousRemoveAllDbs
    [this repo]
    (p/let [r (.listDB this)