|
@@ -1,7 +1,6 @@
|
|
|
(ns frontend.handler.db-based.import
|
|
|
"Handles DB graph imports"
|
|
|
- (:require [cljs.pprint :as pprint]
|
|
|
- [clojure.edn :as edn]
|
|
|
+ (:require [clojure.edn :as edn]
|
|
|
[datascript.core :as d]
|
|
|
[frontend.config :as config]
|
|
|
[frontend.db :as db]
|
|
@@ -15,7 +14,9 @@
|
|
|
[logseq.db.sqlite.export :as sqlite-export]
|
|
|
[logseq.db.sqlite.util :as sqlite-util]
|
|
|
[logseq.shui.ui :as shui]
|
|
|
- [promesa.core :as p]))
|
|
|
+ [promesa.core :as p]
|
|
|
+ [frontend.modules.outliner.ui :as ui-outliner-tx]
|
|
|
+ [frontend.modules.outliner.op :as outliner-op]))
|
|
|
|
|
|
(defn import-from-sqlite-db!
|
|
|
[buffer bare-graph-name finished-ok-handler]
|
|
@@ -50,29 +51,6 @@
|
|
|
(state/set-current-repo! graph)
|
|
|
(finished-ok-handler))))
|
|
|
|
|
|
-(defn- safe-build-edn-import [export-map import-options]
|
|
|
- (try
|
|
|
- (sqlite-export/build-import export-map (db/get-db) import-options)
|
|
|
- (catch :default e
|
|
|
- (js/console.error "Import EDN error: " e)
|
|
|
- {:error "An unexpected error occurred building the import. See the javascript console for details."})))
|
|
|
-
|
|
|
-(defn- import-edn-data-from-file
|
|
|
- [export-map]
|
|
|
- (let [{:keys [init-tx block-props-tx misc-tx error] :as _txs} (safe-build-edn-import export-map {})]
|
|
|
- ;; (cljs.pprint/pprint _txs)
|
|
|
- (if error
|
|
|
- (notification/show! error :error)
|
|
|
- (let [tx-meta {::sqlite-export/imported-data? true
|
|
|
- :import-db? true}
|
|
|
- repo (state/get-current-repo)]
|
|
|
- (p/do
|
|
|
- (db/transact! repo init-tx tx-meta)
|
|
|
- (when (seq block-props-tx)
|
|
|
- (db/transact! repo block-props-tx tx-meta))
|
|
|
- (when (seq misc-tx)
|
|
|
- (db/transact! repo misc-tx tx-meta)))))))
|
|
|
-
|
|
|
(defn import-from-edn-file!
|
|
|
"Creates a new DB graph and imports sqlite.build EDN file"
|
|
|
[bare-graph-name file-body finished-ok-handler]
|
|
@@ -90,13 +68,19 @@
|
|
|
(finished-error-handler)
|
|
|
nil))]
|
|
|
(when (some? edn-data)
|
|
|
- (-> (p/do!
|
|
|
- (persist-db/<new graph {:import-type :edn})
|
|
|
- (state/add-repo! {:url graph})
|
|
|
- (repo-handler/restore-and-setup-repo! graph {:import-type :edn})
|
|
|
- (state/set-current-repo! graph)
|
|
|
- (import-edn-data-from-file edn-data)
|
|
|
- (finished-ok-handler))
|
|
|
+ (-> (p/let
|
|
|
+ [_ (persist-db/<new graph {:import-type :edn})
|
|
|
+ _ (state/add-repo! {:url graph})
|
|
|
+ _ (repo-handler/restore-and-setup-repo! graph {:import-type :edn})
|
|
|
+ _ (state/set-current-repo! graph)
|
|
|
+ {:keys [error]} (ui-outliner-tx/transact!
|
|
|
+ {:outliner-op :batch-import-edn}
|
|
|
+ (outliner-op/batch-import-edn! edn-data {:tx-meta {:import-db? true}}))]
|
|
|
+ (if error
|
|
|
+ (do
|
|
|
+ (notification/show! error :error)
|
|
|
+ (finished-error-handler))
|
|
|
+ (finished-ok-handler)))
|
|
|
(p/catch
|
|
|
(fn [e]
|
|
|
(js/console.error e)
|
|
@@ -109,32 +93,29 @@
|
|
|
import-block? (::sqlite-export/block export-map)
|
|
|
block (when import-block?
|
|
|
(if-let [eid (:block-id (first (state/get-editor-args)))]
|
|
|
- (db/entity [:block/uuid eid])
|
|
|
+ (let [ent (db/entity [:block/uuid eid])]
|
|
|
+ (if-not (:block/page ent)
|
|
|
+ {:error "Can't import block into a non-block entity. Please import block elsewhere."}
|
|
|
+ (merge (select-keys ent [:block/uuid])
|
|
|
+ {:block/page (select-keys (:block/page ent) [:block/uuid])})))
|
|
|
(notification/show! "No block found" :warning)))]
|
|
|
- (if (= ::invalid-import export-map)
|
|
|
- (notification/show! "The submitted EDN data is invalid! Please fix and try again." :warning)
|
|
|
- (let [{:keys [init-tx block-props-tx misc-tx error] :as txs}
|
|
|
- (safe-build-edn-import export-map (when block {:current-block block}))]
|
|
|
- (pprint/pprint txs)
|
|
|
- (if error
|
|
|
- (notification/show! error :error)
|
|
|
- ;; TODO: When not import-block, use metadata that supports undo
|
|
|
- (let [tx-meta (if import-block? {:outliner-op :save-block} {::sqlite-export/imported-data? true})
|
|
|
- repo (state/get-current-repo)]
|
|
|
- (-> (p/do
|
|
|
- (db/transact! repo init-tx tx-meta)
|
|
|
- (when (seq block-props-tx)
|
|
|
- (db/transact! repo block-props-tx tx-meta))
|
|
|
- (when (seq misc-tx)
|
|
|
- (db/transact! repo misc-tx tx-meta))
|
|
|
- (when-not import-block?
|
|
|
- (ui-handler/re-render-root!)
|
|
|
- (notification/show! "Import successful!" :success)))
|
|
|
- (p/catch (fn [e]
|
|
|
- (js/console.error "Import EDN error: " e)
|
|
|
- (notification/show! "An unexpected error occurred during import. See the javascript console for details." :error))))))
|
|
|
- ;; Also close cmd-k
|
|
|
- (shui/dialog-close-all!)))))
|
|
|
+ (cond (or (= ::invalid-import export-map) (not (map? export-map)))
|
|
|
+ (notification/show! "The submitted EDN data is invalid! Please fix and try again." :warning)
|
|
|
+ (:error block)
|
|
|
+ (do
|
|
|
+ (notification/show! (:error block) :error)
|
|
|
+ (shui/dialog-close-all!))
|
|
|
+ :else
|
|
|
+ (p/let [{:keys [error]}
|
|
|
+ (ui-outliner-tx/transact!
|
|
|
+ {:outliner-op :batch-import-edn}
|
|
|
+ (outliner-op/batch-import-edn! export-map (when block {:current-block block})))]
|
|
|
+ ;; Also close cmd-k
|
|
|
+ (shui/dialog-close-all!)
|
|
|
+ (ui-handler/re-render-root!)
|
|
|
+ (if error
|
|
|
+ (notification/show! error :error)
|
|
|
+ (notification/show! "Import successful!" :success))))))
|
|
|
|
|
|
(defn ^:export import-edn-data-dialog
|
|
|
"Displays dialog which allows users to paste and import sqlite.build EDN Data"
|