r102615 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102614‎ | r102615 | r102616 >
Date:01:28, 10 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added very basic support for highlighting formatting tools when the cursor is over content which is formatted that way
Modified paths:
  • /trunk/extensions/VisualEditor/demo/es.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/es.Range.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/es.Range.js
@@ -16,7 +16,6 @@
1717 this.normalize();
1818 };
1919
20 -
2120 /**
2221 * Creates a new es.Range object that's a translated version of another.
2322 *
@@ -32,6 +31,16 @@
3332 /* Methods */
3433
3534 /**
 35+ * Gets a clone of this object.
 36+ *
 37+ * @method
 38+ * @returns {es.Range} Clone of range
 39+ */
 40+es.Range.prototype.clone = function() {
 41+ return new es.Range( this.from, this.to );
 42+};
 43+
 44+/**
3645 * Checks if an offset is within this range.
3746 *
3847 * @method
Index: trunk/extensions/VisualEditor/modules/es/views/es.SurfaceView.js
@@ -7,10 +7,34 @@
88 * @param {es.SurfaceModel} model Surface model to view
99 */
1010 es.SurfaceView = function( $container, model ) {
 11+ var _this = this;
 12+
 13+ es.EventEmitter.call( this );
 14+
1115 this.$ = $container.addClass( 'es-surfaceView' );
1216 this.$window = $( window );
1317 this.model = model;
14 -
 18+ this.selection = new es.Range();
 19+ this.previousSelection = null;
 20+
 21+ this.model.getDocument().on( 'update', function() {
 22+ _this.emit( 'update' );
 23+ } );
 24+ this.emitSelect = function() {
 25+ if ( _this.previousSelection ) {
 26+ if (
 27+ _this.previousSelection.from !== _this.selection.from ||
 28+ _this.previousSelection.to !== _this.selection.to
 29+ ) {
 30+ _this.emit( 'select', _this.selection.clone() );
 31+ _this.previousSelection = _this.selection.clone();
 32+ }
 33+ // Mouse movement that doesn't change selection points will terminate here
 34+ } else {
 35+ _this.previousSelection = _this.selection.clone();
 36+ }
 37+ };
 38+
1539 // Initialize document view
1640 this.documentView = new es.DocumentView( this.model.getDocument(), this );
1741 this.$.append( this.documentView.$ );
@@ -37,7 +61,6 @@
3862 alt: false
3963 }
4064 };
41 - this.selection = new es.Range();
4265
4366 // References for use in closures
4467 var surfaceView = this,
@@ -113,7 +136,7 @@
114137 this.mouse.selectingMode = 1;
115138
116139 this.selection.to = this.documentView.getOffsetFromEvent( e );
117 - console.log(this.selection.to);
 140+ //console.log(this.selection.to);
118141 if ( this.keyboard.keys.shift ) {
119142 this.documentView.drawSelection( this.selection );
120143 this.hideCursor();
@@ -154,15 +177,17 @@
155178 this.$input.focus().select();
156179 }
157180 this.cursor.initialLeft = null;
 181+ this.emitSelect();
