简体   繁体   中英

How do you access highlighted text in Purescript?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM