UNPKG

medsurf-draw

Version:

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

395 lines 16.6 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 { BaseElementInteraction } from "../bases/interactions/BaseElementInteraction"; var WriteInteraction = (function (_super) { __extends(WriteInteraction, _super); function WriteInteraction(element) { return _super.call(this, element) || this; } WriteInteraction.prototype.reset = function () { return; }; WriteInteraction.prototype.onKeyDown = function (keyCode, event) { }; WriteInteraction.prototype.onKeyPressed = function (keyCode, event) { if (event.ctrlKey && (event.key === "c" || event.key === "3" || event.key === "v" || event.key === "F7" || event.key === "x" || event.key === "F9")) { return; } event.preventDefault(); switch (event.key) { case "Backspace": if (this.element.textSelection) { this.deleteSelectedText(); } else if (this.element.startTextIndex > 0) { this.emit("onText", this.element.text.substring(0, this.element.startTextIndex - 1) + this.element.text.substring(this.element.startTextIndex)); this.moveCaretLeft(false); } return; case "Tab": return; case "Enter": if (event.shiftKey) { if (this.element.textSelection) { this.replaceSelectedText("\n"); } else { this.emit("onText", this.element.text.substring(0, this.element.startTextIndex) + "\n" + this.element.text.substring(this.element.startTextIndex)); this.moveCaretRight(false); } } else { this.emit("endText"); } return; case "Shift": return; case "Control": return; case "Alt": return; case "Pause": return; case "CapsLock": return; case "Escape": this.emit("endText"); return; case "PageUp": return; case "PageDown": return; case "End": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } if (event.ctrlKey && this.element.startTextIndex + 1 < this.element.text.length) { this.element.startTextIndex = this.element.text.length; } else { var nextPosition = this.element.text.indexOf("\n", this.element.startTextIndex); if (nextPosition !== this.element.startTextIndex) { this.element.startTextIndex = (nextPosition != -1) ? nextPosition : this.element.text.length; } else { this.moveCaretRight(event.shiftKey); } } if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "Home": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } if (event.ctrlKey && this.element.startTextIndex - 1 > 0) { this.element.startTextIndex = 0; } else { var nextPosition = this.element.text.lastIndexOf("\n", this.element.startTextIndex - 1); if (nextPosition + 1 !== this.element.startTextIndex) { this.element.startTextIndex = (nextPosition != -1) ? nextPosition + 1 : 0; } else { this.moveCaretLeft(event.shiftKey); } } if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "ArrowLeft": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } if (event.ctrlKey && this.element.startTextIndex - 1 > 0) { var nextPosition = this.element.text.lastIndexOf("\n", this.element.startTextIndex - 1); if (nextPosition + 1 !== this.element.startTextIndex) { this.element.startTextIndex = (nextPosition != -1) ? nextPosition + 1 : 0; } else { this.moveCaretLeft(event.shiftKey); } } else { this.moveCaretLeft(event.shiftKey); } if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "ArrowUp": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } this.moveCaretUp(event.shiftKey); if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "ArrowRight": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } if (event.ctrlKey && this.element.startTextIndex + 1 < this.element.text.length) { var nextPosition = this.element.text.indexOf("\n", this.element.startTextIndex); if (nextPosition !== this.element.startTextIndex) { this.element.startTextIndex = (nextPosition != -1) ? nextPosition : this.element.text.length; } else { this.moveCaretRight(event.shiftKey); } } else { this.moveCaretRight(event.shiftKey); } if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "ArrowDown": if (event.shiftKey && !this.element.textSelection) { this.element.textSelection = true; this.element.endTextIndex = this.element.startTextIndex; } this.moveCaretDown(event.shiftKey); if (!event.shiftKey) { this.element.textSelection = false; } this.emit("onSelection"); this.emit("onCaret"); return; case "Insert": return; case "Delete": if (this.element.textSelection) { this.deleteSelectedText(); } else { this.emit("onText", this.element.text.substring(0, this.element.startTextIndex) + this.element.text.substring(this.element.startTextIndex + 1)); this.emit("onCaret"); } return; case "a": if (event.ctrlKey) { this.selectAllText(); return; } break; case "OS": return; case "ContextMenu": return; case "F1": case "F2": case "F3": case "F4": case "F5": case "F6": case "F7": case "F8": case "F9": case "F10": case "F11": case "F12": return; case "NumLock": return; case "ScrollLock": return; case "AltGraph": return; } if (this.element.textSelection) { this.replaceSelectedText(event.key); } else { this.emit("onText", this.element.text.substring(0, this.element.startTextIndex) + event.key + this.element.text.substring(this.element.startTextIndex)); this.moveCaretRight(false); } }; WriteInteraction.prototype.onKeyReleased = function (keyCode, event) { }; WriteInteraction.prototype.onCopy = function (event) { event.preventDefault(); event.clipboardData.setData("text/plain", this.getSelectedText()); }; WriteInteraction.prototype.onCut = function (event) { event.preventDefault(); event.clipboardData.setData("text/plain", this.getSelectedText()); if (this.element.textSelection) { this.deleteSelectedText(); this.moveCaretRight(false); } }; WriteInteraction.prototype.onPaste = function (event) { event.preventDefault(); var text = event.clipboardData.getData("text/plain"); if (this.element.textSelection) { this.deleteSelectedText(); this.moveCaretRight(false); } this.replaceSelectedText(text); }; WriteInteraction.prototype.selectEndText = function () { this.element.startTextIndex = this.element.text.length; this.emit("onCaret"); }; WriteInteraction.prototype.selectAllText = function () { this.element.textSelection = true; this.element.startTextIndex = this.element.text.length; this.element.endTextIndex = 0; this.emit("onSelection"); }; WriteInteraction.prototype.deleteSelectedText = function () { var startPosition; var endPosition; if (this.element.startTextIndex > this.element.endTextIndex) { startPosition = this.element.endTextIndex; endPosition = this.element.startTextIndex; } else { startPosition = this.element.startTextIndex; endPosition = this.element.endTextIndex; } if (startPosition === 0 && endPosition === this.element.text.length) { this.emit("onText", ""); } else { this.emit("onText", this.element.text.substring(0, startPosition) + this.element.text.substring(endPosition)); } this.element.textSelection = false; this.element.startTextIndex = startPosition; if (this.element.startTextIndex < 0) { this.element.startTextIndex = 0; } this.emit("onSelection"); this.emit("onCaret"); }; WriteInteraction.prototype.replaceSelectedText = function (text) { var startPosition; var endPosition; if (this.element.startTextIndex > this.element.endTextIndex) { startPosition = this.element.endTextIndex; endPosition = this.element.startTextIndex; } else { startPosition = this.element.startTextIndex; endPosition = this.element.endTextIndex; } if (startPosition === 0 && endPosition === this.element.text.length) { this.emit("onText", text); } else { this.emit("onText", this.element.text.substring(0, startPosition) + text + this.element.text.substring(endPosition)); } this.element.textSelection = false; this.element.startTextIndex = startPosition + 1; if (this.element.startTextIndex < 0) { this.element.startTextIndex = 0; } this.emit("onSelection"); this.emit("onCaret"); }; WriteInteraction.prototype.moveCaretLeft = function (shiftDown) { if (this.element.textSelection && !shiftDown) { if (this.element.startTextIndex > this.element.endTextIndex) { this.element.startTextIndex = this.element.endTextIndex; } } else { this.element.startTextIndex--; if (this.element.startTextIndex < 0) { this.element.startTextIndex = 0; } } this.emit("onCaret"); }; WriteInteraction.prototype.moveCaretUp = function (shiftDown) { var linePosition = this.element.text.lastIndexOf("\n", this.element.startTextIndex - 1); var previousLinePosition = this.element.text.lastIndexOf("\n", linePosition - 1); var leftIndex = Math.abs(this.element.startTextIndex - linePosition); if (this.element.startTextIndex <= leftIndex) { this.element.startTextIndex = 0; } else if ((this.element.startTextIndex - linePosition) > linePosition - previousLinePosition) { this.element.startTextIndex = linePosition; } else { this.element.startTextIndex = previousLinePosition + leftIndex; } this.emit("onCaret"); }; WriteInteraction.prototype.moveCaretRight = function (shiftDown) { if (this.element.textSelection && !shiftDown) { if (this.element.startTextIndex < this.element.endTextIndex) { this.element.startTextIndex = this.element.endTextIndex; } } else { this.element.startTextIndex++; if (this.element.startTextIndex > this.element.text.length) { this.element.startTextIndex = this.element.text.length; } } this.emit("onCaret"); }; WriteInteraction.prototype.moveCaretDown = function (shiftDown) { var linePosition = this.element.text.lastIndexOf("\n", this.element.startTextIndex - 1); var nextLinePosition = this.element.text.indexOf("\n", this.element.startTextIndex); var leftIndex = Math.abs(this.element.startTextIndex - linePosition); if (nextLinePosition === -1) { this.element.startTextIndex = this.element.text.length; } else if (this.element.startTextIndex == nextLinePosition) { this.element.startTextIndex = nextLinePosition + (nextLinePosition - linePosition); } else { this.element.startTextIndex = nextLinePosition + leftIndex; } this.emit("onCaret"); }; WriteInteraction.prototype.getSelectedText = function () { if (this.element.textSelection) { if (this.element.startTextIndex < this.element.endTextIndex) { return this.element.text.substring(this.element.startTextIndex, this.element.endTextIndex); } else if (this.element.startTextIndex > this.element.endTextIndex) { return this.element.text.substring(this.element.endTextIndex, this.element.startTextIndex); } } return ""; }; return WriteInteraction; }(BaseElementInteraction)); export { WriteInteraction }; //# sourceMappingURL=WriteInteraction.js.map