Skip to content

Commit 8eee0f0

Browse files
committed
support trashing of annotations
- if annotation trashing is supported, do not override key of annotations when deletion is undone - expose annotationManager.clearInterferingHistory so that when an annotation is erased from trash, it is removed from reader's edits history
1 parent 132bb78 commit 8eee0f0

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/common/annotation-manager.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AnnotationManager {
2323
this._onSave = options.onSave;
2424
this._onDelete = options.onDelete;
2525
this._onChangeHistory = options.onChangeHistory;
26+
this._trashesAnnotations = options.trashesAnnotations;
2627
this._adjustTextAnnotationPosition = options.adjustTextAnnotationPosition;
2728
this.render = () => {
2829
options.onRender([...this._annotations]);
@@ -601,8 +602,10 @@ class AnnotationManager {
601602
if (annotation) {
602603
annotation.dateModified = (new Date()).toISOString();
603604
}
604-
// Assign new id when undeleting to reduce sync conflicts
605-
if (!prevAnnotation) {
605+
// Assign new id when undeleting to reduce sync conflicts. Clients that
606+
// trash annotations keep the deleted one around, so undeleting restores
607+
// it in place and the id has to stay the same for the client to find it.
608+
if (!prevAnnotation && !this._trashesAnnotations) {
606609
let newID = this._generateObjectKey();
607610
mapping.set(annotation.id, newID);
608611
annotation.id = newID;
@@ -635,8 +638,10 @@ class AnnotationManager {
635638
if (annotation) {
636639
annotation.dateModified = (new Date()).toISOString();
637640
}
638-
// Assign new id when undeleting to reduce sync conflicts
639-
if (!prevAnnotation) {
641+
// Assign new id when undeleting to reduce sync conflicts. Clients that
642+
// trash annotations keep the deleted one around, so undeleting restores
643+
// it in place and the id has to stay the same for the client to find it.
644+
if (!prevAnnotation && !this._trashesAnnotations) {
640645
let newID = this._generateObjectKey();
641646
mapping.set(annotation.id, newID);
642647
annotation.id = newID;
@@ -654,6 +659,12 @@ class AnnotationManager {
654659
return true;
655660
}
656661

662+
// Drops history points for annotations that have left the reader so
663+
// that erased annotations cannot be brought back.
664+
clearHistoryForAnnotations(ids) {
665+
this._clearInterferingHistory(ids);
666+
}
667+
657668
_clearInterferingHistory(affectedAnnotationIDs) {
658669
for (let i = this._undoStack.length - 1; i >= 0; i--) {
659670
if (affectedAnnotationIDs.some(id => this._undoStack[i].annotations.has(id))) {

src/common/reader.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class Reader {
130130
this._onSetPopupPosition = options.onSetPopupPosition;
131131
this._onChangeUndoHistory = options.onChangeUndoHistory;
132132
this._externalUndoHistory = !!options.onChangeUndoHistory;
133+
this._trashesAnnotations = !!options.trashesAnnotations;
133134

134135
for (let ftl of options.ftl) {
135136
addFTL(ftl);
@@ -361,6 +362,7 @@ class Reader {
361362
this._updateState({ filter });
362363
},
363364
onChangeHistory: this._onChangeUndoHistory,
365+
trashesAnnotations: this._trashesAnnotations,
364366
adjustTextAnnotationPosition: (annotation, option) => {
365367
return this._primaryView.adjustTextAnnotationPosition(annotation, option);
366368
}
@@ -951,6 +953,13 @@ class Reader {
951953
this._annotationManager.unsetAnnotations(ids);
952954
}
953955

956+
// Called when the client permanently deletes annotations that are no longer
957+
// in the view (e.g. erased from the trash), so history points referencing
958+
// them can't be replayed and recreate them
959+
clearAnnotationsHistory(ids) {
960+
this._annotationManager.clearHistoryForAnnotations(ids);
961+
}
962+
954963
openContextMenu(params) {
955964
this._onBringReaderToFront?.(true);
956965
this._updateState({ contextMenu: params });

0 commit comments

Comments
 (0)