git.iko.soy/rowtype-yoga/psfp
on master help sign in
Works
commit0c34f9c63f83570a7fc488961be829c99578ffe1 authorMark Eibes <mark.eibes@gmail.com> dateWed 8 Apr 2020, 22:51 (7 years ago) parent7ef23c6
3 files changed+37−21
blog/.psc-ide-port+1−1 blog/src/components/FillInTheGaps.purs+29−18 components/src/Yoga/InlineCode/Component.purs+7−2
blog/.psc-ide-port+1−1
@@ -1 +1 @@
1−15750
1+15408
blog/src/components/FillInTheGaps.purs+29−18
@@ -5,6 +5,8 @@ import Data.Array (foldMap, intercalate)
55import Data.Array as A
66import Data.Either (Either(..))
77import Data.Foldable (foldl, for_)
8+import Data.Lens (over, (%~))
9+import Data.Lens.Index (ix)
810import Data.Maybe (Maybe(..), fromMaybe, fromMaybe')
911import Data.Monoid (guard)
1012import Data.String (Pattern(..), split)
@@ -14,6 +16,7 @@ import Data.String.Regex as Regex
1416import Data.String.Regex.Flags as RegexFlags
1517import Data.String.Regex.Unsafe (unsafeRegex)
1618import Data.Tuple.Nested ((/\))
19+import Debug.Trace (spy)
1720import Effect (Effect)
1821import Effect.Aff (launchAff_)
1922import Effect.Class (liftEffect)
@@ -70,24 +73,32 @@ complete arr = foldl f true (join arr)
7073 Hole _ s -> s /= ""
7174 _ -> true
7275
76+visibleRange ∷ Array (Array Segment) -> { end ∷ Int, start ∷ Int }
77+visibleRange arr = { start, end }
78+ where
79+ start = A.findIndex (_ == [ Start ]) arr ?|| 0
80+ end = A.findIndex (_ == [ End ]) arr ?|| A.length arr
81+
7382renderSegments ∷ ReactComponent InlineCode.Props -> ((Array (Array Segment) -> Array (Array Segment)) -> Effect Unit) -> Array (Array Segment) -> JSX
74−renderSegments ic update arrs = R.div_ (A.mapWithIndex renderLine (onlyVisible arrs))
83+renderSegments ic update arrs = R.div_ (A.mapWithIndex renderLine arrs)
7584 where
76− onlyVisible arr = A.slice start end arr
77− where
78− start = A.findIndex (_ == [ Start ]) arr ?|| 0
79− end = A.findIndex (_ == [ End ]) arr ?|| A.length arr
85+ { start, end } = visibleRange arrs
8086 renderLine i l = R.div_ (A.mapWithIndex (renderSegment i) l)
81− renderSegment i j = case _ of
82− Filler s -> R.code_ [ R.text s ]
83− Hole width _ -> element ic $ justifill { width, onSubmit: \v -> update (updateSegments i j (S.trim v)) }
84− _ -> mempty
87+ renderSegment i j s =
88+ if between start end i then case s of
89+ Filler s' -> R.code_ [ R.text s' ]
90+ Hole width _ ->
91+ element ic
92+ $ justifill
93+ { width
94+ , onSubmit: update <<< updateSegments i j
95+ }
96+ _ -> mempty
97+ else
98+ mempty
8599
86100updateSegments ∷ Int -> Int -> String -> Array (Array Segment) -> Array (Array Segment)
87−updateSegments i j v arrs = fromMaybe [] (A.modifyAt i (updateLine j v) arrs)
88−
89−updateLine ∷ Int -> String -> Array Segment -> Array Segment
90−updateLine idx v arr = fromMaybe [] (A.modifyAt idx f arr)
101+updateSegments i j v = (ix (spy "i" i) <<< ix (spy "j" j)) %~ f
91102 where
92103 f = case _ of
93104 Hole h _ -> Hole h v
@@ -146,10 +157,10 @@ mkFillInTheGaps fetch = do
146157 , jsx button
147158 { onClick: handler_ onClick
148159 , buttonType:
149− -- if complete segments
150− -- then
151− HighlightedButton
152− -- else DisabledButton
160+ if complete (spy "segsm" segments) then
161+ HighlightedButton
162+ else
163+ HighlightedButton
153164 }
154165 [ R.text "Try it" ]
155166 , result
@@ -160,7 +171,7 @@ mkFillInTheGaps fetch = do
160171 Right { stdout }
161172 | stdout == expectedResult -> "Hooray!"
162173 Left l -> "Does not compile"
163− Right _ -> "Not " <> (findResult (join segments))
174+ Right _ -> "Not " <> (findResult (spy "segs" $ join segments))
164175 }
165176 [ R.text $ compileResultToString result ]
166177 ]
components/src/Yoga/InlineCode/Component.purs+7−2
@@ -1,6 +1,8 @@
11module Yoga.InlineCode.Component where
22
33import Prelude
4+import Prelude
5+import CSS (value)
46import Data.Foldable (fold, intercalate)
57import Data.Maybe (Maybe)
68import Data.String (trim)
@@ -10,7 +12,7 @@ import Foreign.Object as Obj
1012import React.Basic.DOM as R
1113import React.Basic.DOM.Events (preventDefault, targetValue)
1214import React.Basic.Events (handler)
13−import React.Basic.Hooks (ReactComponent, component, useState)
15+import React.Basic.Hooks (ReactComponent, component, useEffect, useState)
1416import React.Basic.Hooks as React
1517import Record.Extra (pick)
1618import Yoga.Helpers ((?||))
@@ -35,6 +37,9 @@ makeComponent = do
3537 useStyles <- makeStylesJSS styles
3638 component "InlineCode" \props@{ className, onSubmit } -> React.do
3739 value /\ modifyValue <- useState ""
40+ useEffect value do
41+ onSubmit value
42+ pure mempty
3843 classes <- useStyles $ pick props
3944 pure
4045 $ R.form
@@ -51,5 +56,5 @@ makeComponent = do
5156 , _data: Obj.singleton "testid" "inline-code"
5257 }
5358 ]
54− , onSubmit: handler preventDefault (const $ onSubmit value)
59+ , onSubmit: handler preventDefault (const $ mempty)
5560 }