2013-03-15 04:07:23 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MetaList class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel meta item.
|
|
|
|
*
|
|
|
|
* @class
|
2013-10-15 11:58:04 +00:00
|
|
|
* @mixins OO.EventEmitter
|
2013-05-01 22:21:32 +00:00
|
|
|
*
|
2013-03-15 04:07:23 +00:00
|
|
|
* @constructor
|
2013-03-21 01:35:38 +00:00
|
|
|
* @param {ve.dm.Surface} surface Surface model
|
2013-03-15 04:07:23 +00:00
|
|
|
*/
|
2013-03-21 01:35:38 +00:00
|
|
|
ve.dm.MetaList = function VeDmMetaList( surface ) {
|
2013-03-15 04:07:23 +00:00
|
|
|
var i, j, jlen, metadata, item, group;
|
2013-05-01 22:21:32 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.EventEmitter.call( this );
|
2013-03-15 04:07:23 +00:00
|
|
|
|
|
|
|
// Properties
|
2013-03-21 01:35:38 +00:00
|
|
|
this.surface = surface;
|
|
|
|
this.document = surface.getDocument();
|
2013-03-15 04:07:23 +00:00
|
|
|
this.groups = {};
|
|
|
|
this.items = [];
|
|
|
|
|
|
|
|
// Event handlers
|
2013-05-01 22:21:32 +00:00
|
|
|
this.document.connect( this, { 'transact': 'onTransact' } );
|
2013-03-15 04:07:23 +00:00
|
|
|
|
|
|
|
// Populate from document
|
|
|
|
metadata = this.document.getMetadata();
|
|
|
|
for ( i in metadata ) {
|
|
|
|
if ( metadata.hasOwnProperty( i ) && ve.isArray( metadata[i] ) ) {
|
|
|
|
for ( j = 0, jlen = metadata[i].length; j < jlen; j++ ) {
|
|
|
|
item = ve.dm.metaItemFactory.createFromElement( metadata[i][j] );
|
|
|
|
group = this.groups[item.getGroup()];
|
|
|
|
if ( !group ) {
|
|
|
|
group = this.groups[item.getGroup()] = [];
|
|
|
|
}
|
|
|
|
item.attach( this, Number( i ), j );
|
|
|
|
group.push( item );
|
|
|
|
this.items.push( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-15 11:58:04 +00:00
|
|
|
OO.mixinClass( ve.dm.MetaList, OO.EventEmitter );
|
2013-03-15 04:07:23 +00:00
|
|
|
|
2013-03-22 04:53:50 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event insert
|
|
|
|
* @param {ve.dm.MetaItem} item Item that was inserted
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event remove
|
|
|
|
* @param {ve.dm.MetaItem} item Item that was removed
|
|
|
|
* @param {Number} offset Linear model offset that the item was at
|
|
|
|
* @param {Number} index Index within that offset the item was at
|
|
|
|
*/
|
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler for transactions on the document.
|
|
|
|
*
|
|
|
|
* When a transaction occurs, update this list to account for it:
|
|
|
|
* - insert items for new metadata that was inserted
|
|
|
|
* - remove items for metadata that was removed
|
|
|
|
* - translate offsets and recompute indices for metadata that has shifted
|
|
|
|
* @param {ve.dm.Transaction} tx Transaction that was applied to the document
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires insert
|
|
|
|
* @fires remove
|
2013-03-15 04:07:23 +00:00
|
|
|
*/
|
Get rid of 'reversed' flag on transactions
The way we implemented undoing transactions was horrible. We'd process
the original transaction, but with a reversed=true flag. That meant we
had to keep track of the 'reversed' flag everywhere, and use ternaries
like insert = reversed ? op.remove : op.insert; all over the place to
access transaction operations. Redo then worked by reapplying the
transaction. We would verify that this was OK by tracking whether the
transaction was in an applied state or an undone state.
This commit makes it so every transaction can only be applied once. To
undo, you obtain a mirror image of the transaction with tx.reverse(),
then apply that. To redo, you clone the original transaction with
tx.clone() and apply that. All the code that had to use ternaries to
check whether the transaction was being applied in reverse or not is
gone now, because you can only apply a given transaction forwards,
never in reverse.
Bonus:
* Make ve.dm.Document's .completeHistory a simple array of
transactions, rather than transaction/boolean pairs
* In the protection of double application test, clone the example
document properly; it modified ve.dm.example.data, which was "fine"
because it ran .commit() and .rollback() the same number of times
Change-Id: I3050c5430be4a12510f22e20853560b92acebb67
2013-10-03 00:43:56 +00:00
|
|
|
ve.dm.MetaList.prototype.onTransact = function ( tx ) {
|
2013-09-04 21:08:55 +00:00
|
|
|
var i, ilen, j, jlen, k, klen, item, ins, rm, insMeta, rmMeta,
|
|
|
|
numItems = this.items.length,
|
|
|
|
itemIndex = 0, // Current index into this.items
|
|
|
|
offset = 0, // Current pre-transaction offset
|
|
|
|
newOffset = 0, // Current post-transaction offset
|
|
|
|
index = 0, // Current pre-transaction index
|
|
|
|
newIndex = 0, // Current post-transaction index
|
|
|
|
// Array of items that should appear in this.items after we're done. This includes newly
|
|
|
|
// inserted items as well as existing items that aren't being removed.
|
|
|
|
// [ { item: ve.dm.MetaItem, offset: offset to move to, index: index to move to } ]
|
|
|
|
newItems = [],
|
|
|
|
removedItems = [], // Array of items that should be removed from this.items
|
|
|
|
events = [], // Array of events that we should emit when we're done
|
|
|
|
ops = tx.getOperations();
|
|
|
|
|
|
|
|
// Go through the transaction operations and plan out where to add, remove and move items. We
|
|
|
|
// don't actually touch this.items yet, otherwise we 1) get it out of order which breaks
|
|
|
|
// findItem() and 2) lose information about what the pre-transaction state of this.items was.
|
2013-03-15 04:07:23 +00:00
|
|
|
for ( i = 0, ilen = ops.length; i < ilen; i++ ) {
|
|
|
|
switch ( ops[i].type ) {
|
|
|
|
case 'retain':
|
2013-09-04 21:08:55 +00:00
|
|
|
// Advance itemIndex through the retain and update items we encounter along the way
|
|
|
|
for ( ;
|
|
|
|
itemIndex < numItems && this.items[itemIndex].offset < offset + ops[i].length;
|
|
|
|
itemIndex++
|
|
|
|
) {
|
|
|
|
// Plan to move this item to the post-transaction offset and index
|
|
|
|
newItems.push( {
|
|
|
|
'item': this.items[itemIndex],
|
|
|
|
'offset': this.items[itemIndex].offset + newOffset - offset,
|
|
|
|
'index': this.items[itemIndex].offset === offset ?
|
|
|
|
// Adjust index for insertions or removals that happened at this offset
|
|
|
|
newIndex - index + this.items[itemIndex].index :
|
|
|
|
// Offset is retained over completely, don't adjust index
|
|
|
|
this.items[itemIndex].index } );
|
|
|
|
}
|
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
offset += ops[i].length;
|
2013-04-06 16:45:26 +00:00
|
|
|
newOffset += ops[i].length;
|
2013-03-15 04:07:23 +00:00
|
|
|
index = 0;
|
2013-09-04 21:08:55 +00:00
|
|
|
newIndex = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'retainMetadata':
|
|
|
|
// Advance itemIndex through the retain and update items we encounter along the way
|
|
|
|
for ( ;
|
|
|
|
itemIndex < numItems && this.items[itemIndex].offset === offset &&
|
|
|
|
this.items[itemIndex].index < index + ops[i].length;
|
|
|
|
itemIndex++
|
|
|
|
) {
|
|
|
|
newItems.push( {
|
|
|
|
'item': this.items[itemIndex],
|
|
|
|
'offset': newOffset,
|
|
|
|
'index': this.items[itemIndex].index + newIndex - index
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
index += ops[i].length;
|
|
|
|
newIndex += ops[i].length;
|
2013-03-15 04:07:23 +00:00
|
|
|
break;
|
2013-09-04 21:08:55 +00:00
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
case 'replace':
|
Get rid of 'reversed' flag on transactions
The way we implemented undoing transactions was horrible. We'd process
the original transaction, but with a reversed=true flag. That meant we
had to keep track of the 'reversed' flag everywhere, and use ternaries
like insert = reversed ? op.remove : op.insert; all over the place to
access transaction operations. Redo then worked by reapplying the
transaction. We would verify that this was OK by tracking whether the
transaction was in an applied state or an undone state.
This commit makes it so every transaction can only be applied once. To
undo, you obtain a mirror image of the transaction with tx.reverse(),
then apply that. To redo, you clone the original transaction with
tx.clone() and apply that. All the code that had to use ternaries to
check whether the transaction was being applied in reverse or not is
gone now, because you can only apply a given transaction forwards,
never in reverse.
Bonus:
* Make ve.dm.Document's .completeHistory a simple array of
transactions, rather than transaction/boolean pairs
* In the protection of double application test, clone the example
document properly; it modified ve.dm.example.data, which was "fine"
because it ran .commit() and .rollback() the same number of times
Change-Id: I3050c5430be4a12510f22e20853560b92acebb67
2013-10-03 00:43:56 +00:00
|
|
|
ins = ops[i].insert;
|
|
|
|
rm = ops[i].remove;
|
2013-09-04 21:08:55 +00:00
|
|
|
if ( ops[i].removeMetadata !== undefined ) {
|
Get rid of 'reversed' flag on transactions
The way we implemented undoing transactions was horrible. We'd process
the original transaction, but with a reversed=true flag. That meant we
had to keep track of the 'reversed' flag everywhere, and use ternaries
like insert = reversed ? op.remove : op.insert; all over the place to
access transaction operations. Redo then worked by reapplying the
transaction. We would verify that this was OK by tracking whether the
transaction was in an applied state or an undone state.
This commit makes it so every transaction can only be applied once. To
undo, you obtain a mirror image of the transaction with tx.reverse(),
then apply that. To redo, you clone the original transaction with
tx.clone() and apply that. All the code that had to use ternaries to
check whether the transaction was being applied in reverse or not is
gone now, because you can only apply a given transaction forwards,
never in reverse.
Bonus:
* Make ve.dm.Document's .completeHistory a simple array of
transactions, rather than transaction/boolean pairs
* In the protection of double application test, clone the example
document properly; it modified ve.dm.example.data, which was "fine"
because it ran .commit() and .rollback() the same number of times
Change-Id: I3050c5430be4a12510f22e20853560b92acebb67
2013-10-03 00:43:56 +00:00
|
|
|
insMeta = ops[i].insertMetadata;
|
|
|
|
rmMeta = ops[i].removeMetadata;
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
// Process removed metadata
|
|
|
|
for ( ;
|
|
|
|
itemIndex < numItems &&
|
|
|
|
this.items[itemIndex].offset < offset + rmMeta.length;
|
|
|
|
itemIndex++
|
|
|
|
) {
|
|
|
|
removedItems.push( this.items[itemIndex] );
|
2013-08-30 22:24:43 +00:00
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
// Process inserted metadata
|
|
|
|
for ( j = 0, jlen = insMeta.length; j < jlen; j++ ) {
|
|
|
|
if ( insMeta[j] ) {
|
|
|
|
for ( k = 0, klen = insMeta[j].length; k < klen; k++ ) {
|
|
|
|
item = ve.dm.metaItemFactory.createFromElement( insMeta[j][k] );
|
|
|
|
newItems.push( {
|
|
|
|
'item': item,
|
|
|
|
'offset': newOffset + j,
|
|
|
|
'index': k
|
|
|
|
} );
|
2013-08-30 22:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-04 21:08:55 +00:00
|
|
|
// No metadata handling specified, which means we just have to deal with offset
|
|
|
|
// adjustments, same as a retain
|
|
|
|
for ( ;
|
|
|
|
itemIndex < numItems &&
|
|
|
|
this.items[itemIndex].offset < offset + rm.length;
|
|
|
|
itemIndex++
|
|
|
|
) {
|
|
|
|
newItems.push( {
|
|
|
|
'item': this.items[itemIndex],
|
|
|
|
'offset': this.items[itemIndex].offset + newOffset - offset,
|
|
|
|
'index': this.items[itemIndex].index
|
|
|
|
} );
|
2013-04-06 16:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
offset += rm.length;
|
|
|
|
newOffset += ins.length;
|
2013-03-15 04:07:23 +00:00
|
|
|
break;
|
2013-09-04 21:08:55 +00:00
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
case 'replaceMetadata':
|
Get rid of 'reversed' flag on transactions
The way we implemented undoing transactions was horrible. We'd process
the original transaction, but with a reversed=true flag. That meant we
had to keep track of the 'reversed' flag everywhere, and use ternaries
like insert = reversed ? op.remove : op.insert; all over the place to
access transaction operations. Redo then worked by reapplying the
transaction. We would verify that this was OK by tracking whether the
transaction was in an applied state or an undone state.
This commit makes it so every transaction can only be applied once. To
undo, you obtain a mirror image of the transaction with tx.reverse(),
then apply that. To redo, you clone the original transaction with
tx.clone() and apply that. All the code that had to use ternaries to
check whether the transaction was being applied in reverse or not is
gone now, because you can only apply a given transaction forwards,
never in reverse.
Bonus:
* Make ve.dm.Document's .completeHistory a simple array of
transactions, rather than transaction/boolean pairs
* In the protection of double application test, clone the example
document properly; it modified ve.dm.example.data, which was "fine"
because it ran .commit() and .rollback() the same number of times
Change-Id: I3050c5430be4a12510f22e20853560b92acebb67
2013-10-03 00:43:56 +00:00
|
|
|
insMeta = ops[i].insert;
|
|
|
|
rmMeta = ops[i].remove;
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
// Process removed items
|
|
|
|
for ( ;
|
|
|
|
itemIndex < numItems && this.items[itemIndex].offset === offset &&
|
|
|
|
this.items[itemIndex].index < index + rmMeta.length;
|
|
|
|
itemIndex++
|
|
|
|
) {
|
|
|
|
removedItems.push( this.items[itemIndex] );
|
2013-03-15 04:07:23 +00:00
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
// Process inserted items
|
|
|
|
for ( j = 0, jlen = insMeta.length; j < jlen; j++ ) {
|
|
|
|
item = ve.dm.metaItemFactory.createFromElement( insMeta[j] );
|
|
|
|
newItems.push( { 'item': item, 'offset': newOffset, 'index': newIndex + j } );
|
2013-03-15 04:07:23 +00:00
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
index += rmMeta.length;
|
|
|
|
newIndex += insMeta.length;
|
2013-03-15 04:07:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
// Update the remaining items that the transaction didn't touch or retain over
|
|
|
|
for ( ; itemIndex < numItems; itemIndex++ ) {
|
|
|
|
newItems.push( {
|
|
|
|
'item': this.items[itemIndex],
|
|
|
|
'offset': this.items[itemIndex].offset + newOffset - offset,
|
|
|
|
'index': this.items[itemIndex].offset === offset ?
|
|
|
|
newIndex - index + this.items[itemIndex].index :
|
|
|
|
this.items[itemIndex].index
|
|
|
|
} );
|
|
|
|
}
|
2013-03-15 04:07:23 +00:00
|
|
|
|
2013-09-04 21:08:55 +00:00
|
|
|
// Process the changes, and queue up events. We emit the events at the end when the MetaList
|
|
|
|
// is back in a consistent state
|
|
|
|
|
|
|
|
// Remove removed items
|
|
|
|
for ( i = 0, ilen = removedItems.length; i < ilen; i++ ) {
|
|
|
|
this.deleteRemovedItem( removedItems[i].offset, removedItems[i].index );
|
|
|
|
events.push( [
|
2013-09-28 00:08:29 +00:00
|
|
|
'remove', removedItems[i], removedItems[i].offset, removedItems[i].index
|
2013-09-04 21:08:55 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move moved items (these appear as inserted items that are already attached)
|
|
|
|
for ( i = 0, ilen = newItems.length; i < ilen; i++ ) {
|
|
|
|
if ( newItems[i].item.isAttached() ) {
|
|
|
|
if ( newItems[i].offset !== newItems[i].item.offset || newItems[i].index !== newItems[i].item.index ) {
|
|
|
|
this.deleteRemovedItem( newItems[i].item.offset, newItems[i].item.index );
|
2013-11-27 17:29:15 +00:00
|
|
|
newItems[i].preExisting = true;
|
2013-04-06 16:45:26 +00:00
|
|
|
}
|
2013-03-15 04:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-22 04:53:50 +00:00
|
|
|
|
2013-09-04 21:08:55 +00:00
|
|
|
// Insert new items
|
|
|
|
for ( i = 0, ilen = newItems.length; i < ilen; i++ ) {
|
|
|
|
if ( !newItems[i].item.isAttached() ) {
|
|
|
|
this.addInsertedItem( newItems[i].offset, newItems[i].index, newItems[i].item );
|
2013-11-27 17:29:15 +00:00
|
|
|
if ( !newItems[i].preExisting ) {
|
|
|
|
events.push( [ 'insert', newItems[i].item ] );
|
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
}
|
2013-03-22 04:53:50 +00:00
|
|
|
}
|
2013-09-04 21:08:55 +00:00
|
|
|
|
|
|
|
// Emit events
|
|
|
|
for ( i = 0, ilen = events.length; i < ilen; i++ ) {
|
|
|
|
this.emit.apply( this, events[i] );
|
2013-03-22 04:53:50 +00:00
|
|
|
}
|
2013-03-15 04:07:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find an item by its offset, index and group.
|
|
|
|
*
|
|
|
|
* This function is mostly for internal usage.
|
|
|
|
*
|
|
|
|
* @param {number} offset Offset in the linear model
|
|
|
|
* @param {number} index Index in the metadata array associated with that offset
|
2013-03-20 17:03:27 +00:00
|
|
|
* @param {string} [group] Group to search in. If not set, search in all groups
|
|
|
|
* @param {boolean} [forInsertion] If the item is not found, return the index where it should have
|
2013-07-25 02:02:50 +00:00
|
|
|
* been rather than null
|
2013-03-20 17:03:27 +00:00
|
|
|
* @returns {number|null} Index into this.items or this.groups[group] where the item was found, or
|
2013-07-25 02:02:50 +00:00
|
|
|
* null if not found
|
2013-03-15 04:07:23 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.findItem = function ( offset, index, group, forInsertion ) {
|
|
|
|
// Binary search for the item
|
|
|
|
var mid, items = typeof group === 'string' ? ( this.groups[group] || [] ) : this.items,
|
|
|
|
left = 0, right = items.length;
|
|
|
|
while ( left < right ) {
|
|
|
|
// Equivalent to Math.floor( ( left + right ) / 2 ) but much faster in V8
|
|
|
|
/*jshint bitwise:false */
|
|
|
|
mid = ( left + right ) >> 1;
|
|
|
|
if ( items[mid].getOffset() === offset && items[mid].getIndex() === index ) {
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
if ( items[mid].getOffset() < offset || (
|
|
|
|
items[mid].getOffset() === offset && items[mid].getIndex() < index
|
|
|
|
) ) {
|
|
|
|
left = mid + 1;
|
|
|
|
} else {
|
|
|
|
right = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return forInsertion ? left : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item at a given offset and index, if there is one.
|
2013-03-20 17:03:27 +00:00
|
|
|
*
|
2013-03-15 04:07:23 +00:00
|
|
|
* @param {number} offset Offset in the linear model
|
|
|
|
* @param {number} index Index in the metadata array
|
|
|
|
* @returns {ve.dm.MetaItem|null} The item at (offset,index), or null if not found
|
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.getItemAt = function ( offset, index ) {
|
|
|
|
var at = this.findItem( offset, index );
|
|
|
|
return at === null ? null : this.items[at];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all items in a group.
|
|
|
|
*
|
|
|
|
* This function returns a shallow copy, so the array isn't returned by reference but the items
|
|
|
|
* themselves are.
|
|
|
|
*
|
|
|
|
* @param {string} group Group
|
|
|
|
* @returns {ve.dm.MetaItem[]} Array of items in the group (shallow copy)
|
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.getItemsInGroup = function ( group ) {
|
|
|
|
return ( this.groups[group] || [] ).slice( 0 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all items in the list.
|
|
|
|
*
|
|
|
|
* This function returns a shallow copy, so the array isn't returned by reference but the items
|
|
|
|
* themselves are.
|
|
|
|
*
|
|
|
|
* @returns {ve.dm.MetaItem[]} Array of items in the list
|
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.getAllItems = function () {
|
|
|
|
return this.items.slice( 0 );
|
|
|
|
};
|
|
|
|
|
2013-03-21 01:35:38 +00:00
|
|
|
/**
|
|
|
|
* Insert new metadata into the document. This builds and processes a transaction that inserts
|
|
|
|
* metadata into the document.
|
2013-11-18 08:37:05 +00:00
|
|
|
*
|
|
|
|
* Pass a plain object rather than a MetaItem into this function unless you know what you're doing.
|
2013-03-21 01:35:38 +00:00
|
|
|
* @param {Object|ve.dm.MetaItem} meta Metadata element (or MetaItem) to insert
|
2013-07-01 23:01:34 +00:00
|
|
|
* @param {Number} [offset] Offset to insert the new metadata, or undefined to add to the end
|
|
|
|
* @param {Number} [index] Index to insert the new metadata, or undefined to add to the end
|
2013-03-21 01:35:38 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.insertMeta = function ( meta, offset, index ) {
|
|
|
|
var tx;
|
|
|
|
if ( meta instanceof ve.dm.MetaItem ) {
|
|
|
|
meta = meta.getElement();
|
|
|
|
}
|
2013-05-03 22:17:35 +00:00
|
|
|
if ( offset === undefined ) {
|
2013-05-11 00:02:07 +00:00
|
|
|
offset = this.document.data.getLength();
|
2013-07-01 23:01:34 +00:00
|
|
|
}
|
|
|
|
if ( index === undefined ) {
|
2013-03-20 22:35:05 +00:00
|
|
|
index = ( this.document.metadata.getData( offset ) || [] ).length;
|
2013-03-21 01:35:38 +00:00
|
|
|
}
|
|
|
|
tx = ve.dm.Transaction.newFromMetadataInsertion( this.document, offset, index, [ meta ] );
|
|
|
|
this.surface.change( tx );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a meta item from the document. This builds and processes a transaction that removes the
|
|
|
|
* associated metadata from the document.
|
|
|
|
* @param {ve.dm.MetaItem} item Item to remove
|
|
|
|
*/
|
|
|
|
ve.dm.MetaList.prototype.removeMeta = function ( item ) {
|
|
|
|
var tx;
|
|
|
|
tx = ve.dm.Transaction.newFromMetadataRemoval(
|
|
|
|
this.document,
|
|
|
|
item.getOffset(),
|
|
|
|
new ve.Range( item.getIndex(), item.getIndex() + 1 )
|
|
|
|
);
|
|
|
|
this.surface.change( tx );
|
|
|
|
};
|
|
|
|
|
2013-03-15 04:07:23 +00:00
|
|
|
/**
|
|
|
|
* Insert an item at a given offset and index in response to a transaction.
|
|
|
|
*
|
2013-03-21 01:35:38 +00:00
|
|
|
* This function is for internal usage by onTransact(). To actually insert an item, use
|
2013-11-13 02:08:08 +00:00
|
|
|
* insertMeta().
|
2013-03-15 04:07:23 +00:00
|
|
|
*
|
|
|
|
* @param {number} offset Offset in the linear model of the new item
|
2013-05-06 11:34:32 +00:00
|
|
|
* @param {number} index Index of the new item in the metadata array at offset
|
2013-03-15 04:07:23 +00:00
|
|
|
* @param {ve.dm.MetaItem} item Item object
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires insert
|
2013-03-15 04:07:23 +00:00
|
|
|
*/
|
2013-03-21 01:35:38 +00:00
|
|
|
ve.dm.MetaList.prototype.addInsertedItem = function ( offset, index, item ) {
|
2013-03-15 04:07:23 +00:00
|
|
|
var group = item.getGroup(), at = this.findItem( offset, index, null, true );
|
|
|
|
this.items.splice( at, 0, item );
|
|
|
|
if ( this.groups[group] ) {
|
|
|
|
at = this.findItem( offset, index, group, true );
|
|
|
|
this.groups[group].splice( at, 0, item );
|
|
|
|
} else {
|
|
|
|
this.groups[group] = [ item ];
|
|
|
|
}
|
|
|
|
item.attach( this, offset, index );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an item in response to a transaction.
|
|
|
|
*
|
2013-03-21 01:35:38 +00:00
|
|
|
* This function is for internal usage by onTransact(). To actually remove an item, use
|
|
|
|
* removeItem().
|
2013-03-15 04:07:23 +00:00
|
|
|
*
|
|
|
|
* @param {number} offset Offset in the linear model of the item
|
|
|
|
* @param {number} index Index of the item in the metadata array at offset
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires remove
|
2013-03-15 04:07:23 +00:00
|
|
|
*/
|
2013-03-21 01:35:38 +00:00
|
|
|
ve.dm.MetaList.prototype.deleteRemovedItem = function ( offset, index ) {
|
2013-03-15 04:07:23 +00:00
|
|
|
var item, group, at = this.findItem( offset, index );
|
|
|
|
if ( at === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
item = this.items[at];
|
|
|
|
group = item.getGroup();
|
|
|
|
this.items.splice( at, 1 );
|
|
|
|
at = this.findItem( offset, index, group );
|
|
|
|
if ( at !== null ) {
|
|
|
|
this.groups[group].splice( at, 1 );
|
|
|
|
}
|
|
|
|
item.detach( this );
|
2013-04-11 21:49:17 +00:00
|
|
|
return item;
|
2013-03-15 04:07:23 +00:00
|
|
|
};
|