/*! * VisualEditor UserInterface IndentationAction class. * * @copyright See AUTHORS.txt */ /** * Indentation action. * * @class * @extends ve.ui.Action * * @constructor * @param {ve.ui.Surface} surface Surface to act on * @param {string} [source] */ ve.ui.IndentationAction = function VeUiIndentationAction() { // Parent constructor ve.ui.IndentationAction.super.apply( this, arguments ); }; /* Inheritance */ OO.inheritClass( ve.ui.IndentationAction, ve.ui.Action ); /* Static Properties */ ve.ui.IndentationAction.static.name = 'indentation'; ve.ui.IndentationAction.static.methods = [ 'increase', 'decrease' ]; /* Methods */ /** * Indent content. * * @return {boolean} Indentation increase occurred */ ve.ui.IndentationAction.prototype.increase = function () { return this.changeIndentation( 1 ); }; /** * Unindent content. * * @return {boolean} Indentation decrease occurred */ ve.ui.IndentationAction.prototype.decrease = function () { return this.changeIndentation( -1 ); }; /** * Change indentation of content. * * @param {number} indent Indentation change, 1 or -1 * @return {boolean} Indentation decrease occurred */ ve.ui.IndentationAction.prototype.changeIndentation = function ( indent ) { const surfaceModel = this.surface.getModel(), selection = surfaceModel.getSelection(); if ( !( selection instanceof ve.dm.LinearSelection ) ) { return false; } const documentModel = surfaceModel.getDocument(); const groups = documentModel.getCoveredSiblingGroups( selection.getRange() ); const fragments = []; let changed = false; // Build fragments from groups (we need their ranges since the nodes will be rebuilt on change) groups.forEach( ( group ) => { if ( group.grandparent && group.grandparent.getType() === 'list' ) { fragments.push( surfaceModel.getLinearFragment( group.parent.getRange(), true ) ); changed = true; } else if ( group.parent && group.parent.getType() === 'list' ) { // In a slug, the node will be the listItem. fragments.push( surfaceModel.getLinearFragment( group.nodes[ 0 ].getRange(), true ) ); changed = true; } } ); // Process each fragment (their ranges are automatically adjusted on change) fragments.forEach( ( fragment ) => { const listItem = documentModel.getBranchNodeFromOffset( fragment.getSelection().getRange().start ); if ( indent > 0 ) { this.indentListItem( listItem ); } else { this.unindentListItem( listItem ); } } ); return changed; }; /** * Indent a list item. * * @param {ve.dm.ListItemNode} listItem List item to indent * @throws {Error} listItem must be a ve.dm.ListItemNode */ ve.ui.IndentationAction.prototype.indentListItem = function ( listItem ) { // This check should never fail /* istanbul ignore next */ if ( !( listItem instanceof ve.dm.ListItemNode ) ) { throw new Error( 'listItem must be a ve.dm.ListItemNode' ); } /* * Indenting a list item is done as follows: * * 1. Wrap the listItem in a list and a listItem (
  • -->