浏览代码

Merge branch 'feat/db' into feat/text-template

Tienson Qin 7 月之前
父节点
当前提交
4b5989a541

+ 2 - 1
deps/db/nbb.edn

@@ -7,4 +7,5 @@
   logseq/clj-fractional-indexing        {:git/url "https://github.com/logseq/clj-fractional-indexing"
   logseq/clj-fractional-indexing        {:git/url "https://github.com/logseq/clj-fractional-indexing"
                                          :sha     "7182b7878410f78536dc2b6df35ed32ef9cd6b61"}
                                          :sha     "7182b7878410f78536dc2b6df35ed32ef9cd6b61"}
   io.github.nextjournal/nbb-test-runner
   io.github.nextjournal/nbb-test-runner
-  {:git/sha "60ed57aa04bca8d604f5ba6b28848bd887109347"}}}
+  {:git/sha "60ed57aa04bca8d604f5ba6b28848bd887109347"}
+  io.github.pez/baldr {:mvn/version "1.0.9"}}}

+ 2 - 1
deps/db/package.json

@@ -9,6 +9,7 @@
     "better-sqlite3": "9.3.0"
     "better-sqlite3": "9.3.0"
   },
   },
   "scripts": {
   "scripts": {
-    "test": "yarn nbb-logseq -cp test -m nextjournal.test-runner"
+    "test": "yarn nbb-logseq -cp test -m nextjournal.test-runner",
+    "test-v": "yarn nbb-logseq -cp test -m logseq.db.test-runner"
   }
   }
 }
 }

+ 12 - 6
deps/db/src/logseq/db/sqlite/build.cljs

@@ -618,12 +618,18 @@
                                  m))
                                  m))
                              properties-tx)
                              properties-tx)
         pages-and-blocks-tx (build-pages-and-blocks-tx pages-and-blocks' all-idents page-uuids
         pages-and-blocks-tx (build-pages-and-blocks-tx pages-and-blocks' all-idents page-uuids
-                                                       (assoc options :properties properties))]
-    ;; Properties first b/c they have schema and are referenced by all. Then classes b/c they can be referenced by pages. Then pages
-    (split-blocks-tx (concat properties-tx'
-                             classes-tx
-                             pages-and-blocks-tx)
-                     properties)))
+                                                       (assoc options :properties properties))
+        ;; Properties first b/c they have schema and are referenced by all. Then
+        ;; classes b/c they can be referenced by pages. Then pages
+        split-txs (split-blocks-tx (concat properties-tx' classes-tx pages-and-blocks-tx)
+                                   properties)]
+    (cond-> split-txs
+      ;; Just add indices option as there are too many out of order uuid cases with importing user content
+      (:build-existing-tx? options)
+      (update :init-tx
+              (fn [init-tx]
+                (let [indices (mapv #(select-keys % [:block/uuid]) (filter :block/uuid init-tx))]
+                  (into indices init-tx)))))))
 
 
 ;; Public API
 ;; Public API
 ;; ==========
 ;; ==========

+ 16 - 9
deps/db/src/logseq/db/sqlite/export.cljs

@@ -11,7 +11,8 @@
             [logseq.db.frontend.entity-plus :as entity-plus]
             [logseq.db.frontend.entity-plus :as entity-plus]
             [logseq.db.frontend.entity-util :as entity-util]
             [logseq.db.frontend.entity-util :as entity-util]
             [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.property :as db-property]
-            [logseq.db.sqlite.build :as sqlite-build]))
+            [logseq.db.sqlite.build :as sqlite-build]
+            [logseq.db.frontend.malli-schema :as db-malli-schema]))
 
 
 ;; Export fns
 ;; Export fns
 ;; ==========
 ;; ==========
