瀏覽代碼

fix set-priority on # heading

rcmerci 4 年之前
父節點
當前提交
2b8679ab14
共有 2 個文件被更改,包括 29 次插入17 次删除
  1. 4 17
      src/main/frontend/commands.cljs
  2. 25 0
      src/main/frontend/util.cljc

+ 4 - 17
src/main/frontend/commands.cljs

@@ -4,6 +4,7 @@
             [frontend.state :as state]
             [frontend.search :as search]
             [frontend.config :as config]
+            [frontend.db :as db]
             [clojure.string :as string]
             [goog.dom :as gdom]
             [goog.object :as gobj]
@@ -455,24 +456,10 @@
 (defmethod handle-step :editor/set-priority [[_ priority] format]
   (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)
-            priority-pattern  #"\[#[A|B|C]{1}\]"
-            prefix (subs edit-content 0 (dec slash-pos))
-            pos (count (re-find priority-pattern prefix))
+      (let [format (or (db/get-page-format (state/get-current-page)) (state/get-preferred-format))
+            edit-content (gobj/get current-input "value")
             new-priority (util/format "[#%s]" priority)
-            new-value (cond
-                        (re-find priority-pattern prefix)
-                        (str (subs edit-content 0 pos)
-                             (string/replace-first (subs edit-content pos)
-                                                   priority-pattern
-                                                   new-priority))
-                        (re-find util/marker-pattern edit-content)
-                        (string/replace-first edit-content util/marker-pattern
-                                              (fn [marker] (str marker new-priority " ")))
-
-                        :else
-                        (str new-priority " " (string/triml edit-content)))]
+            new-value (string/trim (util/add-or-update-priority edit-content format new-priority))]
         (state/set-edit-content! input-id new-value)))))
 
 (defmethod handle-step :editor/set-heading [[_ heading]]

+ 25 - 0
src/main/frontend/util.cljc

@@ -1032,6 +1032,31 @@
                                    (str marker " ")))]
     new-content))
 
+(defn add-or-update-priority
+  [content format priority]
+  (let [priority-pattern  #"(\[#[ABC]\])?\s?"
+        [re-pattern new-line-re-pattern]
+        (if (= :org format)
+          [#"\*+\s" #"\n\*+\s"]
+          [#"#+\s" #"\n#+\s"])
+        skip-hash-pos
+        (if-let [matches (seq (re-pos new-line-re-pattern content))]
+          (let [[start-pos content] (last matches)]
+            (+ start-pos (count content)))
+          (count (re-find re-pattern content)))
+        skip-marker-pos
+        (if-let [matches (seq (re-pos bare-marker-pattern (subs content skip-hash-pos)))]
+          (let [[start-pos content] (last matches)]
+            (+ start-pos (count content)))
+          0)
+        pos (+ skip-hash-pos skip-marker-pos)
+        new-content
+        (str (subs content 0 pos)
+             (string/replace-first (subs content pos)
+                                   priority-pattern
+                                   (str priority " ")))]
+    new-content))
+
 (defn pp-str [x]
   (with-out-str (clojure.pprint/pprint x)))