123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import _ from 'lodash';
- import {
- PositionType, HTMLCoordinateType, PointType, AnnotationPositionType, LinePositionType,
- } from '../constants/type';
- export const rectCalc = (
- position: PositionType, viewHeight: number, scale: number,
- ): HTMLCoordinateType => ({
- top: viewHeight - position.top * scale,
- left: position.left * scale,
- width: (position.right - position.left) * scale,
- height: (position.top - position.bottom) * scale,
- });
- export const getPosition = (type: string, element: Record<string, any>): any => {
- switch (type) {
- case 'Ink': {
- const gestures: any[] = [];
- let nodes: any = element.childNodes[1].childNodes;
- nodes = Array.prototype.slice.call(nodes);
- nodes.forEach((ele: HTMLElement) => {
- if (ele.tagName === 'gesture') {
- const points: PointType[] = [];
- const gestureArray = ele.innerHTML.split(';');
- gestureArray.forEach((ele1: string) => {
- const pointArr = ele1.split(',');
- const point = {
- x: parseInt(pointArr[0], 10),
- y: parseInt(pointArr[1], 10),
- };
- points.push(point);
- });
- gestures.push(points);
- }
- });
- return gestures;
- }
- case 'Line': {
- const start = element.attributes.start.value.split(',');
- const end = element.attributes.end.value.split(',');
- return {
- start: {
- x: start[0],
- y: start[1],
- },
- end: {
- x: end[0],
- y: end[1],
- },
- };
- }
- case 'Square':
- case 'Circle': {
- const width = parseInt(element.attributes.width.value, 10);
- const rect = element.attributes.rect.value.split(',');
- return {
- left: parseInt(rect[0], 10) + width,
- bottom: parseInt(rect[1], 10) + width,
- right: parseInt(rect[2], 10) - width,
- top: parseInt(rect[3], 10) - width,
- };
- }
- case 'Text':
- case 'FreeText': {
- const rect = element.attributes.rect.value.split(',');
- return {
- left: parseInt(rect[0], 10),
- bottom: parseInt(rect[1], 10),
- right: parseInt(rect[2], 10),
- top: parseInt(rect[3], 10),
- };
- }
- case 'Highlight':
- case 'Underline':
- case 'Squiggly':
- case 'StrikeOut': {
- let tempArray: any[] = [];
- if (element.attributes.coords) {
- const coords = element.attributes.coords.value.split(',');
- tempArray = _.chunk(coords, 8);
- }
- const position = tempArray.map((ele: string[]) => ({
- left: parseInt(ele[0], 10),
- bottom: parseInt(ele[1], 10),
- right: parseInt(ele[2], 10),
- top: parseInt(ele[5], 10),
- }));
- return position;
- }
- default:
- return '';
- }
- };
- export const parsePositionForBackend = (
- type: string, position: AnnotationPositionType, pageHeight: number, scale: number,
- ): AnnotationPositionType => {
- switch (type) {
- case 'Highlight':
- case 'Underline':
- case 'Squiggly':
- case 'StrikeOut': {
- const coercionPositionTypeArray = position as PositionType[];
- return coercionPositionTypeArray.map((ele: PositionType) => ({
- left: ele.left / scale,
- bottom: (pageHeight - ele.bottom) / scale,
- right: ele.right / scale,
- top: (pageHeight - ele.top) / scale,
- }));
- }
- case 'Square':
- case 'Circle':
- case 'FreeText':
- case 'Text': {
- const coercionPositionType = position as PositionType;
- return {
- left: coercionPositionType.left / scale,
- bottom: (pageHeight - coercionPositionType.bottom) / scale,
- right: coercionPositionType.right / scale,
- top: (pageHeight - coercionPositionType.top) / scale,
- };
- }
- case 'Line': {
- const { start, end } = position as LinePositionType;
- return {
- start: {
- x: start.x / scale,
- y: (pageHeight - start.y) / scale,
- },
- end: {
- x: end.x / scale,
- y: (pageHeight - end.y) / scale,
- },
- };
- }
- case 'Ink': {
- const points = position as PointType;
- return {
- x: points.x / scale,
- y: (pageHeight - points.y) / scale,
- };
- }
- default:
- return {
- left: 0,
- bottom: 0,
- right: 0,
- top: 0,
- };
- }
- };
- type GetAbsoluteCoordinateType = (
- (
- parentElement: HTMLElement | SVGSVGElement | SVGCircleElement | null,
- clickEvent: MouseEvent | React.MouseEvent
- ) => { x: number; y: number }
- );
- export const getAbsoluteCoordinate: GetAbsoluteCoordinateType = (parentElement, clickEvent) => {
- if (!parentElement) return { x: 0, y: 0 };
- const {
- pageX,
- pageY,
- } = clickEvent;
- const rect = parentElement.getBoundingClientRect();
- const offsetX = window.pageXOffset || window.scrollX || 0;
- const offsetY = window.pageYOffset || window.scrollY || 0;
- const coordinate = {
- x: pageX - rect.left - offsetX,
- y: pageY - rect.top - offsetY,
- };
- return coordinate;
- };
- export default rectCalc;
|