Przeglądaj źródła

fix: favorites not importing

Also made underlying fns reusable from UI and nbb compatible.
Fixes LOG-3031
Gabriel Horner 1 rok temu
rodzic
commit
2f599919fb

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

@@ -569,6 +569,26 @@
 (def write-transit-str sqlite-util/write-transit-str)
 (def read-transit-str sqlite-util/read-transit-str)
 
+(defn create-favorites-page
+  "Creates hidden favorites page for storing favorites"
+  [repo]
+  (transact!
+   repo
+   [(sqlite-util/block-with-timestamps
+     {:block/uuid (d/squuid)
+      :block/name common-config/favorites-page-name
+      :block/original-name common-config/favorites-page-name
+      :block/journal? false
+      :block/type #{"hidden"}
+      :block/format :markdown})]))
+
+(defn build-favorite-tx
+  "Builds tx for a favorite block in favorite page"
+  [favorite-uuid]
+  {:block/link [:block/uuid favorite-uuid]
+   :block/content ""
+   :block/format :markdown})
+
 (comment
   (defn db-based-graph?
     "Whether the current graph is db-only"

+ 1 - 0
deps/graph-parser/script/db_import.cljs

@@ -69,6 +69,7 @@
      (gp-exporter/import-logseq-files conn logseq-files <read-file {:notify-user prn})
      (gp-exporter/import-from-asset-files! asset-files #(<copy-asset-file % db-graph-dir file-graph-dir) {:notify-user prn})
      (gp-exporter/import-from-doc-files! conn doc-files <read-file import-options)
+     (gp-exporter/import-favorites-from-config-edn! conn conn config {})
      (gp-exporter/import-class-properties conn conn))))
 
 (defn- resolve-path

+ 30 - 0
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -776,3 +776,33 @@
           (p/catch (fn [e]
                      (notify-user {:msg (str "Import has an unexpected error:\n" e)
                                    :level :error})))))))
+
+(defn- insert-favorites
+  "Inserts favorited pages as uuids into a new favorite page"
+  [repo-or-conn favorited-ids page-id]
+  (let [tx (reduce (fn [acc favorite-id]
+                     (conj acc
+                           (sqlite-util/block-with-timestamps
+                            (merge (ldb/build-favorite-tx favorite-id)
+                                   {:block/uuid (d/squuid)
+                                    :db/id (or (some-> (:db/id (last acc)) dec) -1)
+                                    :block/left {:db/id (or (:db/id (last acc)) page-id)}
+                                    :block/parent page-id
+                                    :block/page page-id}))))
+                   []
+                   favorited-ids)]
+    (ldb/transact! repo-or-conn tx)))
+
+(defn import-favorites-from-config-edn!
+  [conn repo config {:keys [log-fn] :or {log-fn prn}}]
+  (when-let [favorites (seq (:favorites config))]
+    (p/do!
+     (ldb/create-favorites-page repo)
+     (if-let [favorited-ids
+              (keep (fn [page-name]
+                      (some-> (d/entity @conn [:block/name (common-util/page-name-sanity-lc page-name)])
+                              :block/uuid))
+                    favorites)]
+       (let [page-entity (d/entity @conn [:block/name common-config/favorites-page-name])]
+         (insert-favorites repo favorited-ids (:db/id page-entity)))
+       (log-fn :no-favorites-found {:favorites favorites})))))

+ 1 - 48
src/main/frontend/components/imports.cljs

@@ -2,11 +2,9 @@
   "Import data into Logseq."
   (:require [cljs.core.async.interop :refer [p->c]]
             [clojure.core.async :as async]
-            [clojure.edn :as edn]
             [clojure.string :as string]
             [cljs-time.core :as t]
             [cljs.pprint :as pprint]
-            [datascript.core :as d]
             [frontend.components.onboarding.setups :as setups]
             [frontend.components.repo :as repo]
             [frontend.components.svg :as svg]
@@ -20,7 +18,6 @@
             [frontend.handler.repo :as repo-handler]
             [frontend.handler.route :as route-handler]
             [frontend.handler.ui :as ui-handler]
-            [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.persist-db.browser :as db-browser]
             [frontend.state :as state]
             [frontend.ui :as ui]
@@ -29,10 +26,7 @@
             [goog.functions :refer [debounce]]
             [goog.object :as gobj]
             [logseq.common.path :as path]
-            [logseq.common.util :as common-util]
-            [logseq.db :as ldb]
             [logseq.graph-parser.exporter :as gp-exporter]
-            [logseq.outliner.core :as outliner-core]
             [promesa.core :as p]
             [rum.core :as rum]
             [logseq.common.config :as common-config]
@@ -168,47 +162,6 @@
       (ui/button "Submit"
                  {:on-click on-submit})]]))
 
