From ab028731136f3a82a81b1f75910a8170a184c612 Mon Sep 17 00:00:00 2001
From: barlas <yannisbarlas@gmail.com>
Date: Sun, 8 Jan 2017 22:48:13 +0200
Subject: [PATCH] handle collapsed additions on another users annotation

---
 .../elements/track_change/TrackChange.js      |  1 +
 .../track_change/TrackChangeHTMLConverter.js  |  4 +-
 .../track_change/TrackChangesProvider.js      | 50 +++++++++++++++++--
 3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/app/components/SimpleEditor/elements/track_change/TrackChange.js b/app/components/SimpleEditor/elements/track_change/TrackChange.js
index d4329d5..5c01833 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChange.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChange.js
@@ -5,6 +5,7 @@ class TrackChange extends PropertyAnnotation {}
 TrackChange.define({
   status: { type: 'string' },
   user: {
+    id: 'string',
     roles: 'array',
     username: 'string'
   }
diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangeHTMLConverter.js b/app/components/SimpleEditor/elements/track_change/TrackChangeHTMLConverter.js
index 40e61df..d683348 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChangeHTMLConverter.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChangeHTMLConverter.js
@@ -5,8 +5,9 @@ export default {
   import: function (element, node, converter) {
     node.status = element.attr('status')
     node.user = {
+      id: element.attr('user-id'),
       roles: element.attr('roles').split(','),
-      user: element.attr('username')
+      username: element.attr('username')
     }
   },
 
@@ -15,6 +16,7 @@ export default {
 
     element.setAttribute('status', status)
 
+    element.setAttribute('user-id', user.id)
     element.setAttribute('roles', user.roles.join(','))
     element.setAttribute('username', user.username)
   }
diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
index f9f0aa2..6931544 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
@@ -60,9 +60,27 @@ class TrackChangesProvider {
       // annotation gets expanded automatically, unless the selection is on its left edge
       const annotation = this.getAnnotationByStatus('add')
       const isOnLeftEdge = this.isOnLeftEdge(annotation)
+      const isOnRightEdge = this.isOnRightEdge(annotation)
+      const isFromSameUser = this.isAnnotationFromTheSameUser(annotation)
+      const mode = this.getMode()
+
+      // console.log(annotation)
+
+      if (isFromSameUser) {
+        this.insertText(event)
+        if (isOnLeftEdge) this.expandAnnotationToDirection(annotation)
+      }
+
+      if (!isFromSameUser) {
+        if (isOnRightEdge) {
+          this.insertCharacterWithoutExpandingAnnotation(annotation, options)
+        } else {
+          this.insertText(event)
+        }
+
+        if (mode) this.createAdditionAnnotationOnLastChar()
+      }
 
-      this.insertText(event)
-      if (isOnLeftEdge) this.expandAnnotationToDirection(annotation)
       return
     }
 
@@ -215,7 +233,7 @@ class TrackChangesProvider {
     const endOffset = selection.endOffset - shortenBy
     this.updateSelection(selection, startOffset, endOffset)
 
-    console.log('collapsed', selection.isCollapsed())
+    // console.log('collapsed', selection.isCollapsed())
     if (selection.isCollapsed()) return this.handleDeleteCollapsed(options)
 
     if (isOnDelete) {
@@ -392,6 +410,7 @@ class TrackChangesProvider {
           status: status,
           type: 'track-change',
           user: {
+            id: user.id,
             roles: user.roles,
             username: user.username
           }
@@ -482,8 +501,6 @@ class TrackChangesProvider {
 
   */
 
-  // is annotation from the same user
-
   getAllAnnotationsByStatus (status) {
     const annotations = this.getAllExistingTrackAnnotations()
     const annotationsByStatus = filter(annotations, (annotation) => {
@@ -531,6 +548,14 @@ class TrackChangesProvider {
     return { skipSelection: true }
   }
 
+  isAnnotationFromTheSameUser (annotation) {
+    const annotationUser = annotation.user.id
+    const currentUser = this.config.user.id
+
+    if (annotationUser === currentUser) return true
+    return false
+  }
+
   isNotOnTrackAnnotation () {
     const annotations = this.getAllExistingTrackAnnotations()
     return (annotations.length === 0)
@@ -676,10 +701,19 @@ class TrackChangesProvider {
 
   */
 
+  getCommandManager () {
+    return this.config.commandManager
+  }
+
   getDocumentSession () {
     return this.config.documentSession
   }
 
+  getMode () {
+    const trackState = this.getTrackState()
+    return trackState.mode
+  }
+
   getSelection () {
     const surface = this.getSurface()
     return surface.getSelection()
@@ -691,6 +725,12 @@ class TrackChangesProvider {
 
     return surfaceManager.getSurface(id)
   }
+
+  getTrackState () {
+    const commandManager = this.getCommandManager()
+    const commandStates = commandManager.getCommandStates()
+    return commandStates['track-change']
+  }
 }
 
 export default TrackChangesProvider
-- 
GitLab