add and fix some util fns

pull/1656/head
Weihua Lu 2021-04-09 18:34:36 +08:00 committed by Tienson Qin
parent da09487775
commit a1822eb02b
1 changed files with 57 additions and 11 deletions

View File

@ -631,20 +631,19 @@
#?(:cljs
(defn cursor-move-back [input n]
(let [{:keys [pos]} (get-caret-pos input)]
(set! (.-selectionStart input) (- pos n))
(set! (.-selectionEnd input) (- pos n)))))
(let [{:keys [pos]} (get-caret-pos input)
pos (- pos n)]
(.setSelectionRange input pos pos))))
#?(:cljs
(defn cursor-move-forward [input n]
(let [{:keys [pos]} (get-caret-pos input)]
(set! (.-selectionStart input) (+ pos n))
(set! (.-selectionEnd input) (+ pos n)))))
(let [{:keys [pos]} (get-caret-pos input)
pos (+ pos n)]
(.setSelectionRange input pos pos))))
#?(:cljs
(defn move-cursor-to [input n]
(set! (.-selectionStart input) n)
(set! (.-selectionEnd input) n)))
(.setSelectionRange input n n)))
#?(:cljs
(defn move-cursor-to-end
@ -652,6 +651,33 @@
(let [pos (count (gobj/get input "value"))]
(move-cursor-to input pos))))
#?(:cljs
(defn move-cursor-up
"Move cursor up. If EOL, always move cursor to previous EOL."
[input]
(let [val (gobj/get input "value")
{:keys [pos]} (get-caret-pos 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))]
(if (or (== pos (count val))
(> (- pos prev-idx) (- prev-idx pprev-idx)))
(move-cursor-to input prev-idx)
(move-cursor-to input cal-idx)))))
#?(:cljs
(defn move-cursor-down
"Move cursor down by calculating current cursor line pos.
If EOL, always move cursor to next EOL."
[input]
(let [val (gobj/get input "value")
{:keys [pos]} (get-caret-pos input)
prev-idx (or (string/last-index-of val \newline pos) -1)
next-idx (or (string/index-of val \newline (inc pos))
(count val))
cal-idx (+ next-idx pos (- prev-idx))]
(move-cursor-to input cal-idx))))
;; copied from re_com
#?(:cljs
(defn deref-or-value
@ -703,6 +729,18 @@
end (inc (max idx-1 idx-2))]
(safe-subvec (vec nodes) start end)))))
#?(:cljs
(defn get-direction-between-two-nodes
[id1 id2 class]
(when-let [nodes (array-seq (js/document.getElementsByClassName class))]
(let [node-1 (gdom/getElement id1)
node-2 (gdom/getElement id2)
idx-1 (.indexOf nodes node-1)
idx-2 (.indexOf nodes node-2)]
(if (>= idx-1 idx-2)
:up
:down)))))
#?(:cljs
(defn rec-get-block-node
[node]
@ -1180,3 +1218,11 @@
(= (get-relative-path "a/b/c/d/g.org" "a/b/c/e/f.org")
"../e/f.org"))
#?(:cljs
(defn select-highlight!
[blocks]
(doseq [block blocks]
(d/add-class! block "selected noselect"))))
(defn keyname [key] (str (namespace key) "/" (name key)))