UNPKG

react-pdf-ner-annotator

Version:

A React component to annotate named entities directly onto a PDF.

106 lines 4.98 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; var react_1 = require("react"); var object_hash_1 = __importDefault(require("object-hash")); var hashHelper_1 = require("../helpers/hashHelper"); var useAnnotations = function (defaultAnnotations, readonly, shouldUpdateDefaultAnnotations) { var _a = (0, react_1.useState)([]), annotations = _a[0], setAnnotations = _a[1]; var _b = (0, react_1.useState)(''), lastActionHash = _b[0], setLastActionHash = _b[1]; (0, react_1.useEffect)(function () { if (shouldUpdateDefaultAnnotations) { setAnnotations(defaultAnnotations); } }, [defaultAnnotations, shouldUpdateDefaultAnnotations, (0, object_hash_1["default"])(defaultAnnotations)]); var getAnnotationsForPage = (0, react_1.useCallback)(function (page) { return annotations.filter(function (annotation) { return annotation.page === page; }); }, [annotations]); var addAnnotation = (0, react_1.useCallback)(function (annotation) { if (readonly) { return; } setAnnotations(function (prevAnnotations) { var _a; var lastId = ((_a = prevAnnotations[prevAnnotations.length - 1]) === null || _a === void 0 ? void 0 : _a.id) || 0; var newAnnotation = __assign({ id: lastId + 1 }, annotation); return __spreadArray(__spreadArray([], prevAnnotations, true), [newAnnotation], false); }); setLastActionHash((0, hashHelper_1.generateRandomHash)()); }, [readonly]); var updateAnnotation = (0, react_1.useCallback)(function (annotation) { if (readonly) { return; } setAnnotations(function (prevAnnotations) { return prevAnnotations.map(function (prevAnnotation) { if (prevAnnotation.id === annotation.id) { return annotation; } return prevAnnotation; }); }); setLastActionHash((0, hashHelper_1.generateRandomHash)()); }, [readonly]); var updateLastAnnotationForEntity = (0, react_1.useCallback)(function (annotation) { if (readonly) { return; } var lastAnnotationForEntity = annotations .slice() .reverse() .find(function (x) { return x.entity.id === annotation.entity.id && x.index === annotation.index; }); if (lastAnnotationForEntity) { var updatedAnnotations = __spreadArray([], annotations, true).map(function (x) { if (x.id === lastAnnotationForEntity.id) { return __assign(__assign({}, x), { nerAnnotation: __assign(__assign({}, x.nerAnnotation), { tokens: __spreadArray(__spreadArray([], x.nerAnnotation.tokens, true), annotation.nerAnnotation.tokens, true), textIds: __spreadArray(__spreadArray([], x.nerAnnotation.textIds, true), annotation.nerAnnotation.textIds, true) }) }); } return x; }); setAnnotations(updatedAnnotations); } else { addAnnotation(annotation); } setLastActionHash((0, hashHelper_1.generateRandomHash)()); }, [addAnnotation, annotations, readonly]); var removeAnnotation = (0, react_1.useCallback)(function (id) { if (readonly) { return; } setAnnotations(function (prevAnnotations) { return prevAnnotations.filter(function (a) { return a.id !== id; }); }); setLastActionHash((0, hashHelper_1.generateRandomHash)()); }, [readonly]); return { annotations: annotations, getAnnotationsForPage: getAnnotationsForPage, addAnnotation: addAnnotation, updateAnnotation: updateAnnotation, updateLastAnnotationForEntity: updateLastAnnotationForEntity, removeAnnotation: removeAnnotation, lastActionHash: lastActionHash }; }; exports["default"] = useAnnotations; //# sourceMappingURL=useAnnotations.js.map