sprotty
Version:
A next-gen framework for graphical views
140 lines • 6.53 kB
JavaScript
"use strict";
/********************************************************************************
* Copyright (c) 2017-2023 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var SetViewportCommand_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewportAnimation = exports.GetViewportCommand = exports.SetViewportCommand = void 0;
const inversify_1 = require("inversify");
const actions_1 = require("sprotty-protocol/lib/actions");
const geometry_1 = require("sprotty-protocol/lib/utils/geometry");
const command_1 = require("../../base/commands/command");
const animation_1 = require("../../base/animations/animation");
const model_1 = require("./model");
const types_1 = require("../../base/types");
const request_command_1 = require("../../base/commands/request-command");
let SetViewportCommand = SetViewportCommand_1 = class SetViewportCommand extends command_1.MergeableCommand {
constructor(action) {
super();
this.action = action;
this.newViewport = action.newViewport;
}
execute(context) {
const model = context.root;
const element = model.index.getById(this.action.elementId);
if (element && (0, model_1.isViewport)(element)) {
this.element = element;
this.oldViewport = {
scroll: this.element.scroll,
zoom: this.element.zoom,
};
const { zoomLimits, horizontalScrollLimits, verticalScrollLimits } = this.viewerOptions;
this.newViewport = (0, model_1.limitViewport)(this.newViewport, model.canvasBounds, horizontalScrollLimits, verticalScrollLimits, zoomLimits);
return this.setViewport(element, this.oldViewport, this.newViewport, context);
}
return context.root;
}
setViewport(element, oldViewport, newViewport, context) {
if (element && (0, model_1.isViewport)(element)) {
if (this.action.animate) {
return new ViewportAnimation(element, oldViewport, newViewport, context).start();
}
else {
element.scroll = newViewport.scroll;
element.zoom = newViewport.zoom;
}
}
return context.root;
}
undo(context) {
return this.setViewport(this.element, this.newViewport, this.oldViewport, context);
}
redo(context) {
return this.setViewport(this.element, this.oldViewport, this.newViewport, context);
}
merge(command, context) {
if (!this.action.animate && command instanceof SetViewportCommand_1 && this.element === command.element) {
this.newViewport = command.newViewport;
return true;
}
return false;
}
};
exports.SetViewportCommand = SetViewportCommand;
SetViewportCommand.KIND = actions_1.SetViewportAction.KIND;
__decorate([
(0, inversify_1.inject)(types_1.TYPES.ViewerOptions),
__metadata("design:type", Object)
], SetViewportCommand.prototype, "viewerOptions", void 0);
exports.SetViewportCommand = SetViewportCommand = SetViewportCommand_1 = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(types_1.TYPES.Action)),
__metadata("design:paramtypes", [Object])
], SetViewportCommand);
let GetViewportCommand = class GetViewportCommand extends request_command_1.ModelRequestCommand {
constructor(action) {
super();
this.action = action;
}
retrieveResult(context) {
const elem = context.root;
let viewport;
if ((0, model_1.isViewport)(elem)) {
viewport = { scroll: elem.scroll, zoom: elem.zoom };
}
else {
viewport = { scroll: geometry_1.Point.ORIGIN, zoom: 1 };
}
return actions_1.ViewportResult.create(viewport, elem.canvasBounds, this.action.requestId);
}
};
exports.GetViewportCommand = GetViewportCommand;
GetViewportCommand.KIND = actions_1.GetViewportAction.KIND;
exports.GetViewportCommand = GetViewportCommand = __decorate([
__param(0, (0, inversify_1.inject)(types_1.TYPES.Action)),
__metadata("design:paramtypes", [Object])
], GetViewportCommand);
class ViewportAnimation extends animation_1.Animation {
constructor(element, oldViewport, newViewport, context) {
super(context);
this.element = element;
this.oldViewport = oldViewport;
this.newViewport = newViewport;
this.context = context;
this.zoomFactor = Math.log(newViewport.zoom / oldViewport.zoom);
}
tween(t, context) {
this.element.scroll = {
x: (1 - t) * this.oldViewport.scroll.x + t * this.newViewport.scroll.x,
y: (1 - t) * this.oldViewport.scroll.y + t * this.newViewport.scroll.y
};
this.element.zoom = this.oldViewport.zoom * Math.exp(t * this.zoomFactor);
return context.root;
}
}
exports.ViewportAnimation = ViewportAnimation;
//# sourceMappingURL=viewport.js.map