|
|
@@ -1,17 +1,18 @@
|
|
|
(ns frontend.worker.handler.page.db-based.page
|
|
|
"Page operations for DB graphs"
|
|
|
- (:require [logseq.db :as ldb]
|
|
|
- [logseq.graph-parser.block :as gp-block]
|
|
|
- [logseq.db.sqlite.util :as sqlite-util]
|
|
|
+ (:require [clojure.string :as string]
|
|
|
[datascript.core :as d]
|
|
|
- [clojure.string :as string]
|
|
|
- [logseq.graph-parser.text :as text]
|
|
|
+ [datascript.impl.entity :as de]
|
|
|
[logseq.common.util :as common-util]
|
|
|
+ [logseq.common.util.namespace :as ns-util]
|
|
|
+ [logseq.db :as ldb]
|
|
|
+ [logseq.db.frontend.class :as db-class]
|
|
|
[logseq.db.frontend.order :as db-order]
|
|
|
- [logseq.db.frontend.property.util :as db-property-util]
|
|
|
[logseq.db.frontend.property.build :as db-property-build]
|
|
|
- [logseq.db.frontend.class :as db-class]
|
|
|
- [logseq.outliner.core :as outliner-core]))
|
|
|
+ [logseq.db.frontend.property.util :as db-property-util]
|
|
|
+ [logseq.db.sqlite.util :as sqlite-util]
|
|
|
+ [logseq.graph-parser.block :as gp-block]
|
|
|
+ [logseq.graph-parser.text :as text]))
|
|
|
|
|
|
(defn- build-page-tx [conn properties page {:keys [whiteboard? class? tags]}]
|
|
|
(when (:block/uuid page)
|
|
|
@@ -68,6 +69,80 @@
|
|
|
:block/title ""
|
|
|
:block/format format})]))
|
|
|
|
|
|
+(defn- get-page-by-parent-name
|
|
|
+ [db parent-title child-title]
|
|
|
+ (some->>
|
|
|
+ (d/q
|
|
|
+ '[:find [?b ...]
|
|
|
+ :in $ ?parent-name ?child-name
|
|
|
+ :where
|
|
|
+ [?b :logseq.property/parent ?p]
|
|
|
+ [?b :block/name ?child-name]
|
|
|
+ [?p :block/name ?parent-name]]
|
|
|
+ db
|
|
|
+ (common-util/page-name-sanity-lc parent-title)
|
|
|
+ (common-util/page-name-sanity-lc child-title))
|
|
|
+ first
|
|
|
+ (d/entity db)))
|
|
|
+
|
|
|
+(defn- split-namespace-pages
|
|
|
+ [db page tags date-formatter & {:keys [*changed-uuids]}]
|
|
|
+ (let [tags-set (set (map :block/uuid tags))]
|
|
|
+ (->>
|
|
|
+ (let [{:block/keys [title]} page
|
|
|
+ block-uuid (:block/uuid page)
|
|
|
+ block-type (if (contains? tags-set (:block/uuid page))
|
|
|
+ "class"
|
|
|
+ (:block/type page))]
|
|
|
+ (if (and (contains? #{"page" "class"} block-type) (ns-util/namespace-page? title))
|
|
|
+ (let [class? (= block-type "class")
|
|
|
+ parts (->> (string/split title ns-util/parent-re)
|
|
|
+ (map string/trim)
|
|
|
+ (remove string/blank?))
|
|
|
+ pages (doall
|
|
|
+ (map-indexed
|
|
|
+ (fn [idx part]
|
|
|
+ (let [last-part? (= idx (dec (count parts)))
|
|
|
+ page (if (zero? idx)
|
|
|
+ (ldb/get-page db part)
|
|
|
+ (get-page-by-parent-name db (nth parts (dec idx)) part))
|
|
|
+ result (or page
|
|
|
+ (when last-part? (ldb/get-page db part))
|
|
|
+ (-> (gp-block/page-name->map part db true date-formatter
|
|
|
+ {:page-uuid (when last-part? block-uuid)})
|
|
|
+ (assoc :block/format :markdown)))]
|
|
|
+ (when (and last-part? (not= (:block/uuid result) block-uuid)
|
|
|
+ *changed-uuids)
|
|
|
+ (swap! *changed-uuids assoc block-uuid (:block/uuid result)))
|
|
|
+ result))
|
|
|
+ parts))]
|
|
|
+ (cond
|
|
|
+ (and (not class?) (ldb/class? (first pages)))
|
|
|
+ [page]
|
|
|
+
|
|
|
+ :else
|
|
|
+ (map-indexed
|
|
|
+ (fn [idx page]
|
|
|
+ (let [parent-eid (when (> idx 0)
|
|
|
+ (when-let [id (:block/uuid (nth pages (dec idx)))]
|
|
|
+ [:block/uuid id]))]
|
|
|
+ (if class?
|
|
|
+ (cond
|
|
|
+ (and (de/entity? page) (ldb/class? page))
|
|
|
+ page
|
|
|
+ (de/entity? page) ; page exists but not a class, avoid converting here becuase this could be troublesome.
|
|
|
+ nil
|
|
|
+ (zero? idx)
|
|
|
+ (db-class/build-new-class db page)
|
|
|
+ :else
|
|
|
+ (db-class/build-new-class db (assoc page :logseq.property/parent parent-eid)))
|
|
|
+ (if (or (de/entity? page) (zero? idx))
|
|
|
+ page
|
|
|
+ (assoc page :logseq.property/parent parent-eid)))))
|
|
|
+ pages)))
|
|
|
+ [page]))
|
|
|
+ (remove nil?))))
|
|
|
+
|
|
|
(defn create!
|
|
|
[conn title*
|
|
|
{:keys [create-first-block? properties uuid persist-op? whiteboard? class? today-journal? split-namespace?]
|
|
|
@@ -95,7 +170,7 @@
|
|
|
:skip-existing-page-check? true})
|
|
|
(assoc :block/format format))
|
|
|
[page parents] (if (and (text/namespace-page? title) split-namespace?)
|
|
|
- (let [pages (outliner-core/split-namespace-pages db page nil date-formatter)]
|
|
|
+ (let [pages (split-namespace-pages db page nil date-formatter)]
|
|
|
[(last pages) (butlast pages)])
|
|
|
[page nil])]
|
|
|
(when page
|