Web applications without callbacks or side-effects. Reflex-DOM brings the power of functional reactive programming (FRP) to the web. Build HTML and other Document Object Model (DOM) data with a pure functional interface.
The function below always works on Chrome, but never on Firefox. My guess is that Firefox is stricter in its definition of a user-driven event, which is necessary to execute the copy command, and something is getting interleaved by the ghcjs runtime that violates it. It's possible that this is a ghcjs bug, but putting here for now until we've got an example that can prove it.
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
import Data.Text (Text)
import qualified GHCJS.DOM as DOM
import qualified GHCJS.DOM.Document as Document
import qualified GHCJS.DOM.HTMLTextAreaElement as TextArea
import qualified GHCJS.DOM.Node as Node
import qualified GHCJS.DOM.Types as DOM
import Language.Javascript.JSaddle (MonadJSM)
import Reflex.Dom.Core
-- | Copy the given text to the clipboard
performCopyToClipboard
:: (MonadJSM (Performable m), PerformEvent t m)
=> Event t Text
-- ^ Text to copy to clipboard. Event must come directly from user
-- interaction (e.g. domEvent Click), or the copy will not take place.
-> m (Event t Bool)
-- ^ Did the copy take place successfully?
performCopyToClipboard copyEvent = performEvent $
ffor copyEvent $ \copyText -> do
doc <- DOM.currentDocumentUnchecked
ta <- DOM.uncheckedCastTo TextArea.HTMLTextAreaElement <$> Document.createElement doc ("textarea" :: Text)
TextArea.setValue ta copyText
body <- Document.getBodyUnchecked doc
_ <- Node.appendChild body ta
TextArea.select ta
result <- Document.execCommand doc ("copy" :: Text) False (Nothing :: Maybe Text)
_ <- Node.removeChild body ta
return result
The function below always works on Chrome, but never on Firefox. My guess is that Firefox is stricter in its definition of a user-driven event, which is necessary to execute the copy command, and something is getting interleaved by the ghcjs runtime that violates it. It's possible that this is a ghcjs bug, but putting here for now until we've got an example that can prove it.