r101900 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101899‎ | r101900 | r101901 >
Date:22:42, 3 November 2011
Author:inez
Status:deferred
Tags:
Comment:
Improved version of keyboard & mouse selection
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -43,10 +43,7 @@
4444 alt: false
4545 }
4646 };
47 - this.selection = {
48 - from: 0,
49 - to: 0
50 - };
 47+ this.selection = new es.Range();
5148
5249 // References for use in closures
5350 var surfaceView = this,
@@ -113,18 +110,25 @@
114111 } );
115112 };
116113
 114+es.SurfaceView.prototype.hasSelection = function() {
 115+ return !!this.selection.getLength();
 116+}
 117+
117118 es.SurfaceView.prototype.onMouseDown = function( e ) {
118119 if ( e.button === 0 /* left mouse button */ ) {
119 - var position = es.Position.newFromEventPagePosition( e ),
120 - offset = this.documentView.getOffsetFromEvent( e ),
121 - nodeView = this.documentView.getNodeFromOffset( offset, false );
122 - this.showCursor( offset, position.left > nodeView.$.offset().left );
123120 this.mouse.selecting = true;
124 - if ( !this.keyboard.keys.shift ) {
125 - this.selection.from = offset;
 121+ this.selection.to = this.documentView.getOffsetFromEvent( e );
 122+
 123+ if ( this.keyboard.keys.shift ) {
 124+ this.drawSelection();
 125+ this.hideCursor();
 126+ } else {
 127+ this.documentView.clearSelection();
 128+ this.selection.from = this.selection.to;
 129+ var position = es.Position.newFromEventPagePosition( e ),
 130+ nodeView = this.documentView.getNodeFromOffset( this.selection.to, false );
 131+ this.showCursor( this.selection.to, position.left > nodeView.$.offset().left );
126132 }
127 - this.selection.to = offset;
128 - this.drawSelection();
129133 }
130134 if ( !this.$input.is( ':focus' ) ) {
131135 this.$input.focus().select();
@@ -135,21 +139,18 @@
136140
137141 es.SurfaceView.prototype.onMouseMove = function( e ) {
138142 if ( e.button === 0 /* left mouse button */ && this.mouse.selecting ) {
139 - this.hideCursor();
140143 this.selection.to = this.documentView.getOffsetFromEvent( e );
141 - if ( !this.drawSelection() ) {
142 - this.showCursor();
 144+ this.drawSelection();
 145+ if ( this.selection.getLength() ) {
 146+ this.hideCursor();
143147 }
144148 }
145149 };
146150
147151 es.SurfaceView.prototype.onMouseUp = function( e ) {
148 - if ( e.button === 0 /* left mouse button */ && this.selection.to ) {
149 - if ( this.drawSelection() ) {
150 - this.hideCursor();
151 - }
 152+ if ( e.button === 0 /* left mouse button */ ) {
 153+ this.mouse.selecting = false;
152154 }
153 - this.mouse.selecting = false;
154155 };
155156
156157 es.SurfaceView.prototype.drawSelection = function() {
@@ -161,6 +162,7 @@
162163 switch ( e.keyCode ) {
163164 case 16: // Shift
164165 this.keyboard.keys.shift = true;
 166+ this.keyboard.selecting = true;
165167 break;
166168 case 17: // Control
167169 this.keyboard.keys.control = true;
@@ -231,30 +233,60 @@
232234 };
233235
234236 es.SurfaceView.prototype.moveCursor = function( instruction ) {
 237+ this.selection.normalize();
235238 if ( instruction === 'left') {
236 - this.showCursor(
237 - this.documentView.getModel().getRelativeContentOffset( this.cursor.offset, -1 )
238 - );
 239+ if ( this.keyboard.keys.shift ) {
 240+ this.showCursor(
 241+ this.documentView.getModel().getRelativeContentOffset( this.selection.to, -1 )
 242+ );
 243+ this.drawSelection();
 244+ this.hideCursor();
 245+ } else {
 246+ this.showCursor(
 247+ this.documentView.getModel().getRelativeContentOffset( this.selection.start, -1 )
 248+ );
 249+ this.selection.from = this.selection.to;
 250+ this.documentView.clearSelection();
 251+ }
239252 } else if ( instruction === 'right' ) {
240 - this.showCursor(
241 - this.documentView.getModel().getRelativeContentOffset( this.cursor.offset, 1 )
242 - );
 253+
 254+ if ( this.keyboard.keys.shift ) {
 255+ this.showCursor(
 256+ this.documentView.getModel().getRelativeContentOffset( this.selection.to, 1 )
 257+ );
 258+ this.drawSelection();
 259+ this.hideCursor();
 260+ } else {
 261+ this.showCursor(
 262+ this.documentView.getModel().getRelativeContentOffset( this.selection.end, 1 )
 263+ );
 264+ this.selection.from = this.selection.to;
 265+ this.documentView.clearSelection();
 266+ }
243267 } else if ( instruction === 'up' || instruction === 'down' ) {
244268 // ...
245269 } else if ( instruction === 'home' || instruction === 'end' ) {
246270 var offset;
247271 if ( this.cursor.initialBias ) {
248272 offset = this.documentView.getModel().getRelativeContentOffset(
249 - this.cursor.offset, -1 );
 273+ this.selection.to, -1 );
250274 } else {
251 - offset = this.cursor.offset;
 275+ offset = this.selection.to;
252276 }
 277+
 278+
 279+
253280 if ( instruction === 'home' ) {
254281 this.showCursor(
255282 this.documentView.getRenderedLineRangeFromOffset( offset ).start, false );
256283 } else { // end
257284 this.showCursor( this.documentView.getRenderedLineRangeFromOffset( offset ).end, true );
258285 }
 286+ if ( this.keyboard.keys.shift ) {
 287+ this.drawSelection();
 288+ this.hideCursor();
 289+ }
 290+
259291 }
260292 };
261293
@@ -267,9 +299,9 @@
268300 es.SurfaceView.prototype.showCursor = function( offset, leftBias ) {
269301 if ( typeof offset !== 'undefined' ) {
270302 this.cursor.initialBias = leftBias ? true : false;
271 - this.cursor.offset = offset;
 303+ this.selection.to = offset;
272304 var position = this.documentView.getRenderedPositionFromOffset(
273 - this.cursor.offset, leftBias
 305+ this.selection.to, leftBias
274306 );
275307 this.cursor.$.css( {
276308 'left': position.left,

Status & tagging log