(refresh-content-element! cropped-state)
Replaces a single node in the content, and siblings if necessary.
Source
(defn
refresh-content-element!
"Replaces a single node in the content, and siblings if necessary."
[cropped-state]
(let
[{:keys [element text]}
cropped-state
parent
(.-parentElement element)
last-elem
(.-lastChild parent)
last-error
(loop
[current-elem last-elem]
(cond
(dom/error-node? current-elem)
current-elem
(or (nil? current-elem) (= element current-elem))
nil
:else
(recur (.-previousSibling current-elem))))
last-elem-to-refresh
(when
last-error
(loop
[current-elem last-error]
(if-let
[sibling (.-nextSibling current-elem)]
(if (dom/coll-node? sibling) current-elem (recur sibling))
current-elem)))
old-elems
(loop
[elems [element] current-elem element]
(cond
(= last-elem-to-refresh current-elem)
elems
(or (some? last-elem-to-refresh) (dom/text-node? current-elem))
(if-let
[sibling (.-nextSibling current-elem)]
(recur (conj elems sibling) sibling)
elems)
:else
elems))
_
(set! (.-textContent element) text)
text
(join (map (fn* [p1__18518#] (.-textContent p1__18518#)) old-elems))
temp-elem
(.createElement js/document "span")
_
(set! (.-innerHTML temp-elem) (hs/code->html text))
new-elems
(doall
(for
[i (range (-> temp-elem .-childNodes .-length))]
(-> temp-elem .-childNodes (.item i))))]
(doseq [new-elem new-elems] (.insertBefore parent new-elem element))
(doseq [old-elem old-elems] (.removeChild parent old-elem))
(assoc cropped-state :element (first new-elems))))