Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 21e83db

Browse files
authored
Merge pull request #4 from kRITZCREEK/copy-from-target
Adds a function to copy text from an existing element on the page
2 parents 86bc357 + 1b768af commit 21e83db

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

example/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<button class="test-selector" type="button" data-copy-text="From test-selector child#2!">
1818
Copy
1919
</button>
20+
<div>
21+
<input id="input-selector" type="text" value="Copy me!" />
22+
<button id="input-button-selector" type="button">
23+
Copy Text in Input
24+
</button>
25+
</div>
2026
</form>
2127
</body>
2228
</html>

example/src/Main.purs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@ module Main where
22

33
import Prelude
44

5+
import CSS (Selector, fromString)
6+
import Clipboard as C
57
import Control.Monad.Eff (Eff)
6-
7-
import Data.Maybe (fromMaybe)
8-
98
import DOM (DOM)
10-
import CSS (Selector, fromString)
119
import DOM.Event.EventTarget (addEventListener, eventListener)
1210
import DOM.HTML (window)
1311
import DOM.HTML.Event.EventTypes (load)
1412
import DOM.HTML.Types (windowToEventTarget, htmlDocumentToDocument)
1513
import DOM.HTML.Window (document)
16-
import DOM.Node.NonElementParentNode (getElementById)
17-
import DOM.Node.Types (Element, ElementId(..), documentToNonElementParentNode)
1814
import DOM.Node.Element (getAttribute)
19-
20-
import Clipboard as C
15+
import DOM.Node.NonElementParentNode (getElementById)
16+
import DOM.Node.Types (Element, ElementId(ElementId), documentToNonElementParentNode)
17+
import Data.Maybe (fromJust, fromMaybe)
18+
import Data.Newtype (wrap)
19+
import Partial.Unsafe (unsafePartial)
2120

2221
onLoad :: forall eff. (Eff (dom :: DOM | eff) Unit) -> Eff (dom :: DOM | eff) Unit
2322
onLoad action
@@ -34,10 +33,18 @@ testElement el = void $ C.fromElement el $ stringFromAttr "data-copy-text" el
3433
testSelector :: forall eff. Selector -> Eff (dom :: DOM | eff) Unit
3534
testSelector sel = void $ C.fromCSSSelector sel $ stringFromAttr "data-copy-text"
3635

36+
testInputSelector :: forall eff. Eff (dom :: DOM | eff) Unit
37+
testInputSelector = do
38+
doc <- documentToNonElementParentNode <<< htmlDocumentToDocument <$> (document =<< window)
39+
let getInput = unsafePartial fromJust <$> getElementById (wrap "input-selector") doc
40+
button <- unsafePartial fromJust <$> getElementById (wrap "input-button-selector") doc
41+
void $ C.fromElementWithTarget button getInput
42+
3743
main :: forall eff. Eff (dom :: DOM | eff) Unit
3844
main = onLoad do
3945
win <- window
4046
doc <- documentToNonElementParentNode <<< htmlDocumentToDocument <$> document win
4147
element <- getElementById (ElementId "test-element") doc
4248
fromMaybe (pure unit) $ testElement <$> element
4349
testSelector $ fromString ".test-selector"
50+
testInputSelector

src/Clipboard.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ exports.fromStringSelector = makeFromX(function makeFromX$fromStringSelector (ef
2626
};
2727
});
2828

29+
exports.fromElementWithTarget = function (el) {
30+
return function(targetSelector) {
31+
return function() {
32+
return new Clipboard(el, {
33+
target: targetSelector
34+
});
35+
};
36+
};
37+
};
38+
39+
2940
exports.destroy = function destroy (clipboard) {
3041
return function destroy$Eff () {
3142
clipboard.destroy();

src/Clipboard.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Clipboard
22
( Clipboard
33
, fromElement
44
, fromCSSSelector
5+
, fromElementWithTarget
56
, destroy
67
) where
78

@@ -33,6 +34,14 @@ foreign import fromStringSelector
3334
-> (Element -> Eff (dom :: DOM | eff) String)
3435
-> Eff (dom :: DOM | eff) Clipboard
3536

37+
-- | Registers a click handler on an Event, which triggers the passed `Eff` and
38+
-- | copies the text inside the returned element to the clipboard.
39+
foreign import fromElementWithTarget
40+
:: forall eff
41+
. Element
42+
-> Eff (dom :: DOM | eff) Element
43+
-> Eff (dom :: DOM | eff) Clipboard
44+
3645
foreign import destroy
3746
:: forall eff
3847
. Clipboard

0 commit comments

Comments
 (0)