react-pdf-ner-annotator
Version:
A React component to annotate named entities directly onto a PDF.
54 lines • 2.22 kB
JavaScript
;
exports.__esModule = true;
exports.buildAreaAnnotation = exports.buildNerAnnotation = void 0;
var selectionHelpers_1 = require("./selectionHelpers");
var getImageAsBase64 = function (targetCoords, context) {
var left = targetCoords.left, top = targetCoords.top, width = targetCoords.width, height = targetCoords.height;
var imageContentRaw = context.getImageData(left, top, width, height);
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.getContext('2d').putImageData(imageContentRaw, 0, 0);
return canvas.toDataURL('image/jpeg', 1.0);
};
var buildNerAnnotation = function (pageNumber, entity, selectionChildren, targetCoords) {
var intersects = (0, selectionHelpers_1.findIntersectingChildren)(selectionChildren, targetCoords);
var markToAdd = {
page: pageNumber,
nerAnnotation: {
tokens: [],
textIds: []
},
entity: entity,
index: entity.index
};
intersects.forEach(function (intersect) {
var offsetX = intersect.offsetLeft;
var offsetY = intersect.offsetTop;
(0, selectionHelpers_1.findIntersectingChildren)(intersect.children, targetCoords, offsetX, offsetY).forEach(function (child, index) {
var dataI = child.getAttribute('data-i');
if (dataI) {
markToAdd.nerAnnotation.tokens.push(child.textContent);
markToAdd.nerAnnotation.textIds.push(parseInt(dataI, 10));
}
});
});
return markToAdd;
};
exports.buildNerAnnotation = buildNerAnnotation;
var buildAreaAnnotation = function (pageNumber, entity, targetCoords, pdfInformation, context) {
if ((0, selectionHelpers_1.isCoordsEmpty)(targetCoords)) {
return null;
}
return {
page: pageNumber,
areaAnnotation: {
boundingBox: targetCoords,
pdfInformation: pdfInformation,
base64Image: getImageAsBase64(targetCoords, context)
},
entity: entity
};
};
exports.buildAreaAnnotation = buildAreaAnnotation;
//# sourceMappingURL=annotationHelpers.js.map