Skip to content

Commit

Permalink
Add annotation type to all annotation export formats
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Jan 17, 2024
1 parent d3fbfaa commit 4422012
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/sidebar/helpers/annotation-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export function isSaved(annotation: Annotation): annotation is SavedAnnotation {
*
* @deprecated - Use {@link isSaved} instead
*/
export function isNew(annotation: Annotation): boolean {
export function isNew(annotation: APIAnnotationData): boolean {
return !annotation.id;
}

Expand Down Expand Up @@ -173,7 +173,7 @@ export function isPublic(annotation: Annotation): boolean {
* as opposed to a Page Note which refers to the whole document or a reply,
* which refers to another annotation.
*/
function hasSelector(annotation: Annotation): boolean {
function hasSelector(annotation: APIAnnotationData): boolean {
return !!(
annotation.target &&
annotation.target.length > 0 &&
Expand Down Expand Up @@ -208,12 +208,14 @@ export function isHidden(annotation: Annotation): boolean {
* Highlights are generally identifiable by having no text content AND no tags,
* but there is some nuance.
*/
export function isHighlight(annotation: Annotation): boolean {
export function isHighlight(
annotation: Annotation | APIAnnotationData,
): boolean {
// `$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) {
if ('$highlight' in annotation && annotation.$highlight) {
return true;
}

Expand Down Expand Up @@ -246,7 +248,7 @@ export function isOrphan(annotation: Annotation): boolean {
/**
* Return `true` if the given annotation is a page note.
*/
export function isPageNote(annotation: Annotation): boolean {
export function isPageNote(annotation: APIAnnotationData): boolean {
return !hasSelector(annotation) && !isReply(annotation);
}

Expand All @@ -260,7 +262,7 @@ export function isAnnotation(annotation: Annotation): boolean {
/**
* Return a human-readable string describing the annotation's role.
*/
export function annotationRole(annotation: Annotation): string {
export function annotationRole(annotation: APIAnnotationData): string {
if (isReply(annotation)) {
return 'Reply';
} else if (isHighlight(annotation)) {
Expand Down
10 changes: 8 additions & 2 deletions src/sidebar/services/annotations-exporter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { escapeCSVValue } from '../../shared/csv';
import { trimAndDedent } from '../../shared/trim-and-dedent';
import type { APIAnnotationData, Profile } from '../../types/api';
import {
annotationRole,
documentMetadata,
isReply,
pageLabel,
Expand Down Expand Up @@ -82,6 +83,7 @@ export class AnnotationsExporter {
`Created at: ${formatDateTime(new Date(annotation.created))}`,
`Author: ${extractUsername(annotation)}`,
page ? `Page: ${page}` : undefined,
`Type: ${annotationRole(annotation)}`,
annotationQuote ? `Quote: "${annotationQuote}"` : undefined,
`Comment: ${annotation.text}`,
annotation.tags.length > 0
Expand Down Expand Up @@ -127,7 +129,7 @@ export class AnnotationsExporter {
pageLabel(annotation) ?? '',
uri,
groupName,
isReply(annotation) ? 'Reply' : 'Annotation',
annotationRole(annotation),
quote(annotation) ?? '',
annotation.text,
annotation.tags.join(','),
Expand All @@ -141,7 +143,7 @@ export class AnnotationsExporter {
'Page',
'URL',
'Group',
'Annotation/Reply Type',
'Type',
'Quote',
'Comment',
'Tags',
Expand Down Expand Up @@ -245,6 +247,10 @@ export class AnnotationsExporter {
<td>{page}</td>
</tr>
)}
<tr>
<td>Type:</td>
<td>{annotationRole(annotation)}</td>
</tr>
{annotationQuote && (
<tr>
<td>Quote:</td>
Expand Down
26 changes: 23 additions & 3 deletions src/sidebar/services/test/annotations-exporter-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
newAnnotation,
newHighlight,
newReply,
publicAnnotation,
} from '../../test/annotation-fixtures';
Expand Down Expand Up @@ -123,19 +124,22 @@ Total replies: 1
Annotation 1:
Created at: ${formattedNow}
Author: bill
Type: Annotation
Quote: "this is the quote"
Comment: Annotation text
Tags: tag_1, tag_2
Annotation 2:
Created at: ${formattedNow}
Author: bill
Type: Annotation
Comment: Annotation text
Tags: tag_1, tag_2
Annotation 3:
Created at: ${formattedNow}
Author: jane
Type: Annotation
Quote: "The quote"
Comment: Annotation text
Tags: foo, bar
Expand All @@ -144,13 +148,15 @@ Annotation 4:
Created at: ${formattedNow}
Author: bill
Page: 23
Type: Reply
Comment: Annotation text
Tags: tag_1, tag_2
Annotation 5:
Created at: ${formattedNow}
Author: bill
Page: iii
Type: Annotation
Comment: Annotation text`,
);
});
Expand Down Expand Up @@ -183,6 +189,7 @@ Total replies: 0
Annotation 1:
Created at: ${formattedNow}
Author: John Doe
Type: Annotation
Comment: Annotation text
Tags: tag_1, tag_2`,
);
Expand Down Expand Up @@ -214,6 +221,7 @@ Tags: tag_1, tag_2`,
},
{
...baseAnnotation,
...newHighlight(),
tags: [],
target: targetWithSelectors(pageSelector('iii')),
},
Expand All @@ -226,10 +234,10 @@ Tags: tag_1, tag_2`,

assert.equal(
result,
`Created at,Author,Page,URL,Group,Annotation/Reply Type,Quote,Comment,Tags
`Created at,Author,Page,URL,Group,Type,Quote,Comment,Tags
${formattedNow},jane,,http://example.com,My group,Annotation,,Annotation text,"foo,bar"
${formattedNow},bill,23,http://example.com,My group,Reply,"includes ""double quotes"", and commas",Annotation text,"tag_1,tag_2"
${formattedNow},bill,iii,http://example.com,My group,Annotation,,Annotation text,`,
${formattedNow},bill,iii,http://example.com,My group,Highlight,,Annotation text,`,
);
});

Expand All @@ -249,7 +257,7 @@ ${formattedNow},bill,iii,http://example.com,My group,Annotation,,Annotation text

assert.equal(
result,
`Created at,Author,Page,URL,Group,Annotation/Reply Type,Quote,Comment,Tags
`Created at,Author,Page,URL,Group,Type,Quote,Comment,Tags
${formattedNow},John Doe,,http://example.com,My group,Annotation,,Annotation text,"tag_1,tag_2"`,
);
});
Expand Down Expand Up @@ -361,6 +369,10 @@ ${formattedNow},John Doe,,http://example.com,My group,Annotation,,Annotation tex
<td>Author:</td>
<td>jane</td>
</tr>
<tr>
<td>Type:</td>
<td>Annotation</td>
</tr>
<tr>
<td>Quote:</td>
<td>
Expand Down Expand Up @@ -396,6 +408,10 @@ ${formattedNow},John Doe,,http://example.com,My group,Annotation,,Annotation tex
<td>Page:</td>
<td>23</td>
</tr>
<tr>
<td>Type:</td>
<td>Annotation</td>
</tr>
<tr>
<td>Quote:</td>
<td>
Expand Down Expand Up @@ -431,6 +447,10 @@ ${formattedNow},John Doe,,http://example.com,My group,Annotation,,Annotation tex
<td>Page:</td>
<td>iii</td>
</tr>
<tr>
<td>Type:</td>
<td>Reply</td>
</tr>
<tr>
<td>Comment:</td>
<td>Annotation text</td>
Expand Down

0 comments on commit 4422012

Please sign in to comment.