説明なし

index.ts 10KB

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