UNPKG

medsurf-draw

Version:

Draw annotations on jpg/zoomify images, based on PIXI.js

196 lines 9.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as MedsurfDraw from "../../public-api"; import * as Models from '@ascii-dev-user/medsurf-lib/models'; import { BaseGenerator, BaseGeneratorModel } from "../../bases/generators/BaseGenerator"; import { debounce } from "debounce"; export var MarkerGeneratorStates; (function (MarkerGeneratorStates) { MarkerGeneratorStates[MarkerGeneratorStates["LINE"] = 0] = "LINE"; MarkerGeneratorStates[MarkerGeneratorStates["TEXT"] = 1] = "TEXT"; })(MarkerGeneratorStates || (MarkerGeneratorStates = {})); var MarkerGeneratorModel = (function (_super) { __extends(MarkerGeneratorModel, _super); function MarkerGeneratorModel() { return _super !== null && _super.apply(this, arguments) || this; } return MarkerGeneratorModel; }(BaseGeneratorModel)); export { MarkerGeneratorModel }; var MarkerGenerator = (function (_super) { __extends(MarkerGenerator, _super); function MarkerGenerator(model) { var _this = _super.call(this, model) || this; _this._enableKeyEvents = false; _this._state = MarkerGeneratorStates.LINE; _this.target.modeInteraction.setModeItem("drawing"); _this._debounceSetupGeneratorMethod = debounce(_this._setupGenerator.bind(_this), 50).bind(_this); MedsurfDraw.Keyboard.events.on("pressed_ControlLeft", "onMarkerGeneratorControlLeft", _this.onKeyPressed.bind(_this)); MedsurfDraw.Keyboard.events.on("pressed_ControlRight", "onMarkerGeneratorControlRight", _this.onKeyPressed.bind(_this)); MedsurfDraw.Keyboard.events.on("released_ControlLeft", "onMarkerGeneratorControlLeftEnd", _this.onKeyReleased.bind(_this)); MedsurfDraw.Keyboard.events.on("released_ControlRight", "onMarkerGeneratorControlRightEnd", _this.onKeyReleased.bind(_this)); _this.on("debounceSetupGenerator", _this._debounceSetupGeneratorMethod); _this.emit("debounceSetupGenerator", _this.startElement); return _this; } MarkerGenerator.prototype.end = function () { MedsurfDraw.Keyboard.events.remove("pressed_ControlLeft", "onMarkerGeneratorControlLeft"); MedsurfDraw.Keyboard.events.remove("released_ControlLeft", "onMarkerGeneratorControlLeftEnd"); MedsurfDraw.Keyboard.events.remove("pressed_ControlRight", "onMarkerGeneratorControlRight"); MedsurfDraw.Keyboard.events.remove("released_ControlRight", "onMarkerGeneratorControlRightEnd"); this.off("debounceSetupGenerator", this._debounceSetupGeneratorMethod); if (this._lineGenerator) { this._lineGenerator.removeAllListeners(); } if (this._textPrimitiveGenerator) { this._textPrimitiveGenerator.removeAllListeners(); } _super.prototype.end.call(this); }; MarkerGenerator.prototype.destroy = function (options) { this.end(); if (this._lineGenerator) { this._lineGenerator.destroy(); } if (this._textPrimitiveGenerator) { this._textPrimitiveGenerator.destroy(); } }; MarkerGenerator.prototype.endGenerator = function () { _super.prototype.endGenerator.call(this); this._startPositionPointElement.onButtonSetSelftest(true); this._startPositionPointElement.model.selftestItems = this.target.getImageObjects() .filter(function (imageObject) { return imageObject.modeInteraction.lastMode.startsWith('draw_select'); }) .map(function (imageObject) { return imageObject.name; }); this.target.sortChildren(); this.target.modeInteraction.setMode(this.target.modeInteraction.defaultMode); }; MarkerGenerator.prototype.abortGenerator = function () { _super.prototype.abortGenerator.call(this); this.target.getImageObjects() .filter(function (imageObject) { return imageObject.modeInteraction.lastMode.startsWith('draw_') && imageObject.modeInteraction.lastMode.endsWith('_new'); }) .forEach(function (imageObject) { imageObject.modeInteraction.setMode("delete_draw"); }); this.target.sortChildren(); this.target.modeInteraction.setMode(this.target.modeInteraction.defaultMode); }; MarkerGenerator.prototype._setupGenerator = function (positionPoint) { if (this._state === MarkerGeneratorStates.LINE) { this._lineGenerator = new MedsurfDraw.LineGenerator({ target: this.target, layerGroup: this.data.layerGroup, image: this.data.image, bezierCourve: false, stickMode: MedsurfDraw.StickMode.POSITIONPOINTS, startElement: positionPoint }); this._lineGenerator.once("startGenerator", this.start, this); this._lineGenerator.once("endGenerator", this.endLineGenerator, this); this._lineGenerator.once("abortGenerator", this.abortLineGenerator, this); } else { this._textPrimitiveGenerator = new MedsurfDraw.TextPrimitiveGenerator({ target: this.target, layerGroup: this.data.layerGroup, image: this.data.image, stickMode: MedsurfDraw.StickMode.NOELEMENTS, startElement: positionPoint, rotateWithLine: false, withScaling: false, triggerImmediateEndDraw: false }); this._textPrimitiveGenerator.once("endGenerator", this.endTextPrimitiveGenerator, this); this._textPrimitiveGenerator.once("abortGenerator", this.abortTextPrimitiveGenerator, this); } }; MarkerGenerator.prototype.endLineGenerator = function () { if (!this._startPositionPointElement) { this._startPositionPointElement = this._lineGenerator.startPositionPointElement; } var primitives = this._lineGenerator.endPositionPointElement.model.primitives || []; if (primitives.filter(function (p) { return p.type === Models.ImageObjectType.TEXT; }).length > 0) { this.emit("endGenerator"); this.end(); } else { if (this._lineGenerator.endPositionPointElement.modeInteraction.lastMode !== 'draw_select') { if (!(MedsurfDraw.Keyboard.isKeyDown("ControlLeft") || MedsurfDraw.Keyboard.isKeyDown("ControlRight"))) { this._enableKeyEvents = true; this._state = MarkerGeneratorStates.TEXT; } } this.emit("debounceSetupGenerator", this._lineGenerator.endPositionPointElement); } }; MarkerGenerator.prototype.abortLineGenerator = function () { this.emit("abortGenerator"); this.destroy(); }; MarkerGenerator.prototype.endTextPrimitiveGenerator = function () { this.emit("endGenerator"); this.end(); }; MarkerGenerator.prototype.abortTextPrimitiveGenerator = function () { this.emit("abortGenerator"); this.destroy(); }; MarkerGenerator.prototype.onKeyPressed = function (keyCode, event) { event.preventDefault(); if (!this._enableKeyEvents || this._state === MarkerGeneratorStates.LINE) { return; } var positionPoint = this._textPrimitiveGenerator.startElement; this._textPrimitiveGenerator.destroy(); this._state = MarkerGeneratorStates.LINE; this.emit("debounceSetupGenerator", positionPoint); }; MarkerGenerator.prototype.onKeyReleased = function (keyCode, event) { event.preventDefault(); if (!this._enableKeyEvents || this._state === MarkerGeneratorStates.TEXT) { return; } var positionPoint = this._lineGenerator.startElement; this._lineGenerator.destroyEnd(); this._state = MarkerGeneratorStates.TEXT; this.emit("debounceSetupGenerator", positionPoint); }; Object.defineProperty(MarkerGenerator.prototype, "drawInteraction", { get: function () { if (this._state === MarkerGeneratorStates.LINE) { return this._lineGenerator.drawInteraction; } else { return this._textPrimitiveGenerator.drawInteraction; } }, enumerable: false, configurable: true }); Object.defineProperty(MarkerGenerator.prototype, "startElement", { get: function () { return this.data.startElement; }, set: function (value) { this.data.startElement = value; }, enumerable: false, configurable: true }); return MarkerGenerator; }(BaseGenerator)); export { MarkerGenerator }; //# sourceMappingURL=MarkerGenerator.js.map