|
| 1 | +/* ***** BEGIN LICENSE BLOCK ***** |
| 2 | + * Distributed under the BSD license: |
| 3 | + * |
| 4 | + * Copyright (c) 2010, Ajax.org B.V. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Ajax.org B.V. nor the |
| 15 | + * names of its contributors may be used to endorse or promote products |
| 16 | + * derived from this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY |
| 22 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * ***** END LICENSE BLOCK ***** */ |
| 30 | + |
| 31 | +define(function(require, exports, module) { |
| 32 | +"use strict"; |
| 33 | + |
| 34 | +var oop = require("./lib/oop"); |
| 35 | +var EventEmitter = require("./lib/event_emitter").EventEmitter; |
| 36 | + |
| 37 | +/** |
| 38 | + * |
| 39 | + * Defines the floating pointer in the document. Whenever text is inserted or deleted before the cursor, the position of the cursor is updated. |
| 40 | + * |
| 41 | + * @class Anchor |
| 42 | + **/ |
| 43 | + |
| 44 | +/** |
| 45 | + * Creates a new `Anchor` and associates it with a document. |
| 46 | + * |
| 47 | + * @param {Document} doc The document to associate with the anchor |
| 48 | + * @param {Number} row The starting row position |
| 49 | + * @param {Number} column The starting column position |
| 50 | + * |
| 51 | + * @constructor |
| 52 | + **/ |
| 53 | + |
| 54 | +var Anchor = exports.Anchor = function(doc, row, column) { |
| 55 | + this.$onChange = this.onChange.bind(this); |
| 56 | + this.attach(doc); |
| 57 | + |
| 58 | + if (typeof column == "undefined") |
| 59 | + this.setPosition(row.row, row.column); |
| 60 | + else |
| 61 | + this.setPosition(row, column); |
| 62 | +}; |
| 63 | + |
| 64 | +(function() { |
| 65 | + |
| 66 | + oop.implement(this, EventEmitter); |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns an object identifying the `row` and `column` position of the current anchor. |
| 70 | + * @returns {Object} |
| 71 | + **/ |
| 72 | + this.getPosition = function() { |
| 73 | + return this.$clipPositionToDocument(this.row, this.column); |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * |
| 78 | + * Returns the current document. |
| 79 | + * @returns {Document} |
| 80 | + **/ |
| 81 | + this.getDocument = function() { |
| 82 | + return this.document; |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * experimental: allows anchor to stick to the next on the left |
| 87 | + */ |
| 88 | + this.$insertRight = false; |
| 89 | + /** |
| 90 | + * Fires whenever the anchor position changes. |
| 91 | + * |
| 92 | + * Both of these objects have a `row` and `column` property corresponding to the position. |
| 93 | + * |
| 94 | + * Events that can trigger this function include [[Anchor.setPosition `setPosition()`]]. |
| 95 | + * |
| 96 | + * @event change |
| 97 | + * @param {Object} e An object containing information about the anchor position. It has two properties: |
| 98 | + * - `old`: An object describing the old Anchor position |
| 99 | + * - `value`: An object describing the new Anchor position |
| 100 | + * |
| 101 | + **/ |
| 102 | + this.onChange = function(e) { |
| 103 | + var delta = e.data; |
| 104 | + var range = delta.range; |
| 105 | + |
| 106 | + if (range.start.row == range.end.row && range.start.row != this.row) |
| 107 | + return; |
| 108 | + |
| 109 | + if (range.start.row > this.row) |
| 110 | + return; |
| 111 | + |
| 112 | + if (range.start.row == this.row && range.start.column > this.column) |
| 113 | + return; |
| 114 | + |
| 115 | + var row = this.row; |
| 116 | + var column = this.column; |
| 117 | + var start = range.start; |
| 118 | + var end = range.end; |
| 119 | + |
| 120 | + if (delta.action === "insertText") { |
| 121 | + if (start.row === row && start.column <= column) { |
| 122 | + if (start.column === column && this.$insertRight) { |
| 123 | + // do nothing |
| 124 | + } else if (start.row === end.row) { |
| 125 | + column += end.column - start.column; |
| 126 | + } else { |
| 127 | + column -= start.column; |
| 128 | + row += end.row - start.row; |
| 129 | + } |
| 130 | + } else if (start.row !== end.row && start.row < row) { |
| 131 | + row += end.row - start.row; |
| 132 | + } |
| 133 | + } else if (delta.action === "insertLines") { |
| 134 | + if (start.row <= row) { |
| 135 | + row += end.row - start.row; |
| 136 | + } |
| 137 | + } else if (delta.action === "removeText") { |
| 138 | + if (start.row === row && start.column < column) { |
| 139 | + if (end.column >= column) |
| 140 | + column = start.column; |
| 141 | + else |
| 142 | + column = Math.max(0, column - (end.column - start.column)); |
| 143 | + |
| 144 | + } else if (start.row !== end.row && start.row < row) { |
| 145 | + if (end.row === row) |
| 146 | + column = Math.max(0, column - end.column) + start.column; |
| 147 | + row -= (end.row - start.row); |
| 148 | + } else if (end.row === row) { |
| 149 | + row -= end.row - start.row; |
| 150 | + column = Math.max(0, column - end.column) + start.column; |
| 151 | + } |
| 152 | + } else if (delta.action == "removeLines") { |
| 153 | + if (start.row <= row) { |
| 154 | + if (end.row <= row) |
| 155 | + row -= end.row - start.row; |
| 156 | + else { |
| 157 | + row = start.row; |
| 158 | + column = 0; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + this.setPosition(row, column, true); |
| 164 | + }; |
| 165 | + |
| 166 | + /** |
| 167 | + * Sets the anchor position to the specified row and column. If `noClip` is `true`, the position is not clipped. |
| 168 | + * @param {Number} row The row index to move the anchor to |
| 169 | + * @param {Number} column The column index to move the anchor to |
| 170 | + * @param {Boolean} noClip Identifies if you want the position to be clipped |
| 171 | + * |
| 172 | + **/ |
| 173 | + this.setPosition = function(row, column, noClip) { |
| 174 | + var pos; |
| 175 | + if (noClip) { |
| 176 | + pos = { |
| 177 | + row: row, |
| 178 | + column: column |
| 179 | + }; |
| 180 | + } else { |
| 181 | + pos = this.$clipPositionToDocument(row, column); |
| 182 | + } |
| 183 | + |
| 184 | + if (this.row == pos.row && this.column == pos.column) |
| 185 | + return; |
| 186 | + |
| 187 | + var old = { |
| 188 | + row: this.row, |
| 189 | + column: this.column |
| 190 | + }; |
| 191 | + |
| 192 | + this.row = pos.row; |
| 193 | + this.column = pos.column; |
| 194 | + this._emit("change", { |
| 195 | + old: old, |
| 196 | + value: pos |
| 197 | + }); |
| 198 | + }; |
| 199 | + |
| 200 | + /** |
| 201 | + * When called, the `'change'` event listener is removed. |
| 202 | + * |
| 203 | + **/ |
| 204 | + this.detach = function() { |
| 205 | + this.document.removeEventListener("change", this.$onChange); |
| 206 | + }; |
| 207 | + this.attach = function(doc) { |
| 208 | + this.document = doc || this.document; |
| 209 | + this.document.on("change", this.$onChange); |
| 210 | + }; |
| 211 | + |
| 212 | + /** |
| 213 | + * Clips the anchor position to the specified row and column. |
| 214 | + * @param {Number} row The row index to clip the anchor to |
| 215 | + * @param {Number} column The column index to clip the anchor to |
| 216 | + * |
| 217 | + **/ |
| 218 | + this.$clipPositionToDocument = function(row, column) { |
| 219 | + var pos = {}; |
| 220 | + |
| 221 | + if (row >= this.document.getLength()) { |
| 222 | + pos.row = Math.max(0, this.document.getLength() - 1); |
| 223 | + pos.column = this.document.getLine(pos.row).length; |
| 224 | + } |
| 225 | + else if (row < 0) { |
| 226 | + pos.row = 0; |
| 227 | + pos.column = 0; |
| 228 | + } |
| 229 | + else { |
| 230 | + pos.row = row; |
| 231 | + pos.column = Math.min(this.document.getLine(pos.row).length, Math.max(0, column)); |
| 232 | + } |
| 233 | + |
| 234 | + if (column < 0) |
| 235 | + pos.column = 0; |
| 236 | + |
| 237 | + return pos; |
| 238 | + }; |
| 239 | + |
| 240 | +}).call(Anchor.prototype); |
| 241 | + |
| 242 | +}); |
0 commit comments