-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateDisplayState.js
220 lines (206 loc) · 8.15 KB
/
generateDisplayState.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
/**
* @flow
*/
import {PALLETE_VAR_NAMES} from './constants';
import {isNumber} from './ExpressionNumbers';
import {emptyIdPath, emptyDefinitionPath, step} from './ExprPaths'
import {canvasPtToScreenPt} from './PointConversion'
import store from './store'
import * as t from './types'
import type {
DisplayExpression,
DisplayState,
Expression,
ExprPath,
MeasureRequest,
ScreenDefinition,
ScreenExpression,
State,
UserExpression,
} from './types'
import {IList, IMap, ISet} from './types-collections'
import {canStepUserExpr, expandAllDefinitions} from './UserExpressionEvaluator'
const executeHandler = (exprId) => {
return () => {
store.dispatch(t.EvaluateExpression.make(exprId));
};
};
const generateDisplayState = (state: State): DisplayState => {
const screenExpressions: Array<ScreenExpression> = [];
const screenDefinitions: Array<ScreenDefinition> = [];
const {
highlightedExprs, highlightedEmptyBodies, highlightedDefinitionBodies,
isAutomaticNumbersEnabled,
} = state;
const definitionNames = IList.make(state.definitions.keys()).sort();
const definitions = expandAllDefinitions(
state.definitions, state.isAutomaticNumbersEnabled);
for (let [exprId, canvasExpr] of state.canvasExpressions) {
const rootPath = emptyIdPath(exprId);
const displayExpr = buildDisplayExpression(
canvasExpr.expr, rootPath, highlightedExprs,
highlightedEmptyBodies, definitions, isAutomaticNumbersEnabled);
const isDragging = false;
const isExecutable = canStepUserExpr(
definitions, state.isAutomaticNumbersEnabled, canvasExpr.expr);
screenExpressions.push(t.ScreenExpression.make(
displayExpr,
canvasPtToScreenPt(state, canvasExpr.pos),
'expr' + exprId,
isDragging,
isExecutable ? executeHandler(exprId) : null,
));
}
for (let [fingerId, dragData] of state.activeDrags) {
const payload = dragData.payload;
payload.match({
draggedExpression: ({userExpr}) => {
const displayExpr = buildDisplayExpression(
userExpr, null, highlightedExprs, highlightedEmptyBodies,
definitions, isAutomaticNumbersEnabled);
const isDragging = true;
const executeHandler = null;
screenExpressions.push(t.ScreenExpression.make(
displayExpr,
dragData.screenRect.topLeft,
'dragExpr' + fingerId,
isDragging,
executeHandler,
));
},
draggedDefinition: ({defName}) => {
const userExpr = state.definitions.get(defName);
const isDragging = true;
let displayExpr = null;
if (userExpr != null) {
displayExpr = buildDisplayExpression(
userExpr, null, highlightedExprs,
highlightedEmptyBodies, definitions,
isAutomaticNumbersEnabled);
}
screenDefinitions.push(t.ScreenDefinition.make(
defName,
displayExpr,
dragData.screenRect.topLeft,
null,
null,
null,
false,
'dragDef' + fingerId,
isDragging,
))
}
});
}
for (let [defName, canvasPoint] of state.canvasDefinitions) {
const userExpr = state.definitions.get(defName);
const isDragging = false;
let displayExpr = null;
if (userExpr != null) {
const rootPath = emptyDefinitionPath(defName);
displayExpr = buildDisplayExpression(
userExpr, rootPath, highlightedExprs, highlightedEmptyBodies,
definitions, isAutomaticNumbersEnabled);
}
const shouldHighlightEmptyBody = highlightedDefinitionBodies.has(defName);
screenDefinitions.push(t.ScreenDefinition.make(
defName,
displayExpr,
canvasPtToScreenPt(state, canvasPoint),
t.DefinitionKey.make(defName),
t.DefinitionRefKey.make(defName),
userExpr ? null : t.DefinitionEmptyBodyKey.make(defName),
shouldHighlightEmptyBody,
'def' + defName,
isDragging,
))
}
const measureRequests: Array<MeasureRequest> = [];
for (let [exprId, pendingResult] of state.pendingResults) {
const displayExpr = buildDisplayExpression(
pendingResult.expr, null, highlightedExprs, highlightedEmptyBodies,
definitions, isAutomaticNumbersEnabled);
measureRequests.push(t.MeasureRequest.make(
displayExpr,
(width, height) => {
store.dispatch(t.PlacePendingResult.make(exprId, width, height));
}
))
}
const paletteLambdas = IList.make(PALLETE_VAR_NAMES);
const paletteDefNames = IList.make(state.definitions.keys()).sort();
let isDragging = false;
let isDraggingExpression = false;
for (const [_, dragData] of state.activeDrags) {
isDragging = true;
isDraggingExpression = (
isDraggingExpression ||
dragData.payload instanceof t.DraggedExpression);
}
return t.DisplayState.make(
IList.make(screenExpressions),
IList.make(screenDefinitions),
t.PaletteDisplayState.make(
state.paletteState,
paletteLambdas,
paletteDefNames,
),
IList.make(measureRequests),
definitionNames,
isDragging,
isDraggingExpression,
state.isDeleteBarHighlighted,
state.isAutomaticNumbersEnabled,
);
};
/**
* Build a DisplayExpression with paths for the given expression ID. If exprId
* is null, no paths are attached.
*/
const buildDisplayExpression = (
userExpr: UserExpression, rootPath: ?ExprPath,
highlightedExprs: ISet<ExprPath>,
highlightedEmptyBodies: ISet<ExprPath>,
definitions: IMap<string, ?Expression>,
isAutomaticNumbersEnabled: boolean): DisplayExpression => {
const rec = (expr: UserExpression, path: ?ExprPath): DisplayExpression => {
const exprKey = path && t.ExpressionKey.make(path);
const shouldHighlight = path != null && highlightedExprs.has(path);
return expr.match({
userLambda: ({varName, body}) => {
const varKey = path && t.LambdaVarKey.make(path);
const emptyBodyKey = path && t.EmptyBodyKey.make(path);
if (body) {
const displayBody = rec(body, path && step(path, 'body'));
return t.DisplayLambda.make(
exprKey, shouldHighlight, varKey, null, false, varName,
displayBody);
} else {
const highlightBody = path != null &&
highlightedEmptyBodies.has(path);
return t.DisplayLambda.make(
exprKey, shouldHighlight, varKey, emptyBodyKey,
highlightBody, varName, null);
}
},
userFuncCall: ({func, arg}) => {
const displayFunc = rec(func, path && step(path, 'func'));
const displayArg = rec(arg, path && step(path, 'arg'));
return t.DisplayFuncCall.make(
exprKey, shouldHighlight, displayFunc, displayArg);
},
userVariable: ({varName}) => {
return t.DisplayVariable.make(exprKey, shouldHighlight, varName);
},
userReference: ({defName}) => {
const shouldShowError = !definitions.get(defName) &&
!(isAutomaticNumbersEnabled && isNumber(defName));
return t.DisplayReference.make(
exprKey, shouldHighlight, shouldShowError, defName
);
},
});
};
return rec(userExpr, rootPath);
};
export default generateDisplayState;