Browse Source

fix(electron): try to fix #1228

charlie 4 years ago
parent
commit
3beda2c422

+ 2 - 1
src/main/frontend/components/header.cljs

@@ -15,6 +15,7 @@
             [frontend.components.repo :as repo]
             [frontend.components.search :as search]
             [frontend.handler.project :as project-handler]
+            [frontend.handler.page :as page-handler]
             [frontend.handler.web.nfs :as nfs]
             [goog.dom :as gdom]
             [goog.object :as gobj]))
@@ -188,7 +189,7 @@
                   (not config/publishing?))
          [:a.text-sm.font-medium.opacity-70.hover:opacity-100.ml-3.block
           {:on-click (fn []
-                       (nfs/ls-dir-files))}
+                       (page-handler/ls-dir-files!))}
           [:div.flex.flex-row.text-center
            [:span.inline-block svg/folder-add]
            (when-not config/mobile?

+ 7 - 2
src/main/frontend/components/page.cljs

@@ -1,6 +1,7 @@
 (ns frontend.components.page
   (:require [rum.core :as rum]
             [frontend.util :as util :refer-macros [profile]]
+            [frontend.tools.html-export :as html-export]
             [frontend.handler.file :as file]
             [frontend.handler.page :as page-handler]
             [frontend.handler.ui :as ui-handler]
@@ -306,11 +307,15 @@
                                                                              (page-handler/unpublish-page! page-name))}}
                                                       {:title   (t :page/publish)
                                                        :options {:on-click (fn []
-                                                                             (page-handler/publish-page! page-name project/add-project))}})
+                                                                             (page-handler/publish-page!
+                                                                               page-name project/add-project
+                                                                               html-export/export-page))}})
                                                     (when-not published?
                                                       {:title   (t :page/publish-as-slide)
                                                        :options {:on-click (fn []
-                                                                             (page-handler/publish-page-as-slide! page-name project/add-project))}})
+                                                                             (page-handler/publish-page-as-slide!
+                                                                               page-name project/add-project
+                                                                               html-export/export-page))}})
                                                     {:title   (t (if public? :page/make-private :page/make-public))
                                                      :options {:background (if public? "gray" "indigo")
                                                                :on-click (fn []

+ 2 - 1
src/main/frontend/components/repo.cljs

@@ -9,6 +9,7 @@
             [frontend.handler.route :as route-handler]
             [frontend.handler.export :as export-handler]
             [frontend.handler.web.nfs :as nfs-handler]
+            [frontend.handler.page :as page-handler]
             [frontend.util :as util]
             [frontend.config :as config]
             [reitit.frontend.easy :as rfe]
@@ -44,7 +45,7 @@
              [:div.mr-8
               (ui/button
                (t :open-a-directory)
-               :on-click nfs-handler/ls-dir-files)])
+               :on-click page-handler/ls-dir-files!)])
            (when (and (state/logged?) (not (util/electron?)))
              (ui/button
               "Add another git repo"

+ 2 - 1
src/main/frontend/components/widgets.cljs

@@ -5,6 +5,7 @@
             [frontend.handler.repo :as repo-handler]
             [frontend.handler.notification :as notification]
             [frontend.handler.web.nfs :as nfs]
+            [frontend.handler.page :as page-handler]
             [frontend.state :as state]
             [clojure.string :as string]
             [frontend.ui :as ui]
@@ -87,7 +88,7 @@
        [:div.cp__widgets-open-local-directory
         [:div.select-file-wrap.cursor
          (when nfs-supported?
-           {:on-click nfs/ls-dir-files})
+           {:on-click page-handler/ls-dir-files!})
          [:div
           [:h1.title "Open a local directory"]
           [:p.text-sm

+ 2 - 1
src/main/frontend/config.cljs

@@ -291,7 +291,8 @@
 
 (defn local-db?
   [s]
-  (string/starts-with? s local-db-prefix))
+  (and (string? s)
+       (string/starts-with? s local-db-prefix)))
 
 (defn local-asset?
   [s]

+ 0 - 1
src/main/frontend/handler.cljs

@@ -17,7 +17,6 @@
             [frontend.handler.file :as file-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.ui :as ui-handler]
-            [frontend.handler.export :as export-handler]
             [frontend.handler.web.nfs :as nfs]
             [frontend.fs.watcher-handler :as fs-watcher-handler]
             [frontend.ui :as ui]

+ 8 - 0
src/main/frontend/handler/common.cljs

@@ -13,6 +13,7 @@
             [cljs-time.core :as t]
             [cljs-time.format :as tf]
             [frontend.config :as config]
+            ["ignore" :as Ignore]
             ["/frontend/utils" :as utils]))
 
 (defn get-ref
@@ -61,6 +62,13 @@
   (assoc config
          :document/mode? (state/sub [:document/mode?])))
 
+(defn ignore-files
+  [pattern paths]
+  (-> (Ignore)
+      (.add pattern)
+      (.filter (bean/->js paths))
+      (bean/->clj)))
+
 (comment
   (let [repo (state/get-current-repo)]
     (p/let [remote-oid (get-remote-ref repo)

+ 1 - 0
src/main/frontend/handler/editor.cljs

@@ -1772,6 +1772,7 @@
          (and last-command
               (commands/get-matched-commands last-command)))))
     (catch js/Error e
+      (js/console.error e)
       nil)))
 
 (defn get-matched-block-commands

+ 0 - 8
src/main/frontend/handler/file.cljs

@@ -25,7 +25,6 @@
             [cljs-time.core :as t]
             [cljs-time.coerce :as tc]
             [frontend.utf8 :as utf8]
-            ["ignore" :as Ignore]
             ["/frontend/utils" :as utils]))
 
 ;; TODO: extract all git ops using a channel
@@ -303,13 +302,6 @@
           content (db/get-file path)]
       (alter-file repo path content {:re-render-root? true}))))
 
