-
Notifications
You must be signed in to change notification settings - Fork 0
/
Update.elm
169 lines (146 loc) · 5.89 KB
/
Update.elm
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module Update
exposing
( update
, subscriptions
)
import Msg exposing (..)
import Model exposing (..)
import Expr
import Block
import Mouse
import List.Zipper as Zipper exposing (Zipper)
subscriptions model =
case model.dragging of
Nothing ->
Sub.none
Just _ ->
Sub.batch
[ Mouse.moves DragAt
, Mouse.ups DragEnd
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LitEdit idxs str ->
{ model
| draftExpr =
Maybe.map
(Expr.updateAt idxs
(\e ->
case e of
Expr.Lit _ ->
case String.toInt str of
Ok x ->
Expr.Lit x
Err _ ->
e
_ ->
Debug.crash "Not a literal"
)
)
model.draftExpr
}
! []
DragStart id pos ->
{ model
| dragging =
Just
{ itemId = Debug.log "drag-start: " id
, confirmed = False
, startPos = pos
, currentPos = pos
}
}
! []
-- [hack] needed to intercept parents mousedown event
HoleMouseDown ->
model ! []
DragAt pos ->
let
dist pos1 pos2 =
(abs (pos1.x - pos2.x)) ^ 2 + (abs (pos1.y - pos2.y)) ^ 2
-- [q] in pixels?
threshold =
10
in
{ model
| dragging =
Maybe.map
(\d ->
{ d
| confirmed =
d.confirmed
|| dist d.currentPos d.startPos
> threshold
, currentPos = pos
}
)
model.dragging
}
! []
DragEnd pos ->
let
updExpr =
case model.dragging |> Maybe.map .confirmed of
Just True ->
Maybe.map
(\e ->
case Maybe.map2 ((,)) model.dragging model.hover of
Nothing ->
e
Just ( { itemId }, hoverIdxs ) ->
case itemId of
DraftItem dragIdxs ->
e
-- [note] must remove first!
|> Expr.removeAt dragIdxs
|> (case Expr.exprAt dragIdxs e of
Just dragExpr ->
Expr.setAt hoverIdxs dragExpr
Nothing ->
Debug.crash
("Cannot find expr at index: "
++ toString dragIdxs
)
)
LibItem f ->
e
|> Expr.setAt hoverIdxs
(Block.toExpr f
(getBlock model.blockData f)
)
LibLiteral ->
e
|> Expr.setAt hoverIdxs (Expr.Lit 0)
)
_ ->
identity
in
{ model
| dragging = Nothing
, draftExpr = updExpr model.draftExpr
}
! []
MouseOver idxs ->
{ model | hover = Just idxs }
! Debug.log ("hover: " ++ toString idxs) []
MouseLeave idxs ->
{ model | hover = Nothing } ! []
Reduce idxs ->
{ model
| eval =
model.draftExpr
|> Maybe.andThen
(Block.evalCBVAt idxs
(mkGetDef model.defs)
>> Zipper.fromList
)
|> Maybe.map Zipper.last
}
! []
StepTo frames ->
case model.eval of
Just _ ->
{ model | eval = Just frames } ! []
Nothing ->
Debug.crash "Stepping when not showing eval view"