You can subscribe to the selectionchange
event in the initialize
handler of your halogen component.
Init -> do
doc <- H.liftEffect $ Web.window >>= Window.document
void $ H.subscribe $
ES.eventListenerEventSource (EventType "selectionchange") (Document.toEventTarget doc)
(const $ Just OnSelectionChange)
Then write an FFI function to get current selected text, something like
foreign import getSelectionString :: Effect String
-- js
const getSelectionString = () => window.getSelection().toString()
Then use getSelectionString
in the OnSelectionChange
handler.
A not exactly the same example here
https://github.com/nonbili/halogen-contenteditable-example/blob/master/src/Example/Editor.purs#L178-L180