Skip to content

Commit f4d9a22

Browse files
committed
History mode changes for text fragment
1 parent a76c24d commit f4d9a22

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/BookReader.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ BookReader.prototype.queryStringFromParams = function(
19581958
urlMode = 'hash',
19591959
) {
19601960
const newParams = new URLSearchParams(currQueryString);
1961-
1961+
let textFragmentParam = '';
19621962
if (params.view) {
19631963
// Set ?view=theater when fullscreen
19641964
newParams.set('view', params.view);
@@ -1970,9 +1970,13 @@ BookReader.prototype.queryStringFromParams = function(
19701970
if (params.search && urlMode === 'history') {
19711971
newParams.set('q', params.search);
19721972
}
1973+
if (newParams.get('text')) {
1974+
newParams.delete('text');
1975+
textFragmentParam = `text=${currQueryString.match(/(?<=&?text=)[^&]*/)}`;
1976+
}
19731977
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString
19741978
// Note: This method returns the query string without the question mark.
1975-
const result = newParams.toString();
1979+
const result = textFragmentParam ? newParams.toString() + textFragmentParam : newParams.toString();
19761980
return result ? '?' + result : '';
19771981
};
19781982

src/plugins/url/UrlPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,6 @@ export class UrlPlugin {
201201
}
202202

203203
retrieveTextFragment (urlString) {
204-
return urlString.match(/(?<=&text=)[^&]*/);
204+
return urlString.match(/(?<=&?text=)[^&]*/);
205205
}
206206
}

src/plugins/url/plugin.url.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ BookReader.prototype.urlUpdateFragment = function() {
144144
const currFragment = this.urlReadFragment();
145145
const currQueryString = this.getLocationSearch();
146146
const newQueryString = this.queryStringFromParams(params, currQueryString, this.options.urlMode);
147-
if (currFragment === newFragment && currQueryString === newQueryString) {
147+
const hasTextParam = currQueryString.includes("text");
148+
if (currFragment === newFragment && currQueryString === newQueryString && !hasTextParam) {
148149
return;
149150
}
150151

@@ -154,11 +155,21 @@ BookReader.prototype.urlUpdateFragment = function() {
154155
} else {
155156
const baseWithoutSlash = this.options.urlHistoryBasePath.replace(/\/+$/, '');
156157
const newFragmentWithSlash = newFragment === '' ? '' : `/${newFragment}`;
157-
158+
let textFragment = ""
159+
if (this.urlPlugin.retrieveTextFragment(newQueryString)) {
160+
textFragment = this.urlParamsFiltersOnlySearch(this.readQueryString());
161+
// newQueryString = newQueryString.replace(this.urlPlugin.retrieveTextFragment(newQueryString)[0], "").replace(/(\?text=)/, "")
162+
}
163+
console.log("this is newQueryString", newQueryString);
158164
const newUrlPath = `${baseWithoutSlash}${newFragmentWithSlash}${newQueryString}`;
165+
console.log("this is newURLPath", newUrlPath);
159166
try {
160167
window.history.replaceState({}, null, newUrlPath);
161168
this.oldLocationHash = newFragment + newQueryString;
169+
if (textFragment) {
170+
window.location.replace('#' + textFragment);
171+
this.oldLocationHash = textFragment;
172+
}
162173
} catch (e) {
163174
// DOMException on Chrome when in sandboxed iframe
164175
this.options.urlMode = 'hash';
@@ -199,6 +210,7 @@ BookReader.prototype.urlParamsFiltersOnlySearch = function(url) {
199210
BookReader.prototype.urlReadFragment = function() {
200211
const { urlMode, urlHistoryBasePath } = this.options;
201212
if (urlMode === 'history') {
213+
console.log("within plugin.url", window.location);
202214
return window.location.pathname.substr(urlHistoryBasePath.length);
203215
} else {
204216
return this.urlPlugin.getHash();
@@ -219,6 +231,7 @@ export class BookreaderUrlPlugin extends BookReader {
219231
const location = this.getLocationSearch();
220232
if (location.includes("text=")) {
221233
this.on('textLayerRendered', (_, {pageIndex, container}) => {
234+
console.log("line 235 plugin.url.js window.location.replace", `#${this.oldLocationHash}`);
222235
window.location.replace(`#${this.oldLocationHash}`);
223236
});
224237
}

src/util/TextSelectionManager.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,18 @@ class brHighlightBar extends LitElement {
461461
if (currentParams.includes('text')) {
462462
currentParams = currentParams.replace(/(text=)[\w\W\d%]+/, createParam(currentSelection, textLayer));
463463
} else {
464-
currentParams = `${currentParams}&${createParam(currentSelection, textLayer)}`;
464+
if (this.br.options.urlMode === 'history') {
465+
currentParams = `?${createParam(currentSelection, textLayer)}`
466+
} else {
467+
currentParams = `${currentParams}&${createParam(currentSelection, textLayer)}`;
468+
}
469+
}
470+
console.log("this is after copying", currentParams, currentUrl.pathname);
471+
if (this.br.options.urlMode === 'history') {
472+
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}`)
473+
} else {
474+
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}${currentUrl?.hash}`);
465475
}
466-
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}${currentUrl?.hash}`);
467476
}
468477

469478
addHighlightButtonStyling() {

0 commit comments

Comments
 (0)