clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
sheluchin 2021-06-25T18:19:50.022400Z

I'm trying to figure out how to get the click position of an element node like <p>this is <i>just a</i> test</p>, where the click happens somewhere such that there are previous siblings. I want to know how many text characters there were in all the previous parts of the element. So, for example, if I click on the first t in test, the result should be 15 for this is just a . Can anyone suggest how I might do this? I'm able to use the selection API to pull siblings but not totally clear on how to iterate over all previous siblings.

sheluchin 2021-06-26T10:48:30.025600Z

That's almost convincing enough to stop using DDG over Google, but not quite 🙂

sova-soars-the-sora 2021-06-27T17:17:08.032100Z

i hear ya. biG brother sees all

sova-soars-the-sora 2021-06-25T18:20:53.022500Z

o.O without making each character its own DOM element? (fifteen little [:span]s) ... i'm not sure

sova-soars-the-sora 2021-06-25T18:22:11.022700Z

Oh, I think I misread your goal. How would you do it in JavaScript? Maybe it'd be easier to translate that reasoning.

sheluchin 2021-06-25T18:22:23.022900Z

Yeah, there could be any number of preceding spans as well as some plain text.

sheluchin 2021-06-25T18:23:39.023100Z

I think in JS I could just keep accumulating the length of the previousSibling until there are no more.

sova-soars-the-sora 2021-06-25T18:24:56.023300Z

You can use previousSibling in cljs too. Maybe this is helpful https://github.com/thheller/shadow/blob/master/src/main/shadow/dom.cljs#L533

sheluchin 2021-06-25T18:28:29.023600Z

Hmm, yeah, looks like it would be. I'll give it a try. Thanks!

sova-soars-the-sora 2021-06-25T18:35:53.023900Z

Sure! 😃

sova-soars-the-sora 2021-06-25T18:36:20.024100Z

When you figure it out let me see 😄

sheluchin 2021-06-25T18:37:19.024300Z

How did you find that fn in that lib, btw? Just a lib you're familiar with, or some fancy searching skills brought ya there?

sheluchin 2021-06-25T20:12:59.024500Z

@sova something along these lines:

{:onClick #(let [selection (. js/window getSelection)
                             srange (.getRangeAt selection 0)
                             start-offset (. srange -startOffset)
                             node (. selection -anchorNode)
                             siblings (. srange -commonAncestorContainer.parentNode.childNodes)
                             prev-sibs (vec (take (index-of node) siblings))
                             preceding-char-count (reduce
                                                    (fn [c n]
                                                      (+ c (count (or (. n -data)
                                                                      (. n -innerText)))))
                                                    0 prev-sibs)

sova-soars-the-sora 2021-06-25T21:11:55.024900Z

very nice! just some searching. I searched for "clojurescript previousSibling" and it was the first result