Nav apraksta

index.ts 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import { WhitePageSource } from './../types';
  2. import { Baseboard } from './../Baseboard/index';
  3. import { BaseMarker } from './../../markers/BaseMarker/index';
  4. import { getToolbars } from './../../toolbar/toolbar-items';
  5. import { WhitePage } from './../WhitePage/index';
  6. import { onSyncFunc } from './../../event/Event';
  7. import { Synthetizer } from '../../renderer/Synthetizer';
  8. import { Toolbar } from '../../toolbar/Toolbar';
  9. import { ToolbarItem } from '../../toolbar/ToolbarItem';
  10. import './index.less';
  11. export class Drawboard extends Baseboard {
  12. /** Options */
  13. private scale = 1.0;
  14. /** 句柄 */
  15. page: WhitePage;
  16. private markers: BaseMarker[];
  17. get markerMap(): { [key: string]: BaseMarker } {
  18. const map = {};
  19. this.markers.forEach(marker => {
  20. map[marker.id] = marker;
  21. });
  22. return map;
  23. }
  24. private activeMarker: BaseMarker | null;
  25. private toolbar: Toolbar;
  26. private toolbars: ToolbarItem[];
  27. private toolbarUI: HTMLElement;
  28. /** 回调 */
  29. private onComplete: (dataUrl: string) => void = () => {};
  30. private onChange: onSyncFunc = () => {};
  31. private onCancel: () => void;
  32. constructor(
  33. source: WhitePageSource,
  34. { page, onChange }: { page?: WhitePage; onChange?: onSyncFunc } = {}
  35. ) {
  36. super(source);
  37. if (page) {
  38. this.page = page;
  39. }
  40. this.markers = [];
  41. this.activeMarker = null;
  42. this.toolbars = getToolbars(page);
  43. if (onChange) {
  44. this.onChange = onChange;
  45. }
  46. }
  47. /** @region LifeCycle open - hide - show - ... - close */
  48. /** 打开画板 */
  49. public open = (onComplete?: (dataUrl: string) => void, onCancel?: () => void) => {
  50. if (onComplete) {
  51. this.onComplete = onComplete;
  52. }
  53. if (onCancel) {
  54. this.onCancel = onCancel;
  55. }
  56. this.setTargetRect();
  57. this.initBoard();
  58. this.attachEvents();
  59. this.setStyles();
  60. window.addEventListener('resize', this.adjustUI);
  61. if (this.page.mode === 'master') {
  62. this.showUI();
  63. }
  64. };
  65. public hide = () => {
  66. if (this.source.imgSrc) {
  67. this.target.style.display = 'none';
  68. }
  69. // 这里不使用 display:none,是为了保证在隐藏时候仍然可以执行更新
  70. this.boardHolder.style.opacity = '0';
  71. this.boardHolder.style.zIndex = '-1';
  72. if (this.toolbar) {
  73. this.toolbar.hide();
  74. }
  75. };
  76. public show = () => {
  77. if (this.source.imgSrc) {
  78. this.target.style.display = 'block';
  79. }
  80. this.boardHolder.style.opacity = '1';
  81. this.boardHolder.style.zIndex = '9999';
  82. if (this.toolbar) {
  83. this.toolbar.show();
  84. }
  85. };
  86. public close = () => {
  87. if (this.toolbarUI) {
  88. document.body.removeChild(this.toolbarUI);
  89. }
  90. if (this.boardCanvas) {
  91. document.body.removeChild(this.boardHolder);
  92. }
  93. };
  94. public render = (onComplete: (dataUrl: string) => void, onCancel?: () => void) => {
  95. this.onComplete = onComplete;
  96. if (onCancel) {
  97. this.onCancel = onCancel;
  98. }
  99. this.selectMarker(null);
  100. this.startRender(this.renderFinished);
  101. };
  102. public addMarker = (markerType: typeof BaseMarker, { id }: { id?: string } = {}) => {
  103. // 假如 Drawboard 存在 Page 引用,则传导给 Marker
  104. const marker = markerType.createMarker(this.page);
  105. if (id) {
  106. marker.id = id;
  107. }
  108. marker.onSelected = this.selectMarker;
  109. marker.onChange = this.onChange;
  110. if (marker.defs && marker.defs.length > 0) {
  111. for (const d of marker.defs) {
  112. if (d.id && !this.boardCanvas.getElementById(d.id)) {
  113. this.defs.appendChild(d);
  114. }
  115. }
  116. }
  117. // 触发事件流
  118. this.onChange({
  119. target: 'marker',
  120. parentId: this.page ? this.page.id : this.id,
  121. event: 'add',
  122. data: { type: marker.type, id: marker.id }
  123. });
  124. this.markers.push(marker);
  125. this.selectMarker(marker);
  126. this.boardCanvas.appendChild(marker.visual);
  127. const bbox = marker.visual.getBBox();
  128. const x = this.width / 2 / this.scale - bbox.width / 2;
  129. const y = this.height / 2 / this.scale - bbox.height / 2;
  130. const translate = marker.visual.transform.baseVal.getItem(0);
  131. translate.setMatrix(translate.matrix.translate(x, y));
  132. marker.visual.transform.baseVal.replaceItem(translate, 0);
  133. };
  134. public deleteActiveMarker = () => {
  135. if (this.activeMarker) {
  136. // 触发事件
  137. if (this.onChange) {
  138. this.onChange({
  139. event: 'remove',
  140. id: this.activeMarker.id,
  141. target: 'marker',
  142. data: { id: this.activeMarker.id }
  143. });
  144. }
  145. this.deleteMarker(this.activeMarker);
  146. }
  147. };
  148. private setTargetRect = () => {
  149. const targetRect = this.target.getBoundingClientRect() as DOMRect;
  150. const bodyRect = document.body.parentElement!.getBoundingClientRect();
  151. this.targetRect = {
  152. left: targetRect.left - bodyRect.left,
  153. top: targetRect.top - bodyRect.top
  154. } as ClientRect;
  155. };
  156. private startRender = (done: (dataUrl: string) => void) => {
  157. const renderer = new Synthetizer();
  158. renderer.rasterize(this.target, this.boardCanvas, done);
  159. };
  160. private attachEvents = () => {
  161. this.boardCanvas.addEventListener('mousedown', this.mouseDown);
  162. this.boardCanvas.addEventListener('mousemove', this.mouseMove);
  163. this.boardCanvas.addEventListener('mouseup', this.mouseUp);
  164. };
  165. private mouseDown = (ev: MouseEvent) => {
  166. /* tslint:disable:no-bitwise */
  167. if (this.activeMarker && (ev.buttons & 1) > 0) {
  168. this.activeMarker.deselect();
  169. this.activeMarker = null;
  170. }
  171. };
  172. private mouseMove = (ev: MouseEvent) => {
  173. /* tslint:disable:no-bitwise */
  174. if (this.activeMarker && (ev.buttons & 1) > 0) {
  175. this.activeMarker.manipulate(ev);
  176. }
  177. };
  178. private mouseUp = (ev: MouseEvent) => {
  179. if (this.activeMarker) {
  180. this.activeMarker.endManipulation();
  181. }
  182. };
  183. private adjustUI = (ev: UIEvent) => {
  184. this.adjustSize();
  185. this.positionUI();
  186. };
  187. private adjustSize = () => {
  188. this.width = this.target.clientWidth;
  189. this.height = this.target.clientHeight;
  190. const scale = this.target.clientWidth / this.boardHolder.clientWidth;
  191. if (scale !== 1.0) {
  192. this.scale *= scale;
  193. this.boardHolder.style.width = `${this.width}px`;
  194. this.boardHolder.style.height = `${this.height}px`;
  195. this.boardHolder.style.transform = `scale(${this.scale})`;
  196. }
  197. };
  198. private positionUI = () => {
  199. this.setTargetRect();
  200. this.positionBoard();
  201. this.positionToolbar();
  202. };
  203. private positionToolbar = () => {
  204. this.toolbarUI.style.left = `${this.targetRect.left +
  205. this.target.offsetWidth -
  206. this.toolbarUI.clientWidth}px`;
  207. this.toolbarUI.style.top = `${this.targetRect.top - this.toolbarUI.clientHeight}px`;
  208. };
  209. private showUI = () => {
  210. this.toolbar = new Toolbar(this.toolbars, this.toolbarClick);
  211. this.toolbarUI = this.toolbar.getUI();
  212. document.body.appendChild(this.toolbarUI);
  213. this.toolbarUI.style.position = 'absolute';
  214. this.positionToolbar();
  215. };
  216. private setStyles = () => {
  217. const editorStyleSheet = document.createElementNS('http://www.w3.org/2000/svg', 'style');
  218. editorStyleSheet.innerHTML = `
  219. .rect-marker .render-visual {
  220. stroke: #ff0000;
  221. stroke-width: 3;
  222. fill: transparent;
  223. }
  224. .cover-marker .render-visual {
  225. stroke-width: 0;
  226. fill: #000000;
  227. }
  228. .highlight-marker .render-visual {
  229. stroke: transparent;
  230. stroke-width: 0;
  231. fill: #ffff00;
  232. fill-opacity: 0.4;
  233. }
  234. .line-marker .render-visual {
  235. stroke: #ff0000;
  236. stroke-width: 3;
  237. fill: transparent;
  238. }
  239. .arrow-marker .render-visual {
  240. stroke: #ff0000;
  241. stroke-width: 3;
  242. fill: transparent;
  243. }
  244. .arrow-marker-tip {
  245. stroke-width: 0;
  246. fill: #ff0000;
  247. }
  248. .text-marker text {
  249. fill: #ff0000;
  250. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
  251. Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji",
  252. "Segoe UI Emoji", "Segoe UI Symbol";
  253. }
  254. .fc-whiteboard-rect-control-box .fc-whiteboard-rect-control-rect {
  255. stroke: black;
  256. stroke-width: 1;
  257. stroke-opacity: 0.5;
  258. stroke-dasharray: 3, 2;
  259. fill: transparent;
  260. }
  261. .fc-whiteboard-control-grip {
  262. fill: #cccccc;
  263. stroke: #333333;
  264. stroke-width: 2;
  265. }
  266. `;
  267. this.boardCanvas.appendChild(editorStyleSheet);
  268. };
  269. private toolbarClick = (ev: MouseEvent, toolbarItem: ToolbarItem) => {
  270. if (toolbarItem.markerType) {
  271. this.addMarker(toolbarItem.markerType);
  272. } else {
  273. // command button
  274. switch (toolbarItem.name) {
  275. case 'delete': {
  276. this.deleteActiveMarker();
  277. break;
  278. }
  279. case 'pointer': {
  280. if (this.activeMarker) {
  281. this.selectMarker(null);
  282. }
  283. break;
  284. }
  285. case 'close': {
  286. this.cancel();
  287. break;
  288. }
  289. case 'ok': {
  290. this.complete();
  291. break;
  292. }
  293. default:
  294. break;
  295. }
  296. }
  297. };
  298. private selectMarker = (marker: BaseMarker | null) => {
  299. if (this.activeMarker && this.activeMarker !== marker) {
  300. this.activeMarker.deselect();
  301. }
  302. this.activeMarker = marker;
  303. };
  304. public deleteMarker = (marker: BaseMarker) => {
  305. this.boardCanvas.removeChild(marker.visual);
  306. if (this.activeMarker === marker) {
  307. this.activeMarker = null;
  308. }
  309. this.markers.splice(this.markers.indexOf(marker), 1);
  310. };
  311. private complete = () => {
  312. this.selectMarker(null);
  313. this.startRender(this.renderFinishedClose);
  314. };
  315. private cancel = () => {
  316. this.close();
  317. if (this.onCancel) {
  318. this.onCancel();
  319. }
  320. };
  321. private renderFinished = (dataUrl: string) => {
  322. this.onComplete(dataUrl);
  323. };
  324. private renderFinishedClose = (dataUrl: string) => {
  325. this.close();
  326. this.onComplete(dataUrl);
  327. };
  328. }