Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/actions/ve.ui.IndentationAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface IndentationAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Indentation action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to act on
15 * @param {string} [source]
16 */
17 ve.ui.IndentationAction = function VeUiIndentationAction() {
18 // Parent constructor
19 ve.ui.IndentationAction.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.IndentationAction, ve.ui.Action );
25
26 /* Static Properties */
27
28 ve.ui.IndentationAction.static.name = 'indentation';
29
30 ve.ui.IndentationAction.static.methods = [ 'increase', 'decrease' ];
31
32 /* Methods */
33
34 /**
35 * Indent content.
36 *
37 * @return {boolean} Indentation increase occurred
38 */
39 ve.ui.IndentationAction.prototype.increase = function () {
40 return this.changeIndentation( 1 );
41 };
42
43 /**
44 * Unindent content.
45 *
46 * @return {boolean} Indentation decrease occurred
47 */
48 ve.ui.IndentationAction.prototype.decrease = function () {
49 return this.changeIndentation( -1 );
50 };
51
52 /**
53 * Change indentation of content.
54 *
55 * @param {number} indent Indentation change, 1 or -1
56 * @return {boolean} Indentation decrease occurred
57 */
58 ve.ui.IndentationAction.prototype.changeIndentation = function ( indent ) {
59 const surfaceModel = this.surface.getModel(),
60 selection = surfaceModel.getSelection();
61
62 if ( !( selection instanceof ve.dm.LinearSelection ) ) {
63 return false;
64 }
65
66 const documentModel = surfaceModel.getDocument();
67 const groups = documentModel.getCoveredSiblingGroups( selection.getRange() );
68
69 const fragments = [];
70 let changed = false;
71 // Build fragments from groups (we need their ranges since the nodes will be rebuilt on change)
72 groups.forEach( ( group ) => {
73 if ( group.grandparent && group.grandparent.getType() === 'list' ) {
74 fragments.push( surfaceModel.getLinearFragment( group.parent.getRange(), true ) );
75 changed = true;
76 } else if ( group.parent && group.parent.getType() === 'list' ) {
77 // In a slug, the node will be the listItem.
78 fragments.push( surfaceModel.getLinearFragment( group.nodes[ 0 ].getRange(), true ) );
79 changed = true;
80 }
81 } );
82
83 // Process each fragment (their ranges are automatically adjusted on change)
84 fragments.forEach( ( fragment ) => {
85 const listItem = documentModel.getBranchNodeFromOffset( fragment.getSelection().getRange().start );
86 if ( indent > 0 ) {
87 this.indentListItem( listItem );
88 } else {
89 this.unindentListItem( listItem );
90 }
91 } );
92
93 return changed;
94 };
95
96 /**
97 * Indent a list item.
98 *
99 * @param {ve.dm.ListItemNode} listItem List item to indent
100 * @throws {Error} listItem must be a ve.dm.ListItemNode
101 */
102 ve.ui.IndentationAction.prototype.indentListItem = function ( listItem ) {
103 // This check should never fail
104 /* istanbul ignore next */
105 if ( !( listItem instanceof ve.dm.ListItemNode ) ) {
106 throw new Error( 'listItem must be a ve.dm.ListItemNode' );
107 }
108
109 /*
110 * Indenting a list item is done as follows:
111 *
112 * 1. Wrap the listItem in a list and a listItem (<li> --> <li><ul><li>)
113 * 2. Merge this wrapped listItem into the previous listItem if present
114 * (<li>Previous</li><li><ul><li>This --> <li>Previous<ul><li>This)
115 * 3. If this results in the wrapped list being preceded by another list,
116 * merge those lists.
117 */
118
119 const listType = listItem.getParent().getAttribute( 'style' );
120 const listItemRange = listItem.getOuterRange();
121
122 // CAREFUL: after initializing the variables above, we cannot use the model tree!
123 // The first transaction will cause rebuilds so the nodes we have references to now
124 // will be detached and useless after the first transaction. Instead, inspect
125 // documentModel.data to find out things about the current structure.
126
127 const surfaceModel = this.surface.getModel();
128 // (1) Wrap the listItem in a list and a listItem
129 surfaceModel.getLinearFragment( listItemRange, true )
130 .wrapNodes( [ { type: 'listItem' }, { type: 'list', attributes: { style: listType } } ] );
131
132 const documentModel = surfaceModel.getDocument();
133 // (2) Merge the listItem into the previous listItem (if there is one)
134 if (
135 documentModel.data.getData( listItemRange.start ).type === 'listItem' &&
136 documentModel.data.getData( listItemRange.start - 1 ).type === '/listItem'
137 ) {
138 let mergeStart = listItemRange.start - 1;
139 let mergeEnd = listItemRange.start + 1;
140 // (3) If this results in adjacent lists, merge those too
141 if (
142 documentModel.data.getData( mergeEnd ).type === 'list' &&
143 documentModel.data.getData( mergeStart - 1 ).type === '/list'
144 ) {
145 mergeStart--;
146 mergeEnd++;
147 }
148 surfaceModel.getLinearFragment( new ve.Range( mergeStart, mergeEnd ), true ).removeContent();
149 }
150
151 // TODO If this listItem has a child list, split&unwrap it
152 };
153
154 /**
155 * Unindent a list item.
156 *
157 * TODO: Refactor functionality into {ve.dm.SurfaceFragment}.
158 *
159 * @param {ve.dm.ListItemNode} listItem List item to unindent
160 * @throws {Error} listItem must be a ve.dm.ListItemNode
161 */
162 ve.ui.IndentationAction.prototype.unindentListItem = function ( listItem ) {
163 // This check should never fail
164 /* istanbul ignore next */
165 if ( !( listItem instanceof ve.dm.ListItemNode ) ) {
166 throw new Error( 'listItem must be a ve.dm.ListItemNode' );
167 }
168
169 let tx;
170 const surfaceModel = this.surface.getModel();
171 const documentModel = surfaceModel.getDocument();
172 const fragment = surfaceModel.getLinearFragment( listItem.getOuterRange(), true );
173 const list = listItem.getParent();
174 const listElement = list.getClonedElement();
175 const grandParentType = list.getParent().getType();
176 let listItemRange = listItem.getOuterRange();
177
178 /*
179 * Outdenting a list item is done as follows:
180 * 1. Split the parent list to isolate the listItem in its own list
181 * 1a. Split the list before the listItem if it's not the first child
182 * 1b. Split the list after the listItem if it's not the last child
183 * 2. If this isolated list's parent is not a listItem, unwrap the listItem and the isolated list, and stop.
184 * 3. Split the parent listItem to isolate the list in its own listItem
185 * 3a. Split the listItem before the list if it's not the first child
186 * 3b. Split the listItem after the list if it's not the last child
187 * 4. Unwrap the now-isolated listItem and the isolated list
188 */
189 // TODO: Child list handling, gotta figure that out.
190 // CAREFUL: after initializing the variables above, we cannot use the model tree!
191 // The first transaction will cause rebuilds so the nodes we have references to now
192 // will be detached and useless after the first transaction. Instead, inspect
193 // documentModel.data to find out things about the current structure.
194
195 // (1) Split the listItem into a separate list
196 if ( documentModel.data.getData( listItemRange.start - 1 ).type !== 'list' ) {
197 // (1a) listItem is not the first child, split the list before listItem
198 tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, listItemRange.start,
199 [ { type: '/list' }, listElement ]
200 );
201 surfaceModel.change( tx );
202 // tx.translateRange( listItemRange ) doesn't do what we want
203 listItemRange = listItemRange.translate( 2 );
204 }
205 if ( documentModel.data.getData( listItemRange.end ).type !== '/list' ) {
206 // (1b) listItem is not the last child, split the list after listItem
207 tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, listItemRange.end,
208 [ { type: '/list' }, listElement ]
209 );
210 surfaceModel.change( tx );
211 // listItemRange is not affected by this transaction
212 }
213 let splitListRange = new ve.Range( listItemRange.start - 1, listItemRange.end + 1 );
214
215 if ( grandParentType !== 'listItem' ) {
216 // The user is trying to unindent a list item that's not nested
217 // (2) Unwrap both the list and the listItem, dumping the listItem's contents
218 // into the list's parent
219 surfaceModel.getLinearFragment( new ve.Range( listItemRange.start + 1, listItemRange.end - 1 ), true )
220 .unwrapNodes( 2 );
221
222 // Ensure paragraphs are not generated paragraphs now that they are not in a list
223 const children = fragment.getSiblingNodes();
224 for ( let i = 0, length = children.length; i < length; i++ ) {
225 const child = children[ i ].node;
226 if ( child.type === 'paragraph' && ve.getProp( child.element, 'internal', 'generated' ) ) {
227 surfaceModel.getLinearFragment( child.getOuterRange(), true ).convertNodes( 'paragraph', child.getAttributes(), {} );
228 }
229 }
230 } else {
231 // (3) Split the list away from parentListItem into its own listItem
232 // TODO factor common split logic somehow?
233 if ( documentModel.data.getData( splitListRange.start - 1 ).type !== 'listItem' ) {
234 // (3a) Split parentListItem before list
235 tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, splitListRange.start,
236 [ { type: '/listItem' }, { type: 'listItem' } ]
237 );
238 surfaceModel.change( tx );
239 // tx.translateRange( splitListRange ) doesn't do what we want
240 splitListRange = splitListRange.translate( 2 );
241 }
242 if ( documentModel.data.getData( splitListRange.end ).type !== '/listItem' ) {
243 // (3b) Split parentListItem after list
244 tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, splitListRange.end,
245 [ { type: '/listItem' }, { type: 'listItem' } ]
246 );
247 surfaceModel.change( tx );
248 // splitListRange is not affected by this transaction
249 }
250
251 // (4) Unwrap the list and its containing listItem
252 surfaceModel.getLinearFragment( new ve.Range( splitListRange.start + 1, splitListRange.end - 1 ), true )
253 .unwrapNodes( 2 );
254 }
255 };
256
257 /* Registration */
258
259 ve.ui.actionFactory.register( ve.ui.IndentationAction );