diff --git a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js index c20679d3da77c9fa8f4dd364380fc3ba4516922f..bdf63bb76e29f5c4e778e29359781f81914d09b9 100644 --- a/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js +++ b/app/components/SimpleEditor/elements/track_change/TrackChangesProvider.js @@ -1,8 +1,8 @@ import { clone, // each, - filter, - find, + // filter, + // find, findIndex, includes, keys, @@ -20,6 +20,16 @@ import { TOCProvider } from 'substance' +import { + // getAllAnnotationsByStatus, + getAnnotationByStatus, + isAnnotationFromTheSameUser, + isNotOnTrackAnnotation, + isOnAnnotation, + isSelectionOnLeftEdge, + isSelectionOnRightEdge +} from './utils/annotationHelpers' + import { createAdditionAnnotationOnLastChar, deleteAllOwnAdditions, @@ -88,18 +98,18 @@ class TrackChangesProvider extends TOCProvider { handleAddCollapsed (options) { // const { event } = options - const notOnTrack = this.isNotOnTrackAnnotation() - const isOnAdd = this.isOnAnnotation('add') - const isOnDelete = this.isOnAnnotation('delete') + const notOnTrack = isNotOnTrackAnnotation(options) + const isOnAdd = isOnAnnotation(options, 'add') + const isOnDelete = isOnAnnotation(options, 'delete') if (notOnTrack) return insertCharacterWithAddAnnotation(options) if (isOnAdd) { // 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 annotation = getAnnotationByStatus(options, 'add') + const isOnLeftEdge = isSelectionOnLeftEdge(options, annotation) + const isOnRightEdge = isSelectionOnRightEdge(options, annotation) + const isFromSameUser = isAnnotationFromTheSameUser(options, annotation) const mode = this.getMode() if (isFromSameUser) { @@ -121,10 +131,10 @@ class TrackChangesProvider extends TOCProvider { } if (isOnDelete) { - const annotation = this.getAnnotationByStatus('delete') + const annotation = getAnnotationByStatus(options, 'delete') - const isOnLeftEdge = this.isOnLeftEdge(annotation) - const isOnRightEdge = this.isOnRightEdge(annotation) + const isOnLeftEdge = isSelectionOnLeftEdge(options, annotation) + const isOnRightEdge = isSelectionOnRightEdge(options, annotation) const withinAnnotation = this.isSelectionContainedWithin(annotation, true) // if contained within the delete annotation, move it to the edge, @@ -149,8 +159,8 @@ class TrackChangesProvider extends TOCProvider { handleAddNonCollapsed (options) { let { selection } = options - const notOnTrack = this.isNotOnTrackAnnotation() - const isOnDelete = this.isOnAnnotation('delete') + const notOnTrack = isNotOnTrackAnnotation(options) + const isOnDelete = isOnAnnotation(options, 'delete') if (notOnTrack) return deleteSelectedAndCreateAddition(options) @@ -164,7 +174,7 @@ class TrackChangesProvider extends TOCProvider { if (isOnDelete) { // console.log('on delete') - const annotation = this.getAnnotationByStatus('delete') + const annotation = getAnnotationByStatus(options, 'delete') const withinAnnotation = this.isSelectionContainedWithin(annotation) if (withinAnnotation) { @@ -208,17 +218,17 @@ class TrackChangesProvider extends TOCProvider { handleDeleteCollapsed (options) { const { direction } = options - const notOnTrack = this.isNotOnTrackAnnotation() - const isOnAdd = this.isOnAnnotation('add') - const isOnDelete = this.isOnAnnotation('delete') + const notOnTrack = isNotOnTrackAnnotation(options) + const isOnAdd = isOnAnnotation(options, 'add') + const isOnDelete = isOnAnnotation(options, 'delete') if (notOnTrack) return selectCharacterAndMarkDeleted(options) if (isOnAdd) { - const annotation = this.getAnnotationByStatus('add') - const isOnLeftEdge = this.isOnLeftEdge(annotation) - const isOnRightEdge = this.isOnRightEdge(annotation) - const isFromSameUser = this.isAnnotationFromTheSameUser(annotation) + const annotation = getAnnotationByStatus(options, 'add') + const isOnLeftEdge = isSelectionOnLeftEdge(options, annotation) + const isOnRightEdge = isSelectionOnRightEdge(options, annotation) + const isFromSameUser = isAnnotationFromTheSameUser(options, annotation) const mode = this.getMode() const key = direction.key @@ -243,10 +253,10 @@ class TrackChangesProvider extends TOCProvider { } if (isOnDelete) { - const annotation = this.getAnnotationByStatus('delete') - const isOnLeftEdge = this.isOnLeftEdge(annotation) - const isOnRightEdge = this.isOnRightEdge(annotation) - const isFromSameUser = this.isAnnotationFromTheSameUser(annotation) + const annotation = getAnnotationByStatus(options, 'delete') + const isOnLeftEdge = isSelectionOnLeftEdge(options, annotation) + const isOnRightEdge = isSelectionOnRightEdge(options, annotation) + const isFromSameUser = isAnnotationFromTheSameUser(options, annotation) const key = direction.key let moveOnly, point @@ -279,8 +289,8 @@ class TrackChangesProvider extends TOCProvider { const { direction } = options let { selection } = options - const notOnTrack = this.isNotOnTrackAnnotation() - const isOnDelete = this.isOnAnnotation('delete') + const notOnTrack = isNotOnTrackAnnotation(options) + const isOnDelete = isOnAnnotation(options, 'delete') if (notOnTrack) return markSelectionAsDeleted(options) @@ -296,7 +306,7 @@ class TrackChangesProvider extends TOCProvider { if (isOnDelete) { // console.log - const annotation = this.getAnnotationByStatus('delete') + const annotation = getAnnotationByStatus(options, 'delete') const containedWithin = this.isSelectionContainedWithin(annotation) if (containedWithin) { @@ -421,91 +431,6 @@ class TrackChangesProvider extends TOCProvider { return some(accepted, (role) => includes(user.roles, role)) } - /* - - ANNOTATION HELPERS - - */ - - getAllAnnotationsByStatus (status) { - const annotations = this.getAllExistingTrackAnnotations() - const annotationsByStatus = filter(annotations, (annotation) => { - return annotation.status === status - }) - return annotationsByStatus - } - - // TODO -- handle multiple delete and add annotations - getAnnotationByStatus (status) { - const annotationsForStatus = this.getAllAnnotationsByStatus(status) - return annotationsForStatus[0] - } - - // getExistingAnnotation () { - // const documentSession = this.getEditorSession() - // const selectionState = documentSession.getSelectionState() - // const annotations = selectionState.getAnnotationsForType('track-change') - // return annotations[0] - // } - - getAllExistingTrackAnnotations () { - const editorSession = this.getEditorSession() - const selectionState = editorSession.getSelectionState() - const annotations = selectionState.getAnnotationsForType('track-change') - return annotations - } - - getAnnotationUser (annotation) { - return annotation.user.id - } - - // getDistinctAnnotationsForSelection () { - // const selection = this.getSelection() - // const annotations = this.getAllExistingTrackAnnotations() - // - // console.log(selection) - // console.log(annotations) - // - // console.log(selection.getFragments()) - // - // // get all annotations by status (add / delete) - // // get all pieces of free text - // } - - isAnnotationFromTheSameUser (annotation) { - const annotationUser = this.getAnnotationUser(annotation) - const currentUser = this.getCurrentUser() - - if (annotationUser === currentUser) return true - return false - } - - isNotOnTrackAnnotation () { - const annotations = this.getAllExistingTrackAnnotations() - return (annotations.length === 0) - } - - // returns whether the selection is on an add / delete tracked change - isOnAnnotation (status) { - const annotations = this.getAllExistingTrackAnnotations() - const annotation = find(annotations, (annotation) => { - return annotation.status === status - }) - - if (!annotation) return false - return true - } - - isOnLeftEdge (annotation) { - const selection = this.getSelection() - return (selection.start.offset === annotation.start.offset) - } - - isOnRightEdge (annotation) { - const selection = this.getSelection() - return (selection.end.offset === annotation.end.offset) - } - /** HISTORY HANDLERS diff --git a/app/components/SimpleEditor/elements/track_change/utils/annotationHelpers.js b/app/components/SimpleEditor/elements/track_change/utils/annotationHelpers.js index e8eff85a712bb56e418bddedeadede614251be60..68e5591176ffdfc409a2cc6075ba2e47ace13af2 100644 --- a/app/components/SimpleEditor/elements/track_change/utils/annotationHelpers.js +++ b/app/components/SimpleEditor/elements/track_change/utils/annotationHelpers.js @@ -1,4 +1,6 @@ -import { filter } from 'lodash' +import { filter, find } from 'lodash' + +import { getSelection } from './selectionHelpers' const getAllAnnotationsByStatus = (options, status) => { const annotations = getAllExistingTrackAnnotations(options) @@ -20,6 +22,12 @@ const getAllExistingTrackAnnotations = (options) => { return annotations } +// TODO -- handle multiple delete and add annotations +const getAnnotationByStatus = (options, status) => { + const annotationsForStatus = getAllAnnotationsByStatus(options, status) + return annotationsForStatus[0] +} + const getAnnotationUser = (annotation) => { return annotation.user.id } @@ -32,7 +40,41 @@ const isAnnotationFromTheSameUser = (options, annotation) => { return false } +const isNotOnTrackAnnotation = (options) => { + const annotations = getAllExistingTrackAnnotations(options) + return (annotations.length === 0) +} + +// returns whether the selection is on an add / delete tracked change +const isOnAnnotation = (options, status) => { + const annotations = getAllExistingTrackAnnotations(options) + + const annotation = find(annotations, (annotation) => { + return annotation.status === status + }) + + if (!annotation) return false + return true +} + +const isSelectionOnLeftEdge = (options, annotation) => { + // const selection = this.getSelection() + const selection = getSelection(options.surface) + return (selection.start.offset === annotation.start.offset) +} + +const isSelectionOnRightEdge = (options, annotation) => { + // const selection = this.getSelection() + const selection = getSelection(options.surface) + return (selection.end.offset === annotation.end.offset) +} + export { getAllAnnotationsByStatus, - isAnnotationFromTheSameUser + getAnnotationByStatus, + isAnnotationFromTheSameUser, + isNotOnTrackAnnotation, + isOnAnnotation, + isSelectionOnLeftEdge, + isSelectionOnRightEdge } diff --git a/app/components/SimpleEditor/elements/track_change/utils/transformations.js b/app/components/SimpleEditor/elements/track_change/utils/transformations.js index fef1c9de5df749088f428ca8429d301cfa029d92..1d44240caabb088c8827d316773b6b14ae0e8658 100644 --- a/app/components/SimpleEditor/elements/track_change/utils/transformations.js +++ b/app/components/SimpleEditor/elements/track_change/utils/transformations.js @@ -60,7 +60,7 @@ const createTrackAnnotation = options => { const { selection, status, surface, user } = options const info = getInfo() - console.log(selection) + // console.log(selection) // TODO -- these two ifs don't work anymore if (selection.isContainerSelection()) {