|
|
@@ -1,16 +1,8 @@
|
|
|
(ns frontend.modules.file.core
|
|
|
(:require [clojure.string :as string]
|
|
|
- [frontend.config :as config]
|
|
|
- [frontend.date :as date]
|
|
|
[frontend.db :as db]
|
|
|
- [frontend.db.model :as model]
|
|
|
- [frontend.db.utils :as db-utils]
|
|
|
- [frontend.handler.file :as file-handler]
|
|
|
[frontend.state :as state]
|
|
|
- [frontend.util.fs :as fs-util]
|
|
|
- [frontend.handler.file-based.property.util :as property-util]
|
|
|
- [logseq.common.path :as path]
|
|
|
- [frontend.worker.file.util :as wfu]))
|
|
|
+ [frontend.handler.file-based.property.util :as property-util]))
|
|
|
|
|
|
(defn- indented-block-content
|
|
|
[content spaces-tabs]
|
|
|
@@ -109,70 +101,3 @@
|
|
|
(defn tree->file-content
|
|
|
[tree opts]
|
|
|
(->> (tree->file-content-aux tree opts) (string/join "\n")))
|
|
|
-
|
|
|
-
|
|
|
-(def init-level 1)
|
|
|
-
|
|
|
-(defn- transact-file-tx-if-not-exists!
|
|
|
- [page-block ok-handler]
|
|
|
- (when (and (state/get-current-repo)
|
|
|
- (:block/name page-block))
|
|
|
- (let [format (name (get page-block :block/format
|
|
|
- (state/get-preferred-format)))
|
|
|
- title (string/capitalize (:block/name page-block))
|
|
|
- whiteboard-page? (model/whiteboard-page? page-block)
|
|
|
- format (if whiteboard-page? "edn" format)
|
|
|
- journal-page? (date/valid-journal-title? title)
|
|
|
- journal-title (date/normalize-journal-title title)
|
|
|
- journal-page? (and journal-page? (not (string/blank? journal-title)))
|
|
|
- filename (if journal-page?
|
|
|
- (date/date->file-name journal-title)
|
|
|
- (-> (or (:block/original-name page-block) (:block/name page-block))
|
|
|
- (fs-util/file-name-sanity)))
|
|
|
- sub-dir (cond
|
|
|
- journal-page? (config/get-journals-directory)
|
|
|
- whiteboard-page? (config/get-whiteboards-directory)
|
|
|
- :else (config/get-pages-directory))
|
|
|
- ext (if (= format "markdown") "md" format)
|
|
|
- file-rpath (path/path-join sub-dir (str filename "." ext))
|
|
|
- file {:file/path file-rpath}
|
|
|
- tx [{:file/path file-rpath}
|
|
|
- {:block/name (:block/name page-block)
|
|
|
- :block/file file}]]
|
|
|
- (db/transact! tx)
|
|
|
- (when ok-handler (ok-handler)))))
|
|
|
-
|
|
|
-(defn- remove-transit-ids [block] (dissoc block :db/id :block/file))
|
|
|
-
|
|
|
-(defn save-tree-aux!
|
|
|
- [page-block tree blocks-just-deleted?]
|
|
|
- (let [page-block (db/pull (:db/id page-block))
|
|
|
- file-db-id (-> page-block :block/file :db/id)
|
|
|
- file-path (-> (db-utils/entity file-db-id) :file/path)]
|
|
|
- (if (and (string? file-path) (not-empty file-path))
|
|
|
- (let [new-content (if (contains? (:block/type page-block) "whiteboard")
|
|
|
- (->
|
|
|
- (wfu/ugly-pr-str {:blocks tree
|
|
|
- :pages (list (remove-transit-ids page-block))})
|
|
|
- (string/triml))
|
|
|
- (tree->file-content tree {:init-level init-level}))]
|
|
|
- (if (and (string/blank? new-content)
|
|
|
- (not blocks-just-deleted?))
|
|
|
- (state/pub-event! [:capture-error {:error (js/Error. "Empty content")
|
|
|
- :payload {}}])
|
|
|
- (let [files [[file-path new-content]]
|
|
|
- repo (state/get-current-repo)]
|
|
|
- (file-handler/alter-files-handler! repo files {} {}))))
|
|
|
- ;; In e2e tests, "card" page in db has no :file/path
|
|
|
- (js/console.error "File path from page-block is not valid" page-block tree))))
|
|
|
-
|
|
|
-(defn save-tree!
|
|
|
- [page-block tree blocks-just-deleted?]
|
|
|
- {:pre [(map? page-block)]}
|
|
|
- (let [ok-handler #(save-tree-aux! page-block tree blocks-just-deleted?)
|
|
|
- file (or (:block/file page-block)
|
|
|
- (when-let [page (:db/id (:block/page page-block))]
|
|
|
- (:block/file (db-utils/entity page))))]
|
|
|
- (if file
|
|
|
- (ok-handler)
|
|
|
- (transact-file-tx-if-not-exists! page-block ok-handler))))
|