@@ -6,7 +6,7 @@ import Ace (Annotation)
66import Control.Monad.Except (runExceptT )
77import Data.Array as Array
88import Data.Either (Either (..), either )
9- import Data.Foldable (for_ , oneOf , fold )
9+ import Data.Foldable (for_ , oneOf , fold , traverse_ )
1010import Data.FoldableWithIndex (foldMapWithIndex )
1111import Data.Maybe (Maybe (..), fromMaybe , isNothing )
1212import Data.String as String
@@ -76,7 +76,7 @@ parseViewModeParam = case _ of
7676
7777data Action
7878 = Initialize
79- | Cache String
79+ | EncodeInURL String
8080 | UpdateSettings (Settings -> Settings )
8181 | Compile (Maybe String )
8282 | HandleEditor Editor.Output
@@ -140,7 +140,7 @@ component = H.mkComponent
140140 else
141141 handleAction $ Compile Nothing
142142
143- Cache text -> H .liftEffect do
143+ EncodeInURL text -> H .liftEffect do
144144 setQueryString " purs" $ compressToEncodedURIComponent text
145145
146146 Compile mbCode -> do
@@ -204,7 +204,7 @@ component = H.mkComponent
204204 H .modify_ _ { compiled = Just res }
205205
206206 HandleEditor (Editor.TextChanged text) -> do
207- _ <- H .fork $ handleAction $ Cache text
207+ _ <- H .fork $ handleAction $ EncodeInURL text
208208 { autoCompile } <- H .gets _.settings
209209 when autoCompile $ handleAction $ Compile $ Just text
210210
@@ -438,34 +438,47 @@ renderCompilerErrors errors = do
438438 , renderPlaintext message
439439 ]
440440
441+ type ShareButtonState =
442+ { forkId :: Maybe H.ForkId
443+ , showCopySucceeded :: Maybe Boolean
444+ }
445+
441446shareButton :: forall q i o . H.Component q i o Aff
442447shareButton = H .mkComponent
443- { initialState: \_ -> 0
448+ { initialState: \_ -> { forkId: Nothing , showCopySucceeded: Nothing }
444449 , render
445450 , eval: H .mkEval $ H .defaultEval
446451 { handleAction = handleAction
447452 }
448453 }
449454 where
450- handleAction :: Unit -> H.HalogenM Int Unit () o Aff Unit
455+ handleAction :: Unit -> H.HalogenM ShareButtonState Unit () o Aff Unit
451456 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 .gets _.forkId >>= traverse_ H .kill
458+ url <- H .liftEffect $ window >>= location >>= href
459+ copySucceeded <- H .liftAff $ makeAff \f -> do
460+ runEffectFn3 copyToClipboard url (f (Right true )) (f (Right false ))
461+ mempty
462+ H .modify_ _ { showCopySucceeded = Just copySucceeded }
463+ forkId <- H .fork do
457464 H .liftAff $ delay (1_500 .0 # Milliseconds )
458- H .modify_ (_ - 1 )
459- render :: Int -> H.ComponentHTML Unit () Aff
460- render n =
465+ H .modify_ _ { showCopySucceeded = Nothing }
466+ H .modify_ _ { forkId = Just forkId }
467+ render :: ShareButtonState -> H.ComponentHTML Unit () Aff
468+ render { showCopySucceeded } = do
469+ let
470+ message = case showCopySucceeded of
471+ Just true -> " ️✅ Copied to clipboard"
472+ Just false -> " ️❌ Failed to copy"
473+ Nothing -> " Share URL"
461474 HH .li
462475 [ HP .class_ $ HH.ClassName " menu-item no-mobile" ]
463476 [ HH .label
464477 [ HP .id " share_label"
465478 , HP .title " Share URL"
466479 , HE .onClick \_ -> unit
467480 ]
468- [ HH .text ( if n > 0 then " ✔️ Copied to clipboard " else " Share URL " ) ]
481+ [ HH .text message ]
469482 ]
470483
471484menuRadio
0 commit comments