| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
ve.ce.TextState = function VeCeTextState( element ) { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
this.chunks = this.constructor.static.getChunks( element ); |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
OO.initClass( ve.ce.TextState ); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
ve.ce.TextState.static.getChunks = function ( element ) { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
const elementListStack = [ [] ], |
| 39 |
chunks = []; |
| 40 |
let stackTop = 0; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
function add( text, type ) { |
| 49 |
if ( |
| 50 |
!chunks.length || |
| 51 |
chunks[ chunks.length - 1 ].elements !== elementListStack[ stackTop ] || |
| 52 |
chunks[ chunks.length - 1 ].type !== type |
| 53 |
) { |
| 54 |
chunks.push( new ve.ce.TextStateChunk( |
| 55 |
text, |
| 56 |
elementListStack[ stackTop ], |
| 57 |
type || 'text' |
| 58 |
) ); |
| 59 |
} else { |
| 60 |
chunks[ chunks.length - 1 ].text += text; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
let view; |
| 65 |
const annotationStack = []; |
| 66 |
let node = element; |
| 67 |
while ( true ) { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
if ( node.nodeType === Node.TEXT_NODE ) { |
| 73 |
add( node.data.replace( /\u00A0/g, ' ' ) ); |
| 74 |
} else if ( |
| 75 |
|
| 76 |
|
| 77 |
node.nodeType !== Node.ELEMENT_NODE || |
| 78 |
node.classList.contains( 've-ce-branchNode-blockSlug' ) || |
| 79 |
node.classList.contains( 've-ce-cursorHolder' ) |
| 80 |
) { |
| 81 |
|
| 82 |
} else if ( ( view = $( node ).data( 'view' ) ) && view instanceof ve.ce.LeafNode ) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ( node === view.$element[ 0 ] ) { |
| 88 |
☃ |
| 89 |
add( '\u2603'.repeat( view.getOuterLength() ) ); |
| 90 |
} |
| 91 |
} else if ( node.classList.contains( 've-ce-unicorn' ) ) { |
| 92 |
add( '', 'unicorn' ); |
| 93 |
} else if ( node.firstChild ) { |
| 94 |
if ( ve.ce.isAnnotationElement( node ) ) { |
| 95 |
|
| 96 |
elementListStack.push( elementListStack[ stackTop ].concat( node ) ); |
| 97 |
annotationStack.push( node ); |
| 98 |
stackTop++; |
| 99 |
} |
| 100 |
node = node.firstChild; |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
while ( true ) { |
| 107 |
if ( node === element ) { |
| 108 |
break; |
| 109 |
} |
| 110 |
if ( node === annotationStack[ annotationStack.length - 1 ] ) { |
| 111 |
annotationStack.pop(); |
| 112 |
elementListStack.pop(); |
| 113 |
stackTop--; |
| 114 |
} |
| 115 |
if ( node.nextSibling ) { |
| 116 |
break; |
| 117 |
} |
| 118 |
node = node.parentNode; |
| 119 |
} |
| 120 |
if ( node === element ) { |
| 121 |
break; |
| 122 |
} |
| 123 |
node = node.nextSibling; |
| 124 |
} |
| 125 |
return chunks; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ve.ce.TextState.prototype.isEqual = function ( other ) { |
| 137 |
if ( other === this ) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
if ( !other || this.chunks.length !== other.chunks.length ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
for ( let i = 0, len = this.chunks.length; i < len; i++ ) { |
| 144 |
if ( !( this.chunks[ i ].isEqual( other.chunks[ i ] ) ) ) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
return true; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
ve.ce.TextState.prototype.getChangeTransaction = function ( prev, modelDoc, modelOffset, unicornAnnotations ) { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
function countMissing( newArray, oldArray, equals ) { |
| 175 |
let count = 0; |
| 176 |
for ( let i = 0, iLen = newArray.length; i < iLen; i++ ) { |
| 177 |
let j, jLen; |
| 178 |
for ( j = 0, jLen = oldArray.length; j < jLen; j++ ) { |
| 179 |
if ( equals( newArray[ i ], oldArray[ j ] ) ) { |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
if ( j === jLen ) { |
| 184 |
count++; |
| 185 |
} |
| 186 |
} |
| 187 |
return count; |
| 188 |
} |
| 189 |
|
| 190 |
const oldChunks = prev.chunks, |
| 191 |
newChunks = this.chunks, |
| 192 |
modelData = modelDoc.data, |
| 193 |
newData = []; |
| 194 |
|
| 195 |
|
| 196 |
const change = ve.countEdgeMatches( oldChunks, newChunks, ( a, b ) => a.isEqual( b ) ); |
| 197 |
if ( change === null ) { |
| 198 |
|
| 199 |
return null; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
let textStart = 0; |
| 205 |
let textEnd = 0; |
| 206 |
if ( change.start + change.end < Math.min( oldChunks.length, newChunks.length ) ) { |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
if ( oldChunks[ change.start ].hasEqualElements( newChunks[ change.start ] ) ) { |
| 212 |
const oldChunk = oldChunks[ change.start ]; |
| 213 |
const newChunk = newChunks[ change.start ]; |
| 214 |
const iLen = Math.min( oldChunk.text.length, newChunk.text.length ); |
| 215 |
let i; |
| 216 |
for ( i = 0; i < iLen; i++ ) { |
| 217 |
if ( oldChunk.text[ i ] !== newChunk.text[ i ] ) { |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
textStart = i; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if ( oldChunks[ oldChunks.length - 1 - change.end ].hasEqualElements( |
| 230 |
newChunks[ newChunks.length - 1 - change.end ] |
| 231 |
) ) { |
| 232 |
const oldChunk = oldChunks[ oldChunks.length - 1 - change.end ]; |
| 233 |
const newChunk = newChunks[ newChunks.length - 1 - change.end ]; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
const iLen = Math.min( |
| 241 |
oldChunk.text.length - |
| 242 |
( change.start + change.end === oldChunks.length - 1 ? textStart : 0 ), |
| 243 |
newChunk.text.length - |
| 244 |
( change.start + change.end === newChunks.length - 1 ? textStart : 0 ) |
| 245 |
); |
| 246 |
let i; |
| 247 |
for ( i = 0; i < iLen; i++ ) { |
| 248 |
if ( newChunk.text[ newChunk.text.length - 1 - i ] !== |
| 249 |
oldChunk.text[ oldChunk.text.length - 1 - i ] |
| 250 |
) { |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
textEnd = i; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
let changeOffset = modelOffset + 1; |
| 260 |
for ( let i = 0, iLen = change.start; i < iLen; i++ ) { |
| 261 |
changeOffset += oldChunks[ i ].text.length; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
let removed = 0; |
| 266 |
for ( let i = change.start, iLen = oldChunks.length - change.end; i < iLen; i++ ) { |
| 267 |
removed += oldChunks[ i ].text.length; |
| 268 |
} |
| 269 |
const removeRange = new ve.Range( changeOffset + textStart, changeOffset + removed - textEnd ); |
| 270 |
|
| 271 |
|
| 272 |
for ( let i = change.start, iLen = newChunks.length - change.end; i < iLen; i++ ) { |
| 273 |
const newChunk = newChunks[ i ]; |
| 274 |
if ( newChunk.type === 'unicorn' ) { |
| 275 |
|
| 276 |
continue; |
| 277 |
} |
| 278 |
let data = newChunk.text.split( '' ); |
| 279 |
if ( i === change.start ) { |
| 280 |
data = data.slice( textStart ); |
| 281 |
} |
| 282 |
if ( i === iLen - 1 ) { |
| 283 |
data = data.slice( 0, data.length - textEnd ); |
| 284 |
} |
| 285 |
if ( data.length === 0 ) { |
| 286 |
|
| 287 |
|
| 288 |
continue; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
let annotations = null; |
| 297 |
let missing = null; |
| 298 |
|
| 299 |
let jStart; |
| 300 |
let matchStartOffset; |
| 301 |
if ( change.start === 0 ) { |
| 302 |
jStart = 0; |
| 303 |
matchStartOffset = changeOffset; |
| 304 |
} else { |
| 305 |
|
| 306 |
jStart = change.start - 1; |
| 307 |
matchStartOffset = changeOffset - oldChunks[ jStart ].text.length; |
| 308 |
} |
| 309 |
let jEnd; |
| 310 |
if ( change.end === 0 ) { |
| 311 |
jEnd = oldChunks.length; |
| 312 |
} else { |
| 313 |
|
| 314 |
jEnd = oldChunks.length - change.end + 1; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
let matchOffset = matchStartOffset; |
| 320 |
for ( let j = jStart; j < jEnd; j++ ) { |
| 321 |
const oldChunk = oldChunks[ j ]; |
| 322 |
if ( !oldChunk.hasEqualElements( newChunk ) ) { |
| 323 |
matchOffset += oldChunk.text.length; |
| 324 |
continue; |
| 325 |
} |
| 326 |
if ( oldChunk.type === 'unicorn' ) { |
| 327 |
if ( !unicornAnnotations ) { |
| 328 |
throw new Error( 'No unicorn annotations' ); |
| 329 |
} |
| 330 |
annotations = unicornAnnotations; |
| 331 |
break; |
| 332 |
} |
| 333 |
annotations = modelData.getInsertionAnnotationsFromRange( |
| 334 |
new ve.Range( matchOffset ), |
| 335 |
true |
| 336 |
); |
| 337 |
break; |
| 338 |
} |
| 339 |
if ( annotations === null ) { |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
let leastMissing = newChunk.elements.length; |
| 348 |
let bestOffset = null; |
| 349 |
matchOffset = matchStartOffset; |
| 350 |
for ( let j = jStart; j < jEnd; j++ ) { |
| 351 |
const oldChunk = oldChunks[ j ]; |
| 352 |
missing = countMissing( |
| 353 |
newChunk.elements, |
| 354 |
oldChunk.elements, |
| 355 |
ve.ce.TextStateChunk.static.compareElements |
| 356 |
); |
| 357 |
if ( missing < leastMissing ) { |
| 358 |
leastMissing = missing; |
| 359 |
bestOffset = matchOffset; |
| 360 |
if ( missing === 0 ) { |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
matchOffset += oldChunk.text.length; |
| 365 |
} |
| 366 |
let oldAnnotations; |
| 367 |
if ( bestOffset === null ) { |
| 368 |
oldAnnotations = new ve.dm.AnnotationSet( modelData.getStore() ); |
| 369 |
} else { |
| 370 |
oldAnnotations = modelData.getInsertionAnnotationsFromRange( |
| 371 |
new ve.Range( bestOffset ), |
| 372 |
true |
| 373 |
); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
annotations = new ve.dm.AnnotationSet( modelData.getStore() ); |
| 382 |
for ( let j = 0, jLen = newChunk.elements.length; j < jLen; j++ ) { |
| 383 |
const element = newChunk.elements[ j ]; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
const view = $( element ).data( 'view' ); |
| 388 |
let ann; |
| 389 |
if ( view ) { |
| 390 |
ann = view.getModel(); |
| 391 |
} else { |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
const modelClass = ve.dm.modelRegistry.lookup( |
| 396 |
ve.dm.modelRegistry.matchElement( element ) |
| 397 |
); |
| 398 |
if ( !( modelClass && modelClass.prototype instanceof ve.dm.Annotation ) ) { |
| 399 |
|
| 400 |
continue; |
| 401 |
} |
| 402 |
ann = ve.dm.annotationFactory.createFromElement( |
| 403 |
modelClass.static.toDataElement( [ element ], ve.dm.converter ) |
| 404 |
); |
| 405 |
const oldAnn = oldAnnotations.getComparable( ann ); |
| 406 |
if ( oldAnn ) { |
| 407 |
ann = oldAnn; |
| 408 |
} else if ( !ann.constructor.static.inferFromView ) { |
| 409 |
|
| 410 |
continue; |
| 411 |
} |
| 412 |
} |
| 413 |
annotations.add( ann, annotations.getLength() ); |
| 414 |
} |
| 415 |
} |
| 416 |
ve.dm.Document.static.addAnnotationsToData( data, annotations ); |
| 417 |
ve.batchPush( newData, data ); |
| 418 |
} |
| 419 |
|
| 420 |
return ve.dm.TransactionBuilder.static.newFromReplacement( modelDoc, removeRange, newData ); |
| 421 |
}; |