@@ -2,40 +2,61 @@ module React.Basic.Hooks.UseGesture where
3import Prelude
3import Prelude
4import Data.Array ((!!))
4import Data.Array ((!!))
5−import Data.Foldable (fold)
5+import Data.Maybe (Maybe, fromMaybe')
6−import Data.Monoid.Additive (Additive(..))
7−import Data.Newtype (ala)
6+import Data.Nullable (Nullable)
8import Data.Tuple.Nested ((/\), type (/\))
7import Data.Tuple.Nested ((/\), type (/\))
8+import Debug.Trace (spy)
9import Effect (Effect)
9import Effect (Effect)
10−import Effect.Uncurried (EffectFn1, runEffectFn1)
10+import Effect.Aff.Compat (runEffectFn2)
11+import Effect.Uncurried (EffectFn2)
11import Effect.Unsafe (unsafePerformEffect)
12import Effect.Unsafe (unsafePerformEffect)
13+import Partial.Unsafe (unsafeCrashWith)
12import Prim.Row (class Lacks, class Nub)
14import Prim.Row (class Lacks, class Nub)
13import React.Basic.Events (EventHandler)
15import React.Basic.Events (EventHandler)
16+import React.Basic.Helpers (orUndefined)
14−import React.Basic.Hooks (Hook, unsafeHook)
17+import React.Basic.Hooks (Hook, Ref, unsafeHook)
15import Record (disjointUnion)
18import Record (disjointUnion)
16import Yoga.Helpers ((?||))
20import Yoga.Helpers ((?||))
18foreign import data UseDrag ∷ Type -> Type -> Type
22foreign import data UseDrag ∷ Type -> Type -> Type
20−type DragHandler
24+type DragHandler a
21− = { down ∷ Boolean, movement ∷ Number /\ Number } -> Effect Unit
25+ = { arg ∷ a, down ∷ Boolean, movement ∷ Number /\ Number } -> Effect Unit
23−type DragHandlerImpl
27+type DragHandlerImpl a
24− = { down ∷ Boolean, movement ∷ Array Number } -> Unit
28+ = { args ∷ Array a, down ∷ Boolean, movement ∷ Array Number } -> Unit
26type DragProps
30type DragProps
27 = { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler }
31 = { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler }
29−foreign import useDragImpl ∷ EffectFn1 DragHandlerImpl (Effect DragProps)
33+type GenericOptionsImpl r
34+ = { domTarget ∷ Ref (Nullable Node) | r }
37+ = { domTarget ∷ Maybe (Ref (Nullable Node)) | r }
43+ = GenericOptionsImpl ()
45+dragOptionsToImpl ∷ DragOptions -> DragOptionsImpl
46+dragOptionsToImpl { domTarget } = { domTarget: domTarget # orUndefined }
48+foreign import useDragImpl ∷ ∀ a. EffectFn2 (DragHandlerImpl a) DragOptionsImpl (a -> DragProps)
31−useDrag ∷ DragHandler -> Hook (UseDrag Unit) (Effect DragProps)
50+useDrag ∷ ∀ a. DragOptions -> DragHandler a -> Hook (UseDrag Unit) (a -> DragProps)
32−useDrag dragHandler = unsafeHook (runEffectFn1 useDragImpl f)
51+useDrag dragOptions dragHandler = unsafeHook (runEffectFn2 useDragImpl dragHandlerImpl (dragOptionsToImpl dragOptions))
34− f x =
53+ dragHandlerImpl ∷ DragHandlerImpl a
35 (unsafePerformEffect <<< dragHandler)
55 (unsafePerformEffect <<< dragHandler)
36− { down: x.down, movement: mx /\ my
56+ { arg, down: x.down, movement: mx /\ my
59+ arg = x.args !! 0 # fromMaybe' (\_ -> unsafeCrashWith "Bollox")
39 mx = x.movement !! 0 ?|| 0.0
60 mx = x.movement !! 0 ?|| 0.0
40 my = x.movement !! 1 ?|| 0.0
61 my = x.movement !! 1 ?|| 0.0
@@ -47,6 +68,6 @@ withDragProps ∷
47 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs )
68 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs )
48 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs ) =>
69 ( onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs ) =>
49 { | attrs } ->
70 { | attrs } ->
50− Effect DragProps ->
71+ DragProps ->
51 { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs }
72 { onMouseDown ∷ EventHandler, onTouchStart ∷ EventHandler | attrs }
52−withDragProps x (y ∷ Effect DragProps) = disjointUnion (unsafePerformEffect y) x
73+withDragProps x (y ∷ DragProps) = disjointUnion y x