Skip to content

Commit

Permalink
fix: stuck importing from edn/json
Browse files Browse the repository at this point in the history
Also, this commit added the progress UI for importing and removed the
:file/writes core.async channel.
  • Loading branch information
tiensonqin committed Aug 26, 2022
1 parent 6807931 commit f4aa08f
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 275 deletions.
13 changes: 6 additions & 7 deletions src/main/frontend/components/export.cljs
Expand Up @@ -12,8 +12,13 @@
(when-let [current-repo (state/get-current-repo)]
[:div.export
[:h1.title "Export"]

[:ul.mr-1
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-edn-v2! current-repo)}
(t :export-edn)]]
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-json-v2! current-repo)}
(t :export-json)]]
(when (util/electron?)
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-html! current-repo)}
Expand All @@ -25,12 +30,6 @@
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-opml! current-repo)}
(t :export-opml)]])
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-edn-v2! current-repo)}
(t :export-edn)]]
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-json-v2! current-repo)}
(t :export-json)]]
(when-not (mobile-util/native-platform?)
[:li.mb-4
[:a.font-medium {:on-click #(export/export-repo-as-roam-json! current-repo)}
Expand Down
80 changes: 0 additions & 80 deletions src/main/frontend/components/external.cljs

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/frontend/components/onboarding.cljs
Expand Up @@ -13,8 +13,8 @@
[]
[:div.help.cp__sidebar-help-docs
(let [discourse-with-icon [:div.flex-row.inline-flex.items-center
[:span.mr-1 (t :help/forum-community)]
(ui/icon "message-circle" {:style {:font-size 20}})]
[:span.mr-1 (t :help/forum-community)]
(ui/icon "message-circle" {:style {:font-size 20}})]
list
[{:title "Usage"
:children [[[:a
Expand All @@ -25,7 +25,7 @@
[(t :help/docs) "https://docs.logseq.com/"]
[(t :help/start) "https://docs.logseq.com/#/page/tutorial"]
["FAQ" "https://docs.logseq.com/#/page/faq"]]}

{:title "Community"
:children [[(t :help/awesome-logseq) "https://github.com/logseq/awesome-logseq"]
[(t :help/blog) "https://blog.logseq.com"]
Expand All @@ -36,15 +36,15 @@
[(t :help/bug) "https://github.com/logseq/logseq/issues/new?labels=from:in-app&template=bug_report.yaml"]
[(t :help/feature) "https://discuss.logseq.com/c/feature-requests/"]
[(t :help/changelog) "https://docs.logseq.com/#/page/changelog"]]}

{:title "About"
:children [[(t :help/about) "https://logseq.com/blog/about"]]}

{:title "Terms"
:children [[(t :help/privacy) "https://logseq.com/blog/privacy-policy"]
[(t :help/terms) "https://logseq.com/blog/terms"]]}]]



(map (fn [sublist]
[[:p.mt-4.mb-1 [:b (:title sublist)]]
Expand Down
1 change: 0 additions & 1 deletion src/main/frontend/components/onboarding/index.css
Expand Up @@ -314,7 +314,6 @@ body[data-page=import] {

small {
font-size: 11px;
text-align: center;
}

&:hover {
Expand Down
106 changes: 51 additions & 55 deletions src/main/frontend/components/onboarding/setups.cljs
Expand Up @@ -2,9 +2,11 @@
(:require [frontend.state :as state]
[rum.core :as rum]
[frontend.ui :as ui]
[frontend.context.i18n :refer [t]]
[frontend.components.svg :as svg]
[frontend.handler.page :as page-handler]
[frontend.handler.route :as route-handler]
[frontend.handler.ui :as ui-handler]
[frontend.util :as util]
[frontend.handler.web.nfs :as nfs]
[frontend.mobile.util :as mobile-util]
Expand Down Expand Up @@ -126,29 +128,30 @@
[:strong.uppercase title]
[:small.opacity-50 label]]]))]]])))

(defonce *roam-importing? (atom nil))
(defonce *lsq-importing? (atom nil))
(defonce *opml-importing? (atom nil))
(defonce *opml-imported-pages (atom nil))

(defn- finished-cb
[]
(route-handler/redirect-to-home!)
(notification/show! "Import finished!" :success)
(route-handler/redirect-to-home!))
(ui-handler/re-render-root!))

(defn- roam-import-handler
[e]
(let [file (first (array-seq (.-files (.-target e))))
file-name (gobj/get file "name")]
(if (string/ends-with? file-name ".json")
(do
(reset! *roam-importing? true)
(state/set-state! :graph/importing :roam-json)
(let [reader (js/FileReader.)]
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(external-handler/import-from-roam-json! text
#(do (reset! *roam-importing? false) (finished-cb))))))
(external-handler/import-from-roam-json!
text
#(do
(state/set-state! :graph/importing nil)
(finished-cb))))))
(.readAsText reader file)))
(notification/show! "Please choose a JSON file."
:error))))
Expand All @@ -157,60 +160,62 @@
[e]
(let [file (first (array-seq (.-files (.-target e))))
file-name (some-> (gobj/get file "name")
(string/lower-case))]
(cond (string/ends-with? file-name ".edn")
(do
(reset! *lsq-importing? true)
(let [reader (js/FileReader.)]
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(external-handler/import-from-edn! text
#(do (reset! *lsq-importing? false) (finished-cb))))))
(.readAsText reader file)))

(string/ends-with? file-name ".json")
(do
(reset! *lsq-importing? true)
(let [reader (js/FileReader.)]
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(external-handler/import-from-json! text
#(do (reset! *lsq-importing? false) (finished-cb))))))
(.readAsText reader file)))

:else
(notification/show! "Please choose an EDN or a JSON file."
:error))))
(string/lower-case))
edn? (string/ends-with? file-name ".edn")
json? (string/ends-with? file-name ".json")]
(if (or edn? json?)
(do
(state/set-state! :graph/importing :logseq)
(let [reader (js/FileReader.)
import-f (if edn?
external-handler/import-from-edn!
external-handler/import-from-json!)]
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(import-f
text
#(do
(state/set-state! :graph/importing nil)
(finished-cb))))))
(.readAsText reader file)))
(notification/show! "Please choose an EDN or a JSON file."
:error))))

