Skip to content

Commit 203b2de

Browse files
committed
Use SelectionObserver and Lit Element, fix tests
1 parent 4f90aab commit 203b2de

8 files changed

Lines changed: 191 additions & 156 deletions

File tree

src/BookReader/utils/SelectionObserver.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ export class SelectionObserver {
44
startedInSelector = false;
55
/** @type {HTMLElement} */
66
target = null;
7+
lastKnownFocusNode = null;
78

89
/**
910
* @param {string} selector
10-
* @param {function('started' | 'cleared', HTMLElement): any} handler
11+
* @param {function('started' | 'cleared' | 'focusChanged', HTMLElement): any} handler
1112
*/
1213
constructor(selector, handler) {
1314
this.selector = selector;
@@ -34,9 +35,15 @@ export class SelectionObserver {
3435
if (!target) return;
3536
this.target = target;
3637
this.selecting = true;
38+
this.lastKnownFocusNode = sel.focusNode;
3739
this.handler('started', this.target);
3840
}
3941

42+
if (this.selecting && (this.lastKnownFocusNode != sel.focusNode || sel.toString())) {
43+
this.lastKnownFocusNode = sel.focusNode;
44+
this.handler('focusChanged', this.target);
45+
}
46+
4047
if (this.selecting && (sel.isCollapsed || !sel.toString() || !$(sel.anchorNode).closest(this.selector)[0])) {
4148
this.selecting = false;
4249
this.handler('cleared', this.target);

src/css/_TextSelection.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,15 @@
150150

151151
.BRtranslateLayer .BRparagraphElement.BRtranslateHidden {
152152
display: none;
153+
}
154+
155+
.textFragmentButton {
156+
background: inherit;
157+
background-position: center;
158+
background-repeat: no-repeat;
159+
background-color: darksalmon;
160+
width: 60px;
161+
height: 30px;
162+
top: inherit;
163+
left: inherit;
153164
}

src/plugins/url/UrlPlugin.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -155,45 +155,52 @@ export class UrlPlugin {
155155
}
156156
this.oldLocationHash = urlStrPath;
157157
}
158-
158+
/**
159+
* Get the hash out of the current URL. Also augments it with the text
160+
* from the main part of the URL, since that is not readable by JS
161+
* from the actual hash
162+
* @returns
163+
*/
159164
getHash = () => {
160-
const text = window.location.search.match(/(?<=[&?]text=)[^&]*/);
161-
if (text) {
162-
return `${window.location.hash.slice(1)}:~:text=${text[0]}`;
163-
}
164-
return window.location.hash.slice(1);
165+
const text = this.retrieveTextFragment(window.location.search);
166+
const textFragment = text ? `:~:text=${text[0]}` : '';
167+
return `${window.location.hash.slice(1)}${textFragment}`;
165168
}
166169

167-
/**
170+
/**
168171
* Get the url and check if it has changed
169172
* If it was changeed, update the urlState
170173
*/
171-
listenForHashChanges() {
172-
this.oldLocationHash = this.getHash();
173-
if (this.urlLocationPollId) {
174-
clearInterval(this.urlLocationPollId);
175-
this.urlLocationPollId = null;
176-
}
174+
listenForHashChanges() {
175+
this.oldLocationHash = this.getHash();
176+
if (this.urlLocationPollId) {
177+
clearInterval(this.urlLocationPollId);
178+
this.urlLocationPollId = null;
179+
}
177180

178-
// check if the URL changes
179-
const updateHash = () => {
180-
const newFragment = this.getHash();
181-
const hasFragmentChange = newFragment != this.oldLocationHash;
181+
// check if the URL changes
182+
const updateHash = () => {
183+
const newFragment = this.getHash();
184+
const hasFragmentChange = newFragment != this.oldLocationHash;
182185

183-
if (!hasFragmentChange) { return; }
186+
if (!hasFragmentChange) { return; }
184187

185-
this.urlState = this.urlStringToUrlState(newFragment);
186-
};
187-
this.urlLocationPollId = setInterval(updateHash, 500);
188-
}
188+
this.urlState = this.urlStringToUrlState(newFragment);
189+
};
190+
this.urlLocationPollId = setInterval(updateHash, 500);
191+
}
189192

190-
/**
193+
/**
191194
* Will read either the hash or URL and return the bookreader fragment
192195
*/
193-
pullFromAddressBar (location = window.location) {
194-
const path = this.urlMode === 'history'
195-
? (location.pathname.substr(this.urlHistoryBasePath.length) + location.search)
196-
: location.hash.substr(1);
197-
this.urlState = this.urlStringToUrlState(path);
198-
}
196+
pullFromAddressBar (location = window.location) {
197+
const path = this.urlMode === 'history'
198+
? (location.pathname.substr(this.urlHistoryBasePath.length) + location.search)
199+
: location.hash.substr(1);
200+
this.urlState = this.urlStringToUrlState(path);
201+
}
202+
203+
retrieveTextFragment (urlString) {
204+
return urlString.match(/(?<=&text=)[^&]*/);
205+
}
199206
}

src/plugins/url/plugin.url.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ jQuery.extend(BookReader.defaultOptions, {
2929

3030
/** If true, don't update the URL when `page == n0 (eg "/page/n0")` */
3131
urlTrackIndex0: false,
32-
shareHighlight: null,
3332
});
3433

3534
/** @override */
@@ -184,7 +183,7 @@ BookReader.prototype.urlUpdateFragment = function() {
184183

185184
// testing with this URL http://127.0.0.1:8000/BookReaderDemo/demo-internetarchive.html?ocaid=adventureofsherl0000unse&text=Well%2C I found my plans very seriously menaced.&q=breaking the law#page/18/mode/2up
186185
BookReader.prototype.urlParamsFiltersOnlySearch = function(url) {
187-
const text = url.match(/(?<=&text=)[^&]*/);
186+
const text = this.urlPlugin.retrieveTextFragment(url);
188187
const params = new URLSearchParams(url);
189188
let output = '';
190189
output += params.has('q') ? `?${new URLSearchParams({ q: params.get('q') })}` : '';
@@ -219,9 +218,6 @@ export class BookreaderUrlPlugin extends BookReader {
219218
this.urlPlugin = new UrlPlugin(this.options);
220219
const location = this.getLocationSearch();
221220
if (location.includes("text=")) {
222-
const extractText = location.match(/(text=[\w\d\W]*)/);
223-
const textFragment = `${extractText}`;
224-
this.options.shareHighlight = textFragment;
225221
this.on('textLayerRendered', (_, {pageIndex, container}) => {
226222
window.location.replace(`#${this.oldLocationHash}`);
227223
});

0 commit comments

Comments
 (0)