Selaa lähdekoodia

Fix of bug "TODO mobile bar shortcut only works when called in the beginning of the line" (#1283)

Added insert-before! function
Fix bug Mobile bar TODO shortcut only works in the beginning of the line
akhater 4 vuotta sitten
vanhempi
sitoutus
46fcbd0a78
2 muutettua tiedostoa jossa 21 lisäystä ja 1 poistoa
  1. 20 0
      src/main/frontend/commands.cljs
  2. 1 1
      src/main/frontend/components/editor.cljs

+ 20 - 0
src/main/frontend/commands.cljs

@@ -261,6 +261,26 @@
     (when check-fn
       (check-fn new-value (dec (count prefix)) new-pos))))
 
+(defn insert-before!
+  [id value
+   {:keys [backward-pos forward-pos check-fn]
+    :as option}]
+  (let [input (gdom/getElement id)
+        edit-content (gobj/get input "value")
+        current-pos (:pos (util/get-caret-pos input))
+        suffix (subs edit-content 0 current-pos)
+        new-value (str value
+                       suffix
+                       (subs edit-content current-pos))
+        new-pos (- (+ (count suffix)
+                      (count value)
+                      (or forward-pos 0))
+                   (or backward-pos 0))]
+    (state/set-block-content-and-last-pos! id new-value new-pos)
+    (util/move-cursor-to input new-pos)
+    (when check-fn
+      (check-fn new-value (dec (count suffix)) new-pos))))
+
 (defn simple-replace!
   [id value selected
    {:keys [backward-pos forward-pos check-fn]

+ 1 - 1
src/main/frontend/components/editor.cljs

@@ -250,7 +250,7 @@
     {:on-click #(commands/simple-insert! parent-id "\n" {})}
     svg/multi-line-input]
    [:button.bottom-action
-    {:on-click #(commands/simple-insert! parent-id "TODO " {})}
+    {:on-click #(commands/insert-before! parent-id "TODO " {})} 
     svg/checkbox]
    [:button.font-extrabold.bottom-action.-mt-1
     {:on-click #(commands/simple-insert!