@@ -1,20 +1,116 @@
1module Yoga.InlineCode.Stories where
1module Yoga.InlineCode.Stories where
3import Prelude hiding (add)
3import Prelude hiding (add)
4+import Data.Array (intercalate, length)
5+import Data.Either (Either(..))
4import Data.Maybe (Maybe(..))
6import Data.Maybe (Maybe(..))
7+import Data.String (Pattern(..), split, trim)
8+import Data.String.CodeUnits as String
9+import Data.Tuple.Nested ((/\))
10+import Debug.Trace (spy)
5import Effect (Effect)
11import Effect (Effect)
12+import Effect.Aff (Aff)
6import Effect.Class.Console (log)
13import Effect.Class.Console (log)
14+import Justifill (justifill)
16+import Milkis.Impl.Window (windowFetch)
17+import React.Basic (JSX, fragment)
7import React.Basic.DOM as R
18import React.Basic.DOM as R
19+import React.Basic.Extra.Hooks (useAffReducer)
20+import React.Basic.Helpers (jsx)
21+import React.Basic.Hooks (ReactComponent, component, element)
22+import React.Basic.Hooks as React
8import Storybook.Decorator.FullScreen (fullScreenDecorator)
23import Storybook.Decorator.FullScreen (fullScreenDecorator)
9import Storybook.React (Storybook, add, addDecorator, storiesOf)
24import Storybook.React (Storybook, add, addDecorator, storiesOf)
25+import Yoga.CompileEditor.Component (compileAndRun)
26+import Yoga.InlineCode.Component (Action(..))
10import Yoga.InlineCode.Component as InlineCode
27import Yoga.InlineCode.Component as InlineCode
28+import Yoga.Modal.Component as Modal
12stories ∷ Effect Storybook
30stories ∷ Effect Storybook
13stories = do
31stories = do
14 storiesOf "InlineCode" do
32 storiesOf "InlineCode" do
15 addDecorator fullScreenDecorator
33 addDecorator fullScreenDecorator
16 add "The InlineCode" InlineCode.makeComponent
34 add "The InlineCode" InlineCode.makeComponent
17− [ { dispatch: const $ log "hi"
36+ { dispatch: \(_ ∷ InlineCode.Action) -> ((log "hi") ∷ Effect Unit)
18− , className: Nothing
37+ }
40+ add "The InlineCode with some context" mkWrapper
44+ , element inlineCode (justifill { dispatch: \(_ ∷ InlineCode.Action) -> (log "Hi") ∷ Effect Unit })
45+ , R.text "hello again!"
49+ add "The InlineCode with some code" mkWrapper
52+ [ R.code_ [ R.text "main = logShow \"" ]
53+ , element inlineCode (justifill { dispatch: \(_ ∷ InlineCode.Action) -> (log "Hi") ∷ Effect Unit })
54+ , R.code_ [ R.text "\"" ]
58+ add "The InlineCode with some real code" mkRealWrapper
69+renderCode c = intercalate [ R.br {} ] (toJSX <$> lines)
71+ toJSX line = [ R.code_ [ R.text line ] ]
72+ lines = split (Pattern "\n") c
74+mkWrapper ∷ Effect (ReactComponent { inside ∷ ReactComponent InlineCode.Props -> Array JSX })
76+ inlineCode <- InlineCode.makeComponent
77+ component "InlineCodeWrapper" \{ inside } -> React.do
79+ $ R.div_ (inside inlineCode)
85+ = InlineCodeAction InlineCode.Action
88+derive instance eqRealAction ∷ Eq RealAction
89+mkRealWrapper ∷ Effect (ReactComponent {})
91+ inlineCode <- InlineCode.makeComponent
92+ modal <- Modal.makeComponent
93+ component "InlineCodeWrapper" \{} -> React.do
94+ state /\ dispatch <- useAffReducer Nothing realReducer
97+ [ R.h2_ [ R.text "Let's log some 'Magick'" ]
98+ , fragment $ renderCode codePrefix
99+ , element inlineCode (justifill { dispatch: dispatch <<< InlineCodeAction, width: String.length "Magick" })
100+ , fragment $ renderCode codeSuffix
103+ Just true -> element modal $ justifill { title: "Success!", content: R.text "You did it!", onClose: dispatch CloseModal }
104+ Just false -> element modal $ justifill { title: "Oh no!", content: R.text "Try again!", onClose: dispatch CloseModal }
108+realReducer ∷ State -> RealAction -> Aff State
109+realReducer state = case _ of
110+ CloseModal -> pure Nothing
111+ InlineCodeAction (CompileAndRunCode code) -> do
112+ res <- compileAndRun (M.fetch windowFetch) { code: codePrefix <> code <> codeSuffix }
113+ (pure <<< Just) case spy "res" res of
115+ | String.dropRight 1 stdout == "Magick" -> true