refactor: mv import to appropriate ns and reuse import fns

This commit is contained in:
Gabriel Horner
2025-03-18 08:58:03 -04:00
parent 06b3bba5fc
commit 392fe81173
4 changed files with 76 additions and 85 deletions

View File

@@ -1,19 +1,14 @@
(ns frontend.handler.db-based.export
"Handles DB graph exports and imports across graphs"
(:require [cljs.pprint :as pprint]
[clojure.edn :as edn]
[clojure.string :as string]
[frontend.config :as config]
[frontend.db :as db]
[frontend.handler.notification :as notification]
[frontend.handler.ui :as ui-handler]
[frontend.state :as state]
[frontend.util :as util]
[frontend.util.page :as page-util]
[goog.dom :as gdom]
[logseq.db :as ldb]
[logseq.db.sqlite.export :as sqlite-export]
[logseq.shui.ui :as shui]
[promesa.core :as p]))
(defn ^:export export-block-data []
@@ -92,61 +87,4 @@
(when-let [anchor (gdom/getElement "download-as-db-edn")]
(.setAttribute anchor "href" data-str)
(.setAttribute anchor "download" filename)
(.click anchor))))))
(defn- import-submit [import-inputs _e]
(let [export-map (try (edn/read-string (:import-data @import-inputs)) (catch :default _err ::invalid-import))
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])
(notification/show! "No block found" :warning)))]
(if (= ::invalid-import export-map)
(notification/show! "The submitted EDN data is invalid! Fix and try again." :warning)
(let [{:keys [init-tx block-props-tx misc-tx error] :as txs}
(try
(sqlite-export/build-import export-map
(db/get-db)
(when block {:current-block block}))
(catch :default e
(js/console.error "Import EDN error: " e)
{:error "An unexpected error occurred during import. See the javascript console for details."}))]
(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?
(state/clear-async-query-state!)
(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!)))))
(defn ^:export import-edn-data
[]
(let [import-inputs (atom {:import-data "" :import-block? false})]
(shui/dialog-open!
[:div
[:label.flex.my-2.text-lg "Import EDN Data"]
#_[:label.block.flex.items-center.py-3
(shui/checkbox {:on-checked-change #(swap! import-inputs update :import-block? not)})
[:small.pl-2 (str "Import into current block")]]
(shui/textarea {:placeholder "{}"
:class "overflow-y-auto"
:rows 10
:auto-focus true
:on-change (fn [^js e] (swap! import-inputs assoc :import-data (util/evalue e)))})
(shui/button {:class "mt-3"
:on-click (partial import-submit import-inputs)}
"Import")])))
(.click anchor))))))