Sfoglia il codice sorgente

fix: ordered list numbers

Tienson Qin 3 anni fa
parent
commit
87007f8b07

+ 12 - 13
src/main/frontend/handler/editor.cljs

@@ -2133,19 +2133,18 @@
               (if (= (count full-content)
                      (+ (if ordered (+ (count (str bullet)) 2) 2) (when checkbox (count checkbox))))
                 (delete-and-update input (cursor/line-beginning-pos input) (cursor/line-end-pos input))
-                (do (cursor/move-cursor-to-line-end input)
-                    (insert (str "\n" indent next-bullet " " checkbox))
-                    (when ordered
-                      (let [bullet-atom (atom (inc bullet))]
-                        (while (when-let [next-item (list/get-next-item input)]
-                                 (swap! bullet-atom inc)
-                                 (let [{:keys [full-content start end]} next-item
-                                       new-bullet @bullet-atom]
-                                   (delete-and-update input start end)
-                                   (insert (string/replace-first full-content (:bullet next-item) new-bullet))
-                                   true))
-                          nil)
-                        (cursor/move-cursor-to input (+ (:end item) (count next-bullet) 2)))))))))))))
+                (do
+                  (cursor/move-cursor-to-line-end input)
+                  (insert (str "\n" indent next-bullet " " checkbox))
+                  (when ordered
+                    (let [value (.-value input)
+                          start-pos (util/get-selection-start input)
+                          after-lists-str (string/trim (subs value start-pos))
+                          lines (string/split-lines after-lists-str)
+                          after-lists-str' (list/re-order-items lines (inc bullet))
+                          value' (str (subs value 0 start-pos) "\n" after-lists-str')]
+                      (state/set-edit-content! (state/get-edit-input-id) value')
+                      (cursor/move-cursor-to input (+ (:end item) (count next-bullet) 2)))))))))))))
 
 (defn toggle-list!
   []

+ 20 - 1
src/main/frontend/util/list.cljs

@@ -1,6 +1,8 @@
 (ns frontend.util.list
   (:require [frontend.util.thingatpt :as thingatpt]
-            [frontend.util.cursor :as cursor]))
+            [frontend.util.cursor :as cursor]
+            [clojure.string :as string]
+            [frontend.util :as util]))
 
 (defn get-prev-item [& [input]]
   (when-not (cursor/textarea-cursor-first-row? input)
@@ -37,3 +39,20 @@
                (reset! end-pos (:end next-item))))
       (cursor/move-cursor-to input current-pos)
       @end-pos)))
+
+(defn re-order-items
+  [lines start-idx]
+  (loop [lines lines
+         idx start-idx
+         result nil]
+    (let [[line & others] lines]
+      (if (empty? lines)
+        result
+        (let [[_ num-str] (re-find #"^(\d+){1}\." line)
+              num (if num-str (util/safe-parse-int num-str) nil)
+              [idx' result'] (if num
+                               (let [idx' (inc idx)
+                                     line' (string/replace-first line (str num ".") (str idx' "."))]
+                                 [idx' (if result (str result "\n" line') line')])
+                               [idx (str result "\n" line)])]
+          (recur others idx' result'))))))

+ 1 - 1
src/main/frontend/util/thingatpt.cljs

@@ -91,7 +91,7 @@
                    :end (+ line-beginning-pos (count (str key "::")))}))))]
       (assoc property :type "property-key"))))
 
-(defn- get-list-item-indent&bullet [line]
+(defn get-list-item-indent&bullet [line]
   (when-not (string/blank? line)
     (or (re-matches #"^([ \t\r]*)(\+|\*|-){1} (\[[X ]\])?.*$" line)
         (re-matches #"^([\s]*)(\d+){1}\. (\[[X ]\])?.*$" line))))

+ 18 - 0
src/test/frontend/util/list_test.cljs

@@ -0,0 +1,18 @@
+(ns frontend.util.list-test
+  (:require [cljs.test :refer [deftest is]]
+            [frontend.util.list :as list-util]))
+
+(deftest test-re-order-items
+  (is (= (list-util/re-order-items
+          ["2. x"
+           "3. x"]
+          2)
+         "3. x\n4. x"))
+  (is (= (list-util/re-order-items
+          ["7. x"
+           "foo"
+           "bar 3."
+           "5. x"
+           "baz"]
+          2)
+         "3. x\nfoo\nbar 3.\n4. x\nbaz")))