Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor undo cursor #377

Merged
merged 2 commits into from
Jun 6, 2024
Merged

Refactor undo cursor #377

merged 2 commits into from
Jun 6, 2024

Conversation

zxch3n
Copy link
Member

@zxch3n zxch3n commented Jun 6, 2024

This change internalizes some logic related to cursor transformation when performing undo/redo. It makes the API much simpler.

Specifically, the recorded cursor info for each undo/redo item is different when we're inside an undo/redo loop.

// When in the undo/redo loop, the new undo/redo stack item should restore the selection
// to the state it was in before the item that was popped two steps ago from the stack.
//
//                          ┌────────────┐
//                          │Selection 1 │
//                          └─────┬──────┘
//                                │   Some
//                                ▼   ops
//                          ┌────────────┐
//                          │Selection 2 │
//                          └─────┬──────┘
//                                │   Some
//                                ▼   ops
//                          ┌────────────┐
//                          │Selection 3 │◁ ─ ─ ─ ─ ─ ─ ─  Restore  ─ ─ ─
//                          └─────┬──────┘                               │
//                                │
//                                │                                      │
//                                │                              ┌ ─ ─ ─ ─ ─ ─ ─
//           Enter the            │   Undo ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶   Push Redo   │
//           undo/redo ─ ─ ─ ▶    ▼                              └ ─ ─ ─ ─ ─ ─ ─
//             loop         ┌────────────┐                               │
//                          │Selection 2 │◁─ ─ ─  Restore  ─
//                          └─────┬──────┘                  │            │
//                                │
//                                │                         │            │
//                                │                 ┌ ─ ─ ─ ─ ─ ─ ─
//                                │   Undo ─ ─ ─ ─ ▶   Push Redo   │     │
//                                ▼                 └ ─ ─ ─ ─ ─ ─ ─
//                          ┌────────────┐                  │            │
//                          │Selection 1 │
//                          └─────┬──────┘                  │            │
//                                │   Redo ◀ ─ ─ ─ ─ ─ ─ ─ ─
//                                ▼                                      │
//                          ┌────────────┐
//         ┌   Restore   ─ ▷│Selection 2 │                               │
//                          └─────┬──────┘
//         │                      │                                      │
// ┌ ─ ─ ─ ─ ─ ─ ─                │
//    Push Undo   │◀─ ─ ─ ─ ─ ─ ─ │   Redo ◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
// └ ─ ─ ─ ─ ─ ─ ─                ▼
//         │                ┌────────────┐
//                          │Selection 3 │
//         │                └─────┬──────┘
//          ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶ │   Undo
//                                ▼
//                          ┌────────────┐
//                          │Selection 2 │
//                          └────────────┘
//
// Because users may change the selections during the undo/redo loop, it's
// more stable to keep the selection stored in the last stack item
// rather than using the current selection directly.

@zxch3n zxch3n added the refactor label Jun 6, 2024
@zxch3n zxch3n requested a review from Leeeon233 June 6, 2024 06:21
Copy link
Member

@Leeeon233 Leeeon233 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks much simpler

@zxch3n zxch3n merged commit 6d47015 into main Jun 6, 2024
1 check passed
@zxch3n zxch3n deleted the refactor-undo-cursor branch June 6, 2024 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants