-
-
Notifications
You must be signed in to change notification settings - Fork 399
feat: expose clearHistory() to reset the undo/redo stack #373
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
Changes from all commits
cb4d6a0
e6e688b
601dc4b
15a42c9
1e50bf0
90b0e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,12 @@ export default function (mei: MindElixirInstance) { | |
| } | ||
| } | ||
| } | ||
| mei.clearHistory = function () { | ||
| history = [] | ||
| currentIndex = -1 | ||
| current = mei.getData() | ||
| mei.clearSelection() | ||
| } | ||
|
Comment on lines
+91
to
+96
|
||
| const handleOperation = function (operation: Operation) { | ||
| if (operation.name === 'beginEdit') return | ||
| history = history.slice(0, currentIndex + 1) | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the test is failing, please remove it for now.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oups... |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { test, expect } from './mind-elixir-test' | ||
|
|
||
| const diagramA = { | ||
| nodeData: { | ||
| id: 'root-a', | ||
| topic: 'Diagram A', | ||
| children: [{ id: 'child-a', topic: 'Child A' }], | ||
| }, | ||
| } | ||
|
|
||
| const diagramB = { | ||
| nodeData: { | ||
| id: 'root-b', | ||
| topic: 'Diagram B', | ||
| children: [{ id: 'child-b', topic: 'Child B' }], | ||
| }, | ||
| } | ||
|
|
||
| test.beforeEach(async ({ me }) => { | ||
| await me.init(diagramA) | ||
| }) | ||
|
|
||
| test('clearHistory - undo cannot revert into pre-refresh diagram', async ({ page, me }) => { | ||
| // Perform an operation in Diagram A | ||
| await me.click('Child A') | ||
| await page.keyboard.press('Tab') | ||
| await page.keyboard.press('Enter') | ||
| await expect(me.getByText('New Node')).toBeVisible() | ||
|
|
||
| // Load Diagram B and clear the history stack | ||
| await page.evaluate((data: typeof diagramB) => { | ||
| const mind = (window as any)['#map'] | ||
| mind.refresh(data) | ||
| mind.clearHistory() | ||
| }, diagramB) | ||
|
|
||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
| await expect(me.getByText('Diagram A')).toBeHidden() | ||
|
|
||
| // Undo should be a no-op — must not travel back into Diagram A | ||
| await page.keyboard.press('Control+z') | ||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
| await expect(me.getByText('Diagram A')).toBeHidden() | ||
|
|
||
| // Redo should also be a no-op | ||
| await page.keyboard.press('Control+y') | ||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
| await expect(me.getByText('Diagram A')).toBeHidden() | ||
| }) | ||
|
|
||
| test('clearHistory - operations after clearHistory are undoable normally', async ({ page, me }) => { | ||
| // Perform an operation in Diagram A, then switch to Diagram B and clear history | ||
| await me.click('Child A') | ||
| await page.keyboard.press('Delete') | ||
| await expect(me.getByText('Child A')).toBeHidden() | ||
|
|
||
| await page.evaluate((data: typeof diagramB) => { | ||
| const mind = (window as any)['#map'] | ||
| mind.refresh(data) | ||
| mind.clearHistory() | ||
| }, diagramB) | ||
|
|
||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
|
|
||
| // Add a node to Diagram B | ||
| await me.click('Child B') | ||
| await page.keyboard.press('Tab') | ||
| await page.keyboard.press('Enter') | ||
| await expect(me.getByText('New Node')).toBeVisible() | ||
|
|
||
| // Undo the add — should work | ||
| await page.keyboard.press('Control+z') | ||
| await expect(me.getByText('New Node')).toBeHidden() | ||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
|
|
||
| // Another undo should be a no-op — must not reach Diagram A | ||
| await page.keyboard.press('Control+z') | ||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
| await expect(me.getByText('Diagram A')).toBeHidden() | ||
|
|
||
| // Redo restores the added node | ||
| await page.keyboard.press('Control+y') | ||
| await expect(me.getByText('New Node')).toBeVisible() | ||
| }) | ||
|
|
||
| test('clearHistory - first undo baseline is the refreshed diagram state', async ({ page, me }) => { | ||
| // Switch to Diagram B and clear history | ||
| await page.evaluate((data: typeof diagramB) => { | ||
| const mind = (window as any)['#map'] | ||
| mind.refresh(data) | ||
| mind.clearHistory() | ||
| }, diagramB) | ||
|
|
||
| // Add a node to Diagram B | ||
| await me.click('Child B') | ||
| await page.keyboard.press('Tab') | ||
| await page.keyboard.press('Enter') | ||
| await expect(me.getByText('New Node')).toBeVisible() | ||
|
|
||
| // Undo should restore exactly to the post-refresh state of Diagram B | ||
| await page.keyboard.press('Control+z') | ||
| await expect(me.getByText('New Node')).toBeHidden() | ||
| await expect(me.getByText('Child B')).toBeVisible() | ||
| await expect(me.getByText('Diagram B')).toBeVisible() | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clearHistory()clears the history stack but leavescurrentSelectedNodesunchanged. Since the intended usage is right afterrefresh(), selection is typically cleared, and the next operation may incorrectly record the previous diagram’s selected node IDs incurrentSelected, which can break selection restoration on undo/redo. ResetcurrentSelectedNodesinsideclearHistory()(e.g., derive it frommei.currentNodes) or update the selection tracking to handle deselection events as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
601dc4b— addedcurrentSelectedNodes = []toclearHistory()so stale node IDs from the previous diagram cannot affect selection restoration afterrefresh()+clearHistory().