暂无描述

index.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 index_1 = require("./../Baseboard/index");
  17. var toolbar_items_1 = require("./../../toolbar/toolbar-items");
  18. var Synthetizer_1 = require("../../renderer/Synthetizer");
  19. var Toolbar_1 = require("../../toolbar/Toolbar");
  20. require("./index.less");
  21. var Drawboard = (function (_super) {
  22. __extends(Drawboard, _super);
  23. function Drawboard(source, _a) {
  24. var _b = _a === void 0 ? {} : _a, page = _b.page, onChange = _b.onChange;
  25. var _this = _super.call(this, source) || this;
  26. _this.scale = 1.0;
  27. _this.onComplete = function () { };
  28. _this.onChange = function () { };
  29. _this.open = function (onComplete, onCancel) {
  30. if (onComplete) {
  31. _this.onComplete = onComplete;
  32. }
  33. if (onCancel) {
  34. _this.onCancel = onCancel;
  35. }
  36. _this.setTargetRect();
  37. _this.initBoard();
  38. _this.attachEvents();
  39. _this.setStyles();
  40. window.addEventListener('resize', _this.adjustUI);
  41. if (_this.page.mode === 'master') {
  42. _this.showUI();
  43. }
  44. };
  45. _this.hide = function () {
  46. if (_this.source.imgSrc) {
  47. _this.target.style.display = 'none';
  48. }
  49. _this.boardHolder.style.opacity = '0';
  50. _this.boardHolder.style.zIndex = '-1';
  51. if (_this.toolbar) {
  52. _this.toolbar.hide();
  53. }
  54. };
  55. _this.show = function () {
  56. if (_this.source.imgSrc) {
  57. _this.target.style.display = 'block';
  58. }
  59. _this.boardHolder.style.opacity = '1';
  60. _this.boardHolder.style.zIndex = '9999';
  61. if (_this.toolbar) {
  62. _this.toolbar.show();
  63. }
  64. };
  65. _this.close = function () {
  66. if (_this.toolbarUI) {
  67. document.body.removeChild(_this.toolbarUI);
  68. }
  69. if (_this.boardCanvas) {
  70. document.body.removeChild(_this.boardHolder);
  71. }
  72. };
  73. _this.render = function (onComplete, onCancel) {
  74. _this.onComplete = onComplete;
  75. if (onCancel) {
  76. _this.onCancel = onCancel;
  77. }
  78. _this.selectMarker(null);
  79. _this.startRender(_this.renderFinished);
  80. };
  81. _this.addMarker = function (markerType, _a) {
  82. var id = (_a === void 0 ? {} : _a).id;
  83. var marker = markerType.createMarker(_this.page);
  84. if (id) {
  85. marker.id = id;
  86. }
  87. marker.onSelected = _this.selectMarker;
  88. marker.onChange = _this.onChange;
  89. if (marker.defs && marker.defs.length > 0) {
  90. for (var _i = 0, _b = marker.defs; _i < _b.length; _i++) {
  91. var d = _b[_i];
  92. if (d.id && !_this.boardCanvas.getElementById(d.id)) {
  93. _this.defs.appendChild(d);
  94. }
  95. }
  96. }
  97. _this.onChange({
  98. target: 'marker',
  99. parentId: _this.page ? _this.page.id : _this.id,
  100. event: 'add',
  101. data: { type: marker.type, id: marker.id }
  102. });
  103. _this.markers.push(marker);
  104. _this.selectMarker(marker);
  105. _this.boardCanvas.appendChild(marker.visual);
  106. var bbox = marker.visual.getBBox();
  107. var x = _this.width / 2 / _this.scale - bbox.width / 2;
  108. var y = _this.height / 2 / _this.scale - bbox.height / 2;
  109. var translate = marker.visual.transform.baseVal.getItem(0);
  110. translate.setMatrix(translate.matrix.translate(x, y));
  111. marker.visual.transform.baseVal.replaceItem(translate, 0);
  112. };
  113. _this.deleteActiveMarker = function () {
  114. if (_this.activeMarker) {
  115. if (_this.onChange) {
  116. _this.onChange({
  117. event: 'remove',
  118. id: _this.activeMarker.id,
  119. target: 'marker',
  120. data: { id: _this.activeMarker.id }
  121. });
  122. }
  123. _this.deleteMarker(_this.activeMarker);
  124. }
  125. };
  126. _this.setTargetRect = function () {
  127. var targetRect = _this.target.getBoundingClientRect();
  128. var bodyRect = document.body.parentElement.getBoundingClientRect();
  129. _this.targetRect = {
  130. left: targetRect.left - bodyRect.left,
  131. top: targetRect.top - bodyRect.top
  132. };
  133. };
  134. _this.startRender = function (done) {
  135. var renderer = new Synthetizer_1.Synthetizer();
  136. renderer.rasterize(_this.target, _this.boardCanvas, done);
  137. };
  138. _this.attachEvents = function () {
  139. _this.boardCanvas.addEventListener('mousedown', _this.mouseDown);
  140. _this.boardCanvas.addEventListener('mousemove', _this.mouseMove);
  141. _this.boardCanvas.addEventListener('mouseup', _this.mouseUp);
  142. };
  143. _this.mouseDown = function (ev) {
  144. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  145. _this.activeMarker.deselect();
  146. _this.activeMarker = null;
  147. }
  148. };
  149. _this.mouseMove = function (ev) {
  150. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  151. _this.activeMarker.manipulate(ev);
  152. }
  153. };
  154. _this.mouseUp = function (ev) {
  155. if (_this.activeMarker) {
  156. _this.activeMarker.endManipulation();
  157. }
  158. };
  159. _this.adjustUI = function (ev) {
  160. _this.adjustSize();
  161. _this.positionUI();
  162. };
  163. _this.adjustSize = function () {
  164. _this.width = _this.target.clientWidth;
  165. _this.height = _this.target.clientHeight;
  166. var scale = _this.target.clientWidth / _this.boardHolder.clientWidth;
  167. if (scale !== 1.0) {
  168. _this.scale *= scale;
  169. _this.boardHolder.style.width = _this.width + "px";
  170. _this.boardHolder.style.height = _this.height + "px";
  171. _this.boardHolder.style.transform = "scale(" + _this.scale + ")";
  172. }
  173. };
  174. _this.positionUI = function () {
  175. _this.setTargetRect();
  176. _this.positionBoard();
  177. _this.positionToolbar();
  178. };
  179. _this.positionToolbar = function () {
  180. _this.toolbarUI.style.left = _this.targetRect.left +
  181. _this.target.offsetWidth -
  182. _this.toolbarUI.clientWidth + "px";
  183. _this.toolbarUI.style.top = _this.targetRect.top - _this.toolbarUI.clientHeight + "px";
  184. };
  185. _this.showUI = function () {
  186. _this.toolbar = new Toolbar_1.Toolbar(_this.toolbars, _this.toolbarClick);
  187. _this.toolbarUI = _this.toolbar.getUI();
  188. document.body.appendChild(_this.toolbarUI);
  189. _this.toolbarUI.style.position = 'absolute';
  190. _this.positionToolbar();
  191. };
  192. _this.setStyles = function () {
  193. var editorStyleSheet = document.createElementNS('http://www.w3.org/2000/svg', 'style');
  194. editorStyleSheet.innerHTML = "\n .rect-marker .render-visual {\n stroke: #ff0000;\n stroke-width: 3;\n fill: transparent;\n }\n .cover-marker .render-visual {\n stroke-width: 0;\n fill: #000000;\n }\n .highlight-marker .render-visual {\n stroke: transparent;\n stroke-width: 0;\n fill: #ffff00;\n fill-opacity: 0.4;\n }\n .line-marker .render-visual {\n stroke: #ff0000;\n stroke-width: 3;\n fill: transparent;\n }\n .arrow-marker .render-visual {\n stroke: #ff0000;\n stroke-width: 3;\n fill: transparent;\n }\n .arrow-marker-tip {\n stroke-width: 0;\n fill: #ff0000;\n }\n .text-marker text {\n fill: #ff0000;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\",\n Roboto, Helvetica, Arial, sans-serif, \"Apple Color Emoji\",\n \"Segoe UI Emoji\", \"Segoe UI Symbol\";\n }\n .fc-whiteboard-rect-control-box .fc-whiteboard-rect-control-rect {\n stroke: black;\n stroke-width: 1;\n stroke-opacity: 0.5;\n stroke-dasharray: 3, 2;\n fill: transparent;\n }\n .fc-whiteboard-control-grip {\n fill: #cccccc;\n stroke: #333333;\n stroke-width: 2;\n }\n ";
  195. _this.boardCanvas.appendChild(editorStyleSheet);
  196. };
  197. _this.toolbarClick = function (ev, toolbarItem) {
  198. if (toolbarItem.markerType) {
  199. _this.addMarker(toolbarItem.markerType);
  200. }
  201. else {
  202. switch (toolbarItem.name) {
  203. case 'delete': {
  204. _this.deleteActiveMarker();
  205. break;
  206. }
  207. case 'pointer': {
  208. if (_this.activeMarker) {
  209. _this.selectMarker(null);
  210. }
  211. break;
  212. }
  213. case 'close': {
  214. _this.cancel();
  215. break;
  216. }
  217. case 'ok': {
  218. _this.complete();
  219. break;
  220. }
  221. default:
  222. break;
  223. }
  224. }
  225. };
  226. _this.selectMarker = function (marker) {
  227. if (_this.activeMarker && _this.activeMarker !== marker) {
  228. _this.activeMarker.deselect();
  229. }
  230. _this.activeMarker = marker;
  231. };
  232. _this.deleteMarker = function (marker) {
  233. _this.boardCanvas.removeChild(marker.visual);
  234. if (_this.activeMarker === marker) {
  235. _this.activeMarker = null;
  236. }
  237. _this.markers.splice(_this.markers.indexOf(marker), 1);
  238. };
  239. _this.complete = function () {
  240. _this.selectMarker(null);
  241. _this.startRender(_this.renderFinishedClose);
  242. };
  243. _this.cancel = function () {
  244. _this.close();
  245. if (_this.onCancel) {
  246. _this.onCancel();
  247. }
  248. };
  249. _this.renderFinished = function (dataUrl) {
  250. _this.onComplete(dataUrl);
  251. };
  252. _this.renderFinishedClose = function (dataUrl) {
  253. _this.close();
  254. _this.onComplete(dataUrl);
  255. };
  256. if (page) {
  257. _this.page = page;
  258. }
  259. _this.markers = [];
  260. _this.activeMarker = null;
  261. _this.toolbars = toolbar_items_1.getToolbars(page);
  262. if (onChange) {
  263. _this.onChange = onChange;
  264. }
  265. return _this;
  266. }
  267. Object.defineProperty(Drawboard.prototype, "markerMap", {
  268. get: function () {
  269. var map = {};
  270. this.markers.forEach(function (marker) {
  271. map[marker.id] = marker;
  272. });
  273. return map;
  274. },
  275. enumerable: true,
  276. configurable: true
  277. });
  278. return Drawboard;
  279. }(index_1.Baseboard));
  280. exports.Drawboard = Drawboard;