From 1077ed991f28da04baaa97b0ea080fb6e31fcbf0 Mon Sep 17 00:00:00 2001
From: john <johnbarlas39@gmail.com>
Date: Sat, 31 Dec 2016 01:21:05 +0200
Subject: [PATCH] handle deleting with non-collapsed selections on an existing
 add track annotation

---
 .../track_change/TrackChangesProvider.js      | 44 +++++++++++++++++--
 1 file changed, 40 insertions(+), 4 deletions(-)

diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
index da3fcd3..688e7bf 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
@@ -161,10 +161,39 @@ class TrackChangesProvider {
         }
       }
     } else {
-      selection = this.getSelection()
-      this.createDeleteAnnotation(selection)
-      this.moveCursorTo(direction.cursorTo)
-      return
+      const notOnTrack = this.isNotOnTrackAnnotation()
+
+      if (notOnTrack) {
+        selection = this.getSelection()
+        this.createDeleteAnnotation(selection)
+        this.moveCursorTo(direction.cursorTo)
+        return
+      }
+
+      const onAdd = this.isOnAnnotation('add')
+
+      if (onAdd) {
+        const annotation = this.getAnnotationByStatus('add')
+        const withinAnnotation = this.isSelectionContainedWithin(annotation)
+
+        if (withinAnnotation) {
+          const point = annotation[direction.cursorTo + 'Offset']
+          this.moveCursorTo(point)
+          return
+        }
+
+        const annotationContainedInSelection = this.isAnnotationContainedWithinSelection(annotation)
+        if (annotationContainedInSelection) {
+          selection = this.getSelection()
+          this.createDeleteAnnotation(selection)
+          return
+        }
+
+        selection = this.getSelection()
+        this.createDeleteAnnotation(selection)
+        this.separateAnnotations()
+        return
+      }
     }
   }
 
@@ -460,6 +489,13 @@ class TrackChangesProvider {
     return selection
   }
 
+  isAnnotationContainedWithinSelection (annotation) {
+    const selection = this.getSelection()
+    const annotationSelection = annotation.getSelection()
+
+    return selection.contains(annotationSelection)
+  }
+
   isSelectionCollapsed () {
     const selection = this.getSelection()
     const isCollapsed = selection.isCollapsed()
-- 
GitLab