Nessuna descrizione

MarkerArea.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var Activator_1 = require("./Activator");
  4. var SvgHelper_1 = require("../utils/SvgHelper");
  5. var Renderer_1 = require("./Renderer");
  6. var Toolbar_1 = require("../toolbar/Toolbar");
  7. var ArrowMarkerToolbarItem_1 = require("../markers/arrow/ArrowMarkerToolbarItem");
  8. var CoverMarkerToolbarItem_1 = require("../markers/cover/CoverMarkerToolbarItem");
  9. var HighlightMarkerToolbarItem_1 = require("../markers/highlight/HighlightMarkerToolbarItem");
  10. var LineMarkerToolbarItem_1 = require("../markers/line/LineMarkerToolbarItem");
  11. var RectMarkerToolbarItem_1 = require("../markers/rect/RectMarkerToolbarItem");
  12. var TextMarkerToolbarItem_1 = require("../markers/text/TextMarkerToolbarItem");
  13. var check_svg_1 = require("./assets/core-toolbar-icons/check.svg");
  14. var eraser_svg_1 = require("./assets/core-toolbar-icons/eraser.svg");
  15. var mouse_pointer_svg_1 = require("./assets/core-toolbar-icons/mouse-pointer.svg");
  16. var times_svg_1 = require("./assets/core-toolbar-icons/times.svg");
  17. var markerjs_logo_m_svg_1 = require("./assets/fc-whiteboardlogo-m.svg");
  18. var MarkerArea = (function () {
  19. function MarkerArea(target) {
  20. var _this = this;
  21. this.toolbars = [
  22. {
  23. icon: mouse_pointer_svg_1.default,
  24. name: 'pointer',
  25. tooltipText: 'Pointer'
  26. },
  27. {
  28. icon: eraser_svg_1.default,
  29. name: 'delete',
  30. tooltipText: 'Delete'
  31. },
  32. {
  33. name: 'separator',
  34. tooltipText: ''
  35. },
  36. new RectMarkerToolbarItem_1.RectMarkerToolbarItem(),
  37. new CoverMarkerToolbarItem_1.CoverMarkerToolbarItem(),
  38. new HighlightMarkerToolbarItem_1.HighlightMarkerToolbarItem(),
  39. new LineMarkerToolbarItem_1.LineMarkerToolbarItem(),
  40. new ArrowMarkerToolbarItem_1.ArrowMarkerToolbarItem(),
  41. new TextMarkerToolbarItem_1.TextMarkerToolbarItem(),
  42. {
  43. name: 'separator',
  44. tooltipText: ''
  45. },
  46. {
  47. icon: check_svg_1.default,
  48. name: 'ok',
  49. tooltipText: 'OK'
  50. },
  51. {
  52. icon: times_svg_1.default,
  53. name: 'close',
  54. tooltipText: 'Close'
  55. }
  56. ];
  57. this.scale = 1.0;
  58. this.show = function (completeCallback, cancelCallback) {
  59. _this.completeCallback = completeCallback;
  60. if (cancelCallback) {
  61. _this.cancelCallback = cancelCallback;
  62. }
  63. _this.open();
  64. _this.showUI();
  65. };
  66. this.open = function () {
  67. _this.setTargetRect();
  68. _this.initMarkerCanvas();
  69. _this.attachEvents();
  70. _this.setStyles();
  71. if (!Activator_1.Activator.isLicensed) {
  72. _this.adLogo();
  73. }
  74. window.addEventListener('resize', _this.adjustUI);
  75. };
  76. this.render = function (completeCallback, cancelCallback) {
  77. _this.completeCallback = completeCallback;
  78. if (cancelCallback) {
  79. _this.cancelCallback = cancelCallback;
  80. }
  81. _this.selectMarker(null);
  82. _this.startRender(_this.renderFinished);
  83. };
  84. this.close = function () {
  85. if (_this.toolbarUI) {
  86. document.body.removeChild(_this.toolbarUI);
  87. }
  88. if (_this.markerImage) {
  89. document.body.removeChild(_this.markerImageHolder);
  90. }
  91. if (_this.logoUI) {
  92. document.body.removeChild(_this.logoUI);
  93. }
  94. };
  95. this.addMarker = function (markerType) {
  96. var marker = markerType.createMarker();
  97. marker.onSelected = _this.selectMarker;
  98. if (marker.defs && marker.defs.length > 0) {
  99. for (var _i = 0, _a = marker.defs; _i < _a.length; _i++) {
  100. var d = _a[_i];
  101. if (d.id && !_this.markerImage.getElementById(d.id)) {
  102. _this.defs.appendChild(d);
  103. }
  104. }
  105. }
  106. _this.markers.push(marker);
  107. _this.selectMarker(marker);
  108. _this.markerImage.appendChild(marker.visual);
  109. var bbox = marker.visual.getBBox();
  110. var x = _this.width / 2 / _this.scale - bbox.width / 2;
  111. var y = _this.height / 2 / _this.scale - bbox.height / 2;
  112. var translate = marker.visual.transform.baseVal.getItem(0);
  113. translate.setMatrix(translate.matrix.translate(x, y));
  114. marker.visual.transform.baseVal.replaceItem(translate, 0);
  115. };
  116. this.deleteActiveMarker = function () {
  117. if (_this.activeMarker) {
  118. _this.deleteMarker(_this.activeMarker);
  119. }
  120. };
  121. this.setTargetRect = function () {
  122. var targetRect = _this.target.getBoundingClientRect();
  123. var bodyRect = document.body.parentElement.getBoundingClientRect();
  124. _this.targetRect = {
  125. left: targetRect.left - bodyRect.left,
  126. top: targetRect.top - bodyRect.top
  127. };
  128. };
  129. this.startRender = function (done) {
  130. var renderer = new Renderer_1.Renderer();
  131. renderer.rasterize(_this.target, _this.markerImage, done);
  132. };
  133. this.attachEvents = function () {
  134. _this.markerImage.addEventListener('mousedown', _this.mouseDown);
  135. _this.markerImage.addEventListener('mousemove', _this.mouseMove);
  136. _this.markerImage.addEventListener('mouseup', _this.mouseUp);
  137. };
  138. this.mouseDown = function (ev) {
  139. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  140. _this.activeMarker.deselect();
  141. _this.activeMarker = null;
  142. }
  143. };
  144. this.mouseMove = function (ev) {
  145. if (_this.activeMarker && (ev.buttons & 1) > 0) {
  146. _this.activeMarker.manipulate(ev);
  147. }
  148. };
  149. this.mouseUp = function (ev) {
  150. if (_this.activeMarker) {
  151. _this.activeMarker.endManipulation();
  152. }
  153. };
  154. this.initMarkerCanvas = function () {
  155. _this.markerImageHolder = document.createElement('div');
  156. _this.markerImageHolder.style.setProperty('touch-action', 'none');
  157. _this.markerImageHolder.style.setProperty('-ms-touch-action', 'none');
  158. _this.markerImage = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  159. _this.markerImage.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
  160. _this.markerImage.setAttribute('width', _this.width.toString());
  161. _this.markerImage.setAttribute('height', _this.height.toString());
  162. _this.markerImage.setAttribute('viewBox', '0 0 ' + _this.width.toString() + ' ' + _this.height.toString());
  163. _this.markerImageHolder.style.position = 'absolute';
  164. _this.markerImageHolder.style.width = _this.width + "px";
  165. _this.markerImageHolder.style.height = _this.height + "px";
  166. _this.markerImageHolder.style.transformOrigin = 'top left';
  167. _this.positionMarkerImage();
  168. _this.defs = SvgHelper_1.SvgHelper.createDefs();
  169. _this.markerImage.appendChild(_this.defs);
  170. _this.markerImageHolder.appendChild(_this.markerImage);
  171. document.body.appendChild(_this.markerImageHolder);
  172. };
  173. this.adjustUI = function (ev) {
  174. _this.adjustSize();
  175. _this.positionUI();
  176. };
  177. this.adjustSize = function () {
  178. _this.width = _this.target.clientWidth;
  179. _this.height = _this.target.clientHeight;
  180. var scale = _this.target.clientWidth / _this.markerImageHolder.clientWidth;
  181. if (scale !== 1.0) {
  182. _this.scale *= scale;
  183. _this.markerImageHolder.style.width = _this.width + "px";
  184. _this.markerImageHolder.style.height = _this.height + "px";
  185. _this.markerImageHolder.style.transform = "scale(" + _this.scale + ")";
  186. }
  187. };
  188. this.positionUI = function () {
  189. _this.setTargetRect();
  190. _this.positionMarkerImage();
  191. _this.positionToolbar();
  192. if (_this.logoUI) {
  193. _this.positionLogo();
  194. }
  195. };
  196. this.positionMarkerImage = function () {
  197. _this.markerImageHolder.style.top = _this.targetRect.top + 'px';
  198. _this.markerImageHolder.style.left = _this.targetRect.left + 'px';
  199. };
  200. this.positionToolbar = function () {
  201. _this.toolbarUI.style.left = _this.targetRect.left +
  202. _this.target.offsetWidth -
  203. _this.toolbarUI.clientWidth + "px";
  204. _this.toolbarUI.style.top = _this.targetRect.top - _this.toolbarUI.clientHeight + "px";
  205. };
  206. this.showUI = function () {
  207. _this.toolbar = new Toolbar_1.Toolbar(_this.toolbars, _this.toolbarClick);
  208. _this.toolbarUI = _this.toolbar.getUI();
  209. document.body.appendChild(_this.toolbarUI);
  210. _this.toolbarUI.style.position = 'absolute';
  211. _this.positionToolbar();
  212. };
  213. this.setStyles = function () {
  214. var editorStyleSheet = document.createElementNS('http://www.w3.org/2000/svg', 'style');
  215. 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-whiteboardrect-control-box .fc-whiteboardrect-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-whiteboardcontrol-grip {\n fill: #cccccc;\n stroke: #333333;\n stroke-width: 2;\n }\n ";
  216. _this.markerImage.appendChild(editorStyleSheet);
  217. };
  218. this.toolbarClick = function (ev, toolbarItem) {
  219. if (toolbarItem.markerType) {
  220. _this.addMarker(toolbarItem.markerType);
  221. }
  222. else {
  223. switch (toolbarItem.name) {
  224. case 'delete': {
  225. _this.deleteActiveMarker();
  226. break;
  227. }
  228. case 'pointer': {
  229. if (_this.activeMarker) {
  230. _this.selectMarker(null);
  231. }
  232. break;
  233. }
  234. case 'close': {
  235. _this.cancel();
  236. break;
  237. }
  238. case 'ok': {
  239. _this.complete();
  240. break;
  241. }
  242. }
  243. }
  244. };
  245. this.selectMarker = function (marker) {
  246. if (_this.activeMarker && _this.activeMarker !== marker) {
  247. _this.activeMarker.deselect();
  248. }
  249. _this.activeMarker = marker;
  250. };
  251. this.deleteMarker = function (marker) {
  252. _this.markerImage.removeChild(marker.visual);
  253. if (_this.activeMarker === marker) {
  254. _this.activeMarker = null;
  255. }
  256. _this.markers.splice(_this.markers.indexOf(marker), 1);
  257. };
  258. this.complete = function () {
  259. _this.selectMarker(null);
  260. _this.startRender(_this.renderFinishedClose);
  261. };
  262. this.cancel = function () {
  263. _this.close();
  264. if (_this.cancelCallback) {
  265. _this.cancelCallback();
  266. }
  267. };
  268. this.renderFinished = function (dataUrl) {
  269. _this.completeCallback(dataUrl);
  270. };
  271. this.renderFinishedClose = function (dataUrl) {
  272. _this.close();
  273. _this.completeCallback(dataUrl);
  274. };
  275. this.positionLogo = function () {
  276. if (_this.logoUI) {
  277. _this.logoUI.style.left = _this.targetRect.left + 10 + "px";
  278. _this.logoUI.style.top = _this.targetRect.top +
  279. _this.target.offsetHeight -
  280. _this.logoUI.clientHeight -
  281. 10 + "px";
  282. }
  283. };
  284. this.adLogo = function () {
  285. _this.logoUI = document.createElement('div');
  286. _this.logoUI.className = 'fc-whiteboardlogo';
  287. var link = document.createElement('a');
  288. link.href = 'https://markerjs.com/';
  289. link.target = '_blank';
  290. link.innerHTML = markerjs_logo_m_svg_1.default;
  291. link.title = 'Powered by marker.js';
  292. _this.logoUI.appendChild(link);
  293. document.body.appendChild(_this.logoUI);
  294. _this.logoUI.style.position = 'absolute';
  295. _this.positionLogo();
  296. };
  297. this.target = target;
  298. this.width = target.clientWidth;
  299. this.height = target.clientHeight;
  300. this.markers = [];
  301. this.activeMarker = null;
  302. }
  303. return MarkerArea;
  304. }());
  305. exports.MarkerArea = MarkerArea;