@@ -4,41 +4,31 @@ var oop = require("./lib/oop");
44var EventEmitter = require ( "./lib/event_emitter" ) . EventEmitter ;
55
66/**
7- *
87 * Defines a floating pointer in the document. Whenever text is inserted or deleted before the cursor, the position of the anchor is updated.
9- *
10- * @class Anchor
118 **/
12-
13- /**
14- * Creates a new `Anchor` and associates it with a document.
15- *
16- * @param {Document } doc The document to associate with the anchor
17- * @param {Number } row The starting row position
18- * @param {Number } column The starting column position
19- *
20- * @constructor
21- **/
22-
23- var Anchor = exports . Anchor = function ( doc , row , column ) {
24- this . $onChange = this . onChange . bind ( this ) ;
25- this . attach ( doc ) ;
9+ class Anchor {
10+ /**
11+ * Creates a new `Anchor` and associates it with a document.
12+ *
13+ * @param {Document } doc The document to associate with the anchor
14+ * @param {Number } row The starting row position
15+ * @param {Number } column The starting column position
16+ **/
17+ constructor ( doc , row , column ) {
18+ this . $onChange = this . onChange . bind ( this ) ;
19+ this . attach ( doc ) ;
20+
21+ if ( typeof column == "undefined" )
22+ this . setPosition ( row . row , row . column ) ;
23+ else
24+ this . setPosition ( row , column ) ;
25+ } ;
2626
27- if ( typeof column == "undefined" )
28- this . setPosition ( row . row , row . column ) ;
29- else
30- this . setPosition ( row , column ) ;
31- } ;
32-
33- ( function ( ) {
34-
35- oop . implement ( this , EventEmitter ) ;
36-
3727 /**
3828 * Returns an object identifying the `row` and `column` position of the current anchor.
3929 * @returns {Ace.Point }
4030 **/
41- this . getPosition = function ( ) {
31+ getPosition ( ) {
4232 return this . $clipPositionToDocument ( this . row , this . column ) ;
4333 } ;
4434
@@ -47,14 +37,14 @@ var Anchor = exports.Anchor = function(doc, row, column) {
4737 * Returns the current document.
4838 * @returns {Document }
4939 **/
50- this . getDocument = function ( ) {
40+ getDocument ( ) {
5141 return this . document ;
5242 } ;
5343
5444 /**
5545 * experimental: allows anchor to stick to the next on the left
5646 */
57- this . $insertRight = false ;
47+ $insertRight = false ;
5848 /**
5949 * Fires whenever the anchor position changes.
6050 *
@@ -72,7 +62,7 @@ var Anchor = exports.Anchor = function(doc, row, column) {
7262 * Internal function called when `"change"` event fired.
7363 * @param {Ace.Delta } delta
7464 */
75- this . onChange = function ( delta ) {
65+ onChange ( delta ) {
7666 if ( delta . start . row == delta . end . row && delta . start . row != this . row )
7767 return ;
7868
@@ -82,44 +72,6 @@ var Anchor = exports.Anchor = function(doc, row, column) {
8272 var point = $getTransformedPoint ( delta , { row : this . row , column : this . column } , this . $insertRight ) ;
8373 this . setPosition ( point . row , point . column , true ) ;
8474 } ;
85-
86- function $pointsInOrder ( point1 , point2 , equalPointsInOrder ) {
87- var bColIsAfter = equalPointsInOrder ? point1 . column <= point2 . column : point1 . column < point2 . column ;
88- return ( point1 . row < point2 . row ) || ( point1 . row == point2 . row && bColIsAfter ) ;
89- }
90-
91- function $getTransformedPoint ( delta , point , moveIfEqual ) {
92- // Get delta info.
93- var deltaIsInsert = delta . action == "insert" ;
94- var deltaRowShift = ( deltaIsInsert ? 1 : - 1 ) * ( delta . end . row - delta . start . row ) ;
95- var deltaColShift = ( deltaIsInsert ? 1 : - 1 ) * ( delta . end . column - delta . start . column ) ;
96- var deltaStart = delta . start ;
97- var deltaEnd = deltaIsInsert ? deltaStart : delta . end ; // Collapse insert range.
98-
99- // DELTA AFTER POINT: No change needed.
100- if ( $pointsInOrder ( point , deltaStart , moveIfEqual ) ) {
101- return {
102- row : point . row ,
103- column : point . column
104- } ;
105- }
106-
107- // DELTA BEFORE POINT: Move point by delta shift.
108- if ( $pointsInOrder ( deltaEnd , point , ! moveIfEqual ) ) {
109- return {
110- row : point . row + deltaRowShift ,
111- column : point . column + ( point . row == deltaEnd . row ? deltaColShift : 0 )
112- } ;
113- }
114-
115- // DELTA ENVELOPS POINT (delete only): Move point to delta start.
116- // TODO warn if delta.action != "remove" ?
117-
118- return {
119- row : deltaStart . row ,
120- column : deltaStart . column
121- } ;
122- }
12375
12476 /**
12577 * Sets the anchor position to the specified row and column. If `noClip` is `true`, the position is not clipped.
@@ -128,7 +80,7 @@ var Anchor = exports.Anchor = function(doc, row, column) {
12880 * @param {Boolean } noClip Identifies if you want the position to be clipped
12981 *
13082 **/
131- this . setPosition = function ( row , column , noClip ) {
83+ setPosition ( row , column , noClip ) {
13284 var pos ;
13385 if ( noClip ) {
13486 pos = {
@@ -159,7 +111,7 @@ var Anchor = exports.Anchor = function(doc, row, column) {
159111 * When called, the `"change"` event listener is removed.
160112 *
161113 **/
162- this . detach = function ( ) {
114+ detach ( ) {
163115 this . document . off ( "change" , this . $onChange ) ;
164116 } ;
165117
@@ -168,7 +120,7 @@ var Anchor = exports.Anchor = function(doc, row, column) {
168120 * @param {Document } doc The document to associate with
169121 *
170122 **/
171- this . attach = function ( doc ) {
123+ attach ( doc ) {
172124 this . document = doc || this . document ;
173125 this . document . on ( "change" , this . $onChange ) ;
174126 } ;
@@ -180,7 +132,7 @@ var Anchor = exports.Anchor = function(doc, row, column) {
180132 * @returns {Ace.Point }
181133 *
182134 **/
183- this . $clipPositionToDocument = function ( row , column ) {
135+ $clipPositionToDocument ( row , column ) {
184136 var pos = { } ;
185137
186138 if ( row >= this . document . getLength ( ) ) {
@@ -201,5 +153,46 @@ var Anchor = exports.Anchor = function(doc, row, column) {
201153
202154 return pos ;
203155 } ;
156+ }
157+
158+ oop . implement ( Anchor . prototype , EventEmitter ) ;
159+
160+ function $pointsInOrder ( point1 , point2 , equalPointsInOrder ) {
161+ var bColIsAfter = equalPointsInOrder ? point1 . column <= point2 . column : point1 . column < point2 . column ;
162+ return ( point1 . row < point2 . row ) || ( point1 . row == point2 . row && bColIsAfter ) ;
163+ }
164+
165+ function $getTransformedPoint ( delta , point , moveIfEqual ) {
166+ // Get delta info.
167+ var deltaIsInsert = delta . action == "insert" ;
168+ var deltaRowShift = ( deltaIsInsert ? 1 : - 1 ) * ( delta . end . row - delta . start . row ) ;
169+ var deltaColShift = ( deltaIsInsert ? 1 : - 1 ) * ( delta . end . column - delta . start . column ) ;
170+ var deltaStart = delta . start ;
171+ var deltaEnd = deltaIsInsert ? deltaStart : delta . end ; // Collapse insert range.
172+
173+ // DELTA AFTER POINT: No change needed.
174+ if ( $pointsInOrder ( point , deltaStart , moveIfEqual ) ) {
175+ return {
176+ row : point . row ,
177+ column : point . column
178+ } ;
179+ }
180+
181+ // DELTA BEFORE POINT: Move point by delta shift.
182+ if ( $pointsInOrder ( deltaEnd , point , ! moveIfEqual ) ) {
183+ return {
184+ row : point . row + deltaRowShift ,
185+ column : point . column + ( point . row == deltaEnd . row ? deltaColShift : 0 )
186+ } ;
187+ }
188+
189+ // DELTA ENVELOPS POINT (delete only): Move point to delta start.
190+ // TODO warn if delta.action != "remove" ?
191+
192+ return {
193+ row : deltaStart . row ,
194+ column : deltaStart . column
195+ } ;
196+ }
204197
205- } ) . call ( Anchor . prototype ) ;
198+ exports . Anchor = Anchor ;
0 commit comments