Skip to content

Commit d13eff6

Browse files
committed
Store textFragment and page, fix trigger conditions
Include fix for breaking tests
1 parent 8eb648a commit d13eff6

4 files changed

Lines changed: 46 additions & 9 deletions

File tree

src/BookReader/Mode2UpLit.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,7 @@ export class Mode2UpLit extends LitElement {
628628
progression == 'lr' ? [nextSpread.left, nextSpread.right] : [nextSpread.right, nextSpread.left]
629629
).filter(x => x);
630630
nextPageContainers.forEach(c => {
631-
this.br.trigger('pageVisible', {
632-
pageContainerEl: c.$container[0],
633-
});
631+
this.br.trigger('pageVisible', {pageContainerEl: c.$container[0]});
634632
});
635633
}
636634

src/plugins/plugin.text_selection.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ export class TextSelectionPlugin extends BookReaderPlugin {
5353
init() {
5454
if (!this.options.enabled) return;
5555

56+
this.br.on('pageVisible', (_, {pageContainerEl}) => {
57+
if (pageContainerEl.querySelector('.BRtextLayer')) {
58+
this.br.trigger('textLayerVisible', {pageContainerEl});
59+
}
60+
});
61+
5662
this.loadData();
5763
this.textSelectionManager.init();
5864
}
@@ -202,6 +208,11 @@ export class TextSelectionPlugin extends BookReaderPlugin {
202208
pageIndex,
203209
pageContainer,
204210
});
211+
212+
// Check if page is visible
213+
if ($container.hasClass('BRpage-visible')) {
214+
this.br.trigger('textLayerVisible', {pageContainerEl: $container[0]});
215+
}
205216
}
206217

207218
/**

src/plugins/url/plugin.url.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ BookReader.prototype.setup = (function(super_) {
4242
this.locationPollId = null;
4343
this.oldLocationHash = null;
4444
this.oldUserHash = null;
45+
// Should include the :~:text= prefix
46+
this.textFragment = null;
47+
// Tracks the original textFragment page num when first loaded
48+
this.textFragmentPage = null;
4549
};
4650
})(BookReader.prototype.setup);
4751

@@ -167,6 +171,14 @@ BookReader.prototype.urlUpdateFragment = function() {
167171
const newFragmentWithSlash = newFragment === '' ? '' : `/${newFragment}`;
168172
const textFragment = this.urlPlugin.retrieveTextFragment(newQueryString);
169173
const newUrlPath = `${baseWithoutSlash}${newFragmentWithSlash}${newQueryString}`;
174+
let extractedPage = newFragmentWithSlash.match(/(?<=\/)n?\d+(?=\/)/);
175+
if (extractedPage) {
176+
extractedPage = extractedPage[0];
177+
}
178+
if (!this.textFragmentPage && textFragment) {
179+
this.textFragmentPage = extractedPage ? extractedPage : null;
180+
this.textFragment = `:~:text=${textFragment}`;
181+
}
170182
try {
171183
window.history.replaceState({}, null, newUrlPath);
172184
this.oldLocationHash = newFragment + newQueryString;
@@ -183,8 +195,20 @@ BookReader.prototype.urlUpdateFragment = function() {
183195
if (this.options.urlMode === 'hash') {
184196
const newQueryStringSearch = this.urlParamsFiltersOnlySearch(this.readQueryString());
185197
let textFragment = this.urlPlugin.retrieveTextFragment(this.readQueryString());
198+
let extractedPage = newFragment.match(/(?<=\/)n?\d+(?=\/)/);
199+
if (extractedPage) {
200+
extractedPage = extractedPage[0];
201+
}
186202
if (textFragment) {
187203
textFragment = `:~:text=${textFragment[0]}`;
204+
} else {
205+
textFragment = '';
206+
}
207+
if (!this.textFragmentPage && textFragment) {
208+
this.textFragmentPage = extractedPage ? extractedPage : null;
209+
this.textFragment = textFragment;
210+
} else if (this.textFragmentPage && extractedPage != this.textFragmentPage) {
211+
textFragment = '';
188212
}
189213
window.location.replace('#' + newFragment + newQueryStringSearch + textFragment);
190214
this.oldLocationHash = newFragment + newQueryStringSearch + textFragment;
@@ -234,15 +258,17 @@ export class BookreaderUrlPlugin extends BookReader {
234258
this.urlPlugin = new UrlPlugin(this.options);
235259
const location = this.getLocationSearch();
236260
if (location.includes("text=")) {
237-
this.on('textLayerRendered', (_, {pageIndex, container}) => {
261+
this.on('textLayerVisible', async (_, {pageContainerEl}) => {
262+
const visiblePageNum = pageContainerEl.getAttribute('data-page-num');
263+
await new Promise(resolve => setTimeout(resolve, 100));
238264
if (this.options.urlMode === 'history') {
239-
// only want the text fragment forwarded
240-
if (this.oldLocationHash.includes(':~:')) {
241-
const newHash = this.oldLocationHash.split(':~:')[1];
242-
window.location.replace(`#:~:${newHash}`);
265+
if (this.textFragment && this.textFragmentPage == visiblePageNum) {
266+
window.location.replace(`#${this.textFragment}`);
243267
}
244268
} else {
245-
window.location.replace(`#${this.oldLocationHash}`);
269+
if (this.textFragment && this.textFragmentPage == visiblePageNum) {
270+
window.location.replace(`#${this.oldLocationHash}`);
271+
}
246272
}
247273
});
248274
}

tests/jest/BookReader.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ test('gets index from fragment when both fragment and cookie when InitParams cal
7676
});
7777

7878
test('sets prevReadMode when init called', () => {
79+
BookReader.prototype.urlUpdateFragment = jest.fn();
7980
BookReader.prototype.urlParamsFiltersOnlySearch = jest.fn();
8081
br.init();
8182
expect(br.prevReadMode).toBeTruthy();
@@ -85,6 +86,7 @@ test('sets prevReadMode when init called', () => {
8586
// BookReader.prototype.drawLeafsThumbnail
8687
// BookReader.prototype.getPrevReadMode
8788
test('sets prevPageMode if initial mode is thumb', () => {
89+
BookReader.prototype.urlUpdateFragment = jest.fn();
8890
br.urlReadFragment = jest.fn(() => 'page/n4/mode/thumb');
8991
BookReader.prototype.urlParamsFiltersOnlySearch = jest.fn();
9092
br.init();

0 commit comments

Comments
 (0)