-(defn ignore-files
-  [pattern paths]
-  (-> (Ignore)
-      (.add pattern)
-      (.filter (bean/->js paths))
-      (bean/->clj)))
-
 ;; TODO: batch writes, how to deal with file history?
 (defn run-writes-chan!
   []

+ 14 - 7
src/main/frontend/handler/page.cljs

@@ -4,7 +4,6 @@
             [datascript.core :as d]
             [frontend.state :as state]
             [frontend.util :as util :refer-macros [profile]]
-            [frontend.tools.html-export :as html-export]
             [frontend.config :as config]
             [frontend.handler.common :as common-handler]
             [frontend.handler.route :as route-handler]
@@ -13,6 +12,7 @@
             [frontend.handler.git :as git-handler]
             [frontend.handler.editor :as editor-handler]
             [frontend.handler.project :as project-handler]
+            [frontend.handler.web.nfs :as web-nfs]
             [frontend.handler.notification :as notification]
             [frontend.handler.config :as config-handler]
             [frontend.handler.ui :as ui-handler]
@@ -173,9 +173,9 @@
     @plugins))
 
 (defn publish-page-as-slide!
-  ([page-name project-add-modal]
-   (publish-page-as-slide! page-name (db/get-page-blocks page-name) project-add-modal))
-  ([page-name blocks project-add-modal]
+  ([page-name project-add-modal export-page-html]
+   (publish-page-as-slide! page-name (db/get-page-blocks page-name) project-add-modal export-page-html))
+  ([page-name blocks project-add-modal export-page-html]
    (project-handler/exists-or-create!
     (fn [project]
       (config-handler/set-config! [:project :name] project)
@@ -186,7 +186,7 @@
             data {:project project
                   :title page-name
                   :permalink (:permalink properties)
-                  :html (html-export/export-page page-name blocks notification/show!)
+                  :html (export-page-html page-name blocks notification/show!)
                   :tags (:tags properties)
                   :settings (merge
                              (assoc properties
@@ -201,7 +201,7 @@
     project-add-modal)))
 
 (defn publish-page!
-  [page-name project-add-modal]
+  [page-name project-add-modal export-page-html]
   (project-handler/exists-or-create!
    (fn [project]
      (let [properties (db/get-page-properties page-name)
@@ -218,7 +218,7 @@
              (let [data {:project project
                          :title page-name
                          :permalink (:permalink properties)
-                         :html (html-export/export-page page-name blocks notification/show!)
+                         :html (export-page-html page-name blocks notification/show!)
                          :tags (:tags properties)
                          :settings (merge properties plugins)
                          :repo (state/get-current-repo)}]
@@ -496,6 +496,13 @@
         (let [templates (map string/lower-case templates)]
           (contains? (set templates) (string/lower-case title)))))))
 
+(defn ls-dir-files!
+  []
+  (web-nfs/ls-dir-files-with-handler!
+    (fn []
+      (init-commands!))))
+
+
 ;; TODO: add use :file/last-modified-at
 (defn get-pages-with-modified-at
   [repo]

+ 4 - 5
src/main/frontend/handler/web/nfs.cljs

@@ -4,12 +4,10 @@
             [promesa.core :as p]
             [medley.core :as medley]
             [goog.object :as gobj]
-            [goog.dom :as gdom]
             [frontend.util :as util]
             [frontend.handler.common :as common-handler]
             ["/frontend/utils" :as utils]
             [frontend.handler.repo :as repo-handler]
-            [frontend.handler.file :as file-handler]
             [frontend.idb :as idb]
             [frontend.state :as state]
             [clojure.string :as string]
@@ -33,7 +31,7 @@
       (if-let [file (:file/file ignore-file)]
         (p/let [content (.text file)]
           (when content
-            (let [paths (set (file-handler/ignore-files content (map :file/path files)))]
+            (let [paths (set (common-handler/ignore-files content (map :file/path files)))]
               (when (seq paths)
                 (filter (fn [f] (contains? paths (:file/path f))) files)))))
         (p/resolved files))
@@ -99,8 +97,8 @@
     (set-files-aux! handles)))
 
 ;; TODO: extract code for `ls-dir-files` and `reload-dir!`
-(defn ls-dir-files
-  []
+(defn ls-dir-files-with-handler!
+  [ok-handler]
   (let [path-handles (atom {})
         electron? (util/electron?)
         nfs? (not electron?)]
@@ -154,6 +152,7 @@
                                                        :nfs-files    files})
 
                        (state/add-repo! {:url repo :nfs? true})
+                       (and ok-handler (ok-handler))
                        (when (util/electron?)
                          (fs/watch-dir! dir-name)))))
            (p/catch (fn [error]

+ 1 - 1
src/main/frontend/state.cljs

@@ -144,7 +144,7 @@
 
 (defn get-current-repo
   []
-  (:git/current-repo @state))
+  (or (:git/current-repo @state) "local"))
 
 (defn get-config
   ([]