@@ -48,8 +49,11 @@
         [:build/page {:build/journal (:block/journal-day pvalue)}]
         [:build/page {:build/journal (:block/journal-day pvalue)}]
         :else
         :else
         (if (= :node (:logseq.property/type property-ent))
         (if (= :node (:logseq.property/type property-ent))
-          ;; Have to distinguish from block references that don't exist like closed values
-          ^::existing-property-value? [:block/uuid (:block/uuid pvalue)]
+          ;; Internal idents take precedence over uuid because they are keep data graph-agnostic
+          (if (some-> pvalue :db/ident db-malli-schema/internal-ident?)
+            (:db/ident pvalue)
+            ;; Use metadata distinguish from block references that don't exist like closed values
+            ^::existing-property-value? [:block/uuid (:block/uuid pvalue)])
           (or (:db/ident pvalue)
           (or (:db/ident pvalue)
               ;; nbb-compatible version of db-property/property-value-content
               ;; nbb-compatible version of db-property/property-value-content
               (or (block-title pvalue)
               (or (block-title pvalue)
@@ -177,16 +181,16 @@
 (defn- build-node-export
 (defn- build-node-export
   "Given a block/page entity and optional existing properties, build an export map of its
   "Given a block/page entity and optional existing properties, build an export map of its
    tags and properties"
    tags and properties"
-  [db entity {:keys [properties include-uuid-fn keep-uuid? shallow-copy?]
+  [db entity {:keys [properties include-uuid-fn shallow-copy?]
               :or {include-uuid-fn (constantly false)}}]
               :or {include-uuid-fn (constantly false)}}]
   (let [ent-properties (dissoc (db-property/properties entity) :block/tags)
   (let [ent-properties (dissoc (db-property/properties entity) :block/tags)
         build-tags (when (seq (:block/tags entity)) (->build-tags (:block/tags entity)))
         build-tags (when (seq (:block/tags entity)) (->build-tags (:block/tags entity)))
         new-properties (when-not shallow-copy? (build-node-properties db entity ent-properties properties))
         new-properties (when-not shallow-copy? (build-node-properties db entity ent-properties properties))
         build-node (cond-> {:block/title (block-title entity)}
         build-node (cond-> {:block/title (block-title entity)}
+                     (:block/link entity)
+                     (assoc :block/link [:block/uuid (:block/uuid (:block/link entity))])
                      (include-uuid-fn (:block/uuid entity))
                      (include-uuid-fn (:block/uuid entity))
                      (assoc :block/uuid (:block/uuid entity) :build/keep-uuid? true)
                      (assoc :block/uuid (:block/uuid entity) :build/keep-uuid? true)
-                     keep-uuid?
-                     (assoc :build/keep-uuid? true)
                      (and (not shallow-copy?) (seq build-tags))
                      (and (not shallow-copy?) (seq build-tags))
                      (assoc :build/tags build-tags)
                      (assoc :build/tags build-tags)
                      (and (not shallow-copy?) (seq ent-properties))
                      (and (not shallow-copy?) (seq ent-properties))
@@ -247,12 +251,16 @@
       classes (assoc :classes classes))))
       classes (assoc :classes classes))))
 
 
 (defn- build-content-ref-export
 (defn- build-content-ref-export
-  "Builds an export config (and additional info) for refs in the given blocks. All the exported
+  "Builds an export config (and additional info) for refs in the given blocks. Refs are detected
+   if they are a :block/link or if a `[[UUID]]` ref in the content. All the exported
    entities found in block refs include their uuid in order to preserve the relationship to the blocks"
    entities found in block refs include their uuid in order to preserve the relationship to the blocks"
   [db blocks*]
   [db blocks*]
   (let [;; Remove property value blocks that can't have content refs
   (let [;; Remove property value blocks that can't have content refs
         blocks (remove :logseq.property/value blocks*)
         blocks (remove :logseq.property/value blocks*)
-        content-ref-uuids (set (mapcat (comp db-content/get-matched-ids block-title) blocks))
+        block-links (->> (filter :block/link blocks)
+                         (map #(:block/uuid (:block/link %))))
+        content-ref-uuids (into (set (mapcat (comp db-content/get-matched-ids block-title) blocks))
+                                block-links)
         content-ref-ents (map #(d/entity db [:block/uuid %]) content-ref-uuids)
         content-ref-ents (map #(d/entity db [:block/uuid %]) content-ref-uuids)
         content-ref-pages (filter #(or (entity-util/internal-page? %) (entity-util/journal? %)) content-ref-ents)
         content-ref-pages (filter #(or (entity-util/internal-page? %) (entity-util/journal? %)) content-ref-ents)
         {:keys [properties classes]}
         {:keys [properties classes]}
@@ -326,7 +334,6 @@
                       (merge (build-blocks-export db
                       (merge (build-blocks-export db
                                                   (sort-by :block/order blocks)
                                                   (sort-by :block/order blocks)
                                                   {:include-uuid-fn (constantly true)
                                                   {:include-uuid-fn (constantly true)
-                                                   :keep-uuid? true
                                                    ;; shallow copy to disallow failing pvalues
                                                    ;; shallow copy to disallow failing pvalues
                                                    :shallow-copy? true})
                                                    :shallow-copy? true})
                              {:page (shallow-copy-page parent-page-ent)})))))]
                              {:page (shallow-copy-page parent-page-ent)})))))]

+ 3 - 3
deps/db/test/logseq/db/sqlite/create_graph_test.cljs

@@ -163,9 +163,9 @@
                                        :block/order ;; TODO: block/order should be same as well
                                        :block/order ;; TODO: block/order should be same as well
                                        ))
                                        ))
                      init-data*)))]
                      init-data*)))]
