How do you access highlighted text in Purescript?

98 Views Asked by At

I'm creating an application in Purescript and I want to have a text box displaying some documentation, and then I want to perform some NLP tasks on the server based on the sentences highlighted by the mouse on that text.

How could I extract that text in Purescript?

1

There are 1 best solutions below

0
rnons On

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