Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate TtsPlugin to BookReaderPlugin #1376

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ codecov:
project:
default:
# Allow small drops in coverage
threshold: 0.005 # .5 %
threshold: 0.5 # 0.5%
if_not_found: failure

coverage:
Expand Down
10 changes: 9 additions & 1 deletion src/BookReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ BookReader.PLUGINS = {
resume: null,
/** @type {typeof import('./plugins/plugin.text_selection.js').TextSelectionPlugin | null}*/
textSelection: null,
/** @type {typeof import('./plugins/tts/plugin.tts.js').TtsPlugin | null}*/
tts: null,
};

/**
Expand Down Expand Up @@ -264,6 +266,7 @@ BookReader.prototype.setup = function(options) {
autoplay: BookReader.PLUGINS.autoplay ? new BookReader.PLUGINS.autoplay(this) : null,
resume: BookReader.PLUGINS.resume ? new BookReader.PLUGINS.resume(this) : null,
textSelection: BookReader.PLUGINS.textSelection ? new BookReader.PLUGINS.textSelection(this) : null,
tts: BookReader.PLUGINS.tts ? new BookReader.PLUGINS.tts(this) : null,
};

// Delete anything that's null
Expand Down Expand Up @@ -590,7 +593,12 @@ BookReader.prototype.init = function() {
this.initToolbar(this.mode, this.ui); // Build inside of toolbar div
}
if (this.options.showNavbar) { // default navigation
this.initNavbar();
const $navBar = this.initNavbar();

// extend navbar with plugins
for (const plugin of Object.values(this._plugins)) {
plugin.extendNavBar($navBar);
}
}

// Switch navbar controls on mobile/desktop
Expand Down
2 changes: 2 additions & 0 deletions src/BookReader/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export const DEFAULT_OPTIONS = {
resume: null,
/** @type {import('../plugins/plugin.text_selection.js').TextSelectionPlugin['options']} */
textSelection: null,
/** @type {import('../plugins/tts/plugin.tts.js').TtsPlugin['options']} */
tts: null,
},

/**
Expand Down
5 changes: 5 additions & 0 deletions src/BookReaderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ export class BookReaderPlugin {

/** @abstract @protected */
_bindNavigationHandlers() {}

/**
* @param {JQuery<HTMLElement>} $navBar
*/
extendNavBar($navBar) {}
}
6 changes: 2 additions & 4 deletions src/plugins/tts/AbstractTTSEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { hasLocalStorage } from './utils.js';
/**
* @export
* @typedef {Object} TTSEngineOptions
* @property {String} server
* @property {String} bookPath
* @property {import('@/src/util/strings.js').StringWithVars} pageChunkUrl
* @property {ISO6391} bookLanguage
* @property {Function} onLoadingStart
* @property {Function} onLoadingComplete
Expand Down Expand Up @@ -85,8 +84,7 @@ export default class AbstractTTSEngine {
this.opts.onLoadingStart();

this._chunkIterator = new PageChunkIterator(numLeafs, leafIndex, {
server: this.opts.server,
bookPath: this.opts.bookPath,
pageChunkUrl: this.opts.pageChunkUrl,
pageBufferSize: 5,
});

Expand Down
14 changes: 5 additions & 9 deletions src/plugins/tts/PageChunk.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { applyVariables } from "../../util/strings.js";

/**
* Class to manage a 'chunk' (approximately a paragraph) of text on a page.
*/
Expand All @@ -16,24 +18,18 @@
}

/**
* @param {string} server
* @param {string} bookPath
* @param {import('@/src/util/strings.js').StringWithVars} pageChunkUrl
* @param {number} leafIndex
* @return {Promise<PageChunk[]>}
*/
static async fetch(server, bookPath, leafIndex) {
static async fetch(pageChunkUrl, leafIndex) {

Check warning on line 25 in src/plugins/tts/PageChunk.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/tts/PageChunk.js#L25

Added line #L25 was not covered by tests
const chunks = await $.ajax({
type: 'GET',
url: `https://${server}/BookReader/BookReaderGetTextWrapper.php`,
url: applyVariables(pageChunkUrl, { pageIndex: leafIndex }),
cache: true,
xhrFields: {
withCredentials: window.br.protected,
},
data: {
path: `${bookPath}_djvu.xml`,
page: leafIndex,
callback: 'false',
},
});
return PageChunk._fromTextWrapperResponse(leafIndex, chunks);
}
Expand Down
8 changes: 3 additions & 5 deletions src/plugins/tts/PageChunkIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,20 @@
* @param {number} index
*/
_fetchPageChunksDirect(index) {
return PageChunk.fetch(this.opts.server, this.opts.bookPath, index);
return PageChunk.fetch(this.opts.pageChunkUrl, index);

Check warning on line 141 in src/plugins/tts/PageChunkIterator.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/tts/PageChunkIterator.js#L141

Added line #L141 was not covered by tests
}
}

PageChunkIterator.AT_END = "__PageChunkIterator.AT_END__";

/** @type {PageChunkIteratorOptions} */
const DEFAULT_OPTS = {
server: null,
bookPath: null,
pageChunkUrl: null,
pageBufferSize: 2,
};

/**
* @typedef {Object} PageChunkIteratorOptions
* @property {string} server
* @property {string} bookPath
* @property {import('@/src/util/strings.js').StringWithVars} pageChunkUrl
* @property {number} [pageBufferSize] number of pages to buffer before/after the current page
*/
Loading