Skip to content

Commit

Permalink
Add protected option to text selection plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Jan 31, 2023
1 parent 6df6ec5 commit 9389505
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/plugins/plugin.text_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const DEFAULT_OPTIONS = {
singlePageDjvuXmlUrl: null,
/** Whether to fetch the XML as a jsonp */
jsonp: false,
/** Whether the book is a protected book */
protected: false,
};
/** @typedef {typeof DEFAULT_OPTIONS} TextSelectionPluginOptions */

Expand Down Expand Up @@ -71,6 +73,21 @@ export class TextSelectionPlugin {
}

init() {
if (this.options.protected) {
// Prevent right clicking when selected text
$(document.body).on('contextmenu dragstart copy', (e) => {
const selection = document.getSelection();

Check warning on line 79 in src/plugins/plugin.text_selection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/plugin.text_selection.js#L78-L79

Added lines #L78 - L79 were not covered by tests
if (selection && selection.toString()) {
const intersectsTextLayer = $('.textSelectionSVG')

Check warning on line 81 in src/plugins/plugin.text_selection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/plugin.text_selection.js#L81

Added line #L81 was not covered by tests
.toArray()
.some(el => selection.containsNode(el, true));

Check warning on line 83 in src/plugins/plugin.text_selection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/plugin.text_selection.js#L83

Added line #L83 was not covered by tests
if (intersectsTextLayer) {
e.preventDefault();
return false;

Check warning on line 86 in src/plugins/plugin.text_selection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/plugin.text_selection.js#L85-L86

Added lines #L85 - L86 were not covered by tests
}
}
});
}
// Only fetch the full djvu xml if the single page url isn't there
if (this.options.singlePageDjvuXmlUrl) return;
this.djvuPagesPromise = $.ajax({
Expand Down Expand Up @@ -126,6 +143,10 @@ export class TextSelectionPlugin {
*/
interceptCopy($container) {
$container[0].addEventListener('copy', (event) => {
if (this.options.protected) {
event.preventDefault();
return false;

Check warning on line 148 in src/plugins/plugin.text_selection.js

View check run for this annotation

Codecov / codecov/patch

src/plugins/plugin.text_selection.js#L147-L148

Added lines #L147 - L148 were not covered by tests
}
const selection = document.getSelection();
event.clipboardData.setData('text/plain', selection.toString());
event.preventDefault();
Expand Down Expand Up @@ -266,7 +287,8 @@ export class TextSelectionPlugin {

export class BookreaderWithTextSelection extends BookReader {
init() {
const options = Object.assign({}, DEFAULT_OPTIONS, this.options.plugins.textSelection);
const inheritedOptions = 'protected' in this.options ? { protected: this.options.protected } : {};
const options = Object.assign({}, DEFAULT_OPTIONS, inheritedOptions, this.options.plugins.textSelection);
if (options.enabled) {
this.textSelectionPlugin = new TextSelectionPlugin(options, this.options.vars);
// Write this back; this way the plugin is the source of truth, and BR just
Expand Down

0 comments on commit 9389505

Please sign in to comment.