Spaces:
Configuration error
Configuration error
File size: 44,683 Bytes
a7060a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 | import { throttle } from "@humansignal/core/lib/utils/lodash-replacements";
import { destroy, detach, flow, getEnv, getParent, getRoot, isAlive, onSnapshot, types } from "mobx-state-tree";
import { ff } from "@humansignal/core";
import { errorBuilder } from "../../core/DataValidator/ConfigValidator";
import { guidGenerator } from "../../core/Helpers";
import { Hotkey } from "../../core/Hotkey";
import TimeTraveller from "../../core/TimeTraveller";
import Tree, { TRAVERSE_STOP } from "../../core/Tree";
import Types from "../../core/Types";
import Area from "../../regions/Area";
import Result from "../../regions/Result";
import Utils from "../../utils";
import { FF_DEV_1284, FF_DEV_3391, FF_LSDV_4583, FF_REVIEWER_FLOW, isFF } from "../../utils/feature-flags";
import { delay, isDefined } from "../../utils/utilities";
import { CommentStore } from "../Comment/CommentStore";
import RegionStore from "../RegionStore";
import RelationStore from "../RelationStore";
import { UserExtended } from "../UserStore";
import { LinkingModes } from "./LinkingModes";
const hotkeys = Hotkey("Annotations", "Annotations");
/**
* Omit value fields from the object.
*
* This should fix a problem with wrong region type detection caused by overlapping fields from a result and from an area.
* The current problem is related to the text field of richtext region that could appear in the result for the textarea as well.
* As these fields have the same name and different types it could make the region to be detected as a classification region instead of richtext,
* and we may miss its displaying.
*
* For now, it is mostly related to the rich text region with per region textareas.
* The problem may appear when we get wrong order of results for deserialization.
* The other reason may be the omitted main result (We declare that all the data that we need to restore the main region is also contained in the per-region results).
* @example Wrong order:
* [{
* "id": "id_1",
* "from_name": "comment",
* "to_name": "text",
* "type": "textarea",
* "value": {
* "start": 0,
* "end": 11,
* "text": ["A comment for the region],
* },
* },
* {
* "id": "id_1",
* "from_name": "labels",
* "to_name": "text",
* "type": "labels",
* "value": {
* "start": 0,
* "end": 11,
* "labels": ["Label 1"],
* "text": "Just a text",
* },
* }]
*
* @example Omitted main result:
* [{
* "id": "only_per_region_textarea",
* "from_name": "comment",
* "to_name": "text",
* "type": "textarea",
* "value": {
* "start": 0,
* "end": 11,
* "labels": ["Label 1"],
* "text": ["A comment for the region"],
* },
* }]
*
* @param value {Object} object to fix
* @returns {Object} new object without value fields
*/
const omitValueFields = ff.isActive(ff.FF_CUSTOM_TAGS)
? (value) => {
// @todo describe that we only omit `text` from TextArea
if (Array.isArray(value.text)) {
const { text: _, ...newValue } = value;
return newValue;
}
return value;
}
: (value) => {
const newValue = { ...value };
Result.properties.value.propertyNames.forEach((propName) => {
delete newValue[propName];
});
return newValue;
};
const TrackedState = types.model("TrackedState", {
areas: types.map(Area),
relationStore: types.optional(RelationStore, {}),
});
// Create a union type that can handle both user references and frozen user objects
const UserOrReference = types.union({
dispatcher: (snapshot) => {
// If it's a number, it's a reference to a user ID
if (typeof snapshot === "number") {
return types.safeReference(UserExtended);
}
// If it's a full user object, store it as frozen to avoid duplicate instances
if (snapshot && typeof snapshot === "object" && (snapshot.firstName || snapshot.email || snapshot.username)) {
return types.frozen();
}
// Default to reference for any other case
return types.safeReference(UserExtended);
},
cases: {
frozen: types.frozen(),
reference: types.safeReference(UserExtended),
},
});
const _Annotation = types
.model("AnnotationBase", {
id: types.identifier,
// @todo this value used `guidGenerator(5)` as default value before
// @todo but it calculates once, so all the annotations have the same pk
// @todo why don't use only `id`?
// @todo reverted back to wrong type; maybe it breaks all the deserialisation
pk: types.maybeNull(types.string),
selected: types.optional(types.boolean, false),
type: types.enumeration(["annotation", "prediction", "history"]),
createdDate: types.optional(types.string, Utils.UDate.currentISODate()),
createdAgo: types.maybeNull(types.string),
createdBy: types.optional(types.string, "Admin"),
user: types.optional(types.maybeNull(UserOrReference), null),
score: types.maybeNull(types.number),
parent_prediction: types.maybeNull(types.integer),
parent_annotation: types.maybeNull(types.integer),
last_annotation_history: types.maybeNull(types.integer),
comment_count: types.maybeNull(types.integer),
unresolved_comment_count: types.maybeNull(types.integer),
loadedDate: types.optional(types.Date, () => new Date()),
leadTime: types.maybeNull(types.number),
// @todo use types.Date
draftSaved: types.maybe(types.string),
// created by user during this session
userGenerate: types.optional(types.boolean, true),
sentUserGenerate: types.optional(types.boolean, false),
localUpdate: types.optional(types.boolean, false),
ground_truth: types.optional(types.boolean, false),
skipped: false,
// This field stores all data that affects undo/redo history
// It should contain real objects to be able to work with them through snapshots
// Annotation will use getters to get them at the top level
// This data is never redefined directly, it's empty at the start
trackedState: types.optional(TrackedState, {}),
history: types.optional(TimeTraveller, { targetPath: "../trackedState" }),
dragMode: types.optional(types.boolean, false),
editable: types.optional(types.boolean, true),
readonly: types.optional(types.boolean, false),
suggestions: types.map(Area),
regionStore: types.optional(RegionStore, {
regions: [],
}),
isDrawing: types.optional(types.boolean, false),
commentStore: types.optional(CommentStore, {
comments: [],
}),
...(isFF(FF_DEV_3391) ? { root: Types.allModelsTypes() } : {}),
})
.views((self) => ({
get areas() {
return self.trackedState.areas;
},
get relationStore() {
return self.trackedState.relationStore;
},
}))
.preProcessSnapshot((sn) => {
// sn.draft = Boolean(sn.draft);
const user = sn.user ?? sn.completed_by ?? undefined;
let root;
const updateIds = (item) => {
const children = item.children?.map(updateIds);
const imageEntities = item.imageEntities?.map(updateIds);
let updatedItem = item;
if (children) updatedItem = { ...updatedItem, children };
if (imageEntities) updatedItem = { ...updatedItem, imageEntities };
if (updatedItem.id) updatedItem = { ...updatedItem, id: `${updatedItem.name ?? updatedItem.id}@${sn.id}` };
// @todo fallback for tags with name as id:
// if (item.name) item = { ...item, name: item.name + "@" + sn.id };
// @todo soon no such tags should left
return updatedItem;
};
if (isFF(FF_DEV_3391)) {
root = updateIds(sn.root.toJSON());
}
const getCreatedBy = (snapshot) => {
if (snapshot.type === "prediction") {
const modelVersion = snapshot.model_version?.trim() ?? "";
return modelVersion || "Admin";
}
return snapshot.createdBy ?? "Admin";
};
const getCreatedAt = (snapshot) => {
return snapshot.draft_created_at ?? snapshot.created_at ?? snapshot.createdDate;
};
return {
...sn,
...(isFF(FF_DEV_3391) ? { root } : {}),
user,
editable: sn.editable ?? sn.type === "annotation",
createdBy: getCreatedBy(sn),
createdDate: getCreatedAt(sn),
ground_truth: sn.honeypot ?? sn.ground_truth ?? false,
skipped: sn.skipped || sn.was_cancelled,
acceptedState: sn.accepted_state ?? sn.acceptedState ?? null,
};
})
.views((self) =>
isFF(FF_DEV_3391)
? {}
: {
get root() {
return self.list.root;
},
get names() {
return self.list.names;
},
get toNames() {
return self.list.toNames;
},
},
)
.views((self) => ({
get store() {
return getRoot(self);
},
get list() {
return getParent(self, 2);
},
get objects() {
// Without correct validation toname may be null for control tags so we need to check isObjectTag instead of it
return Array.from(self.names.values()).filter((tag) => tag.isObjectTag);
},
get regions() {
return Array.from(self.areas.values());
},
get lastSelectedRegion() {
return self.selectedRegions[self.selectedRegions.length - 1];
},
get results() {
const results = [];
if (isAlive(self)) self.areas.forEach((a) => a.results.forEach((r) => results.push(r)));
return results;
},
get hasIncompletePolygons() {
if (!isAlive(self)) return false;
for (const area of self.areas.values()) {
if (area.type === "polygonregion" && area.incomplete) return true;
}
return false;
},
get serialized() {
// Dirty hack to force MST track changes
self.areas.toJSON();
return self.results
.map((r) => r.serialize())
.filter(Boolean)
.concat(self.relationStore.serialize());
},
get serializedSelection() {
// Dirty hack to force MST track changes
self.areas.toJSON();
const selectedResults = [];
self.areas.forEach((a) => {
if (!a.inSelection) return;
a.results.forEach((r) => {
selectedResults.push(r);
});
});
return selectedResults.map((r) => r.serialize()).filter(Boolean);
},
get highlightedNode() {
return self.regionStore.selection.highlighted;
},
get hasSelection() {
return self.regionStore.hasSelection;
},
get selectionSize() {
return self.regionStore.selection.size;
},
get selectedRegions() {
return Array.from(self.regionStore.selection.selected.values());
},
get selectedDrawingRegions() {
return Array.from(self.regionStore.selection.drawingSelected.values());
},
// existing annotation which can be updated
get exists() {
const dataExists = (self.userGenerate && self.sentUserGenerate) || isDefined(self.versions.result);
const pkExists = isDefined(self.pk);
return dataExists && pkExists;
},
get hasSuggestionsSupport() {
return self.objects.some((obj) => {
return obj.supportSuggestions;
});
},
get isNonEditableDraft() {
const isKnownUsers = !!self.user && !!self.store.user;
// If we do not know what user created draft
// and who we are, then, we shouldn't prevent the ability to edit annotation
// because we can't predict is it our draft or not.
// It most probably could be relevant for standalone `lsf`
if (!isKnownUsers) return false;
// If there is no `pk` than there is no annotation in DataBase
const isDraft = self.pk === null;
const isNonEditable = self.user.id !== self.store.user.id;
return isDraft && isNonEditable;
},
isReadOnly() {
return self.isNonEditableDraft || self.readonly || !self.editable;
},
}))
.volatile(() => ({
hidden: false,
draftId: 0,
draftSelected: false,
autosaveDelay: 5000,
isDraftSaving: false,
// This flag indicates that we are accepting suggestions right now (an accepting is started and not finished yet)
isSuggestionsAccepting: false,
submissionStarted: 0,
versions: {},
resultSnapshot: "",
}))
.volatile(() =>
isFF(FF_DEV_3391)
? {
names: new Map(),
toNames: new Map(),
ids: new Map(),
}
: {},
)
.views((self) => ({
// experiment to display review buttons in Quick View
get canBeReviewed() {
const store = self.store;
return (
isFF(FF_REVIEWER_FLOW) &&
// not a current user — we can only review others' annotations
self.user?.email &&
store.user?.email !== self.user?.email &&
// we have this only in LSE
getEnv(self).events.hasEvent("acceptAnnotation") &&
// Quick View — we don't have View All in Label Stream
store.hasInterface("annotations:view-all") &&
// skipped annotations can't be reviewed
!self.skipped &&
// annotation was submitted already
!isNaN(self.pk)
);
},
}))
.actions((self) => ({
reinitHistory(force = true) {
self.history.reinit(force);
self.autosave?.cancel();
if (self.type === "annotation") self.setInitialValues();
},
setEditable(val) {
self.editable = val;
},
setReadonly(val) {
self.readonly = val;
},
setIsDrawing(isDrawing) {
self.isDrawing = isDrawing;
},
setUnresolvedCommentCount(val) {
self.unresolved_comment_count = val;
},
setCommentCount(val) {
self.comment_count = val;
},
setGroundTruth(value, ivokeEvent = true) {
const root = getRoot(self);
if (root && root !== self && ivokeEvent) {
const as = root.annotationStore;
const assignGroundTruths = (p) => {
if (self !== p) p.setGroundTruth(false, false);
};
as.predictions.forEach(assignGroundTruths);
as.annotations.forEach(assignGroundTruths);
}
self.ground_truth = value;
if (ivokeEvent) {
getEnv(self).events.invoke("groundTruth", self.store, self, value);
}
},
sendUserGenerate() {
self.sentUserGenerate = true;
},
setLocalUpdate(value) {
self.localUpdate = value;
},
setDragMode(val) {
self.dragMode = val;
},
updatePersonalKey(value) {
self.pk = value;
getRoot(self).addAnnotationToTaskHistory?.(self.pk);
},
toggleVisibility(visible) {
self.hidden = visible === undefined ? !self.hidden : !visible;
},
setHighlightedNode() {
// moved to selectArea and others
},
selectArea(area) {
if (self.highlightedNode === area) return;
// if (current) current.setSelected(false);
self.regionStore.highlight(area);
// area.setSelected(true);
},
toggleRegionSelection(area, isSelected) {
self.regionStore.toggleSelection(area, isSelected);
},
selectAreas(areas) {
self.unselectAreas();
self.extendSelectionWith(areas);
},
extendSelectionWith(areas) {
for (const area of Array.isArray(areas) ? areas : [areas]) {
self.regionStore.toggleSelection(area, true);
}
},
unselectArea(area) {
if (self.highlightedNode !== area) return;
// area.setSelected(false);
self.regionStore.toggleSelection(area, false);
},
unselectAreas() {
if (!self.selectionSize) return;
self.regionStore.clearSelection();
},
lockSelectedRegions() {
for (const region of self.selectedRegions) {
if (region.incomplete === true) continue;
region.setLocked(!region.locked);
}
},
hideSelectedRegions() {
for (const region of self.selectedRegions) {
if (region.incomplete === true) continue;
region.toggleHidden();
}
},
deleteSelectedRegions() {
for (const region of self.selectedRegions) {
region.deleteRegion();
}
},
unselectStates() {
self.names.forEach((tag) => tag.unselectAll?.());
},
/**
* @param {boolean} tryToKeepStates don't unselect labels if such setting is enabled
*/
unselectAll(tryToKeepStates = false) {
const keepStates = tryToKeepStates && self.store.settings.continuousLabeling;
self.unselectAreas();
if (!keepStates) self.unselectStates();
},
removeArea(area) {
destroy(area);
},
deleteAllRegions({ deleteReadOnly = false } = {}) {
let regions = Array.from(self.areas.values());
// remove everything unconditionally
if (deleteReadOnly) {
self.unselectAll(true);
self.setIsDrawing(false);
self.relationStore.deleteAllRelations();
for (const r of regions) {
r.destroyRegion?.();
destroy(r);
}
self.updateObjects();
return;
}
if (deleteReadOnly === false) regions = regions.filter((r) => r.readonly === false);
for (const r of regions) {
r.deleteRegion();
}
self.updateObjects();
},
addRegion(reg) {
self.regionStore.unselectAll(true);
if (self.isLinkingMode) {
self.addLinkedRegion(reg);
self.stopLinkingMode();
}
},
validate() {
let ok = true;
self.traverseTree((node) => {
ok = node.validate?.();
if (ok === false) {
return TRAVERSE_STOP;
}
});
// should be true or false
return ok ?? true;
},
traverseTree(cb) {
return Tree.traverseTree(self.root, cb);
},
/**
*
*/
beforeSend() {
self.traverseTree((node) => {
node?.beforeSend?.();
});
self.stopLinkingMode();
self.unselectAll();
},
/**
* Delete region
* @param {*} region
*/
deleteRegion(region) {
if (region.isReadOnly()) return;
const { regions } = self.regionStore;
// move all children into the parent region of the given one
const children = regions.filter((r) => r.parentID === region.id);
if (children) {
for (const r of children) {
r.setParentID(region.parentID);
}
}
if (!region.classification) getEnv(self).events.invoke("entityDelete", region);
self.relationStore.deleteNodeRelation(region);
if (region.type === "polygonregion" || region.type === "vectorregion") {
detach(region);
}
destroy(region);
// If the annotation was in a drawing state and the user deletes it, we need to reset the drawing state
// to avoid the user being stuck in a drawing state
self.setIsDrawing(false);
},
deleteArea(area) {
destroy(area);
},
undo() {
const { history, regionStore } = self;
if (history?.canUndo) {
let stopDrawingAfterNextUndo = false;
const selectedIds = regionStore.selectedIds;
const currentRegion = regionStore.findRegion(
selectedIds[selectedIds.length - 1] ?? regionStore.regions[regionStore.regions.length - 1]?.id,
);
if (currentRegion?.type === "polygonregion") {
const points = currentRegion?.points?.length ?? 0;
stopDrawingAfterNextUndo = points <= 1;
} else if (currentRegion?.type === "vectorregion") {
const vertices = currentRegion?.vertices?.length ?? 0;
stopDrawingAfterNextUndo = vertices <= 1;
}
history.undo();
regionStore.selectRegionsByIds(selectedIds);
if (stopDrawingAfterNextUndo) {
currentRegion.setDrawing(false);
self.setIsDrawing(false);
}
}
},
redo() {
const { history, regionStore } = self;
if (history?.canRedo) {
const selectedIds = regionStore.selectedIds;
history.redo();
regionStore.selectRegionsByIds(selectedIds);
}
},
/**
* update some fragile parts after snapshot manipulations (undo/redo)
*
* @param {boolean} [force=true] force update will unselect all regions
*/
updateObjects(force = true) {
// Some async or lazy mode operations (ie. Images lazy load) need to reinitHistory without removing state selections
if (force) self.unselectAll();
self.names.forEach((tag) => tag.needsUpdate?.());
self.updateAppearenceFromState();
const areas = Array.from(self.areas.values());
// It should find just one unfinished region, but just in case we work with array
const filtered = areas.filter((area) => area.isDrawing);
// Update UI to reflect the state of an unfinished region in case if it exists
if (filtered.length) self.regionStore.selection._updateResultsFromRegions(filtered);
},
updateAppearenceFromState() {
self.areas.forEach((area) => area.updateAppearenceFromState?.());
},
setInitialValues() {
// <Label selected="true"/>
self.names.forEach((tag) => {
if (tag.type.endsWith("labels")) {
// @todo check for choice="multiple" and multiple preselected labels
const preselected = tag.children?.find((label) => label.initiallySelected);
if (preselected) preselected.setSelected(true);
}
});
// @todo deal with `defaultValue`s
},
setDefaultValues() {
self.names.forEach((tag) => {
if (["choices", "taxonomy"].includes(tag?.type) && tag.preselectedValues?.length) {
// <Choice selected="true"/>
self.createResult({}, { [tag?.type]: tag.preselectedValues }, tag, tag.toname);
}
});
},
addVersions(versions) {
self.versions = { ...self.versions, ...versions };
if (versions.draft) self.setDraftSelected();
},
toggleDraft(explicitValue) {
const isDraft = self.draftSelected;
const shouldSelectDraft = explicitValue ?? !isDraft;
// if explicitValue already achieved
if (shouldSelectDraft === isDraft) return;
// if there are no draft to switch to
if (shouldSelectDraft && !self.versions.draft) return;
// if there were some changes waiting they'll be saved
self.autosave.flush();
self.pauseAutosave();
// reinit annotation from required state
self.deleteAllRegions({ deleteReadOnly: true });
if (shouldSelectDraft) {
self.deserializeResults(self.versions.draft);
} else {
self.deserializeResults(self.versions.result);
}
self.draftSelected = shouldSelectDraft;
// reinit objects
self.updateObjects();
self.startAutosave();
},
startAutosave: flow(function* () {
if (!getEnv(self).events.hasEvent("submitDraft")) return;
// view all must never trigger autosave
if (self.isReadOnly()) return;
// some async tasks should be performed after deserialization
// so start autosave on next tick
yield delay(0);
if (self.autosave) {
self.autosave.cancel();
self.autosave.paused = false;
return;
}
// mobx will modify methods, so add it directly to have cancel() method
self.autosave = throttle(
() => {
// if autosave is paused, do nothing
if (self.autosave.paused) return;
self.saveDraft();
},
self.autosaveDelay,
{ leading: false },
);
onSnapshot(self.areas, self.autosave);
}),
async saveDraft(params) {
// There is no draft to save as it was already saved as an annotation
if (self.submissionStarted) return;
// if this is now a history item or prediction don't save it
if (!self.editable) return;
const result = self.serializeAnnotation({ fast: true });
self.setDraftSelected();
self.versions.draft = result;
self.setDraftSaving(true);
return self.store.submitDraft(self, params).then((res) => {
self.onDraftSaved(res);
return res;
});
},
submissionInProgress() {
self.submissionStarted = Date.now();
},
saveDraftImmediately() {
if (self.autosave) self.autosave.flush();
},
async saveDraftImmediatelyWithResults(params) {
// There is no draft to save as it was already saved as an annotation
if (self.submissionStarted || self.isDraftSaving) return {};
self.setDraftSaving(true);
const res = await self.saveDraft(params);
return res;
},
pauseAutosave() {
if (!self.autosave) return;
self.autosave.paused = true;
self.autosave.cancel();
},
beforeDestroy() {
self.autosave?.cancel?.();
},
setDraftId(id) {
self.draftId = id;
},
setDraftSelected(selected = true) {
self.draftSelected = selected;
},
onDraftSaved() {
self.setDraftSaved(Utils.UDate.currentISODate());
self.setDraftSaving(false);
},
dropDraft() {
if (!self.autosave) return;
self.autosave.cancel();
self.draftId = 0;
self.draftSelected = false;
self.draftSaved = undefined;
self.versions.draft = undefined;
},
setDraftSaving(saving = false) {
self.isDraftSaving = saving;
},
setDraftSaved(date) {
self.draftSaved = date;
},
afterAttach() {
self.traverseTree((node) => {
// called when the annotation is attached to the main store,
// at this point the whole tree is available. This method
// may come handy when you have a tag that acts or depends
// on other elements in the tree.
if (node.annotationAttached) node.annotationAttached();
});
self.history.onUpdate(self.updateObjects);
self.startAutosave();
},
afterCreate() {
if (isFF(FF_DEV_3391)) {
const { names, toNames } = Tree.extractNames(self.root);
names.forEach((tag, name) => self.names.set(name, tag));
toNames.forEach((tags, name) => self.toNames.set(name, tags));
Tree.traverseTree(self.root, (node) => {
const id = node.id ?? node.name;
if (id) {
self.ids.set(Tree.cleanUpId(id), node);
}
if (self.store.task && node.updateValue) node.updateValue(self.store);
});
}
if (self.userGenerate && !self.sentUserGenerate) {
self.loadedDate = new Date();
}
},
setupHotKeys() {
hotkeys.unbindAll();
let audiosNum = 0;
let audioNode = null;
const mod = "shift+space";
let comb = mod;
// [TODO] we need to traverse this two times, fix
// Hotkeys setup
self.traverseTree((node) => {
if (node && node.onHotKey && node.hotkey) {
hotkeys.addKey(node.hotkey, node.onHotKey, undefined, node.hotkeyScope);
}
});
self.traverseTree((node) => {
// add Space hotkey for playbacks of audio, there might be
// multiple audios on the screen
if (node && !node.hotkey && (node.type === "audio" || node.type === "audioplus")) {
if (audiosNum > 0) comb = `${mod}+${audiosNum + 1}`;
else audioNode = node;
node.hotkey = comb;
hotkeys.addKey(comb, node.onHotKey, "Play an audio", Hotkey.ALL_SCOPES);
audiosNum++;
}
});
self.traverseTree((node) => {
/**
* Hotkey for controls
*/
if (node && node.onHotKey && !node.hotkey) {
const comb = hotkeys.makeComb();
if (!comb) return;
node.hotkey = comb;
hotkeys.addKey(node.hotkey, node.onHotKey);
}
});
if (audioNode && audiosNum > 1) {
audioNode.hotkey = `${mod}+1`;
hotkeys.addKey(audioNode.hotkey, audioNode.onHotKey);
hotkeys.removeKey(mod);
}
// prevent spacebar from scrolling
// document.onkeypress = function(e) {
// e = e || window.event;
// var charCode = e.keyCode || e.which;
// if (charCode === 32) {
// e.preventDefault();
// return false;
// }
// };
const { enableHotkeys } = self.store.settings;
Hotkey.setScope(enableHotkeys ? Hotkey.DEFAULT_SCOPE : "__none__");
},
createResult(areaValue, resultValue, control, object, skipAfrerCreate = false, additionalStates = []) {
// Without correct validation object may be null, but it it shouldn't be so in results - so we should find any
if (!object && control.type === "textarea") {
object = self.objects[0];
}
const objectTag = self.names.get(object.name ?? object);
const result = {
from_name: self.names.get(control.name),
// @todo should stick to area
to_name: objectTag,
type: control.resultType,
value: resultValue,
readonly: self.readonly,
};
const areaRaw = {
id: guidGenerator(),
object: objectTag,
// data for Model instance
...areaValue,
// for Model detection
value: areaValue,
results: [result],
};
// TODO: MST is crashing if we don't validate areas?, this problem isn't
// happening locally. So to reproduce you have to test in production or environment
const area = self?.areas?.put(areaRaw);
objectTag?.afterResultCreated?.(area);
if (!area) return;
if (ff.isActive(ff.FF_MULTIPLE_LABELS_REGIONS)) {
// Add additional states before any deselection happens
additionalStates.forEach((state) => {
area.setValue(state);
});
}
// This is added mostly for the reason of updating indexes in labels
// for the elements (like highlights in text) that won't be dynamically changed
// but are dependent on the whole region list values
self.updateAppearenceFromState();
if (!area.classification) getEnv(self).events.invoke("entityCreate", area);
if (!skipAfrerCreate) self.afterCreateResult(area, control);
return area;
},
afterCreateResult(area, control) {
if (self.store.settings.selectAfterCreate) {
if (!area.classification) {
// some regions might need some actions right after creation (i.e. text)
// and some may be already deleted (i.e. bboxes)
setTimeout(() => isAlive(area) && self.selectArea(area));
}
} else {
// unselect labeling tools after use, but consider "keep labels selected" settings
if (control.isLabeling) self.unselectAll(true);
}
},
appendResults(results) {
if (!self.editable || self.readonly) return;
const regionIdMap = {};
const prevSize = self.regionStore.regions.length;
// Generate new ids to prevent collisions
for (const result of results) {
const regionId = result.id;
if (!regionIdMap[regionId]) {
regionIdMap[regionId] = guidGenerator();
}
result.id = regionIdMap[regionId];
}
self.deserializeResults(results);
self.updateObjects();
return self.regionStore.regions.slice(prevSize);
},
serializeAnnotation(options) {
// return self.serialized;
document.body.style.cursor = "wait";
const result = self.results
.map((r) => r.serialize(options))
.filter(Boolean)
.concat(self.relationStore.serialize(options));
document.body.style.cursor = "default";
return result;
},
// Some annotations may be created with wrong assumptions
// And this problems are fixable, so better to fix them on start
fixBrokenAnnotation(json) {
return (json ?? []).reduce((res, objRaw) => {
if (!objRaw) return res;
const obj = structuredClone(objRaw) ?? {};
if (obj.type === "relation") {
res.push(objRaw);
return res;
}
if (obj.type === "htmllabels") obj.type = "hypertextlabels";
if (obj.normalization) obj.meta = { ...obj.meta, text: [obj.normalization] };
const tagNames = self.names;
// Clear non-existent labels
if (obj.type.endsWith("labels")) {
const keys = Object.keys(obj.value);
for (const key of keys) {
if (key.endsWith("labels")) {
// detect most relevant label tags if that one from from_name is missing
// can be useful for predictions in old format with config in new format:
// Rectangle + Labels -> RectangleLabels
if (!tagNames.has(obj.from_name) || (!obj.value[key].length && !tagNames.get(obj.from_name).allowempty)) {
delete obj.value[key];
if (tagNames.has(obj.to_name)) {
// Redirect references to existent tool
const targetObject = tagNames.get(obj.to_name);
// @todo Video tag returns only `*labels` so maybe we have to add this check here
const states = self.toNames.get(targetObject.name);
if (states?.length) {
const altToolsControllerType = obj.type.replace(/labels$/, "");
const sameLabelsType = obj.type;
const simpleLabelsType = "labels";
for (const altType of [altToolsControllerType, sameLabelsType, simpleLabelsType]) {
const state = states.find((state) => state.type === altType);
if (state) {
obj.type = altType;
obj.from_name = state.name;
break;
}
}
}
}
}
}
}
}
if (tagNames.has(obj.from_name) && tagNames.has(obj.to_name)) {
res.push(obj);
}
// Insert image dimensions from result
(() => {
if (!isDefined(obj.original_width)) return;
if (!tagNames.has(obj.to_name)) return;
const tag = tagNames.get(obj.to_name);
if (tag.type !== "image") return;
const imageEntity = tag.findImageEntity(obj.item_index ?? 0);
if (!imageEntity || imageEntity.imageLoaded) return;
imageEntity.setNaturalWidth(obj.original_width);
imageEntity.setNaturalHeight(obj.original_height);
})();
return res;
}, []);
},
setSuggestions(rawSuggestions) {
const { history } = self;
self.suggestions.clear();
if (!rawSuggestions) return;
self.deserializeResults(rawSuggestions, {
suggestions: true,
});
self.isSuggestionsAccepting = true;
if (getRoot(self).autoAcceptSuggestions) {
if (isFF(FF_DEV_1284)) {
self.history.setReplaceNextUndoState(true);
}
self.acceptAllSuggestions();
} else {
self.suggestions.forEach((suggestion) => {
// regions that can't be accepted in usual way, should be auto-accepted;
const supportSuggestions = suggestion.supportSuggestions;
// If we cannot display suggestions on object/control then just accept them
if (!supportSuggestions) {
self.acceptSuggestion(suggestion.id);
if (isFF(FF_DEV_1284)) {
// This is necessary to prevent the occurrence of new steps in the history after updating objects at the end of current method
history.setReplaceNextUndoState(true);
}
}
});
}
self.isSuggestionsAccepting = false;
if (!isFF(FF_DEV_1284)) {
history.freeze("richtext:suggestions");
}
self.names.forEach((tag) => tag.needsUpdate?.({ suggestions: true }));
if (!isFF(FF_DEV_1284)) {
history.setReplaceNextUndoState(true);
history.unfreeze("richtext:suggestions");
}
},
cleanClassificationAreas() {
const classificationAreasByControlName = {};
const duplicateAreaIds = [];
self.areas.forEach((a) => {
const controlName = a.results[0].from_name.name;
// May be null but null is also valid key in this case
const itemIndex = a.item_index;
if (a.classification) {
if (classificationAreasByControlName[controlName]?.[itemIndex]) {
duplicateAreaIds.push(classificationAreasByControlName[controlName][itemIndex]);
}
classificationAreasByControlName[controlName] = classificationAreasByControlName[controlName] || {};
classificationAreasByControlName[controlName][itemIndex] = a.id;
}
});
for (const id of duplicateAreaIds) {
self.areas.delete(id);
}
},
/**
* Deserialize results
* @param {string | Array<any>} json Input results
* @param {{
* suggestions: boolean
* }} options Deserialization options
*/
deserializeResults(json, { suggestions = false, hidden = false } = {}) {
try {
const objAnnotation = self.prepareAnnotation(json);
const areas = suggestions ? self.suggestions : self.areas;
self._initialAnnotationObj = objAnnotation;
for (const obj of objAnnotation) {
self.deserializeSingleResult(
obj,
(id) => areas.get(id),
(snapshot) => areas.put(snapshot),
);
}
// It's not necessary, but it's calmer with this
self.cleanClassificationAreas();
if (!hidden) {
for (const r of self.results) {
if (r.area.classification) {
r.from_name.updateFromResult?.(r.mainValue);
}
}
}
for (const obj of objAnnotation) {
if (obj.type === "relation") {
self.relationStore.deserializeRelation(
`${obj.from_id}#${self.id}`,
`${obj.to_id}#${self.id}`,
obj.direction,
obj.labels,
);
}
}
} catch (e) {
console.error(e);
self.list.addErrors([errorBuilder.generalError(e)]);
}
},
deserializeAnnotation(...args) {
console.warn("deserializeAnnotation() is deprecated. Use deserializeResults() instead");
return self.deserializeResults(...args);
},
prepareAnnotation(rawAnnotation) {
let objAnnotation = rawAnnotation;
if (typeof objAnnotation !== "object") {
objAnnotation = JSON.parse(objAnnotation);
}
objAnnotation = self.fixBrokenAnnotation(objAnnotation ?? []);
return objAnnotation;
},
deserializeSingleResult(obj, getArea, createArea) {
if (obj.type !== "relation") {
const { id, value: rawValue, type, ...data } = obj;
let { from_name, to_name } = data;
const object = self.names.get(data.to_name) ?? {};
const tagType = object.type;
// avoid duplicates of the same areas in different annotations/predictions
const areaId = `${id || guidGenerator()}#${self.id}`;
const resultId = `${data.from_name}@${areaId}`;
const value = self.prepareValue(rawValue, tagType);
if (isFF(FF_DEV_3391)) {
to_name = `${to_name}@${self.id}`;
from_name = `${from_name}@${self.id}`;
}
let area = getArea(areaId);
if (!area) {
const areaSnapshot = {
id: areaId,
object: to_name,
...data,
// We need to omit value properties due to there may be conflicting property types, for example a text.
// if we don't it can create a classification instead of proper area
/** @see `omitValueFields` */
...omitValueFields(value),
value,
};
area = createArea(areaSnapshot);
if (isFF(FF_LSDV_4583)) {
// store copy of the original result inside the area
// useful when you need to serialize a result without
// updating it from current/actual data
// For safety reasons this object is always readonly
Object.defineProperty(area, "_rawResult", {
value: Object.freeze(structuredClone(obj)),
});
}
}
const newResult = { ...data, id: resultId, type, value, from_name, to_name };
area.addResult(newResult);
// apply additional data, that were skipped in favour of region type detection
area.applyAdditionalDataFromResult?.(newResult);
// if there is merged result with region data and type and also with the labels
// and object allows such merge — create new result with these labels
if (!type.endsWith("labels") && value.labels && object.mergeLabelsAndResults) {
const labels = value.labels;
const controls = self.toNames.get(object.name).filter((s) => s.type.endsWith("labels"));
const labelControl = controls.find((control) => control?.findLabel(labels[0]));
if (labelControl) {
area.setValue(labelControl);
area.results.find((r) => r.type.endsWith("labels"))?.setValue(labels);
}
}
}
},
prepareValue(value, type) {
switch (type) {
case "text":
case "hypertext":
case "richtext": {
const hasStartEnd = isDefined(value.start) && isDefined(value.end);
const lacksOffsets = !isDefined(value.startOffset) && !isDefined(value.endOffset);
// @todo move this Text regions offsets transform to RichTextRegion
if (hasStartEnd && lacksOffsets) {
return Object.assign({}, value, {
start: "",
end: "",
startOffset: Number(value.start),
endOffset: Number(value.end),
isText: true,
});
}
break;
}
default:
return value;
}
return value;
},
acceptAllSuggestions() {
for (const id of self.suggestions.keys()) {
self.acceptSuggestion(id);
}
self.deleteAllDynamicregions(isFF(FF_DEV_1284));
},
rejectAllSuggestions() {
for (const id of self.suggestions.keys()) {
self.suggestions.delete(id);
}
self.deleteAllDynamicregions(isFF(FF_DEV_1284));
},
deleteAllDynamicregions(silent = false) {
for (const r of self.regions) {
if (r.dynamic) {
if (silent) {
// dirty hack to prevent sending regionFinishedDrawing notification
r.setDrawing(true);
}
r.deleteRegion();
}
}
},
acceptSuggestion(id) {
const item = self.suggestions.get(id);
let itemId = id;
const isGlobalClassification = item.classification;
// this piece of code prevents from creating duplicated global classifications
if (isGlobalClassification) {
const itemResult = item.results[0];
const areasIterator = self.areas.values();
for (const area of areasIterator) {
const areaResult = area.results[0];
const isFound =
areaResult.from_name === itemResult.from_name &&
areaResult.to_name === itemResult.to_name &&
areaResult.item_index === itemResult.item_index;
if (isFound) {
itemId = area.id;
break;
}
}
} else {
// @todo: there is a strange behaviour that should be documented somewhere
// On serialization we use area id as result id to save it somewhere
// and on deserialization we use result id as area id
// but when we use suggestions we should keep in mind that we need to do it manually or use serialized data instead
// or we can get weird regions duplication in some cases
const area = self.areas.get(item.cleanId);
if (area) {
itemId = area.id;
}
}
self.areas.set(itemId, {
...item.toJSON(),
id: itemId,
fromSuggestion: true,
});
const area = self.areas.get(itemId);
const activeStates = area.object.activeStates();
for (const state of activeStates) {
area.setValue(state);
}
self.suggestions.delete(id);
},
rejectSuggestion(id) {
self.suggestions.delete(id);
},
resetReady() {
self.objects.forEach((object) => object.setReady?.(false));
self.areas.forEach((area) => area.setReady?.(false));
},
}));
export const Annotation = types.compose("Annotation", LinkingModes, _Annotation);
|