@@ -14,7 +14,7 @@ import Data.String (Pattern(..))
1414import Data.String.Regex as Regex
1515import Data.String.Regex.Flags as RegexFlags
1616import Effect (Effect )
17- import Effect.Aff (Aff , makeAff )
17+ import Effect.Aff (Aff , Milliseconds (..), delay , makeAff )
1818import Effect.Aff as Aff
1919import Effect.Class.Console (error )
2020import Effect.Uncurried (EffectFn3 , runEffectFn3 )
@@ -34,9 +34,10 @@ import Try.QueryString (compressToEncodedURIComponent, decompressFromEncodedURIC
3434import Try.SharedConfig as SharedConfig
3535import Type.Proxy (Proxy (..))
3636import Web.HTML (window )
37- import Web.HTML.Window (alert )
37+ import Web.HTML.Location (href )
38+ import Web.HTML.Window (alert , location )
3839
39- type Slots = ( editor :: Editor.Slot Unit )
40+ type Slots = ( editor :: Editor.Slot Unit , shareButton :: forall q o . H.Slot q o Unit )
4041
4142data SourceFile = GitHub String | Gist String
4243
@@ -83,10 +84,11 @@ data Action
8384_editor :: Proxy " editor"
8485_editor = Proxy
8586
86- type LoadCb = Effect Unit
87+ type SucceedCb = Effect Unit
8788type FailCb = Effect Unit
88- foreign import setupIFrame :: EffectFn3 { code :: String } LoadCb FailCb Unit
89+ foreign import setupIFrame :: EffectFn3 { code :: String } SucceedCb FailCb Unit
8990foreign import teardownIFrame :: Effect Unit
91+ foreign import copyToClipboard :: EffectFn3 String SucceedCb FailCb Unit
9092
9193component :: forall q i o . H.Component q i o Aff
9294component = H .mkComponent
@@ -333,6 +335,7 @@ component = H.mkComponent
333335 ]
334336 [ HH .text " Show JS" ]
335337 ]
338+ , HH .slot_ (Proxy :: _ " shareButton" ) unit shareButton unit
336339 , HH .li
337340 [ HP .class_ $ HH.ClassName " menu-item" ]
338341 [ HH .a
@@ -435,6 +438,36 @@ renderCompilerErrors errors = do
435438 , renderPlaintext message
436439 ]
437440
441+ shareButton :: forall q i o . H.Component q i o Aff
442+ shareButton = H .mkComponent
443+ { initialState: \_ -> 0
444+ , render
445+ , eval: H .mkEval $ H .defaultEval
446+ { handleAction = handleAction
447+ }
448+ }
449+ where
450+ handleAction :: Unit -> H.HalogenM Int Unit () o Aff Unit
451+ handleAction _ = do
452+ url <- H .liftEffect $ window >>= location >>= href
453+ H .liftAff $ makeAff \f -> do
454+ runEffectFn3 copyToClipboard url (f (Right unit)) (f (Left $ Aff .error " Failed to copy to clipboard" ))
455+ mempty
456+ H .modify_ (_ + 1 )
457+ H .liftAff $ delay (1_500 .0 # Milliseconds )
458+ H .modify_ (_ - 1 )
459+ render :: Int -> H.ComponentHTML Unit () Aff
460+ render n =
461+ HH .li
462+ [ HP .class_ $ HH.ClassName " menu-item no-mobile" ]
463+ [ HH .label
464+ [ HP .id " share_label"
465+ , HP .title " Share URL"
466+ , HE .onClick \_ -> unit
467+ ]
468+ [ HH .text (if n > 0 then " ✔️ Copied to clipboard" else " Share URL" ) ]
469+ ]
470+
438471menuRadio
439472 :: forall w
440473 . { name :: String
0 commit comments