158182 return false;
159183 };
160184
161185 es.SurfaceView.prototype.onMouseMove = function( e ) {
 186+ var wordBoundaries;
162187 if ( e.button === 0 /* left mouse button */ && this.mouse.selectingMode ) {
163188 if ( this.mouse.selectingMode === 1 ) {
164189 this.selection.to = this.documentView.getOffsetFromEvent( e );
165190 } else if ( this.mouse.selectingMode === 2 ) {
166 - var wordBoundaries = this.documentView.model.getWordBoundaries(
 191+ wordBoundaries = this.documentView.model.getWordBoundaries(
167192 this.documentView.getOffsetFromEvent( e )
168193 );
169194 if ( wordBoundaries.to <= this.mouse.selectedRange.from ) {
@@ -193,7 +218,6 @@
194219 this.hideCursor();
195220 }
196221 }
197 - return;
198222
199223 if ( e.button === 0 /* left mouse button */ && this.mouse.selected ) {
200224
@@ -201,7 +225,7 @@
202226 if ( this.mouse.selected.containsOffset( offset ) ) {
203227 //return;
204228 }
205 - var wordBoundaries = this.documentView.model.getWordBoundaries( offset );
 229+ wordBoundaries = this.documentView.model.getWordBoundaries( offset );
206230 if ( wordBoundaries.to <= this.mouse.selected.from ) {
207231 this.selection.to = wordBoundaries.from;
208232 this.selection.from = this.mouse.selected.to;
@@ -226,8 +250,8 @@
227251
228252 } else if ( e.button === 0 /* left mouse button */ && this.mouse.selecting ) {
229253 this.selection.to = this.documentView.getOffsetFromEvent( e );
230 -
231254 }
 255+ this.emitSelect();
232256 };
233257
234258 es.SurfaceView.prototype.onMouseUp = function( e ) {
@@ -237,6 +261,7 @@
238262 };
239263
240264 es.SurfaceView.prototype.onKeyDown = function( e ) {
 265+ var tx;
241266 switch ( e.keyCode ) {
242267 case 16: // Shift
243268 this.keyboard.keys.shift = true;
@@ -280,14 +305,18 @@
281306 this.moveCursor( 'down' );
282307 break;
283308 case 8: // Backspace
284 - var transaction = this.documentView.model.prepareRemoval( new es.Range( this.selection.to, this.selection.to - 1 ) );
285 - this.documentView.model.commit ( transaction );
 309+ tx = this.documentView.model.prepareRemoval(
 310+ new es.Range( this.selection.to, this.selection.to - 1 )
 311+ );
 312+ this.documentView.model.commit( tx );
286313 this.selection.from = this.selection.to -= 1;
287314 this.showCursor();
288315 break;
289316 case 46: // Delete
290 - var transaction = this.documentView.model.prepareRemoval( new es.Range( this.selection.to, this.selection.to + 1 ) );
291 - this.documentView.model.commit ( transaction );
 317+ tx = this.documentView.model.prepareRemoval(
 318+ new es.Range( this.selection.to, this.selection.to + 1 )
 319+ );
 320+ this.documentView.model.commit( tx );
292321 break;
293322 default: // Insert content (maybe)
294323 if ( this.keyboard.keydownTimeout ) {
@@ -445,6 +474,7 @@
446475 this.selection.from = this.selection.to = newTo;
447476 this.showCursor();
448477 }
 478+ this.emitSelect();
449479 };
450480
451481 /**
@@ -489,3 +519,7 @@
490520 }
491521 this.cursor.$.hide();
492522 };
 523+
 524+/* Inheritance */
 525+
 526+es.extendClass( es.SurfaceView, es.EventEmitter );
\ No newline at end of file
Index: trunk/extensions/VisualEditor/demo/es.js
@@ -294,4 +294,24 @@
295295 window.doc = es.DocumentModel.newFromPlainObject( window.wikiDom );
296296 window.surfaceModel = new es.SurfaceModel( window.doc );
297297 window.surfaceView = new es.SurfaceView( $( '#es-editor' ), window.surfaceModel );
 298+
 299+ var tools = {
 300+ 'textStyle/bold': $( '#es-toolbar-bold' ),
 301+ 'textStyle/italic': $( '#es-toolbar-italic' )
 302+ };
 303+ surfaceView.on( 'select', function( range ) {
 304+ for ( var key in tools ) {
 305+ tools[key].removeClass( 'es-toolbarTool-down' );
 306+ }
 307+ if ( range.start == range.end ) {
 308+ var annotations = doc.getAnnotationsFromOffset( range.start );
 309+ if ( annotations.length ) {
 310+ for ( var i = 0; i < annotations.length; i++ ) {
 311+ if ( annotations[i].type in tools ) {
 312+ tools[annotations[i].type].addClass( 'es-toolbarTool-down' );
 313+ }
 314+ }
 315+ }
 316+ }
 317+ } );
298318 } );

Status & tagging log