* Moves the characters from `start` and `end` to `index`.
(start: number, end: number, index: number)
| 480 | * Moves the characters from `start` and `end` to `index`. |
| 481 | */ |
| 482 | move(start: number, end: number, index: number): this { |
| 483 | start = start + this.offset |
| 484 | end = end + this.offset |
| 485 | index = index + this.offset |
| 486 | |
| 487 | if (start === end) |
| 488 | return this |
| 489 | |
| 490 | if (index >= start && index <= end) |
| 491 | throw new Error('Cannot move a selection inside itself') |
| 492 | |
| 493 | if (DEBUG) |
| 494 | this.stats.time('move') |
| 495 | |
| 496 | this._split(start) |
| 497 | this._split(end) |
| 498 | this._split(index) |
| 499 | |
| 500 | const first = this.byStart[start] |
| 501 | const last = this.byEnd[end] |
| 502 | |
| 503 | const oldLeft = first.previous |
| 504 | const oldRight = last.next |
| 505 | |
| 506 | const newRight = this.byStart[index] |
| 507 | if (!newRight && last === this.lastChunk) |
| 508 | return this |
| 509 | const newLeft = newRight ? newRight.previous : this.lastChunk |
| 510 | |
| 511 | if (oldLeft) |
| 512 | oldLeft.next = oldRight |
| 513 | if (oldRight) |
| 514 | oldRight.previous = oldLeft |
| 515 | |
| 516 | if (newLeft) |
| 517 | newLeft.next = first |
| 518 | if (newRight) |
| 519 | newRight.previous = last |
| 520 | |
| 521 | if (!first.previous) |
| 522 | this.firstChunk = last.next |
| 523 | if (!last.next) { |
| 524 | this.lastChunk = first.previous |
| 525 | this.lastChunk.next = null |
| 526 | } |
| 527 | |
| 528 | first.previous = newLeft |
| 529 | last.next = newRight || null |
| 530 | |
| 531 | if (!newLeft) |
| 532 | this.firstChunk = first |
| 533 | if (!newRight) |
| 534 | this.lastChunk = last |
| 535 | |
| 536 | if (DEBUG) |
| 537 | this.stats.timeEnd('move') |
| 538 | return this |
| 539 | } |