/** * 设置重做、撤销栈 * @param baseState this.state.baseState * @param contentState 当前设置的新内容State,newContentState * @param isSetUndo 是否设置撤销栈,线性调整在调整过程中不记录堆栈,调整结束时手动记录调整开始位置的状态为撤销栈 */ static push(baseState: BaseState, contentState: ContentState, isSetUndo: boolean = true): BaseState { if (baseState.getCurrentContent() === contentState) { return baseState; } const currentContent: ContentState = baseState.getCurrentContent(); let tempContentState: ContentState = baseState.getTempContentState(); let undoStack: Stack<ContentState> = baseState.getUndoStack(); const redoStack: Stack<ContentState> = baseState.getRedoStack(); const newContent: ContentState = contentState; if (newContent !== currentContent && isSetUndo === true) { tempContentState = newContent; undoStack = undoStack.push(currentContent); } const editorStateChanges: any = { currentContent: newContent, tempContentState, undoStack, redoStack }; return BaseState.set(baseState, editorStateChanges); }
it('push inserts at lowest index', () => { var s0 = Stack.of('a', 'b', 'c'); var s1 = s0.push('d', 'e', 'f'); expect(s0.size).toBe(3); expect(s1.size).toBe(6); expect(s1.toArray()).toEqual(['d', 'e', 'f', 'a', 'b', 'c']); });
/** * 重做 * @param baseState 当前baseState */ static redo(baseState: BaseState): BaseState { const redoStack: Stack<ContentState> = baseState.getRedoStack(); const newCurrentContent: ContentState = redoStack.peek(); if (!newCurrentContent) { return baseState; } const currentContent: ContentState = baseState.getCurrentContent(); return BaseState.set(baseState, { currentContent: newCurrentContent, tempContentState: newCurrentContent, undoStack: baseState.getUndoStack().push(currentContent), redoStack: redoStack.shift() }); }
export const createInterpreter = (): InterpreterState => { const globalEnv: Scope = { name: '_global_', parent: undefined, environment: Map<string, any>() } return new InterpreterState({ _done: false, _isReturned: false, _result: undefined, isRunning: true, frames: Stack.of(0), scopes: Map.of(0, globalEnv), errors: List(), value: undefined, node: undefined }) }
it('iterable', () => { var s = Stack.of('a', 'b', 'c'); expect(s.size).toBe(3); var forEachResults = []; s.forEach((val, i) => forEachResults.push([i, val])); expect(forEachResults).toEqual([ [0,'a'], [1,'b'], [2,'c'], ]); // map will cause reverse iterate expect(s.map(val => val + val).toArray()).toEqual([ 'aa', 'bb', 'cc', ]); var iteratorResults = []; var iterator = s.entries(); var step; while (!(step = iterator.next()).done) { iteratorResults.push(step.value); } expect(iteratorResults).toEqual([ [0,'a'], [1,'b'], [2,'c'], ]); iteratorResults = []; iterator = s.toSeq().reverse().entries(); while (!(step = iterator.next()).done) { iteratorResults.push(step.value); } expect(iteratorResults).toEqual([ [0,'c'], [1,'b'], [2,'a'], ]); });
constructor(init: T, private _onChange?: (e: T) => void) { this._history = Immutable.Stack.of<T>(init); this._redoHistory = Immutable.Stack<T>(); }
it('slice helpers make for easier to read code', () => { var s = Stack.of('a', 'b', 'c'); expect(s.rest().toArray()).toEqual(['b', 'c']); });
it('get helpers make for easier to read code', () => { var s = Stack.of('a', 'b', 'c'); expect(s.first()).toBe('a'); expect(s.last()).toBe('c'); expect(s.peek()).toBe('a'); });
it('pushing creates a new instance', () => { var s0 = Stack.of('a'); var s1 = s0.push('A'); expect(s0.get(0)).toBe('a'); expect(s1.get(0)).toBe('A'); });
it('toArray provides a JS array', () => { var s = Stack.of('a', 'b', 'c'); expect(s.toArray()).toEqual(['a', 'b', 'c']); });