ContentEditableSelectionManager.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2015, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../../../lib/@waca/core-client/js/core-client/ui/core/Class', './NormalizedPosition', './NormalizedSelection', 'jquery', 'underscore'], function (Class, NormalizedPosition, NormalizedSelection, $, _) {
  8. /**
  9. * Represents a selection from the document model, but with normalized positions within its ranges
  10. */
  11. var ContentEditableSelectionManager = Class.extend({
  12. /**
  13. * @param {node} contentEditableNode - the node with contenteditable=true
  14. * @param {string} editableNodesSelector - a jQuery selector which will identify valid nodes under 'contentEditableNode' that the user can insert
  15. * text to, and which the first one should never be deleted
  16. */
  17. init: function init(contentEditableNode, editableNodesSelector) {
  18. this.node = $(contentEditableNode);
  19. this.editableNodesSelector = editableNodesSelector;
  20. },
  21. /*
  22. * Finds all nodes under this.node which can receive text
  23. */
  24. _getEditableNodes: function _getEditableNodes() {
  25. return this.node.find(this.editableNodesSelector);
  26. },
  27. /*
  28. * @param {node} node
  29. * @return true iff 'node' can receive text from the user
  30. */
  31. _isInEditableNode: function _isInEditableNode(node) {
  32. return !!this._getEditableNodes().filter(function (i, n) {
  33. return n === node || $.contains(n, node);
  34. }).length;
  35. },
  36. filterKeypress: function filterKeypress(event) {
  37. var mayNeedToBlock = false,
  38. shouldBeCursor = false,
  39. block = false;
  40. if (event.which === 8 /*backspace*/) {
  41. mayNeedToBlock = true;
  42. shouldBeCursor = true;
  43. } else if (event.ctrlKey || event.metaKey) {
  44. /* ctrl-s, ctrl-z, ctrl-y*/
  45. block = event.which === 83 || event.which === 90 || event.which === 89;
  46. }
  47. if (mayNeedToBlock) {
  48. var selection = new NormalizedSelection(document.getSelection());
  49. var startPosition;
  50. if ($(this.node[0]).find('.note-editable')) {
  51. startPosition = new NormalizedPosition($(this.node[0]).find('.note-editable').get(0), 0);
  52. } else {
  53. startPosition = new NormalizedPosition(this.node[0], 0);
  54. }
  55. block = _.find(selection.ranges, function (range) {
  56. return range.isCursor() === shouldBeCursor && range.start.equals(startPosition);
  57. });
  58. }
  59. if (block) {
  60. event.preventDefault();
  61. }
  62. }
  63. });
  64. return ContentEditableSelectionManager;
  65. });
  66. //# sourceMappingURL=ContentEditableSelectionManager.js.map