(defn- opml-import-handler
[e]
(let [file (first (array-seq (.-files (.-target e))))
file-name (gobj/get file "name")]
(if (string/ends-with? file-name ".opml")
(do
(reset! *opml-importing? true)
(state/set-state! :graph/importing :opml)
(let [reader (js/FileReader.)]
(set! (.-onload reader)
(fn [e]
(let [text (.. e -target -result)]
(external-handler/import-from-opml! text
(fn [pages]
(reset! *opml-imported-pages pages)
(reset! *opml-importing? false)
(state/set-state! :graph/importing nil)
(finished-cb))))))
(.readAsText reader file)))
(notification/show! "Please choose a OPML file."
:error))))

(rum/defc importer < rum/reactive
[{:keys [query-params]}]
(let [roam-importing? (rum/react *roam-importing?)
lsq-importing? (rum/react *lsq-importing?)
opml-importing? (rum/react *opml-importing?)
importing? (or roam-importing? lsq-importing? opml-importing?)]

(if (state/sub :graph/importing)
(let [{:keys [total current-idx current-page]} (state/sub :graph/importing-state)
left-label [:div.flex.flex-row.font-bold
(t :importing)
[:div.hidden.md:flex.flex-row
[:span.mr-1 ": "]
[:div.text-ellipsis-wrapper {:style {:max-width 300}}
current-page]]]
width (js/Math.round (* (.toFixed (/ current-idx total) 2) 100))
process (when (and total current-idx)
(str current-idx "/" total))]
(ui/progress-bar-with-label width left-label process))
(setups-container
:importer
[:article.flex.flex-col.items-center.importer.py-16.px-8
Expand All @@ -219,39 +224,30 @@
[:h2 "If they are in a JSON, EDN or Markdown format Logseq can work with them."]]
[:section.d.md:flex
[:label.action-input.flex.items-center.mx-2.my-2
{:disabled importing?}
[:span.as-flex-center [:i (svg/roam-research 28)]]
[:div.flex.flex-col
(if roam-importing?
(ui/loading "Importing ...")
[[:strong "RoamResearch"]
[:small "Import a JSON Export of your Roam graph"]])]
[[:strong "RoamResearch"]
[:small "Import a JSON Export of your Roam graph"]]]
[:input.absolute.hidden
{:id "import-roam"
:type "file"
:on-change roam-import-handler}]]

[:label.action-input.flex.items-center.mx-2.my-2
{:disabled importing?}
[:span.as-flex-center [:i (svg/logo 28)]]
[:span.flex.flex-col
(if lsq-importing?
(ui/loading "Importing ...")
[[:strong "EDN / JSON"]
[:small "Import an EDN or a JSON Export of your Logseq graph"]])]
[[:strong "EDN / JSON"]
[:small "Import an EDN or a JSON Export of your Logseq graph"]]]
[:input.absolute.hidden
{:id "import-lsq"
:type "file"
:on-change lsq-import-handler}]]

[:label.action-input.flex.items-center.mx-2.my-2
{:disabled importing?}
[:span.as-flex-center (ui/icon "sitemap" {:style {:fontSize "26px"}})]
[:span.flex.flex-col
(if opml-importing?
(ui/loading "Importing ...")
[[:strong "OPML"]
[:small " Import OPML files"]])]
[[:strong "OPML"]
[:small " Import OPML files"]]]

[:input.absolute.hidden
{:id "import-opml"
Expand Down
1 change: 1 addition & 0 deletions src/main/frontend/dicts.cljc
Expand Up @@ -252,6 +252,7 @@
:developer-mode-alert "You need to restart the app to enable the plugin system. Do you want to restart it now?"
:relaunch-confirm-to-work "Should relaunch app to make it work. Do you want to restart it now?"
:import "Import"
:importing "Importing"
:join-community "Join the community"
:sponsor-us "Sponsor Us"
:discourse-title "Our forum!"
Expand Down
6 changes: 2 additions & 4 deletions src/main/frontend/fs/capacitor_fs.cljs
Expand Up @@ -185,15 +185,13 @@
repo-dir (config/get-local-dir repo)
ext (util/get-file-ext path)
db-content (or old-content (db/get-file repo path) "")
contents-matched? (contents-matched? disk-content db-content)
pending-writes (state/get-write-chan-length)]
contents-matched? (contents-matched? disk-content db-content)]
(cond
(and
(not= stat :not-found) ; file on the disk was deleted
(not contents-matched?)
(not (contains? #{"excalidraw" "edn" "css"} ext))
(not (string/includes? path "/.recycle/"))
(zero? pending-writes))
(not (string/includes? path "/.recycle/")))
(p/let [disk-content (encrypt/decrypt disk-content)]
(state/pub-event! [:file/not-matched-from-disk path disk-content content]))

Expand Down
4 changes: 1 addition & 3 deletions src/main/frontend/fs/nfs.cljs
Expand Up @@ -163,7 +163,6 @@
(if file-handle
(-> (p/let [local-file (.getFile file-handle)
local-content (.text local-file)
pending-writes (state/get-write-chan-length)
ext (string/lower-case (util/get-file-ext path))
db-content (db/get-file repo path)
contents-matched? (contents-matched? local-content (or db-content ""))]
Expand All @@ -173,8 +172,7 @@
(not (:skip-compare? opts))
(not contents-matched?)
(not (contains? #{"excalidraw" "edn" "css"} ext))
(not (string/includes? path "/.recycle/"))
(zero? pending-writes))
(not (string/includes? path "/.recycle/")))
(p/let [local-content (encrypt/decrypt local-content)]
(state/pub-event! [:file/not-matched-from-disk path local-content content]))
(p/let [_ (verify-permission repo file-handle true)
Expand Down

0 comments on commit f4aa08f

Please sign in to comment.