r103872 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103871‎ | r103872 | r103873 >
Date:23:51, 21 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
* Refactored es.HistoryModel to always be working from a single array rather than a buffer and an array
* Added support for associating a selection with a state
Modified paths:
  • /trunk/extensions/VisualEditor/demo/index.html (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.HistoryModel.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.HistoryStateModel.js (added) (history)
  • /trunk/extensions/VisualEditor/tests/es/index.html (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/es/index.html
@@ -32,6 +32,7 @@
3333 <!-- Models -->
3434 <script src="../../modules/es/models/es.DocumentModel.js"></script>
3535 <script src="../../modules/es/models/es.HistoryModel.js"></script>
 36+ <script src="../../modules/es/models/es.HistoryStateModel.js"></script>
3637 <script src="../../modules/es/models/es.ListItemModel.js"></script>
3738 <script src="../../modules/es/models/es.ListModel.js"></script>
3839 <script src="../../modules/es/models/es.ParagraphModel.js"></script>
Index: trunk/extensions/VisualEditor/demo/index.html
@@ -82,6 +82,7 @@
8383 <script src="../modules/es/models/es.SurfaceModel.js"></script>
8484 <script src="../modules/es/models/es.DocumentModel.js"></script>
8585 <script src="../modules/es/models/es.HistoryModel.js"></script>
 86+ <script src="../modules/es/models/es.HistoryStateModel.js"></script>
8687 <script src="../modules/es/models/es.ParagraphModel.js"></script>
8788 <script src="../modules/es/models/es.PreModel.js"></script>
8889 <script src="../modules/es/models/es.ListModel.js"></script>
Index: trunk/extensions/VisualEditor/modules/es/models/es.HistoryModel.js
@@ -12,13 +12,13 @@
1313
1414 // Properties
1515 this.doc = doc;
16 - this.states = [];
17 - this.currentStateIndex = -1;
18 - this.transactions = [];
19 - this.transactionsDiff = 0;
 16+ this.currentState = new es.HistoryStateModel();
 17+ this.currentStateIndex = 0;
 18+ this.states = [this.currentState];
 19+ this.currentStateDiff = 0;
2020
2121 // Configuration
22 - this.maxTransactionsDiff = 24;
 22+ this.maxStateDiff = 24;
2323 };
2424
2525 /* Methods */
@@ -33,6 +33,15 @@
3434 };
3535
3636 /**
 37+ * Gets the index of the current state.
 38+ *
 39+ *
 40+ */
 41+es.HistoryModel.prototype.getCurrentStateSelection = function() {
 42+ return this.currentState.getSelection();
 43+};
 44+
 45+/**
3746 * Gets the number of states available.
3847 *
3948 * @method
@@ -72,24 +81,26 @@
7382 *
7483 * @method
7584 * @param {es.TransactionModel} transaction Transaction to commit
76 - * @param {Boolean} accumulate Prevent automatic state pushing
 85+ * @param {es.Range} [selection] Selection to use after the transaction has been applied
 86+ * @param {Boolean} [accumulate] Prevent automatic state pushing
7787 */
78 -es.HistoryModel.prototype.commit = function( transaction, accumulate ) {
79 - var absLengthDiff = Math.abs( transaction.getLengthDiff() );
 88+es.HistoryModel.prototype.commit = function( transaction, selection, accumulate ) {
 89+ var transactionDiff = Math.abs( transaction.getLengthDiff() );
8090 // Unless we should intentionally accumulate transactions or this is the first one for this
8191 // state, automatically push state
82 - if ( !accumulate && this.transactions.length ) {
 92+ var transactionCount = this.currentState.getTransactionCount();
 93+ if ( !accumulate && transactionCount ) {
8394 if (
8495 // If the transactions are of a different type
85 - this.transactions[this.transactions.length - 1].type !== transaction.type ||
 96+ this.currentState.getTransactions()[transactionCount - 1].type !== transaction.type ||
8697 // This transaction would make the state longer than the maximum length
87 - this.transactionsDiff + absLengthDiff > this.maxTransactionsDiff
 98+ this.currentStateDiff + transactionDiff > this.maxStateDiff
8899 ) {
89100 this.pushState();
90101 }
91102 }
92 - this.transactions.push( transaction );
93 - this.transactionsDiff += absLengthDiff;
 103+ this.currentState.pushTransaction( transaction );
 104+ this.currentStateDiff += transactionDiff;
94105 // Apply transaction to the document
95106 this.doc.commit( transaction );
96107 // Emit a do event with the transaction that was just committed
@@ -103,7 +114,7 @@
104115 */
105116 es.HistoryModel.prototype.pushState = function() {
106117 // If any transactions have been pushed since the last state push
107 - if ( this.transactions.length ) {
 118+ if ( this.currentState.getTransactionCount() ) {
108119 // If the current state is not the most recently added state
109120 if ( this.currentStateIndex < this.states.length - 1 ) {
110121 // Forget about states newer than the current one
@@ -111,14 +122,17 @@
112123 this.currentStateIndex, this.states.length - this.currentStateIndex
113124 );
114125 }
115 - // Add accumulated transactions as a state
116 - this.states.push( this.transactions );
117 - // Clear the transaction buffer
118 - this.transactions = [];
119 - this.transactionsDiff = 0;
120 - // Move the current state forward
121 - this.currentStateIndex++;
 126+ // Create a new current state
 127+ this.currentState = new es.HistoryStateModel();
 128+ // Add the new current state to the stack
 129+ this.states.push( this.currentState );
 130+ // Reset the state diff counter
 131+ this.currentStateDiff = 0;
 132+ // Move the current state index to the end (should be equivilant of ++)
 133+ this.currentStateIndex = this.states.length - 1;
122134 }
 135+ // Emit the completed
 136+ this.emit( 'pushState', this.states[this.states.length - 1] );
123137 };
124138
125139 /**
@@ -130,16 +144,15 @@
131145 if ( steps === undefined ) {
132146 steps = 1;
133147 }
134 - // Apply transactions in the buffer
135 - this.pushState();
136148 // Stop undo just before the first state
137149 var previousStateIndex = this.currentStateIndex;
138150 this.currentStateIndex = Math.max( -1, this.currentStateIndex - steps );
139151 if ( previousStateIndex > this.currentStateIndex ) {
140152 for ( var i = previousStateIndex; i > this.currentStateIndex; i-- ) {
141153 // Apply transaction to the document
142 - for ( var j = this.states[i].length - 1; j >= 0; j-- ) {
143 - this.doc.rollback( this.states[i][j] );
 154+ var transactions = this.states[i].getTransactions();
 155+ for ( var j = transactions.length - 1; j >= 0; j-- ) {
 156+ this.doc.rollback( transactions[j] );
144157 }
145158 // Emit an undo event with the state to be rolled back
146159 this.emit( 'undo', this.states[i] );
@@ -156,15 +169,16 @@
157170 if ( steps === undefined ) {
158171 steps = 1;
159172 }
160 - // Apply transactions in the buffer
161 - this.pushState();
162173 // Stop redo at the last state
163174 var previousStateIndex = this.currentStateIndex;
164175 this.currentStateIndex = Math.min( this.states.length - 1, this.currentStateIndex + steps );
165176 if ( previousStateIndex < this.currentStateIndex ) {
166177 for ( var i = previousStateIndex + 1; i >= this.currentStateIndex; i++ ) {
167178 // Apply transaction to the document
168 - this.doc.rollback( this.states[i] );
 179+ var transactions = this.states[i].getTransactions();
 180+ for ( var j = 0; j < transactions.length; j++ ) {
 181+ this.doc.commit( transactions[j] );
 182+ }
169183 // Emit an undo event with the state to be rolled back
170184 this.emit( 'redo', this.states[i] );
171185 }
Index: trunk/extensions/VisualEditor/modules/es/models/es.HistoryStateModel.js
@@ -0,0 +1,31 @@
 2+/**
 3+ * Creates an es.HistoryStateModel object.
 4+ *
 5+ * @class
 6+ * @constructor
 7+ */
 8+es.HistoryStateModel = function() {
 9+ this.transactions = [];
 10+ this.selection = null;
 11+};
 12+
 13+/* Methods */
 14+
 15+es.HistoryStateModel.prototype.getSelection = function() {
 16+ return this.selection;
 17+};
 18+
 19+es.HistoryStateModel.prototype.getTransactions = function() {
 20+ return this.transactions;
 21+};
 22+
 23+es.HistoryStateModel.prototype.getTransactionCount = function() {
 24+ return this.transactions.length;
 25+};
 26+
 27+es.HistoryStateModel.prototype.pushTransaction = function( transaction, selection ) {
 28+ this.transactions.push( transaction );
 29+ if ( selection !== undefined ) {
 30+ this.selection = selection.clone();
 31+ }
 32+};

Status & tagging log