-      (let [[first-only second-only common]
+      (let [[first-only second-only _common]
             (data/diff (remove-ignored-attrs&entities (sqlite-create-graph/build-db-initial-data ""))
             (data/diff (remove-ignored-attrs&entities (sqlite-create-graph/build-db-initial-data ""))
                        (remove-ignored-attrs&entities (sqlite-create-graph/build-db-initial-data "")))]
                        (remove-ignored-attrs&entities (sqlite-create-graph/build-db-initial-data "")))]
+        ;; (pr-str [first-only second-only _common])
         (is (and (every? nil? first-only)
         (is (and (every? nil? first-only)
-                 (every? nil? second-only))
-            (pr-str [first-only second-only common]))))))
+                 (every? nil? second-only)))))))

+ 23 - 1
deps/db/test/logseq/db/sqlite/export_test.cljs

@@ -311,6 +311,27 @@
                                       (into (vec (remove #(= "hola" (:block/title %)) bs))
                                       (into (vec (remove #(= "hola" (:block/title %)) bs))
                                             bs))})))
                                             bs))})))
 
 
+(deftest import-page-with-block-links
+  (let [block-uuid (random-uuid)
+        original-data
+        {:pages-and-blocks
+         [{:page {:block/title "page1"}
+           :blocks [{:block/title "b1" :block/uuid block-uuid :build/keep-uuid? true}
+                    {:block/title "" :block/link [:block/uuid block-uuid]}]}]}
+        ;; add option to test out of order uuids
+        conn (db-test/create-conn-with-blocks (assoc original-data :build-existing-tx? true))
+        conn2 (db-test/create-conn)
+        imported-page (export-page-and-import-to-another-graph conn conn2 "page1")]
+    (is (= (:pages-and-blocks original-data) (:pages-and-blocks imported-page))
+        "Page's blocks are imported")
+
+    (import-second-time-assertions conn conn2 "page1" original-data
+                                   {:transform-expected-blocks
+                                    (fn [bs]
+                                      ;; internal referenced block doesn't get copied b/c it already exists
+                                      (into (vec (remove #(= "b1" (:block/title %)) bs))
+                                            bs))})))
+
 (deftest import-page-with-different-page-and-classes
 (deftest import-page-with-different-page-and-classes
   (let [original-data
   (let [original-data
         {:properties {:user.property/p1 {:logseq.property/type :default}
         {:properties {:user.property/p1 {:logseq.property/type :default}
@@ -392,7 +413,8 @@
                     {:block/title "node block"
                     {:block/title "node block"
                      :build/properties {:user.property/node #{[:build/page {:block/title "page object"
                      :build/properties {:user.property/node #{[:build/page {:block/title "page object"
                                                                             :build/tags [:user.class/MyClass]}]
                                                                             :build/tags [:user.class/MyClass]}]
-                                                              [:block/uuid block-object-uuid]}}}]}
+                                                              [:block/uuid block-object-uuid]
+                                                              :logseq.class/Task}}}]}
           {:page {:block/title "Blocks"}
           {:page {:block/title "Blocks"}
            :blocks [{:block/title "myclass object"
            :blocks [{:block/title "myclass object"
                      :build/tags [:user.class/MyClass]
                      :build/tags [:user.class/MyClass]

+ 6 - 0
deps/db/test/logseq/db/test_runner.cljs

@@ -0,0 +1,6 @@
+(ns logseq.db.test-runner
+  "Test runner which enables https://github.com/PEZ/baldr by default"
+  (:require [nextjournal.test-runner :as next-runner]
+            [pez.baldr]))
+
+(def -main next-runner/-main)

+ 1 - 1
src/main/frontend/components/header.cljs

@@ -354,7 +354,7 @@
                              (mobile-util/native-platform?)
                              (mobile-util/native-platform?)
                              (util/scroll-to-top true))))
                              (util/scroll-to-top true))))
       :style           {:fontSize 50}}
       :style           {:fontSize 50}}
-     [:div.l.flex.drag-region
+     [:div.l.flex.items-center.drag-region
       [left-menu
       [left-menu
        (if (mobile-util/native-platform?)
        (if (mobile-util/native-platform?)
          ;; back button for mobile
          ;; back button for mobile

+ 3 - 2
src/main/frontend/ui.cljs

@@ -908,8 +908,9 @@
 (rum/defc with-shortcut < rum/reactive
 (rum/defc with-shortcut < rum/reactive
   < {:key-fn (fn [key pos] (str "shortcut-" key pos))}
   < {:key-fn (fn [key pos] (str "shortcut-" key pos))}
   [shortcut-key position content]
   [shortcut-key position content]
-  (let [tooltip? (state/sub :ui/shortcut-tooltip?)]
-    (if tooltip?
+  (let [shortcut-tooltip? (state/sub :ui/shortcut-tooltip?)
+        enabled-tooltip? (state/enable-tooltip?)]
+    (if (and enabled-tooltip? shortcut-tooltip?)
       (tippy
       (tippy
        {:html [:div.text-sm.font-medium (keyboard-shortcut-from-config shortcut-key)]
        {:html [:div.text-sm.font-medium (keyboard-shortcut-from-config shortcut-key)]
         :interactive true
         :interactive true