|
@@ -1,15 +1,16 @@
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
import { ANNOTATION_TYPE } from '../constants';
|
|
import { ANNOTATION_TYPE } from '../constants';
|
|
-import {
|
|
|
|
- AnnotationType, PositionType, ViewportType,
|
|
|
|
-} from '../constants/type';
|
|
|
|
|
|
+import { AnnotationType, PositionType, ViewportType } from '../constants/type';
|
|
import { getPdfPage, renderTextLayer } from './pdf';
|
|
import { getPdfPage, renderTextLayer } from './pdf';
|
|
import { getPosition, parsePositionForBackend } from './position';
|
|
import { getPosition, parsePositionForBackend } from './position';
|
|
import { normalizeRound, floatToHex } from './utility';
|
|
import { normalizeRound, floatToHex } from './utility';
|
|
import { xmlParser, getElementsByTagName } from './dom';
|
|
import { xmlParser, getElementsByTagName } from './dom';
|
|
|
|
|
|
-type GetFontAttributeFunc = (type: string, element: Record<string, any>) => Record<string, any>;
|
|
|
|
|
|
+type GetFontAttributeFunc = (
|
|
|
|
+ type: string,
|
|
|
|
+ element: Record<string, any>
|
|
|
|
+) => Record<string, any>;
|
|
|
|
|
|
const getContent = (type: string, element: Record<string, any>): string => {
|
|
const getContent = (type: string, element: Record<string, any>): string => {
|
|
if (type !== 'Text' && type !== 'FreeText') return '';
|
|
if (type !== 'Text' && type !== 'FreeText') return '';
|
|
@@ -28,13 +29,18 @@ const getContent = (type: string, element: Record<string, any>): string => {
|
|
|
|
|
|
const getFontAttribute: GetFontAttributeFunc = (type, element) => {
|
|
const getFontAttribute: GetFontAttributeFunc = (type, element) => {
|
|
if (type !== 'FreeText') return {};
|
|
if (type !== 'FreeText') return {};
|
|
- const appearanceString = element.childNodes[1].innerHTML || element.childNodes[1].textContent;
|
|
|
|
|
|
+ const appearanceString =
|
|
|
|
+ element.childNodes[1].innerHTML || element.childNodes[1].textContent;
|
|
const arr = appearanceString.split(' ');
|
|
const arr = appearanceString.split(' ');
|
|
|
|
|
|
return {
|
|
return {
|
|
fontsize: parseInt(arr[5], 10),
|
|
fontsize: parseInt(arr[5], 10),
|
|
fontname: arr[4].substr(1),
|
|
fontname: arr[4].substr(1),
|
|
- textcolor: floatToHex(parseFloat(arr[0]), parseFloat(arr[1]), parseFloat(arr[2])),
|
|
|
|
|
|
+ textcolor: floatToHex(
|
|
|
|
+ parseFloat(arr[0]),
|
|
|
|
+ parseFloat(arr[1]),
|
|
|
|
+ parseFloat(arr[2])
|
|
|
|
+ ),
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
@@ -56,12 +62,22 @@ export const parseAnnotationFromXml = (xmlString: string): AnnotationType[] => {
|
|
obj_attr: {
|
|
obj_attr: {
|
|
page,
|
|
page,
|
|
position: getPosition(type, cur),
|
|
position: getPosition(type, cur),
|
|
- bdcolor: cur.attributes.color ? cur.attributes.color.value : undefined,
|
|
|
|
- bdwidth: cur.attributes.width ? parseInt(cur.attributes.width.value, 10) : 0,
|
|
|
|
- transparency: cur.attributes.opacity ? parseFloat(cur.attributes.opacity.value) : 1,
|
|
|
|
|
|
+ bdcolor: cur.attributes.color
|
|
|
|
+ ? cur.attributes.color.value
|
|
|
|
+ : undefined,
|
|
|
|
+ bdwidth: cur.attributes.width
|
|
|
|
+ ? parseInt(cur.attributes.width.value, 10)
|
|
|
|
+ : 0,
|
|
|
|
+ transparency: cur.attributes.opacity
|
|
|
|
+ ? parseFloat(cur.attributes.opacity.value)
|
|
|
|
+ : 1,
|
|
content: getContent(type, cur) || undefined,
|
|
content: getContent(type, cur) || undefined,
|
|
- fcolor: cur.attributes['interior-color'] ? cur.attributes['interior-color'].value : undefined,
|
|
|
|
- ftransparency: cur.attributes['interior-opacity'] ? cur.attributes['interior-opacity'].value : undefined,
|
|
|
|
|
|
+ fcolor: cur.attributes['interior-color']
|
|
|
|
+ ? cur.attributes['interior-color'].value
|
|
|
|
+ : undefined,
|
|
|
|
+ ftransparency: cur.attributes['interior-opacity']
|
|
|
|
+ ? cur.attributes['interior-opacity'].value
|
|
|
|
+ : undefined,
|
|
is_arrow: cur.attributes.tail,
|
|
is_arrow: cur.attributes.tail,
|
|
...getFontAttribute(type, cur),
|
|
...getFontAttribute(type, cur),
|
|
},
|
|
},
|
|
@@ -75,7 +91,12 @@ export const parseAnnotationFromXml = (xmlString: string): AnnotationType[] => {
|
|
};
|
|
};
|
|
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
// eslint-disable-next-line consistent-return
|
|
-const getEleText = (coord: any, elements: any, viewport: any, scale: any): string => {
|
|
|
|
|
|
+const getEleText = (
|
|
|
|
+ coord: any,
|
|
|
|
+ elements: any,
|
|
|
|
+ viewport: any,
|
|
|
|
+ scale: any
|
|
|
|
+): string => {
|
|
const top = normalizeRound(viewport.height - coord.top * scale);
|
|
const top = normalizeRound(viewport.height - coord.top * scale);
|
|
const left = normalizeRound(coord.left * scale);
|
|
const left = normalizeRound(coord.left * scale);
|
|
const bottom = normalizeRound(viewport.height - coord.bottom * scale);
|
|
const bottom = normalizeRound(viewport.height - coord.bottom * scale);
|
|
@@ -98,7 +119,7 @@ const getEleText = (coord: any, elements: any, viewport: any, scale: any): strin
|
|
const start = Math.floor(textLength * rateL);
|
|
const start = Math.floor(textLength * rateL);
|
|
const distanceR = eleRight - right;
|
|
const distanceR = eleRight - right;
|
|
const rateR = distanceR / width;
|
|
const rateR = distanceR / width;
|
|
- const end = Math.floor(textLength - (textLength * rateR));
|
|
|
|
|
|
+ const end = Math.floor(textLength - textLength * rateR);
|
|
return ` ${element.innerText.slice(start, end)}`;
|
|
return ` ${element.innerText.slice(start, end)}`;
|
|
}
|
|
}
|
|
if (eleLeft < left && eleRight > left) {
|
|
if (eleLeft < left && eleRight > left) {
|
|
@@ -110,7 +131,7 @@ const getEleText = (coord: any, elements: any, viewport: any, scale: any): strin
|
|
if (eleRight > right && eleLeft < right) {
|
|
if (eleRight > right && eleLeft < right) {
|
|
const distance = eleRight - right;
|
|
const distance = eleRight - right;
|
|
const rate = distance / width;
|
|
const rate = distance / width;
|
|
- const end = Math.floor(textLength - (textLength * rate));
|
|
|
|
|
|
+ const end = Math.floor(textLength - textLength * rate);
|
|
return ` ${element.innerText.slice(0, end)}`;
|
|
return ` ${element.innerText.slice(0, end)}`;
|
|
}
|
|
}
|
|
if (eleLeft >= left && eleRight <= right) {
|
|
if (eleLeft >= left && eleRight <= right) {
|
|
@@ -123,7 +144,11 @@ const getEleText = (coord: any, elements: any, viewport: any, scale: any): strin
|
|
};
|
|
};
|
|
|
|
|
|
export const getAnnotationText = async ({
|
|
export const getAnnotationText = async ({
|
|
- viewport, scale, page, coords, pdf,
|
|
|
|
|
|
+ viewport,
|
|
|
|
+ scale,
|
|
|
|
+ page,
|
|
|
|
+ coords,
|
|
|
|
+ pdf,
|
|
}: {
|
|
}: {
|
|
viewport: ViewportType;
|
|
viewport: ViewportType;
|
|
scale: number;
|
|
scale: number;
|
|
@@ -132,7 +157,9 @@ export const getAnnotationText = async ({
|
|
pdf: any;
|
|
pdf: any;
|
|
}): Promise<any> => {
|
|
}): Promise<any> => {
|
|
const pageContainer = document.getElementById(`page_${page}`) as HTMLElement;
|
|
const pageContainer = document.getElementById(`page_${page}`) as HTMLElement;
|
|
- const textLayer = pageContainer.querySelector('[data-id="text-layer"]') as HTMLElement;
|
|
|
|
|
|
+ const textLayer = pageContainer.querySelector(
|
|
|
|
+ '[data-id="text-layer"]'
|
|
|
|
+ ) as HTMLElement;
|
|
const pdfPage = await getPdfPage(pdf, page);
|
|
const pdfPage = await getPdfPage(pdf, page);
|
|
|
|
|
|
if (!textLayer.childNodes.length) {
|
|
if (!textLayer.childNodes.length) {
|
|
@@ -142,6 +169,7 @@ export const getAnnotationText = async ({
|
|
viewport,
|
|
viewport,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
const textElements = [...textLayer.childNodes];
|
|
const textElements = [...textLayer.childNodes];
|
|
let text = '';
|
|
let text = '';
|
|
@@ -155,25 +183,29 @@ export const getAnnotationText = async ({
|
|
return text;
|
|
return text;
|
|
};
|
|
};
|
|
|
|
|
|
-export const parseAnnotationObject = ({
|
|
|
|
- id,
|
|
|
|
- obj_type,
|
|
|
|
- obj_attr: {
|
|
|
|
- page,
|
|
|
|
- bdcolor,
|
|
|
|
- transparency,
|
|
|
|
- fcolor,
|
|
|
|
- ftransparency,
|
|
|
|
- position = '',
|
|
|
|
- content,
|
|
|
|
- style,
|
|
|
|
- bdwidth,
|
|
|
|
- fontname,
|
|
|
|
- fontsize,
|
|
|
|
- textcolor,
|
|
|
|
- is_arrow,
|
|
|
|
- },
|
|
|
|
-}: AnnotationType, pageHeight: number, scale: number): AnnotationType => ({
|
|
|
|
|
|
+export const parseAnnotationObject = (
|
|
|
|
+ {
|
|
|
|
+ id,
|
|
|
|
+ obj_type,
|
|
|
|
+ obj_attr: {
|
|
|
|
+ page,
|
|
|
|
+ bdcolor,
|
|
|
|
+ transparency,
|
|
|
|
+ fcolor,
|
|
|
|
+ ftransparency,
|
|
|
|
+ position = '',
|
|
|
|
+ content,
|
|
|
|
+ style,
|
|
|
|
+ bdwidth,
|
|
|
|
+ fontname,
|
|
|
|
+ fontsize,
|
|
|
|
+ textcolor,
|
|
|
|
+ is_arrow,
|
|
|
|
+ },
|
|
|
|
+ }: AnnotationType,
|
|
|
|
+ pageHeight: number,
|
|
|
|
+ scale: number
|
|
|
|
+): AnnotationType => ({
|
|
id,
|
|
id,
|
|
obj_type,
|
|
obj_type,
|
|
obj_attr: {
|
|
obj_attr: {
|