Переглянути джерело

fix: more async queries and export to roam

Tienson Qin 1 рік тому
батько
коміт
99b6d543f0

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser.cljs

@@ -15,7 +15,7 @@
   (mapcat (fn [{uuid :block/uuid eid :db/id}]
             (if (and uuid (contains? retain-uuids uuid))
               (map (fn [attr] [:db.fn/retractAttribute eid attr]) db-schema/retract-attributes)
-              [[:db.fn/retractEntity eid]]))
+              (when eid [[:db.fn/retractEntity eid]])))
           blocks))
 
 (defn- get-file-page

+ 8 - 6
deps/outliner/src/logseq/outliner/core.cljs

@@ -153,7 +153,7 @@
                                   ;; Only delete if last reference
                                   (keep #(when (<= (count (:block/_macros (d/entity db (:db/id %))))
                                                    1)
-                                           (vector :db.fn/retractEntity (:db/id %)))
+                                           (when (:db/id %) (vector :db.fn/retractEntity (:db/id %))))
                                         (:block/macros block-entity)))))))
 
 (comment
@@ -413,11 +413,13 @@
     (assert (ds/outliner-txs-state? txs-state)
             "db should be satisfied outliner-tx-state?")
     (let [block-id (otree/-get-id this conn)
-          ids (set (if children?
-                     (let [children (ldb/get-block-children @conn block-id)
-                           children-ids (map :block/uuid children)]
-                       (conj children-ids block-id))
-                     [block-id]))
+          ids (->>
+               (if children?
+                 (let [children (ldb/get-block-children @conn block-id)
+                       children-ids (map :block/uuid children)]
+                   (conj children-ids block-id))
+                 [block-id])
+               (remove nil?))
           txs (map (fn [id] [:db.fn/retractEntity [:block/uuid id]]) ids)
           txs (if-not children?
                 (let [immediate-children (ldb/get-block-immediate-children @conn block-id)]

+ 1 - 1
deps/outliner/src/logseq/outliner/datascript.cljs

@@ -65,7 +65,7 @@
                               ;; Only delete if last reference
                               (keep #(when (<= (count (:block/_macros (d/entity db (:db/id %))))
                                                1)
-                                       (vector :db.fn/retractEntity (:db/id %)))
+                                       (when (:db/id %) (vector :db.fn/retractEntity (:db/id %))))
                                     (:block/macros b)))
                             retracted-blocks)]
       (when (and (seq retracted-tx') (fn? set-state-fn))

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

@@ -32,7 +32,7 @@
   delete-blocks get-pre-block
   delete-files delete-pages-by-files get-all-tagged-pages
   get-block-and-children get-block-by-uuid get-block-children sort-by-left
-  get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks get-all-referenced-blocks-uuid
+  get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks
   get-block-immediate-children get-block-page
   get-custom-css get-date-scheduled-or-deadlines
   get-file-last-modified-at get-file get-file-page get-file-page-id file-exists?

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

@@ -147,3 +147,12 @@
   (assert (integer? eid))
   (when-let [^Object worker @db-browser/*worker]
     (.get-block-refs-count worker graph eid)))
+
+(defn <get-all-referenced-blocks-uuid
+  "Get all uuids of blocks with any back link exists."
+  [graph]
+  (<q '[:find [?refed-uuid ...]
+        :where
+           ;; ?referee-b is block with ref towards ?refed-b
+        [?refed-b   :block/uuid ?refed-uuid]
+        [?referee-b :block/refs ?refed-b]]))

+ 3 - 3
src/main/frontend/db/model.cljs

@@ -906,7 +906,6 @@ independent of format as format specific heading characters are stripped"
          (sort-by :block/name)
          (first))))
 
-;; TODO: async query
 (defn get-all-referenced-blocks-uuid
   "Get all uuids of blocks with any back link exists."
   []
@@ -920,7 +919,8 @@ independent of format as format specific heading characters are stripped"
 (defn delete-blocks
   [repo-url files _delete-page?]
   (when (seq files)
-    (let [blocks (get-files-blocks repo-url files)]
+    (let [blocks (->> (get-files-blocks repo-url files)
+                      (remove nil?))]
       (mapv (fn [eid] [:db.fn/retractEntity eid]) blocks))))
 
 (defn delete-files
@@ -943,7 +943,7 @@ independent of format as format specific heading characters are stripped"
                     :file/content content}]
        (db-utils/transact! repo [tx-data] (merge opts {:skip-refresh? true}))))))
 
-;; TODO: async query
+;; TODO: check whether this works when adding pdf back on Web
 (defn get-pre-block
   [repo page-id]
   (-> (d/q '[:find (pull ?b [*])

+ 16 - 16
src/main/frontend/external/roam_export.cljs

@@ -2,9 +2,9 @@
   (:require [clojure.set :as s]
             [clojure.string :as str]
             [clojure.walk :as walk]
-            [datascript.core :as d]
-            [frontend.db :as db]
-            [frontend.state :as state]))
+            [frontend.db.async :as db-async]
+            [frontend.state :as state]
+            [promesa.core :as p]))
 
 (def todo-marker-regex
   #"^(NOW|LATER|TODO|DOING|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS)")
@@ -20,18 +20,18 @@
   (->> (repeatedly 9 nano-id-char)
        (str/join)))
 
-;; TODO: async
-(defn uuid->uid-map []
-  (let [db (db/get-db (state/get-current-repo))]
-    (->>
-     (d/q '[:find (pull ?r [:block/uuid])
-            :in $
-            :where
-            [?b :block/refs ?r]] db)
-     (map (comp :block/uuid first))
-     (distinct)
-     (map (fn [uuid] [uuid (nano-id)]))
-     (into {}))))
+(defn <uuid->uid-map []
+  (let [repo (state/get-current-repo)]
+    (p/let [result (db-async/<q repo
+                                '[:find (pull ?r [:block/uuid])
+                                  :in $
+                                  :where
+                                  [?b :block/refs ?r]])]
+      (->> result
+       (map (comp :block/uuid first))
+       (distinct)
+       (map (fn [uuid] [uuid (nano-id)]))
+       (into {})))))
 
 (defn update-content [content uuid->uid-map]
   (when content                         ; page block doesn't have content
@@ -66,7 +66,7 @@
 
 (defn traverse
   [keyseq vec-tree]
-  (let [uuid->uid-map (uuid->uid-map)]
+  (p/let [uuid->uid-map (<uuid->uid-map)]
     (walk/postwalk
      (fn [x]
        (cond

+ 6 - 4
src/main/frontend/handler/import.cljs

@@ -21,9 +21,11 @@
             [frontend.handler.notification :as notification]
             [frontend.util :as util]
             [clojure.core.async :as async]
+            [cljs.core.async.interop :refer [p->c]]
             [medley.core :as medley]
             [frontend.persist-db :as persist-db]
-            [promesa.core :as p]))
+            [promesa.core :as p]
+            [frontend.db.async :as db-async]))
 
 (defn index-files!
   "Create file structure, then parse into DB (client only)"
@@ -194,9 +196,9 @@
               (async/<! (async/timeout 10))
               (create-page-with-exported-tree! block)
               (recur))
-            (do
-              (editor/set-blocks-id! (db/get-all-referenced-blocks-uuid))
-              (async/offer! imported-chan true)))))
+            (let [result (async/<! (p->c (db-async/<get-all-referenced-blocks-uuid (state/get-current-repo))))]
+                (editor/set-blocks-id! result)
+                (async/offer! imported-chan true)))))
 
       (catch :default e
         (notification/show! (str "Error happens when importing:\n" e) :error)

+ 0 - 1
src/main/frontend/worker/export.cljs

@@ -31,7 +31,6 @@
                              (gp-property/valid-property-name? (str k))) properties)
                    (into {}))))))
 
-;; TODO: async query
 (defn get-all-pages
   "Get all pages and their children blocks."
   [repo db]