|
| 1 | +module AceEditorHalogenHooks.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Ace (Editor) |
| 6 | +import Ace as Ace |
| 7 | +import Ace.EditSession as Session |
| 8 | +import Ace.Editor as Editor |
| 9 | +import Data.Foldable (traverse_) |
| 10 | +import Data.Maybe (Maybe(..)) |
| 11 | +import Data.Symbol (SProxy(..)) |
| 12 | +import Data.Tuple.Nested ((/\)) |
| 13 | +import Effect (Effect) |
| 14 | +import Effect.Aff.Class (class MonadAff) |
| 15 | +import Halogen (liftEffect) |
| 16 | +import Halogen as H |
| 17 | +import Halogen.Aff as HA |
| 18 | +import Halogen.HTML as HH |
| 19 | +import Halogen.HTML.Events as HE |
| 20 | +import Halogen.HTML.Properties as HP |
| 21 | +import Halogen.Hooks as Hooks |
| 22 | +import Halogen.Query.EventSource as ES |
| 23 | +import Halogen.VDom.Driver (runUI) |
| 24 | + |
| 25 | +main :: Effect Unit |
| 26 | +main = |
| 27 | + HA.runHalogenAff do |
| 28 | + body <- HA.awaitBody |
| 29 | + void $ runUI containerComponent unit body |
| 30 | + |
| 31 | +_ace = SProxy :: SProxy "ace" |
| 32 | + |
| 33 | +containerComponent |
| 34 | + :: forall unusedQuery unusedInput unusedOutput anyMonad |
| 35 | + . MonadAff anyMonad |
| 36 | + => H.Component HH.HTML unusedQuery unusedInput unusedOutput anyMonad |
| 37 | +containerComponent = Hooks.component \rec _ -> Hooks.do |
| 38 | + msg /\ msgIdx <- Hooks.useState "" |
| 39 | + Hooks.pure $ |
| 40 | + HH.div_ |
| 41 | + [ HH.h1_ |
| 42 | + [ HH.text "ace editor" ] |
| 43 | + , HH.div_ |
| 44 | + [ HH.p_ |
| 45 | + [ HH.button |
| 46 | + [ HE.onClick \_ -> Just do |
| 47 | + void $ Hooks.query rec.slotToken _ace unit $ H.tell (ChangeText "") |
| 48 | + ] |
| 49 | + [ HH.text "Clear" ] |
| 50 | + ] |
| 51 | + ] |
| 52 | + , HH.div_ |
| 53 | + [ HH.slot _ace unit aceComponent unit \(TextChanged t) -> Just do |
| 54 | + Hooks.put msgIdx t |
| 55 | + ] |
| 56 | + , HH.p_ |
| 57 | + [ HH.text ("Current text: " <> msg) ] |
| 58 | + ] |
| 59 | + |
| 60 | +data ParentAction |
| 61 | + = ClearText |
| 62 | + | HandleAceUpdate AceOutput |
| 63 | + |
| 64 | +data AceQuery a = ChangeText String a |
| 65 | + |
| 66 | +data AceOutput = TextChanged String |
| 67 | + |
| 68 | +aceElemLabel :: H.RefLabel |
| 69 | +aceElemLabel = H.RefLabel "ace" |
| 70 | + |
| 71 | +aceComponent |
| 72 | + :: forall unusedInput anyMonad |
| 73 | + . MonadAff anyMonad |
| 74 | + => H.Component HH.HTML AceQuery unusedInput AceOutput anyMonad |
| 75 | +aceComponent = Hooks.component \rec _ -> Hooks.do |
| 76 | + state /\ stateIdx <- Hooks.useState (Nothing :: Maybe Editor) |
| 77 | + Hooks.useLifecycleEffect do |
| 78 | + Hooks.getHTMLElementRef aceElemLabel >>= traverse_ \element -> do |
| 79 | + editor <- liftEffect $ Ace.editNode element Ace.ace |
| 80 | + session <- liftEffect $ Editor.getSession editor |
| 81 | + Hooks.put stateIdx $ Just editor |
| 82 | + void $ Hooks.subscribe $ ES.effectEventSource \emitter -> do |
| 83 | + Session.onChange session \_ -> ES.emit emitter do |
| 84 | + Hooks.get stateIdx >>= traverse_ \editor' -> do |
| 85 | + text <- liftEffect (Editor.getValue editor') |
| 86 | + Hooks.raise rec.outputToken $ TextChanged text |
| 87 | + pure mempty |
| 88 | + pure $ Just do |
| 89 | + Hooks.put stateIdx Nothing |
| 90 | + |
| 91 | + Hooks.useQuery rec.queryToken case _ of |
| 92 | + ChangeText text next -> do |
| 93 | + maybeEditor <- Hooks.get stateIdx |
| 94 | + case maybeEditor of |
| 95 | + Nothing -> pure unit |
| 96 | + Just editor -> do |
| 97 | + current <- liftEffect $ Editor.getValue editor |
| 98 | + when (text /= current) do |
| 99 | + void $ H.liftEffect $ Editor.setValue text Nothing editor |
| 100 | + Hooks.raise rec.outputToken $ TextChanged text |
| 101 | + pure (Just next) |
| 102 | + |
| 103 | + Hooks.pure $ |
| 104 | + HH.div [ HP.ref aceElemLabel ] [] |
0 commit comments