refactor: clean code

pull/1735/head
Tienson Qin 2021-05-02 18:12:37 +08:00
parent b8b32febd3
commit 30f21c7bba
9 changed files with 11 additions and 79 deletions

View File

@ -5,7 +5,6 @@
[frontend.components.svg :as svg] [frontend.components.svg :as svg]
[frontend.handler.route :as route] [frontend.handler.route :as route]
[frontend.handler.page :as page-handler] [frontend.handler.page :as page-handler]
[frontend.handler.file :as file-handler]
[frontend.db :as db] [frontend.db :as db]
[frontend.handler.search :as search-handler] [frontend.handler.search :as search-handler]
[frontend.ui :as ui] [frontend.ui :as ui]
@ -141,10 +140,7 @@
(rum/defc search-auto-complete (rum/defc search-auto-complete
[{:keys [pages files blocks has-more?] :as result} search-q all?] [{:keys [pages files blocks has-more?] :as result} search-q all?]
(rum/with-context [[t] i18n/*tongue-context*] (rum/with-context [[t] i18n/*tongue-context*]
(let [new-file (when-let [ext (util/get-file-ext search-q)] (let [pages (when-not all? (map (fn [page] {:type :page :data page}) pages))
(when (contains? config/mldoc-support-formats (keyword (string/lower-case ext)))
[{:type :new-file}]))
pages (when-not all? (map (fn [page] {:type :page :data page}) pages))
files (when-not all? (map (fn [file] {:type :file :data file}) files)) files (when-not all? (map (fn [file] {:type :file :data file}) files))
blocks (map (fn [block] {:type :block :data block}) blocks) blocks (map (fn [block] {:type :block :data block}) blocks)
search-mode (state/get-search-mode) search-mode (state/get-search-mode)
@ -159,7 +155,7 @@
[{:type :new-page}]) [{:type :new-page}])
result (if config/publishing? result (if config/publishing?
(concat pages files blocks) (concat pages files blocks)
(concat new-page pages new-file files blocks))] (concat new-page pages files blocks))]
[:div.rounded-md.shadow-lg [:div.rounded-md.shadow-lg
{:style (merge {:style (merge
{:top 48 {:top 48
@ -180,9 +176,6 @@
(route/redirect! {:to :page (route/redirect! {:to :page
:path-params {:name data}}) :path-params {:name data}})
:new-file
(file-handler/create! search-q)
:file :file
(route/redirect! {:to :file (route/redirect! {:to :file
:path-params {:path data}}) :path-params {:path data}})
@ -221,10 +214,6 @@
[:div.text.font-bold (str (t :new-page) ": ") [:div.text.font-bold (str (t :new-page) ": ")
[:span.ml-1 (str "\"" search-q "\"")]] [:span.ml-1 (str "\"" search-q "\"")]]
:new-file
[:div.text.font-bold (str (t :new-file) ": ")
[:span.ml-1 (str "\"" search-q "\"")]]
:page :page
(search-result-item "Page" (highlight-exact-query data search-q)) (search-result-item "Page" (highlight-exact-query data search-q))

View File

@ -3,7 +3,6 @@
[frontend.util :as util] [frontend.util :as util]
[goog.dom :as gdom] [goog.dom :as gdom]
[goog.object :as gobj] [goog.object :as gobj]
[medley.core :as medley]
[frontend.db :as db] [frontend.db :as db]
[frontend.state :as state] [frontend.state :as state]
[frontend.handler.editor :as editor-handler] [frontend.handler.editor :as editor-handler]

View File

@ -2,7 +2,6 @@
(:require [frontend.state :as state] (:require [frontend.state :as state]
[frontend.handler.file :as file-handler] [frontend.handler.file :as file-handler]
[frontend.config :as config] [frontend.config :as config]
[frontend.db :as db]
[clojure.string :as string])) [clojure.string :as string]))
(defn set-config! (defn set-config!
@ -18,7 +17,3 @@
[project] [project]
(when-not (string/blank? project) (when-not (string/blank? project)
(set-config! [:project :name] project))) (set-config! [:project :name] project)))
(defn set-preferred-workflow!
[workflow]
(set-config! :preferred-workflow (name workflow)))

View File

@ -7,11 +7,7 @@
[frontend.db :as db] [frontend.db :as db]
[frontend.handler.file :as file-handler] [frontend.handler.file :as file-handler]
[frontend.date :as date] [frontend.date :as date]
[frontend.config :as config] [frontend.config :as config]))
[frontend.storage :as storage]
[clojure.string :as string]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]))
(defn create-draws-directory! (defn create-draws-directory!
[repo] [repo]

View File

@ -58,31 +58,9 @@
input input
:upload-images)))) :upload-images))))
(editor-handler/clear-when-saved!) (editor-handler/clear-when-saved!)
(if file? ;; TODO: ugly
(let [path (:file-path config) (when-not (contains? #{:insert :indent :outdent :auto-save :undo :redo} (state/get-editor-op))
content (db/get-file-no-sub path) (editor-handler/save-block! (get-state) value)))
value (some-> (gdom/getElement path)
(gobj/get "value"))]
(when (and
(not (string/blank? value))
(not= (string/trim value) (string/trim content)))
(let [old-page-name (db/get-file-page path false)]
(p/let [[journal? new-name] (page-handler/rename-when-alter-title-property! old-page-name path format content value)]
(if (and journal? new-name (not= old-page-name (string/lower-case new-name)))
(notification/show! "Journal title can't be changed." :warning)
(let [new-name (if journal? (date/journal-title->default new-name) new-name)
new-path (if new-name
(if (and
new-name old-page-name
(= (string/lower-case new-name) (string/lower-case old-page-name)))
path
(page-handler/compute-new-file-path path new-name))
path)]
(file/alter-file (state/get-current-repo) new-path (string/trim value)
{:re-render-root? true})))))))
;; TODO: ugly
(when-not (contains? #{:insert :indent :outdent :auto-save :undo :redo} (state/get-editor-op))
(editor-handler/save-block! (get-state) value))))
state) state)
(def lifecycle (def lifecycle

View File

@ -7,7 +7,6 @@
[frontend.date :as date] [frontend.date :as date]
[frontend.config :as config] [frontend.config :as config]
[clojure.string :as string] [clojure.string :as string]
[promesa.core :as p]
[frontend.db :as db])) [frontend.db :as db]))
(defonce debug-files (atom nil)) (defonce debug-files (atom nil))

View File

@ -276,7 +276,7 @@
(when-not (string/blank? file) (when-not (string/blank? file)
(-> (->
(p/let [_ (or (config/local-db? repo) (git/remove-file repo file)) (p/let [_ (or (config/local-db? repo) (git/remove-file repo file))
result (fs/unlink! (config/get-repo-path repo file) nil)] _ (fs/unlink! (config/get-repo-path repo file) nil)]
(when-let [file (db/entity repo [:file/path file])] (when-let [file (db/entity repo [:file/path file])]
(common-handler/check-changed-files-status) (common-handler/check-changed-files-status)
(let [file-id (:db/id file) (let [file-id (:db/id file)

View File

@ -6,8 +6,7 @@
[frontend.db :as db] [frontend.db :as db]
[datascript.db :as ddb] [datascript.db :as ddb]
[clojure.string :as string] [clojure.string :as string]
[promesa.core :as p] [promesa.core :as p]))
[medley.core :as medley]))
(def default-metadata-str "{}") (def default-metadata-str "{}")

View File

@ -22,10 +22,7 @@
[frontend.fs :as fs] [frontend.fs :as fs]
[promesa.core :as p] [promesa.core :as p]
[lambdaisland.glogi :as log] [lambdaisland.glogi :as log]
[frontend.format.mldoc :as mldoc]
[frontend.format.block :as block] [frontend.format.block :as block]
[cljs-time.core :as t]
[cljs-time.coerce :as tc]
[cljs.reader :as reader] [cljs.reader :as reader]
[goog.object :as gobj])) [goog.object :as gobj]))
@ -304,11 +301,7 @@
(let [new-name (string/trim new-name)] (let [new-name (string/trim new-name)]
(when-not (string/blank? new-name) (when-not (string/blank? new-name)
(when (and old-name new-name) (when (and old-name new-name)
(let [case-changed? (and (= (string/lower-case old-name) (let [name-changed? (not= (string/lower-case (string/trim old-name))
(string/lower-case new-name))
(not= (string/trim old-name)
(string/trim new-name)))
name-changed? (not= (string/lower-case (string/trim old-name))
(string/lower-case (string/trim new-name)))] (string/lower-case (string/trim new-name)))]
(when-let [repo (state/get-current-repo)] (when-let [repo (state/get-current-repo)]
(when-let [page (db/entity [:block/name (string/lower-case old-name)])] (when-let [page (db/entity [:block/name (string/lower-case old-name)])]
@ -317,13 +310,12 @@
journal? (:block/journal? page)] journal? (:block/journal? page)]
(d/transact! (db/get-conn repo false) (d/transact! (db/get-conn repo false)
[{:db/id (:db/id page) [{:db/id (:db/id page)
:block/uuid (:block/uuid page)
:block/name (string/lower-case new-name) :block/name (string/lower-case new-name)
:block/original-name new-name}]) :block/original-name new-name}])
(when (and file (not journal?) name-changed?) (when (and file (not journal?) name-changed?)
(rename-file! file new-name (rename-file! file new-name (fn [] nil)))
(fn []
(page-add-properties! (string/lower-case new-name) {:title new-name}))))
;; update all files which have references to this page ;; update all files which have references to this page
(let [files (db/get-files-that-referenced-page (:db/id page))] (let [files (db/get-files-that-referenced-page (:db/id page))]
@ -351,21 +343,6 @@
(ui-handler/re-render-root!)))))))) (ui-handler/re-render-root!))))))))
(defn rename-when-alter-title-property!
[page path format original-content content]
(when (and page (contains? config/mldoc-support-formats format))
(let [old-name page
new-name (let [ast (mldoc/->edn content (mldoc/default-config format))]
(db/get-page-name path ast))
journal? (date/valid-journal-title? old-name)]
(if (not= old-name new-name)
(if journal?
[true old-name]
(do
(rename! old-name new-name)
[false new-name]))
[journal? old-name]))))
(defn handle-add-page-to-contents! (defn handle-add-page-to-contents!
[page-name] [page-name]
(let [last-block (last (db/get-page-blocks (state/get-current-repo) "contents")) (let [last-block (last (db/get-page-blocks (state/get-current-repo) "contents"))