utility.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* eslint-disable no-underscore-dangle */
  2. import { fromEvent } from 'rxjs';
  3. import {
  4. auditTime,
  5. throttleTime,
  6. } from 'rxjs/operators';
  7. import { ScrollStateType, CoordType } from '../constants/type';
  8. export const objIsEmpty = (obj: Record<string, any>): boolean => !Object.keys(obj).length;
  9. export const watchScroll = (
  10. viewAreaElement: HTMLElement | null, cb: (state: ScrollStateType) => void,
  11. ): ScrollStateType => {
  12. let rAF: number | null = null;
  13. const element = viewAreaElement as HTMLElement;
  14. const state = {
  15. right: true,
  16. down: true,
  17. lastX: element.scrollLeft,
  18. lastY: element.scrollTop,
  19. subscriber: {},
  20. };
  21. const debounceScroll = (): void => {
  22. if (rAF) {
  23. return;
  24. }
  25. // schedule an invocation of scroll for next animation frame.
  26. rAF = window.requestAnimationFrame(() => {
  27. rAF = null;
  28. const currentX = element.scrollLeft;
  29. const { lastX } = state;
  30. if (currentX !== lastX) {
  31. state.right = currentX > lastX;
  32. }
  33. state.lastX = currentX;
  34. const currentY = element.scrollTop;
  35. const { lastY } = state;
  36. if (currentY !== lastY) {
  37. state.down = currentY > lastY;
  38. }
  39. state.lastY = currentY;
  40. cb(state);
  41. });
  42. };
  43. const subscriber = fromEvent(element, 'scroll').pipe(
  44. throttleTime(160),
  45. auditTime(250),
  46. ).subscribe(debounceScroll);
  47. state.subscriber = subscriber;
  48. return state;
  49. };
  50. export const scrollIntoView = (
  51. element: HTMLElement, spot?: {top: number}, skipOverflowHiddenElements = false,
  52. ): void => {
  53. let parent: HTMLElement = element.offsetParent as HTMLElement;
  54. let offsetY = element.offsetTop + element.clientTop;
  55. if (!parent) {
  56. return; // no need to scroll
  57. }
  58. while (
  59. (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth)
  60. || (skipOverflowHiddenElements && getComputedStyle(parent).overflow === 'hidden')
  61. ) {
  62. if (parent.dataset._scaleY) {
  63. offsetY /= parseInt(parent.dataset._scaleY, 10);
  64. }
  65. offsetY += parent.offsetTop;
  66. parent = parent.offsetParent as HTMLElement;
  67. if (!parent) {
  68. return; // no need to scroll
  69. }
  70. }
  71. if (spot) {
  72. if (spot.top !== undefined) {
  73. offsetY += spot.top;
  74. }
  75. }
  76. parent.scrollTop = offsetY;
  77. };
  78. export const scaleCheck = (scale: number): number => {
  79. if (typeof scale === 'number' && scale >= 50 && scale <= 250) {
  80. return Math.round(scale * 100) / 10000;
  81. }
  82. if (scale < 50) {
  83. return 0.5;
  84. }
  85. return 2.5;
  86. };
  87. export const downloadFileWithUri = (name: string, uri: string): void => {
  88. const ele = document.createElement('a');
  89. ele.download = name;
  90. ele.href = uri;
  91. document.body.appendChild(ele);
  92. ele.click();
  93. document.body.removeChild(ele);
  94. ele.remove();
  95. };
  96. export const uploadFile = (extension: string): Promise<any> => new Promise((resolve) => {
  97. const fileInput = document.createElement('input');
  98. fileInput.type = 'file';
  99. fileInput.accept = extension;
  100. fileInput.onchange = (): void => {
  101. if (fileInput.files) {
  102. const reader = new FileReader();
  103. reader.onload = (): void => {
  104. const contents = reader.result;
  105. resolve(contents);
  106. };
  107. if (extension.includes('xfdf')) {
  108. reader.readAsText(fileInput.files[0]);
  109. } else {
  110. reader.readAsDataURL(fileInput.files[0]);
  111. }
  112. }
  113. };
  114. document.body.appendChild(fileInput);
  115. fileInput.click();
  116. });
  117. const componentToHex = (c: number): string => {
  118. const hex = c.toString(16);
  119. return hex.length === 1 ? `0${hex}` : hex;
  120. };
  121. export const hexToRgb = (hex: string): any => {
  122. const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  123. return result ? {
  124. r: parseInt(result[1], 16),
  125. g: parseInt(result[2], 16),
  126. b: parseInt(result[3], 16),
  127. } : null;
  128. };
  129. export const rgbToHex = (r: number, g: number, b: number): string => (
  130. `#${componentToHex(r)}${componentToHex(g)}${componentToHex(b)}`
  131. );
  132. export const floatToHex = (r: number, g: number, b: number): string => (
  133. rgbToHex(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255))
  134. );
  135. type ScaleDataType = CoordType & {
  136. operator: string;
  137. clickX: number;
  138. clickY: number;
  139. moveX: number;
  140. moveY: number;
  141. };
  142. export const calcDragAndDropScale = ({
  143. top, left, width = 0, height = 0, operator, clickX, clickY, moveX, moveY,
  144. }: ScaleDataType): CoordType => {
  145. let scaleCoord = {
  146. left,
  147. top,
  148. width,
  149. height,
  150. };
  151. switch (operator) {
  152. case 'top':
  153. scaleCoord = {
  154. ...scaleCoord,
  155. top: moveY - (clickY - top),
  156. height: height + (clickY - moveY),
  157. };
  158. break;
  159. case 'top-left':
  160. scaleCoord = {
  161. top: moveY - (clickY - top),
  162. left: moveX - (clickX - left),
  163. height: height + (clickY - moveY),
  164. width: width + (clickX - moveX),
  165. };
  166. break;
  167. case 'left':
  168. scaleCoord = {
  169. ...scaleCoord,
  170. left: moveX - (clickX - left),
  171. width: width + (clickX - moveX),
  172. };
  173. break;
  174. case 'bottom-left':
  175. scaleCoord = {
  176. ...scaleCoord,
  177. left: moveX - (clickX - left),
  178. width: width + (clickX - moveX),
  179. height: height + moveY - clickY,
  180. };
  181. break;
  182. case 'bottom':
  183. scaleCoord = {
  184. ...scaleCoord,
  185. height: height + moveY - clickY,
  186. };
  187. break;
  188. case 'bottom-right':
  189. scaleCoord = {
  190. ...scaleCoord,
  191. width: width + moveX - clickX,
  192. height: height + moveY - clickY,
  193. };
  194. break;
  195. case 'right':
  196. scaleCoord = {
  197. ...scaleCoord,
  198. width: width + moveX - clickX,
  199. };
  200. break;
  201. case 'top-right':
  202. scaleCoord = {
  203. ...scaleCoord,
  204. top: moveY - (clickY - top),
  205. width: width + moveX - clickX,
  206. height: height + (clickY - moveY),
  207. };
  208. break;
  209. default:
  210. break;
  211. }
  212. return scaleCoord;
  213. };
  214. const FRACTIONDIGITS = 2;
  215. type NormalizeRoundFunc = (num: number, fractionDigits?: number) => number;
  216. export const normalizeRound: NormalizeRoundFunc = (num, fractionDigits) => {
  217. const frac = fractionDigits || FRACTIONDIGITS;
  218. return Math.round(num * (10 ** frac)) / (10 ** frac);
  219. };