feat: add h1 ~ h6 commands

pull/1697/head
Tienson Qin 2021-04-28 20:26:37 +08:00
parent 4e688de2ac
commit 625428c05c
2 changed files with 53 additions and 13 deletions

View File

@ -89,6 +89,20 @@
["NOW" (->marker "NOW")]])))
;; Credits to roamresearch.com
(defn- ->heading
[heading]
[[:editor/clear-current-slash]
[:editor/set-heading heading]
[:editor/move-cursor-to-end]])
(defn- markdown-headings
[]
(let [format (state/get-preferred-format)]
(when (= (name format) "markdown")
(mapv (fn [level]
(let [heading (str "h" level)]
[heading (->heading (apply str (repeat level "#")))])) (range 1 7)))))
(defn commands-map
[get-page-ref-text]
(->>
@ -157,7 +171,8 @@
;; TODO:
;; ["Upload a file" nil]
]
]
(markdown-headings)
;; Allow user to modify or extend, should specify how to extend.
(state/get-commands))
(remove nil?)
@ -459,6 +474,24 @@
(str new-priority " " (string/triml edit-content)))]
(state/set-edit-content! input-id new-value)))))
(defmethod handle-step :editor/set-heading [[_ heading]]
(when-let [input-id (state/get-edit-input-id)]
(when-let [current-input (gdom/getElement input-id)]
(let [edit-content (gobj/get current-input "value")
slash-pos (:pos @*slash-caret-pos)
heading-pattern #"^#\+"
prefix (subs edit-content 0 (dec slash-pos))
pos (count (re-find heading-pattern prefix))
new-value (cond
(re-find heading-pattern prefix)
(str (subs edit-content 0 pos)
(string/replace-first (subs edit-content pos)
heading-pattern
heading))
:else
(str heading " " (string/triml edit-content)))]
(state/set-edit-content! input-id new-value)))))
(defmethod handle-step :editor/search-page [[_]]
(state/set-editor-show-page-search! true))

View File

@ -137,23 +137,30 @@
(p/let [_ (verify-permission repo file-handle true)
_ (utils/writeFile file-handle content)
file (.getFile file-handle)]
(if (and local-content new?
(or
draw?
;; Writing not finished
(> pending-writes 0)
;; not changed by other editors
not-changed?
new-created?))
(do
(do
(p/let [_ (verify-permission repo file-handle true)
_ (utils/writeFile file-handle content)
file (.getFile file-handle)]
(when file
(nfs-saved-handler repo path file))))
(do
(js/alert (str "The file has been modified on your local disk! File path: " path
", please save your changes and click the refresh button to reload it."))))))
;; (if (and local-content new?
;; (or
;; draw?
;; ;; Writing not finished
;; (> pending-writes 0)
;; ;; not changed by other editors
;; not-changed?
;; new-created?))
;; (do
;; (p/let [_ (verify-permission repo file-handle true)
;; _ (utils/writeFile file-handle content)
;; file (.getFile file-handle)]
;; (when file
;; (nfs-saved-handler repo path file))))
;; (do
;; (js/alert (str "The file has been modified on your local disk! File path: " path
;; ", please save your changes and click the refresh button to reload it."))))
))
;; create file handle
(->
(p/let [handle (idb/get-item handle-path)]