refactor: mv file graph model fns to their own ns

All these fns are only used by file graphs. Most only act on file only
attributes e.g. :block/file, :block/pre-block? and :block/namespace
This commit is contained in:
Gabriel Horner
2025-05-07 13:27:01 -04:00
committed by Tienson Qin
parent 0b2c1d4a0a
commit b985020d1f
26 changed files with 311 additions and 300 deletions

View File

@@ -85,6 +85,7 @@
frontend.config config
frontend.date date
frontend.db db
frontend.db.file-based.model file-model
frontend.db-mixins db-mixins
frontend.db.query-custom query-custom
frontend.db.query-dsl query-dsl

View File

@@ -19,7 +19,7 @@
blocks))
(defn- get-file-page
"Copy of db/get-file-page. Too basic to couple to main app"
"Copy of file-model/get-file-page. Too basic to couple to main app"
[db file-path]
(ffirst
(d/q

View File

@@ -7,7 +7,7 @@
[electron.ipc :as ipc]
[frontend.db :as db]
[frontend.db.async :as db-async]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.fs.sync :as sync]
[frontend.fs.watcher-handler :as watcher-handler]
[frontend.handler.file-sync :as file-sync-handler]
@@ -95,7 +95,7 @@
(notification/show! (str "Open link failed. Block-id `" block-id "` doesn't exist in the graph.") :error false)))
file
(if-let [db-page-name (db-model/get-file-page file false)]
(if-let [db-page-name (file-model/get-file-page file false)]
(route-handler/redirect-to-page! db-page-name)
(notification/show! (str "Open link failed. File `" file "` doesn't exist in the graph.") :error false))))))

View File

@@ -29,6 +29,7 @@
[frontend.db-mixins :as db-mixins]
[frontend.db.async :as db-async]
[frontend.db.model :as model]
[frontend.db.file-based.model :as file-model]
[frontend.extensions.highlight :as highlight]
[frontend.extensions.latex :as latex]
[frontend.extensions.lightbox :as lightbox]
@@ -1565,7 +1566,7 @@
config (assoc config :redirect-page-name redirect-page-name)
label-text (get-label-text label)
page (if (string/blank? label-text)
{:block/name (db/get-file-page (string/replace href "file:" "") false)}
{:block/name (file-model/get-file-page (string/replace href "file:" "") false)}
(get-page label))
show-brackets? (state/show-brackets?)]
(if (and page
@@ -1856,7 +1857,7 @@
(let [namespace (first arguments)]
(when-not (string/blank? namespace)
(let [namespace (string/lower-case (page-ref/get-page-name! namespace))
children (model/get-namespace-hierarchy (state/get-current-repo) namespace)]
children (file-model/get-namespace-hierarchy (state/get-current-repo) namespace)]
(namespace-hierarchy config namespace children)))))
(= name "youtube")

View File

