No Description

index.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 RectangularMarker_1 = require("../RectangularMarker");
  17. var SvgHelper_1 = require("../../renderer/SvgHelper");
  18. var OkIcon = require('../../assets/check.svg');
  19. var CancelIcon = require('../../assets/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.type = 'text';
  25. _this.MIN_SIZE = 50;
  26. _this.DEFAULT_TEXT = 'Double-click to edit text';
  27. _this.text = _this.DEFAULT_TEXT;
  28. _this.inDoubleTap = false;
  29. _this.renderText = function () {
  30. var LINE_SIZE = '1.2em';
  31. while (_this.textElement.lastChild) {
  32. _this.textElement.removeChild(_this.textElement.lastChild);
  33. }
  34. var lines = _this.text.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/);
  35. for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
  36. var line = lines_1[_i];
  37. if (line.trim() === '') {
  38. line = ' ';
  39. }
  40. _this.textElement.appendChild(SvgHelper_1.SvgHelper.createTSpan(line, [['x', '0'], ['dy', LINE_SIZE]]));
  41. }
  42. setTimeout(_this.sizeText, 10);
  43. };
  44. _this.sizeText = function () {
  45. var textSize = _this.textElement.getBBox();
  46. var x = 0;
  47. var y = 0;
  48. var scale = 1.0;
  49. if (textSize.width > 0 && textSize.height > 0) {
  50. var xScale = (_this.width * 1.0) / textSize.width;
  51. var yScale = (_this.height * 1.0) / textSize.height;
  52. scale = Math.min(xScale, yScale);
  53. x = (_this.width - textSize.width * scale) / 2;
  54. y = (_this.height - textSize.height * scale) / 2;
  55. }
  56. _this.textElement.transform.baseVal.getItem(0).setTranslate(x, y);
  57. _this.textElement.transform.baseVal.getItem(1).setScale(scale, scale);
  58. };
  59. _this.onDblClick = function (ev) {
  60. _this.showEditor();
  61. };
  62. _this.onTap = function (ev) {
  63. if (_this.inDoubleTap) {
  64. _this.inDoubleTap = false;
  65. _this.showEditor();
  66. }
  67. else {
  68. _this.inDoubleTap = true;
  69. setTimeout(function () {
  70. _this.inDoubleTap = false;
  71. }, 300);
  72. }
  73. };
  74. _this.showEditor = function () {
  75. _this.editor = document.createElement('div');
  76. _this.editor.className = 'fc-whiteboard-text-editor';
  77. _this.editorTextArea = document.createElement('textarea');
  78. if (_this.text !== _this.DEFAULT_TEXT) {
  79. _this.editorTextArea.value = _this.text;
  80. }
  81. _this.editorTextArea.addEventListener('keydown', _this.onEditorKeyDown);
  82. _this.editor.appendChild(_this.editorTextArea);
  83. document.body.appendChild(_this.editor);
  84. var buttons = document.createElement('div');
  85. buttons.className = 'fc-whiteboard-text-editor-button-bar';
  86. _this.editor.appendChild(buttons);
  87. var okButton = document.createElement('div');
  88. okButton.className = 'fc-whiteboard-text-editor-button';
  89. okButton.innerHTML = OkIcon;
  90. okButton.addEventListener('click', _this.onEditorOkClick);
  91. buttons.appendChild(okButton);
  92. var cancelButton = document.createElement('div');
  93. cancelButton.className = 'fc-whiteboard-text-editor-button';
  94. cancelButton.innerHTML = CancelIcon;
  95. cancelButton.addEventListener('click', _this.closeEditor);
  96. buttons.appendChild(cancelButton);
  97. };
  98. _this.onEditorOkClick = function (ev) {
  99. if (_this.editorTextArea.value.trim()) {
  100. _this.text = _this.editorTextArea.value;
  101. }
  102. else {
  103. _this.text = _this.DEFAULT_TEXT;
  104. }
  105. _this.onChange({ target: 'marker', id: _this.id, event: 'changeText', data: _this.text });
  106. _this.renderText();
  107. _this.closeEditor();
  108. };
  109. _this.closeEditor = function () {
  110. document.body.removeChild(_this.editor);
  111. };
  112. _this.onEditorKeyDown = function (ev) {
  113. if (ev.key === 'Enter' && ev.ctrlKey) {
  114. ev.preventDefault();
  115. _this.onEditorOkClick(null);
  116. }
  117. };
  118. return _this;
  119. }
  120. TextMarker.prototype.setText = function (text) {
  121. this.text = text;
  122. this.renderText();
  123. };
  124. TextMarker.prototype.setup = function () {
  125. _super.prototype.setup.call(this);
  126. this.textElement = SvgHelper_1.SvgHelper.createText();
  127. this.addToRenderVisual(this.textElement);
  128. SvgHelper_1.SvgHelper.setAttributes(this.visual, [['class', 'text-marker']]);
  129. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  130. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  131. this.renderText();
  132. this.visual.addEventListener('dblclick', this.onDblClick);
  133. this.visual.addEventListener('touchstart', this.onTap);
  134. };
  135. TextMarker.prototype.resize = function (x, y, onPosition) {
  136. _super.prototype.resize.call(this, x, y, onPosition);
  137. this.sizeText();
  138. };
  139. TextMarker.createMarker = function (page) {
  140. var marker = new TextMarker();
  141. marker.page = page;
  142. marker.setup();
  143. return marker;
  144. };
  145. return TextMarker;
  146. }(RectangularMarker_1.RectangularMarker));
  147. exports.TextMarker = TextMarker;