enhance: safe check input on selectionStart

This commit is contained in:
Tienson Qin
2022-01-19 23:08:50 +08:00
parent 3a2724fb4d
commit 73e0c9968a
5 changed files with 58 additions and 45 deletions

View File

@@ -29,7 +29,7 @@
If you only need character position, use `pos` instead. Do NOT call this."
[input]
(let [pos (.-selectionStart input)
(let [pos (util/get-selection-start input)
rect (bean/->clj (.. input (getBoundingClientRect) (toJSON)))]
(try
(-> (gdom/getElement "mock-text")
@@ -48,15 +48,15 @@
(defn pos [input]
(when input
(.-selectionStart input)))
(util/get-selection-start input)))
(defn start? [input]
(and input (zero? (.-selectionStart input))))
(and input (zero? (util/get-selection-start input))))
(defn end? [input]
(and input
(= (count (.-value input))
(.-selectionStart input))))
(util/get-selection-start input))))
(defn set-selection-to [input n m]
(.setSelectionRange input n m))
@@ -147,7 +147,7 @@
(defn move-cursor-forward-by-word
[input]
(let [val (.-value input)
current (.-selectionStart input)
current (util/get-selection-start input)
current (loop [idx current]
(if (#{\space \newline} (util/nth-safe val idx))
(recur (inc idx))
@@ -162,7 +162,7 @@
(defn move-cursor-backward-by-word
[input]
(let [val (.-value input)
current (.-selectionStart input)
current (util/get-selection-start input)
prev (or
(->> [(string/last-index-of val \space (dec current))
(string/last-index-of val \newline (dec current))]
@@ -243,7 +243,7 @@
"Move cursor up. If EOL, always move cursor to previous EOL."
[input]
(let [val (gobj/get input "value")
pos (.-selectionStart input)
pos (util/get-selection-start input)
prev-idx (string/last-index-of val \newline pos)
pprev-idx (or (string/last-index-of val \newline (dec prev-idx)) -1)
cal-idx (+ pprev-idx pos (- prev-idx))]
@@ -257,7 +257,7 @@
If EOL, always move cursor to next EOL."
[input]
(let [val (gobj/get input "value")
pos (.-selectionStart input)
pos (util/get-selection-start input)
prev-idx (or (string/last-index-of val \newline pos) -1)
next-idx (or (string/index-of val \newline (inc pos))
(count val))