Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protected option to text selection plugin #1142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 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 @@ -82,6 +84,22 @@ export class TextSelectionPlugin {
init() {
this.selectionObserver.attach();

if (this.options.protected) {
// Prevent right clicking when selected text
$(document.body).on('contextmenu dragstart copy', (e) => {
const selection = document.getSelection();
if (selection && selection.toString()) {
const intersectsTextLayer = $('.textSelectionSVG')
.toArray()
.some(el => selection.containsNode(el, true));
if (intersectsTextLayer) {
e.preventDefault();
return false;
}
}
});
}

// 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 @@ -137,6 +155,10 @@ export class TextSelectionPlugin {
*/
interceptCopy($container) {
$container[0].addEventListener('copy', (event) => {
if (this.options.protected) {
event.preventDefault();
return false;
}
const selection = document.getSelection();
event.clipboardData.setData('text/plain', selection.toString());
event.preventDefault();
Expand Down Expand Up @@ -406,7 +428,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, this.pageProgression);
// Write this back; this way the plugin is the source of truth, and BR just
Expand Down