mediawiki-extensions-Visual.../modules/ve/actions/ve.FormatAction.js
Ed Sanders f506e33718 Change FormatAction.splitAndUnwrap to use the new SurfaceFrament.isolate method
Bug: 45246
Change-Id: I6e10ed87b25150cfc9d8e42c0f0bd3733b08fee7
2013-02-26 00:22:19 +00:00

133 lines
3.7 KiB
JavaScript

/*!
* VisualEditor FormatAction class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Format action.
*
* @class
* @extends ve.Action
* @constructor
* @param {ve.Surface} surface Surface to act on
*/
ve.FormatAction = function VeFormatAction( surface ) {
// Parent constructor
ve.Action.call( this, surface );
};
/* Inheritance */
ve.inheritClass( ve.FormatAction, ve.Action );
/* Static Properties */
/**
* List of allowed methods for this action.
*
* @static
* @property
*/
ve.FormatAction.static.methods = ['convert'];
/* Methods */
/**
* Convert the format of content.
*
* Conversion splits and unwraps all lists and replaces content branch nodes.
*
* TODO: Refactor functionality into {ve.dm.SurfaceFragment}.
*
* @param {string} type
* @param {Object} attributes
*/
ve.FormatAction.prototype.convert = function ( type, attributes ) {
var selected, prevList, firstInList, lastInList, i, contentBranch, listItem, txs,
surfaceView = this.surface.getView(),
surfaceModel = this.surface.getModel(),
selection = surfaceModel.getSelection(),
doc = surfaceModel.getDocument();
if ( type !== 'paragraph' ) {
// We can't have headings or pre's in a list, so if we're trying to convert
// things that are in lists to a heading or a pre, split the list
selected = doc.selectNodes( selection, 'leaves' );
for ( i = 0; i < selected.length; i++ ) {
contentBranch = selected[i].node.isContent() ?
selected[i].node.getParent() :
selected[i].node;
// Check if it's in a list
listItem = contentBranch;
// TODO: Add getMatchingAncestor to ve.dm.Node and use it here
while ( listItem && listItem.getType() !== 'listItem' ) {
listItem = listItem.getParent();
}
if ( !listItem || listItem.getParent() !== prevList ) {
// Not in a list or in a different list
if ( prevList ) {
// Split and unwrap prevList
selection = ve.FormatAction.splitAndUnwrap(
surfaceModel, prevList, firstInList, lastInList, selection
);
// Reset
prevList = firstInList = lastInList = undefined;
}
if ( listItem ) {
prevList = listItem.getParent();
firstInList = listItem;
lastInList = firstInList;
}
} else {
// This node is in the current list
lastInList = listItem;
}
}
if ( prevList ) {
// Split and unwrap prevList
selection = ve.FormatAction.splitAndUnwrap(
surfaceModel, prevList, firstInList, lastInList, selection
);
}
}
txs = ve.dm.Transaction.newFromContentBranchConversion( doc, selection, type, attributes );
surfaceModel.change( txs, selection );
surfaceView.showSelection( selection );
};
/**
* Split a list up by unwrapping some of its items.
*
* TODO: Refactor functionality into {ve.dm.SurfaceFragment}.
*
* @param {ve.dm.Surface} model
* @param {ve.dm.ListNode} list
* @param {ve.dm.ListItemNode} firstItem
* @param {ve.dm.ListItemNode} lastItem
* @returns {ve.Range} selection
*/
ve.FormatAction.splitAndUnwrap = function ( model, list, firstItem, lastItem, selection ) {
var doc = model.getDocument(),
start = firstItem.getOuterRange().start,
end = lastItem.getOuterRange().end,
fragment = new ve.dm.SurfaceFragment( model, new ve.Range( start, end ), true ),
fragmentForSelection = new ve.dm.SurfaceFragment( model, selection, true ),
tx;
fragment.isolate();
selection = fragmentForSelection.getRange();
// Unwrap the list
tx = ve.dm.Transaction.newFromWrap( doc, fragment.getRange(),
[{ 'type': 'list' }], [], [{ 'type': 'listItem' }], []
);
selection = tx.translateRange( selection );
model.change( tx, selection );
return selection;
};
/* Registration */
ve.actionFactory.register( 'format', ve.FormatAction );