mirror of https://github.com/logseq/logseq
Add template support
parent
ff905cd5c3
commit
800e623205
|
@ -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]
|
||||
|
|
|
@ -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)))))
|
||||
|
|
|
@ -672,8 +672,8 @@
|
|||
(when-let [node (gdom/getElement (str id))]
|
||||
(when-let [cursor-range (state/get-cursor-range)]
|
||||
(when-let [range (string/trim cursor-range)]
|
||||
(let [pos (inc (diff/find-position markup range))]
|
||||
(util/set-caret-pos! node pos)))))))
|
||||
(let [pos (inc (diff/find-position markup range))]
|
||||
(util/set-caret-pos! node pos)))))))
|
||||
|
||||
(defn search
|
||||
[q]
|
||||
|
@ -844,32 +844,36 @@
|
|||
nil)))))
|
||||
|
||||
(defn insert-new-heading!
|
||||
[{:heading/keys [uuid content meta file dummy? level] :as heading} value]
|
||||
(let [repo (state/get-current-repo)
|
||||
value (string/trim value)
|
||||
heading (with-heading-meta heading)
|
||||
format (:heading/format heading)
|
||||
new-heading-content (config/default-empty-heading format level)]
|
||||
(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")
|
||||
[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)
|
||||
last-heading (last headings)
|
||||
after-headings (rebuild-after-headings repo file (:end-pos meta) end-pos)]
|
||||
(transact-react-and-alter-file!
|
||||
repo
|
||||
(concat
|
||||
pages
|
||||
headings
|
||||
after-headings)
|
||||
{:key :heading/change
|
||||
:data headings}
|
||||
file-path
|
||||
new-content)
|
||||
[first-heading last-heading new-heading-content])))
|
||||
([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)
|
||||
format (:heading/format heading)
|
||||
new-heading-content (config/default-empty-heading format level)]
|
||||
(let [file (db/entity (:db/id file))
|
||||
file-path (:file/path file)
|
||||
file-content (db/get-file file-path)
|
||||
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)
|
||||
last-heading (last headings)
|
||||
after-headings (rebuild-after-headings repo file (:end-pos meta) end-pos)]
|
||||
(transact-react-and-alter-file!
|
||||
repo
|
||||
(concat
|
||||
pages
|
||||
headings
|
||||
after-headings)
|
||||
{:key :heading/change
|
||||
:data headings}
|
||||
file-path
|
||||
new-content)
|
||||
[first-heading last-heading new-heading-content]))))
|
||||
|
||||
;; TODO: utf8 encode performance
|
||||
(defn check
|
||||
|
|
Loading…
Reference in New Issue