Skip to content

Commit 548778a

Browse files
committed
Add as experiment, fix mobile bug
1 parent f4d9a22 commit 548778a

5 files changed

Lines changed: 56 additions & 15 deletions

File tree

src/BookReader/utils/SelectionObserver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class SelectionObserver {
3939
this.handler('started', this.target);
4040
}
4141

42-
if (this.selecting && (this.lastKnownFocusNode != sel.focusNode || sel.toString())) {
42+
if (this.selecting && (this.lastKnownFocusNode != sel.focusNode || sel.toString() && !sel.isCollapsed)) {
4343
this.lastKnownFocusNode = sel.focusNode;
4444
this.handler('focusChanged', this.target);
4545
}

src/plugins/plugin.experiments.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,27 @@ export class ExperimentsPlugin extends BookReaderPlugin {
5151
localStorageKey: 'BrExperiments',
5252

5353
/** The experiments that should be shown in the experiments panel */
54-
enabledExperiments: ['translate'],
54+
enabledExperiments: ['translate', 'highlightUrl'],
5555
}
5656

5757
/** @type {ExperimentModel[]} */
5858
allExperiments = [
59+
new class extends ExperimentModel {
60+
name = 'highlightUrl';
61+
title = 'URL Link to Highlight';
62+
description = 'Share highlighted content via URL';
63+
learnMore = 'none';
64+
icon = 'images/translate.svg';
65+
enabled = false;
66+
async enable ({ manual = false }) {
67+
this.br.plugins.textSelection.enableExperiment();
68+
}
69+
async disable() {
70+
sleep(0).then(() => {
71+
window.location.reload();
72+
});
73+
}
74+
}(),
5975
new class extends ExperimentModel {
6076
name = 'translate';
6177
title = 'Translate Plugin';

src/plugins/plugin.text_selection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export class TextSelectionPlugin extends BookReaderPlugin {
5757
this.textSelectionManager.init();
5858
}
5959

60+
enableExperiment() {
61+
if (!this.textSelectionManager) return;
62+
this.textSelectionManager.highlightEnabled = true;
63+
this.textSelectionManager.addHighlightBar();
64+
}
65+
6066
/**
6167
* @override
6268
* @param {PageContainer} pageContainer

src/plugins/url/plugin.url.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ BookReader.prototype.urlUpdateFragment = function() {
155155
} else {
156156
const baseWithoutSlash = this.options.urlHistoryBasePath.replace(/\/+$/, '');
157157
const newFragmentWithSlash = newFragment === '' ? '' : `/${newFragment}`;
158-
let textFragment = ""
158+
let textFragment = "";
159159
if (this.urlPlugin.retrieveTextFragment(newQueryString)) {
160160
textFragment = this.urlParamsFiltersOnlySearch(this.readQueryString());
161161
// newQueryString = newQueryString.replace(this.urlPlugin.retrieveTextFragment(newQueryString)[0], "").replace(/(\?text=)/, "")
@@ -165,11 +165,11 @@ BookReader.prototype.urlUpdateFragment = function() {
165165
console.log("this is newURLPath", newUrlPath);
166166
try {
167167
window.history.replaceState({}, null, newUrlPath);
168-
this.oldLocationHash = newFragment + newQueryString;
169-
if (textFragment) {
170-
window.location.replace('#' + textFragment);
171-
this.oldLocationHash = textFragment;
172-
}
168+
this.oldLocationHash = newFragment + newQueryString + textFragment;
169+
// if (textFragment) {
170+
// window.location.replace('#' + textFragment);
171+
// this.oldLocationHash = textFragment;
172+
// }
173173
} catch (e) {
174174
// DOMException on Chrome when in sandboxed iframe
175175
this.options.urlMode = 'hash';
@@ -210,7 +210,6 @@ BookReader.prototype.urlParamsFiltersOnlySearch = function(url) {
210210
BookReader.prototype.urlReadFragment = function() {
211211
const { urlMode, urlHistoryBasePath } = this.options;
212212
if (urlMode === 'history') {
213-
console.log("within plugin.url", window.location);
214213
return window.location.pathname.substr(urlHistoryBasePath.length);
215214
} else {
216215
return this.urlPlugin.getHash();
@@ -231,7 +230,9 @@ export class BookreaderUrlPlugin extends BookReader {
231230
const location = this.getLocationSearch();
232231
if (location.includes("text=")) {
233232
this.on('textLayerRendered', (_, {pageIndex, container}) => {
234-
console.log("line 235 plugin.url.js window.location.replace", `#${this.oldLocationHash}`);
233+
window.location.replace(`#${this.oldLocationHash}`);
234+
});
235+
this.on('pageVisible', (_) => {
235236
window.location.replace(`#${this.oldLocationHash}`);
236237
});
237238
}

src/util/TextSelectionManager.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { customElement } from 'lit/decorators.js';
66
export class TextSelectionManager {
77
/** @type {brHighlightBar} */
88
highlightBar;
9+
highlightEnabled;
910
options = {
1011
// Current Translation plugin implementation does not have words, will limit to one BRlineElement for now
1112
maxProtectedWords: 200,
@@ -28,6 +29,7 @@ export class TextSelectionManager {
2829
this.selectionObserver = new SelectionObserver(this.layer, this._onSelectionChange);
2930
this.options.maxProtectedWords = maxWords ? maxWords : 200;
3031
this.highlightBar = new brHighlightBar(br);
32+
3133
this.highlightBar.className = "textFragmentBar";
3234
}
3335

@@ -42,23 +44,32 @@ export class TextSelectionManager {
4244
this.br.refs.$br.find('.BRpagecontainer--hasSelection').removeClass('BRpagecontainer--hasSelection');
4345
$(window.getSelection().anchorNode).closest('.BRpagecontainer').addClass('BRpagecontainer--hasSelection');
4446
this.highlightBar.addHighlightButtonStyling();
47+
4548
}
4649

4750
if (selectEvent == 'focusChanged') {
4851
// hide the button as user changes their selection
4952
if (this.mouseIsDown) {
5053
this.highlightBar.hideHighlightStyle();
54+
5155
} else {
52-
this.highlightBar.addHighlightButtonStyling();
56+
if (window.getSelection().toString().length != 0) {
57+
this.highlightBar.addHighlightButtonStyling();
58+
59+
}
5360
}
5461
}
62+
63+
if (selectEvent == 'cleared') {
64+
this.highlightBar.hideHighlightStyle();
65+
}
5566
}).attach();
5667
}
5768

5869
// Need attach + detach methods to toggle w/ Translation plugin
5970
attach() {
6071
this.selectionObserver.attach();
61-
if (!this.br.plugins.translate) {
72+
if (!this.br.plugins.translate && this.highlightEnabled) {
6273
document.body.append(this.highlightBar);
6374
}
6475
if (this.br.protected) {
@@ -86,6 +97,12 @@ export class TextSelectionManager {
8697
}
8798
}
8899

100+
addHighlightBar() {
101+
if (document.querySelector('.textFragmentBar')) return;
102+
if (this.highlightEnabled && !this.br.plugins.translate) {
103+
document.body.append(this.highlightBar);
104+
}
105+
}
89106
/**
90107
* @param {'started' | 'cleared' | 'focusChanged'} type
91108
* @param {HTMLElement} target
@@ -146,6 +163,7 @@ export class TextSelectionManager {
146163
// blocking selection
147164
$(textLayer).on("mousedown.textSelectPluginHandler", (event) => {
148165
this.mouseIsDown = true;
166+
this.highlightBar.hideHighlightStyle();
149167
if ($(event.target).is(this.selectionElement.join(", "))) {
150168
event.stopPropagation();
151169
}
@@ -179,6 +197,7 @@ export class TextSelectionManager {
179197
if (event.which != 1) return;
180198
this.mouseIsDown = true;
181199
event.stopPropagation();
200+
this.highlightBar.hideHighlightStyle();
182201
});
183202

184203
// Prevent page flip on click
@@ -462,14 +481,13 @@ class brHighlightBar extends LitElement {
462481
currentParams = currentParams.replace(/(text=)[\w\W\d%]+/, createParam(currentSelection, textLayer));
463482
} else {
464483
if (this.br.options.urlMode === 'history') {
465-
currentParams = `?${createParam(currentSelection, textLayer)}`
484+
currentParams = `?${createParam(currentSelection, textLayer)}`;
466485
} else {
467486
currentParams = `${currentParams}&${createParam(currentSelection, textLayer)}`;
468487
}
469488
}
470-
console.log("this is after copying", currentParams, currentUrl.pathname);
471489
if (this.br.options.urlMode === 'history') {
472-
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}`)
490+
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}`);
473491
} else {
474492
navigator.clipboard.writeText(`${currentUrl.origin}${currentUrl.pathname}${currentParams}${currentUrl?.hash}`);
475493
}

0 commit comments

Comments
 (0)