-
Notifications
You must be signed in to change notification settings - Fork 1
/
reducers.js
346 lines (338 loc) · 14.6 KB
/
reducers.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* @flow
*/
import {
ToastAndroid
} from 'react-native';
import {emptyIdPath} from './ExprPaths';
import {
canStepUserExpr, evaluateUserExpr, expandAllDefinitions, expressionSize,
} from './UserExpressionEvaluator';
import type {
Action,
CanvasExpression,
CanvasPoint,
DragData,
ScreenPoint,
State,
UserExpression,
} from './types'
import * as t from './types'
import {IMap, ISet} from './types-collections'
import {isGiantNumber, tryUserExpressionForNumber} from './ExpressionNumbers';
import {
addExpression,
decomposeExpression,
insertAsArg,
insertAsBody,
updateExprContainer,
} from './ExpressionState'
import {resolveDrop, resolveTouch} from './HitTester'
import {screenPtToCanvasPt} from './PointConversion'
import {deserialize} from './types-lib'
import {getPositionOnScreen} from './ViewTracker';
const initialState: State = t.State.make(
IMap.make(),
0,
IMap.make(),
IMap.make(),
IMap.make(),
IMap.make(),
IMap.make(),
t.CanvasPoint.make(0, 0),
ISet.make(),
ISet.make(),
ISet.make(),
false,
'lambda',
false,
);
// TODO: Consider adding a top-level try/catch.
const playgroundApp = (state: State = initialState, rawAction: any): State => {
// Despite our action union, there are some internal redux actions that
// start with @@, which we want to just ignore.
if (rawAction.type.startsWith('@@')) {
return state;
}
const action: Action = deserialize(rawAction);
const exprWithId = (exprId: number): CanvasExpression => {
const result = state.canvasExpressions.get(exprId);
if (!result) {
throw new Error('Expected expression with ID ' + exprId);
}
return result;
};
return action.match({
reset: () => initialState,
addExpression: ({canvasExpr}) => addExpression(state, canvasExpr),
toggleLambdaPalette: () => state.updatePaletteState(
(s) => s === 'lambda' ? 'none' : 'lambda'),
toggleDefinitionPalette: () => state.updatePaletteState(
(s) => s === 'definition' ? 'none' : 'definition'),
placeDefinition: ({defName, screenPos}) => {
let newDef: ?UserExpression;
if (!state.definitions.hasKey(defName) && state.isAutomaticNumbersEnabled) {
if (isGiantNumber(defName)) {
ToastAndroid.show(
'That definition is too big to show!.', ToastAndroid.SHORT);
return state;
}
newDef = tryUserExpressionForNumber(defName);
}
if (newDef == null) {
newDef = state.definitions.get(defName);
}
if (newDef != null) {
ToastAndroid.show('Showing existing definition.', ToastAndroid.SHORT);
}
return state
.lens().canvasDefinitions().atKey(defName).replace(
screenPtToCanvasPt(state, screenPos))
// Create an entry for the definition, which may be null.
.lens().definitions().atKey(defName).replace(newDef);
},
deleteDefinition: ({defName}) =>
state.lens().definitions().deleteKey(defName)
.lens().canvasDefinitions().deleteKey(defName),
moveExpression: ({exprId, pos}) => {
return state
.lens().canvasExpressions().atKey(exprId).pos().replace(pos);
},
decomposeExpressionAction: ({path: {container, pathSteps}, targetPos}) => {
let extracted = null;
state = updateExprContainer(state, container, expr => {
const decomposed = decomposeExpression(expr, pathSteps);
extracted = decomposed.extracted;
return decomposed.original;
});
if (extracted == null) {
throw new Error('Expected extracted to be set.');
}
state = addExpression(
state, t.CanvasExpression.make(extracted, targetPos));
return state;
},
insertAsArg: ({argExprId, path: {container, pathSteps}}) => {
state = updateExprContainer(state, container, expr => {
const argCanvasExpr = exprWithId(argExprId);
return insertAsArg(expr, argCanvasExpr.expr, pathSteps);
});
state = state.lens().canvasExpressions().deleteKey(argExprId);
return state;
},
insertAsBody: ({bodyExprId, path: {container, pathSteps}}) => {
state = updateExprContainer(state, container, expr => {
const bodyCanvasExpr = exprWithId(bodyExprId);
return insertAsBody(expr, bodyCanvasExpr.expr, pathSteps);
});
state = state.lens().canvasExpressions().deleteKey(bodyExprId);
return state;
},
evaluateExpression: ({exprId}) => {
const definitions = expandAllDefinitions(
state.definitions, state.isAutomaticNumbersEnabled);
const existingExpr = exprWithId(exprId);
if (!canStepUserExpr(
definitions, state.isAutomaticNumbersEnabled, existingExpr.expr)) {
return state;
}
const evaluatedExpr = evaluateUserExpr(
definitions, state.isAutomaticNumbersEnabled, existingExpr.expr);
if (!evaluatedExpr) {
ToastAndroid.show('Evaluation took too long.', ToastAndroid.SHORT);
return state;
}
if (expressionSize(evaluatedExpr) > 30) {
ToastAndroid.show(
'The result is too big to fit. Double-check your work!',
ToastAndroid.SHORT);
return state;
}
// We don't have enough information to place the expression yet,
// since we don't know how big it is. Instead, place it in a list to
// be measured, and complete the operation when placePendingResult
// is triggered.
const pendingResultId = state.nextExprId;
return state
.lens().pendingResults().atKey(pendingResultId).
replace(t.PendingResult.make(evaluatedExpr, exprId))
.withNextExprId(pendingResultId + 1);
},
placePendingResult: ({exprId, width}) => {
const pendingResult = state.pendingResults.get(exprId);
if (pendingResult == null) {
return state;
}
const {expr, sourceExprId} = pendingResult;
const resultScreenPos = computeResultPos(sourceExprId, width);
const resultCanvasPos = screenPtToCanvasPt(state, resultScreenPos);
state = state.lens().pendingResults().deleteKey(exprId);
return addExpression(state, t.CanvasExpression.make(expr, resultCanvasPos));
},
fingerDown: ({fingerId, screenPos}) => {
const dragResult = resolveTouch(state, screenPos);
state = dragResult.match({
pickUpExpression: ({exprId, offset, screenRect}) => {
const expr = exprWithId(exprId).expr;
return state
.lens().canvasExpressions().deleteKey(exprId)
.lens().activeDrags().atKey(fingerId).replace(
t.DragData.make(
t.DraggedExpression.make(expr),
offset, screenRect));
},
pickUpDefinition: ({defName, offset, screenRect}) => {
return state
.lens().canvasDefinitions().deleteKey(defName)
.lens().activeDrags().atKey(fingerId).replace(
t.DragData.make(
t.DraggedDefinition.make(defName),
offset, screenRect));
},
extractDefinition: ({defName, offset, screenRect}) => {
const expr = state.definitions.get(defName);
if (expr == null) {
throw new Error('Expected expression to extract.');
}
return state
.lens().definitions().atKey(defName).replace(null)
.lens().activeDrags().atKey(fingerId).replace(
t.DragData.make(
t.DraggedExpression.make(expr),
offset, screenRect));
},
decomposeExpression: ({exprPath, offset, screenRect}) => {
let extracted;
state = updateExprContainer(state, exprPath.container, expr => {
const decomposed = decomposeExpression(expr, exprPath.pathSteps);
extracted = decomposed.extracted;
return decomposed.original;
});
if (extracted == null) {
throw new Error('Expected extracted to be set.');
}
return state.lens().activeDrags().atKey(fingerId).replace(
t.DragData.make(
t.DraggedExpression.make(extracted),
offset, screenRect));
},
createExpression: ({expr, offset, screenRect}) => {
return state.updateActiveDrags(drags =>
drags.set(fingerId,
t.DragData.make(
t.DraggedExpression.make(expr),
offset, screenRect)));
},
startPan: () => {
if (state.activePan.isEmpty()) {
const canvasGrabPoint = screenPtToCanvasPt(state, screenPos);
return state.lens()
.activePan().atKey(fingerId).replace(canvasGrabPoint);
} else {
return state;
}
},
});
return computeHighlights(state);
},
fingerMove: ({fingerId, screenPos}) => {
if (state.activePan.hasKey(fingerId)) {
const grabCanvasPos = state.activePan.get(fingerId);
return state.withPanOffset(
grabCanvasPos.minusDiff(screenPos.asDiff()));
}
const dragData: ?DragData = state.activeDrags.get(fingerId);
if (!dragData) {
return state;
}
const {grabOffset, screenRect} = dragData;
const oldGrabPoint = screenRect.topLeft.plusDiff(grabOffset);
const shiftAmount = screenPos.minus(oldGrabPoint);
const newScreenRect = screenRect.plusDiff(shiftAmount);
state = state.lens().activeDrags().atKey(fingerId).screenRect()
.replace(newScreenRect);
return computeHighlights(state);
},
fingerUp: ({fingerId, screenPos}) => {
if (state.activePan.hasKey(fingerId)) {
return state.lens().activePan().deleteKey(fingerId);
}
const dragData: ?DragData = state.activeDrags.get(fingerId);
if (!dragData) {
return state;
}
const dropResult = resolveDrop(state, dragData, screenPos);
state = state.lens().activeDrags().deleteKey(fingerId);
state = dropResult.match({
addToTopLevelResult: ({payload, screenPos}) => {
const canvasPos = screenPtToCanvasPt(state, screenPos);
return payload.match({
draggedExpression: ({userExpr}) =>
addExpression(state,
t.CanvasExpression.make(userExpr, canvasPos)),
draggedDefinition: ({defName}) =>
state.lens().canvasDefinitions().atKey(defName)
.replace(canvasPos),
});
},
insertAsBodyResult: ({lambdaPath: {container, pathSteps}, expr}) =>
updateExprContainer(state, container, targetExpr =>
insertAsBody(targetExpr, expr, pathSteps)
),
insertAsArgResult: ({path: {container, pathSteps}, expr}) =>
updateExprContainer(state, container, targetExpr =>
insertAsArg(targetExpr, expr, pathSteps)
),
insertAsDefinitionResult: ({defName, expr}) =>
state.lens().definitions().atKey(defName).replace(expr),
removeResult: () => {
// Do nothing; we've already removed the expression.
return state;
},
removeWithDeleteBarResult: () => state,
});
return computeHighlights(state);
},
toggleAutomaticNumbers: () =>
state.lens().isAutomaticNumbersEnabled().update((enabled) => !enabled),
});
};
const computeResultPos = (sourceExprId: number, width: number):
ScreenPoint => {
const sourceExprKey = t.ExpressionKey.make(emptyIdPath(sourceExprId));
const sourceRect = getPositionOnScreen(sourceExprKey);
if (sourceRect == null) {
return t.CanvasPoint.make(100, 100);
}
const midPoint = (sourceRect.topLeft.screenX + sourceRect.bottomRight.screenX) / 2;
return t.ScreenPoint.make(
midPoint - (width / 2),
sourceRect.bottomRight.screenY + 15,
)
};
const computeHighlights = (state: State): State => {
const exprPaths = [];
const emptyBodyPaths = [];
const definitions = [];
let isDeleteBarHighlighted = false;
for (let [_, dragData] of state.activeDrags) {
resolveDrop(state, dragData).match({
addToTopLevelResult: () => {},
insertAsBodyResult: ({lambdaPath}) => {
emptyBodyPaths.push(lambdaPath)
},
insertAsArgResult: ({path}) => {exprPaths.push(path)},
insertAsDefinitionResult: ({defName}) => {definitions.push(defName)},
removeResult: () => {},
removeWithDeleteBarResult: () => {
isDeleteBarHighlighted = true;
},
});
}
return state
.withIsDeleteBarHighlighted(isDeleteBarHighlighted)
.withHighlightedExprs(ISet.make(exprPaths))
.withHighlightedEmptyBodies(ISet.make(emptyBodyPaths))
.withHighlightedDefinitionBodies(ISet.make(definitions));
};
export default playgroundApp;