Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add undo functionality for human annotations #194

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions factgenie/static/js/annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ $("#hideOverlayBtn").click(function () {
$("#overlay-start").fadeOut();
});

$("#undo-button").click(function () {
// Get current document ID based on current example index
const currentDocId = `p${current_example_idx}`;
spanAnnotator.undo(currentDocId);
});

$(".btn-err-cat").change(function () {
if (this.checked) {
const cat_idx = $(this).attr("data-cat-idx");
Expand Down
43 changes: 43 additions & 0 deletions factgenie/static/js/span-annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class SpanAnnotator {
this.eraserPreviewActive = false;
this.rightClickPreviewActive = false;
this.eventListeners = new Map();
this.history = new Map(); // Map of document ID -> array of history states
this.currentHistoryIndex = new Map(); // Map of document ID -> current history index
}

init(granularity, overlapAllowed, annotationTypes) {
Expand Down Expand Up @@ -50,6 +52,43 @@ class SpanAnnotator {
this.eventListeners.get(eventName).push(callback);
}

_addToHistory(objectId) {
const doc = this.documents.get(objectId);
if (!this.history.has(objectId)) {
this.history.set(objectId, []);
this.currentHistoryIndex.set(objectId, -1);
}

// Remove any future history after current index
const currentIndex = this.currentHistoryIndex.get(objectId);
this.history.get(objectId).splice(currentIndex + 1);

// Add new state
this.history.get(objectId).push({
annotations: JSON.parse(JSON.stringify(doc.annotations)) // Deep copy
});
this.currentHistoryIndex.set(objectId, this.currentHistoryIndex.get(objectId) + 1);
}

undo(objectId) {
if (!this.history.has(objectId)) return;

const currentIndex = this.currentHistoryIndex.get(objectId);
if (currentIndex < 0) return;

// Restore previous state
const previousState = this.history.get(objectId)[currentIndex];
const doc = this.documents.get(objectId);
doc.annotations = JSON.parse(JSON.stringify(previousState.annotations));

// Update index
this.currentHistoryIndex.set(objectId, currentIndex - 1);

// Rerender
this._renderAnnotations(objectId);
this.emit('annotationUndone', { objectId });
}

emit(eventName, data) {
if (this.eventListeners.has(eventName)) {
this.eventListeners.get(eventName).forEach(callback => callback(data));
Expand Down Expand Up @@ -306,6 +345,8 @@ class SpanAnnotator {
}

_createAnnotation(objectId, $start, $end) {
this._addToHistory(objectId);

const doc = this.documents.get(objectId);
const startIdx = parseInt($start.data('index'));
const endIdx = parseInt($end.data('index')) + $end.data('content').length - 1;
Expand Down Expand Up @@ -335,6 +376,8 @@ class SpanAnnotator {
}

_removeAnnotation(objectId, $span) {
this._addToHistory(objectId);

const doc = this.documents.get(objectId);
const position = parseInt($span.data('index'));

Expand Down