refactor: modularize repos-dropdown

This commit is contained in:
Peng Xiao
2022-03-03 14:01:12 +08:00
committed by Tienson Qin
parent 417950cdea
commit 421a45c447
3 changed files with 105 additions and 95 deletions

View File

@@ -195,105 +195,105 @@
:else
(db/get-repo-path repo)))
(rum/defcs ^:large-vars/cleanup-todo repos-dropdown < rum/reactive
(defn- repos-dropdown-links [repos current-repo *multiple-windows?]
(let [switch-repos (remove (fn [repo] (= current-repo (:url repo))) repos) ; exclude current repo
repo-links (mapv
(fn [{:keys [url]}]
(let [repo-path (get-repo-name url)
short-repo-name (text/get-graph-name-from-path repo-path)]
{:title short-repo-name
:hover-detail repo-path ;; show full path on hover
:options {:class "ml-1"
:on-click (fn [e]
(if (gobj/get e "shiftKey")
(ui-handler/open-new-window! url)
(state/pub-event! [:graph/switch url])))}}))
switch-repos)
refresh-link (let [nfs-repo? (config/local-db? current-repo)]
(when (and nfs-repo?
(not= current-repo config/local-repo)
(or (nfs-handler/supported?)
(mobile-util/is-native-platform?)))
{:title (t :sync-from-local-files)
:hover-detail (t :sync-from-local-files-detail)
:options {:on-click
(fn []
(state/pub-event!
[:modal/show
[:div {:style {:max-width 700}}
[:p (t :sync-from-local-changes-detected)]
(ui/button
(t :yes)
:autoFocus "on"
:large? true
:on-click (fn []
(state/close-modal!)
(nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))}}))
reindex-link {:title (t :re-index)
:hover-detail (t :re-index-detail)
:options (cond->
{:on-click
(fn []
(if @*multiple-windows?
(state/pub-event!
[:modal/show
[:div
[:p (t :re-index-multiple-windows-warning)]]])
(state/pub-event!
[:modal/show
[:div {:style {:max-width 700}}
[:p (t :re-index-discard-unsaved-changes-warning)]
(ui/button
(t :yes)
:autoFocus "on"
:large? true
:on-click (fn []
(state/close-modal!)
(repo-handler/re-index!
nfs-handler/rebuild-index!
page-handler/create-today-journal!)))]])))})}
new-window-link (when (util/electron?)
{:title (t :open-new-window)
:options {:on-click ui-handler/open-new-window!}})]
(->>
(concat repo-links
[(when (seq repos) {:hr true})
{:title (t :new-graph) :options {:href (rfe/href :repo-add)}}
{:title (t :all-graphs) :options {:href (rfe/href :repos)}}
refresh-link
reindex-link
new-window-link])
(remove nil?))))
(rum/defcs repos-dropdown < rum/reactive
(rum/local false ::electron-multiple-windows?)
[state]
(let [multiple-windows? (::electron-multiple-windows? state)]
(when-let [current-repo (state/sub :git/current-repo)]
(let [repos (state/sub [:me :repos])
repos (remove (fn [r] (= config/local-repo (:url r))) repos)
switch-repos (remove (fn [repo]
(= current-repo (:url repo)))
repos)
repo-links (mapv
(fn [{:keys [url]}]
(let [repo-path (get-repo-name url)
short-repo-name (text/get-graph-name-from-path repo-path)]
{:title short-repo-name
:hover-detail repo-path ;; show full path on hover
:options {:class "ml-1"
:on-click (fn [e]
(if (gobj/get e "shiftKey")
(ui-handler/open-new-window! url)
(state/pub-event! [:graph/switch url])))}}))
switch-repos)
links (->>
(concat repo-links
[(when (seq switch-repos)
{:hr true})
{:title (t :new-graph)
:options {:href (rfe/href :repo-add)}}
{:title (t :all-graphs)
:options {:href (rfe/href :repos)}}
(let [nfs-repo? (config/local-db? current-repo)]
(when (and nfs-repo?
(not= current-repo config/local-repo)
(or (nfs-handler/supported?)
(mobile-util/is-native-platform?)))
{:title (t :sync-from-local-files)
:hover-detail (t :sync-from-local-files-detail)
:options {:on-click
(fn []
(state/pub-event!
[:modal/show
[:div {:style {:max-width 700}}
[:p "Refresh detects and processes files modified on your disk and diverged from the actual Logseq page content. Continue?"]
(ui/button
"Yes"
:autoFocus "on"
:large? true
:on-click (fn []
(state/close-modal!)
(nfs-handler/refresh! (state/get-current-repo) refresh-cb)))]]))}}))
{:title (t :re-index)
:hover-detail (t :re-index-detail)
:options (cond->
{:on-click
(fn []
(if @multiple-windows?
(state/pub-event!
[:modal/show
[:div
[:p "You need to close the other windows before re-index this graph."]]])
(state/pub-event!
[:modal/show
[:div {:style {:max-width 700}}
[:p "Re-index will discard the current graph, and then processes all the files again as they are currently stored on disk. You will lose unsaved changes and it might take a while. Continue?"]
(ui/button
"Yes"
:autoFocus "on"
:large? true
:on-click (fn []
(state/close-modal!)
(repo-handler/re-index!
nfs-handler/rebuild-index!
page-handler/create-today-journal!)))]])))})}
(when (util/electron?)
{:title (t :open-new-window)
:options {:on-click ui-handler/open-new-window!}})])
(remove nil?))]
links (repos-dropdown-links repos current-repo multiple-windows?)
render-content (fn [{:keys [toggle-fn]}]
(let [repo-path (get-repo-name current-repo)
short-repo-name (if (or (util/electron?)
(mobile-util/is-native-platform?))
(text/get-file-basename repo-path)
repo-path)]
[:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md
{:on-click (fn []
(check-multiple-windows? state)
(toggle-fn))
:title repo-path} ;; show full path on hover
(ui/icon "database mr-3" {:style {:font-size 20} :id "database-icon"})
[:div.graphs
[:span#repo-switch.block.pr-2.whitespace-nowrap
[:span [:span#repo-name.font-medium short-repo-name]]
[:span.dropdown-caret.ml-2 {:style {:border-top-color "#6b7280"}}]]]]))
links-header (cond->
{:modal-class (util/hiccup->class
"origin-top-right.absolute.left-0.mt-2.rounded-md.shadow-lg")}
(> (count repos) 1) ; show switch to if there are multiple repos
(assoc :links-header [:div.font-medium.text-sm.opacity-60.px-4.pt-2
"Switch to:"]))]
(when (seq repos)
(ui/dropdown-with-links
(fn [{:keys [toggle-fn]}]
(let [repo-path (get-repo-name current-repo)
short-repo-name (if (or (util/electron?)
(mobile-util/is-native-platform?))
(text/get-file-basename repo-path)
repo-path)]
[:a.item.group.flex.items-center.px-2.py-2.text-sm.font-medium.rounded-md
{:on-click (fn []
(check-multiple-windows? state)
(toggle-fn))
:title repo-path} ;; show full path on hover
(ui/icon "database mr-3" {:style {:font-size 20} :id "database-icon"})
[:div.graphs
[:span#repo-switch.block.pr-2.whitespace-nowrap
[:span [:span#repo-name.font-medium short-repo-name]]
[:span.dropdown-caret.ml-2 {:style {:border-top-color "#6b7280"}}]]]]))
links
(cond->
{:modal-class (util/hiccup->class
"origin-top-right.absolute.left-0.mt-2.rounded-md.shadow-lg")}
(seq switch-repos)
(assoc :links-header [:div.font-medium.text-sm.opacity-60.px-4.pt-2
"Switch to:"]))))))))
(ui/dropdown-with-links render-content links links-header))))))

View File

@@ -286,9 +286,13 @@
:port "Port"
:re-index "Re-index"
:re-index-detail "Rebuild the graph"
:re-index-multiple-windows-warning "You need to close the other windows before re-index this graph."
:re-index-discard-unsaved-changes-warning "Re-index will discard the current graph, and then processes all the files again as they are currently stored on disk. You will lose unsaved changes and it might take a while. Continue?"
:open-new-window "New window"
:sync-from-local-files "Refresh"
:sync-from-local-files-detail "Import changes from local files"
:sync-from-local-changes-detected "Refresh detects and processes files modified on your disk and diverged from the actual Logseq page content. Continue?"
:unlink "unlink"
:search (if config/publishing?
"Search"
@@ -1170,8 +1174,13 @@
:cancel "取消"
:new-graph "添加图谱"
:re-index "重新建立索引"
:re-index-detail "重新建立索引"
:re-index-multiple-windows-warning "在重建当前图谱索引前,你需要先关闭其它窗口"
:re-index-discard-unsaved-changes-warning "重建索引将丢弃当前图谱,之后重新导入保存在磁盘上的所有文件。此操作将丢弃未保存的更改,同时可能需要一段时间。是否继续?"
:open-new-window "打开新窗口"
:sync-from-local-files "刷新(读取本地最新文件)"
:sync-from-local-files-detail "读取本地最新文件"
:sync-from-local-changes-detected "执行刷新操作将会导入磁盘上修改过的、或是与实际Logseq页面内容不同的文件。是否继续"
:export-graph "导出图谱"
:export-page "导出当前页面"
:export-json "以 JSON 格式导出"

View File

@@ -302,5 +302,6 @@
(defn open-new-window!
[repo]
; TODO: find out a better way to open a new window with a different repo path
(when repo (storage/set :git/current-repo repo))
(ipc/ipc "openNewWindow"))