No Description

TextMarker.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var SvgHelper_1 = require("../../utils/SvgHelper");
  17. var RectangularMarkerBase_1 = require("../base/RectangularMarkerBase");
  18. var check_svg_1 = require("./check.svg");
  19. var times_svg_1 = require("./times.svg");
  20. var TextMarker = (function (_super) {
  21. __extends(TextMarker, _super);
  22. function TextMarker() {
  23. var _this = _super !== null && _super.apply(this, arguments) || this;
  24. _this.MIN_SIZE = 50;
  25. _this.DEFAULT_TEXT = 'Double-click to edit text';
  26. _this.text = _this.DEFAULT_TEXT;
  27. _this.inDoubleTap = false;
  28. _this.renderText = function () {
  29. var LINE_SIZE = '1.2em';
  30. while (_this.textElement.lastChild) {
  31. _this.textElement.removeChild(_this.textElement.lastChild);
  32. }
  33. var lines = _this.text.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/);
  34. for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
  35. var line = lines_1[_i];
  36. if (line.trim() === '') {
  37. line = ' ';
  38. }
  39. _this.textElement.appendChild(SvgHelper_1.SvgHelper.createTSpan(line, [['x', '0'], ['dy', LINE_SIZE]]));
  40. }
  41. setTimeout(_this.sizeText, 10);
  42. };
  43. _this.sizeText = function () {
  44. var textSize = _this.textElement.getBBox();
  45. var x = 0;
  46. var y = 0;
  47. var scale = 1.0;
  48. if (textSize.width > 0 && textSize.height > 0) {
  49. var xScale = (_this.width * 1.0) / textSize.width;
  50. var yScale = (_this.height * 1.0) / textSize.height;
  51. scale = Math.min(xScale, yScale);
  52. x = (_this.width - textSize.width * scale) / 2;
  53. y = (_this.height - textSize.height * scale) / 2;
  54. }
  55. _this.textElement.transform.baseVal.getItem(0).setTranslate(x, y);
  56. _this.textElement.transform.baseVal.getItem(1).setScale(scale, scale);
  57. };
  58. _this.onDblClick = function (ev) {
  59. _this.showEditor();
  60. };
  61. _this.onTap = function (ev) {
  62. if (_this.inDoubleTap) {
  63. _this.inDoubleTap = false;
  64. _this.showEditor();
  65. }
  66. else {
  67. _this.inDoubleTap = true;
  68. setTimeout(function () {
  69. _this.inDoubleTap = false;
  70. }, 300);
  71. }
  72. };
  73. _this.showEditor = function () {
  74. _this.editor = document.createElement('div');
  75. _this.editor.className = 'fc-whiteboardtext-editor';
  76. _this.editorTextArea = document.createElement('textarea');
  77. if (_this.text !== _this.DEFAULT_TEXT) {
  78. _this.editorTextArea.value = _this.text;
  79. }
  80. _this.editorTextArea.addEventListener('keydown', _this.onEditorKeyDown);
  81. _this.editor.appendChild(_this.editorTextArea);
  82. document.body.appendChild(_this.editor);
  83. var buttons = document.createElement('div');
  84. buttons.className = 'fc-whiteboardtext-editor-button-bar';
  85. _this.editor.appendChild(buttons);
  86. var okButton = document.createElement('div');
  87. okButton.className = 'fc-whiteboardtext-editor-button';
  88. okButton.innerHTML = check_svg_1.default;
  89. okButton.addEventListener('click', _this.onEditorOkClick);
  90. buttons.appendChild(okButton);
  91. var cancelButton = document.createElement('div');
  92. cancelButton.className = 'fc-whiteboardtext-editor-button';
  93. cancelButton.innerHTML = times_svg_1.default;
  94. cancelButton.addEventListener('click', _this.closeEditor);
  95. buttons.appendChild(cancelButton);
  96. };
  97. _this.onEditorOkClick = function (ev) {
  98. if (_this.editorTextArea.value.trim()) {
  99. _this.text = _this.editorTextArea.value;
  100. }
  101. else {
  102. _this.text = _this.DEFAULT_TEXT;
  103. }
  104. _this.renderText();
  105. _this.closeEditor();
  106. };
  107. _this.closeEditor = function () {
  108. document.body.removeChild(_this.editor);
  109. };
  110. _this.onEditorKeyDown = function (ev) {
  111. if (ev.key === 'Enter' && ev.ctrlKey) {
  112. ev.preventDefault();
  113. _this.onEditorOkClick(null);
  114. }
  115. };
  116. return _this;
  117. }
  118. TextMarker.prototype.setup = function () {
  119. _super.prototype.setup.call(this);
  120. this.textElement = SvgHelper_1.SvgHelper.createText();
  121. this.addToRenderVisual(this.textElement);
  122. SvgHelper_1.SvgHelper.setAttributes(this.visual, [['class', 'text-marker']]);
  123. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  124. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  125. this.renderText();
  126. this.visual.addEventListener('dblclick', this.onDblClick);
  127. this.visual.addEventListener('touchstart', this.onTap);
  128. };
  129. TextMarker.prototype.resize = function (x, y) {
  130. _super.prototype.resize.call(this, x, y);
  131. this.sizeText();
  132. };
  133. TextMarker.createMarker = function () {
  134. var marker = new TextMarker();
  135. marker.setup();
  136. return marker;
  137. };
  138. return TextMarker;
  139. }(RectangularMarkerBase_1.RectangularMarkerBase));
  140. exports.TextMarker = TextMarker;