Commit e6aadd79 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Add `isHighlight` util function

parent 34d63c81
...@@ -113,7 +113,7 @@ function oldHighlight() { ...@@ -113,7 +113,7 @@ function oldHighlight() {
return { return {
id: 'annotation_id', id: 'annotation_id',
$highlight: undefined, $highlight: undefined,
target: ['foo', 'bar'], target: [{ source: 'source', selector: [] }],
references: [], references: [],
text: '', text: '',
tags: [], tags: [],
......
...@@ -149,6 +149,43 @@ function isWaitingToAnchor(annotation) { ...@@ -149,6 +149,43 @@ function isWaitingToAnchor(annotation) {
); );
} }
/**
* Is this annotation a highlight?
*
* Highlights are generally identifiable by having no text content AND no tags,
* but there is some nuance.
*
* @param {Object} annotation
* @return {boolean}
*/
function isHighlight(annotation) {
// `$highlight` is an ephemeral attribute set by the `annotator` on new
// annotation objects (created by clicking the "highlight" button).
// It is not persisted and cannot be relied upon, but if it IS present,
// this is definitely a highlight (one which is not yet saved).
if (annotation.$highlight) {
return true;
}
if (isNew(annotation)) {
// For new (unsaved-to-service) annotations, unless they have a truthy
// `$highlight` attribute, we don't know yet if they are a highlight.
return false;
}
// Note that it is possible to end up with an empty (no `text`) annotation
// that is not a highlight by adding at least one tag—thus, it is necessary
// to check for the existence of tags as well as text content.
return (
!isPageNote(annotation) &&
!isReply(annotation) &&
!annotation.hidden && // A hidden annotation has some form of objectionable content
!annotation.text &&
!(annotation.tags && annotation.tags.length)
);
}
/** Return `true` if the given annotation is an orphan. */ /** Return `true` if the given annotation is an orphan. */
function isOrphan(annotation) { function isOrphan(annotation) {
return hasSelector(annotation) && annotation.$orphan; return hasSelector(annotation) && annotation.$orphan;
...@@ -220,6 +257,7 @@ module.exports = { ...@@ -220,6 +257,7 @@ module.exports = {
domainAndTitle: domainAndTitle, domainAndTitle: domainAndTitle,
flagCount: flagCount, flagCount: flagCount,
isAnnotation: isAnnotation, isAnnotation: isAnnotation,
isHighlight: isHighlight,
isNew: isNew, isNew: isNew,
isOrphan: isOrphan, isOrphan: isOrphan,
isPageNote: isPageNote, isPageNote: isPageNote,
......
...@@ -234,6 +234,58 @@ describe('annotation-metadata', function() { ...@@ -234,6 +234,58 @@ describe('annotation-metadata', function() {
}); });
}); });
describe('.isHighlight', () => {
[
{
annotation: fixtures.newEmptyAnnotation(),
expect: false,
desc: 'new, empty annotation',
},
{
annotation: fixtures.newReply(),
expect: false,
desc: 'new, reply annotation',
},
{
annotation: fixtures.newAnnotation(),
expect: false,
desc: 'new, with some text',
},
{
annotation: fixtures.newHighlight(),
expect: true,
desc: 'new, marked as $highlight',
},
{
annotation: fixtures.oldAnnotation(),
expect: false,
desc: 'pre-existing annotation',
},
{
annotation: fixtures.oldHighlight(),
expect: true,
desc: 'pre-existing higlight',
},
{
annotation: fixtures.oldPageNote(),
expect: false,
desc: 'pre-existing page note',
},
{
annotation: fixtures.oldReply(),
expect: false,
desc: 'pre-existing reply',
},
].forEach(testcase => {
it(`returns ${testcase.expect} for isHighlight when annotation is: ${testcase.desc}`, () => {
assert.equal(
annotationMetadata.isHighlight(testcase.annotation),
testcase.expect
);
});
});
});
describe('.isPageNote', function() { describe('.isPageNote', function() {
it('returns true for an annotation with an empty target', function() { it('returns true for an annotation with an empty target', function() {
assert.isTrue( assert.isTrue(
......
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