From e48b14a409f55752ae604b0354e6de66b0582644 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 7 Nov 2022 22:25:19 +0800 Subject: [PATCH] chore: add picker back for both Android and PC --- src/main/frontend/components/file_sync.cljs | 53 +++++++++++++++++++ src/main/frontend/handler/events.cljs | 58 +++++++++++---------- 2 files changed, 84 insertions(+), 27 deletions(-) diff --git a/src/main/frontend/components/file_sync.cljs b/src/main/frontend/components/file_sync.cljs index 685e1b20bc..13e44a7468 100644 --- a/src/main/frontend/components/file_sync.cljs +++ b/src/main/frontend/components/file_sync.cljs @@ -16,6 +16,7 @@ [frontend.handler.notification :as notification] [frontend.handler.repo :as repo-handler] [frontend.handler.user :as user-handler] + [frontend.handler.page :as page-handler] [frontend.handler.web.nfs :as web-nfs] [frontend.mobile.util :as mobile-util] [frontend.state :as state] @@ -514,6 +515,58 @@ synced-file-graph? queuing?) [:div.head-ctls (sync-now)])]}))]))) +(rum/defc pick-local-graph-for-sync [graph] + [:div.cp__file-sync-related-normal-modal + [:div.flex.justify-center.pb-4 [:span.icon-wrap (ui/icon "cloud-download")]] + + [:h1.mb-5.text-2xl.text-center.font-bold (util/format "Sync graph \"%s\" to local" + (:GraphName graph))] + + (ui/button + "Open a local directory" + :class "block w-full py-4 mt-4" + :on-click #(do + (state/close-modal!) + (fs-sync/ + (page-handler/ls-dir-files! + (fn [{:keys [url]}] + (file-sync-handler/init-remote-graph url graph) + (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200)) + + {:empty-dir?-or-pred + (fn [ret] + (let [empty-dir? (nil? (second ret))] + (if-let [root (first ret)] + + ;; verify directory + (-> (if empty-dir? + (p/resolved nil) + (if (util/electron?) + (ipc/ipc :readGraphTxIdInfo root) + (fs-util/read-graphs-txid-info root))) + + (p/then (fn [^js info] + (when (and (not empty-dir?) + (or (nil? info) + (nil? (second info)) + (not= (second info) (:GraphUUID graph)))) + (if (js/confirm "This directory is not empty, are you sure to sync the remote graph to it? Make sure to back up the directory first.") + (p/resolved nil) + (throw (js/Error. nil))))))) + + ;; cancel pick a directory + (throw (js/Error. nil)))))}) + (p/catch (fn []))))) + + [:div.text-xs.opacity-50.px-1.flex-row.flex.items-center.p-2 + (ui/icon "alert-circle") + [:span.ml-1 " An empty directory or an existing remote graph!"]]]) + +(defn pick-dest-to-sync-panel [graph] + (fn [] + (pick-local-graph-for-sync graph))) + (rum/defc page-history-list [graph-uuid page-entity set-list-ready? set-page] diff --git a/src/main/frontend/handler/events.cljs b/src/main/frontend/handler/events.cljs index 8d46f17798..da2f7cdf25 100644 --- a/src/main/frontend/handler/events.cljs +++ b/src/main/frontend/handler/events.cljs @@ -180,34 +180,38 @@ :warning)))) (defmethod handle :graph/pull-down-remote-graph [[_ graph dir-name]] - (when-let [graph-name (or dir-name (:GraphName graph))] - (let [graph-name (util/safe-sanitize-file-name graph-name)] - (if (string/blank? graph-name) - (notification/show! "Illegal graph folder name.") + (if (mobile-util/native-ios?) + (when-let [graph-name (or dir-name (:GraphName graph))] + (let [graph-name (util/safe-sanitize-file-name graph-name)] + (if (string/blank? graph-name) + (notification/show! "Illegal graph folder name.") - ;; Create graph directory under Logseq document folder (local) - (when-let [root (state/get-local-container-root-url)] - (let [graph-path (graph-picker/validate-graph-dirname root graph-name)] - (-> - (p/let [exists? (fs/dir-exists? graph-path)] - (let [overwrite? (if exists? - (js/confirm (str "There's already a directory with the name \"" graph-name "\", do you want to overwrite it? Make sure to backup it first if you're not sure about it.")) - true)] - (if overwrite? - (p/let [_ (fs/mkdir-if-not-exists graph-path)] - (nfs-handler/ls-dir-files-with-path! - graph-path - {:ok-handler (fn [] - (file-sync-handler/init-remote-graph graph-path graph) - (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200))})) - (let [graph-name (-> (js/prompt "Please specify a new directory name to download the graph:") - str - string/trim)] - (when-not (string/blank? graph-name) - (state/pub-event! [:graph/pull-down-remote-graph graph graph-name])))))) - (p/catch (fn [^js e] - (notification/show! (str e) :error) - (js/console.error e)))))))))) + ;; Create graph directory under Logseq document folder (local) + (when-let [root (state/get-local-container-root-url)] + (let [graph-path (graph-picker/validate-graph-dirname root graph-name)] + (-> + (p/let [exists? (fs/dir-exists? graph-path)] + (let [overwrite? (if exists? + (js/confirm (str "There's already a directory with the name \"" graph-name "\", do you want to overwrite it? Make sure to backup it first if you're not sure about it.")) + true)] + (if overwrite? + (p/let [_ (fs/mkdir-if-not-exists graph-path)] + (nfs-handler/ls-dir-files-with-path! + graph-path + {:ok-handler (fn [] + (file-sync-handler/init-remote-graph graph-path graph) + (js/setTimeout (fn [] (repo-handler/refresh-repos!)) 200))})) + (let [graph-name (-> (js/prompt "Please specify a new directory name to download the graph:") + str + string/trim)] + (when-not (string/blank? graph-name) + (state/pub-event! [:graph/pull-down-remote-graph graph graph-name])))))) + (p/catch (fn [^js e] + (notification/show! (str e) :error) + (js/console.error e))))))))) + (state/set-modal! + (file-sync/pick-dest-to-sync-panel graph) + {:center? true}))) (defmethod handle :graph/pick-page-histories [[_ graph-uuid page-name]] (state/set-modal!