Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add undo / redo
- Loading branch information
Showing
7 changed files
with
173 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { type Selection } from '../core/utils'; | ||
|
||
|
||
export | ||
type InputHistoryState = { | ||
unmaskedValue: string, | ||
selection: Selection, | ||
}; | ||
|
||
|
||
export default | ||
class InputHistory { | ||
static MAX_LENGTH = 100; | ||
states: InputHistoryState[] = []; | ||
currentIndex = 0; | ||
|
||
get currentState (): InputHistoryState | undefined { | ||
return this.states[this.currentIndex]; | ||
} | ||
|
||
get isEmpty (): boolean { | ||
return this.states.length === 0; | ||
} | ||
|
||
push (state: InputHistoryState) { | ||
// if current index points before the last element then remove the future | ||
if (this.currentIndex < this.states.length - 1) this.states.length = this.currentIndex + 1; | ||
this.states.push(state); | ||
if (this.states.length > InputHistory.MAX_LENGTH) this.states.shift(); | ||
this.currentIndex = this.states.length - 1; | ||
} | ||
|
||
go (steps: number): InputHistoryState | undefined { | ||
this.currentIndex = Math.min(Math.max(this.currentIndex + steps, 0), this.states.length - 1); | ||
return this.currentState; | ||
} | ||
|
||
undo () { | ||
return this.go(-1); | ||
} | ||
|
||
redo () { | ||
return this.go(+1); | ||
} | ||
|
||
clear () { | ||
this.states.length = 0; | ||
this.currentIndex = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import assert from 'assert'; | ||
import { describe, it, beforeEach } from 'node:test'; | ||
|
||
import InputHistory from '../../src/controls/input-history'; | ||
|
||
|
||
describe('InputHistory', function () { | ||
const history = new InputHistory(); | ||
|
||
beforeEach(function () { | ||
history.clear(); | ||
}); | ||
|
||
it('should work', function () { | ||
const state1 = { unmaskedValue: '1', selection: { start: 0, end: 1 } }; | ||
const state2 = { unmaskedValue: '2', selection: { start: 1, end: 2 } }; | ||
|
||
history.push(state1); | ||
history.push(state2); | ||
assert.equal(history.currentIndex, 1); | ||
|
||
assert.equal(history.undo(), state1); | ||
assert.equal(history.currentIndex, 0); | ||
|
||
assert.equal(history.redo(), state2); | ||
assert.equal(history.currentIndex, 1); | ||
|
||
history.clear(); | ||
assert.equal(history.states.length, 0); | ||
}); | ||
}); |