Created
July 5, 2018 00:12
-
-
Save khanage/848b3503c5391b8fd19e21f81438e676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.setHTML = function(el) { | |
return function (htmlElement) { | |
return function() { | |
el.innerHTML = ''; | |
el.appendChild(htmlElement); | |
}; | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Halogen.Component.Raw where | |
import Prelude | |
import Control.Monad.Aff (Aff) | |
import Control.Monad.Eff (Eff, kind Effect) | |
import Control.Monad.Eff.Class (liftEff) | |
import DOM.HTML.Types (HTMLElement) | |
import Data.Maybe (Maybe(..)) | |
import Halogen as H | |
import Halogen.HTML as HH | |
import Halogen.HTML.Properties as HP | |
foreign import data RAWHTML :: Effect | |
foreign import setHTML :: forall e. HTMLElement -> HTMLElement -> Eff (rawhtml :: RAWHTML | e) Unit | |
type RawState = | |
{ elRef :: String | |
, html :: HTMLElement | |
} | |
data RawQuery a | |
= Initialize a | |
type RawOutput = Void | |
type RawMonad e = (Aff (rawhtml :: RAWHTML | e)) | |
type RawDSL e = H.ComponentDSL RawState RawQuery RawOutput (RawMonad e) | |
rawComponent :: forall e. H.Component HH.HTML RawQuery RawState RawOutput (RawMonad e) | |
rawComponent = | |
H.lifecycleComponent | |
{ render | |
, eval | |
, initialState : id | |
, initializer : Just (H.action Initialize) | |
, finalizer : Nothing | |
, receiver: const Nothing | |
} | |
where | |
render :: RawState -> H.ComponentHTML RawQuery | |
render s = HH.div [ HP.ref (H.RefLabel s.elRef) ] [] | |
eval :: RawQuery ~> RawDSL e | |
eval = case _ of | |
Initialize next -> do | |
updateHTML | |
pure next | |
updateHTML :: RawDSL e Unit | |
updateHTML = do | |
elRef <- H.gets _.elRef | |
H.getHTMLElementRef (H.RefLabel elRef) >>= case _ of | |
Nothing -> pure unit | |
Just el -> do | |
html <- H.gets _.html | |
liftEff $ setHTML el html | |
pure unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment