mediawiki-extensions-Visual.../modules/ve/dm/ve.dm.Surface.js

353 lines
9 KiB
JavaScript
Raw Normal View History

/**
* VisualEditor data model Surface class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel surface.
*
* @class
* @constructor
* @extends {ve.EventEmitter}
* @param {ve.dm.Document} doc Document model to create surface for
*/
ve.dm.Surface = function VeDmSurface( doc ) {
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
// Parent constructor
ve.EventEmitter.call( this );
// Properties
this.documentModel = doc;
this.selection = new ve.Range( 0, 0 );
this.selectedNodes = {};
this.smallStack = [];
this.bigStack = [];
this.undoIndex = 0;
this.historyTrackingInterval = null;
this.insertionAnnotations = new ve.AnnotationSet();
};
Object management: Object create/inherit/clone utilities * For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
/* Inheritance */
ve.inheritClass( ve.dm.Surface, ve.EventEmitter );
/* Methods */
/**
* Start tracking state changes in history.
*
* @method
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.startHistoryTracking = function () {
Refactor ve.js utilities and improve documentation Refactor: * ve.indexOf Renamed from ve.inArray. This was named after the jQuery method which in turn has a longer story about why it is so unfortunately named. It doesn't return a boolean, but an index. Hence the native method being called indexOf as well. * ve.bind Renamed from ve.proxy. I considered making it use Function.prototype.bind if available. As it performs better than $.proxy (which doesn't use to the native bind if available). However since bind needs to be bound itself in order to use it detached, it turns out with the "call()" and "bind()" it is slower than the $.proxy shim: http://jsperf.com/function-bind-shim-perf It would've been like this: ve.bind = Function.prototype.bind ? Function.prototype.call.bind( Function.prototype.bind ) : $.proxy; But instead sticking to ve.bind = $.proxy; * ve.extendObject Documented the parts of jQuery.extend that we use. This makes it easier to replace in the future. Documentation: * Added function documentation blocks. * Added annotations to functions that we will be able to remove in the future in favour of the native methods. With "@until + when/how". In this case "ES5". Meaning, whenever we drop support for browsers that don't support ES5. Although in the developer community ES5 is still fairly fresh, browsers have been aware for it long enough that thee moment we're able to drop it may be sooner than we think. The only blocker so far is IE8. The rest of the browsers have had it long enough that the traffic we need to support of non-IE supports it. Misc.: * Removed 'node: true' from .jshintrc since Parsoid is no longer in this repo and thus no more nodejs files. - This unraveled two lint errors: Usage of 'module' and 'console'. (both were considered 'safe globals' due to nodejs, but not in browser code). * Replaced usage (before renaming): - $.inArray -> ve.inArray - Function.prototype.bind -> ve.proxy - Array.isArray -> ve.isArray - [].indexOf -> ve.inArray - $.fn.bind/live/delegate/unbind/die/delegate -> $.fn.on/off Change-Id: Idcf1fa6a685b6ed3d7c99ffe17bd57a7bc586a2c
2012-08-11 08:14:56 +00:00
this.historyTrackingInterval = setInterval( ve.bind( this.breakpoint, this ), 750 );
};
/**
* Stop tracking state changes in history.
*
* @method
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.stopHistoryTracking = function () {
clearInterval( this.historyTrackingInterval );
};
/**
* Removes all states from history.
*
* @method
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.purgeHistory = function () {
this.selection = null;
this.smallStack = [];
this.bigStack = [];
this.undoIndex = 0;
};
/**
* Gets a list of all history states.
*
* @method
* @returns {Array[]} List of transaction stacks
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.getHistory = function () {
2011-12-13 00:56:29 +00:00
if ( this.smallStack.length > 0 ) {
return this.bigStack.slice( 0 ).concat( [{ 'stack': this.smallStack.slice(0) }] );
2011-12-13 00:56:29 +00:00
} else {
return this.bigStack.slice( 0 );
2011-12-13 00:56:29 +00:00
}
};
/**
* Gets annotations that will be used upon insertion.
*
* @method
* @returns {ve.AnnotationSet|null} Insertion anotations or null if not being used
*/
ve.dm.Surface.prototype.getInsertionAnnotations = function () {
return this.insertionAnnotations.clone();
};
/**
* Sets annotations that will be used upon insertion.
*
* @method
* @param {ve.AnnotationSet|null} Insertion anotations to use or null to disable them
* @emits 'contextChange'
*/
ve.dm.Surface.prototype.setInsertionAnnotations = function ( annotations ) {
this.insertionAnnotations = annotations.clone();
this.emit( 'contextChange' );
};
/**
* Adds an annotation to the insertion annotations.
*
* @method
* @param {ve.AnnotationSet} Insertion anotation to add
* @emits 'contextChange'
*/
ve.dm.Surface.prototype.addInsertionAnnotation = function ( annotation ) {
this.insertionAnnotations.push( annotation );
this.emit( 'contextChange' );
};
/**
* Removes an annotation from the insertion annotations.
*
* @method
* @param {ve.AnnotationSet} Insertion anotation to remove
* @emits 'contextChange'
*/
ve.dm.Surface.prototype.removeInsertionAnnotation = function ( annotation ) {
this.insertionAnnotations.remove( annotation );
this.emit( 'contextChange' );
};
/**
* Checks if there is a state to redo.
*
* @method
* @returns {Boolean} Has a future state
*/
ve.dm.Surface.prototype.hasFutureState = function() {
return this.undoIndex > 0;
};
/**
* Checks if there is a state to undo.
*
* @method
* @returns {Boolean} Has a past state
*/
ve.dm.Surface.prototype.hasPastState = function() {
return this.bigStack.length - this.undoIndex > 0;
};
/**
* Gets the document model of the surface.
*
* @method
* @returns {ve.dm.DocumentNode} Document model of the surface
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.getDocument = function () {
return this.documentModel;
};
/**
* Gets the selection
*
* @method
* @returns {ve.Range} Current selection
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.getSelection = function () {
return this.selection;
};
/**
* Gets a fragment from this document and selection.
*
* @method
* @param {ve.Range} [range] Range within target document, current selection used by default
* @param {Boolean} [noAutoSelect] Don't update the surface's selection when making changes
*/
ve.dm.Surface.prototype.getFragment = function ( range, noAutoSelect ) {
return new ve.dm.SurfaceFragment( this, range || this.selection, noAutoSelect );
};
/**
* Applies a series of transactions to the content data and sets the selection.
*
* @method
* @param {ve.dm.Transaction|ve.dm.Transaction[]|null} transactions One or more transactions to
* process, or null to process none
Make use of new jshint options * Restricting "camelcase": No changes, we were passing all of these already * Explicitly unrestricting "forin" and "plusplus" These are off by default in node-jshint, but some distro of jshint and editors that use their own wrapper around jshint instead of node-jshint (Eclipse?) may have different defaults. Therefor setting them to false explicitly. This also serves as a reminder for the future so we'll always know we don't pass that, in case we would want to change that. * Fix order ("quotemark" before "regexp") * Restricting "unused" We're not passing all of this, which is why I've set it to false for now. But I did put it in .jshintrc as placeholder. I've fixed most of them, there's some left where there is no clean solution. * While at it fix a few issues: - Unused variables ($target, $window) - Bad practices (using jQuery context for find instead of creation) - Redundant /*global */ comments - Parameters that are not used and don't have documentation either - Lines longer than 100 chars @ 4 spaces/tab * Note: - ve.ce.Surface.prototype.onChange takes two arguments but never uses the former. And even the second one can be null/undefined. Aside from that, the .change() function emits another event for the transaction already. Looks like this should be refactored a bit, two more separated events probably or one that is actually used better. - Also cleaned up a lot of comments, some of which were missing, others were incorrect - Reworked the contentChange event so we are no longer using the word new as an object key; expanded a complex object into multiple arguments being passed through the event to make it easier to work with and document Change-Id: I8490815a508c6c379d5f9a743bb4aefd14576aa6
2012-08-07 06:02:18 +00:00
* @param {ve.Range|undefined} selection
*/
ve.dm.Surface.prototype.change = function ( transactions, selection ) {
var i, len, offset, annotations,
selectedNodes = {},
selectionChange = false,
contextChange = false;
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
(bug 42925) Inspector doesn't open properly ve.Range * Rewrote truncate so that it works as expected, truncation should always reduce the length using the start/end values, not the from/to values ve.ui.Inspector * Added a comment about where the name argument to onBeforeInspectorOpen comes from, since it's a little bit confusing on first read (and I wrote it!) * Calling onInitialize, onOpen and onClose methods directly, since we need to control the before or after-ness of listeners getting in there and doing their stuff - plus it's more direct * Removed onRemove stub, which is never actually called * Added before/after versions of initialize, open and close events * Got rid of recursion guard since we don't need it anymore thanks to changes made in ve.dm.Surface (see below) ve.ui.Context * Updated event names to deal with new before/after naming of initialize, open and close events * Removed fade-in logic since fading in doesn't even work anymore - since now we now annotate first, then open the inspector, the menu will actually exist and be open when we open the inspector even though you don't see it because it's quickly obscured ve.ui.LinkInspector * Made fragments non-auto selecting, in the case of onInitialize we actually call select(), which is silly since we were using an auto-selecting fragment - it's clear that this was a mistake ve.dm.Surface * Moved locking (polling stop and start) to the far outside edges of the change method * I need a lot of eyes and testing on this change, it seems OK to me, but I'm suspicious that it may have side effects * What was happening is that selection changes were being applied and then the poll was picking them up, and then the selection was coming in again as a change, but it wasn't a change at all, it was just feedback - this change event was then closing the inspector the instant it was opened - the odd part was that this only occurred when you selected backwards, which seems to be caused by the range being normalized, so it looked like a new selection even though it wasn't ve.dm.Document * trimOuterSpace from Range didn't consider annotated spaces to be spaces, by using the [0] trick (first character of a plain text character string or first element in an annotated character's array both are the character's value - but elements don't have a property named '0' so it skips those safely as well) we can always get the right value for comparison Change-Id: I0873d906c058203b83b8d4bbe5a4b274f05a26fd
2012-12-10 21:36:28 +00:00
// Stop observation polling, things changing right now are known already
this.emit( 'lock' );
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
// Process transactions and apply selection changes
if ( transactions ) {
if ( transactions instanceof ve.dm.Transaction ) {
transactions = [transactions];
}
for ( i = 0, len = transactions.length; i < len; i++ ) {
if ( !transactions[i].isNoOp() ) {
this.bigStack = this.bigStack.slice( 0, this.bigStack.length - this.undoIndex );
this.undoIndex = 0;
this.smallStack.push( transactions[i] );
ve.dm.TransactionProcessor.commit( this.documentModel, transactions[i] );
}
}
}
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
if ( selection ) {
// Detect if selection range changed
if ( !this.selection || !this.selection.equals( selection ) ) {
selection.normalize();
selectionChange = true;
}
// Detect if selected nodes changed
selectedNodes.start = this.documentModel.getNodeFromOffset( selection.start );
if ( selection.getLength() ) {
selectedNodes.end = this.documentModel.getNodeFromOffset( selection.end );
}
if (
selectedNodes.start !== this.selectedNodes.start ||
selectedNodes.end !== this.selectedNodes.end
) {
contextChange = true;
}
this.selectedNodes = selectedNodes;
if ( selectionChange ) {
this.emit( 'select', this.selection.clone() );
}
this.selection = selection;
}
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
// Only emit a transact event if transactions were actually processed
if ( transactions ) {
this.emit( 'transact', transactions );
// Detect context change, if not detected already, when element attributes have changed
if ( !contextChange ) {
for ( i = 0, len = transactions.length; i < len; i++ ) {
if ( transactions[i].hasElementAttributeOperations() ) {
contextChange = true;
break;
}
}
}
}
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
// Figure out which offset which we should get insertion annotations from
if ( this.selection.isCollapsed() ) {
// Get annotations from the left of the cursor
offset = this.documentModel.getNearestContentOffset(
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
Math.max( 0, this.selection.start - 1 ), -1
);
Fix even more inspector issues, and some ce ones as well ve.dm.SurfaceFragment * Removed flawed implementation of word mode for expandRange method and made use of new getNearestWordBoundary method in the document model ve.dm.Surface * Got rid of useInsertionAnnotations, which allowed disabling and enabling of insertion annotations - this isn't needed anymore because it was just a dirty hack around the improper starting and stopping of surface observer that's now solved more elegantly by emitting lock and unlock before committing or rolling back transactions * Get annotations from the first character of the selection if the selection is not collapsed * Only emit annotationChange events if it really changed ve.dm.Document * Added getNearestWordBoundary method which performs the work behind the surface fragment expandRange word method ve.ce.SurfaceObserver * Allow using an initial selection to avoid the observer thinking the selection has changed just because it started out with null * Only emit selectionChange event if there was a meaningful change ve.ce.Surface * (bug 42279) Only annotate characters if insertion annotations are not empty * Remove manual locking and unlocking, this is now done inside the change method of surface model * Provide an initial selection to surface observer when we clear it * Remove enabling and disabling of insertionAnnotations, this isn't needed anymore * Stop/start observer on key presses that execute actions as well as those that have no special handling ve.ce.Document * Make getNodeFromOffsetand getSlugAtOffset return null when given -1 as an offset Change-Id: Ibf6b26de299e54ae8688a2653bf5d5538927f8c3
2012-11-21 21:22:29 +00:00
} else {
// Get annotations from the first character of the selection
offset = this.documentModel.getNearestContentOffset( this.selection.start );
}
if ( offset === -1 ) {
// Document is empty, use empty set
annotations = new ve.AnnotationSet();
} else {
annotations = this.documentModel.getAnnotationsFromOffset( offset );
}
// Only emit an annotations change event if there's a meaningful difference
if (
!annotations.containsAllOf( this.insertionAnnotations ) ||
!this.insertionAnnotations.containsAllOf( annotations )
) {
this.insertionAnnotations = annotations;
contextChange = true;
}
(bug 42925) Inspector doesn't open properly ve.Range * Rewrote truncate so that it works as expected, truncation should always reduce the length using the start/end values, not the from/to values ve.ui.Inspector * Added a comment about where the name argument to onBeforeInspectorOpen comes from, since it's a little bit confusing on first read (and I wrote it!) * Calling onInitialize, onOpen and onClose methods directly, since we need to control the before or after-ness of listeners getting in there and doing their stuff - plus it's more direct * Removed onRemove stub, which is never actually called * Added before/after versions of initialize, open and close events * Got rid of recursion guard since we don't need it anymore thanks to changes made in ve.dm.Surface (see below) ve.ui.Context * Updated event names to deal with new before/after naming of initialize, open and close events * Removed fade-in logic since fading in doesn't even work anymore - since now we now annotate first, then open the inspector, the menu will actually exist and be open when we open the inspector even though you don't see it because it's quickly obscured ve.ui.LinkInspector * Made fragments non-auto selecting, in the case of onInitialize we actually call select(), which is silly since we were using an auto-selecting fragment - it's clear that this was a mistake ve.dm.Surface * Moved locking (polling stop and start) to the far outside edges of the change method * I need a lot of eyes and testing on this change, it seems OK to me, but I'm suspicious that it may have side effects * What was happening is that selection changes were being applied and then the poll was picking them up, and then the selection was coming in again as a change, but it wasn't a change at all, it was just feedback - this change event was then closing the inspector the instant it was opened - the odd part was that this only occurred when you selected backwards, which seems to be caused by the range being normalized, so it looked like a new selection even though it wasn't ve.dm.Document * trimOuterSpace from Range didn't consider annotated spaces to be spaces, by using the [0] trick (first character of a plain text character string or first element in an annotated character's array both are the character's value - but elements don't have a property named '0' so it skips those safely as well) we can always get the right value for comparison Change-Id: I0873d906c058203b83b8d4bbe5a4b274f05a26fd
2012-12-10 21:36:28 +00:00
// Only emit one context change event
if ( contextChange ) {
this.emit( 'contextChange' );
}
this.emit( 'change', transactions, selection );
(bug 42925) Inspector doesn't open properly ve.Range * Rewrote truncate so that it works as expected, truncation should always reduce the length using the start/end values, not the from/to values ve.ui.Inspector * Added a comment about where the name argument to onBeforeInspectorOpen comes from, since it's a little bit confusing on first read (and I wrote it!) * Calling onInitialize, onOpen and onClose methods directly, since we need to control the before or after-ness of listeners getting in there and doing their stuff - plus it's more direct * Removed onRemove stub, which is never actually called * Added before/after versions of initialize, open and close events * Got rid of recursion guard since we don't need it anymore thanks to changes made in ve.dm.Surface (see below) ve.ui.Context * Updated event names to deal with new before/after naming of initialize, open and close events * Removed fade-in logic since fading in doesn't even work anymore - since now we now annotate first, then open the inspector, the menu will actually exist and be open when we open the inspector even though you don't see it because it's quickly obscured ve.ui.LinkInspector * Made fragments non-auto selecting, in the case of onInitialize we actually call select(), which is silly since we were using an auto-selecting fragment - it's clear that this was a mistake ve.dm.Surface * Moved locking (polling stop and start) to the far outside edges of the change method * I need a lot of eyes and testing on this change, it seems OK to me, but I'm suspicious that it may have side effects * What was happening is that selection changes were being applied and then the poll was picking them up, and then the selection was coming in again as a change, but it wasn't a change at all, it was just feedback - this change event was then closing the inspector the instant it was opened - the odd part was that this only occurred when you selected backwards, which seems to be caused by the range being normalized, so it looked like a new selection even though it wasn't ve.dm.Document * trimOuterSpace from Range didn't consider annotated spaces to be spaces, by using the [0] trick (first character of a plain text character string or first element in an annotated character's array both are the character's value - but elements don't have a property named '0' so it skips those safely as well) we can always get the right value for comparison Change-Id: I0873d906c058203b83b8d4bbe5a4b274f05a26fd
2012-12-10 21:36:28 +00:00
// Continue observation polling, we want to know about things that change from here on out
this.emit( 'unlock' );
};
/**
* Sets a history state breakpoint.
*
* @method
* @param {ve.Range} selection New selection range
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.breakpoint = function ( selection ) {
if ( this.smallStack.length > 0 ) {
this.bigStack.push( {
stack: this.smallStack,
selection: selection || this.selection.clone()
} );
this.smallStack = [];
this.emit( 'history' );
}
};
/**
* Steps backwards in history.
*
* @method
* @returns {ve.Range} Selection or null if no further state could be reached
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.undo = function () {
var item, i, transaction, selection;
this.breakpoint();
this.undoIndex++;
if ( this.bigStack[this.bigStack.length - this.undoIndex] ) {
this.emit( 'lock' );
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
item = this.bigStack[this.bigStack.length - this.undoIndex];
selection = item.selection;
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
for ( i = item.stack.length - 1; i >= 0; i-- ) {
transaction = item.stack[i];
selection = transaction.translateRange( selection, true );
this.documentModel.rollback( item.stack[i] );
}
this.emit( 'unlock' );
this.emit( 'history' );
return selection;
}
return null;
};
/**
* Steps forwards in history.
*
* @method
* @returns {ve.Range} Selection or null if no further state could be reached
*/
Kranitor #3: jQuerlyfornication ft. The Cascaders * Classicifation (JS) Use addClass instead of attr( 'class' ) whenever possible. addClass will manipulate the properties directly instead of (re-)setting an attribute which (most) browsers then sync with the properties. Difference between: elem.className and elem.setAttribute( 'class', .. ); Just like .checked, .value, .disabled and other interactive properties, the HTML attributes should only be used for initial values from the html document. When in javascript, only set properties. Attributes are either ignored or slow. * Styling (JS) Use .css() instead of attr( 'style' ). Again, setting properties instead of attributes is much faster, easier and safer. And this way it takes care of cross-browser issues where applicable, and less prone to error due to dealing with key-value pairs instead of css strings. Difference between: elem.style.foo = 'bar'; and elem.setAttribute( 'style', 'foo: bar;' ); * Finding (JS) Use .find( 'foo bar' ) instead of .find( 'foo' ).find( 'bar' ). It is CSS! * Vendor prefixes (CSS) It is important to always list newer (standards-compliant) versions *after* the older/prefixed variants. See also http://css-tricks.com/ordering-css3-properties/ So the following three: -webkit-gradient (Chrome, Safari 4) -webkit-linear-gradient (Chrome 10, Safari 5+) linear-gradient (CSS3 standard) ... must be in that order. Notes: - "-moz-opacity" is from before Mozilla 1.7 (Firefox < 0.8) Has not been renamed to "opacity" since Firefox 0.9. - Removed redundant "-moz-opacity" - Added "filter: alpha(opacity=**);" where missing - Fixed order of css3 properties (old to new) - Add standardized css3 versions where missing (some 'border-radius' groups didn't have the non-prefixed version) - Spacing - @embed - Shorten hex colors where possible (#dddddd -> #ddd) $ ack '#([0-9a-f])\1{5}' --css $ ack '#([0-9a-f])\1{2};' --css Change-Id: I386fedb9058c2567fd0af5f55291e9859a53329d
2012-07-28 19:15:23 +00:00
ve.dm.Surface.prototype.redo = function () {
var selection, item, i;
this.breakpoint();
if ( this.undoIndex > 0 && this.bigStack[this.bigStack.length - this.undoIndex] ) {
this.emit( 'lock' );
item = this.bigStack[this.bigStack.length - this.undoIndex];
selection = item.selection;
for ( i = 0; i < item.stack.length; i++ ) {
this.documentModel.commit( item.stack[i] );
}
this.undoIndex--;
this.emit( 'unlock' );
this.emit( 'history' );
return selection;
}
return null;
};