소스 검색

fix(editor): incorrect behavior of the delete key for deleting the last char

charlie 1 년 전
부모
커밋
369b5ece15
2개의 변경된 파일15개의 추가작업 그리고 9개의 파일을 삭제
  1. 8 6
      src/main/frontend/util.cljc
  2. 7 3
      src/test/frontend/util_test.cljs

+ 8 - 6
src/main/frontend/util.cljc

@@ -684,7 +684,7 @@
    (defn safe-dec-current-pos-from-end
      [input current-pos]
      (if-let [len (and (string? input) (.-length input))]
-       (when-let [input (and (>= len 2) (<= current-pos len)
+       (if-let [input (and (>= len 2) (<= current-pos len)
                              (.substring input (max (- current-pos 20) 0) current-pos))]
          (try
            (let [^js splitter (GraphemeSplitter.)
@@ -692,15 +692,16 @@
              (- current-pos (.-length (.pop input))))
            (catch :default e
              (js/console.error e)
-             (dec current-pos))))
-       (dec current-pos))))
+             (dec current-pos)))
+         (dec current-pos))
+       current-pos)))
 
 #?(:cljs
    ;; for widen char
    (defn safe-inc-current-pos-from-start
      [input current-pos]
      (if-let [len (and (string? input) (.-length input))]
-       (when-let [input (and (>= len 2) (<= current-pos len)
+       (if-let [input (and (>= len 2) (<= current-pos len)
                              (.substr input current-pos 20))]
          (try
            (let [^js splitter (GraphemeSplitter.)
@@ -708,8 +709,9 @@
              (+ current-pos (.-length (.shift input))))
            (catch :default e
              (js/console.error e)
-             (inc current-pos))))
-       (inc current-pos))))
+             (inc current-pos)))
+         (inc current-pos))
+       current-pos)))
 
 #?(:cljs
    (defn kill-line-before!

+ 7 - 3
src/test/frontend/util_test.cljs

@@ -14,13 +14,17 @@
     (is (= 3 (util/safe-dec-current-pos-from-end "abc😀" 5)))
     (is (= 0 (util/safe-dec-current-pos-from-end "😀" 2)))
     (is (= 4 (util/safe-dec-current-pos-from-end "abcde" 5)))
-    (is (= 1 (util/safe-dec-current-pos-from-end "中文" 2))))
+    (is (= 1 (util/safe-dec-current-pos-from-end "中文" 2)))
+    (is (= 0 (util/safe-dec-current-pos-from-end "中" 1)))
+    (is (= 0 (util/safe-dec-current-pos-from-end "a" 1))))
 
   (testing "safe current position from start for emoji"
     (is (= 5 (util/safe-inc-current-pos-from-start "abc😀d" 3)))
-    (is (= 2 (util/safe-inc-current-pos-from-start "😀" 0)))
     (is (= 2 (util/safe-inc-current-pos-from-start "abcde" 1)))
-    (is (= 1 (util/safe-inc-current-pos-from-start "中文" 0)))))
+    (is (= 1 (util/safe-inc-current-pos-from-start "中文" 0)))
+    (is (= 2 (util/safe-inc-current-pos-from-start "😀" 0)))
+    (is (= 1 (util/safe-inc-current-pos-from-start "中" 0)))
+    (is (= 1 (util/safe-inc-current-pos-from-start "a" 0)))))
 
 (deftest test-memoize-last
   (testing "memoize-last add test"