forked from source-academy/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayValue.tsx
More file actions
170 lines (150 loc) · 5.46 KB
/
Copy pathArrayValue.tsx
File metadata and controls
170 lines (150 loc) · 5.46 KB
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
import { KonvaEventObject } from 'konva/lib/Node';
import React from 'react';
import { Group } from 'react-konva';
import CseMachine from '../../CseMachine';
import { Config } from '../../CseMachineConfig';
import { Layout } from '../../CseMachineLayout';
import { DataArray, IHoverable, ReferenceType } from '../../CseMachineTypes';
import { isMainReference } from '../../CseMachineUtils';
import { ArrayEmptyUnit } from '../ArrayEmptyUnit';
import { ArrayUnit } from '../ArrayUnit';
import { Binding } from '../Binding';
import { Frame } from '../Frame';
import { FnValue } from './FnValue';
import { GlobalFnValue } from './GlobalFnValue';
import { Value } from './Value';
/** this class encapsulates an array value in source,
* defined as a JS array with not 2 elements */
export class ArrayValue extends Value implements IHoverable {
/** frame that encloses this array, if any */
enclosingFrame?: Frame;
/** array of units this array is made of */
units: ArrayUnit[] = [];
/** width of the array or the nested values inside the array. */
totalWidth: number = 0;
/** height of the array and nested values inside the array */
totalHeight: number = 0;
constructor(
/** underlying values this array contains */
readonly data: DataArray,
/** what this value is being referenced by */
firstReference: ReferenceType
) {
super();
Layout.memoizeValue(data, this);
this.addReference(firstReference);
}
handleNewReference(newReference: ReferenceType): void {
if (!isMainReference(this, newReference)) return;
// derive the coordinates from the main reference (binding / array unit)
if (newReference instanceof Binding) {
this.enclosingFrame = newReference.frame;
// check for whether cache already has x cooridnates
const ghostX = Layout.getGhostFrameX(newReference.frame.environment.id);
// If frame x cooridnates exists in cache, use it. Otherwise, fallback to current (live) X.
const frameX = ghostX !== undefined ? ghostX : newReference.frame.x();
this._x = frameX + newReference.frame.width() + Config.FrameMarginX;
this._y = newReference.y();
} else {
if (newReference.isLastUnit) {
this._x = newReference.x() + Config.DataUnitWidth * 2;
this._y = newReference.y();
} else {
this._x = newReference.x();
this._y = newReference.y() + newReference.parent.totalHeight + Config.DataUnitHeight;
}
}
this._width = Math.max(this.data.length * Config.DataUnitWidth, Config.DataMinWidth);
this.totalWidth = this._width;
this._height = Config.DataUnitHeight;
this.totalHeight = this._height;
this.units = new Array(this.data.length);
// initialize array units from the last index
for (let i = this.data.length - 1; i >= 0; i--) {
const unit = new ArrayUnit(i, this.data[i], this);
// Update total width and height for values that are drawn next to the array
if (
(unit.value instanceof ArrayValue ||
unit.value instanceof FnValue ||
unit.value instanceof GlobalFnValue) &&
isMainReference(unit.value, unit)
) {
const childWidth =
unit.value instanceof ArrayValue
? unit.value.totalWidth
: CseMachine.getPrintableMode()
? unit.value.totalWidth
: unit.value.width();
const bottomY =
unit.value instanceof ArrayValue
? unit.value.y() + unit.value.totalHeight
: CseMachine.getPrintableMode()
? unit.value.y() +
Config.FnRadius +
Config.TextMargin +
unit.value.printDescriptionOffsetY +
unit.value.printDescriptionHeight +
unit.value.printDescriptionBottomGap
: unit.value.y() + unit.value.height() / 2;
this.totalWidth = Math.max(
this.totalWidth,
childWidth +
(i === this.data.length - 1 ? (i + 2) * Config.DataUnitWidth : i * Config.DataUnitWidth)
);
this.totalHeight = Math.max(this.totalHeight, bottomY - unit.y());
}
this.units[i] = unit;
}
}
setArrowSourceHighlightedStyle(): void {
this.units.forEach(unit => unit.setArrowSourceHighlightedStyle());
}
setArrowSourceNormalStyle(): void {
this.units.forEach(unit => unit.setArrowSourceNormalStyle());
}
isEnclosingFrameLive(): boolean {
const id = (this.data as any).id;
return id ? Layout.liveObjectIDs.has(id) : false;
}
isLive(): boolean {
return this.isEnclosingFrameLive();
}
markAsReferenced() {
if (this.isReferenced()) return;
super.markAsReferenced();
for (const unit of this.units) {
unit.value.markAsReferenced();
}
}
onMouseEnter = (e: KonvaEventObject<MouseEvent>) => {
e.cancelBubble = true;
for (const unit of this.units) {
unit.showIndex();
}
};
onMouseLeave = (e: KonvaEventObject<MouseEvent>) => {
e.cancelBubble = true;
for (const unit of this.units) {
unit.hideIndex();
}
};
draw(): React.ReactNode {
if (Layout.clearDeadFrames && !this.isLive()) {
return null;
}
if (this.isDrawn()) return null;
this._isDrawn = true;
return (
<Group
key={Layout.key++}
ref={this.ref}
onMouseEnter={this.onMouseEnter}
onMouseLeave={this.onMouseLeave}
>
{this.units.length > 0
? this.units.map(unit => unit.draw())
: new ArrayEmptyUnit(this).draw()}
</Group>
);
}
}