react-pdf-ner-annotator
Version:
A React component to annotate named entities directly onto a PDF.
135 lines • 6.7 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var react_1 = __importStar(require("react"));
var mouse_position_1 = __importDefault(require("@react-hook/mouse-position"));
var selectionHelpers_1 = require("../helpers/selectionHelpers");
var SelectionRectangle_1 = __importDefault(require("./SelectionRectangle"));
var annotationHelpers_1 = require("../helpers/annotationHelpers");
var useKeyPressedListener_1 = __importDefault(require("../hooks/useKeyPressedListener"));
var CursorText_1 = __importDefault(require("./CursorText"));
var entityContext_1 = __importDefault(require("../context/entityContext"));
var initialCoords = { left: 0, top: 0, width: 0, height: 0 };
var Selection = function (_a) {
var pageNumber = _a.pageNumber, children = _a.children, addAnnotation = _a.addAnnotation, updateLastAnnotationForEntity = _a.updateLastAnnotationForEntity, className = _a.className, style = _a.style, pdfInformation = _a.pdfInformation, pdfContext = _a.pdfContext;
var selectionRef = (0, react_1.useRef)(null);
var mouse = (0, mouse_position_1["default"])(selectionRef);
var keyPressed = (0, useKeyPressedListener_1["default"])();
var _b = (0, react_1.useState)(false), isDragging = _b[0], setIsDragging = _b[1];
var _c = (0, react_1.useState)({ x: 0, y: 0 }), mouseCoords = _c[0], setMouseCoords = _c[1];
var _d = (0, react_1.useState)(initialCoords), coords = _d[0], setCoords = _d[1];
var entity = (0, react_1.useContext)(entityContext_1["default"]).entity;
var mode = (0, react_1.useMemo)(function () {
if (entity && isDragging) {
return 'annotating-mode';
}
if (entity) {
return 'normal-mode';
}
return 'text-selection-mode';
}, [entity, isDragging]);
var handleKeyEvent = (0, react_1.useCallback)(function (event) {
var _a;
if (((_a = event === null || event === void 0 ? void 0 : event.key) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'escape' && isDragging) {
setIsDragging(false);
setMouseCoords({ x: 0, y: 0 });
setCoords(initialCoords);
}
}, [isDragging]);
(0, react_1.useEffect)(function () {
document.addEventListener('keydown', handleKeyEvent, false);
return function () {
document.removeEventListener('keydown', handleKeyEvent, false);
};
}, [handleKeyEvent]);
var handleMouseDown = (0, react_1.useCallback)(function () {
if (entity) {
var x = mouse.x, y = mouse.y;
setMouseCoords({ x: x, y: y });
setIsDragging(true);
}
}, [entity, mouse]);
var handleMouseUp = (0, react_1.useCallback)(function () {
if (selectionRef && entity) {
var coordsToUse = coords;
if ((0, selectionHelpers_1.isCoordsEmpty)(coords) && entity.entityType === 'NER') {
var x = mouse.x, y = mouse.y;
coordsToUse = { left: x, top: y, width: 1, height: 1 };
}
switch (entity.entityType) {
case 'NER': {
var selectionChildren = selectionRef.current.children;
var markToAdd = (0, annotationHelpers_1.buildNerAnnotation)(pageNumber, entity, selectionChildren, coordsToUse);
if (markToAdd.nerAnnotation.textIds.length) {
if (keyPressed) {
updateLastAnnotationForEntity(markToAdd);
}
else {
addAnnotation(markToAdd);
}
}
break;
}
case 'AREA': {
var areaToAdd = (0, annotationHelpers_1.buildAreaAnnotation)(pageNumber, entity, coordsToUse, pdfInformation, pdfContext);
if (areaToAdd) {
addAnnotation(areaToAdd);
}
break;
}
default:
break;
}
}
setIsDragging(false);
setMouseCoords({ x: 0, y: 0 });
setCoords(initialCoords);
}, [
entity,
coords,
mouse,
pageNumber,
keyPressed,
updateLastAnnotationForEntity,
addAnnotation,
pdfInformation,
pdfContext,
]);
var handleMouseMove = (0, react_1.useCallback)(function () {
if (isDragging && entity) {
var x = mouse.x, y = mouse.y;
setCoords((0, selectionHelpers_1.calculateSelectionRectangle)(mouseCoords, { x: x, y: y }));
}
}, [isDragging, entity, mouse, mouseCoords]);
return (react_1["default"].createElement("div", { role: "document", ref: selectionRef, className: "selection-container ".concat(className, " ").concat(mode), style: style, onMouseDown: handleMouseDown, onMouseUp: handleMouseUp, onMouseMove: handleMouseMove },
react_1["default"].createElement(CursorText_1["default"], { entity: entity, mouseCoords: { x: mouse === null || mouse === void 0 ? void 0 : mouse.x, y: mouse === null || mouse === void 0 ? void 0 : mouse.y } }),
react_1["default"].createElement(SelectionRectangle_1["default"], { isDragging: isDragging, coordinates: coords }),
children));
};
exports["default"] = Selection;
//# sourceMappingURL=Selection.js.map