|
| 1 | +/** |
| 2 | + * Creates new selection for the editor. |
| 3 | + * @param {Editor} editor. |
| 4 | + * @constructor |
| 5 | + */ |
| 6 | +Selection = function(editor) { |
| 7 | + this.editor = editor; |
| 8 | + this.blinkInterval = 500; |
| 9 | + |
| 10 | + this.start = { |
| 11 | + line: 0, |
| 12 | + character: 0 |
| 13 | + }; |
| 14 | + |
| 15 | + this.end = { |
| 16 | + line: 0, |
| 17 | + character: 0 |
| 18 | + }; |
| 19 | + |
| 20 | + this.el = document.createElement('div'); |
| 21 | + this.el.style.position = 'absolute'; |
| 22 | + this.el.style.width = '1px'; |
| 23 | + this.el.style.height = this.editor.getFontMetrics().getHeight() + 'px'; |
| 24 | + this.el.style.backgroundColor = '#000'; |
| 25 | + |
| 26 | + this.editor.getEl().appendChild(this.el); |
| 27 | + this.setPosition(0, 0); |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Responsible for blinking |
| 32 | + * @return {void} |
| 33 | + */ |
| 34 | +Selection.prototype.blink = function() { |
| 35 | + if (parseInt(this.el.style.opacity, 10)) { |
| 36 | + this.el.style.opacity = 0; |
| 37 | + } else { |
| 38 | + this.el.style.opacity = 1; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Moves both start and end to a specified position inside document. |
| 44 | + * @param {number?} line |
| 45 | + * @param {number?} character |
| 46 | + */ |
| 47 | +Selection.prototype.setPosition = function(line, character) { |
| 48 | + // Providing defaults for both line and character parts of position |
| 49 | + if (typeof line === 'undefined') line = this.end.line |
| 50 | + if (typeof character === 'undefined') character = this.end.character |
| 51 | + |
| 52 | + // Checking lower bounds |
| 53 | + line >= 0 || (line = 0); |
| 54 | + character >= 0 || (character = 0); |
| 55 | + |
| 56 | + // Checking upper bounds |
| 57 | + var lineCount = this.editor.getDocument().getLineCount(); |
| 58 | + line < lineCount || (line = lineCount - 1); |
| 59 | + var characterCount = this.editor.getDocument().getLine(line).trim('\n').length; |
| 60 | + character <= characterCount || (character = characterCount); |
| 61 | + |
| 62 | + // Saving new value |
| 63 | + this.start.line = this.end.line = line; |
| 64 | + this.start.character = this.end.character = character; |
| 65 | + |
| 66 | + // Calculating new position on the screen |
| 67 | + var metrics = this.editor.getFontMetrics(), |
| 68 | + offsetX = character * metrics.getWidth(), |
| 69 | + offsetY = line * metrics.getHeight(); |
| 70 | + this.el.style.left = offsetX + 'px'; |
| 71 | + this.el.style.top = offsetY + 'px'; |
| 72 | + |
| 73 | + // This helps to see moving cursor when it is always in blink on |
| 74 | + // state on a new position. Try to move cursror in any editor and you |
| 75 | + // will see this in action. |
| 76 | + if(this.isVisible()) { |
| 77 | + this.el.style.opacity = 1; |
| 78 | + clearInterval(this.interval); |
| 79 | + this.interval = setInterval(this.blink.bind(this), this.blinkInterval); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Moves up specified amount of lines. |
| 85 | + * @param {number} length |
| 86 | + */ |
| 87 | +Selection.prototype.moveUp = function(length) { |
| 88 | + arguments.length || (length = 1); |
| 89 | + this.setPosition(this.end.line - length); |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Moves down specified amount of lines. |
| 94 | + * @param {number} length |
| 95 | + */ |
| 96 | +Selection.prototype.moveDown = function(length) { |
| 97 | + arguments.length || (length = 1); |
| 98 | + this.setPosition(this.end.line + length); |
| 99 | +}; |
| 100 | + |
| 101 | +/** |
| 102 | + * Moves up specified amount of lines. |
| 103 | + * @param {number} length |
| 104 | + */ |
| 105 | +Selection.prototype.moveLeft = function(length) { |
| 106 | + arguments.length || (length = 1); |
| 107 | + this.setPosition(undefined, this.end.character - length); |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | + * Moves down specified amount of lines. |
| 112 | + * @param {number} length |
| 113 | + */ |
| 114 | +Selection.prototype.moveRight = function(length) { |
| 115 | + arguments.length || (length = 1); |
| 116 | + this.setPosition(undefined, this.end.character + length); |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * Shows or hides cursor. |
| 121 | + * @param {void} visible Whether cursor should be visible |
| 122 | + */ |
| 123 | +Selection.prototype.setVisible = function(visible) { |
| 124 | + clearInterval(this.interval); |
| 125 | + if(visible) { |
| 126 | + this.el.style.display = 'block'; |
| 127 | + this.el.style.opacity = 1; |
| 128 | + this.interval = setInterval(this.blink.bind(this), this.blinkInterval); |
| 129 | + } else { |
| 130 | + this.el.style.display = 'none'; |
| 131 | + } |
| 132 | + this.visible = visible; |
| 133 | +}; |
| 134 | + |
| 135 | +/** |
| 136 | + * Returns visibility of the cursor. |
| 137 | + * @return {Boolean} |
| 138 | + */ |
| 139 | +Selection.prototype.isVisible = function() { |
| 140 | + return this.visible; |
| 141 | +}; |
| 142 | + |
| 143 | +module.exports = Selection; |
0 commit comments