暫無描述

index.ts 11KB

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