No Description

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 RectangularMarkerGrips_1 = require("./RectangularMarkerGrips");
  17. var BaseMarker_1 = require("../BaseMarker");
  18. var ResizeGrip_1 = require("../BaseMarker/ResizeGrip");
  19. var SvgHelper_1 = require("../../renderer/SvgHelper");
  20. var RectangularMarker = (function (_super) {
  21. __extends(RectangularMarker, _super);
  22. function RectangularMarker() {
  23. var _this = _super !== null && _super.apply(this, arguments) || this;
  24. _this.MIN_SIZE = 5;
  25. _this.CB_DISTANCE = 10;
  26. _this.addControlBox = function () {
  27. _this.controlBox = SvgHelper_1.SvgHelper.createGroup([['class', 'fc-whiteboard-rect-control-box']]);
  28. var translate = SvgHelper_1.SvgHelper.createTransform();
  29. translate.setTranslate(-_this.CB_DISTANCE / 2, -_this.CB_DISTANCE / 2);
  30. _this.controlBox.transform.baseVal.appendItem(translate);
  31. _this.addToVisual(_this.controlBox);
  32. _this.controlRect = SvgHelper_1.SvgHelper.createRect(_this.width + _this.CB_DISTANCE, _this.height + _this.CB_DISTANCE, [['class', 'fc-whiteboard-rect-control-rect']]);
  33. _this.controlBox.appendChild(_this.controlRect);
  34. _this.controlGrips = new RectangularMarkerGrips_1.RectangularMarkerGrips();
  35. _this.addControlGrips();
  36. };
  37. _this.adjustControlBox = function () {
  38. _this.controlRect.setAttribute('width', (_this.width + _this.CB_DISTANCE).toString());
  39. _this.controlRect.setAttribute('height', (_this.height + _this.CB_DISTANCE).toString());
  40. _this.positionGrips();
  41. };
  42. _this.addControlGrips = function () {
  43. _this.controlGrips.topLeft = _this.createGrip();
  44. _this.controlGrips.topCenter = _this.createGrip();
  45. _this.controlGrips.topRight = _this.createGrip();
  46. _this.controlGrips.centerLeft = _this.createGrip();
  47. _this.controlGrips.centerRight = _this.createGrip();
  48. _this.controlGrips.bottomLeft = _this.createGrip();
  49. _this.controlGrips.bottomCenter = _this.createGrip();
  50. _this.controlGrips.bottomRight = _this.createGrip();
  51. _this.positionGrips();
  52. };
  53. _this.createGrip = function () {
  54. var grip = new ResizeGrip_1.ResizeGrip();
  55. grip.visual.transform.baseVal.appendItem(SvgHelper_1.SvgHelper.createTransform());
  56. _this.controlBox.appendChild(grip.visual);
  57. grip.visual.addEventListener('mousedown', _this.gripMouseDown);
  58. grip.visual.addEventListener('mousemove', _this.gripMouseMove);
  59. grip.visual.addEventListener('mouseup', _this.gripMouseUp);
  60. grip.visual.addEventListener('touchstart', _this.onTouch, { passive: false });
  61. grip.visual.addEventListener('touchend', _this.onTouch, { passive: false });
  62. grip.visual.addEventListener('touchmove', _this.onTouch, { passive: false });
  63. return grip;
  64. };
  65. _this.gripMouseDown = function (ev) {
  66. _this.isResizing = true;
  67. _this.activeGrip = _this.controlGrips.findGripByVisual(ev.target) || null;
  68. _this.previousMouseX = ev.screenX;
  69. _this.previousMouseY = ev.screenY;
  70. ev.stopPropagation();
  71. };
  72. _this.gripMouseUp = function (ev) {
  73. _this.isResizing = false;
  74. _this.activeGrip = null;
  75. ev.stopPropagation();
  76. };
  77. _this.gripMouseMove = function (ev) {
  78. if (_this.isResizing) {
  79. _this.manipulate(ev);
  80. }
  81. };
  82. _this.positionGrips = function () {
  83. var gripSize = _this.controlGrips.topLeft.GRIP_SIZE;
  84. var left = -gripSize / 2;
  85. var top = left;
  86. var cx = (_this.width + _this.CB_DISTANCE) / 2 - gripSize / 2;
  87. var cy = (_this.height + _this.CB_DISTANCE) / 2 - gripSize / 2;
  88. var bottom = _this.height + _this.CB_DISTANCE - gripSize / 2;
  89. var right = _this.width + _this.CB_DISTANCE - gripSize / 2;
  90. _this.positionGrip(_this.controlGrips.topLeft.visual, left, top);
  91. _this.positionGrip(_this.controlGrips.topCenter.visual, cx, top);
  92. _this.positionGrip(_this.controlGrips.topRight.visual, right, top);
  93. _this.positionGrip(_this.controlGrips.centerLeft.visual, left, cy);
  94. _this.positionGrip(_this.controlGrips.centerRight.visual, right, cy);
  95. _this.positionGrip(_this.controlGrips.bottomLeft.visual, left, bottom);
  96. _this.positionGrip(_this.controlGrips.bottomCenter.visual, cx, bottom);
  97. _this.positionGrip(_this.controlGrips.bottomRight.visual, right, bottom);
  98. };
  99. _this.positionGrip = function (grip, x, y) {
  100. var translate = grip.transform.baseVal.getItem(0);
  101. translate.setTranslate(x, y);
  102. grip.transform.baseVal.replaceItem(translate, 0);
  103. };
  104. return _this;
  105. }
  106. RectangularMarker.prototype.captureSnap = function () {
  107. var snap = _super.prototype.captureSnap.call(this);
  108. snap.rectSnap = {
  109. width: this.width,
  110. height: this.height
  111. };
  112. return snap;
  113. };
  114. RectangularMarker.prototype.applySnap = function (snap) {
  115. _super.prototype.applySnap.call(this, snap);
  116. if (snap.rectSnap) {
  117. var _a = snap.rectSnap, width = _a.width, height = _a.height;
  118. if (width && height) {
  119. this.width = width;
  120. this.height = height;
  121. this.adjustControlBox();
  122. }
  123. }
  124. };
  125. RectangularMarker.prototype.endManipulation = function () {
  126. _super.prototype.endManipulation.call(this);
  127. this.isResizing = false;
  128. this.activeGrip = null;
  129. };
  130. RectangularMarker.prototype.select = function () {
  131. _super.prototype.select.call(this);
  132. this.controlBox.style.display = '';
  133. };
  134. RectangularMarker.prototype.deselect = function () {
  135. _super.prototype.deselect.call(this);
  136. this.controlBox.style.display = 'none';
  137. };
  138. RectangularMarker.prototype.init = function () {
  139. _super.prototype.init.call(this);
  140. this.addControlBox();
  141. if (this.page && this.page.mode === 'mirror') {
  142. this.controlBox.style.display = 'none';
  143. }
  144. };
  145. RectangularMarker.prototype.resizeByEvent = function (dx, dy, pos) {
  146. this.activeGrip = this.controlGrips[pos];
  147. this.resize(dx, dy);
  148. };
  149. RectangularMarker.prototype.resize = function (dx, dy, onPosition) {
  150. var translateX = 0;
  151. var translateY = 0;
  152. switch (this.activeGrip) {
  153. case this.controlGrips.topLeft:
  154. this.width -= dx;
  155. this.height -= dy;
  156. translateX += dx;
  157. translateY += dy;
  158. this.x += dx;
  159. this.y += dy;
  160. if (onPosition) {
  161. onPosition('topLeft');
  162. }
  163. break;
  164. case this.controlGrips.bottomLeft:
  165. this.width -= dx;
  166. this.height += dy;
  167. translateX += dx;
  168. this.x += dx;
  169. if (onPosition) {
  170. onPosition('bottomLeft');
  171. }
  172. break;
  173. case this.controlGrips.topRight:
  174. this.width += dx;
  175. this.height -= dy;
  176. translateY += dy;
  177. this.y += dy;
  178. if (onPosition) {
  179. onPosition('topRight');
  180. }
  181. break;
  182. case this.controlGrips.bottomRight:
  183. this.width += dx;
  184. this.height += dy;
  185. if (onPosition) {
  186. onPosition('bottomRight');
  187. }
  188. break;
  189. case this.controlGrips.centerLeft:
  190. this.width -= dx;
  191. translateX += dx;
  192. this.x += dx;
  193. if (onPosition) {
  194. onPosition('centerLeft');
  195. }
  196. break;
  197. case this.controlGrips.centerRight:
  198. this.width += dx;
  199. if (onPosition) {
  200. onPosition('centerRight');
  201. }
  202. break;
  203. case this.controlGrips.topCenter:
  204. this.height -= dy;
  205. translateY += dy;
  206. this.y += dy;
  207. if (onPosition) {
  208. onPosition('topCenter');
  209. }
  210. break;
  211. case this.controlGrips.bottomCenter:
  212. this.height += dy;
  213. if (onPosition) {
  214. onPosition('bottomCenter');
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. if (this.width < this.MIN_SIZE) {
  221. var offset = this.MIN_SIZE - this.width;
  222. this.width = this.MIN_SIZE;
  223. if (translateX !== 0) {
  224. translateX -= offset;
  225. }
  226. }
  227. if (this.height < this.MIN_SIZE) {
  228. var offset = this.MIN_SIZE - this.height;
  229. this.height = this.MIN_SIZE;
  230. if (translateY !== 0) {
  231. translateY -= offset;
  232. }
  233. }
  234. if (translateX !== 0 || translateY !== 0) {
  235. var translate = this.visual.transform.baseVal.getItem(0);
  236. translate.setMatrix(translate.matrix.translate(translateX, translateY));
  237. this.visual.transform.baseVal.replaceItem(translate, 0);
  238. }
  239. this.adjustControlBox();
  240. };
  241. RectangularMarker.prototype.onTouch = function (ev) {
  242. _super.prototype.onTouch.call(this, ev);
  243. };
  244. RectangularMarker.createMarker = function (page) {
  245. var marker = new RectangularMarker();
  246. marker.page = page;
  247. marker.init();
  248. return marker;
  249. };
  250. return RectangularMarker;
  251. }(BaseMarker_1.BaseMarker));
  252. exports.RectangularMarker = RectangularMarker;