@@ -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 ]