-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoughNotation.purs
61 lines (51 loc) · 2.34 KB
/
RoughNotation.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module RoughNotation
( Annotation
, animationDuration
, annotate
--, annotationGroup
, hideAnnotation
, removeAnnotation
, showAnnotation
)
where
import Prelude
import Effect (Effect)
import Effect.Aff (Aff, Canceler(..), Milliseconds(..), cancelWith, delay)
import Effect.Class (liftEffect)
import Prim.Row (class Nub, class Union)
import Record as Record
import RoughNotation.Config (Config, NativeConfig, RoughAnnotationType, ConfigRows, defaultConfig, toNativeConfig)
import Unsafe.Coerce (unsafeCoerce)
import Web.DOM (Element)
foreign import data Annotation :: Type
foreign import annotate_ :: Element -> String -> NativeConfig -> Effect Annotation
-- | Creates an annotation from a target element, an annotation type, and a config record.
-- | Note that the config record must be a subrecord of `Config` type, see README.md for more details
annotate :: forall config rest. Union config rest ConfigRows => Nub ConfigRows ConfigRows =>
Element -> RoughAnnotationType -> Record config -> Aff Annotation
annotate element roughAnnotationType config =
liftEffect (annotate_ element (show roughAnnotationType) (toNativeConfig config'))
where
config' = (Record.merge :: Record config -> Record rest -> Config) config (unsafeCoerce defaultConfig)
foreign import show_ :: forall a. a -> Effect Unit
-- | Shows annotation, blocks until annotation is completed
showAnnotation :: Annotation -> Aff Unit
showAnnotation annotation = show `cancelWith` Canceler \_ -> removeAnnotation annotation
where
show = do
Milliseconds duration <- animationDuration annotation
liftEffect (show_ annotation)
delay (Milliseconds duration)
foreign import hide_ :: forall a. a -> Effect Unit
-- | Hides annotation, annotation can be shown again
hideAnnotation :: Annotation -> Aff Unit
hideAnnotation annotation =
liftEffect (hide_ annotation) `cancelWith` Canceler \_ -> removeAnnotation annotation
foreign import remove_ :: Annotation -> Effect Unit
-- | Removes annotation, annotation cannot be run again
removeAnnotation :: Annotation -> Aff Unit
removeAnnotation = liftEffect <<< remove_
foreign import animationDuration_ :: Annotation -> Effect Milliseconds
-- | total duration of an annotation, used to block Aff while annotation is running
animationDuration :: Annotation -> Aff Milliseconds
animationDuration = liftEffect <<< animationDuration_