@@ -41,13 +41,11 @@ define(function (require, exports, module) {
4141 /**
4242 * Inline widget containing a ColorEditor control
4343 * @param {!string } color Initially selected color
44- * @param {!CodeMirror.Bookmark } startBookmark
45- * @param {!CodeMirror.Bookmark } endBookmark
44+ * @param {!CodeMirror.TextMarker } marker
4645 */
47- function InlineColorEditor ( color , startBookmark , endBookmark ) {
46+ function InlineColorEditor ( color , marker ) {
4847 this . _color = color ;
49- this . _startBookmark = startBookmark ;
50- this . _endBookmark = endBookmark ;
48+ this . _marker = marker ;
5149 this . _isOwnChange = false ;
5250 this . _isHostChange = false ;
5351 this . _origin = "+InlineColorEditor_" + ( lastOriginId ++ ) ;
@@ -69,17 +67,10 @@ define(function (require, exports, module) {
6967 InlineColorEditor . prototype . _color = null ;
7068
7169 /**
72- * Start of the range of code we're attached to; _startBookmark .find() may by null if sync is lost.
73- * @type {!CodeMirror.Bookmark }
70+ * Range of code we're attached to; _marker .find() may by null if sync is lost.
71+ * @type {!CodeMirror.TextMarker }
7472 */
75- InlineColorEditor . prototype . _startBookmark = null ;
76-
77- /**
78- * End of the range of code we're attached to; _endBookmark.find() may by null if sync is lost or even
79- * in some cases when it's not. Call getCurrentRange() for the definitive text range we're attached to.
80- * @type {!CodeMirror.Bookmark }
81- */
82- InlineColorEditor . prototype . _endBookmark = null ;
73+ InlineColorEditor . prototype . _marker = null ;
8374
8475 /** @type {boolean } True while we're syncing a color picker change into the code editor */
8576 InlineColorEditor . prototype . _isOwnChange = null ;
@@ -97,25 +88,24 @@ define(function (require, exports, module) {
9788 * @return {?{start:{line:number, ch:number}, end:{line:number, ch:number}} }
9889 */
9990 InlineColorEditor . prototype . getCurrentRange = function ( ) {
100- var start , end ;
91+ var pos , start , end ;
10192
102- start = this . _startBookmark . find ( ) ;
93+ pos = this . _marker && this . _marker . find ( ) ;
94+
95+ start = pos && pos . from ;
10396 if ( ! start ) {
10497 return null ;
10598 }
10699
107- end = this . _endBookmark . find ( ) ;
100+ end = pos . to ;
108101 if ( ! end ) {
109- end = { line : start . line } ;
102+ end = { line : start . line } ;
110103 }
111104
112- // Even if we think we have a good end bookmark , we want to run the
113- // regexp match to see if there's a valid match that extends past the bookmark .
105+ // Even if we think we have a good range end , we want to run the
106+ // regexp match to see if there's a valid match that extends past the marker .
114107 // This can happen if the user deletes the end of the existing color and then
115108 // types some more.
116- // TODO: when we migrate to CodeMirror v3, we might be able to use markText()
117- // instead of two bookmarks to track the range. (In our current old version of
118- // CodeMirror v2, markText() isn't robust enough for this case.)
119109
120110 var line = this . hostEditor . document . getLine ( start . line ) ,
121111 matches = line . substr ( start . ch ) . match ( ColorUtils . COLOR_REGEX ) ;
@@ -124,12 +114,12 @@ define(function (require, exports, module) {
124114 // the matched length here.
125115 if ( matches && ( end . ch === undefined || end . ch - start . ch < matches [ 0 ] . length ) ) {
126116 end . ch = start . ch + matches [ 0 ] . length ;
127- this . _endBookmark . clear ( ) ;
128- this . _endBookmark = this . hostEditor . _codeMirror . setBookmark ( end ) ;
117+ this . _marker . clear ( ) ;
118+ this . _marker = this . hostEditor . _codeMirror . markText ( start , end ) ;
129119 }
130120
131121 if ( end . ch === undefined ) {
132- // We were unable to resync the end bookmark .
122+ // We were unable to resync the marker .
133123 return null ;
134124 } else {
135125 return { start : start , end : end } ;
@@ -150,14 +140,20 @@ define(function (require, exports, module) {
150140
151141 // Don't push the change back into the host editor if it came from the host editor.
152142 if ( ! this . _isHostChange ) {
143+ var endPos = {
144+ line : range . start . line ,
145+ ch : range . start . ch + colorString . length
146+ } ;
153147 this . _isOwnChange = true ;
154148 this . hostEditor . document . batchOperation ( function ( ) {
155149 // Replace old color in code with the picker's color, and select it
150+ self . hostEditor . setSelection ( range . start , range . end ) ; // workaround for #2805
156151 self . hostEditor . document . replaceRange ( colorString , range . start , range . end , self . _origin ) ;
157- self . hostEditor . setSelection ( range . start , {
158- line : range . start . line ,
159- ch : range . start . ch + colorString . length
160- } ) ;
152+ self . hostEditor . setSelection ( range . start , endPos ) ;
153+ if ( self . _marker ) {
154+ self . _marker . clear ( ) ;
155+ self . _marker = self . hostEditor . _codeMirror . markText ( range . start , endPos ) ;
156+ }
161157 } ) ;
162158 this . _isOwnChange = false ;
163159 }
@@ -202,11 +198,8 @@ define(function (require, exports, module) {
202198 InlineColorEditor . prototype . onClosed = function ( ) {
203199 InlineColorEditor . prototype . parentClass . onClosed . apply ( this , arguments ) ;
204200
205- if ( this . _startBookmark ) {
206- this . _startBookmark . clear ( ) ;
207- }
208- if ( this . _endBookmark ) {
209- this . _endBookmark . clear ( ) ;
201+ if ( this . _marker ) {
202+ this . _marker . clear ( ) ;
210203 }
211204
212205 var doc = this . hostEditor . document ;
@@ -273,15 +266,17 @@ define(function (require, exports, module) {
273266 if ( range ) {
274267 var newColor = this . hostEditor . document . getRange ( range . start , range . end ) ;
275268 if ( newColor !== this . _color ) {
276- this . _isHostChange = true ;
277- this . colorEditor . setColorFromString ( newColor ) ;
278- this . _isHostChange = false ;
269+ if ( this . colorEditor . isValidColor ( newColor ) ) { // only update the editor if the color string is valid
270+ this . _isHostChange = true ;
271+ this . colorEditor . setColorFromString ( newColor ) ;
272+ this . _isHostChange = false ;
273+ }
279274 }
280275 } else {
281276 // The edit caused our range to become invalid. Close the editor.
282277 this . close ( ) ;
283278 }
284279 } ;
285280
286- module . exports . InlineColorEditor = InlineColorEditor ;
281+ exports . InlineColorEditor = InlineColorEditor ;
287282} ) ;
0 commit comments