(get-selection element full-selection?)
Returns the objects related to selection for the given element. If full-selection? is true,
it will use rangy instead of the native selection API in order to get the beginning and ending
of the selection (it is, however, much slower).
Source
(defn
get-selection
"Returns the objects related to selection for the given element. If full-selection? is true,\nit will use rangy instead of the native selection API in order to get the beginning and ending\nof the selection (it is, however, much slower)."
[element full-selection?]
{:element element,
:cursor-position
(cond
full-selection?
(let
[selection
(.getSelection js/rangy)
ranges
(.saveCharacterRanges selection element)]
(if-let
[char-range (some-> ranges (aget 0) (gobj/get "characterRange"))]
[(gobj/get char-range "start") (gobj/get char-range "end")]
[0 0]))
(= 0 (.-rangeCount (.getSelection js/window)))
[0 0]
:else
(let
[selection
(.getSelection js/window)
range
(.getRangeAt selection 0)
pre-caret-range
(doto
(.cloneRange range)
(.selectNodeContents element)
(.setEnd (.-endContainer range) (.-endOffset range)))
pos
(-> pre-caret-range .toString .-length)]
[pos pos]))})