{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Miso.Native.Element.Text.Event
(
onLayout
, onLayoutWith
, onLayoutMain
, onLayoutMainWith
, onSelectionChange
, onSelectionChangeWith
, onSelectionChangeMain
, onSelectionChangeMainWith
, LayoutEvent (..)
, LineInfo (..)
, Size (..)
, SelectionChangeEvent (..)
, Direction (..)
, layoutDecoder
, selectionChangeDecoder
, textEvents
) where
import qualified Data.Map as M
import Miso.Event
import Miso.JSON
import Miso.Types (Attribute, EventHandler, DOMRef)
textEvents :: Events
textEvents :: Events
textEvents
= [(Text, Phase)] -> Events
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
[ (Text
"layout", Phase
BUBBLE)
, (Text
"selectionchange", Phase
BUBBLE)
]
onLayout :: (LayoutEvent -> action) -> Attribute model action
onLayout :: forall action model.
(LayoutEvent -> action) -> Attribute model action
onLayout LayoutEvent -> action
action = Text
-> Decoder LayoutEvent
-> (LayoutEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on Text
"layout" Decoder LayoutEvent
layoutDecoder (\LayoutEvent
e model
_ DOMRef
_ -> LayoutEvent -> action
action LayoutEvent
e)
onLayoutMain :: (LayoutEvent -> action) -> EventHandler model action
onLayoutMain :: forall action model.
(LayoutEvent -> action) -> EventHandler model action
onLayoutMain LayoutEvent -> action
action = Text
-> Decoder LayoutEvent
-> (LayoutEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain Text
"layout" Decoder LayoutEvent
layoutDecoder (\LayoutEvent
e model
_ DOMRef
_ -> LayoutEvent -> action
action LayoutEvent
e)
onLayoutMainWith :: (LayoutEvent -> model -> DOMRef -> action) -> EventHandler model action
onLayoutMainWith :: forall model action.
(LayoutEvent -> model -> DOMRef -> action)
-> EventHandler model action
onLayoutMainWith LayoutEvent -> model -> DOMRef -> action
action = Text
-> Decoder LayoutEvent
-> (LayoutEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain Text
"layout" Decoder LayoutEvent
layoutDecoder LayoutEvent -> model -> DOMRef -> action
action
onSelectionChange :: (SelectionChangeEvent -> action) -> Attribute model action
onSelectionChange :: forall action model.
(SelectionChangeEvent -> action) -> Attribute model action
onSelectionChange SelectionChangeEvent -> action
action = Text
-> Decoder SelectionChangeEvent
-> (SelectionChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on Text
"selectionchange" Decoder SelectionChangeEvent
selectionChangeDecoder (\SelectionChangeEvent
e model
_ DOMRef
_ -> SelectionChangeEvent -> action
action SelectionChangeEvent
e)
onSelectionChangeMain :: (SelectionChangeEvent -> action) -> EventHandler model action
onSelectionChangeMain :: forall action model.
(SelectionChangeEvent -> action) -> EventHandler model action
onSelectionChangeMain SelectionChangeEvent -> action
action = Text
-> Decoder SelectionChangeEvent
-> (SelectionChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain Text
"selectionchange" Decoder SelectionChangeEvent
selectionChangeDecoder (\SelectionChangeEvent
e model
_ DOMRef
_ -> SelectionChangeEvent -> action
action SelectionChangeEvent
e)
onSelectionChangeMainWith :: (SelectionChangeEvent -> model -> DOMRef -> action) -> EventHandler model action
onSelectionChangeMainWith :: forall model action.
(SelectionChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
onSelectionChangeMainWith SelectionChangeEvent -> model -> DOMRef -> action
action = Text
-> Decoder SelectionChangeEvent
-> (SelectionChangeEvent -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain Text
"selectionchange" Decoder SelectionChangeEvent
selectionChangeDecoder SelectionChangeEvent -> model -> DOMRef -> action
action
selectionChangeDecoder :: Decoder SelectionChangeEvent
selectionChangeDecoder :: Decoder SelectionChangeEvent
selectionChangeDecoder = [Text
"detail"] [Text]
-> (Value -> Parser SelectionChangeEvent)
-> Decoder SelectionChangeEvent
forall a. [Text] -> (Value -> Parser a) -> Decoder a
`at` Value -> Parser SelectionChangeEvent
parser
where
parser :: Value -> Parser SelectionChangeEvent
parser :: Value -> Parser SelectionChangeEvent
parser = Text
-> (Map Text Value -> Parser SelectionChangeEvent)
-> Value
-> Parser SelectionChangeEvent
forall a. Text -> (Map Text Value -> Parser a) -> Value -> Parser a
withObject Text
"SelectionChangeEvent" ((Map Text Value -> Parser SelectionChangeEvent)
-> Value -> Parser SelectionChangeEvent)
-> (Map Text Value -> Parser SelectionChangeEvent)
-> Value
-> Parser SelectionChangeEvent
forall a b. (a -> b) -> a -> b
$ \Map Text Value
o -> do
Double -> Double -> Direction -> SelectionChangeEvent
SelectionChangeEvent
(Double -> Double -> Direction -> SelectionChangeEvent)
-> Parser Double
-> Parser (Double -> Direction -> SelectionChangeEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"start"
Parser (Double -> Direction -> SelectionChangeEvent)
-> Parser Double -> Parser (Direction -> SelectionChangeEvent)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"end"
Parser (Direction -> SelectionChangeEvent)
-> Parser Direction -> Parser SelectionChangeEvent
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Text Value
o Map Text Value -> Text -> Parser Direction
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"direction"
data SelectionChangeEvent
= SelectionChangeEvent
{ SelectionChangeEvent -> Double
start, SelectionChangeEvent -> Double
end :: Double
, SelectionChangeEvent -> Direction
direction :: Direction
} deriving (Int -> SelectionChangeEvent -> ShowS
[SelectionChangeEvent] -> ShowS
SelectionChangeEvent -> String
(Int -> SelectionChangeEvent -> ShowS)
-> (SelectionChangeEvent -> String)
-> ([SelectionChangeEvent] -> ShowS)
-> Show SelectionChangeEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SelectionChangeEvent -> ShowS
showsPrec :: Int -> SelectionChangeEvent -> ShowS
$cshow :: SelectionChangeEvent -> String
show :: SelectionChangeEvent -> String
$cshowList :: [SelectionChangeEvent] -> ShowS
showList :: [SelectionChangeEvent] -> ShowS
Show, SelectionChangeEvent -> SelectionChangeEvent -> Bool
(SelectionChangeEvent -> SelectionChangeEvent -> Bool)
-> (SelectionChangeEvent -> SelectionChangeEvent -> Bool)
-> Eq SelectionChangeEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SelectionChangeEvent -> SelectionChangeEvent -> Bool
== :: SelectionChangeEvent -> SelectionChangeEvent -> Bool
$c/= :: SelectionChangeEvent -> SelectionChangeEvent -> Bool
/= :: SelectionChangeEvent -> SelectionChangeEvent -> Bool
Eq)
data Direction = Forward | Backward
deriving (Int -> Direction -> ShowS
[Direction] -> ShowS
Direction -> String
(Int -> Direction -> ShowS)
-> (Direction -> String)
-> ([Direction] -> ShowS)
-> Show Direction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Direction -> ShowS
showsPrec :: Int -> Direction -> ShowS
$cshow :: Direction -> String
show :: Direction -> String
$cshowList :: [Direction] -> ShowS
showList :: [Direction] -> ShowS
Show, Direction -> Direction -> Bool
(Direction -> Direction -> Bool)
-> (Direction -> Direction -> Bool) -> Eq Direction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Direction -> Direction -> Bool
== :: Direction -> Direction -> Bool
$c/= :: Direction -> Direction -> Bool
/= :: Direction -> Direction -> Bool
Eq)
instance FromJSON Direction where
parseJSON :: Value -> Parser Direction
parseJSON = Text -> (Text -> Parser Direction) -> Value -> Parser Direction
forall a. Text -> (Text -> Parser a) -> Value -> Parser a
withText Text
"Direction" ((Text -> Parser Direction) -> Value -> Parser Direction)
-> (Text -> Parser Direction) -> Value -> Parser Direction
forall a b. (a -> b) -> a -> b
$ \case
Text
"forward" -> Direction -> Parser Direction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Direction
Forward
Text
"backward" -> Direction -> Parser Direction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Direction
Backward
Text
x -> Text -> Value -> Parser Direction
forall a. Text -> Value -> Parser a
typeMismatch Text
"Direction" (Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
x)
data LayoutEvent
= LayoutEvent
{ LayoutEvent -> Double
lineInfoLineCount :: Double
, LayoutEvent -> [LineInfo]
lineInfoLines :: [LineInfo]
, LayoutEvent -> Size
lineInfoSize :: Size
} deriving (Int -> LayoutEvent -> ShowS
[LayoutEvent] -> ShowS
LayoutEvent -> String
(Int -> LayoutEvent -> ShowS)
-> (LayoutEvent -> String)
-> ([LayoutEvent] -> ShowS)
-> Show LayoutEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LayoutEvent -> ShowS
showsPrec :: Int -> LayoutEvent -> ShowS
$cshow :: LayoutEvent -> String
show :: LayoutEvent -> String
$cshowList :: [LayoutEvent] -> ShowS
showList :: [LayoutEvent] -> ShowS
Show, LayoutEvent -> LayoutEvent -> Bool
(LayoutEvent -> LayoutEvent -> Bool)
-> (LayoutEvent -> LayoutEvent -> Bool) -> Eq LayoutEvent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LayoutEvent -> LayoutEvent -> Bool
== :: LayoutEvent -> LayoutEvent -> Bool
$c/= :: LayoutEvent -> LayoutEvent -> Bool
/= :: LayoutEvent -> LayoutEvent -> Bool
Eq)
layoutDecoder :: Decoder LayoutEvent
layoutDecoder :: Decoder LayoutEvent
layoutDecoder = [Text
"detail"] [Text] -> (Value -> Parser LayoutEvent) -> Decoder LayoutEvent
forall a. [Text] -> (Value -> Parser a) -> Decoder a
`at` do
Text
-> (Map Text Value -> Parser LayoutEvent)
-> Value
-> Parser LayoutEvent
forall a. Text -> (Map Text Value -> Parser a) -> Value -> Parser a
withObject Text
"LayoutEvent" ((Map Text Value -> Parser LayoutEvent)
-> Value -> Parser LayoutEvent)
-> (Map Text Value -> Parser LayoutEvent)
-> Value
-> Parser LayoutEvent
forall a b. (a -> b) -> a -> b
$ \Map Text Value
o ->
Double -> [LineInfo] -> Size -> LayoutEvent
LayoutEvent
(Double -> [LineInfo] -> Size -> LayoutEvent)
-> Parser Double -> Parser ([LineInfo] -> Size -> LayoutEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"lineCount"
Parser ([LineInfo] -> Size -> LayoutEvent)
-> Parser [LineInfo] -> Parser (Size -> LayoutEvent)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Text Value
o Map Text Value -> Text -> Parser [LineInfo]
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"lines"
Parser (Size -> LayoutEvent) -> Parser Size -> Parser LayoutEvent
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> do
s <- Map Text Value
o Map Text Value -> Text -> Parser (Map Text Value)
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"size"
Size <$> s .: "width" <*> s .: "height"
instance FromJSON LineInfo where
parseJSON :: Value -> Parser LineInfo
parseJSON = Text
-> (Map Text Value -> Parser LineInfo) -> Value -> Parser LineInfo
forall a. Text -> (Map Text Value -> Parser a) -> Value -> Parser a
withObject Text
"lineInfo" ((Map Text Value -> Parser LineInfo) -> Value -> Parser LineInfo)
-> (Map Text Value -> Parser LineInfo) -> Value -> Parser LineInfo
forall a b. (a -> b) -> a -> b
$ \Map Text Value
o ->
Double -> Double -> Double -> LineInfo
LineInfo
(Double -> Double -> Double -> LineInfo)
-> Parser Double -> Parser (Double -> Double -> LineInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"start"
Parser (Double -> Double -> LineInfo)
-> Parser Double -> Parser (Double -> LineInfo)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"end"
Parser (Double -> LineInfo) -> Parser Double -> Parser LineInfo
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map Text Value
o Map Text Value -> Text -> Parser Double
forall a. FromJSON a => Map Text Value -> Text -> Parser a
.: Text
"ellipsisCount"
data LineInfo
= LineInfo
{ LineInfo -> Double
lineInfoStart, LineInfo -> Double
lineInfoEnd, LineInfo -> Double
lineInfoEllipsisCount :: Double
} deriving (Int -> LineInfo -> ShowS
[LineInfo] -> ShowS
LineInfo -> String
(Int -> LineInfo -> ShowS)
-> (LineInfo -> String) -> ([LineInfo] -> ShowS) -> Show LineInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LineInfo -> ShowS
showsPrec :: Int -> LineInfo -> ShowS
$cshow :: LineInfo -> String
show :: LineInfo -> String
$cshowList :: [LineInfo] -> ShowS
showList :: [LineInfo] -> ShowS
Show, LineInfo -> LineInfo -> Bool
(LineInfo -> LineInfo -> Bool)
-> (LineInfo -> LineInfo -> Bool) -> Eq LineInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LineInfo -> LineInfo -> Bool
== :: LineInfo -> LineInfo -> Bool
$c/= :: LineInfo -> LineInfo -> Bool
/= :: LineInfo -> LineInfo -> Bool
Eq)
data Size
= Size
{ Size -> Double
sizeWidth, Size -> Double
sizeHeight :: Double
} deriving (Int -> Size -> ShowS
[Size] -> ShowS
Size -> String
(Int -> Size -> ShowS)
-> (Size -> String) -> ([Size] -> ShowS) -> Show Size
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Size -> ShowS
showsPrec :: Int -> Size -> ShowS
$cshow :: Size -> String
show :: Size -> String
$cshowList :: [Size] -> ShowS
showList :: [Size] -> ShowS
Show, Size -> Size -> Bool
(Size -> Size -> Bool) -> (Size -> Size -> Bool) -> Eq Size
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Size -> Size -> Bool
== :: Size -> Size -> Bool
$c/= :: Size -> Size -> Bool
/= :: Size -> Size -> Bool
Eq)
onLayoutWith :: (LayoutEvent -> DOMRef -> action) -> Attribute model action
onLayoutWith :: forall action model.
(LayoutEvent -> DOMRef -> action) -> Attribute model action
onLayoutWith LayoutEvent -> DOMRef -> action
action = Text
-> Decoder LayoutEvent
-> (LayoutEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on Text
"layout" Decoder LayoutEvent
layoutDecoder ((LayoutEvent -> model -> DOMRef -> action)
-> Attribute model action)
-> (LayoutEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \LayoutEvent
v model
_ DOMRef
domRef -> LayoutEvent -> DOMRef -> action
action LayoutEvent
v DOMRef
domRef
onSelectionChangeWith :: (SelectionChangeEvent -> DOMRef -> action) -> Attribute model action
onSelectionChangeWith :: forall action model.
(SelectionChangeEvent -> DOMRef -> action)
-> Attribute model action
onSelectionChangeWith SelectionChangeEvent -> DOMRef -> action
action = Text
-> Decoder SelectionChangeEvent
-> (SelectionChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
Text
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on Text
"selectionchange" Decoder SelectionChangeEvent
selectionChangeDecoder ((SelectionChangeEvent -> model -> DOMRef -> action)
-> Attribute model action)
-> (SelectionChangeEvent -> model -> DOMRef -> action)
-> Attribute model action
forall a b. (a -> b) -> a -> b
$ \SelectionChangeEvent
v model
_ DOMRef
domRef -> SelectionChangeEvent -> DOMRef -> action
action SelectionChangeEvent
v DOMRef
domRef