| 1 | /*! |
| 2 | * VisualEditor Selection class. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @class |
| 9 | * @abstract |
| 10 | * @constructor |
| 11 | */ |
| 12 | ve.dm.Selection = function VeDmSelection() { |
| 13 | }; |
| 14 | |
| 15 | /* Inheritance */ |
| 16 | |
| 17 | OO.initClass( ve.dm.Selection ); |
| 18 | |
| 19 | /* Static Properties */ |
| 20 | |
| 21 | ve.dm.Selection.static.type = null; |
| 22 | |
| 23 | /* Static Methods */ |
| 24 | |
| 25 | /** |
| 26 | * Create a new selection from a JSON serialization |
| 27 | * |
| 28 | * @param {string|Object} json JSON serialization or hash object |
| 29 | * @return {ve.dm.Selection} New selection |
| 30 | * @throws {Error} Unknown selection type |
| 31 | */ |
| 32 | ve.dm.Selection.static.newFromJSON = function ( json ) { |
| 33 | if ( ve.dm.Document && arguments[ 0 ] instanceof ve.dm.Document ) { |
| 34 | throw new Error( 'Got obsolete ve.dm.Document argument' ); |
| 35 | } |
| 36 | const hash = typeof json === 'string' ? JSON.parse( json ) : json; |
| 37 | const constructor = ve.dm.selectionFactory.lookup( hash.type ); |
| 38 | |
| 39 | if ( !constructor ) { |
| 40 | throw new Error( 'Unknown selection type ' + hash.name ); |
| 41 | } |
| 42 | |
| 43 | return constructor.static.newFromHash( hash ); |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Create a new selection from a hash object |
| 48 | * |
| 49 | * @abstract |
| 50 | * @param {Object} hash |
| 51 | * @return {ve.dm.Selection} New selection |
| 52 | */ |
| 53 | ve.dm.Selection.static.newFromHash = null; |
| 54 | |
| 55 | /* Methods */ |
| 56 | |
| 57 | /** |
| 58 | * Get a JSON serialization of this selection |
| 59 | * |
| 60 | * @abstract |
| 61 | * @param {string} [key] Key in parent object |
| 62 | * @return {Object} Object for JSON serialization |
| 63 | */ |
| 64 | ve.dm.Selection.prototype.toJSON = null; |
| 65 | |
| 66 | /** |
| 67 | * Get a textual description of this selection, for debugging purposes |
| 68 | * |
| 69 | * @abstract |
| 70 | * @return {string} Textual description |
| 71 | */ |
| 72 | ve.dm.Selection.prototype.getDescription = null; |
| 73 | |
| 74 | /** |
| 75 | * Get a new selection at the start point of this one |
| 76 | * |
| 77 | * @abstract |
| 78 | * @return {ve.dm.Selection} Collapsed selection |
| 79 | */ |
| 80 | ve.dm.Selection.prototype.collapseToStart = null; |
| 81 | |
| 82 | /** |
| 83 | * Get a new selection at the end point of this one |
| 84 | * |
| 85 | * @abstract |
| 86 | * @return {ve.dm.Selection} Collapsed selection |
| 87 | */ |
| 88 | ve.dm.Selection.prototype.collapseToEnd = null; |
| 89 | |
| 90 | /** |
| 91 | * Get a new selection at the 'from' point of this one |
| 92 | * |
| 93 | * @abstract |
| 94 | * @return {ve.dm.Selection} Collapsed selection |
| 95 | */ |
| 96 | ve.dm.Selection.prototype.collapseToFrom = null; |
| 97 | |
| 98 | /** |
| 99 | * Get a new selection at the 'to' point of this one |
| 100 | * |
| 101 | * @abstract |
| 102 | * @return {ve.dm.Selection} Collapsed selection |
| 103 | */ |
| 104 | ve.dm.Selection.prototype.collapseToTo = null; |
| 105 | |
| 106 | /** |
| 107 | * Check if a selection is collapsed |
| 108 | * |
| 109 | * @abstract |
| 110 | * @return {boolean} Selection is collapsed |
| 111 | */ |
| 112 | ve.dm.Selection.prototype.isCollapsed = null; |
| 113 | |
| 114 | /** |
| 115 | * Apply translations from a transaction |
| 116 | * |
| 117 | * @abstract |
| 118 | * @param {ve.dm.Transaction} tx |
| 119 | * @param {boolean} [excludeInsertion] Do not grow to cover insertions at boundaries |
| 120 | * @return {ve.dm.Selection} A new translated selection |
| 121 | */ |
| 122 | ve.dm.Selection.prototype.translateByTransaction = null; |
| 123 | |
| 124 | /** |
| 125 | * Apply translations from a transaction, with bias depending on author ID comparison |
| 126 | * |
| 127 | * @abstract |
| 128 | * @param {ve.dm.Transaction} tx |
| 129 | * @param {number} authorId The selection's author ID |
| 130 | * @return {ve.dm.Selection} A new translated selection |
| 131 | */ |
| 132 | ve.dm.Selection.prototype.translateByTransactionWithAuthor = null; |
| 133 | |
| 134 | /** |
| 135 | * Apply translations from a set of transactions |
| 136 | * |
| 137 | * @param {ve.dm.Transaction[]} txs Transactions |
| 138 | * @param {boolean} [excludeInsertion] Do not grow to cover insertions at boundaries |
| 139 | * @return {ve.dm.Selection} A new translated selection |
| 140 | */ |
| 141 | ve.dm.Selection.prototype.translateByTransactions = function ( txs, excludeInsertion ) { |
| 142 | let selection = this; |
| 143 | for ( let i = 0, l = txs.length; i < l; i++ ) { |
| 144 | selection = selection.translateByTransaction( txs[ i ], excludeInsertion ); |
| 145 | } |
| 146 | return selection; |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Apply translations from a change |
| 151 | * |
| 152 | * @param {ve.dm.Change} change |
| 153 | * @param {number} authorId The author ID of this selection |
| 154 | * @return {ve.dm.Selection} A new translated selection |
| 155 | */ |
| 156 | ve.dm.Selection.prototype.translateByChange = function ( change, authorId ) { |
| 157 | let selection = this; |
| 158 | for ( let i = 0, len = change.transactions.length; i < len; i++ ) { |
| 159 | selection = selection.translateByTransactionWithAuthor( |
| 160 | change.transactions[ i ], |
| 161 | authorId |
| 162 | ); |
| 163 | } |
| 164 | return selection; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * Check if this selection is null |
| 169 | * |
| 170 | * @return {boolean} The selection is null |
| 171 | */ |
| 172 | ve.dm.Selection.prototype.isNull = function () { |
| 173 | return false; |
| 174 | }; |
| 175 | |
| 176 | /** |
| 177 | * Get the content ranges for this selection |
| 178 | * |
| 179 | * @abstract |
| 180 | * @param {ve.dm.Document} doc The document to which this selection applies |
| 181 | * @return {ve.Range[]} Ranges |
| 182 | */ |
| 183 | ve.dm.Selection.prototype.getRanges = null; |
| 184 | |
| 185 | /** |
| 186 | * Get the covering linear range for this selection |
| 187 | * |
| 188 | * @abstract |
| 189 | * @return {ve.Range|null} Covering range, if not null |
| 190 | */ |
| 191 | ve.dm.Selection.prototype.getCoveringRange = null; |
| 192 | |
| 193 | /** |
| 194 | * Get the name of the selection type |
| 195 | * |
| 196 | * @return {string} Selection type name |
| 197 | */ |
| 198 | ve.dm.Selection.prototype.getName = function () { |
| 199 | return this.constructor.static.name; |
| 200 | }; |
| 201 | |
| 202 | /** |
| 203 | * Check if two selections are equal |
| 204 | * |
| 205 | * @abstract |
| 206 | * @param {ve.dm.Selection} other |
| 207 | * @return {boolean} Selections are equal |
| 208 | */ |
| 209 | ve.dm.Selection.prototype.equals = null; |
| 210 | |
| 211 | /* Factory */ |
| 212 | |
| 213 | ve.dm.selectionFactory = new OO.Factory(); |