No Description

index.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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({
  106. target: 'marker',
  107. id: _this.id,
  108. event: 'inputMarker',
  109. marker: { text: _this.text }
  110. });
  111. _this.renderText();
  112. _this.closeEditor();
  113. };
  114. _this.closeEditor = function () {
  115. document.body.removeChild(_this.editor);
  116. };
  117. _this.onEditorKeyDown = function (ev) {
  118. if (ev.key === 'Enter' && ev.ctrlKey) {
  119. ev.preventDefault();
  120. _this.onEditorOkClick(null);
  121. }
  122. };
  123. return _this;
  124. }
  125. TextMarker.prototype.setText = function (text) {
  126. this.text = text;
  127. this.renderText();
  128. };
  129. TextMarker.prototype.captureSnap = function () {
  130. var baseSnap = _super.prototype.captureSnap.call(this);
  131. baseSnap.textSnap = { text: this.text };
  132. return baseSnap;
  133. };
  134. TextMarker.prototype.applySnap = function (snap) {
  135. _super.prototype.applySnap.call(this, snap);
  136. if (snap.textSnap && snap.textSnap.text !== this.text) {
  137. this.setText(snap.textSnap.text);
  138. }
  139. };
  140. TextMarker.prototype.init = function () {
  141. _super.prototype.init.call(this);
  142. this.textElement = SvgHelper_1.SvgHelper.createText();
  143. this.addToRenderVisual(this.textElement);
  144. SvgHelper_1.SvgHelper.setAttributes(this.visual, [['class', 'text-marker']]);
  145. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  146. this.textElement.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  147. this.renderText();
  148. this.visual.addEventListener('dblclick', this.onDblClick);
  149. this.visual.addEventListener('touchstart', this.onTap);
  150. };
  151. TextMarker.prototype.resize = function (x, y, onPosition) {
  152. _super.prototype.resize.call(this, x, y, onPosition);
  153. this.sizeText();
  154. };
  155. TextMarker.createMarker = function (page) {
  156. var marker = new TextMarker();
  157. marker.page = page;
  158. marker.init();
  159. return marker;
  160. };
  161. return TextMarker;
  162. }(RectangularMarker_1.RectangularMarker));
  163. exports.TextMarker = TextMarker;