Files
blockly/core/events/events_click.ts
2022-07-07 22:22:51 +00:00

84 lines
2.1 KiB
TypeScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Events fired as a result of UI click in Blockly's editor.
*/
/**
* Events fired as a result of UI click in Blockly's editor.
* @class
*/
import type {Block} from '../block';
import * as registry from '../registry';
import {UiBase} from './events_ui_base';
import * as eventUtils from './utils';
/**
* Class for a click event.
* @alias Blockly.Events.Click
*/
export class Click extends UiBase {
blockId: AnyDuringMigration;
targetType?: string;
override type: string;
/**
* @param opt_block The affected block. Null for click events that do not have
* an associated block (i.e. workspace click). Undefined for a blank
* event.
* @param opt_workspaceId The workspace identifier for this event.
* Not used if block is passed. Undefined for a blank event.
* @param opt_targetType The type of element targeted by this click event.
* Undefined for a blank event.
*/
constructor(
opt_block?: Block|null, opt_workspaceId?: string|null,
opt_targetType?: string) {
let workspaceId = opt_block ? opt_block.workspace.id : opt_workspaceId;
if (workspaceId === null) {
workspaceId = undefined;
}
super(workspaceId);
this.blockId = opt_block ? opt_block.id : null;
/** The type of element targeted by this click event. */
this.targetType = opt_targetType;
/** Type of this event. */
this.type = eventUtils.CLICK;
}
/**
* Encode the event as JSON.
* @return JSON representation.
*/
override toJson(): AnyDuringMigration {
const json = super.toJson();
json['targetType'] = this.targetType;
if (this.blockId) {
json['blockId'] = this.blockId;
}
return json;
}
/**
* Decode the JSON event.
* @param json JSON representation.
*/
override fromJson(json: AnyDuringMigration) {
super.fromJson(json);
this.targetType = json['targetType'];
this.blockId = json['blockId'];
}
}
registry.register(registry.Type.EVENT, eventUtils.CLICK, Click);