Skip to content

Commit

Permalink
feat: Add a generator for all fields on a block.
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfunko committed Nov 21, 2024
1 parent af5905a commit 33ccbdc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 58 deletions.
73 changes: 34 additions & 39 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,8 @@ export class Block implements IASTNodeLocation {
*/
setEditable(editable: boolean) {
this.editable_ = editable;
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
field.updateEditable();
}
for (const field of this.getFields()) {
field.updateEditable();
}
}

Expand Down Expand Up @@ -1102,29 +1100,32 @@ export class Block implements IASTNodeLocation {
' instead',
);
}
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (field.name === name) {
return field;
}
for (const field of this.getFields()) {
if (field.name === name) {
return field;
}
}
return null;
}

*getFields(): Generator<Field> {
for (const input of this.inputList) {
for (const field of input.fieldRow) {
yield field;
}
}
}

/**
* Return all variables referenced by this block.
*
* @returns List of variable ids.
*/
getVars(): string[] {
const vars: string[] = [];
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (field.referencesVariables()) {
// NOTE: This only applies to `FieldVariable`, a `Field<string>`
vars.push(field.getValue() as string);
}
for (const field of this.getFields()) {
if (field.referencesVariables()) {
vars.push(field.getValue());
}
}
return vars;
Expand All @@ -1138,17 +1139,15 @@ export class Block implements IASTNodeLocation {
*/
getVarModels(): IVariableModel<IVariableState>[] {
const vars = [];
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (field.referencesVariables()) {
const model = this.workspace.getVariableById(
field.getValue() as string,
);
// Check if the variable actually exists (and isn't just a potential
// variable).
if (model) {
vars.push(model);
}
for (const field of this.getFields()) {
if (field.referencesVariables()) {
const model = this.workspace.getVariableById(
field.getValue() as string,
);
// Check if the variable actually exists (and isn't just a potential
// variable).
if (model) {
vars.push(model);
}
}
}
Expand All @@ -1163,14 +1162,12 @@ export class Block implements IASTNodeLocation {
* @internal
*/
updateVarName(variable: IVariableModel<IVariableState>) {
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (
field.referencesVariables() &&
variable.getId() === field.getValue()
) {
field.refreshVariableName();
}
for (const field of this.getFields()) {
if (
field.referencesVariables() &&
variable.getId() === field.getValue()
) {
field.refreshVariableName();
}
}
}
Expand All @@ -1184,11 +1181,9 @@ export class Block implements IASTNodeLocation {
* updated name.
*/
renameVarById(oldId: string, newId: string) {
for (let i = 0, input; (input = this.inputList[i]); i++) {
for (let j = 0, field; (field = input.fieldRow[j]); j++) {
if (field.referencesVariables() && oldId === field.getValue()) {
field.setValue(newId);
}
for (const field of this.getFields()) {
if (field.referencesVariables() && oldId === field.getValue()) {
field.setValue(newId);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,10 +887,8 @@ export class BlockSvg
icons[i].applyColour();
}

for (let x = 0, input; (input = this.inputList[x]); x++) {
for (let y = 0, field; (field = input.fieldRow[y]); y++) {
field.applyColour();
}
for (const field of this.getFields()) {
field.applyColour();
}
}

Expand Down
10 changes: 3 additions & 7 deletions core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,9 @@ function saveIcons(block: Block, state: State, doFullSerialization: boolean) {
*/
function saveFields(block: Block, state: State, doFullSerialization: boolean) {
const fields = Object.create(null);
for (let i = 0; i < block.inputList.length; i++) {
const input = block.inputList[i];
for (let j = 0; j < input.fieldRow.length; j++) {
const field = input.fieldRow[j];
if (field.isSerializable()) {
fields[field.name!] = field.saveState(doFullSerialization);
}
for (const field of block.getFields()) {
if (field.isSerializable()) {
fields[field.name!] = field.saveState(doFullSerialization);
}
}
if (Object.keys(fields).length) {
Expand Down
12 changes: 4 additions & 8 deletions core/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,10 @@ function fieldToDom(field: Field): Element | null {
* @param element The XML element to which the field DOM should be attached.
*/
function allFieldsToDom(block: Block, element: Element) {
for (let i = 0; i < block.inputList.length; i++) {
const input = block.inputList[i];
for (let j = 0; j < input.fieldRow.length; j++) {
const field = input.fieldRow[j];
const fieldDom = fieldToDom(field);
if (fieldDom) {
element.appendChild(fieldDom);
}
for (const field of block.getFields()) {
const fieldDom = fieldToDom(field);
if (fieldDom) {
element.appendChild(fieldDom);
}
}
}
Expand Down

0 comments on commit 33ccbdc

Please sign in to comment.