| 1 | /*! |
| 2 | * VisualEditor LinearData class. |
| 3 | * |
| 4 | * Class containing linear data and an hash-value store. |
| 5 | * |
| 6 | * @copyright See AUTHORS.txt |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Generic linear data storage |
| 11 | * |
| 12 | * @class |
| 13 | * @constructor |
| 14 | * @param {ve.dm.HashValueStore} store Hash-value store |
| 15 | * @param {Array} [data] Linear data |
| 16 | */ |
| 17 | ve.dm.LinearData = function VeDmLinearData( store, data ) { |
| 18 | this.store = store; |
| 19 | this.data = data ? ve.deepFreeze( data, true ) : []; |
| 20 | }; |
| 21 | |
| 22 | /* Inheritance */ |
| 23 | |
| 24 | OO.initClass( ve.dm.LinearData ); |
| 25 | |
| 26 | /* Static Methods */ |
| 27 | |
| 28 | /** |
| 29 | * Get the type of an element |
| 30 | * |
| 31 | * This will return the same string for close and open elements. |
| 32 | * |
| 33 | * @param {Object} item Element item |
| 34 | * @return {string} Type of the element |
| 35 | */ |
| 36 | ve.dm.LinearData.static.getType = function ( item ) { |
| 37 | return this.isCloseElementData( item ) ? item.type.slice( 1 ) : item.type; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * Check if data item is an element. |
| 42 | * |
| 43 | * This method assumes that any value that has a type property that's a string is an element object. |
| 44 | * |
| 45 | * Element data: |
| 46 | * |
| 47 | * <heading> a </heading> <paragraph> b c <img></img> </paragraph> |
| 48 | * ^ . ^ ^ . . ^ ^ ^ . |
| 49 | * |
| 50 | * @param {Object|Array|string} item Linear data item |
| 51 | * @return {boolean} Item is an element |
| 52 | */ |
| 53 | ve.dm.LinearData.static.isElementData = function ( item ) { |
| 54 | // Data exists and appears to be an element |
| 55 | return item !== undefined && typeof item.type === 'string'; |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * Checks if data item is an open element. |
| 60 | * |
| 61 | * @param {Object} item Element item |
| 62 | * @return {boolean} Item is an open element |
| 63 | */ |
| 64 | ve.dm.LinearData.static.isOpenElementData = function ( item ) { |
| 65 | return this.isElementData( item ) && item.type.charAt( 0 ) !== '/'; |
| 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * Checks if data item is a close element. |
| 70 | * |
| 71 | * @param {Object} item Element item |
| 72 | * @return {boolean} Item is a close element |
| 73 | */ |
| 74 | ve.dm.LinearData.static.isCloseElementData = function ( item ) { |
| 75 | return this.isElementData( item ) && item.type.charAt( 0 ) === '/'; |
| 76 | }; |
| 77 | |
| 78 | /* Methods */ |
| 79 | |
| 80 | /** |
| 81 | * Gets linear data from a specified index, or all data if no index specified |
| 82 | * |
| 83 | * @param {number} [offset] Offset to get data from |
| 84 | * @return {Object|Array} Data from index, or all data (by reference) |
| 85 | */ |
| 86 | ve.dm.LinearData.prototype.getData = function ( offset ) { |
| 87 | return offset === undefined ? this.data : this.data[ offset ]; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Sets linear data at a specified index |
| 92 | * |
| 93 | * @param {number} offset Offset to set data at |
| 94 | * @param {Object|string} value Value to store |
| 95 | */ |
| 96 | ve.dm.LinearData.prototype.setData = function ( offset, value ) { |
| 97 | this.data[ offset ] = typeof value === 'object' ? ve.deepFreeze( value ) : value; |
| 98 | // this.data[ offset ] = value; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Modify an existing object in the linear model |
| 103 | * |
| 104 | * As objects in the model and immutable, this creates |
| 105 | * a clone which is passed to a callback function for |
| 106 | * modification, then the clone is inserted into the model. |
| 107 | * |
| 108 | * @param {number} offset Offset to modify data at |
| 109 | * @param {Function} modify Modification function. First argument is the data element to modify. |
| 110 | */ |
| 111 | ve.dm.LinearData.prototype.modifyData = function ( offset, modify ) { |
| 112 | // Unfreeze object by creating a new copy |
| 113 | const newItem = ve.copy( this.getData( offset ) ); |
| 114 | // Modify via callback |
| 115 | modify( newItem ); |
| 116 | // Insert into linear model, re-freezing it |
| 117 | this.setData( offset, newItem ); |
| 118 | }; |
| 119 | |
| 120 | /** |
| 121 | * Push data to the end of the array |
| 122 | * |
| 123 | * @param {...Object} [values] Values to store |
| 124 | * @return {number} The new length of the linear data |
| 125 | */ |
| 126 | ve.dm.LinearData.prototype.push = function ( ...values ) { |
| 127 | return this.data.push( ...values ); |
| 128 | }; |
| 129 | |
| 130 | /** |
| 131 | * Gets length of the linear data |
| 132 | * |
| 133 | * @return {number} Length of the linear data |
| 134 | */ |
| 135 | ve.dm.LinearData.prototype.getLength = function () { |
| 136 | return this.getData().length; |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * Gets the hash-value store |
| 141 | * |
| 142 | * @return {ve.dm.HashValueStore} The hash-value store |
| 143 | */ |
| 144 | ve.dm.LinearData.prototype.getStore = function () { |
| 145 | return this.store; |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * Slice linear data |
| 150 | * |
| 151 | * @param {number} begin Index to begin at |
| 152 | * @param {number} [end] Index to end at |
| 153 | * @return {Array} One-level deep copy of sliced range |
| 154 | */ |
| 155 | ve.dm.LinearData.prototype.slice = function ( begin, end ) { |
| 156 | return this.data.slice( begin, end ); |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * Slice linear data and return new LinearData object containing result |
| 161 | * |
| 162 | * @param {number} begin Index to begin at |
| 163 | * @param {number} [end] Index to end at |
| 164 | * @return {ve.dm.LinearData} LinearData object containing one-level deep copy of sliced range |
| 165 | */ |
| 166 | ve.dm.LinearData.prototype.sliceObject = function ( begin, end ) { |
| 167 | return new this.constructor( this.getStore(), this.slice( begin, end ) ); |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * Splice linear data |
| 172 | * |
| 173 | * @param {number} index Splice from |
| 174 | * @param {number} deleteCount Items to be removed |
| 175 | * @param {...Object} [elements] Items to be inserted |
| 176 | * @return {Array} Elements removed |
| 177 | */ |
| 178 | ve.dm.LinearData.prototype.splice = function ( index, deleteCount, ...elements ) { |
| 179 | return this.data.splice( index, deleteCount, ...elements ); |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Splice linear data and return new LinearData object containing result |
| 184 | * |
| 185 | * @param {number} index Splice from |
| 186 | * @param {number} deleteCount Items to be removed |
| 187 | * @param {...Object} [elements] Items to be inserted |
| 188 | * @return {ve.dm.LinearData} LinearData object containing elements removed |
| 189 | */ |
| 190 | ve.dm.LinearData.prototype.spliceObject = function ( index, deleteCount, ...elements ) { |
| 191 | return new this.constructor( this.getStore(), this.splice( index, deleteCount, ...elements ) ); |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * Returns ve.batchSplice of linear data |
| 196 | * |
| 197 | * @see ve.batchSplice |
| 198 | * @param {number} offset |
| 199 | * @param {number} remove |
| 200 | * @param {Array} data |
| 201 | * @return {Array} |
| 202 | */ |
| 203 | ve.dm.LinearData.prototype.batchSplice = function ( offset, remove, data ) { |
| 204 | return ve.batchSplice( this.getData(), offset, remove, data ); |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * Returns ve.batchSplice of linear data, wrapped in a LinearData object |
| 209 | * |
| 210 | * @see ve.batchSplice |
| 211 | * @param {number} offset |
| 212 | * @param {number} remove |
| 213 | * @param {Array} data |
| 214 | * @return {ve.dm.LinearData} |
| 215 | */ |
| 216 | ve.dm.LinearData.prototype.batchSpliceObject = function ( offset, remove, data ) { |
| 217 | return new this.constructor( |
| 218 | this.getStore(), |
| 219 | this.batchSplice( offset, remove, data ) |
| 220 | ); |
| 221 | }; |
| 222 | |
| 223 | /** |
| 224 | * Get a slice or copy of the provided data. |
| 225 | * |
| 226 | * @param {ve.Range} [range] Range of data to get, all data will be given by default |
| 227 | * @param {boolean} [deep=false] Whether to return a deep copy (WARNING! This may be very slow) |
| 228 | * @return {Array} Slice or copy of data |
| 229 | */ |
| 230 | ve.dm.LinearData.prototype.getDataSlice = function ( range, deep ) { |
| 231 | const length = this.getLength(); |
| 232 | let end, |
| 233 | start = 0; |
| 234 | if ( range !== undefined ) { |
| 235 | start = Math.max( 0, Math.min( length, range.start ) ); |
| 236 | end = Math.max( 0, Math.min( length, range.end ) ); |
| 237 | } |
| 238 | const data = this.slice( start, end ); |
| 239 | // Return either the slice or a deep copy of the slice |
| 240 | return deep ? ve.copy( data ) : data; |
| 241 | }; |
| 242 | |
| 243 | /* |
| 244 | * Clone the data, with a deep copy of the data. |
| 245 | * |
| 246 | * @return {ve.dm.LinearData} Clone of this object |
| 247 | */ |
| 248 | ve.dm.LinearData.prototype.clone = function () { |
| 249 | return new this.constructor( |
| 250 | this.getStore(), |
| 251 | ve.copy( this.data ) |
| 252 | ); |
| 253 | }; |