mirror of
https://github.com/google/blockly.git
synced 2026-01-08 09:30:06 +01:00
* feat: add new path for deserialization of events * chore: add tests for round-tripping events * chore: add static fromJson methods to all events * chore: add inline docs to new static methods * chore: add deprecation warnings * chore: cleanup * chore: add deprecation and docs to abstract * chore: format * chore: cleanup from rebase * chore: update docs comment
151 lines
4.4 KiB
TypeScript
151 lines
4.4 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2020 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Events fired as a result of a viewport change.
|
|
*
|
|
* @class
|
|
*/
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.Events.ViewportChange');
|
|
|
|
import * as deprecation from '../utils/deprecation.js';
|
|
import * as registry from '../registry.js';
|
|
import {AbstractEventJson} from './events_abstract.js';
|
|
import {UiBase} from './events_ui_base.js';
|
|
import * as eventUtils from './utils.js';
|
|
import type {Workspace} from '../workspace.js';
|
|
|
|
|
|
/**
|
|
* Class for a viewport change event.
|
|
*
|
|
* @alias Blockly.Events.ViewportChange
|
|
*/
|
|
export class ViewportChange extends UiBase {
|
|
viewTop?: number;
|
|
viewLeft?: number;
|
|
scale?: number;
|
|
oldScale?: number;
|
|
override type = eventUtils.VIEWPORT_CHANGE;
|
|
|
|
/**
|
|
* @param opt_top Top-edge of the visible portion of the workspace, relative
|
|
* to the workspace origin. Undefined for a blank event.
|
|
* @param opt_left Left-edge of the visible portion of the workspace relative
|
|
* to the workspace origin. Undefined for a blank event.
|
|
* @param opt_scale The scale of the workspace. Undefined for a blank event.
|
|
* @param opt_workspaceId The workspace identifier for this event.
|
|
* Undefined for a blank event.
|
|
* @param opt_oldScale The old scale of the workspace. Undefined for a blank
|
|
* event.
|
|
*/
|
|
constructor(
|
|
opt_top?: number, opt_left?: number, opt_scale?: number,
|
|
opt_workspaceId?: string, opt_oldScale?: number) {
|
|
super(opt_workspaceId);
|
|
|
|
/**
|
|
* Top-edge of the visible portion of the workspace, relative to the
|
|
* workspace origin.
|
|
*/
|
|
this.viewTop = opt_top;
|
|
|
|
/**
|
|
* Left-edge of the visible portion of the workspace, relative to the
|
|
* workspace origin.
|
|
*/
|
|
this.viewLeft = opt_left;
|
|
|
|
/** The scale of the workspace. */
|
|
this.scale = opt_scale;
|
|
|
|
/** The old scale of the workspace. */
|
|
this.oldScale = opt_oldScale;
|
|
}
|
|
|
|
/**
|
|
* Encode the event as JSON.
|
|
*
|
|
* @returns JSON representation.
|
|
*/
|
|
override toJson(): ViewportChangeJson {
|
|
const json = super.toJson() as ViewportChangeJson;
|
|
if (this.viewTop === undefined) {
|
|
throw new Error(
|
|
'The view top is undefined. Either pass a value to ' +
|
|
'the constructor, or call fromJson');
|
|
}
|
|
if (this.viewLeft === undefined) {
|
|
throw new Error(
|
|
'The view left is undefined. Either pass a value to ' +
|
|
'the constructor, or call fromJson');
|
|
}
|
|
if (this.scale === undefined) {
|
|
throw new Error(
|
|
'The scale is undefined. Either pass a value to ' +
|
|
'the constructor, or call fromJson');
|
|
}
|
|
if (this.oldScale === undefined) {
|
|
throw new Error(
|
|
'The old scale is undefined. Either pass a value to ' +
|
|
'the constructor, or call fromJson');
|
|
}
|
|
json['viewTop'] = this.viewTop;
|
|
json['viewLeft'] = this.viewLeft;
|
|
json['scale'] = this.scale;
|
|
json['oldScale'] = this.oldScale;
|
|
return json;
|
|
}
|
|
|
|
/**
|
|
* Decode the JSON event.
|
|
*
|
|
* @param json JSON representation.
|
|
*/
|
|
override fromJson(json: ViewportChangeJson) {
|
|
deprecation.warn(
|
|
'Blockly.Events.Viewport.prototype.fromJson', 'version 9', 'version 10',
|
|
'Blockly.Events.fromJson');
|
|
super.fromJson(json);
|
|
this.viewTop = json['viewTop'];
|
|
this.viewLeft = json['viewLeft'];
|
|
this.scale = json['scale'];
|
|
this.oldScale = json['oldScale'];
|
|
}
|
|
|
|
/**
|
|
* Deserializes the JSON event.
|
|
*
|
|
* @param event The event to append new properties to. Should be a subclass
|
|
* of Viewport, 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: ViewportChangeJson, workspace: Workspace, event?: any):
|
|
ViewportChange {
|
|
const newEvent =
|
|
super.fromJson(json, workspace, event ?? new ViewportChange()) as
|
|
ViewportChange;
|
|
newEvent.viewTop = json['viewTop'];
|
|
newEvent.viewLeft = json['viewLeft'];
|
|
newEvent.scale = json['scale'];
|
|
newEvent.oldScale = json['oldScale'];
|
|
return newEvent;
|
|
}
|
|
}
|
|
|
|
export interface ViewportChangeJson extends AbstractEventJson {
|
|
viewTop: number;
|
|
viewLeft: number;
|
|
scale: number;
|
|
oldScale: number;
|
|
}
|
|
|
|
registry.register(
|
|
registry.Type.EVENT, eventUtils.VIEWPORT_CHANGE, ViewportChange);
|