fix: caret-range should include newlines

pull/3954/head
Tienson Qin 2022-01-20 12:44:40 +08:00
parent e6531f404f
commit 723a202faa
1 changed files with 25 additions and 6 deletions

View File

@ -337,6 +337,16 @@
(apply f args))
threshold)))))))
(defn nth-safe [c i]
(if (or (< i 0) (>= i (count c)))
nil
(nth c i)))
#?(:cljs
(extend-type js/NodeList
ISeqable
(-seq [array] (array-seq array 0))))
;; Caret
#?(:cljs
(defn caret-range [node]
@ -355,7 +365,21 @@
(.setEnd pre-caret-range
(gobj/get range "endContainer")
(gobj/get range "endOffset"))
(.toString pre-caret-range))))
(let [contents (.cloneContents pre-caret-range)
html (some-> (first (.-childNodes contents))
(gobj/get "innerHTML")
str)
;; FIXME: this depends on the dom structure,
;; need a converter from html to text includes newlines
br-ended? (or
;; first line with a new line
(string/ends-with? html "<div class=\"is-paragraph\"></div></div></span></div></div></div>")
;; multiple lines with a new line
(string/ends-with? html "<br></div></div></span></div></div></div>"))
value (.toString pre-caret-range)]
(if br-ended?
(str value "\n")
value)))))
(when-let [selection (gobj/get doc "selection")]
(when (not= "Control" (gobj/get selection "type"))
(let [text-range (.createRange selection)
@ -997,11 +1021,6 @@
(when section
(gdom/getElement section "id"))))))
(defn nth-safe [c i]
(if (or (< i 0) (>= i (count c)))
nil
(nth c i)))
#?(:cljs
(defn get-prev-block-non-collapsed
[block]