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

Commit

Permalink
Merge pull request #5 from slamdata/ps/0.12
Browse files Browse the repository at this point in the history
update for ps0.12
  • Loading branch information
cryogenian authored Nov 13, 2018
2 parents 21e83db + edbb689 commit ee11bad
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 63 deletions.
10 changes: 7 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
"package.json"
],
"dependencies": {
"purescript-prelude": "^3.0.0",
"purescript-dom": "^4.2.0",
"purescript-css": "^3.0.0"
"purescript-prelude": "^4.1.0",
"purescript-web-dom": "^1.0.0",
"purescript-css": "^4.0.0"
},
"devDependencies": {
"purescript-web-html": "^1.2.0",
"purescript-web-events": "^1.0.1"
}
}
58 changes: 28 additions & 30 deletions example/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,45 @@ import Prelude

import CSS (Selector, fromString)
import Clipboard as C
import Control.Monad.Eff (Eff)
import DOM (DOM)
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.Element (getAttribute)
import DOM.Node.NonElementParentNode (getElementById)
import DOM.Node.Types (Element, ElementId(ElementId), documentToNonElementParentNode)
import Data.Maybe (fromJust, fromMaybe)
import Data.Newtype (wrap)
import Effect (Effect)
import Partial.Unsafe (unsafePartial)

onLoad :: forall eff. (Eff (dom :: DOM | eff) Unit) -> Eff (dom :: DOM | eff) Unit
onLoad action
= addEventListener load (eventListener (const action)) false
<<< windowToEventTarget
=<< window

stringFromAttr :: forall eff. String -> Element -> Eff (dom :: DOM | eff) String
stringFromAttr attr el = fromMaybe "" <$> getAttribute attr el

testElement :: forall eff. Element -> Eff (dom :: DOM | eff) Unit
import Web.DOM.Element (Element)
import Web.DOM.Element as Element
import Web.DOM.NonElementParentNode (getElementById)
import Web.Event.EventTarget (addEventListener, eventListener)
import Web.HTML (window)
import Web.HTML.Event.EventTypes (load)
import Web.HTML.HTMLDocument as HTMLDocument
import Web.HTML.Window as Window

onLoad :: Effect Unit -> Effect Unit --forall eff. (Eff (dom :: DOM | eff) Unit) -> Eff (dom :: DOM | eff) Unit
onLoad action = do
listener <- eventListener $ const action
win <- map Window.toEventTarget window
addEventListener load listener false win

stringFromAttr :: String -> Element -> Effect String
stringFromAttr attr el = fromMaybe "" <$> Element.getAttribute attr el

testElement :: Element -> Effect Unit
testElement el = void $ C.fromElement el $ stringFromAttr "data-copy-text" el

testSelector :: forall eff. Selector -> Eff (dom :: DOM | eff) Unit
testSelector :: Selector -> Effect Unit
testSelector sel = void $ C.fromCSSSelector sel $ stringFromAttr "data-copy-text"

testInputSelector :: forall eff. Eff (dom :: DOM | eff) Unit
testInputSelector :: Effect 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
doc <- HTMLDocument.toNonElementParentNode <$> (Window.document =<< window)
let getInput = unsafePartial fromJust <$> getElementById "input-selector" doc
button <- unsafePartial fromJust <$> getElementById "input-button-selector" doc
void $ C.fromElementWithTarget button getInput

main :: forall eff. Eff (dom :: DOM | eff) Unit
main :: Effect Unit
main = onLoad do
win <- window
doc <- documentToNonElementParentNode <<< htmlDocumentToDocument <$> document win
element <- getElementById (ElementId "test-element") doc
doc <- HTMLDocument.toNonElementParentNode <$> Window.document win
element <- getElementById "test-element" doc
fromMaybe (pure unit) $ testElement <$> element
testSelector $ fromString ".test-selector"
testInputSelector
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "pulp build -I example/src -- --censor-lib --strict",
"build-example": "pulp browserify -I example/src --to example/example.js"
},
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "pulp build -I example/src -- --censor-lib --strict",
"build-example": "pulp browserify -I example/src --to example/example.js"
},
"devDependencies": {
"eslint": "^3.19.0",
"pulp": "^11.0.0",
"purescript": "^0.11.4",
"purescript-psa": "^0.5.1",
"pulp": "^12.2.0",
"purescript": "^0.12.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.1"
},
"dependencies": {
Expand Down
38 changes: 16 additions & 22 deletions src/Clipboard.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,35 @@ module Clipboard

import Prelude

import Control.Monad.Eff (Eff)
import DOM (DOM)
import Effect (Effect)
import CSS (Selector, selector)
import DOM.Node.Types (Element)
import Web.DOM.Element (Element)

foreign import data Clipboard :: Type

foreign import fromElement
:: forall eff
. Element
-> Eff (dom :: DOM | eff) String
-> Eff (dom :: DOM | eff) Clipboard
:: Element
-> Effect String
-> Effect Clipboard

fromCSSSelector
:: forall eff
. Selector
-> (Element -> Eff (dom :: DOM | eff) String)
-> Eff (dom :: DOM | eff) Clipboard
:: Selector
-> (Element -> Effect String)
-> Effect Clipboard
fromCSSSelector = selector >>> fromStringSelector

foreign import fromStringSelector
:: forall eff
. String
-> (Element -> Eff (dom :: DOM | eff) String)
-> Eff (dom :: DOM | eff) Clipboard
:: String
-> (Element -> Effect String)
-> Effect 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
:: Element
-> Effect Element
-> Effect Clipboard

foreign import destroy
:: forall eff
. Clipboard
-> Eff (dom :: DOM | eff) Unit
:: Clipboard
-> Effect Unit

0 comments on commit ee11bad

Please sign in to comment.