Skip to content

Commit d1962c6

Browse files
committed
Merge pull request #9 from archaeron/example
Update the text below the ace component with the current text
2 parents 98e407f + d70f9de commit d1962c6

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

example/src/Main.purs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import Prelude
44

55
import Ace.Editor as Editor
66
import 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

109
import Control.Monad.Aff (Aff (), runAff)
1110
import Control.Monad.Eff (Eff ())
@@ -14,7 +13,7 @@ import Control.Monad.Eff.Exception (throwException)
1413
import Control.Plus (Plus)
1514

1615
import Data.Functor.Coproduct (Coproduct())
17-
import Data.Maybe (Maybe (..))
16+
import Data.Maybe (Maybe (..), fromMaybe)
1817

1918
import Halogen
2019
import Halogen.Util (appendToBody, onLoad)
@@ -23,7 +22,7 @@ import qualified Halogen.HTML.Indexed as H
2322
-- Effects
2423
import Control.Monad.Eff.Random (RANDOM ())
2524
import Control.Monad.Eff.Ref (REF ())
26-
import Ace.Types (ACE ())
25+
import Ace.Types (ACE (), Editor ())
2726
import Data.Date (Now ())
2827

2928

@@ -55,33 +54,37 @@ type MainEffects = HalogenEffects (random :: RANDOM, now :: Now, ref :: REF, ace
5554
type MainAff = Aff MainEffects
5655

5756
ui :: 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+
8588
main :: Eff MainEffects Unit
8689
main = runAff throwException (const (pure unit)) $ do
8790
app <- runUI ui (installedState initialState)

0 commit comments

Comments
 (0)