diff --git a/src/main/frontend/commands.cljs b/src/main/frontend/commands.cljs index 6b5300172..4443d7ee4 100644 --- a/src/main/frontend/commands.cljs +++ b/src/main/frontend/commands.cljs @@ -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)) diff --git a/src/main/frontend/fs/nfs.cljs b/src/main/frontend/fs/nfs.cljs index f77924789..66d830b40 100644 --- a/src/main/frontend/fs/nfs.cljs +++ b/src/main/frontend/fs/nfs.cljs @@ -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)]