Files
edx-platform/lms/static/js/edxnotes/plugins/caret_navigation.js
Syed Ali Abbas Zaidi 8480dbc228 chore: apply amnesty on existing not fixable issues (#32215)
* fix: eslint operator-linebreak issue

* fix: eslint quotes issue

* fix: react jsx indent and props issues

* fix: eslint trailing spaces issues

* fix: eslint line around directives issue

* fix: eslint semi rule

* fix: eslint newline per chain rule

* fix: eslint space infix ops rule

* fix: eslint space-in-parens issue

* fix: eslint space before function paren issue

* fix: eslint space before blocks issue

* fix: eslint arrow body style issue

* fix: eslint dot-location issue

* fix: eslint quotes issue

* fix: eslint quote props issue

* fix: eslint operator assignment issue

* fix: eslint new line after import issue

* fix: indent issues

* fix: operator assignment issue

* fix: all autofixable eslint issues

* fix: all react related fixable issues

* fix: autofixable eslint issues

* chore: remove all template literals

* fix: remaining autofixable issues

* chore: apply amnesty on all existing issues

* fix: failing xss-lint issues

* refactor: apply amnesty on remaining issues

* refactor: apply amnesty on new issues

* fix: remove file level suppressions

* refactor: apply amnesty on new issues
2023-08-07 19:13:19 +05:00

120 lines
4.7 KiB
JavaScript

// eslint-disable-next-line no-shadow-restricted-names
(function(define, undefined) {
'use strict';
define(['jquery', 'underscore', 'annotator_1.2.9'], function($, _, Annotator) {
/**
* The CaretNavigation Plugin which allows notes creation when users use
* caret navigation to select the text.
* Use `Ctrl + SPACE` or `Ctrl + ENTER` to open the editor.
* */
Annotator.Plugin.CaretNavigation = function() {
// Call the Annotator.Plugin constructor this sets up the element and
// options properties.
_.bindAll(this, 'onKeyUp');
Annotator.Plugin.apply(this, arguments);
};
$.extend(Annotator.Plugin.CaretNavigation.prototype, new Annotator.Plugin(), {
pluginInit: function() {
$(document).on('keyup', this.onKeyUp);
},
destroy: function() {
$(document).off('keyup', this.onKeyUp);
},
isShortcut: function(event) {
// Character ']' has keyCode 221
return event.keyCode === 221 && event.ctrlKey && event.shiftKey;
},
hasSelection: function(ranges) {
return (ranges || []).length;
},
saveSelection: function() {
this.savedRange = Annotator.Util.getGlobal().getSelection().getRangeAt(0);
},
restoreSelection: function() {
if (this.savedRange) {
var browserRange = new Annotator.Range.BrowserRange(this.savedRange),
normedRange = browserRange.normalize().limit(this.annotator.wrapper[0]);
Annotator.Util.readRangeViaSelection(normedRange);
this.savedRange = null;
}
},
onKeyUp: function(event) {
var annotator = this.annotator,
self = this,
isAnnotator, annotation, highlights, position, save, cancel, cleanup;
// Do nothing if not a shortcut.
if (!this.isShortcut(event)) {
return true;
}
// Get the currently selected ranges.
annotator.selectedRanges = annotator.getSelectedRanges();
// Do nothing if there is no selection
if (!this.hasSelection(annotator.selectedRanges)) {
return true;
}
isAnnotator = _.some(annotator.selectedRanges, function(range) {
return annotator.isAnnotator(range.commonAncestor);
});
// Do nothing if we are in Annotator.
if (isAnnotator) {
return true;
}
// Show a temporary highlight so the user can see what they selected
// Also extract the quotation and serialize the ranges
annotation = annotator.setupAnnotation(annotator.createAnnotation());
highlights = $(annotation.highlights).addClass('annotator-hl-temporary');
if (annotator.adder.is(':visible')) {
position = annotator.adder.position();
annotator.adder.hide();
} else {
position = highlights.last().position();
}
// Subscribe to the editor events
// Make the highlights permanent if the annotation is saved
save = function() {
cleanup();
highlights.removeClass('annotator-hl-temporary');
// Fire annotationCreated events so that plugins can react to them
annotator.publish('annotationCreated', [annotation]);
};
// Remove the highlights if the edit is cancelled
cancel = function() {
self.restoreSelection();
cleanup();
annotator.deleteAnnotation(annotation);
};
// Don't leak handlers at the end
cleanup = function() {
annotator.unsubscribe('annotationEditorHidden', cancel);
annotator.unsubscribe('annotationEditorSubmit', save);
self.savedRange = null;
};
annotator.subscribe('annotationEditorHidden', cancel);
annotator.subscribe('annotationEditorSubmit', save);
this.saveSelection();
// Display the editor.
annotator.showEditor(annotation, position);
event.preventDefault();
}
});
});
}).call(this, define || RequireJS.define);