mirror of
https://github.com/google/blockly.git
synced 2026-01-06 00:20:37 +01:00
fix: make getSourceBlock nullable again (#6542)
* fix: make getSourceBlock nullable again * chore: format * chore: move to a specific error * chore: also update procedures with new error * chore: format
This commit is contained in:
@@ -16,7 +16,7 @@ goog.declareModuleId('Blockly.FieldVariable');
|
||||
import './events/events_block_change.js';
|
||||
|
||||
import type {Block} from './block.js';
|
||||
import {Field, FieldConfig} from './field.js';
|
||||
import {Field, FieldConfig, UnattachedFieldError} from './field.js';
|
||||
import {FieldDropdown} from './field_dropdown.js';
|
||||
import * as fieldRegistry from './field_registry.js';
|
||||
import * as internalConstants from './internal_constants.js';
|
||||
@@ -135,20 +135,27 @@ export class FieldVariable extends FieldDropdown {
|
||||
* @internal
|
||||
*/
|
||||
override initModel() {
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
if (this.variable_) {
|
||||
return; // Initialization already happened.
|
||||
}
|
||||
const variable = Variables.getOrCreateVariablePackage(
|
||||
this.getSourceBlock().workspace, null, this.defaultVariableName,
|
||||
this.defaultType_);
|
||||
block.workspace, null, this.defaultVariableName, this.defaultType_);
|
||||
// Don't call setValue because we don't want to cause a rerender.
|
||||
this.doValueUpdate_(variable.getId());
|
||||
}
|
||||
|
||||
override shouldAddBorderRect_() {
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
return super.shouldAddBorderRect_() &&
|
||||
(!this.getConstants()!.FIELD_DROPDOWN_NO_BORDER_RECT_SHADOW ||
|
||||
this.getSourceBlock().type !== 'variables_get');
|
||||
block.type !== 'variables_get');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,6 +165,10 @@ export class FieldVariable extends FieldDropdown {
|
||||
* field's state.
|
||||
*/
|
||||
override fromXml(fieldElement: Element) {
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
const id = fieldElement.getAttribute('id');
|
||||
const variableName = fieldElement.textContent;
|
||||
// 'variabletype' should be lowercase, but until July 2019 it was sometimes
|
||||
@@ -168,8 +179,7 @@ export class FieldVariable extends FieldDropdown {
|
||||
// AnyDuringMigration because: Argument of type 'string | null' is not
|
||||
// assignable to parameter of type 'string | undefined'.
|
||||
const variable = Variables.getOrCreateVariablePackage(
|
||||
this.getSourceBlock().workspace, id, variableName as AnyDuringMigration,
|
||||
variableType);
|
||||
block.workspace, id, variableName as AnyDuringMigration, variableType);
|
||||
|
||||
// This should never happen :)
|
||||
if (variableType !== null && variableType !== variable.type) {
|
||||
@@ -233,12 +243,16 @@ export class FieldVariable extends FieldDropdown {
|
||||
* @internal
|
||||
*/
|
||||
override loadState(state: AnyDuringMigration) {
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
if (this.loadLegacyState(FieldVariable, state)) {
|
||||
return;
|
||||
}
|
||||
// This is necessary so that blocks in the flyout can have custom var names.
|
||||
const variable = Variables.getOrCreateVariablePackage(
|
||||
this.getSourceBlock().workspace, state['id'] || null, state['name'],
|
||||
block.workspace, state['id'] || null, state['name'],
|
||||
state['type'] || '');
|
||||
this.setValue(variable.getId());
|
||||
}
|
||||
@@ -315,9 +329,12 @@ export class FieldVariable extends FieldDropdown {
|
||||
if (opt_newValue === null) {
|
||||
return null;
|
||||
}
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
const newId = opt_newValue as string;
|
||||
const variable =
|
||||
Variables.getVariable(this.getSourceBlock().workspace, newId);
|
||||
const variable = Variables.getVariable(block.workspace, newId);
|
||||
if (!variable) {
|
||||
console.warn(
|
||||
'Variable id doesn\'t point to a real variable! ' +
|
||||
@@ -343,8 +360,11 @@ export class FieldVariable extends FieldDropdown {
|
||||
* @param newId The value to be saved.
|
||||
*/
|
||||
protected override doValueUpdate_(newId: AnyDuringMigration) {
|
||||
this.variable_ =
|
||||
Variables.getVariable(this.getSourceBlock().workspace, newId as string);
|
||||
const block = this.getSourceBlock();
|
||||
if (!block) {
|
||||
throw new UnattachedFieldError();
|
||||
}
|
||||
this.variable_ = Variables.getVariable(block.workspace, newId as string);
|
||||
super.doValueUpdate_(newId);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user