From 622f53b0a3fa6cc141444b68f15f2e24a901e5b0 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 6 Feb 2022 08:13:20 +0100 Subject: [PATCH] Add goToPointer --- src/undo_manager/index.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/undo_manager/index.js b/src/undo_manager/index.js index 9dafbe5b9..395b677ed 100644 --- a/src/undo_manager/index.js +++ b/src/undo_manager/index.js @@ -25,7 +25,7 @@ */ import UndoManager from 'backbone-undo'; -import { isArray, isBoolean, isEmpty, unique } from 'underscore'; +import { isArray, isBoolean, isEmpty, unique, times } from 'underscore'; export default () => { let em; @@ -319,13 +319,13 @@ export default () => { getGroupedStack() { const result = {}; const stack = this.getStack(); - const createItem = item => { + const createItem = (item, index) => { const { type, after, before, object, options = {} } = item.attributes; - return { type, after, before, object, options }; + return { index, type, after, before, object, options }; }; - stack.forEach(item => { + stack.forEach((item, i) => { const index = item.get('magicFusionIndex'); - const value = createItem(item); + const value = createItem(item, i); if (!result[index]) { result[index] = [value]; @@ -337,8 +337,7 @@ export default () => { return Object.keys(result).map(index => { const actions = result[index]; return { - // index: stack.models.indexOf(actions[0]), - index, + index: actions[actions.length - 1].index, actions, labels: unique( actions.reduce((res, item) => { @@ -351,6 +350,15 @@ export default () => { }); }, + goToPointer(group) { + if (!group) return; + const current = this.getPointer(); + const goTo = group.index - current; + times(Math.abs(goTo), () => { + this[goTo < 0 ? 'undo' : 'redo'](false); + }); + }, + getPointer() { return this.getStack().pointer; },