Browse Source

chore: create custom.css automatically

Also, make it possible to create a file using the search box.
Tienson Qin 5 years ago
parent
commit
5d4168ef6c

+ 5 - 3
deps.edn

@@ -4,11 +4,13 @@
   rum                         {:mvn/version "0.12.3"}
   ;; rum                         {:local/root "/home/tienson/codes/source/clj/rum"}
   ;; persistent-sorted-set       {:mvn/version "0.1.2"}
-  datascript                  {:git/url "https://github.com/tiensonqin/datascript",
-                               :sha "7c2822565d9a114c7d8604c335af89de4640e2e5"}
+  ;; FIXME: doesn't work on my archlinux laptop (tienson)
+  ;; The required namespace "datascript.core" is not available, it was required by "frontend/db.cljs".
+  ;; datascript                  {:git/url "https://github.com/tiensonqin/datascript",
+  ;;                              :sha "7c2822565d9a114c7d8604c335af89de4640e2e5"}
   datascript-transit          {:mvn/version "0.3.0"
                                :exclusions [datascript]}
-  ;; datascript                  {:mvn/version "1.0.1"}
+  datascript                  {:mvn/version "1.0.1"}
   funcool/promesa             {:mvn/version "4.0.2"}
   medley                      {:mvn/version "1.2.0"}
   metosin/reitit-frontend     {:mvn/version "0.3.10"}

+ 10 - 1
src/main/frontend/components/search.cljs

@@ -3,6 +3,7 @@
             [frontend.util :as util]
             [frontend.handler.route :as route]
             [frontend.handler.page :as page-handler]
+            [frontend.handler.file :as file-handler]
             [frontend.db :as db]
             [frontend.handler.search :as search-handler]
             [frontend.ui :as ui]
@@ -49,12 +50,13 @@
   [{:keys [pages files blocks]} search-q]
   (rum/with-context [[t] i18n/*tongue-context*]
     (let [new-page [{:type :new-page}]
+          new-file [{:type :new-file}]
           pages (map (fn [page] {:type :page :data page}) pages)
           files (map (fn [file] {:type :file :data file}) files)
           blocks (map (fn [block] {:type :block :data block}) blocks)
           result (if config/publishing?
                    (concat pages files blocks)
-                   (concat new-page pages files blocks))]
+                   (concat new-page pages new-file files blocks))]
       [:div.absolute.rounded-md.shadow-lg
        {:style (merge
                 {:top 48
@@ -73,6 +75,9 @@
                         (route/redirect! {:to :page
                                           :path-params {:name data}})
 
+                        :new-file
+                        (file-handler/create! search-q)
+
                         :file
                         (route/redirect! {:to :file
                                           :path-params {:path data}})
@@ -107,6 +112,10 @@
                           [:div.text.font-bold (str (t :new-page) ": ")
                            [:span.ml-1 (str "\"" search-q "\"")]]
 
+                          :new-file
+                          [:div.text.font-bold (str (t :new-file) ": ")
+                           [:span.ml-1 (str "\"" search-q "\"")]]
+
                           :page
                           [:div.text-sm.font-medium
                            [:span.text-xs.rounded.border.mr-2.px-1 {:title "Page"}

+ 2 - 0
src/main/frontend/dicts.cljs

@@ -297,6 +297,7 @@ title: How to take dummy notes?
                   "Search"
                   "Search or Create Page")
         :new-page "New page"
+        :new-file "New file"
         :graph "Graph"
         :publishing "Publishing"
         :all-repos "All repos"
@@ -543,6 +544,7 @@ title: How to take dummy notes?
                      "搜索"
                      "搜索或者创建新页面")
            :new-page "新页面"
+           :new-file "新文件"
            :graph "图谱"
            :publishing "发布/下载 HTML 文件"
            :all-repos "所有库"

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

@@ -9,6 +9,7 @@
             [frontend.handler.common :as common-handler]
             [frontend.handler.git :as git-handler]
             [frontend.handler.ui :as ui-handler]
+            [frontend.handler.route :as route-handler]
             [cljs-bean.core :as bean]
             [frontend.config :as config]
             [frontend.format :as format]
@@ -133,6 +134,18 @@
        (println "Write file failed, path: " path ", content: " content)
        (js/console.error error)))))
 
+(defn create!
+  ([path]
+   (create! path ""))
+  ([path content]
+   (when-let [repo (state/get-current-repo)]
+     (when (and path content)
+       (p/let [_ (alter-file repo path content {:reset? false
+                                                :re-render-root? false
+                                                :update-status? true})]
+         (route-handler/redirect! {:to :file
+                                   :path-params {:path path}})))))  )
+
 (defn alter-files
   ([repo files]
    (alter-files repo files {}))

+ 17 - 2
src/main/frontend/handler/repo.cljs

@@ -128,6 +128,19 @@
         (db/reset-file! repo-url path default-content)
         (git-handler/git-add repo-url path)))))
 
+(defn create-custom-theme
+  [repo-url]
+  (let [repo-dir (util/get-repo-dir repo-url)
+        path (str config/app-name "/" config/custom-css-file)
+        file-path (str "/" path)
+        default-content ""]
+    (p/let [_ (-> (fs/mkdir (str repo-dir "/" config/app-name))
+                  (p/catch (fn [_e])))
+            file-exists? (fs/create-if-not-exists repo-dir file-path default-content)]
+      (when-not file-exists?
+        (db/reset-file! repo-url path default-content)
+        (git-handler/git-add repo-url path)))))
+
 (defn create-dummy-notes-page
   [repo-url content]
   (let [repo-dir (util/get-repo-dir repo-url)
@@ -180,7 +193,8 @@
   (when-let [name (get-in @state/state [:me :name])]
     (create-config-file-if-not-exists repo-url)
     (create-today-journal-if-not-exists repo-url)
-    (create-contents-file repo-url)))
+    (create-contents-file repo-url)
+    (create-custom-theme repo-url)))
 
 (defn load-repo-to-db!
   [repo-url diffs first-clone?]
@@ -516,7 +530,8 @@
                         tutorial (string/replace-first tutorial "$today" (date/today))]
                     (create-today-journal-if-not-exists repo tutorial)))
               _ (create-config-file-if-not-exists repo)
-              _ (create-contents-file repo)]
+              _ (create-contents-file repo)
+              _ (create-custom-theme repo)]
         (state/set-db-restoring! false)))
     (js/setTimeout setup-local-repo-if-not-exists! 100)))