Skip to content

Commit 956f272

Browse files
authored
feat: Add a generator for all fields on a block. (#8667)
* feat: Add a generator for all fields on a block. * chore: Add docstring.
1 parent 503cd00 commit 956f272

4 files changed

Lines changed: 48 additions & 58 deletions

File tree

core/block.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,8 @@ export class Block implements IASTNodeLocation {
937937
*/
938938
setEditable(editable: boolean) {
939939
this.editable = editable;
940-
for (let i = 0, input; (input = this.inputList[i]); i++) {
941-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
942-
field.updateEditable();
943-
}
940+
for (const field of this.getFields()) {
941+
field.updateEditable();
944942
}
945943
}
946944

@@ -1107,29 +1105,37 @@ export class Block implements IASTNodeLocation {
11071105
' instead',
11081106
);
11091107
}
1110-
for (let i = 0, input; (input = this.inputList[i]); i++) {
1111-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
1112-
if (field.name === name) {
1113-
return field;
1114-
}
1108+
for (const field of this.getFields()) {
1109+
if (field.name === name) {
1110+
return field;
11151111
}
11161112
}
11171113
return null;
11181114
}
11191115

1116+
/**
1117+
* Returns a generator that provides every field on the block.
1118+
*
1119+
* @yields A generator that can be used to iterate the fields on the block.
1120+
*/
1121+
*getFields(): Generator<Field> {
1122+
for (const input of this.inputList) {
1123+
for (const field of input.fieldRow) {
1124+
yield field;
1125+
}
1126+
}
1127+
}
1128+
11201129
/**
11211130
* Return all variables referenced by this block.
11221131
*
11231132
* @returns List of variable ids.
11241133
*/
11251134
getVars(): string[] {
11261135
const vars: string[] = [];
1127-
for (let i = 0, input; (input = this.inputList[i]); i++) {
1128-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
1129-
if (field.referencesVariables()) {
1130-
// NOTE: This only applies to `FieldVariable`, a `Field<string>`
1131-
vars.push(field.getValue() as string);
1132-
}
1136+
for (const field of this.getFields()) {
1137+
if (field.referencesVariables()) {
1138+
vars.push(field.getValue());
11331139
}
11341140
}
11351141
return vars;
@@ -1143,17 +1149,15 @@ export class Block implements IASTNodeLocation {
11431149
*/
11441150
getVarModels(): IVariableModel<IVariableState>[] {
11451151
const vars = [];
1146-
for (let i = 0, input; (input = this.inputList[i]); i++) {
1147-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
1148-
if (field.referencesVariables()) {
1149-
const model = this.workspace.getVariableById(
1150-
field.getValue() as string,
1151-
);
1152-
// Check if the variable actually exists (and isn't just a potential
1153-
// variable).
1154-
if (model) {
1155-
vars.push(model);
1156-
}
1152+
for (const field of this.getFields()) {
1153+
if (field.referencesVariables()) {
1154+
const model = this.workspace.getVariableById(
1155+
field.getValue() as string,
1156+
);
1157+
// Check if the variable actually exists (and isn't just a potential
1158+
// variable).
1159+
if (model) {
1160+
vars.push(model);
11571161
}
11581162
}
11591163
}
@@ -1168,14 +1172,12 @@ export class Block implements IASTNodeLocation {
11681172
* @internal
11691173
*/
11701174
updateVarName(variable: IVariableModel<IVariableState>) {
1171-
for (let i = 0, input; (input = this.inputList[i]); i++) {
1172-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
1173-
if (
1174-
field.referencesVariables() &&
1175-
variable.getId() === field.getValue()
1176-
) {
1177-
field.refreshVariableName();
1178-
}
1175+
for (const field of this.getFields()) {
1176+
if (
1177+
field.referencesVariables() &&
1178+
variable.getId() === field.getValue()
1179+
) {
1180+
field.refreshVariableName();
11791181
}
11801182
}
11811183
}
@@ -1189,11 +1191,9 @@ export class Block implements IASTNodeLocation {
11891191
* updated name.
11901192
*/
11911193
renameVarById(oldId: string, newId: string) {
1192-
for (let i = 0, input; (input = this.inputList[i]); i++) {
1193-
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
1194-
if (field.referencesVariables() && oldId === field.getValue()) {
1195-
field.setValue(newId);
1196-
}
1194+
for (const field of this.getFields()) {
1195+
if (field.referencesVariables() && oldId === field.getValue()) {
1196+
field.setValue(newId);
11971197
}
11981198
}
11991199
}

core/block_svg.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,8 @@ export class BlockSvg
911911
icons[i].applyColour();
912912
}
913913

914-
for (let x = 0, input; (input = this.inputList[x]); x++) {
915-
for (let y = 0, field; (field = input.fieldRow[y]); y++) {
916-
field.applyColour();
917-
}
914+
for (const field of this.getFields()) {
915+
field.applyColour();
918916
}
919917
}
920918

core/serialization/blocks.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,9 @@ function saveIcons(block: Block, state: State, doFullSerialization: boolean) {
261261
*/
262262
function saveFields(block: Block, state: State, doFullSerialization: boolean) {
263263
const fields = Object.create(null);
264-
for (let i = 0; i < block.inputList.length; i++) {
265-
const input = block.inputList[i];
266-
for (let j = 0; j < input.fieldRow.length; j++) {
267-
const field = input.fieldRow[j];
268-
if (field.isSerializable()) {
269-
fields[field.name!] = field.saveState(doFullSerialization);
270-
}
264+
for (const field of block.getFields()) {
265+
if (field.isSerializable()) {
266+
fields[field.name!] = field.saveState(doFullSerialization);
271267
}
272268
}
273269
if (Object.keys(fields).length) {

core/xml.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,10 @@ function fieldToDom(field: Field): Element | null {
168168
* @param element The XML element to which the field DOM should be attached.
169169
*/
170170
function allFieldsToDom(block: Block, element: Element) {
171-
for (let i = 0; i < block.inputList.length; i++) {
172-
const input = block.inputList[i];
173-
for (let j = 0; j < input.fieldRow.length; j++) {
174-
const field = input.fieldRow[j];
175-
const fieldDom = fieldToDom(field);
176-
if (fieldDom) {
177-
element.appendChild(fieldDom);
178-
}
171+
for (const field of block.getFields()) {
172+
const fieldDom = fieldToDom(field);
173+
if (fieldDom) {
174+
element.appendChild(fieldDom);
179175
}
180176
}
181177
}

0 commit comments

Comments
 (0)