feat: added intermediate event change (#7671)

* feat: added intermediate event change

* fix: update prettier format for the code

* fix: update comment style

* fix: update test statements
This commit is contained in:
truongductri01
2023-12-04 16:10:09 -05:00
committed by GitHub
parent 0a27e1c472
commit 96a354b46b
2 changed files with 64 additions and 0 deletions

View File

@@ -122,6 +122,36 @@ export class BlockFieldIntermediateChange extends BlockBase {
override isNull(): boolean {
return this.oldValue === this.newValue;
}
/**
* Run a change event.
*
* @param forward True if run forward, false if run backward (undo).
*/
override run(forward: boolean) {
const workspace = this.getEventWorkspace_();
if (!this.blockId) {
throw new Error(
'The block ID is undefined. Either pass a block to ' +
'the constructor, or call fromJson',
);
}
const block = workspace.getBlockById(this.blockId);
if (!block) {
throw new Error(
'The associated block is undefined. Either pass a ' +
'block to the constructor, or call fromJson',
);
}
const value = forward ? this.newValue : this.oldValue;
const field = block.getField(this.name!);
if (field) {
field.setValue(value);
} else {
console.warn("Can't set non-existent field: " + this.name);
}
}
}
export interface BlockFieldIntermediateChangeJson extends BlockBaseJson {

View File

@@ -35,4 +35,38 @@ suite('Field Intermediate Change Event', function () {
chai.assert.deepEqual(newEvent, origEvent);
});
});
suite('Change Value', function () {
test("running forward changes the block's value to new value", function () {
const block = this.workspace.newBlock('text', 'block_id');
const origEvent = new Blockly.Events.BlockFieldIntermediateChange(
block,
'TEXT',
'old value',
'new value',
);
origEvent.run(true);
chai.assert.deepEqual(
block.getField(origEvent.name).getValue(),
'new value',
);
});
test("running backward changes the block's value to old value", function () {
const block = this.workspace.newBlock('text', 'block_id');
const origEvent = new Blockly.Events.BlockFieldIntermediateChange(
block,
'TEXT',
'old value',
'new value',
);
origEvent.run(false);
chai.assert.deepEqual(
block.getField(origEvent.name).getValue(),
'old value',
);
});
});
});