import { ProjectUpdatePayload } from '@meetalva/message' ; import { EditHistoryCommit } from './edit-history-commit' ; import { EditHistoryStage } from './edit-history-stage' ; export class EditHistory { private commits : EditHistoryCommit [ ] = [ ] ; private reverts : EditHistoryCommit [ ] = [ ] ; private stage = new EditHistoryStage ( ) ; public get length ( ) : number { return this . commits . length ; } public record ( record ) { this . stage . add ( record ) ; } public commit ( ) { if ( this . stage . length === 0 ) { return ; } this . reverts = [ ] ; this . commits . unshift ( this . stage . toCommit ( ) ) ; this . clearStage ( ) ; } public peek ( offset = 0 ) { return this . commits [ offset ] ; } public undo ( ) { const commit = this . commits . shift ( ) ; if ( ! commit ) { return ; } const revert = commit . revert ( ) ; this . reverts . unshift ( revert ) ; return revert ; } public redo ( ) { const revert = this . reverts . shift ( ) ; if ( ! revert ) { return ; } const commit = revert . revert ( ) ; this . commits . unshift ( commit ) ; return commit ; } public clearStage ( ) { this . stage = new EditHistoryStage ( ) ; } }