0

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?

tonicebrian
  • 4,715
  • 5
  • 41
  • 65

1 Answers1

0

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

rnons
  • 363
  • 2
  • 10