| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.IndentationAction = function VeUiIndentationAction() { |
| 18 |
|
| 19 |
ve.ui.IndentationAction.super.apply( this, arguments ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.inheritClass( ve.ui.IndentationAction, ve.ui.Action ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ve.ui.IndentationAction.static.name = 'indentation'; |
| 29 |
|
| 30 |
ve.ui.IndentationAction.static.methods = [ 'increase', 'decrease' ]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
ve.ui.IndentationAction.prototype.increase = function () { |
| 40 |
return this.changeIndentation( 1 ); |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ui.IndentationAction.prototype.decrease = function () { |
| 49 |
return this.changeIndentation( -1 ); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 78 |
fragments.push( surfaceModel.getLinearFragment( group.nodes[ 0 ].getRange(), true ) ); |
| 79 |
changed = true; |
| 80 |
} |
| 81 |
} ); |
| 82 |
|
| 83 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ve.ui.IndentationAction.prototype.indentListItem = function ( listItem ) { |
| 103 |
|
| 104 |
|
| 105 |
if ( !( listItem instanceof ve.dm.ListItemNode ) ) { |
| 106 |
throw new Error( 'listItem must be a ve.dm.ListItemNode' ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
const listType = listItem.getParent().getAttribute( 'style' ); |
| 120 |
const listItemRange = listItem.getOuterRange(); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
const surfaceModel = this.surface.getModel(); |
| 128 |
|
| 129 |
surfaceModel.getLinearFragment( listItemRange, true ) |
| 130 |
.wrapNodes( [ { type: 'listItem' }, { type: 'list', attributes: { style: listType } } ] ); |
| 131 |
|
| 132 |
const documentModel = surfaceModel.getDocument(); |
| 133 |
|
| 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 |
|
| 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 |
|
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
ve.ui.IndentationAction.prototype.unindentListItem = function ( listItem ) { |
| 163 |
|
| 164 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
if ( documentModel.data.getData( listItemRange.start - 1 ).type !== 'list' ) { |
| 197 |
|
| 198 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, listItemRange.start, |
| 199 |
[ { type: '/list' }, listElement ] |
| 200 |
); |
| 201 |
surfaceModel.change( tx ); |
| 202 |
|
| 203 |
listItemRange = listItemRange.translate( 2 ); |
| 204 |
} |
| 205 |
if ( documentModel.data.getData( listItemRange.end ).type !== '/list' ) { |
| 206 |
|
| 207 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, listItemRange.end, |
| 208 |
[ { type: '/list' }, listElement ] |
| 209 |
); |
| 210 |
surfaceModel.change( tx ); |
| 211 |
|
| 212 |
} |
| 213 |
let splitListRange = new ve.Range( listItemRange.start - 1, listItemRange.end + 1 ); |
| 214 |
|
| 215 |
if ( grandParentType !== 'listItem' ) { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
surfaceModel.getLinearFragment( new ve.Range( listItemRange.start + 1, listItemRange.end - 1 ), true ) |
| 220 |
.unwrapNodes( 2 ); |
| 221 |
|
| 222 |
|
| 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 |
|
| 232 |
|
| 233 |
if ( documentModel.data.getData( splitListRange.start - 1 ).type !== 'listItem' ) { |
| 234 |
|
| 235 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, splitListRange.start, |
| 236 |
[ { type: '/listItem' }, { type: 'listItem' } ] |
| 237 |
); |
| 238 |
surfaceModel.change( tx ); |
| 239 |
|
| 240 |
splitListRange = splitListRange.translate( 2 ); |
| 241 |
} |
| 242 |
if ( documentModel.data.getData( splitListRange.end ).type !== '/listItem' ) { |
| 243 |
|
| 244 |
tx = ve.dm.TransactionBuilder.static.newFromInsertion( documentModel, splitListRange.end, |
| 245 |
[ { type: '/listItem' }, { type: 'listItem' } ] |
| 246 |
); |
| 247 |
surfaceModel.change( tx ); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
surfaceModel.getLinearFragment( new ve.Range( splitListRange.start + 1, splitListRange.end - 1 ), true ) |
| 253 |
.unwrapNodes( 2 ); |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
ve.ui.actionFactory.register( ve.ui.IndentationAction ); |