Commit 4825ddc1 authored by Aron Carroll's avatar Aron Carroll

Merge pull request #1717 from hypothesis/1238-restore-diff-behavior

Restore visual diff to original behaviour.
parents 77ccedaf 423769d8
...@@ -128,6 +128,19 @@ AnnotationController = [ ...@@ -128,6 +128,19 @@ AnnotationController = [
@action = 'view' @action = 'view'
@editing = false @editing = false
# Calculates the visual diff flags from the targets
#
# hasDiff is set to true is there are any targets with a difference
# shouldShowDiff is set to true if there are some meaningful differences
# - that is, more than just uppercase / lowercase
diffFromTargets = (targets) ->
hasDiff = targets.some (t) ->
t.diffHTML?
shouldShowDiff = hasDiff and targets.some (t) ->
t.diffHTML? and not t.diffCaseOnly
{hasDiff, shouldShowDiff}
###* ###*
# @ngdoc method # @ngdoc method
# @name annotation.AnnotationController#save # @name annotation.AnnotationController#save
...@@ -215,13 +228,15 @@ AnnotationController = [ ...@@ -215,13 +228,15 @@ AnnotationController = [
@annotation.tags = ({text} for text in (model.tags or [])) @annotation.tags = ({text} for text in (model.tags or []))
# Calculate the visual diff flags # Calculate the visual diff flags
@hasDiff = false diffFlags = diffFromTargets(@annotation.target)
for t in @annotation.target or [] @hasDiff = diffFlags.hasDiff
if t.diffHTML? and not t.diffCaseOnly if @hasDiff
@hasDiff = t.hasDiff = true # We don't want to override the showDiff value manually changed
else # by the user, that's why we use a conditional assignment here,
t.hasDiff = false # instead of directly setting showDiff to the calculated value
@showDiff ?= @hasDiff or undefined @showDiff ?= diffFlags.shouldShowDiff
else
@showDiff = undefined
updateTimestamp = (repeat=false) => updateTimestamp = (repeat=false) =>
@timestamp = timeHelpers.toFuzzyString model.updated @timestamp = timeHelpers.toFuzzyString model.updated
......
...@@ -44,14 +44,14 @@ ...@@ -44,14 +44,14 @@
<section class="annotation-target" <section class="annotation-target"
ng-repeat="target in vm.annotation.target track by $index"> ng-repeat="target in vm.annotation.target track by $index">
<blockquote class="annotation-quote" <blockquote class="annotation-quote"
ng-hide="target.hasDiff && vm.showDiff" ng-hide="target.diffHTML && vm.showDiff"
ng-bind-html="selector.exact" ng-bind-html="selector.exact"
ng-repeat="selector in target.selector ng-repeat="selector in target.selector
| filter : {'type': 'TextQuoteSelector'} | filter : {'type': 'TextQuoteSelector'}
track by $index"></blockquote> track by $index"></blockquote>
<blockquote class="annotation-quote" <blockquote class="annotation-quote"
ng-bind-html="target.diffHTML" ng-bind-html="target.diffHTML"
ng-show="target.hasDiff && vm.showDiff"></blockquote> ng-show="target.diffHTML && vm.showDiff"></blockquote>
</section> </section>
<div class="small pull-right" <div class="small pull-right"
ng-show="vm.hasDiff"> ng-show="vm.hasDiff">
......
...@@ -127,20 +127,50 @@ describe 'h.directives.annotation', -> ...@@ -127,20 +127,50 @@ describe 'h.directives.annotation', ->
controller.render() controller.render()
assert.isNull(controller.document) assert.isNull(controller.document)
describe 'when targets have the same selection text as the anchor', -> describe 'when a single target has text identical to what was in the selectors', ->
it 'sets `showDiff` to undefined and `hasDiff` to false', -> it 'sets `showDiff` to undefined and `hasDiff` to false', ->
controller.render() controller.render()
assert.isFalse(controller.hasDiff) assert.isFalse(controller.hasDiff)
assert.isUndefined(controller.showDiff) assert.isUndefined(controller.showDiff)
describe 'when targets have different selection text from the anchor', -> describe 'when a single target has different text, compared to what was in the selector', ->
targets = null
beforeEach ->
annotation.target = [
{diffHTML: "This is <ins>not</ins> the same quote", diffCaseOnly: false},
]
controller.render()
targets = controller.annotation.target
it 'sets both `hasDiff` and `showDiff` to true', ->
controller.render()
assert.isTrue(controller.hasDiff)
assert.isTrue(controller.showDiff)
describe 'when thre are only upper case/lower case difference between the text in the single target, and what was saved in a selector', ->
targets = null
beforeEach ->
annotation.target = [
{diffHTML: "<ins>s</ins><del>S</del>tuff", diffCaseOnly: true},
]
controller.render()
targets = controller.annotation.target
it 'sets `hasDiff` to true and `showDiff` to false', ->
controller.render()
assert.isTrue(controller.hasDiff)
assert.isFalse(controller.showDiff)
describe 'when there are multiple targets, some with differences in text', ->
targets = null targets = null
beforeEach -> beforeEach ->
annotation.target = [ annotation.target = [
{otherProperty: 'bar'}, {otherProperty: 'bar'},
{diffHTML: "things"}, {diffHTML: "This is <ins>not</ins> the same quote", diffCaseOnly: false},
{diffHTML: "stuff", diffCaseOnly: true}, {diffHTML: "<ins>s</ins><del>S</del>tuff", diffCaseOnly: true},
] ]
controller.render() controller.render()
targets = controller.annotation.target targets = controller.annotation.target
...@@ -151,13 +181,6 @@ describe 'h.directives.annotation', -> ...@@ -151,13 +181,6 @@ describe 'h.directives.annotation', ->
it 'sets `showDiff` to true', -> it 'sets `showDiff` to true', ->
assert.isTrue(controller.showDiff) assert.isTrue(controller.showDiff)
it 'sets `hasDiff` to true on targets with differences', ->
assert.isFalse(targets[0].hasDiff)
assert.isTrue(targets[1].hasDiff)
it 'sets `hasDiff` to false on targets with only case differences', ->
assert.isFalse(targets[2].hasDiff)
it 'preserves the `showDiff` value on update', -> it 'preserves the `showDiff` value on update', ->
controller.showDiff = false controller.showDiff = false
annotation.target = annotation.target.slice(1) annotation.target = annotation.target.slice(1)
...@@ -169,6 +192,34 @@ describe 'h.directives.annotation', -> ...@@ -169,6 +192,34 @@ describe 'h.directives.annotation', ->
controller.render() controller.render()
assert.isFalse(controller.hasDiff) assert.isFalse(controller.hasDiff)
describe 'when there are multiple targets, some with differences, but only upper case / lower case', ->
targets = null
beforeEach ->
annotation.target = [
{otherProperty: 'bar'},
{diffHTML: "<ins>s</ins><del>S</del>tuff", diffCaseOnly: true},
]
controller.render()
targets = controller.annotation.target
it 'sets `hasDiff` to true', ->
assert.isTrue(controller.hasDiff)
it 'sets `showDiff` to false', ->
assert.isFalse(controller.showDiff)
it 'preserves the `showDiff` value on update', ->
controller.showDiff = true
annotation.target = annotation.target.slice(1)
controller.render()
assert.isTrue(controller.showDiff)
it 'unsets `hasDiff` if differences go away', ->
annotation.target = annotation.target.splice(0, 1)
controller.render()
assert.isFalse(controller.hasDiff)
describe 'timestamp', -> describe 'timestamp', ->
clock = null clock = null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment