From 6131a657539f75aabe6d48c14cc769c2dcaf2098 Mon Sep 17 00:00:00 2001
From: john <johnbarlas39@gmail.com>
Date: Fri, 30 Dec 2016 01:00:43 +0200
Subject: [PATCH] select and write strikes through selection and creates add
 annotation after it

---
 .../track_change/TrackChangesProvider.js      | 170 ++++--------------
 1 file changed, 37 insertions(+), 133 deletions(-)

diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
index 26ed589..63c653c 100644
--- a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
+++ b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js
@@ -18,11 +18,8 @@ class TrackChangesProvider {
   }
 
   handleTransaction (options) {
-    // const status = options.status
-
     this.handleAdd(options)
     this.handleDelete(options)
-    // if (this.shouldDelete(status)) this.delete()
   }
 
   handleAdd (options) {
@@ -41,6 +38,17 @@ class TrackChangesProvider {
       this.createAddAnnotation(selection)
       this.moveCursorTo('end')
       return
+    } else {
+      selection = this.getSelection()
+      this.createDeleteAnnotation(selection)
+      this.moveCursorTo('end')
+
+      this.insertText(event)
+      selection = this.setSelectionPlusOne('left')
+      this.createAddAnnotation(selection)
+      this.separateAnnotations()
+      this.moveCursorTo('end')
+      return
     }
   }
 
@@ -49,37 +57,37 @@ class TrackChangesProvider {
     if (status !== 'delete') return
 
     const mode = this.getMode()
+    const isSelectionCollapsed = this.isSelectionCollapsed()
+
     let selection
 
-    console.log('move', move)
     const direction = {
       cursorTo: (move === 'left') ? 'start' : 'end',
       move: move
     }
 
-    if (!mode) {
-      // selection = this.setSelectionPlusOne('left')
-      selection = this.setSelectionPlusOne(direction.move)
+    if (isSelectionCollapsed) {
+      if (!mode) {
+        selection = this.setSelectionPlusOne(direction.move)
+        this.createDeleteAnnotation(selection)
+        this.moveCursorTo(direction.cursorTo)
+        return
+      }
+      if (mode === 'delete') {
+        selection = this.setSelectionPlusOne(direction.move)
+        const annotation = this.getAnnotationByStatus('delete')
+        this.expandTrackAnnotation(selection, annotation)
+        this.moveCursorTo(direction.cursorTo)
+        return
+      }
+    } else {
+      selection = this.getSelection()
       this.createDeleteAnnotation(selection)
       this.moveCursorTo(direction.cursorTo)
       return
     }
-    if (mode === 'delete') {
-      selection = this.setSelectionPlusOne(direction.move)
-      const annotation = this.getAnnotationByStatus('delete')
-      this.expandTrackAnnotation(selection, annotation)
-      this.moveCursorTo(direction.cursorTo)
-    }
-
-    // const isSelectionCollapsed = this.isSelectionCollapsed()
-
-    // if (!this.isSelectionCollapsed()) options.deleteCollapsed = true
-    // this.createDeletionAnnotation(options)
   }
 
-  // expand annotation
-  // truncate annotation
-
   createAddAnnotation (selection) {
     this.createTrackAnnotation(selection, 'add')
   }
@@ -114,60 +122,11 @@ class TrackChangesProvider {
     })
   }
 
-  insertText (event) {
-    const surface = this.getSurface()
-    surface.transaction(function (tx, args) {
-      if (surface.domSelection) surface.domSelection.clear()
-      args.text = event.data
-      return surface.insertText(tx, args)
-    }, { action: 'type' })
-  }
-
-  createAdditionAnnotation () {
-    const mode = this.getMode()
-    const onDelete = this.isOnAnnotation('delete')
-    // const onAdd = this.isOnAnnotation('add')
-    // console.log('mode', mode)
-    // console.log('on delete', onDelete)
-    // console.log('on add', onAdd)
-    if (mode && !onDelete) return
-
-    const surface = this.getSurface()
-
-    const transformation = (tx, args) => {
-      const selection = this.createSelection(args)
-
-      const newNode = {
-        selection: selection,
-        node: {
-          'status': 'add',
-          'type': 'track-change'
-        }
-      }
-
-      createAnnotation(tx, newNode)
-      this.clearSelection(args, 'add')
-    }
-
-    const info = {}
-    surface.transaction(transformation, info)
-
-    if (onDelete) {
-      this.separateAnnotations()
-    }
-
-    // if (onAdd) {
-    //
-    // }
-  }
-
   separateAnnotations () {
-    console.log('separate')
     const surface = this.getSurface()
-    const deleteAnnotation = this.getAnnotationByStatus('delete')
+
     const addAnnotation = this.getAnnotationByStatus('add')
-    console.log('delete anno', deleteAnnotation)
-    console.log('add anno', addAnnotation)
+    const deleteAnnotation = this.getAnnotationByStatus('delete')
 
     surface.transaction((tx, args) => {
       args.anno = deleteAnnotation
@@ -176,70 +135,15 @@ class TrackChangesProvider {
     })
   }
 
-  createDeletionAnnotation (options) {
-    const deleteCollapsed = options.deleteCollapsed
-    const mode = this.getMode()
+  insertText (event) {
     const surface = this.getSurface()
-
-    const transformation = (tx, args) => {
-      let selection = args.selection
-      if (!deleteCollapsed) selection = this.createSelection(args)
-
-      if (mode === 'delete') {
-        args.anno = this.getExistingAnnotation()
-        expandAnnotation(tx, args)
-      } else {
-        const newNode = {
-          selection: selection,
-          node: {
-            'status': 'delete',
-            'type': 'track-change'
-          }
-        }
-
-        createAnnotation(tx, newNode)
-      }
-
-      args.deleteCollapsed = deleteCollapsed
-      this.clearSelection(args, 'delete')
-    }
-
-    const info = {}
-    surface.transaction(transformation, info)
-  }
-
-  /*
-
-    DECISION MAKERS
-
-  */
-
-  // shouldAdd (status) {
-  //   if (status !== 'add') return false
-  //
-  //   const mode = this.getMode()
-  //   if (mode) return false
-  //
-  //   return true
-  // }
-
-  shouldDelete (status) {
-    if (status === 'delete') return true
-
-    console.log(status)
-    console.log(this.isSelectionCollapsed())
-    if (status === 'create' && !this.isSelectionCollapsed()) return true
-    return false
+    surface.transaction(function (tx, args) {
+      if (surface.domSelection) surface.domSelection.clear()
+      args.text = event.data
+      return surface.insertText(tx, args)
+    }, { action: 'type' })
   }
 
-  // shouldExpand () {
-  //
-  // }
-
-  // shouldTruncate () {
-  //
-  // }
-
   /*
 
     ANNOTATION HELPERS
-- 
GitLab