Skip to content

Commit 1d9a4bb

Browse files
authored
Merge pull request #384 from statebox/383/kdmoncat-help
kdmoncat: Add help #383
2 parents 160a3ac + aa3f6f5 commit 1d9a4bb

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

halogen-grid-kit/src/View/ReactiveInput.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ComponentSpec surface state action slots input output m =
3131

3232
type ComponentSpecQuery surface state query action slots input output m =
3333
Record (ComponentSpecRow surface state action slots input output m
34-
(handleQuery :: forall a. query a -> H.HalogenM state action slots output m (Maybe a)))
34+
(handleQuery :: forall a. input -> query a -> H.HalogenM state action slots output m (Maybe a)))
3535

3636
mkComponentWithQuery
3737
:: surface state query action slots input output m
@@ -45,7 +45,7 @@ mkComponentWithQuery spec@{ initialState, render } =
4545
, render: \{ input, rest } -> render input rest # mapAction Rest
4646
, eval: H.mkEval $ H.defaultEval
4747
{ handleAction = handle spec
48-
, handleQuery = \q -> H.get >>= \{ input } -> mapHalogenM input (spec.handleQuery q)
48+
, handleQuery = \q -> H.get >>= \{ input } -> mapHalogenM input (spec.handleQuery input q)
4949
, receive = Just <<< Update
5050
, initialize = Just Initialize
5151
}
@@ -58,7 +58,7 @@ mkComponent
5858
=> ComponentSpec surface state action slots input output m
5959
-> H.Component surface query input output m
6060
mkComponent { initialState, render, handleInput, handleAction } =
61-
mkComponentWithQuery { initialState, render, handleInput, handleAction, handleQuery: H.defaultEval.handleQuery }
61+
mkComponentWithQuery { initialState, render, handleInput, handleAction, handleQuery: \_ -> H.defaultEval.handleQuery }
6262

6363
handle
6464
:: surface state query action slots input output m

halogen-kdmoncat/spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
, "ordered-collections"
1515
, "profunctor-lenses"
1616
, "psci-support"
17+
, "random"
1718
, "strings"
1819
, "variant"
1920
, "vec"

halogen-kdmoncat/src/View/KDMonCat/App.purs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import Data.Maybe (Maybe(..), maybe)
1212
import Data.Set as Set
1313
import Data.String (replaceAll, Pattern(..), Replacement(..))
1414
import Data.Symbol (SProxy(..))
15+
import Data.Tuple.Nested ((/\))
1516
import Effect.Class (class MonadEffect, liftEffect)
1617
import Global (encodeURIComponent)
1718
import 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(..))
2123
import View.ReactiveInput as ReactiveInput
2224
import Web.HTML (window)
2325
import Web.HTML.Location (setHash)
2426
import Web.HTML.Window (location)
27+
import Web.Event.Event (Event)
28+
import Web.Event.Event as Event
29+
import Web.UIEvent.MouseEvent (toEvent)
2530

2631
import KDMonCat.Bricks as Bricks
2732
import KDMonCat.InferType
@@ -33,7 +38,7 @@ import KDMonCat.Output.JSON (json)
3338

3439
import View.KDMonCat.Bricks as Bricks
3540
import View.KDMonCat.Term as Term
36-
import View.KDMonCat.CopyToClipboard (copyToClipboard)
41+
import View.KDMonCat.EditDomHelpers (copyToClipboard, insertText)
3742

3843
type Input = String.Input
3944
type 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

6774
data 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+
130155
toBricksInput :: Input -> Box -> Bricks.Input
131156
toBricksInput 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

171201
updateWindowLocation :: m. MonadEffect m => Input -> H.HalogenM State Action ChildSlots Output m Unit

halogen-kdmoncat/src/View/KDMonCat/CopyToClipboard.js renamed to halogen-kdmoncat/src/View/KDMonCat/EditDomHelpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ exports.copyToClipboard = function (text) {
99
document.execCommand('copy');
1010
document.body.removeChild(textArea);
1111
}
12+
13+
exports.insertText = function (text) {
14+
document.execCommand("insertText", false, text);
15+
}

halogen-kdmoncat/src/View/KDMonCat/CopyToClipboard.purs renamed to halogen-kdmoncat/src/View/KDMonCat/EditDomHelpers.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
module View.KDMonCat.CopyToClipboard where
1+
module View.KDMonCat.EditDomHelpers where
22

33
import Prelude
44
import Effect (Effect)
55

66
foreign import copyToClipboard :: String -> Effect Unit
7+
foreign import insertText :: String -> Effect Unit

0 commit comments

Comments
 (0)