Files
blockly/core/events/events_var_base.ts
Rachel Fenichel de904ab128 chore: remove deprecated functionality for v10 (#7077)
* chore: remove deprecated functionality in events files

* chore: remove deprecated items in renderers

* chore: remove deprecated items in core

* chore: remove mixin deprecation

* chore: fix tests after removing deprecations
2023-05-11 14:30:54 -07:00

88 lines
2.0 KiB
TypeScript

/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Abstract class for a variable event.
*
* @class
*/
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.Events.VarBase');
import type {VariableModel} from '../variable_model.js';
import {
Abstract as AbstractEvent,
AbstractEventJson,
} from './events_abstract.js';
import type {Workspace} from '../workspace.js';
/**
* Abstract class for a variable event.
*/
export class VarBase extends AbstractEvent {
override isBlank = true;
/** The ID of the variable this event references. */
varId?: string;
/**
* @param opt_variable The variable this event corresponds to. Undefined for
* a blank event.
*/
constructor(opt_variable?: VariableModel) {
super();
this.isBlank = typeof opt_variable === 'undefined';
if (!opt_variable) return;
this.varId = opt_variable.getId();
this.workspaceId = opt_variable.workspace.id;
}
/**
* Encode the event as JSON.
*
* @returns JSON representation.
*/
override toJson(): VarBaseJson {
const json = super.toJson() as VarBaseJson;
if (!this.varId) {
throw new Error(
'The var ID is undefined. Either pass a variable to ' +
'the constructor, or call fromJson'
);
}
json['varId'] = this.varId;
return json;
}
/**
* Deserializes the JSON event.
*
* @param event The event to append new properties to. Should be a subclass
* of VarBase, but we can't specify that due to the fact that parameters
* to static methods in subclasses must be supertypes of parameters to
* static methods in superclasses.
* @internal
*/
static fromJson(
json: VarBaseJson,
workspace: Workspace,
event?: any
): VarBase {
const newEvent = super.fromJson(
json,
workspace,
event ?? new VarBase()
) as VarBase;
newEvent.varId = json['varId'];
return newEvent;
}
}
export interface VarBaseJson extends AbstractEventJson {
varId: string;
}