@@ -12,16 +12,21 @@ import Data.Maybe (Maybe(..), maybe)
1212import Data.Set as Set
1313import Data.String (replaceAll , Pattern (..), Replacement (..))
1414import Data.Symbol (SProxy (..))
15+ import Data.Tuple.Nested ((/\))
1516import Effect.Class (class MonadEffect , liftEffect )
1617import Global (encodeURIComponent )
1718import Halogen as H
18- import Halogen.HTML hiding (map , head , i , prop )
19- import Halogen.HTML.Properties (classes , value , readOnly , href , type_ , InputType (InputText))
20- import Halogen.HTML.Events (onValueInput , onClick )
19+ import Halogen.HTML hiding (map , head , i , prop , title )
20+ import Halogen.HTML.Properties (classes , value , readOnly , href , type_ , InputType (InputText), ref , title )
21+ import Halogen.HTML.Events (onValueInput , onClick , onMouseDown )
22+ import Halogen.Query.Input (RefLabel (..))
2123import View.ReactiveInput as ReactiveInput
2224import Web.HTML (window )
2325import Web.HTML.Location (setHash )
2426import Web.HTML.Window (location )
27+ import Web.Event.Event (Event )
28+ import Web.Event.Event as Event
29+ import Web.UIEvent.MouseEvent (toEvent )
2530
2631import KDMonCat.Bricks as Bricks
2732import KDMonCat.InferType
@@ -33,7 +38,7 @@ import KDMonCat.Output.JSON (json)
3338
3439import View.KDMonCat.Bricks as Bricks
3540import View.KDMonCat.Term as Term
36- import View.KDMonCat.CopyToClipboard (copyToClipboard )
41+ import View.KDMonCat.EditDomHelpers (copyToClipboard , insertText )
3742
3843type Input = String.Input
3944type Output = String.Input
@@ -63,6 +68,8 @@ data Action
6368 | UpdateContext String
6469 | BricksMessage Bricks.Output
6570 | CopyToClipboard String
71+ | InsertPixel String
72+ | PreventDefault Action Event
6673
6774data Query a
6875 = DoAction Action a
@@ -81,7 +88,7 @@ appView =
8188 ReactiveInput .mkComponentWithQuery
8289 { initialState
8390 , render
84- , handleAction: \_ -> handleAction
91+ , handleAction
8592 , handleQuery
8693 , handleInput
8794 }
@@ -99,9 +106,11 @@ render _ st = div [ classes [ ClassName "kdmoncat-app" ] ]
99106 [ label_ [ text " Whole" ], input [ type_ InputText , value termTypeStr, readOnly true ] ]
100107 <> (selectionType # maybe [] \s -> [ label_ [ text " Selection" ], input [ type_ InputText , value s, readOnly true ] ])
101108 , h2_ [ text " Pixels" ]
102- , textarea [ value st.input.pixels, onValueInput (Just <<< UpdatePixels ) ]
109+ , textarea [ value st.input.pixels, onValueInput (Just <<< UpdatePixels ), ref (RefLabel " pixels" ) ]
110+ , div_ pixelButtons
103111 , h2_ [ text " Context" ]
104- , textarea [ value st.input.context, onValueInput (Just <<< UpdateContext ) ]
112+ , textarea [ value st.input.context, onValueInput (Just <<< UpdateContext ), ref (RefLabel " context" ) ]
113+ , div_ [ text " F.e.: " , code_ [ text " a b -> c d, [4 2 3 1] (Wire swaps), 3.5, 4o2 (Spiders)" ] ]
105114 , h2_ [ text " Copy serialized output to clipboard" ]
106115 , div_ $ inferredType # either (const [] ) (\{ term } ->
107116 [ button [ onClick \_ -> Just (CopyToClipboard $ json term) ]
@@ -127,6 +136,22 @@ render _ st = div [ classes [ ClassName "kdmoncat-app" ] ]
127136 selectionPath = Bricks .toSelection st.selectionBox bricksInput.bricks.term Nil
128137 selectionType = hush inferredType <#> \{ errors, term } -> showInferred { errors, term: getSubTerm selectionPath term }
129138
139+ predefined =
140+ [ " -" /\ " id, type [1]"
141+ , " =" /\ " id ⊗ id, type [1 2]"
142+ , " σ" /\ " Swap, type [2 1]"
143+ , " ε" /\ " Counit (discard), type 1.0"
144+ , " δ" /\ " Comultiply (copy), type 1.2"
145+ , " η" /\ " Unit (zero), type 1o0"
146+ , " μ" /\ " Multiply (sum), type 1o2"
147+ , " (" /\ " Cup"
148+ , " )" /\ " Cap"
149+ ]
150+ pixelButtons = predefined <#> \(pixel /\ help) ->
151+ button [ onMouseDown $ Just <<< PreventDefault (InsertPixel pixel) <<< toEvent
152+ , title help ]
153+ [ text pixel ]
154+
130155toBricksInput :: Input -> Box -> Bricks.Input
131156toBricksInput input selectionBox =
132157 { bricks, matches, context, selectedBoxes, renderBoxContent: Bricks .defaultRenderBoxContent }
@@ -148,8 +173,8 @@ handleInput input = do
148173 H .put $ initialState { input = input }
149174 updateWindowLocation input
150175
151- handleAction :: ∀ m . MonadEffect m => Action -> H.HalogenM State Action ChildSlots Output m Unit
152- handleAction = case _ of
176+ handleAction :: ∀ m . MonadEffect m => Input -> Action -> H.HalogenM State Action ChildSlots Output m Unit
177+ handleAction inp = case _ of
153178 UpdatePixels p -> do
154179 st <- H .modify $ set (_input <<< _pixels) p
155180 H .raise st.input
@@ -162,10 +187,15 @@ handleAction = case _ of
162187 H .modify_ $ set _selectionBox sel
163188 CopyToClipboard s ->
164189 liftEffect (copyToClipboard s)
165-
166- handleQuery :: ∀ m a . MonadEffect m => Query a -> H.HalogenM State Action ChildSlots Output m (Maybe a )
167- handleQuery (DoAction x next) = do
168- handleAction x
190+ InsertPixel s ->
191+ liftEffect (insertText s)
192+ PreventDefault action event -> do
193+ H .liftEffect $ Event .preventDefault event
194+ handleAction inp action
195+
196+ handleQuery :: ∀ m a . MonadEffect m => Input -> Query a -> H.HalogenM State Action ChildSlots Output m (Maybe a )
197+ handleQuery inp (DoAction x next) = do
198+ handleAction inp x
169199 pure (Just next)
170200
171201updateWindowLocation :: ∀ m . MonadEffect m => Input -> H.HalogenM State Action ChildSlots Output m Unit
0 commit comments