diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js index b9ed5d70493d8545c5a9ea0e83c420f02f49cd98..ca50b0db2db05a860c9e6e3a44e2d41839deedeb 100644 --- a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js +++ b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js @@ -42,11 +42,12 @@ class TrackChangesProvider { let selection if (isSelectionCollapsed) { - this.insertText(event) + // this.insertText(event) const notOnTrack = this.isNotOnTrackAnnotation() if (notOnTrack) { + this.insertText(event) selection = this.setSelectionPlusOne('left') this.createAddAnnotation(selection) this.moveCursorTo('end') @@ -58,6 +59,7 @@ class TrackChangesProvider { const annotation = this.getAnnotationByStatus('add') const isOnLeftEdge = this.isOnLeftEdge(annotation) + this.insertText(event) if (isOnLeftEdge) { selection = this.setSelectionPlusOne('left') this.expandTrackAnnotation(selection, annotation) @@ -65,6 +67,47 @@ class TrackChangesProvider { return } } + + const isOnDelete = this.isOnAnnotation('delete') + + if (isOnDelete) { + console.log('on delete') + const annotation = this.getAnnotationByStatus('delete') + const withinAnnotation = this.isSelectionContainedWithin(annotation, true) + + if (withinAnnotation) { + // console.log('in') + this.moveCursorTo(annotation.endOffset) + this.insertText(event) + selection = this.setSelectionPlusOne('left') + this.createAddAnnotation(selection) + this.separateAnnotations() + this.moveCursorTo('end') + return + } + + console.log('out') + const isOnLeftEdge = this.isOnLeftEdge(annotation) + if (isOnLeftEdge) { + // console.log('left') + this.insertText(event) + selection = this.setSelectionPlusOne('left') + this.createAddAnnotation(selection) + this.moveCursorTo('end') + return + } + + const isOnRightEdge = this.isOnRightEdge(annotation) + if (isOnRightEdge) { + // console.log('right') + this.insertText(event) + selection = this.setSelectionPlusOne('left') + this.createAddAnnotation(selection) + this.separateAnnotations() + this.moveCursorTo('end') + return + } + } } } else { selection = this.getSelection() @@ -103,6 +146,35 @@ class TrackChangesProvider { selection = this.setSelectionPlusOne('left') this.createAddAnnotation(selection) this.moveCursorTo('end') + return + } + + const isOnDelete = this.isOnAnnotation('delete') + + if (isOnDelete) { + const annotation = this.getAnnotationByStatus('delete') + const withinAnnotation = this.isSelectionContainedWithin(annotation) + + if (withinAnnotation) { + this.moveCursorTo(annotation.endOffset) + this.insertText(event) + selection = this.setSelectionPlusOne('left') + this.createAddAnnotation(selection) + this.separateAnnotations() + this.moveCursorTo('end') + return + } + + selection = this.getSelection() + this.expandTrackAnnotation(selection, annotation) + + this.moveCursorTo(annotation.endOffset) + this.insertText(event) + selection = this.setSelectionPlusOne('left') + this.createAddAnnotation(selection) + this.separateAnnotations() + this.moveCursorTo('end') + return } } } @@ -130,16 +202,6 @@ class TrackChangesProvider { return } else { const isOnAdd = this.isOnAnnotation('add') - const isOnDelete = this.isOnAnnotation('delete') - - if (isOnDelete) { - selection = this.setSelectionPlusOne(direction.move) - const annotation = this.getAnnotationByStatus('delete') - this.expandTrackAnnotation(selection, annotation) - this.moveCursorTo(direction.cursorTo) - return - } - if (isOnAdd) { const annotation = this.getAnnotationByStatus('add') const isOnLeftEdge = this.isOnLeftEdge(annotation) @@ -159,6 +221,34 @@ class TrackChangesProvider { this.deleteCharacter(direction.move) return } + + const isOnDelete = this.isOnAnnotation('delete') + if (isOnDelete) { + const annotation = this.getAnnotationByStatus('delete') + const isOnLeftEdge = this.isOnLeftEdge(annotation) + const isOnRightEdge = this.isOnRightEdge(annotation) + const key = direction.key + + let moveOnly, point + + if (!isOnLeftEdge && !isOnRightEdge) { + point = annotation[direction.cursorTo + 'Offset'] + moveOnly = true + } else if (isOnLeftEdge && key === 'DELETE') { + point = annotation.endOffset + moveOnly = true + } else if (isOnRightEdge && key === 'BACKSPACE') { + point = annotation.startOffset + moveOnly = true + } + + if (moveOnly) return this.moveCursorTo(point) + + selection = this.setSelectionPlusOne(direction.move) + this.expandTrackAnnotation(selection, annotation) + this.moveCursorTo(direction.cursorTo) + return + } } } else { const notOnTrack = this.isNotOnTrackAnnotation() @@ -194,6 +284,28 @@ class TrackChangesProvider { this.separateAnnotations() return } + + const isOnDelete = this.isOnAnnotation('delete') + + if (isOnDelete) { + const annotation = this.getAnnotationByStatus('delete') + const containedWithin = this.isSelectionContainedWithin(annotation) + + if (containedWithin) { + const point = annotation[direction.cursorTo + 'Offset'] + return this.moveCursorTo(point) + } + + const selection = this.getSelection() + this.expandTrackAnnotation(selection, annotation) + + const key = direction.key + + let point + if (key === 'BACKSPACE') point = selection.startOffset + if (key === 'DELETE') point = selection.endOffset + this.moveCursorTo(point) + } } } @@ -211,6 +323,7 @@ class TrackChangesProvider { this.createTrackAnnotation(selection, 'delete') } + // TODO -- selection could default to current selection createTrackAnnotation (selection, status) { const surface = this.getSurface() const info = this.getInfo() @@ -245,6 +358,7 @@ class TrackChangesProvider { separateAnnotations () { const surface = this.getSurface() + const info = this.getInfo() // TODO -- separate all annotations // to see this fail: @@ -255,11 +369,13 @@ class TrackChangesProvider { const addAnnotation = this.getAnnotationByStatus('add') const deleteAnnotation = this.getAnnotationByStatus('delete') - surface.transaction((tx, args) => { + const transformation = (tx, args) => { args.anno = deleteAnnotation args.selection = addAnnotation.getSelection() truncateAnnotation(tx, args) - }) + } + + surface.transaction(transformation, info) } insertText (event) { @@ -495,11 +611,11 @@ class TrackChangesProvider { return selection } - isAnnotationContainedWithinSelection (annotation) { + isAnnotationContainedWithinSelection (annotation, strict) { const selection = this.getSelection() const annotationSelection = annotation.getSelection() - return selection.contains(annotationSelection) + return selection.contains(annotationSelection, strict) } isSelectionCollapsed () { @@ -508,14 +624,18 @@ class TrackChangesProvider { return isCollapsed } - isSelectionContainedWithin (annotation) { + // TODO -- refactor this and isAnnotationContainedWithinSelection into one + isSelectionContainedWithin (annotation, strict) { const selection = this.getSelection() + const annotationSelection = annotation.getSelection() - const leftSide = (selection.startOffset < annotation.startOffset) - const rightSide = (selection.endOffset > annotation.endOffset) + return annotationSelection.contains(selection, strict) - if (leftSide || rightSide) return false - return true + // const leftSide = (selection.startOffset < annotation.startOffset) + // const rightSide = (selection.endOffset > annotation.endOffset) + // + // if (leftSide || rightSide) return false + // return true } /*