on develop help sign in
Add missing comment lenses and widget functions
commitcf61c5bdce81a56462dd7a4e41608559e08a527d authorTom Smalley <tfsmalley@gmail.com> dateThu 1 Nov 2018, 13:24 (8 years ago) parent4565246
2 files changed+30
reflex-dom-core/src/Reflex/Dom/Builder/Class.hs+15 reflex-dom-core/src/Reflex/Dom/Widget/Basic.hs+15
reflex-dom-core/src/Reflex/Dom/Builder/Class.hs+15
@@ -425,6 +425,7 @@ data SelectElement er d t = SelectElement
425425#ifdef USE_TEMPLATE_HASKELL
426426concat <$> mapM (uncurry makeLensesWithoutField)
427427 [ (["_textNodeConfig_setContents"], ''TextNodeConfig)
428+ , (["_commentNodeConfig_setContents"], ''CommentNodeConfig)
428429 , ([ "_inputElementConfig_setValue"
429430 , "_inputElementConfig_setChecked" ], ''InputElementConfig)
430431 , (["_rawElementConfig_modifyAttributes"], ''RawElementConfig)
@@ -441,6 +442,13 @@ textNodeConfig_setContents =
441442 setter t e = t { _textNodeConfig_setContents = Just e }
442443 in lens getter setter
443444
445+-- | This lens is technically illegal. The implementation of 'TextNodeConfig' uses a 'Maybe' under the hood for efficiency reasons. However, always interacting with 'TextNodeConfig' via lenses will always behave correctly, and if you pattern match on it, you should always treat 'Nothing' as 'never'.
446+commentNodeConfig_setContents :: Reflex t => Lens (CommentNodeConfig t) (CommentNodeConfig t) (Event t Text) (Event t Text)
447+commentNodeConfig_setContents =
448+ let getter = fromMaybe never . _commentNodeConfig_setContents
449+ setter t e = t { _commentNodeConfig_setContents = Just e }
450+ in lens getter setter
451+
444452-- | This lens is technically illegal. The implementation of 'InputElementConfig' uses a 'Maybe' under the hood for efficiency reasons. However, always interacting with 'InputElementConfig' via lenses will always behave correctly, and if you pattern match on it, you should always treat 'Nothing' as 'never'.
445453inputElementConfig_setValue :: Reflex t => Lens (InputElementConfig er t m) (InputElementConfig er t m) (Event t Text) (Event t Text)
446454inputElementConfig_setValue =
@@ -553,6 +561,7 @@ instance (MountableDomBuilder t m, PerformEvent t m, MonadFix m, MonadHold t m)
553561instance (DomBuilder t m, Monoid w, MonadHold t m, MonadFix m) => DomBuilder t (DynamicWriterT t w m) where
554562 type DomBuilderSpace (DynamicWriterT t w m) = DomBuilderSpace m
555563 textNode = liftTextNode
564+ commentNode = liftCommentNode
556565 element elementTag cfg (DynamicWriterT child) = DynamicWriterT $ do
557566 s <- get
558567 (el, (a, newS)) <- lift $ element elementTag cfg $ runStateT child s
@@ -571,6 +580,7 @@ instance (DomBuilder t m, Monoid w, MonadHold t m, MonadFix m) => DomBuilder t (
571580instance (DomBuilder t m, MonadHold t m, MonadFix m) => DomBuilder t (RequesterT t request response m) where
572581 type DomBuilderSpace (RequesterT t request response m) = DomBuilderSpace m
573582 textNode = liftTextNode
583+ commentNode = liftCommentNode
574584 element elementTag cfg (RequesterT child) = RequesterT $ do
575585 r <- ask
576586 old <- get
@@ -591,6 +601,7 @@ instance (DomBuilder t m, MonadHold t m, MonadFix m) => DomBuilder t (RequesterT
591601instance (DomBuilder t m, MonadHold t m, MonadFix m, Semigroup w) => DomBuilder t (EventWriterT t w m) where
592602 type DomBuilderSpace (EventWriterT t w m) = DomBuilderSpace m
593603 textNode = liftTextNode
604+ commentNode = liftCommentNode
594605 element elementTag cfg (EventWriterT child) = EventWriterT $ do
595606 old <- get
596607 (el, (a, new)) <- lift $ element elementTag cfg $ runStateT child old
@@ -609,6 +620,7 @@ instance (DomBuilder t m, MonadHold t m, MonadFix m, Semigroup w) => DomBuilder
609620instance (DomBuilder t m, MonadFix m, MonadHold t m, Group q, Query q, Additive q) => DomBuilder t (QueryT t q m) where
610621 type DomBuilderSpace (QueryT t q m) = DomBuilderSpace m
611622 textNode = liftTextNode
623+ commentNode = liftCommentNode
612624 element elementTag cfg (QueryT child) = QueryT $ do
613625 s <- get
614626 (e, (a, newS)) <- lift $ element elementTag cfg $ runStateT child s
@@ -677,6 +689,9 @@ liftWithStateless a = liftWith $ \run -> a $ fmap (fromStT (Proxy :: Proxy t)) .
677689liftTextNode :: (MonadTrans f, DomBuilder t m) => TextNodeConfig t -> f m (TextNode (DomBuilderSpace m) t)
678690liftTextNode = lift . textNode
679691
692+liftCommentNode :: (MonadTrans f, DomBuilder t m) => CommentNodeConfig t -> f m (CommentNode (DomBuilderSpace m) t)
693+liftCommentNode = lift . commentNode
694+
680695liftElement :: LiftDomBuilder t f m => Text -> ElementConfig er t (DomBuilderSpace m) -> f m a -> f m (Element er (DomBuilderSpace m) t, a)
681696liftElement elementTag cfg child = liftWithStateless $ \run -> element elementTag cfg $ run child
682697
reflex-dom-core/src/Reflex/Dom/Widget/Basic.hs+15
@@ -16,6 +16,8 @@ module Reflex.Dom.Widget.Basic
1616 -- * Displaying Values
1717 , text
1818 , dynText
19+ , comment
20+ , dynComment
1921 , display
2022 , button
2123 , dyn
@@ -120,6 +122,19 @@ dynText t = do
120122 ]
121123 notReadyUntil postBuild
122124
125+comment :: DomBuilder t m => Text -> m ()
126+comment t = void $ commentNode $ def & commentNodeConfig_initialContents .~ t
127+
128+{-# INLINABLE dynComment #-}
129+dynComment :: forall t m. (PostBuild t m, DomBuilder t m) => Dynamic t Text -> m ()
130+dynComment t = do
131+ postBuild <- getPostBuild
132+ void $ commentNode $ (def :: CommentNodeConfig t) & commentNodeConfig_setContents .~ leftmost
133+ [ updated t
134+ , tag (current t) postBuild
135+ ]
136+ notReadyUntil postBuild
137+
123138display :: (PostBuild t m, DomBuilder t m, Show a) => Dynamic t a -> m ()
124139display = dynText . fmap (T.pack . show)
125140