@@ -4,8 +4,7 @@ import Prelude
44
55import Ace.Editor as Editor
66import Ace.EditSession as Session
7- import Ace.Halogen.Component (AceState (), AceQuery ())
8- import qualified Ace.Halogen.Component as Ace
7+ import Ace.Halogen.Component (AceState (), AceQuery (TextChanged, GetText), aceConstructor )
98
109import Control.Monad.Aff (Aff (), runAff )
1110import Control.Monad.Eff (Eff ())
@@ -14,7 +13,7 @@ import Control.Monad.Eff.Exception (throwException)
1413import Control.Plus (Plus )
1514
1615import Data.Functor.Coproduct (Coproduct ())
17- import Data.Maybe (Maybe (..))
16+ import Data.Maybe (Maybe (..), fromMaybe )
1817
1918import Halogen
2019import Halogen.Util (appendToBody , onLoad )
@@ -23,7 +22,7 @@ import qualified Halogen.HTML.Indexed as H
2322-- Effects
2423import Control.Monad.Eff.Random (RANDOM ())
2524import Control.Monad.Eff.Ref (REF ())
26- import Ace.Types (ACE ())
25+ import Ace.Types (ACE (), Editor () )
2726import Data.Date (Now ())
2827
2928
@@ -55,33 +54,37 @@ type MainEffects = HalogenEffects (random :: RANDOM, now :: Now, ref :: REF, ace
5554type MainAff = Aff MainEffects
5655
5756ui :: Component (StateP MainAff ) QueryP MainAff
58- ui = parentComponent render eval
57+ ui = parentComponent' render eval peek
5958 where
6059
6160 render :: State -> MainHtml MainAff
6261 render state =
6362 H .div_
64- [ H .slot AceSlot \_ ->
65- { component :
66- Ace .aceComponent
67- (\editor -> liftEff $ do
68- session <- Editor .getSession editor
69- Session .setMode " ace/mode/yaml" session
70- Editor .setValue state.text Nothing editor
71- pure unit
72- )
73- Nothing
74- , initialState :
75- { key : Nothing , editor : Nothing }
76- }
63+ [ H.Slot $ aceConstructor AceSlot (initEditor state) Nothing
7764 , H .div_
7865 [ H .text state.text ]
7966 ]
8067
68+ initEditor :: State -> Editor -> MainAff Unit
69+ initEditor state editor = liftEff $ do
70+ session <- Editor .getSession editor
71+ Session .setMode " ace/mode/yaml" session
72+ Editor .setValue state.text Nothing editor
73+ pure unit
74+
8175 eval :: EvalParent Query State AceState Query AceQuery MainAff AceSlot
8276 eval (UpdateText next) = do
8377 pure next
8478
79+ peek :: Peek (ChildF AceSlot AceQuery ) State AceState Query AceQuery MainAff AceSlot
80+ peek (ChildF p q) =
81+ case q of
82+ TextChanged _ -> do
83+ text <- query AceSlot $ request GetText
84+ modify (_ { text = fromMaybe " " text })
85+ _ ->
86+ pure unit
87+
8588main :: Eff MainEffects Unit
8689main = runAff throwException (const (pure unit)) $ do
8790 app <- runUI ui (installedState initialState)
0 commit comments