Add template support

pull/645/head
Tienson Qin 2020-05-30 16:43:40 +08:00
parent ff905cd5c3
commit 800e623205
3 changed files with 49 additions and 37 deletions

View File

@ -11,18 +11,27 @@
[frontend.components.hiccup :as hiccup]
[frontend.components.reference :as reference]
[frontend.utf8 :as utf8]
[goog.object :as gobj]))
[goog.object :as gobj]
[clojure.string :as string]))
(rum/defc journal-cp < rum/reactive
[[title headings format]]
[[title format]]
(let [;; Don't edit the journal title
page (string/lower-case title)
headings (db/get-page-headings page)
headings (when (seq headings)
(update (vec headings) 0 assoc :heading/lock? true))
headings (db/with-dummy-heading headings format nil true)
raw-headings (db/get-page-headings page)
raw-headings (when (seq raw-headings)
(update (vec raw-headings) 0 assoc :heading/lock? true))
headings (db/with-dummy-heading raw-headings format nil true)
encoded-page-name (util/url-encode page)]
(when (and
(= (string/lower-case title)
(string/lower-case (util/journal-name)))
(= 1 (count raw-headings)))
(when-let [template (state/get-journal-template)]
(handler/insert-new-heading!
(first headings)
(str (:heading/content (first headings)) "\n" template)
false)))
[:div.flex-1
[:a.initial-color {:href (str "/page/" encoded-page-name)
:on-click (fn [e]

View File

@ -87,5 +87,4 @@
(default-empty-heading format 2))
([format n]
(let [heading-pattern (get-heading-pattern format)]
(str (apply str (repeat n heading-pattern))
" "))))
(apply str (repeat n heading-pattern)))))

View File

@ -844,7 +844,9 @@
nil)))))
(defn insert-new-heading!
[{:heading/keys [uuid content meta file dummy? level] :as heading} value]
([heading value]
(insert-new-heading! heading value true))
([{:heading/keys [uuid content meta file dummy? level] :as heading} value create-new-heading?]
(let [repo (state/get-current-repo)
value (string/trim value)
heading (with-heading-meta heading)
@ -853,7 +855,9 @@
(let [file (db/entity (:db/id file))
file-path (:file/path file)
file-content (db/get-file file-path)
value (str value "\n" new-heading-content "\n")
value (if create-new-heading?
(str value "\n" new-heading-content "\n")
(str value "\n"))
[new-content value] (new-file-content heading file-content value)
{:keys [headings pages start-pos end-pos]} (block/parse-heading (assoc heading :heading/content value) format)
first-heading (first headings)
@ -869,7 +873,7 @@
:data headings}
file-path
new-content)
[first-heading last-heading new-heading-content])))
[first-heading last-heading new-heading-content]))))
;; TODO: utf8 encode performance
(defn check