@@ -9,6 +9,7 @@
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.async :as db-async]
[frontend.db.file-based.model :as file-model]
[frontend.fs :as fs]
[frontend.state :as state]
[frontend.ui :as ui]
@@ -103,7 +104,7 @@
(let [repo-dir (config/get-repo-dir (state/get-current-repo))
rel-path (when (string/starts-with? path repo-dir)
(path/trim-dir-prefix repo-dir path))
title (db/get-file-page (or path rel-path))
title (file-model/get-file-page (or path rel-path))
in-db? (when-not (path/absolute? path)
(boolean (db/get-file (or path rel-path))))
file-path (cond

View File

@@ -2,7 +2,7 @@
(:require [clojure.string :as string]
[frontend.components.block :as block]
[frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.state :as state]
[frontend.ui :as ui]
[frontend.util :as util]
@@ -23,15 +23,15 @@
(when-let [page (or (first (filter text/namespace-page? all-page-names))
(when (:block/_namespace (db/entity [:block/name (util/page-name-sanity-lc page)]))
page))]
(let [namespace-pages (db/get-namespace-pages repo page)
parent-routes (db-model/get-page-namespace-routes repo page)
(let [namespace-pages (file-model/get-namespace-pages repo page)
parent-routes (file-model/get-page-namespace-routes repo page)
pages (->> (concat namespace-pages parent-routes)
(distinct)
(sort-by :block/name)
(map (fn [page]
(or (:block/title page) (:block/name page))))
(map #(string/split % "/")))
page-namespace (db-model/get-page-namespace repo page)
page-namespace (file-model/get-page-namespace repo page)
page-namespace (util/get-page-title page-namespace)]
(cond
(seq pages)

View File

@@ -9,8 +9,8 @@
[frontend.components.onboarding.quick-tour :as quick-tour]
[frontend.components.page :as page]
[frontend.config :as config]
[frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.fs :as fs]
[frontend.fs.sync :as fs-sync]
[frontend.handler.file-based.nfs :as nfs-handler]
@@ -478,7 +478,7 @@
(map-indexed (fn [i f] (:time f)
(when-let [path (:path f)]
(let [full-path (util/node-path.join (config/get-repo-dir current-repo) path)
page-name (db/get-file-page full-path)]
page-name (file-model/get-file-page full-path)]
{:title [:div.files-history.cursor-pointer
{:key i :class (when (= i 0) "is-first")
:on-click (fn []

View File

@@ -8,6 +8,7 @@
[frontend.db-mixins :as db-mixins]
[frontend.db.async :as db-async]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.query-dsl :as query-dsl]
[frontend.handler.editor :as editor-handler]
[frontend.handler.query.builder :as query-builder]
@@ -321,7 +322,7 @@
[:div
(case @*mode
"namespace"
(let [items (sort (map :block/title (db-model/get-all-namespace-parents repo)))]
(let [items (sort (map :block/title (file-model/get-all-namespace-parents repo)))]
(select items
(fn [{:keys [value]}]
(append-tree! *tree opts loc [:namespace value]))))

View File

@@ -25,21 +25,15 @@
entity pull pull-many]
[frontend.db.model
delete-blocks get-pre-block
delete-files delete-pages-by-files
get-block-and-children get-block-by-uuid get-block-children sort-by-order
delete-files get-block-and-children get-block-by-uuid get-block-children sort-by-order
get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks
get-block-immediate-children get-block-page
get-custom-css
get-file get-file-page get-file-page-id file-exists?
get-files-blocks get-files-full get-journals-length
get-custom-css get-file file-exists? 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
journal-page? page? page-alias-set sub-block sub-entity
get-page-blocks-count get-page-blocks-no-cache get-page-format
get-referenced-blocks journal-page? page? page-alias-set sub-block sub-entity
page-empty? page-exists? get-alias-source-page
has-children? whiteboard-page?
get-namespace-pages get-all-namespace-relation]
has-children? whiteboard-page?]
[frontend.db.react
get-current-page

View File

@@ -10,6 +10,7 @@
[frontend.db.async.util :as db-async-util]
[frontend.db.file-based.async :as file-async]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.react :as react]
[frontend.db.utils :as db-utils]
[frontend.handler.file-based.property.util :as property-util]
@@ -251,7 +252,7 @@
[(>= ?d ?day)])]
date
future-day
db-model/file-graph-block-attrs))]
file-model/file-graph-block-attrs))]
(->> result
db-model/sort-by-order-recursive
db-utils/group-by-page))))))

View File

@@ -0,0 +1,223 @@
(ns frontend.db.file-based.model
"Core db functions for file graphs"
(:require [clojure.string :as string]
[datascript.core :as d]
[frontend.common.file-based.db :as common-file-db]
[frontend.db.utils :as db-utils]
[frontend.db.conn :as conn]
[frontend.state :as state]
[frontend.util :as util]
[logseq.db :as ldb]
[logseq.graph-parser.db :as gp-db]))
(def file-graph-block-attrs
"In file graphs, use it to replace '*' for datalog queries"
'[:db/id
:block/uuid
:block/parent
:block/order
:block/collapsed?
:block/format
:block/refs
:block/_refs
:block/path-refs
:block/tags
:block/link
:block/title
:block/marker
:block/priority
:block/properties
:block/properties-order
:block/properties-text-values
:block/pre-block?
:block/scheduled
:block/deadline
:block/repeated?
:block/created-at
:block/updated-at
;; TODO: remove this in later releases
:block/heading-level
:block/file
:logseq.property/parent
{:block/page [:db/id :block/name :block/title :block/uuid :block/journal-day :block/type]}
{:block/_parent ...}])
;; File-based only
;; Diverged of get-sorted-page-block-ids
(defn get-sorted-page-block-ids-and-levels
"page-name: the page name, original name
return: a list with elements in:
:id - a list of block ids, sorted by :block/order
:level - the level of the block, 1 for root, 2 for children of root, etc."
[page-name]
{:pre [(string? page-name)]}
(let [root (ldb/get-page (conn/get-db) page-name)]
(loop [result []
children (ldb/sort-by-order (:block/_parent root))
;; BFS log of walking depth
levels (repeat (count children) 1)]
(if (seq children)
(let [child (first children)
cur-level (first levels)
next-children (ldb/sort-by-order (:block/_parent child))]
(recur (conj result {:id (:db/id child) :level cur-level})
(concat
next-children
(rest children))
(concat
(repeat (count next-children) (inc cur-level))
(rest levels))))
result))))
(defn get-page-file
([page-name]
(get-page-file (state/get-current-repo) page-name))
([repo page-name]
(when-let [db (conn/get-db repo)]
(gp-db/get-page-file db page-name))))
(defn get-block-file-path
[block]
(when-let [page-id (:db/id (:block/page block))]
(:file/path (:block/file (db-utils/entity page-id)))))
(defn get-file-page-id
[file-path]
(when-let [repo (state/get-current-repo)]
(when-let [db (conn/get-db repo)]
(some->
(d/q
'[:find ?page
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/name]
[?page :block/file ?file]]
db file-path)
db-utils/seq-flatten
first))))
(defn- get-files-blocks
[repo-url paths]
(let [paths (set paths)
pred (fn [_db e]
(contains? paths e))]
(-> (d/q '[:find ?block
:in $ ?pred
:where
[?file :file/path ?path]
[(?pred $ ?path)]
[?p :block/file ?file]
[?block :block/page ?p]]
(conn/get-db repo-url) pred)
db-utils/seq-flatten)))
(defn delete-blocks
[repo-url files _delete-page?]
(when (seq files)
(let [blocks (->> (get-files-blocks repo-url files)
(remove nil?))]
(mapv (fn [eid] [:db.fn/retractEntity eid]) blocks))))
(defn get-file-page
([file-path]
(get-file-page file-path true))
([file-path title?]
(when-let [repo (state/get-current-repo)]
(when-let [db (conn/get-db repo)]
(some->
(d/q
(if title?
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/file ?file]
[?page :block/title ?page-name]]
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/file ?file]
[?page :block/name ?page-name]])
db file-path)
db-utils/seq-flatten
first)))))
;; file-based only so it's safe to use :block/name lookup refs here
(defn delete-pages-by-files
[files]
(let [pages (->> (mapv get-file-page files)
(remove nil?))]
(when (seq pages)
(mapv (fn [page] [:db.fn/retractEntity [:block/name page]]) (map util/page-name-sanity-lc pages)))))
;; TODO: check whether this works when adding pdf back on Web
(defn get-pre-block
[repo page-id]
(-> (d/q '[:find (pull ?b [*])
:in $ ?page
:where
[?b :block/page ?page]
[?b :block/pre-block? true]]
(conn/get-db repo)
page-id)
ffirst))
(defn- get-all-namespace-relation
[repo]
(gp-db/get-all-namespace-relation (conn/get-db repo)))
(defn get-all-namespace-parents
[repo]
(let [db (conn/get-db repo)]
(->> (get-all-namespace-relation repo)
(map (fn [[_ ?parent]]
(db-utils/entity db ?parent))))))
(defn get-namespace-pages
"Accepts both sanitized and unsanitized namespaces"
[repo namespace]
(common-file-db/get-namespace-pages (conn/get-db repo) namespace))
(defn- tree [flat-col root]
(let [sort-fn #(sort-by :block/name %)
children (group-by :block/namespace flat-col)
namespace-children (fn namespace-children [parent-id]
(map (fn [m]
(assoc m :namespace/children
(sort-fn (namespace-children {:db/id (:db/id m)}))))
(sort-fn (get children parent-id))))]
(namespace-children root)))
(defn get-namespace-hierarchy
"Unsanitized namespaces"
[repo namespace]
(let [children (get-namespace-pages repo namespace)
namespace-id (:db/id (db-utils/entity [:block/name (util/page-name-sanity-lc namespace)]))
root {:db/id namespace-id}
col (conj children root)]
(tree col root)))
(defn get-page-namespace
[repo page]
(:block/namespace (db-utils/entity repo [:block/name (util/page-name-sanity-lc page)])))
(defn get-page-namespace-routes
[repo page]
(assert (string? page))
(when-let [db (conn/get-db repo)]
(when-not (string/blank? page)
(let [page (util/page-name-sanity-lc (string/trim page))
page-exist? (db-utils/entity repo [:block/name page])
ids (if page-exist?
'()
(->> (d/datoms db :aevt :block/name)
(filter (fn [datom]
(string/ends-with? (:v datom) (str "/" page))))
(map :e)))]
(when (seq ids)
(db-utils/pull-many repo
'[:db/id :block/name :block/title
{:block/file [:db/id :file/path]}]
ids))))))

