Tienson Qin 4 лет назад
Родитель
Сommit
625428c05c
2 измененных файлов с 53 добавлено и 13 удалено
  1. 34 1
      src/main/frontend/commands.cljs
  2. 19 12
      src/main/frontend/fs/nfs.cljs

+ 34 - 1
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))
 

+ 19 - 12
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)]