diff --git a/example/index.html b/example/index.html index 129b065..899148d 100644 --- a/example/index.html +++ b/example/index.html @@ -17,6 +17,12 @@ +
+ + +
diff --git a/example/src/Main.purs b/example/src/Main.purs index 944b990..f572d7e 100644 --- a/example/src/Main.purs +++ b/example/src/Main.purs @@ -2,22 +2,21 @@ module Main where import Prelude +import CSS (Selector, fromString) +import Clipboard as C import Control.Monad.Eff (Eff) - -import Data.Maybe (fromMaybe) - import DOM (DOM) -import CSS (Selector, fromString) import DOM.Event.EventTarget (addEventListener, eventListener) import DOM.HTML (window) import DOM.HTML.Event.EventTypes (load) import DOM.HTML.Types (windowToEventTarget, htmlDocumentToDocument) import DOM.HTML.Window (document) -import DOM.Node.NonElementParentNode (getElementById) -import DOM.Node.Types (Element, ElementId(..), documentToNonElementParentNode) import DOM.Node.Element (getAttribute) - -import Clipboard as C +import DOM.Node.NonElementParentNode (getElementById) +import DOM.Node.Types (Element, ElementId(ElementId), documentToNonElementParentNode) +import Data.Maybe (fromJust, fromMaybe) +import Data.Newtype (wrap) +import Partial.Unsafe (unsafePartial) onLoad :: forall eff. (Eff (dom :: DOM | eff) Unit) -> Eff (dom :: DOM | eff) Unit onLoad action @@ -34,6 +33,13 @@ testElement el = void $ C.fromElement el $ stringFromAttr "data-copy-text" el testSelector :: forall eff. Selector -> Eff (dom :: DOM | eff) Unit testSelector sel = void $ C.fromCSSSelector sel $ stringFromAttr "data-copy-text" +testInputSelector :: forall eff. Eff (dom :: DOM | eff) Unit +testInputSelector = do + doc <- documentToNonElementParentNode <<< htmlDocumentToDocument <$> (document =<< window) + let getInput = unsafePartial fromJust <$> getElementById (wrap "input-selector") doc + button <- unsafePartial fromJust <$> getElementById (wrap "input-button-selector") doc + void $ C.fromElementWithTarget button getInput + main :: forall eff. Eff (dom :: DOM | eff) Unit main = onLoad do win <- window @@ -41,3 +47,4 @@ main = onLoad do element <- getElementById (ElementId "test-element") doc fromMaybe (pure unit) $ testElement <$> element testSelector $ fromString ".test-selector" + testInputSelector diff --git a/src/Clipboard.js b/src/Clipboard.js index 6f2d092..2ab631d 100644 --- a/src/Clipboard.js +++ b/src/Clipboard.js @@ -26,6 +26,17 @@ exports.fromStringSelector = makeFromX(function makeFromX$fromStringSelector (ef }; }); +exports.fromElementWithTarget = function (el) { + return function(targetSelector) { + return function() { + return new Clipboard(el, { + target: targetSelector + }); + }; + }; +}; + + exports.destroy = function destroy (clipboard) { return function destroy$Eff () { clipboard.destroy(); diff --git a/src/Clipboard.purs b/src/Clipboard.purs index c206599..233f34d 100644 --- a/src/Clipboard.purs +++ b/src/Clipboard.purs @@ -2,6 +2,7 @@ module Clipboard ( Clipboard , fromElement , fromCSSSelector + , fromElementWithTarget , destroy ) where @@ -33,6 +34,14 @@ foreign import fromStringSelector -> (Element -> Eff (dom :: DOM | eff) String) -> Eff (dom :: DOM | eff) Clipboard +-- | Registers a click handler on an Event, which triggers the passed `Eff` and +-- | copies the text inside the returned element to the clipboard. +foreign import fromElementWithTarget + :: forall eff + . Element + -> Eff (dom :: DOM | eff) Element + -> Eff (dom :: DOM | eff) Clipboard + foreign import destroy :: forall eff . Clipboard