mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-12-01 17:36:35 +00:00
5c31d3215b
Document slice only ever contained linear data, with extra functionality to preserve the range. It pre-dated LinearData, but now we should refactor it to reflect its purpose. Change-Id: Ifc908f7526c83a43a51372c8d2494d7260e7facd
59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel SlicedLinearData class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Sliced linear data storage.
|
|
*
|
|
* @abstract
|
|
* @constructor
|
|
* @param {ve.Range} [range] Original context within data
|
|
*/
|
|
ve.dm.SlicedLinearData = function VeDmSlicedLinearData( range ) {
|
|
// Clone data
|
|
this.data = ve.copy( this.data );
|
|
|
|
// Properties
|
|
this.range = range || new ve.Range( 0, this.data.length );
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Get the range representing the original context within the data
|
|
*
|
|
* @returns {ve.Range} Original context within data
|
|
*/
|
|
ve.dm.SlicedLinearData.prototype.getRange = function () {
|
|
return this.range;
|
|
};
|
|
|
|
/**
|
|
* Get the original data excluding any balancing data that was added.
|
|
*
|
|
* @method
|
|
* @param {number} [offset] Offset to get data from
|
|
* @returns {Object|Array} Data from index, or all data (by reference)
|
|
*/
|
|
ve.dm.SlicedLinearData.prototype.getOriginalData = function ( offset ) {
|
|
return offset === undefined ? this.data.slice( this.range.start, this.range.end ) : this.data[this.range.start + offset];
|
|
};
|
|
|
|
/**
|
|
* Run all elements in this document slice through getClonedElement(). This should be done if
|
|
* you intend to insert the sliced data back into the document as a copy of the original data
|
|
* (e.g. for copy and paste).
|
|
*/
|
|
ve.dm.SlicedLinearData.prototype.cloneElements = function () {
|
|
var i, len, node;
|
|
for ( i = 0, len = this.getLength(); i < len; i++ ) {
|
|
if ( this.isOpenElementData( i ) ) {
|
|
node = ve.dm.nodeFactory.create( this.getType( i ), [], this.getData( i ) );
|
|
this.data[i] = node.getClonedElement();
|
|
}
|
|
}
|
|
};
|