Нема описа

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 fc_hotkeys_1 = require("fc-hotkeys");
  17. var debounce = require("lodash.debounce");
  18. var index_1 = require("./../Baseboard/index");
  19. var toolbar_items_1 = require("./../../toolbar/toolbar-items");
  20. var Synthetizer_1 = require("../../renderer/Synthetizer");
  21. var Toolbar_1 = require("../../toolbar/Toolbar");
  22. require("./index.less");
  23. var layout_1 = require("../../utils/layout");
  24. var index_2 = require("../../markers/RectMarker/index");
  25. var index_3 = require("../../markers/HighlightMarker/index");
  26. var index_4 = require("../../markers/CoverMarker/index");
  27. var index_5 = require("../../markers/LineMarker/index");
  28. var index_6 = require("../../markers/ArrowMarker/index");
  29. var index_7 = require("../../markers/TextMarker/index");
  30. var Drawboard = (function (_super) {
  31. __extends(Drawboard, _super);
  32. function Drawboard(source, _a) {
  33. var _b = _a === void 0 ? {} : _a, _c = _b.allowKeyboard, allowKeyboard = _c === void 0 ? true : _c, extraToolbarItems = _b.extraToolbarItems, mountContainer = _b.mountContainer, page = _b.page, zIndex = _b.zIndex, onChange = _b.onChange;
  34. var _this = _super.call(this, source) || this;
  35. _this.mountContainer = document.body;
  36. _this.scale = 1.0;
  37. _this.zIndex = 999;
  38. _this.allowKeyboard = true;
  39. _this.onComplete = function () { };
  40. _this.onChange = function () { };
  41. _this.open = function (onComplete, onCancel) {
  42. if (onComplete) {
  43. _this.onComplete = onComplete;
  44. }
  45. if (onCancel) {
  46. _this.onCancel = onCancel;
  47. }
  48. _this.setTargetRect();
  49. _this.initBoard(_this.mountContainer);
  50. _this.attachEvents();
  51. _this.setStyles();
  52. window.addEventListener('resize', _this.adjustUI);
  53. if ((_this.page && _this.page.mode === 'master') || !_this.page) {
  54. _this.showUI();
  55. }
  56. };
  57. _this.hide = function () {
  58. if (_this.source.imgSrc) {
  59. _this.target.style.display = 'none';
  60. }
  61. _this.boardHolder.style.visibility = 'hidden';
  62. _this.boardHolder.style.zIndex = '-1';
  63. if (_this.toolbar) {
  64. _this.toolbar.hide();
  65. }
  66. };
  67. _this.show = function () {
  68. if (_this.source.imgSrc) {
  69. _this.target.style.display = 'block';
  70. }
  71. _this.boardHolder.style.visibility = 'visible';
  72. _this.boardHolder.style.zIndex = "" + _this.zIndex;
  73. if (_this.toolbar) {
  74. _this.toolbar.show();
  75. }
  76. };
  77. _this.destroy = function () {
  78. if (_this.toolbarUI) {
  79. _this.mountContainer.removeChild(_this.toolbarUI);
  80. }
  81. if (_this.boardCanvas) {
  82. _this.mountContainer.removeChild(_this.boardHolder);
  83. }
  84. if (_this.listener) {
  85. _this.listener.reset();
  86. }
  87. };
  88. _this.render = function (onComplete, onCancel) {
  89. _this.onComplete = onComplete;
  90. if (onCancel) {
  91. _this.onCancel = onCancel;
  92. }
  93. _this.selectMarker(null);
  94. _this.startRender(_this.renderFinished);
  95. };
  96. _this.addMarker = function (markerType, _a) {
  97. var _b = _a === void 0 ? {} : _a, id = _b.id, originX = _b.originX, originY = _b.originY;
  98. var marker = markerType.createMarker(_this.page);
  99. if (id) {
  100. marker.id = id;
  101. }
  102. marker.drawboard = _this;
  103. marker.onSelected = _this.selectMarker;
  104. marker.onChange = _this.onChange;
  105. if (marker.defs && marker.defs.length > 0) {
  106. for (var _i = 0, _c = marker.defs; _i < _c.length; _i++) {
  107. var d = _c[_i];
  108. if (d.id && !_this.boardCanvas.getElementById(d.id)) {
  109. _this.defs.appendChild(d);
  110. }
  111. }
  112. }
  113. _this.markers.push(marker);
  114. _this.selectMarker(marker);
  115. _this.boardCanvas.appendChild(marker.visual);
  116. var x;
  117. var y;
  118. if (originX && originY) {
  119. x = originX;
  120. y = originY;
  121. }
  122. else {
  123. var bbox = marker.visual.getBBox();
  124. x = _this.width / 2 / _this.scale - bbox.width / 2;
  125. y = _this.height / 2 / _this.scale - bbox.height / 2;
  126. }
  127. _this.onChange({
  128. target: 'marker',
  129. parentId: _this.page ? _this.page.id : _this.id,
  130. event: 'addMarker',
  131. marker: { type: marker.type, id: marker.id, dx: x, dy: y }
  132. });
  133. marker.moveTo(x, y);
  134. return marker;
  135. };
  136. _this.deleteActiveMarker = function () {
  137. _this.deleteMarkerWithEvent(_this.activeMarker);
  138. };
  139. _this.clearMarkers = function () {
  140. _this.markers.slice().forEach(function (marker) {
  141. _this.deleteMarkerWithEvent(marker);
  142. });
  143. };
  144. _this.deleteMarkerWithEvent = function (marker) {
  145. if (marker) {
  146. if (_this.onChange) {
  147. _this.onChange({
  148. event: 'removeMarker',
  149. id: marker.id,
  150. target: 'marker',
  151. marker: { id: marker.id }
  152. });
  153. }
  154. _this.deleteMarker(marker);
  155. }
  156. };
  157. _this.setTargetRect = function () {
  158. var targetRect = _this.target.getBoundingClientRect();
  159. var bodyRect = document.body.parentElement.getBoundingClientRect();
  160. _this.targetRect = {
  161. left: targetRect.left - bodyRect.left,
  162. top: targetRect.top - bodyRect.top
  163. };
  164. };
  165. _this.startRender = function (done) {
  166. var renderer = new Synthetizer_1.Synthetizer();
  167. renderer.rasterize(_this.target, _this.boardCanvas, done);
  168. };
  169. _this.attachEvents = function () {
  170. _this.boardCanvas.addEventListener('mousedown', _this.mouseDown);
  171. _this.boardCanvas.addEventListener('mousemove', _this.mouseMove);
  172. _this.boardCanvas.addEventListener('mouseup', _this.mouseUp);
  173. };
  174. _this.mouseDown = function (ev) {
  175. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  176. _this.activeMarker.deselect();
  177. _this.activeMarker = null;
  178. }
  179. };
  180. _this.mouseMove = function (ev) {
  181. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  182. _this.activeMarker.manipulate(ev);
  183. }
  184. };
  185. _this.mouseUp = function (ev) {
  186. if (_this.activeMarker) {
  187. _this.activeMarker.endManipulation();
  188. }
  189. };
  190. _this.onKeyboard = function (e, _a) {
  191. var hotkey = _a.hotkey;
  192. switch (hotkey) {
  193. case 'Shift+R':
  194. _this.addMarker(index_2.RectMarker);
  195. return;
  196. case 'Shift+H':
  197. _this.addMarker(index_3.HighlightMarker);
  198. return;
  199. case 'Shift+C':
  200. _this.addMarker(index_4.CoverMarker);
  201. return;
  202. case 'Shift+L':
  203. _this.addMarker(index_5.LineMarker);
  204. return;
  205. case 'Shift+A':
  206. _this.addMarker(index_6.ArrowMarker);
  207. return;
  208. case 'Shift+T':
  209. _this.addMarker(index_7.TextMarker);
  210. return;
  211. case 'ESC':
  212. _this.page.whiteboard.rollbackSnap();
  213. return;
  214. default:
  215. break;
  216. }
  217. if (!_this.activeMarker) {
  218. return;
  219. }
  220. switch (hotkey) {
  221. case 'UP':
  222. _this.activeMarker.move(0, -10);
  223. return;
  224. case 'LEFT':
  225. _this.activeMarker.move(-10, 0);
  226. return;
  227. case 'RIGHT':
  228. _this.activeMarker.move(10, 0);
  229. return;
  230. case 'DOWN':
  231. _this.activeMarker.move(0, 10);
  232. return;
  233. case 'BACKSPACE':
  234. _this.deleteActiveMarker();
  235. return;
  236. default:
  237. return;
  238. }
  239. };
  240. _this.adjustUI = function (ev) {
  241. _this.adjustSize();
  242. _this.positionUI();
  243. };
  244. _this.adjustSize = function () {
  245. _this.width = _this.target.clientWidth;
  246. _this.height = _this.target.clientHeight;
  247. var scale = _this.target.clientWidth / _this.boardHolder.clientWidth;
  248. if (scale !== 1.0) {
  249. _this.scale *= scale;
  250. _this.boardHolder.style.width = _this.width + "px";
  251. _this.boardHolder.style.height = _this.height + "px";
  252. _this.boardHolder.style.transform = "scale(" + _this.scale + ")";
  253. }
  254. };
  255. _this.positionUI = function () {
  256. _this.setTargetRect();
  257. _this.positionBoard();
  258. _this.positionToolbar();
  259. };
  260. _this.positionToolbar = function () {
  261. if (_this.toolbarUI && _this.targetRect) {
  262. _this.toolbarUI.style.left = _this.targetRect.left +
  263. _this.target.offsetWidth -
  264. _this.toolbarUI.clientWidth + "px";
  265. _this.toolbarUI.style.top = _this.targetRect.top - _this.toolbarUI.clientHeight + "px";
  266. }
  267. };
  268. _this.showUI = function () {
  269. _this.toolbar = new Toolbar_1.Toolbar(_this.toolbarItems, _this.toolbarClick);
  270. _this.toolbar.zIndex = _this.zIndex + 1;
  271. _this.toolbarUI = _this.toolbar.getUI(_this);
  272. _this.boardHolder.appendChild(_this.toolbarUI);
  273. _this.toolbarUI.style.position = 'absolute';
  274. _this.positionToolbar();
  275. _this.toolbar.show();
  276. _this.toolbar.toolbarButtons.forEach(function (button) {
  277. if (button.toolbarItem.draggable) {
  278. button.container.draggable = true;
  279. button.container.ondragstart = function (ev) {
  280. if (ev) {
  281. ev.dataTransfer.setData('id', button.id);
  282. }
  283. };
  284. }
  285. });
  286. _this.boardCanvas.ondragover = function (ev) {
  287. ev.preventDefault();
  288. };
  289. _this.boardCanvas.ondrop = function (ev) {
  290. var markerX = ev.x;
  291. var markerY = ev.y;
  292. var rect = _this.boardHolder.getBoundingClientRect();
  293. if (layout_1.rectContains(rect, { x: markerX, y: markerY })) {
  294. var buttonId = ev.dataTransfer.getData('id');
  295. var button = _this.toolbar.toolbarButtonMap[buttonId];
  296. if (button.toolbarItem && button.toolbarItem.markerType) {
  297. _this.addMarker(button.toolbarItem.markerType, {
  298. originX: markerX - rect.left,
  299. originY: markerY - rect.top
  300. });
  301. }
  302. }
  303. };
  304. };
  305. _this.setStyles = function () {
  306. var editorStyleSheet = document.createElementNS('http://www.w3.org/2000/svg', 'style');
  307. 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 ";
  308. _this.boardCanvas.appendChild(editorStyleSheet);
  309. };
  310. _this.toolbarClick = function (ev, toolbarItem) {
  311. if (toolbarItem.onClick) {
  312. toolbarItem.onClick();
  313. }
  314. else if (toolbarItem.markerType) {
  315. _this.addMarker(toolbarItem.markerType);
  316. }
  317. else {
  318. switch (toolbarItem.name) {
  319. case 'delete': {
  320. _this.deleteActiveMarker();
  321. break;
  322. }
  323. case 'pointer': {
  324. if (_this.activeMarker) {
  325. _this.selectMarker(null);
  326. }
  327. break;
  328. }
  329. case 'close': {
  330. _this.cancel();
  331. break;
  332. }
  333. case 'ok': {
  334. _this.complete();
  335. break;
  336. }
  337. default:
  338. break;
  339. }
  340. }
  341. };
  342. _this.selectMarker = function (marker) {
  343. if (_this.activeMarker && _this.activeMarker !== marker) {
  344. _this.activeMarker.deselect();
  345. }
  346. _this.activeMarker = marker;
  347. };
  348. _this.deleteMarker = function (marker) {
  349. _this.boardCanvas.removeChild(marker.visual);
  350. if (_this.activeMarker === marker) {
  351. _this.activeMarker = null;
  352. }
  353. _this.markers.splice(_this.markers.indexOf(marker), 1);
  354. };
  355. _this.complete = function () {
  356. _this.selectMarker(null);
  357. _this.startRender(_this.renderFinishedClose);
  358. };
  359. _this.cancel = function () {
  360. _this.destroy();
  361. if (_this.onCancel) {
  362. _this.onCancel();
  363. }
  364. };
  365. _this.renderFinished = function (dataUrl) {
  366. _this.onComplete(dataUrl);
  367. };
  368. _this.renderFinishedClose = function (dataUrl) {
  369. _this.destroy();
  370. _this.onComplete(dataUrl);
  371. };
  372. if (page) {
  373. _this.page = page;
  374. }
  375. if (zIndex) {
  376. _this.zIndex = zIndex;
  377. }
  378. _this.allowKeyboard = allowKeyboard;
  379. _this.markers = [];
  380. _this.activeMarker = null;
  381. var toolbarItems = toolbar_items_1.getToolbars(page);
  382. if (extraToolbarItems) {
  383. toolbarItems.push.apply(toolbarItems, extraToolbarItems);
  384. }
  385. _this.toolbarItems = toolbarItems;
  386. if (onChange) {
  387. _this.onChange = onChange;
  388. }
  389. if (allowKeyboard && _this.page && _this.page.mode === 'master') {
  390. _this.listener = new fc_hotkeys_1.HotkeysListener();
  391. _this.listener.on(fc_hotkeys_1.KEY_ALL, debounce(_this.onKeyboard, 150));
  392. }
  393. if (mountContainer) {
  394. _this.mountContainer = mountContainer;
  395. }
  396. return _this;
  397. }
  398. Object.defineProperty(Drawboard.prototype, "markerMap", {
  399. get: function () {
  400. var map = {};
  401. this.markers.forEach(function (marker) {
  402. map[marker.id] = marker;
  403. });
  404. return map;
  405. },
  406. enumerable: true,
  407. configurable: true
  408. });
  409. return Drawboard;
  410. }(index_1.Baseboard));
  411. exports.Drawboard = Drawboard;