From 156decc4c7981b8efcce33d2383169ac63b28ca2 Mon Sep 17 00:00:00 2001 From: kritzcreek Date: Tue, 2 Jan 2018 11:09:21 +0100 Subject: [PATCH 1/3] adds a function to copy text from an existing element on the page --- example/index.html | 6 ++++++ example/src/Main.purs | 23 +++++++++++++++-------- src/Clipboard.js | 11 +++++++++++ src/Clipboard.purs | 9 +++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/example/index.html b/example/index.html index 129b065..65c26ed 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..da8937e 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..2a0d8b9 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 From a34342ce4e36969cf104125d3dfb40ad85a953a1 Mon Sep 17 00:00:00 2001 From: kritzcreek Date: Tue, 2 Jan 2018 11:24:34 +0100 Subject: [PATCH 2/3] fixes spacing --- example/index.html | 8 ++++---- src/Clipboard.js | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/example/index.html b/example/index.html index 65c26ed..899148d 100644 --- a/example/index.html +++ b/example/index.html @@ -18,10 +18,10 @@ Copy
- - + +
diff --git a/src/Clipboard.js b/src/Clipboard.js index da8937e..2ab631d 100644 --- a/src/Clipboard.js +++ b/src/Clipboard.js @@ -26,14 +26,14 @@ exports.fromStringSelector = makeFromX(function makeFromX$fromStringSelector (ef }; }); -exports.fromElementWithTarget = function (el){ - return function(targetSelector) { - return function() { - return new Clipboard(el, { - target: targetSelector - }); - }; +exports.fromElementWithTarget = function (el) { + return function(targetSelector) { + return function() { + return new Clipboard(el, { + target: targetSelector + }); }; + }; }; From 1b768af04c2c04dc6a15dbbe3de4f30369ba3331 Mon Sep 17 00:00:00 2001 From: kritzcreek Date: Tue, 2 Jan 2018 12:02:45 +0100 Subject: [PATCH 3/3] more spacing, for consistency --- src/Clipboard.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Clipboard.purs b/src/Clipboard.purs index 2a0d8b9..233f34d 100644 --- a/src/Clipboard.purs +++ b/src/Clipboard.purs @@ -38,7 +38,7 @@ foreign import fromStringSelector -- | copies the text inside the returned element to the clipboard. foreign import fromElementWithTarget :: forall eff - . Element + . Element -> Eff (dom :: DOM | eff) Element -> Eff (dom :: DOM | eff) Clipboard