1- module DOM .Util.TextCursor.Element
2- ( module DOM .Util.TextCursor.Element.Type
3- , module DOM .Util.TextCursor.Element.HTML
1+ module Web .Util.TextCursor.Element
2+ ( module Web .Util.TextCursor.Element.Type
3+ , module Web .Util.TextCursor.Element.HTML
44 , textCursor , setTextCursor
55 , modifyTextCursor , modifyTextCursorST
66 , focusTextCursor , focusTextCursorById
77 ) where
88
99import Prelude
10- import Data.Maybe (Maybe (Just))
1110import Data.String (length , splitAt )
1211import Data.Lens (Lens' , (.~))
13- import Control.Monad.Eff ( Eff )
14- import Control.Monad.Eff. Class (class MonadEff , liftEff )
12+ import Effect ( Effect )
13+ import Effect. Class (class MonadEffect , liftEffect )
1514import Control.Monad.State.Class (class MonadState , modify )
16- import DOM (DOM )
17- import DOM.Node.Types (ElementId )
18- import DOM.HTML.HTMLElement (focus )
19- import DOM.Util.TextCursor (TextCursor (..), content )
20- import DOM.Util.TextCursor.Element.Type
15+ import Web.HTML.HTMLElement (focus )
16+ import Web.Util.TextCursor (TextCursor (..), content )
17+ import Web.Util.TextCursor.Element.Type
2118 ( TextCursorElement (..)
22- , htmlTextCursorElementToHTMLElement
19+ , toHTMLElement
2320 , read , readEventTarget
2421 , validate , validate'
2522 , lookupAndValidate
2623 , lookupValidateAndDo
2724 )
28- import DOM .Util.TextCursor.Element.HTML
25+ import Web .Util.TextCursor.Element.HTML
2926 ( value , setValue
3027 , selectionStart , setSelectionStart
3128 , selectionEnd , setSelectionEnd
3229 , selectionDirection , setSelectionDirection
3330 )
3431
35- -- | Helper to split a `String` at a specific position without worrying about
36- -- | `Nothing`.
37- splitAtRec :: Int -> String -> { before :: String , after :: String }
38- splitAtRec i s = case splitAt i s of
39- Just split -> split
40- _ | i > 0 -> { before: s, after: " " }
41- | otherwise -> { before: " " , after: s }
42-
4332-- | Get the `TextCursor` from a `TextCursorElement`.
44- textCursor :: forall eff . TextCursorElement -> Eff ( dom :: DOM | eff ) TextCursor
33+ textCursor :: TextCursorElement -> Effect TextCursor
4534textCursor element = do
4635 val <- value element
4736 start <- selectionStart element
4837 end <- selectionEnd element
4938 direction <- selectionDirection element
50- let { before: prior, after } = splitAtRec end val
51- let { before, after: selected } = splitAtRec start prior
39+ let { before: prior, after } = splitAt end val
40+ let { before, after: selected } = splitAt start prior
5241 pure $ TextCursor
5342 { before
5443 , selected
@@ -59,7 +48,7 @@ textCursor element = do
5948-- | Set the `TextCursor` on a `TextCursorElement`. Calls `setValue`,
6049-- | `setSelectionStart`, `setSelectionEnd`, and `setSelectionDirection` to
6150-- | ensure a consistent state for the field.
62- setTextCursor :: forall eff . TextCursor -> TextCursorElement -> Eff ( dom :: DOM | eff ) Unit
51+ setTextCursor :: TextCursor -> TextCursorElement -> Effect Unit
6352setTextCursor (tc@TextCursor { before, selected, after, direction }) element = do
6453 setValue (content tc) element
6554 let start = length before
@@ -69,31 +58,39 @@ setTextCursor (tc@TextCursor { before, selected, after, direction }) element = d
6958 setSelectionDirection direction element
7059
7160-- | Modifies the `TextCursor` on an element through the given endomorphism.
72- modifyTextCursor :: forall eff . (TextCursor -> TextCursor ) -> TextCursorElement -> Eff ( dom :: DOM | eff ) Unit
61+ modifyTextCursor :: (TextCursor -> TextCursor ) -> TextCursorElement -> Effect Unit
7362modifyTextCursor f element = do
7463 tc <- f <$> textCursor element
7564 setTextCursor tc element
7665
7766-- | Modifies the `TextCursor` on an element as well as setting the result in a
7867-- | State+Eff monad via a lens. Useful for components processing input events!
79- modifyTextCursorST :: forall eff m s .
68+ modifyTextCursorST :: forall m s .
8069 MonadState s m =>
81- MonadEff ( dom :: DOM | eff ) m =>
70+ MonadEffect m =>
8271 Lens' s TextCursor ->
8372 (TextCursor -> TextCursor ) ->
84- TextCursorElement -> m Unit
73+ TextCursorElement -> m s
8574modifyTextCursorST l f element = do
86- tc <- liftEff $ f <$> textCursor element
87- liftEff $ setTextCursor tc element
75+ tc <- liftEffect $ f <$> textCursor element
76+ liftEffect $ setTextCursor tc element
8877 modify $ l .~ tc
8978
79+ modifyTextCursorST_ :: forall m s .
80+ MonadState s m =>
81+ MonadEffect m =>
82+ Lens' s TextCursor ->
83+ (TextCursor -> TextCursor ) ->
84+ TextCursorElement -> m Unit
85+ modifyTextCursorST_ l f element = void $ modifyTextCursorST l f element
86+
9087-- | Focuses an element after setting the `TextCursor`.
91- focusTextCursor :: forall eff . TextCursor -> TextCursorElement -> Eff ( dom :: DOM | eff ) Unit
88+ focusTextCursor :: TextCursor -> TextCursorElement -> Effect Unit
9289focusTextCursor tc element = do
9390 setTextCursor tc element
94- focus (htmlTextCursorElementToHTMLElement element)
91+ focus (toHTMLElement element)
9592
9693-- | Looks up an element by id to focus with a `TextCursor`.
97- focusTextCursorById :: forall eff . ElementId -> TextCursor -> Eff ( dom :: DOM | eff ) Unit
94+ focusTextCursorById :: String -> TextCursor -> Effect Unit
9895focusTextCursorById name tc = do
9996 lookupValidateAndDo name (focusTextCursor tc)
0 commit comments