View File

@@ -6,11 +6,11 @@
[clojure.string :as string]
[clojure.walk :as walk]
[datascript.core :as d]
[frontend.common.file-based.db :as common-file-db]
[frontend.common.graph-view :as graph-view]
[frontend.config :as config]
[frontend.date :as date]
[frontend.db.conn :as conn]
[frontend.db.file-based.model :as file-model]
[frontend.db.react :as react]
[frontend.db.utils :as db-utils]
[frontend.state :as state]
@@ -21,44 +21,11 @@
[logseq.db.frontend.class :as db-class]
[logseq.db.frontend.content :as db-content]
[logseq.db.frontend.rules :as rules]
[logseq.graph-parser.db :as gp-db]
[logseq.shui.hooks :as hooks]))
;; TODO: extract to specific models and move data transform logic to the
;; corresponding handlers.
(def file-graph-block-attrs
"In file graphs, use it to replace '*' for datalog queries"
'[:db/id
:block/uuid
:block/parent
:block/order
:block/collapsed?
:block/format
:block/refs
:block/_refs
:block/path-refs
:block/tags
:block/link
:block/title
:block/marker
:block/priority
:block/properties
:block/properties-order
:block/properties-text-values
:block/pre-block?
:block/scheduled
:block/deadline
:block/repeated?
:block/created-at
:block/updated-at
;; TODO: remove this in later releases
:block/heading-level
:block/file
:logseq.property/parent
{:block/page [:db/id :block/name :block/title :block/uuid :block/journal-day :block/type]}
{:block/_parent ...}])
(def hidden-page? ldb/hidden?)
(defn get-alias-source-page
@@ -67,21 +34,6 @@
(when-let [db (conn/get-db repo)]
(ldb/get-alias-source-page db alias-id)))
(defn get-files-blocks
[repo-url paths]
(let [paths (set paths)
pred (fn [_db e]
(contains? paths e))]
(-> (d/q '[:find ?block
:in $ ?pred
:where
[?file :file/path ?path]
[(?pred $ ?path)]
[?p :block/file ?file]
[?block :block/page ?p]]
(conn/get-db repo-url) pred)
db-utils/seq-flatten)))
(defn file-exists?
[repo path]
(when (and repo path)
@@ -261,33 +213,6 @@ independent of format as format specific heading characters are stripped"
f))
form))
;; File-based only
;; Diverged of get-sorted-page-block-ids
(defn get-sorted-page-block-ids-and-levels
"page-name: the page name, original name
return: a list with elements in:
:id - a list of block ids, sorted by :block/order
:level - the level of the block, 1 for root, 2 for children of root, etc."
[page-name]
{:pre [(string? page-name)]}
(let [root (ldb/get-page (conn/get-db) page-name)]
(loop [result []
children (sort-by-order (:block/_parent root))
;; BFS log of walking depth
levels (repeat (count children) 1)]
(if (seq children)
(let [child (first children)
cur-level (first levels)
next-children (sort-by-order (:block/_parent child))]
(recur (conj result {:id (:db/id child) :level cur-level})
(concat
next-children
(rest children))
(concat
(repeat (count next-children) (inc cur-level))
(rest levels))))
result))))
(defn has-children?
([block-id]
(has-children? (conn/get-db) block-id))
@@ -421,59 +346,6 @@ independent of format as format specific heading characters are stripped"
(let [db (conn/get-db repo)]
(ldb/get-block-and-children db block-uuid opts)))
(defn get-file-page
([file-path]
(get-file-page file-path true))
([file-path title?]
(when-let [repo (state/get-current-repo)]
(when-let [db (conn/get-db repo)]
(some->
(d/q
(if title?
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/file ?file]
[?page :block/title ?page-name]]
'[:find ?page-name
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/file ?file]
[?page :block/name ?page-name]])
db file-path)
db-utils/seq-flatten
first)))))
(defn get-page-file
([page-name]
(get-page-file (state/get-current-repo) page-name))
([repo page-name]
(when-let [db (conn/get-db repo)]
(gp-db/get-page-file db page-name))))
(defn get-block-file-path
[block]
(when-let [page-id (:db/id (:block/page block))]
(:file/path (:block/file (db-utils/entity page-id)))))
(defn get-file-page-id
[file-path]
(when-let [repo (state/get-current-repo)]
(when-let [db (conn/get-db repo)]
(some->
(d/q
'[:find ?page
:in $ ?path
:where
[?file :file/path ?path]
[?page :block/name]
[?page :block/file ?file]]
db file-path)
db-utils/seq-flatten
first))))
(defn get-page
[page-id-name-or-uuid]
(when page-id-name-or-uuid
@@ -574,7 +446,7 @@ independent of format as format specific heading characters are stripped"
[?block :block/path-refs ?ref-page]]
db
pages
(butlast file-graph-block-attrs))
(butlast file-model/file-graph-block-attrs))
(remove (fn [block] (= page-id (:db/id (:block/page block)))))
db-utils/group-by-page
(map (fn [[k blocks]]
@@ -635,37 +507,10 @@ independent of format as format specific heading characters are stripped"
[?refed-b :block/uuid ?refed-uuid]
[?referee-b :block/refs ?refed-b]] db)))
(defn delete-blocks
[repo-url files _delete-page?]
(when (seq files)
(let [blocks (->> (get-files-blocks repo-url files)
(remove nil?))]
(mapv (fn [eid] [:db.fn/retractEntity eid]) blocks))))
(defn delete-files
[files]
(mapv (fn [path] [:db.fn/retractEntity [:file/path path]]) files))
;; file-based only so it's safe to use :block/name lookup refs here
(defn delete-pages-by-files
[files]
(let [pages (->> (mapv get-file-page files)
(remove nil?))]
(when (seq pages)
(mapv (fn [page] [:db.fn/retractEntity [:block/name page]]) (map util/page-name-sanity-lc pages)))))
;; TODO: check whether this works when adding pdf back on Web
(defn get-pre-block
[repo page-id]
(-> (d/q '[:find (pull ?b [*])
:in $ ?page
:where
[?b :block/page ?page]
[?b :block/pre-block? true]]
(conn/get-db repo)
page-id)
ffirst))
(defn whiteboard-page?
"Given a page entity, page object or page name, check if it is a whiteboard page"
[page]
@@ -674,17 +519,6 @@ independent of format as format specific heading characters are stripped"
page)]
(ldb/whiteboard? page)))
(comment
(defn get-orphaned-pages
[opts]
(let [db (conn/get-db)]
(ldb/get-orphaned-pages db
(merge opts
{:built-in-pages-names
(if (config/db-based-graph? (state/get-current-repo))
sqlite-create-graph/built-in-pages-names
gp-db/built-in-pages-names)})))))
;; FIXME: use `Untitled` instead of UUID for db based graphs
(defn untitled-page?
[page-name]
@@ -771,64 +605,6 @@ independent of format as format specific heading characters are stripped"
(:block/_tags class))
(remove ldb/hidden?))))
(defn get-all-namespace-relation
[repo]
(gp-db/get-all-namespace-relation (conn/get-db repo)))
(defn get-all-namespace-parents
[repo]
(let [db (conn/get-db repo)]
(->> (get-all-namespace-relation repo)
(map (fn [[_ ?parent]]
(db-utils/entity db ?parent))))))
(defn get-namespace-pages
"Accepts both sanitized and unsanitized namespaces"
[repo namespace]
(common-file-db/get-namespace-pages (conn/get-db repo) namespace))
(defn- tree [flat-col root]
(let [sort-fn #(sort-by :block/name %)
children (group-by :block/namespace flat-col)
namespace-children (fn namespace-children [parent-id]
(map (fn [m]
(assoc m :namespace/children
(sort-fn (namespace-children {:db/id (:db/id m)}))))
(sort-fn (get children parent-id))))]
(namespace-children root)))
(defn get-namespace-hierarchy
"Unsanitized namespaces"
[repo namespace]
(let [children (get-namespace-pages repo namespace)
namespace-id (:db/id (db-utils/entity [:block/name (util/page-name-sanity-lc namespace)]))
root {:db/id namespace-id}
col (conj children root)]
(tree col root)))
(defn get-page-namespace
[repo page]
(:block/namespace (db-utils/entity repo [:block/name (util/page-name-sanity-lc page)])))
(defn get-page-namespace-routes
[repo page]
(assert (string? page))
(when-let [db (conn/get-db repo)]
(when-not (string/blank? page)
(let [page (util/page-name-sanity-lc (string/trim page))
page-exist? (db-utils/entity repo [:block/name page])
ids (if page-exist?
'()
(->> (d/datoms db :aevt :block/name)
(filter (fn [datom]
(string/ends-with? (:v datom) (str "/" page))))
(map :e)))]
(when (seq ids)
(db-utils/pull-many repo
'[:db/id :block/name :block/title
{:block/file [:db/id :file/path]}]
ids))))))
(comment
;; For debugging
(defn get-all-blocks

View File

@@ -2,7 +2,7 @@
"Handles executing custom queries a.k.a. advanced queries"
(:require [clojure.walk :as walk]
[frontend.config :as config]
[frontend.db.model :as model]
[frontend.db.file-based.model :as file-model]
[frontend.db.query-dsl :as query-dsl]
[frontend.db.query-react :as query-react]
[frontend.state :as state]
@@ -13,7 +13,7 @@
;; FIXME: what if users want to query other attributes than block-attrs?
(defn- replace-star-with-block-attrs!
[l]
(let [block-attrs (butlast model/file-graph-block-attrs)]
(let [block-attrs (butlast file-model/file-graph-block-attrs)]
(walk/postwalk
(fn [f]
(if (and (list? f)

View File

@@ -8,7 +8,7 @@
[clojure.walk :as walk]
[frontend.config :as config]
[frontend.date :as date]
[frontend.db.model :as model]
[frontend.db.file-based.model :as file-model]
[frontend.db.query-react :as query-react]
[frontend.db.utils :as db-utils]
[frontend.state :as state]
@@ -707,7 +707,7 @@ Some bindings in this fn:
(defn query-wrapper
[where {:keys [blocks? block-attrs]}]
(let [block-attrs (or block-attrs (butlast model/file-graph-block-attrs))
(let [block-attrs (or block-attrs (butlast file-model/file-graph-block-attrs))
q (if blocks? ; FIXME: it doesn't need to be either blocks or pages
`[:find (~'pull ~'?b ~block-attrs)
:in ~'$ ~'%

View File

@@ -2,7 +2,7 @@
"Implementation of text (file) based content diff & merge for conflict resolution"
(:require ["@logseq/diff-merge" :refer [attach_uuids Differ Merger]]
[cljs-bean.core :as bean]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.utils :as db-utils]
[logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.mldoc :as gp-mldoc]
@@ -29,7 +29,7 @@
page-name: string"
[page-name]
{:pre (string? page-name)}
(let [walked (db-model/get-sorted-page-block-ids-and-levels page-name)
(let [walked (file-model/get-sorted-page-block-ids-and-levels page-name)
blocks (db-utils/pull-many [:block/uuid :block/title :block/level] (map :id walked))
levels (map :level walked)
blocks (map (fn [block level]

View File

@@ -6,6 +6,7 @@
[frontend.db :as db]
[frontend.db.async :as db-async]
[frontend.db.model :as model]
[frontend.db.file-based.model :as file-model]
[frontend.fs :as fs]
[frontend.handler.file-based.file :as file-handler]
[frontend.handler.file-based.property :as file-property-handler]
@@ -101,7 +102,7 @@
exists-in-db?)
(p/let [dir-exists? (fs/file-exists? dir "")]
(when dir-exists?
(when-let [page-name (db/get-file-page path)]
(when-let [page-name (file-model/get-file-page path)]
(println "Delete page: " page-name ", file path: " path ".")
(page-handler/<delete! page-name #()))))
@@ -177,7 +178,7 @@
(p/do!
(when (seq deleted-files)
(p/all (map (fn [path]
(when-let [page-name (db/get-file-page path)]
(when-let [page-name (file-model/get-file-page path)]
(println "Delete page: " page-name ", file path: " path ".")
(page-handler/<delete! page-name #())))
deleted-files)))

View File

@@ -9,6 +9,7 @@
[frontend.db :as db]
[frontend.db.async :as db-async]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.utils :as db-utils]
[frontend.diff :as diff]
[frontend.extensions.pdf.utils :as pdf-utils]
@@ -1397,7 +1398,7 @@
(delete-block-aux! asset-block)
(when-let [href (if (util/electron?) href
(second (re-find #"\((.+)\)$" full-text)))]
(let [block-file-rpath (db-model/get-block-file-path block)
(let [block-file-rpath (file-model/get-block-file-path block)
asset-fpath (if (string/starts-with? href "assets://")
(path/url-to-path href)
(config/get-repo-fpath

View File

@@ -5,7 +5,7 @@
[frontend.config :as config]
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.query-dsl :as query-dsl]
[frontend.format.block :as block]
[frontend.format.mldoc :as mldoc]
@@ -354,7 +354,7 @@
Requires editing state"
[file-path]
(if-let [current-file-rpath (or (db-model/get-block-file-path (state/get-edit-block))
(if-let [current-file-rpath (or (file-model/get-block-file-path (state/get-edit-block))
;; fix dummy file path of page
(when (config/get-pages-directory)
(path/path-join (config/get-pages-directory) "_.md"))

View File

@@ -3,6 +3,7 @@
(:refer-clojure :exclude [load-file])
(:require [frontend.config :as config]
[frontend.db :as db]
[frontend.db.file-based.model :as file-model]
[frontend.fs :as fs]
[frontend.fs.nfs :as nfs]
[frontend.fs.capacitor-fs :as capacitor-fs]
@@ -163,7 +164,7 @@
result (if reset?
(do
(when-not skip-db-transact?
(when-let [page-id (db/get-file-page-id path)]
(when-let [page-id (file-model/get-file-page-id path)]
(db/transact! repo
[[:db/retract page-id :block/alias]
[:db/retract page-id :block/tags]]

View File

@@ -2,8 +2,7 @@
(:require [clojure.string :as string]
[frontend.config :as config]
[frontend.date :as date]
[frontend.db :as db]
[frontend.db.model :as model]
[frontend.db.file-based.model :as file-model]
[frontend.handler.common.page :as page-common-handler]
[frontend.mobile.util :as mobile-util]
[frontend.state :as state]
@@ -29,11 +28,11 @@
(defn get-page-ref-text
[page]
(let [edit-block-file-path (model/get-block-file-path (state/get-edit-block))
(let [edit-block-file-path (file-model/get-block-file-path (state/get-edit-block))
page-name (string/lower-case page)]
(if (and edit-block-file-path
(state/org-mode-file-link? (state/get-current-repo)))
(if-let [ref-file-path (:file/path (db/get-page-file page-name))]
(if-let [ref-file-path (:file/path (file-model/get-page-file page-name))]
(util/format "[[file:%s][%s]]"
(util/get-relative-path edit-block-file-path ref-file-path)
page)

View File

@@ -2,6 +2,7 @@
"Page property fns for file graphs"
(:require [clojure.string :as string]
[frontend.db :as db]
[frontend.db.file-based.model :as file-model]
[frontend.modules.outliner.op :as outliner-op]
[frontend.modules.outliner.ui :as ui-outliner-tx]
[frontend.state :as state]
@@ -53,7 +54,7 @@
[page key value]
(let [repo (state/get-current-repo)
key (keyword key)
pre-block (db/get-pre-block repo (:db/id page))
pre-block (file-model/get-pre-block repo (:db/id page))
format (state/get-preferred-format)
page-id {:db/id (:db/id page)}
org? (= format :org)

View File

@@ -2,6 +2,7 @@
"Repo fns for creating, loading and parsing file graphs"
(:require [frontend.config :as config]
[frontend.db :as db]
[frontend.db.file-based.model :as file-model]
[frontend.fs :as fs]
[frontend.handler.file-based.file :as file-handler]
[frontend.handler.repo-config :as repo-config-handler]
@@ -313,14 +314,14 @@
add-files (filter-diffs "add")
delete-files (when (seq remove-files)
(db/delete-files remove-files))
delete-blocks (db/delete-blocks repo-url remove-files true)
delete-blocks (file-model/delete-blocks repo-url remove-files true)
delete-blocks (->>
(concat
delete-blocks
(db/delete-blocks repo-url modify-files false))
(file-model/delete-blocks repo-url modify-files false))
(remove nil?))
delete-pages (if (seq remove-files)
(db/delete-pages-by-files remove-files)
(file-model/delete-pages-by-files remove-files)
[])
add-or-modify-files (some->>
(concat modify-files add-files)

View File

@@ -3,6 +3,7 @@
(:require [frontend.config :as config]
[frontend.state :as state]
[frontend.db :as db]
[frontend.db.file-based.model :as file-model]
[logseq.graph-parser :as graph-parser]
[logseq.common.util :as common-util]
[frontend.fs.diff-merge :as diff-merge]
@@ -17,7 +18,7 @@
"Conflict of files towards same page"
[repo-url page file]
(when-let [page-name (:block/name page)]
(let [current-file (:file/path (db/get-page-file repo-url page-name))]
(let [current-file (:file/path (file-model/get-page-file repo-url page-name))]
(when (not= file current-file)
current-file))))

View File

@@ -10,6 +10,7 @@
[frontend.db.async :as db-async]
[frontend.db.conn :as conn]
[frontend.db.model :as db-model]
[frontend.db.file-based.model :as file-model]
[frontend.db.query-custom :as query-custom]
[frontend.db.query-dsl :as query-dsl]
[frontend.db.query-react :as query-react]
@@ -1046,13 +1047,13 @@
(defn ^:export get_pages_from_namespace
[ns]
(when-let [repo (and ns (state/get-current-repo))]
(when-let [pages (db-model/get-namespace-pages repo ns)]
(when-let [pages (file-model/get-namespace-pages repo ns)]
(bean/->js (sdk-utils/normalize-keyword-for-json pages)))))
(defn ^:export get_pages_tree_from_namespace
[ns]
(when-let [repo (and ns (state/get-current-repo))]
(when-let [pages (db-model/get-namespace-hierarchy repo ns)]
(when-let [pages (file-model/get-namespace-hierarchy repo ns)]
(bean/->js (sdk-utils/normalize-keyword-for-json pages)))))
(defn- first-child-of-block

View File

@@ -0,0 +1,33 @@
(ns frontend.db.file-based.model-test
(:require [cljs.test :refer [use-fixtures deftest is]]
[frontend.db.file-based.model :as file-model]
[frontend.test.helper :as test-helper :refer [load-test-files]]))
(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})
(deftest get-namespace-pages
(load-test-files [{:file/path "pages/a.b.c.md"
:file/content "foo"}
{:file/path "pages/b.c.md"
:file/content "bar"}
{:file/path "pages/b.d.md"
:file/content "baz"}])
(is (= ["a/b" "a/b/c"]
(map :block/name (file-model/get-namespace-pages test-helper/test-db "a"))))
(is (= ["b/c" "b/d"]
(map :block/name (file-model/get-namespace-pages test-helper/test-db "b")))))
(deftest get-page-namespace-routes
(load-test-files [{:file/path "pages/a.b.c.md"
:file/content "foo"}
{:file/path "pages/b.c.md"
:file/content "bar"}
{:file/path "pages/b.d.md"
:file/content "baz"}])
(is (= '()
(map :block/name (file-model/get-page-namespace-routes test-helper/test-db "b/c")))
"Empty if page exists"))

View File

@@ -9,32 +9,6 @@
(use-fixtures :each {:before test-helper/start-test-db!
:after test-helper/destroy-test-db!})
(deftest get-namespace-pages
(load-test-files [{:file/path "pages/a.b.c.md"
:file/content "foo"}
{:file/path "pages/b.c.md"
:file/content "bar"}
{:file/path "pages/b.d.md"
:file/content "baz"}])
(is (= ["a/b" "a/b/c"]
(map :block/name (model/get-namespace-pages test-helper/test-db "a"))))
(is (= ["b/c" "b/d"]
(map :block/name (model/get-namespace-pages test-helper/test-db "b")))))
(deftest get-page-namespace-routes
(load-test-files [{:file/path "pages/a.b.c.md"
:file/content "foo"}
{:file/path "pages/b.c.md"
:file/content "bar"}
{:file/path "pages/b.d.md"
:file/content "baz"}])
(is (= '()
(map :block/name (model/get-page-namespace-routes test-helper/test-db "b/c")))
"Empty if page exists"))
(deftest test-page-alias-with-multiple-alias
(load-test-files [{:file/path "aa.md"
:file/content "alias:: ab, ac"}