-(defn- build-hidden-favorites-page-blocks
-  [page-block-uuid-coll]
-  (map
-   (fn [uuid]
-     {:block/link [:block/uuid uuid]
-      :block/content ""
-      :block/format :markdown})
-   page-block-uuid-coll))
-
-(def hidden-favorites-page-name common-config/favorites-page-name)
-(def hidden-favorites-page-tx
-  {:block/uuid (d/squuid)
-   :block/name hidden-favorites-page-name
-   :block/original-name hidden-favorites-page-name
-   :block/journal? false
-   :block/type #{"hidden"}
-   :block/format :markdown})
-
-(defn- import-favorites-from-config-edn!
-  [db-conn repo config-file]
-  (let [now (inst-ms (js/Date.))]
-    (p/do!
-     (ldb/transact! repo [(assoc hidden-favorites-page-tx
-                                 :block/created-at now
-                                 :block/updated-at now)])
-     (p/let [content (when config-file (.text (:file-object config-file)))]
-       (when-let [content-edn (try (edn/read-string content)
-                                   (catch :default _ nil))]
-         (when-let [favorites (seq (:favorites content-edn))]
-           (when-let [page-block-uuid-coll
-                      (seq
-                       (keep (fn [page-name]
-                               (some-> (d/entity @db-conn [:block/name (common-util/page-name-sanity-lc page-name)])
-                                       :block/uuid))
-                             favorites))]
-             (let [page-entity (d/entity @db-conn [:block/name hidden-favorites-page-name])]
-               (ui-outliner-tx/transact!
-                {:outliner-op :insert-blocks}
-                (outliner-core/insert-blocks! repo db-conn (build-hidden-favorites-page-blocks page-block-uuid-coll)
-                                              page-entity {}))))))))))
-
 (rum/defc import-file-graph-dialog
   [initial-name on-graph-name-confirmed]
   (let [[graph-input set-graph-input!] (rum/use-state initial-name)
@@ -376,7 +329,7 @@
       (state/set-state! [:graph/importing-state :current-page] "Asset files")
       (async/<! (p->c (gp-exporter/import-from-asset-files! asset-files <copy-asset {:notify-user show-notification})))
       (async/<! (p->c (gp-exporter/import-from-doc-files! db-conn doc-files <read-file import-options)))
-      (async/<! (p->c (import-favorites-from-config-edn! db-conn repo config-file)))
+      (async/<! (p->c (gp-exporter/import-favorites-from-config-edn! db-conn repo config {})))
       (async/<! (p->c (gp-exporter/import-class-properties db-conn repo)))
       (log/info :import-file-graph {:msg (str "Import finished in " (/ (t/in-millis (t/interval start-time (t/now))) 1000) " seconds")})
       (state/set-state! :graph/importing nil)

+ 3 - 14
src/main/frontend/handler/common/page.cljs

@@ -18,20 +18,12 @@
             [promesa.core :as p]
             [frontend.handler.block :as block-handler]
             [frontend.handler.file-based.recent :as file-recent-handler]
-            [frontend.format.block :as block]
             [logseq.db :as ldb]
             [frontend.db.conn :as conn]
             [datascript.core :as d]
             [frontend.modules.outliner.ui :as ui-outliner-tx]
             [frontend.modules.outliner.op :as outliner-op]))
 
-(defn build-hidden-page-tx-data
-  [page-name]
-  (let [page-name* (str "$$$" page-name)]
-    (assoc (block/page-name->map page-name* true true)
-           :block/type #{"hidden"}
-           :block/format :markdown)))
-
 ;; TODO: return page entity instead
 (defn create!
   "Create page. Has the following options:
@@ -126,16 +118,13 @@
 (defn <favorite-page!-v2
   [page-block-uuid]
   {:pre [(uuid? page-block-uuid)]}
-  (let [favorites-page (d/entity (conn/get-db) [:block/name common-config/favorites-page-name])
-        favorites-page-tx-data (build-hidden-page-tx-data "favorites")]
+  (let [favorites-page (d/entity (conn/get-db) [:block/name common-config/favorites-page-name])]
     (when (d/entity (conn/get-db) [:block/uuid page-block-uuid])
       (p/do!
-       (when-not favorites-page (ldb/transact! nil [favorites-page-tx-data]))
+       (when-not favorites-page (ldb/create-favorites-page (state/get-current-repo)))
        (ui-outliner-tx/transact!
         {:outliner-op :insert-blocks}
-        (outliner-op/insert-blocks! [{:block/link [:block/uuid page-block-uuid]
-                                      :block/content ""
-                                      :block/format :markdown}]
+        (outliner-op/insert-blocks! [(ldb/build-favorite-tx page-block-uuid)]
                                     (d/entity (conn/get-db) [:block/name common-config/favorites-page-name])
                                     {}))))))