on develop help sign in
Add ability to update textNode from an Event reusing same node.
0566171Luigy Leon
src/Reflex/Dom/Builder/Class.hs+5−3
@@ -64,13 +64,15 @@ class (Monad m, Reflex t, Deletable t m, DomSpace (DomBuilderSpace m)) => DomBui
64type Namespace = Text
64type Namespace = Text
65
65
66data TextNodeConfig t
66data TextNodeConfig t
67− = TextNodeConfig { _textNodeConfig_contents :: Text
67+ = TextNodeConfig { _textNodeConfig_initialContents :: Text
68+ , _textNodeConfig_setContents :: Event t Text
68 }
69 }
69
70
70−instance Default (TextNodeConfig t) where
71+instance (Reflex t) => Default (TextNodeConfig t) where
71 {-# INLINABLE def #-}
72 {-# INLINABLE def #-}
72 def = TextNodeConfig
73 def = TextNodeConfig
73− { _textNodeConfig_contents = mempty
74+ { _textNodeConfig_initialContents = mempty
75+ , _textNodeConfig_setContents = never
74 }
76 }
75
77
76data TextNode t = TextNode
78data TextNode t = TextNode
src/Reflex/Dom/Builder/Immediate.hs+4−3
@@ -47,7 +47,7 @@ import qualified GHCJS.DOM.HTMLInputElement as Input
47import qualified GHCJS.DOM.HTMLTextAreaElement as TextArea
47import qualified GHCJS.DOM.HTMLTextAreaElement as TextArea
48import GHCJS.DOM.MouseEvent
48import GHCJS.DOM.MouseEvent
49import GHCJS.DOM.Node (appendChild, getOwnerDocument, getParentNode, getPreviousSibling, insertBefore,
49import GHCJS.DOM.Node (appendChild, getOwnerDocument, getParentNode, getPreviousSibling, insertBefore,
50− removeChild, toNode)
50+ removeChild, toNode, setNodeValue)
51import GHCJS.DOM.Types (FocusEvent, IsElement, IsEvent, IsNode, KeyboardEvent, Node, ToDOMString, TouchEvent,
51import GHCJS.DOM.Types (FocusEvent, IsElement, IsEvent, IsNode, KeyboardEvent, Node, ToDOMString, TouchEvent,
52 WheelEvent, castToHTMLInputElement, castToHTMLTextAreaElement)
52 WheelEvent, castToHTMLInputElement, castToHTMLTextAreaElement)
53import qualified GHCJS.DOM.Types as DOM
53import qualified GHCJS.DOM.Types as DOM
@@ -202,8 +202,9 @@ instance DomSpace GhcjsDomSpace where
202instance SupportsImmediateDomBuilder t m => DomBuilder t (ImmediateDomBuilderT t m) where
202instance SupportsImmediateDomBuilder t m => DomBuilder t (ImmediateDomBuilderT t m) where
203 type DomBuilderSpace (ImmediateDomBuilderT t m) = GhcjsDomSpace
203 type DomBuilderSpace (ImmediateDomBuilderT t m) = GhcjsDomSpace
204 {-# INLINABLE textNode #-}
204 {-# INLINABLE textNode #-}
205− textNode (TextNodeConfig contents) = do
205+ textNode (TextNodeConfig initialContents eSetContents) = do
206− _ <- textNodeInternal contents
206+ n <- textNodeInternal initialContents
207+ lift $ performEvent_ $ ffor eSetContents $ \t -> setNodeValue n (Just t)
207 return TextNode
208 return TextNode
208 {-# INLINABLE element #-}
209 {-# INLINABLE element #-}
209 element elementTag cfg child = fst <$> makeElement elementTag cfg child
210 element elementTag cfg child = fst <$> makeElement elementTag cfg child
src/Reflex/Dom/Builder/Static.hs+3−2
@@ -119,8 +119,9 @@ instance DomSpace StaticDomSpace where
119instance SupportsStaticDomBuilder t m => DomBuilder t (StaticDomBuilderT t m) where
119instance SupportsStaticDomBuilder t m => DomBuilder t (StaticDomBuilderT t m) where
120 type DomBuilderSpace (StaticDomBuilderT t m) = StaticDomSpace
120 type DomBuilderSpace (StaticDomBuilderT t m) = StaticDomSpace
121 {-# INLINABLE textNode #-}
121 {-# INLINABLE textNode #-}
122− textNode (TextNodeConfig contents) = StaticDomBuilderT $ do
122+ textNode (TextNodeConfig initialContents setContents) = StaticDomBuilderT $ do
123− modify $ (:) $ constant $ BL.toStrict $ toLazyByteString $ fromHtmlEscapedText contents
123+ let escape = BL.toStrict . toLazyByteString . fromHtmlEscapedText
124+ modify . (:) <=< hold (escape initialContents) $ fmap escape setContents
124 return TextNode
125 return TextNode
125
126
126 {-# INLINABLE element #-}
127 {-# INLINABLE element #-}
src/Reflex/Dom/Widget/Basic.hs+3−4
@@ -85,16 +85,15 @@ listHoldWithKey initialChildren modifyChildren buildChild = do
85 return $ fmap (fmap (fst . unChildResult)) augmentedResults
85 return $ fmap (fmap (fst . unChildResult)) augmentedResults
86
86
87text :: DomBuilder t m => Text -> m ()
87text :: DomBuilder t m => Text -> m ()
88−text t = void $ textNode $ def & textNodeConfig_contents .~ t
88+text t = void $ textNode $ def & textNodeConfig_initialContents .~ t
89
89
90−dynText :: (PostBuild t m, DomBuilder t m) => Dynamic t Text -> m ()
90+dynText :: forall t m. (PostBuild t m, DomBuilder t m) => Dynamic t Text -> m ()
91dynText t = do
91dynText t = do
92 postBuild <- getPostBuild
92 postBuild <- getPostBuild
93− _ <- widgetHoldInternal (return ()) $ text <$> leftmost
93+ void $ textNode $ (def :: TextNodeConfig t) & textNodeConfig_setContents .~ leftmost
94 [ updated t
94 [ updated t
95 , tag (current t) postBuild
95 , tag (current t) postBuild
96 ]
96 ]
97− return ()
98
97
99display :: (PostBuild t m, DomBuilder t m, Show a) => Dynamic t a -> m ()
98display :: (PostBuild t m, DomBuilder t m, Show a) => Dynamic t a -> m ()
100display = dynText . fmap (T.pack . show)
99display = dynText . fmap (T.pack . show)