2525
2626import * as React from 'react' ;
2727import { Dispatcher , Cmd , Sub , noCmd , nothing , just , Maybe } from 'tea-cup-core' ;
28- import { DocumentEvents , WindowEvents } from " react-tea-cup" ;
28+ import { DocumentEvents , WindowEvents } from ' react-tea-cup' ;
2929
3030export type Model = {
31- clicked : Maybe < MousePosition >
32- moved : Maybe < MousePosition >
33- scrolled : Maybe < Position >
34- nbResizeLeft : number
35- nbResizeRight : number
31+ clicked : Maybe < MousePosition > ;
32+ moved : Maybe < MousePosition > ;
33+ scrolled : Maybe < Position > ;
34+ nbResizeLeft : number ;
35+ nbResizeRight : number ;
3636} ;
3737
3838type MousePosition = {
3939 pos : Position ;
4040 page : Position ;
4141 offset : Position ;
42- }
42+ } ;
4343
44- type Position = [ number , number ]
44+ type Position = [ number , number ] ;
4545
46- export type Msg = {
47- type : 'clicked' ,
48- position : MousePosition
49- } | {
50- type : 'moved' ,
51- position : MousePosition
52- } | {
53- type : 'resized' ,
54- left : boolean
55- } | {
56- type : 'scrolled' ,
57- scroll : Position
58- } ;
46+ export type Msg =
47+ | {
48+ type : 'clicked' ;
49+ position : MousePosition ;
50+ }
51+ | {
52+ type : 'moved' ;
53+ position : MousePosition ;
54+ }
55+ | {
56+ type : 'resized' ;
57+ left : boolean ;
58+ }
59+ | {
60+ type : 'scrolled' ;
61+ scroll : Position ;
62+ } ;
5963
6064export function init ( ) : [ Model , Cmd < Msg > ] {
6165 return noCmd ( {
@@ -70,18 +74,9 @@ export function init(): [Model, Cmd<Msg>] {
7074export function view ( dispatch : Dispatcher < Msg > , model : Model ) {
7175 return (
7276 < div className = "events" >
73- { model . clicked
74- . map ( viewMousePosition ( 'Clicked' ) )
75- . withDefault ( < div > Waiting for click ...</ div > )
76- }
77- { model . moved
78- . map ( viewMousePosition ( 'Moved' ) )
79- . withDefault ( < div > Waiting for move ...</ div > )
80- }
81- { model . scrolled
82- . map ( viewPosition ( 'Scrolled' ) )
83- . withDefault ( < div > Waiting for move ...</ div > )
84- }
77+ { model . clicked . map ( viewMousePosition ( 'Clicked' ) ) . withDefault ( < div > Waiting for click ...</ div > ) }
78+ { model . moved . map ( viewMousePosition ( 'Moved' ) ) . withDefault ( < div > Waiting for move ...</ div > ) }
79+ { model . scrolled . map ( viewPosition ( 'Scrolled' ) ) . withDefault ( < div > Waiting for move ...</ div > ) }
8580 < div > resized left : { model . nbResizeLeft } </ div >
8681 < div > resized right : { model . nbResizeRight } </ div >
8782 </ div >
@@ -93,21 +88,21 @@ export function update(msg: Msg, model: Model): [Model, Cmd<Msg>] {
9388 case 'clicked' : {
9489 const model1 : Model = {
9590 ...model ,
96- clicked : just ( msg . position )
91+ clicked : just ( msg . position ) ,
9792 } ;
9893 return [ model1 , Cmd . none ( ) ] ;
9994 }
10095 case 'moved' : {
10196 const model1 : Model = {
10297 ...model ,
103- moved : just ( msg . position )
98+ moved : just ( msg . position ) ,
10499 } ;
105100 return [ model1 , Cmd . none ( ) ] ;
106101 }
107102 case 'scrolled' : {
108103 const model1 : Model = {
109104 ...model ,
110- scrolled : just ( msg . scroll )
105+ scrolled : just ( msg . scroll ) ,
111106 } ;
112107 return [ model1 , Cmd . none ( ) ] ;
113108 }
@@ -126,55 +121,62 @@ const windowEvents = new WindowEvents<Msg>();
126121
127122export function subscriptions ( model : Model ) : Sub < Msg > {
128123 return Sub . batch ( [
129- documentEvents . on ( 'click' , ( e : MouseEvent ) => (
130- {
131- type : 'clicked' ,
132- position : {
133- pos : [ e . x , e . y ] ,
134- page : [ e . pageX , e . pageY ] ,
135- offset : [ e . offsetX , e . offsetY ]
136- }
137- } as Msg
138- ) ) ,
139- documentEvents . on ( 'mousemove' , ( e : MouseEvent ) => ( {
140- type : 'moved' ,
141- position : {
142- pos : [ e . x , e . y ] ,
143- page : [ e . pageX , e . pageY ] ,
144- offset : [ e . offsetX , e . offsetY ]
145- }
146- } as Msg ) ) ,
124+ documentEvents . on (
125+ 'click' ,
126+ ( e : MouseEvent ) =>
127+ ( {
128+ type : 'clicked' ,
129+ position : {
130+ pos : [ e . x , e . y ] ,
131+ page : [ e . pageX , e . pageY ] ,
132+ offset : [ e . offsetX , e . offsetY ] ,
133+ } ,
134+ } as Msg ) ,
135+ ) ,
136+ documentEvents . on (
137+ 'mousemove' ,
138+ ( e : MouseEvent ) =>
139+ ( {
140+ type : 'moved' ,
141+ position : {
142+ pos : [ e . x , e . y ] ,
143+ page : [ e . pageX , e . pageY ] ,
144+ offset : [ e . offsetX , e . offsetY ] ,
145+ } ,
146+ } as Msg ) ,
147+ ) ,
147148 windowEvents . on ( 'scroll' , ( e : Event ) => {
148149 return {
149150 type : 'scrolled' ,
150- scroll : [ window . scrollX , window . scrollY ]
151+ scroll : [ window . scrollX , window . scrollY ] ,
151152 } as Msg ;
152153 } ) ,
153154 windowEvents . on ( 'resize' , ( ) => {
154155 return {
155156 type : 'resized' ,
156- left : true
157+ left : true ,
157158 } as Msg ;
158159 } ) ,
159160 windowEvents . on ( 'resize' , ( ) => {
160161 return {
161162 type : 'resized' ,
162- left : false
163+ left : false ,
163164 } as Msg ;
164165 } ) ,
165166 ] ) ;
166167}
167168
168169function viewMousePosition ( title : string ) {
169170 return ( position : MousePosition ) => {
170- return ( < div >
171- < b > { title } : </ b >
172- { viewPosition ( 'Position' ) ( position . pos ) }
173- { viewPosition ( 'Page' ) ( position . page ) }
174- { viewPosition ( 'Offset' ) ( position . offset ) }
175- </ div >
171+ return (
172+ < div >
173+ < b > { title } : </ b >
174+ { viewPosition ( 'Position' ) ( position . pos ) }
175+ { viewPosition ( 'Page' ) ( position . page ) }
176+ { viewPosition ( 'Offset' ) ( position . offset ) }
177+ </ div >
176178 ) ;
177- }
179+ } ;
178180}
179181
180182function viewPosition ( title : string ) {
@@ -184,5 +186,5 @@ function viewPosition(title: string) {
184186 { title } { position [ 0 ] } ,{ position [ 1 ] }
185187 </ >
186188 ) ;
187- }
189+ } ;
188190}
0 commit comments