mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
feat: Add a generator for all fields on a block. (#8667)
* feat: Add a generator for all fields on a block. * chore: Add docstring.
This commit is contained in:
@@ -937,10 +937,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1107,16 +1105,27 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a generator that provides every field on the block.
|
||||
*
|
||||
* @yields A generator that can be used to iterate the fields on the block.
|
||||
*/
|
||||
*getFields(): Generator<Field> {
|
||||
for (const input of this.inputList) {
|
||||
for (const field of input.fieldRow) {
|
||||
yield field;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all variables referenced by this block.
|
||||
*
|
||||
@@ -1124,12 +1133,9 @@ export class Block implements IASTNodeLocation {
|
||||
*/
|
||||
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;
|
||||
@@ -1143,17 +1149,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1168,14 +1172,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1189,11 +1191,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -911,10 +911,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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -261,13 +261,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) {
|
||||
|
||||
12
core/xml.ts
12
core/xml.ts
@@ -168,14 +168,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user