Files
blockly/core/events/events_block_base.ts
Beka Westberg f678531389 chore: remove AnyDuringMigration from events (#6382)
* chore: remove AnyDuringMigration from events

* chore: format

* chore: lint

* chore: fix tests

* fix: correct types for JSON properties

* chore: fix tests

* refactor: replace exclamation markers with errors

* chore: fix build and tests

* chore: set event types as members, rather than in constructor

* chore: update comment

* chore: export new types

* chore: format

* chore: remove unnecessary override

* chore: change how we're overriding isBlank

* chore: remove unnecessary ?

* chore: remove non-null assertion in block move event

* chore: format

* chore: revert changes to isNull

* chore: format
2022-09-27 15:08:05 -07:00

76 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Base class for all types of block events.
*
* @class
*/
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Events.BlockBase');
import type {Block} from '../block.js';
import {Abstract as AbstractEvent, AbstractEventJson} from './events_abstract.js';
/**
* Abstract class for a block event.
*
* @alias Blockly.Events.BlockBase
*/
export class BlockBase extends AbstractEvent {
override isBlank = true;
blockId?: string;
/**
* @param opt_block The block this event corresponds to.
* Undefined for a blank event.
*/
constructor(opt_block?: Block) {
super();
this.isBlank = !!opt_block;
if (!opt_block) return;
/** The block ID for the block this event pertains to */
this.blockId = opt_block.id;
/** The workspace identifier for this event. */
this.workspaceId = opt_block.workspace.id;
}
/**
* Encode the event as JSON.
*
* @returns JSON representation.
*/
override toJson(): AbstractEventJson {
const json = super.toJson() as BlockBaseJson;
if (!this.blockId) {
throw new Error(
'The block ID is undefined. Either pass a block to ' +
'the constructor, or call fromJson');
}
json['blockId'] = this.blockId;
return json;
}
/**
* Decode the JSON event.
*
* @param json JSON representation.
*/
override fromJson(json: BlockBaseJson) {
super.fromJson(json);
this.blockId = json['blockId'];
}
}
export interface BlockBaseJson extends AbstractEventJson {
blockId: string;
}