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