{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Miso.Types
(
App
, Component (..)
, ComponentId
, SomeComponent (..)
, View (..)
, Key (..)
, Attribute (..)
, Namespace (..)
, CSS (..)
, JS (..)
, LogLevel (..)
, VTree (..)
, VTreeType (..)
, Tag
, CacheBust
, MountPoint
, DOMRef
, Events
, Phase (..)
, URI (..)
, ToKey (..)
, emptyURI
, component
, vcomp
, (+>)
, mount_
, mountUseContext
, mountWithProps_
, mountWithProps
, keyed
, fragment
, fragment_
, vfrag
, vfrag_
, getMountPoint
, optionalAttrs
, optionalVoidAttrs
, optionalChildren
, prettyURI
, prettyQueryString
, node
, vnode
, text
, vtext
, text_
, textRaw
, textKey
, textKey_
, htmlEncode
, MisoString
, toMisoString
, fromMisoString
, ms
) where
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe, isJust)
import Data.String (IsString, fromString)
import qualified Data.Text as T
import GHC.Generics
import Prelude
import Miso.DSL
import Miso.Effect (Effect, Sub, Sink, DOMRef, ComponentId)
import Miso.Event.Types
import Miso.JSON (Value, ToJSON(..), encode)
import qualified Miso.String as MS
import Miso.String (ToMisoString, MisoString, toMisoString, ms, fromMisoString)
import Miso.CSS.Types (StyleSheet)
data Component context props model action
= Component
{ forall context props model action.
Component context props model action -> model
model :: model
, forall context props model action.
Component context props model action -> Maybe (IO model)
hydrateModel :: Maybe (IO model)
, forall context props model action.
Component context props model action
-> action -> Effect context props model action
update :: action -> Effect context props model action
, forall context props model action.
Component context props model action
-> context -> props -> model -> View context action
view :: context -> props -> model -> View context action
, forall context props model action.
Component context props model action -> Bool
useContext :: Bool
, forall context props model action.
Component context props model action -> [Sub action]
subs :: [ Sub action ]
, forall context props model action.
Component context props model action -> [CSS]
styles :: [CSS]
, forall context props model action.
Component context props model action -> [JS]
scripts :: [JS]
, forall context props model action.
Component context props model action -> Maybe MisoString
mountPoint :: Maybe MountPoint
, forall context props model action.
Component context props model action -> LogLevel
logLevel :: LogLevel
, forall context props model action.
Component context props model action -> Value -> Maybe action
mailbox :: Value -> Maybe action
, forall context props model action.
Component context props model action -> Bool
eventPropagation :: Bool
, forall context props model action.
Component context props model action -> Maybe action
mount :: Maybe action
, forall context props model action.
Component context props model action -> Maybe action
unmount :: Maybe action
, forall context props model action.
Component context props model action
-> Maybe (props -> props -> action)
onPropsChanged :: Maybe (props -> props -> action)
}
type MountPoint = MisoString
data CSS
= Href MisoString CacheBust
| Style MisoString
| Sheet StyleSheet
deriving (Int -> CSS -> ShowS
[CSS] -> ShowS
CSS -> String
(Int -> CSS -> ShowS)
-> (CSS -> String) -> ([CSS] -> ShowS) -> Show CSS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CSS -> ShowS
showsPrec :: Int -> CSS -> ShowS
$cshow :: CSS -> String
show :: CSS -> String
$cshowList :: [CSS] -> ShowS
showList :: [CSS] -> ShowS
Show, CSS -> CSS -> Bool
(CSS -> CSS -> Bool) -> (CSS -> CSS -> Bool) -> Eq CSS
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CSS -> CSS -> Bool
== :: CSS -> CSS -> Bool
$c/= :: CSS -> CSS -> Bool
/= :: CSS -> CSS -> Bool
Eq)
type CacheBust = Bool
data JS
= Src MisoString CacheBust
| Script MisoString
| Module MisoString
| ImportMap [(MisoString,MisoString)]
deriving (Int -> JS -> ShowS
[JS] -> ShowS
JS -> String
(Int -> JS -> ShowS)
-> (JS -> String) -> ([JS] -> ShowS) -> Show JS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JS -> ShowS
showsPrec :: Int -> JS -> ShowS
$cshow :: JS -> String
show :: JS -> String
$cshowList :: [JS] -> ShowS
showList :: [JS] -> ShowS
Show, JS -> JS -> Bool
(JS -> JS -> Bool) -> (JS -> JS -> Bool) -> Eq JS
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JS -> JS -> Bool
== :: JS -> JS -> Bool
$c/= :: JS -> JS -> Bool
/= :: JS -> JS -> Bool
Eq)
getMountPoint :: Maybe MisoString -> MisoString
getMountPoint :: Maybe MisoString -> MisoString
getMountPoint = MisoString -> Maybe MisoString -> MisoString
forall a. a -> Maybe a -> a
fromMaybe MisoString
"body"
component
:: model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
component :: forall model action context props.
model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
component model
m action -> Effect context props model action
u context -> props -> model -> View context action
v = Component
{ model :: model
model = model
m
, hydrateModel :: Maybe (IO model)
hydrateModel = Maybe (IO model)
forall a. Maybe a
Nothing
, update :: action -> Effect context props model action
update = action -> Effect context props model action
u
, view :: context -> props -> model -> View context action
view = context -> props -> model -> View context action
v
, useContext :: Bool
useContext = Bool
False
, subs :: [Sub action]
subs = []
, styles :: [CSS]
styles = []
, scripts :: [JS]
scripts = []
, mountPoint :: Maybe MisoString
mountPoint = Maybe MisoString
forall a. Maybe a
Nothing
, logLevel :: LogLevel
logLevel = LogLevel
Off
, mailbox :: Value -> Maybe action
mailbox = Maybe action -> Value -> Maybe action
forall a b. a -> b -> a
const Maybe action
forall a. Maybe a
Nothing
, eventPropagation :: Bool
eventPropagation = Bool
False
, mount :: Maybe action
mount = Maybe action
forall a. Maybe a
Nothing
, unmount :: Maybe action
unmount = Maybe action
forall a. Maybe a
Nothing
, onPropsChanged :: Maybe (props -> props -> action)
onPropsChanged = Maybe (props -> props -> action)
forall a. Maybe a
Nothing
}
vcomp
:: model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
vcomp :: forall model action context props.
model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
vcomp = model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
forall model action context props.
model
-> (action -> Effect context props model action)
-> (context -> props -> model -> View context action)
-> Component context props model action
component
type App model action = Component () () model action
data LogLevel
= Off
| DebugHydrate
| DebugEvents
| DebugAll
deriving (Int -> LogLevel -> ShowS
[LogLevel] -> ShowS
LogLevel -> String
(Int -> LogLevel -> ShowS)
-> (LogLevel -> String) -> ([LogLevel] -> ShowS) -> Show LogLevel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LogLevel -> ShowS
showsPrec :: Int -> LogLevel -> ShowS
$cshow :: LogLevel -> String
show :: LogLevel -> String
$cshowList :: [LogLevel] -> ShowS
showList :: [LogLevel] -> ShowS
Show, LogLevel -> LogLevel -> Bool
(LogLevel -> LogLevel -> Bool)
-> (LogLevel -> LogLevel -> Bool) -> Eq LogLevel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LogLevel -> LogLevel -> Bool
== :: LogLevel -> LogLevel -> Bool
$c/= :: LogLevel -> LogLevel -> Bool
/= :: LogLevel -> LogLevel -> Bool
Eq)
type Tag = MisoString
data View context action
= VNode Namespace Tag [Attribute action] [View context action]
| VText (Maybe Key) MisoString
| VComp (Maybe Key) (SomeComponent context)
| VFrag (Maybe Key) [View context action]
deriving (forall a b. (a -> b) -> View context a -> View context b)
-> (forall a b. a -> View context b -> View context a)
-> Functor (View context)
forall a b. a -> View context b -> View context a
forall a b. (a -> b) -> View context a -> View context b
forall context a b. a -> View context b -> View context a
forall context a b. (a -> b) -> View context a -> View context b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall context a b. (a -> b) -> View context a -> View context b
fmap :: forall a b. (a -> b) -> View context a -> View context b
$c<$ :: forall context a b. a -> View context b -> View context a
<$ :: forall a b. a -> View context b -> View context a
Functor
data SomeComponent context
= forall model action props . (Eq context, Eq model, Eq props)
=> SomeComponent props (Component context props model action)
keyed
:: MisoString
-> View context action
-> View context action
keyed :: forall context action.
MisoString -> View context action -> View context action
keyed MisoString
key = \case
VText Maybe Key
_ MisoString
txt ->
Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key)) MisoString
txt
VComp Maybe Key
_ SomeComponent context
comp ->
Maybe Key -> SomeComponent context -> View context action
forall context action.
Maybe Key -> SomeComponent context -> View context action
VComp (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key)) SomeComponent context
comp
VFrag Maybe Key
_ [View context action]
kids ->
Maybe Key -> [View context action] -> View context action
forall context action.
Maybe Key -> [View context action] -> View context action
VFrag (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key)) [View context action]
kids
VNode Namespace
ns MisoString
tag [Attribute action]
attrs [View context action]
kids ->
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall context action.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
VNode Namespace
ns MisoString
tag (MisoString -> Value -> Attribute action
forall action. MisoString -> Value -> Attribute action
Property MisoString
"key" (MisoString -> Value
forall a. ToJSON a => a -> Value
toJSON MisoString
key) Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: [Attribute action]
attrs) [View context action]
kids
vfrag :: [View context action] -> View context action
vfrag :: forall context action. [View context action] -> View context action
vfrag = [View context action] -> View context action
forall context action. [View context action] -> View context action
fragment
fragment :: [View context action] -> View context action
fragment :: forall context action. [View context action] -> View context action
fragment = Maybe Key -> [View context action] -> View context action
forall context action.
Maybe Key -> [View context action] -> View context action
VFrag Maybe Key
forall a. Maybe a
Nothing
vfrag_ :: MisoString -> [View context action] -> View context action
vfrag_ :: forall context action.
MisoString -> [View context action] -> View context action
vfrag_ MisoString
key = Maybe Key -> [View context action] -> View context action
forall context action.
Maybe Key -> [View context action] -> View context action
VFrag (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key))
fragment_ :: MisoString -> [View context action] -> View context action
fragment_ :: forall context action.
MisoString -> [View context action] -> View context action
fragment_ MisoString
key = Maybe Key -> [View context action] -> View context action
forall context action.
Maybe Key -> [View context action] -> View context action
VFrag (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key))
(+>)
:: forall context model action a . (Eq context, Eq model)
=> MisoString
-> Component context () model action
-> View context a
infixr 0 +>
MisoString
key +> :: forall context model action a.
(Eq context, Eq model) =>
MisoString -> Component context () model action -> View context a
+> Component context () model action
comp = Maybe Key -> SomeComponent context -> View context a
forall context action.
Maybe Key -> SomeComponent context -> View context action
VComp (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
forall key. ToKey key => key -> Key
toKey MisoString
key)) (() -> Component context () model action -> SomeComponent context
forall context model action props.
(Eq context, Eq model, Eq props) =>
props
-> Component context props model action -> SomeComponent context
SomeComponent () Component context () model action
comp)
mountWithProps
:: (Eq context, Eq model, Eq props)
=> props
-> Component context props model action
-> View context a
mountWithProps :: forall context model props action a.
(Eq context, Eq model, Eq props) =>
props -> Component context props model action -> View context a
mountWithProps props
props Component context props model action
comp = Maybe Key -> SomeComponent context -> View context a
forall context action.
Maybe Key -> SomeComponent context -> View context action
VComp Maybe Key
forall a. Maybe a
Nothing (props
-> Component context props model action -> SomeComponent context
forall context model action props.
(Eq context, Eq model, Eq props) =>
props
-> Component context props model action -> SomeComponent context
SomeComponent props
props Component context props model action
comp)
mountWithProps_
:: (Eq context, Eq model, Eq props)
=> MisoString
-> props
-> Component context props model action
-> View context a
mountWithProps_ :: forall context model props action a.
(Eq context, Eq model, Eq props) =>
MisoString
-> props -> Component context props model action -> View context a
mountWithProps_ MisoString
key props
props Component context props model action
comp = Maybe Key -> SomeComponent context -> View context a
forall context action.
Maybe Key -> SomeComponent context -> View context action
VComp (Key -> Maybe Key
forall a. a -> Maybe a
Just (MisoString -> Key
Key MisoString
key)) (props
-> Component context props model action -> SomeComponent context
forall context model action props.
(Eq context, Eq model, Eq props) =>
props
-> Component context props model action -> SomeComponent context
SomeComponent props
props Component context props model action
comp)
mount_
:: (Eq context, Eq model)
=> Component context () model action
-> View context a
mount_ :: forall context model action a.
(Eq context, Eq model) =>
Component context () model action -> View context a
mount_ Component context () model action
comp = Maybe Key -> SomeComponent context -> View context a
forall context action.
Maybe Key -> SomeComponent context -> View context action
VComp Maybe Key
forall a. Maybe a
Nothing (() -> Component context () model action -> SomeComponent context
forall context model action props.
(Eq context, Eq model, Eq props) =>
props
-> Component context props model action -> SomeComponent context
SomeComponent () Component context () model action
comp)
mountUseContext
:: (Eq context, Eq model)
=> Component context () model action
-> View context a
mountUseContext :: forall context model action a.
(Eq context, Eq model) =>
Component context () model action -> View context a
mountUseContext Component context () model action
comp = Component context () model action -> View context a
forall context model action a.
(Eq context, Eq model) =>
Component context () model action -> View context a
mount_ Component context () model action
comp { useContext = True }
data Namespace
= HTML
| SVG
| MATHML
deriving (Int -> Namespace -> ShowS
[Namespace] -> ShowS
Namespace -> String
(Int -> Namespace -> ShowS)
-> (Namespace -> String)
-> ([Namespace] -> ShowS)
-> Show Namespace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Namespace -> ShowS
showsPrec :: Int -> Namespace -> ShowS
$cshow :: Namespace -> String
show :: Namespace -> String
$cshowList :: [Namespace] -> ShowS
showList :: [Namespace] -> ShowS
Show, Namespace -> Namespace -> Bool
(Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool) -> Eq Namespace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Namespace -> Namespace -> Bool
== :: Namespace -> Namespace -> Bool
$c/= :: Namespace -> Namespace -> Bool
/= :: Namespace -> Namespace -> Bool
Eq)
instance ToJSVal Namespace where
toJSVal :: Namespace -> IO JSVal
toJSVal = \case
Namespace
SVG -> MisoString -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (MisoString
"svg" :: MisoString)
Namespace
HTML -> MisoString -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (MisoString
"html" :: MisoString)
Namespace
MATHML -> MisoString -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (MisoString
"mathml" :: MisoString)
newtype Key = Key MisoString
deriving newtype (Int -> Key -> ShowS
[Key] -> ShowS
Key -> String
(Int -> Key -> ShowS)
-> (Key -> String) -> ([Key] -> ShowS) -> Show Key
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Key -> ShowS
showsPrec :: Int -> Key -> ShowS
$cshow :: Key -> String
show :: Key -> String
$cshowList :: [Key] -> ShowS
showList :: [Key] -> ShowS
Show, Key -> Key -> Bool
(Key -> Key -> Bool) -> (Key -> Key -> Bool) -> Eq Key
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Key -> Key -> Bool
== :: Key -> Key -> Bool
$c/= :: Key -> Key -> Bool
/= :: Key -> Key -> Bool
Eq, String -> Key
(String -> Key) -> IsString Key
forall a. (String -> a) -> IsString a
$cfromString :: String -> Key
fromString :: String -> Key
IsString, [Key] -> Value
Key -> Value
(Key -> Value) -> ([Key] -> Value) -> ToJSON Key
forall a. (a -> Value) -> ([a] -> Value) -> ToJSON a
$ctoJSON :: Key -> Value
toJSON :: Key -> Value
$ctoJSONList :: [Key] -> Value
toJSONList :: [Key] -> Value
ToJSON, Key -> MisoString
(Key -> MisoString) -> ToMisoString Key
forall str. (str -> MisoString) -> ToMisoString str
$ctoMisoString :: Key -> MisoString
toMisoString :: Key -> MisoString
ToMisoString)
instance ToJSVal Key where
toJSVal :: Key -> IO JSVal
toJSVal (Key MisoString
x) = MisoString -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal MisoString
x
class ToKey key where
toKey :: key -> Key
instance ToKey Key where toKey :: Key -> Key
toKey = Key -> Key
forall a. a -> a
id
#ifndef VANILLA
instance ToKey MisoString where toKey = Key
#endif
instance ToKey T.Text where toKey :: MisoString -> Key
toKey = MisoString -> Key
Key (MisoString -> Key)
-> (MisoString -> MisoString) -> MisoString -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MisoString -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
instance ToKey String where toKey :: String -> Key
toKey = MisoString -> Key
Key (MisoString -> Key) -> (String -> MisoString) -> String -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
instance ToKey Int where toKey :: Int -> Key
toKey = MisoString -> Key
Key (MisoString -> Key) -> (Int -> MisoString) -> Int -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
instance ToKey Double where toKey :: Double -> Key
toKey = MisoString -> Key
Key (MisoString -> Key) -> (Double -> MisoString) -> Double -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
instance ToKey Float where toKey :: Float -> Key
toKey = MisoString -> Key
Key (MisoString -> Key) -> (Float -> MisoString) -> Float -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
instance ToKey Word where toKey :: Word -> Key
toKey = MisoString -> Key
Key (MisoString -> Key) -> (Word -> MisoString) -> Word -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
data Attribute action
= Property MisoString Value
| ClassList [MisoString]
| On (Sink action -> VTree -> LogLevel -> Events -> IO ())
| Styles (M.Map MisoString MisoString)
deriving (forall a b. (a -> b) -> Attribute a -> Attribute b)
-> (forall a b. a -> Attribute b -> Attribute a)
-> Functor Attribute
forall a b. a -> Attribute b -> Attribute a
forall a b. (a -> b) -> Attribute a -> Attribute b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Attribute a -> Attribute b
fmap :: forall a b. (a -> b) -> Attribute a -> Attribute b
$c<$ :: forall a b. a -> Attribute b -> Attribute a
<$ :: forall a b. a -> Attribute b -> Attribute a
Functor
instance Eq (Attribute action) where
Property MisoString
k1 Value
v1 == :: Attribute action -> Attribute action -> Bool
== Property MisoString
k2 Value
v2 = MisoString
k1 MisoString -> MisoString -> Bool
forall a. Eq a => a -> a -> Bool
== MisoString
k2 Bool -> Bool -> Bool
&& Value
v1 Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
== Value
v2
ClassList [MisoString]
x == ClassList [MisoString]
y = [MisoString]
x [MisoString] -> [MisoString] -> Bool
forall a. Eq a => a -> a -> Bool
== [MisoString]
y
Styles Map MisoString MisoString
x == Styles Map MisoString MisoString
y = Map MisoString MisoString
x Map MisoString MisoString -> Map MisoString MisoString -> Bool
forall a. Eq a => a -> a -> Bool
== Map MisoString MisoString
y
Attribute action
_ == Attribute action
_ = Bool
False
instance Show (Attribute action) where
show :: Attribute action -> String
show = \case
Property MisoString
key Value
value ->
MisoString -> String
MS.unpack MisoString
key String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> MisoString -> String
MS.unpack (MisoString -> MisoString
forall str. ToMisoString str => str -> MisoString
ms (Value -> MisoString
forall a. ToJSON a => a -> MisoString
encode Value
value))
ClassList [MisoString]
classes ->
MisoString -> String
MS.unpack (MisoString -> [MisoString] -> MisoString
MS.intercalate MisoString
" " [MisoString]
classes)
On Sink action -> VTree -> LogLevel -> Events -> IO ()
_ ->
String
"<event-handler>"
Styles Map MisoString MisoString
styles ->
MisoString -> String
MS.unpack (MisoString -> String) -> MisoString -> String
forall a b. (a -> b) -> a -> b
$ [MisoString] -> MisoString
MS.concat
[ MisoString
k MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
"=" MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
v MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
";"
| (MisoString
k, MisoString
v) <- Map MisoString MisoString -> [(MisoString, MisoString)]
forall k a. Map k a -> [(k, a)]
M.toList Map MisoString MisoString
styles
]
instance IsString (View context action) where
fromString :: String -> View context action
fromString = Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText Maybe Key
forall a. Maybe a
Nothing (MisoString -> View context action)
-> (String -> MisoString) -> String -> View context action
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> MisoString
forall a. IsString a => String -> a
fromString
newtype VTree = VTree
{ VTree -> Object
getTree :: Object
} deriving newtype (VTree -> IO Object
(VTree -> IO Object) -> ToObject VTree
forall a. (a -> IO Object) -> ToObject a
$ctoObject :: VTree -> IO Object
toObject :: VTree -> IO Object
ToObject, VTree -> IO JSVal
(VTree -> IO JSVal) -> ToJSVal VTree
forall a. (a -> IO JSVal) -> ToJSVal a
$ctoJSVal :: VTree -> IO JSVal
toJSVal :: VTree -> IO JSVal
ToJSVal)
node
:: Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
node :: forall action context.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
node = Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall context action.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
VNode
vnode
:: Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
vnode :: forall action context.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
vnode = Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall action context.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
node
text :: MisoString -> View context action
#ifdef SSR
text = VText Nothing . htmlEncode
#else
text :: forall context action. MisoString -> View context action
text = Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText Maybe Key
forall a. Maybe a
Nothing
#endif
vtext :: MisoString -> View context action
vtext :: forall context action. MisoString -> View context action
vtext = MisoString -> View context action
forall context action. MisoString -> View context action
text
textRaw :: MisoString -> View context action
= Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText Maybe Key
forall a. Maybe a
Nothing
htmlEncode :: MisoString -> MisoString
htmlEncode :: MisoString -> MisoString
htmlEncode = (Char -> MisoString) -> MisoString -> MisoString
MS.concatMap ((Char -> MisoString) -> MisoString -> MisoString)
-> (Char -> MisoString) -> MisoString -> MisoString
forall a b. (a -> b) -> a -> b
$ \case
Char
'<' -> MisoString
"<"
Char
'>' -> MisoString
">"
Char
'&' -> MisoString
"&"
Char
'"' -> MisoString
"""
Char
'\'' -> MisoString
"'"
Char
x -> Char -> MisoString
MS.singleton Char
x
text_ :: [MisoString] -> View context action
text_ :: forall context action. [MisoString] -> View context action
text_ = Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText Maybe Key
forall a. Maybe a
Nothing (MisoString -> View context action)
-> ([MisoString] -> MisoString)
-> [MisoString]
-> View context action
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MisoString -> [MisoString] -> MisoString
MS.intercalate MisoString
" "
textKey :: ToKey key => key -> MisoString -> View context action
textKey :: forall key context action.
ToKey key =>
key -> MisoString -> View context action
textKey key
k = Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText (Key -> Maybe Key
forall a. a -> Maybe a
Just (key -> Key
forall key. ToKey key => key -> Key
toKey key
k))
textKey_ :: ToKey key => key -> [MisoString] -> View context action
textKey_ :: forall key context action.
ToKey key =>
key -> [MisoString] -> View context action
textKey_ key
k [MisoString]
xs = Maybe Key -> MisoString -> View context action
forall context action.
Maybe Key -> MisoString -> View context action
VText (Key -> Maybe Key
forall a. a -> Maybe a
Just (key -> Key
forall key. ToKey key => key -> Key
toKey key
k)) (MisoString -> [MisoString] -> MisoString
MS.intercalate MisoString
" " [MisoString]
xs)
optionalAttrs
:: ([Attribute action] -> [View context action] -> View context action)
-> [Attribute action]
-> Bool
-> [Attribute action]
-> [View context action]
-> View context action
optionalAttrs :: forall action context.
([Attribute action]
-> [View context action] -> View context action)
-> [Attribute action]
-> Bool
-> [Attribute action]
-> [View context action]
-> View context action
optionalAttrs [Attribute action] -> [View context action] -> View context action
element [Attribute action]
attrs Bool
condition [Attribute action]
opts [View context action]
kids =
case [Attribute action] -> [View context action] -> View context action
element [Attribute action]
attrs [View context action]
kids of
VNode Namespace
ns MisoString
name [Attribute action]
_ [View context action]
_ -> do
let newAttrs :: [Attribute action]
newAttrs = [[Attribute action]] -> [Attribute action]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [Attribute action]
opts | Bool
condition ] [Attribute action] -> [Attribute action] -> [Attribute action]
forall a. [a] -> [a] -> [a]
++ [Attribute action]
attrs
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall context action.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
VNode Namespace
ns MisoString
name [Attribute action]
newAttrs [View context action]
kids
View context action
x -> View context action
x
optionalVoidAttrs
:: ([Attribute action] -> View context action)
-> [Attribute action]
-> Bool
-> [Attribute action]
-> View context action
optionalVoidAttrs :: forall action context.
([Attribute action] -> View context action)
-> [Attribute action]
-> Bool
-> [Attribute action]
-> View context action
optionalVoidAttrs [Attribute action] -> View context action
element [Attribute action]
attrs Bool
condition [Attribute action]
opts =
case [Attribute action] -> View context action
element [Attribute action]
attrs of
VNode Namespace
ns MisoString
name [Attribute action]
_ [View context action]
kids -> do
let newAttrs :: [Attribute action]
newAttrs = [[Attribute action]] -> [Attribute action]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [Attribute action]
opts | Bool
condition ] [Attribute action] -> [Attribute action] -> [Attribute action]
forall a. [a] -> [a] -> [a]
++ [Attribute action]
attrs
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall context action.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
VNode Namespace
ns MisoString
name [Attribute action]
newAttrs [View context action]
kids
View context action
x -> View context action
x
optionalChildren
:: ([Attribute action] -> [View context action] -> View context action)
-> [Attribute action]
-> [View context action]
-> Bool
-> [View context action]
-> View context action
optionalChildren :: forall action context.
([Attribute action]
-> [View context action] -> View context action)
-> [Attribute action]
-> [View context action]
-> Bool
-> [View context action]
-> View context action
optionalChildren [Attribute action] -> [View context action] -> View context action
element [Attribute action]
attrs [View context action]
kids Bool
condition [View context action]
opts =
case [Attribute action] -> [View context action] -> View context action
element [Attribute action]
attrs [View context action]
kids of
VNode Namespace
ns MisoString
name [Attribute action]
_ [View context action]
_ -> do
let newKids :: [View context action]
newKids = [View context action]
kids [View context action]
-> [View context action] -> [View context action]
forall a. [a] -> [a] -> [a]
++ [[View context action]] -> [View context action]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ [View context action]
opts | Bool
condition ]
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
forall context action.
Namespace
-> MisoString
-> [Attribute action]
-> [View context action]
-> View context action
VNode Namespace
ns MisoString
name [Attribute action]
attrs [View context action]
newKids
View context action
x -> View context action
x
data URI
= URI
{ URI -> MisoString
uriPath :: MisoString
, URI -> MisoString
uriFragment :: MisoString
, URI -> Map MisoString (Maybe MisoString)
uriQueryString :: M.Map MisoString (Maybe MisoString)
} deriving stock (Int -> URI -> ShowS
[URI] -> ShowS
URI -> String
(Int -> URI -> ShowS)
-> (URI -> String) -> ([URI] -> ShowS) -> Show URI
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> URI -> ShowS
showsPrec :: Int -> URI -> ShowS
$cshow :: URI -> String
show :: URI -> String
$cshowList :: [URI] -> ShowS
showList :: [URI] -> ShowS
Show, URI -> URI -> Bool
(URI -> URI -> Bool) -> (URI -> URI -> Bool) -> Eq URI
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: URI -> URI -> Bool
== :: URI -> URI -> Bool
$c/= :: URI -> URI -> Bool
/= :: URI -> URI -> Bool
Eq, (forall x. URI -> Rep URI x)
-> (forall x. Rep URI x -> URI) -> Generic URI
forall x. Rep URI x -> URI
forall x. URI -> Rep URI x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. URI -> Rep URI x
from :: forall x. URI -> Rep URI x
$cto :: forall x. Rep URI x -> URI
to :: forall x. Rep URI x -> URI
Generic)
deriving anyclass (URI -> IO JSVal
(URI -> IO JSVal) -> ToJSVal URI
forall a. (a -> IO JSVal) -> ToJSVal a
$ctoJSVal :: URI -> IO JSVal
toJSVal :: URI -> IO JSVal
ToJSVal, URI -> IO Object
(URI -> IO Object) -> ToObject URI
forall a. (a -> IO Object) -> ToObject a
$ctoObject :: URI -> IO Object
toObject :: URI -> IO Object
ToObject)
emptyURI :: URI
emptyURI :: URI
emptyURI = MisoString
-> MisoString -> Map MisoString (Maybe MisoString) -> URI
URI MisoString
forall a. Monoid a => a
mempty MisoString
forall a. Monoid a => a
mempty Map MisoString (Maybe MisoString)
forall a. Monoid a => a
mempty
instance ToMisoString URI where
toMisoString :: URI -> MisoString
toMisoString = URI -> MisoString
prettyURI
instance ToJSON URI where
toJSON :: URI -> Value
toJSON = MisoString -> Value
forall a. ToJSON a => a -> Value
toJSON (MisoString -> Value) -> (URI -> MisoString) -> URI -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> MisoString
forall str. ToMisoString str => str -> MisoString
toMisoString
prettyURI :: URI -> MisoString
prettyURI :: URI -> MisoString
prettyURI uri :: URI
uri@URI {Map MisoString (Maybe MisoString)
MisoString
uriPath :: URI -> MisoString
uriFragment :: URI -> MisoString
uriQueryString :: URI -> Map MisoString (Maybe MisoString)
uriPath :: MisoString
uriFragment :: MisoString
uriQueryString :: Map MisoString (Maybe MisoString)
..} = MisoString
"/" MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
uriPath MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> URI -> MisoString
prettyQueryString URI
uri MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
uriFragment
prettyQueryString :: URI -> MisoString
prettyQueryString :: URI -> MisoString
prettyQueryString URI {Map MisoString (Maybe MisoString)
MisoString
uriPath :: URI -> MisoString
uriFragment :: URI -> MisoString
uriQueryString :: URI -> Map MisoString (Maybe MisoString)
uriPath :: MisoString
uriFragment :: MisoString
uriQueryString :: Map MisoString (Maybe MisoString)
..} = MisoString
queries MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
flags
where
queries :: MisoString
queries =
[MisoString] -> MisoString
MS.concat
[ MisoString
"?" MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<>
MisoString -> [MisoString] -> MisoString
MS.intercalate MisoString
"&"
[ MisoString
k MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
"=" MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
v
| (MisoString
k, Just MisoString
v) <- Map MisoString (Maybe MisoString)
-> [(MisoString, Maybe MisoString)]
forall k a. Map k a -> [(k, a)]
M.toList Map MisoString (Maybe MisoString)
uriQueryString
]
| (Maybe MisoString -> Bool) -> [Maybe MisoString] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Maybe MisoString -> Bool
forall a. Maybe a -> Bool
isJust (Map MisoString (Maybe MisoString) -> [Maybe MisoString]
forall k a. Map k a -> [a]
M.elems Map MisoString (Maybe MisoString)
uriQueryString)
]
flags :: MisoString
flags = [MisoString] -> MisoString
forall a. Monoid a => [a] -> a
mconcat
[ MisoString
"?" MisoString -> MisoString -> MisoString
forall a. Semigroup a => a -> a -> a
<> MisoString
k
| (MisoString
k, Maybe MisoString
Nothing) <- Map MisoString (Maybe MisoString)
-> [(MisoString, Maybe MisoString)]
forall k a. Map k a -> [(k, a)]
M.toList Map MisoString (Maybe MisoString)
uriQueryString
]
data VTreeType
= VCompType
| VNodeType
| VTextType
| VFragType
deriving (Int -> VTreeType -> ShowS
[VTreeType] -> ShowS
VTreeType -> String
(Int -> VTreeType -> ShowS)
-> (VTreeType -> String)
-> ([VTreeType] -> ShowS)
-> Show VTreeType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VTreeType -> ShowS
showsPrec :: Int -> VTreeType -> ShowS
$cshow :: VTreeType -> String
show :: VTreeType -> String
$cshowList :: [VTreeType] -> ShowS
showList :: [VTreeType] -> ShowS
Show, VTreeType -> VTreeType -> Bool
(VTreeType -> VTreeType -> Bool)
-> (VTreeType -> VTreeType -> Bool) -> Eq VTreeType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VTreeType -> VTreeType -> Bool
== :: VTreeType -> VTreeType -> Bool
$c/= :: VTreeType -> VTreeType -> Bool
/= :: VTreeType -> VTreeType -> Bool
Eq)
instance ToJSVal VTreeType where
toJSVal :: VTreeType -> IO JSVal
toJSVal = \case
VTreeType
VCompType -> Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (Int
0 :: Int)
VTreeType
VNodeType -> Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (Int
1 :: Int)
VTreeType
VTextType -> Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (Int
2 :: Int)
VTreeType
VFragType -> Int -> IO JSVal
forall a. ToJSVal a => a -> IO JSVal
toJSVal (Int
3 :: Int)