Skip to content

Commit

Permalink
Add stats functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AbeJellinek authored and dstillman committed Nov 8, 2024
1 parent cfb5058 commit 88f2b5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/common/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,15 @@ class Reader {
return this._annotationManager.mergeAnnotations(ids);
}

/**
* @param {BufferSource} metadata
* @returns {{ count: number, lastModified?: Date }}
*/
getKOReaderAnnotationStats(metadata) {
this._ensureType('epub');
return this._primaryView.getKOReaderAnnotationStats(metadata);
}

/**
* @param {BufferSource} metadata
*/
Expand All @@ -1037,6 +1046,15 @@ class Reader {
this._primaryView.importAnnotationsFromKOReaderMetadata(metadata);
}

/**
* @param {string} metadata
* @returns {{ count: number, lastModified?: Date }}
*/
getCalibreAnnotationStats(metadata) {
this._ensureType('epub');
return this._primaryView.getCalibreAnnotationStats(metadata);
}

/**
* @param {string} metadata
*/
Expand Down
32 changes: 32 additions & 0 deletions src/dom/epub/epub-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,22 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
}
}

getKOReaderAnnotationStats(metadata: BufferSource): { count: number, lastModified?: Date } {
try {
let annotations = parseAnnotationsFromKOReaderMetadata(metadata);
if (annotations.length) {
return {
count: annotations.length,
lastModified: annotations.map(a => new Date(a.datetime)).reduce(
(max, cur) => (cur > max ? cur : max)
),
};
}
}
catch (e) {}
return { count: 0 };
}

importAnnotationsFromKOReaderMetadata(metadata: BufferSource) {
for (let koReaderAnnotation of parseAnnotationsFromKOReaderMetadata(metadata)) {
let range = koReaderAnnotationToRange(koReaderAnnotation, this._sectionRenderers);
Expand All @@ -514,6 +530,22 @@ class EPUBView extends DOMView<EPUBViewState, EPUBViewData> {
}
}

getCalibreAnnotationStats(metadata: string): { count: number, lastModified?: Date } {
try {
let annotations = parseAnnotationsFromCalibreMetadata(metadata);
if (annotations.length) {
return {
count: annotations.length,
lastModified: annotations.map(a => new Date(a.timestamp)).reduce(
(max, cur) => (cur > max ? cur : max)
),
};
}
}
catch (e) {}
return { count: 0 };
}

importAnnotationsFromCalibreMetadata(metadata: string) {
for (let calibreAnnotation of parseAnnotationsFromCalibreMetadata(metadata)) {
let range = calibreAnnotationToRange(calibreAnnotation, this._sectionRenderers);
Expand Down
1 change: 1 addition & 0 deletions src/dom/epub/lib/calibre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ export type CalibreAnnotation = {
kind: 'decoration';
which: 'wavy' | 'strikeout' | string;
};
timestamp: string;
};
5 changes: 4 additions & 1 deletion src/dom/epub/lib/koreader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ export function parseAnnotationsFromKOReaderMetadata(metadata: BufferSource): KO
pos0: findField(annotationTable, 'pos0'),
pos1: findField(annotationTable, 'pos1'),
text: findField(annotationTable, 'text'),
datetime: findField(annotationTable, 'datetime'),
};
for (let [key, value] of Object.entries(annotationFields)) {
if (['pos0', 'pos1', 'text'].includes(key) && !value) {
if (['pos0', 'pos1', 'text', 'datetime'].includes(key) && !value) {
throw new Error(`Invalid KOReader metadata: annotation is missing required field "${key}"`);
}
if (value && value.type !== 'StringLiteral') {
Expand All @@ -125,6 +126,7 @@ export function parseAnnotationsFromKOReaderMetadata(metadata: BufferSource): KO
pos0: parseKOReaderPosition((annotationFields.pos0 as StringLiteral).value),
pos1: parseKOReaderPosition((annotationFields.pos1 as StringLiteral).value),
text: (annotationFields.text as StringLiteral).value,
datetime: (annotationFields.datetime as StringLiteral).value,
});
}
return annotations;
Expand All @@ -136,6 +138,7 @@ export type KOReaderAnnotation = {
pos0: KOReaderPosition;
pos1: KOReaderPosition;
text: string;
datetime: string;
};

export type KOReaderPosition = {
Expand Down

0 comments on commit 88f2b5b

Please sign in to comment.