mirror of
https://github.com/google/blockly.git
synced 2026-01-05 08:00:09 +01:00
* chore: add linting for tsdoc * chore: don't require types on return * chore: remove redundant fileoverview from ts * chore: change return to returns and add some newlines * chore: remove license tag * chore: don't require params/return docs * chore: remove spurious struct tags * Revert "chore: change return to returns and add some newlines" This reverts commitd6d8656a45. * chore: don't auto-add param names * chore: disable require-param bc it breaks on this * return to returns and add line breaks * chore: configure additional jsdoc rules * chore: run format * Revert "chore: remove license tag" This reverts commit173455588a. * chore: allow license tag format * chore: only require jsdoc on exported items * chore: add missing jsdoc or silence where needed * chore: run format * chore: lint fixes
115 lines
2.6 KiB
TypeScript
115 lines
2.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2018 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Abstract class for events fired as a result of actions in
|
|
* Blockly's editor.
|
|
*
|
|
* @class
|
|
*/
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.Events.Abstract');
|
|
|
|
import * as common from '../common.js';
|
|
import type {Workspace} from '../workspace.js';
|
|
|
|
import * as eventUtils from './utils.js';
|
|
|
|
|
|
/**
|
|
* Abstract class for an event.
|
|
*
|
|
* @alias Blockly.Events.Abstract
|
|
*/
|
|
export abstract class Abstract {
|
|
/** Whether or not the event is blank (to be populated by fromJson). */
|
|
isBlank: boolean|null = null;
|
|
|
|
/** The workspace identifier for this event. */
|
|
workspaceId?: string = undefined;
|
|
group: string;
|
|
recordUndo: boolean;
|
|
|
|
/** Whether or not the event is a UI event. */
|
|
isUiEvent = false;
|
|
|
|
/** Type of this event. */
|
|
type?: string = undefined;
|
|
|
|
/** @alias Blockly.Events.Abstract */
|
|
constructor() {
|
|
/**
|
|
* The event group id for the group this event belongs to. Groups define
|
|
* events that should be treated as an single action from the user's
|
|
* perspective, and should be undone together.
|
|
*/
|
|
this.group = eventUtils.getGroup();
|
|
|
|
/** Sets whether the event should be added to the undo stack. */
|
|
this.recordUndo = eventUtils.getRecordUndo();
|
|
}
|
|
|
|
/**
|
|
* Encode the event as JSON.
|
|
*
|
|
* @returns JSON representation.
|
|
*/
|
|
toJson(): AnyDuringMigration {
|
|
const json = {'type': this.type};
|
|
if (this.group) {
|
|
(json as AnyDuringMigration)['group'] = this.group;
|
|
}
|
|
return json;
|
|
}
|
|
|
|
/**
|
|
* Decode the JSON event.
|
|
*
|
|
* @param json JSON representation.
|
|
*/
|
|
fromJson(json: AnyDuringMigration) {
|
|
this.isBlank = false;
|
|
this.group = json['group'];
|
|
}
|
|
|
|
/**
|
|
* Does this event record any change of state?
|
|
*
|
|
* @returns True if null, false if something changed.
|
|
*/
|
|
isNull(): boolean {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Run an event.
|
|
*
|
|
* @param _forward True if run forward, false if run backward (undo).
|
|
*/
|
|
run(_forward: boolean) {}
|
|
// Defined by subclasses.
|
|
|
|
/**
|
|
* Get workspace the event belongs to.
|
|
*
|
|
* @returns The workspace the event belongs to.
|
|
* @throws {Error} if workspace is null.
|
|
* @internal
|
|
*/
|
|
getEventWorkspace_(): Workspace {
|
|
let workspace;
|
|
if (this.workspaceId) {
|
|
workspace = common.getWorkspaceById(this.workspaceId);
|
|
}
|
|
if (!workspace) {
|
|
throw Error(
|
|
'Workspace is null. Event must have been generated from real' +
|
|
' Blockly events.');
|
|
}
|
|
return workspace;
|
|
}
|
|
}
|