|
| 1 | +import Module from '../core/Module'; |
| 2 | +import DOM from '../utils/DOM'; |
| 3 | + |
| 4 | +const Undo = Module({ |
| 5 | + name: 'Undo', |
| 6 | + props: { |
| 7 | + contentEditableElem: null, |
| 8 | + currentHistoryIndex: -1, |
| 9 | + history: [], |
| 10 | + ignoreSelectionChanges: false |
| 11 | + }, |
| 12 | + |
| 13 | + handlers: { |
| 14 | + events: { |
| 15 | + 'contenteditable:mutation:observed': 'handleMutation', |
| 16 | + 'contenteditable:focus': 'handleFocus', |
| 17 | + 'import:from:canvas:start': 'handleImportStart', |
| 18 | + 'import:from:canvas:complete': 'handleImportComplete', |
| 19 | + 'selection:change': 'handleSelectionChange', |
| 20 | + 'export:to:canvas:start': 'handleExportStart' |
| 21 | + } |
| 22 | + }, |
| 23 | + |
| 24 | + methods: { |
| 25 | + setup () {}, |
| 26 | + init () {}, |
| 27 | + |
| 28 | + handleMutation () { |
| 29 | + const { props, mediator } = this; |
| 30 | + const { history, currentHistoryIndex } = props; |
| 31 | + const states = { |
| 32 | + currentHistoryIndex, |
| 33 | + current: this.createHistoryState(), |
| 34 | + previous: history[currentHistoryIndex], |
| 35 | + beforePrevious: history[currentHistoryIndex - 1], |
| 36 | + next: history[currentHistoryIndex + 1], |
| 37 | + afterNext: history[currentHistoryIndex + 2] |
| 38 | + }; |
| 39 | + |
| 40 | + const { |
| 41 | + isUndo, |
| 42 | + isRedo, |
| 43 | + noChange |
| 44 | + } = this.analyzeStates(states); |
| 45 | + |
| 46 | + if (noChange) { |
| 47 | + return; |
| 48 | + } else if (!isUndo && !isRedo) { |
| 49 | + props.history.length = currentHistoryIndex + 1; |
| 50 | + props.history.push(states.current); |
| 51 | + props.currentHistoryIndex += 1; |
| 52 | + } else if (isUndo) { |
| 53 | + props.currentHistoryIndex -= 1; |
| 54 | + mediator.exec('format:clean', props.contentEditableElem); |
| 55 | + mediator.exec('selection:select:coordinates', states.beforePrevious.selectionRangeCoordinates); |
| 56 | + } else if (isRedo) { |
| 57 | + props.currentHistoryIndex += 1; |
| 58 | + mediator.exec('format:clean', props.contentEditableElem); |
| 59 | + mediator.exec('selection:select:coordinates', states.next.selectionRangeCoordinates); |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + handleFocus () { |
| 64 | + const { mediator, props } = this; |
| 65 | + const contentEditableElem = mediator.get('contenteditable:element'); |
| 66 | + |
| 67 | + if (props.contentEditableElem !== contentEditableElem) { |
| 68 | + setTimeout(() => { |
| 69 | + props.contentEditableElem = contentEditableElem; |
| 70 | + props.history = [this.createHistoryState()]; |
| 71 | + props.currentHistoryIndex = 0; |
| 72 | + }, 150); |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + handleImportStart () { |
| 77 | + const { props } = this; |
| 78 | + props.ignoreSelectionChanges = true; |
| 79 | + }, |
| 80 | + |
| 81 | + handleImportComplete () { |
| 82 | + const { props } = this; |
| 83 | + props.ignoreSelectionChanges = false; |
| 84 | + }, |
| 85 | + |
| 86 | + handleExportStart () { |
| 87 | + this.updateCurrentHistoryState(); |
| 88 | + }, |
| 89 | + |
| 90 | + handleSelectionChange () { |
| 91 | + const { props } = this; |
| 92 | + if (!props.ignoreSelectionChanges) { |
| 93 | + this.updateCurrentHistoryState(); |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + updateCurrentHistoryState () { |
| 98 | + const { props } = this; |
| 99 | + const { history, currentHistoryIndex } = props; |
| 100 | + const currentHistoryState = history[currentHistoryIndex]; |
| 101 | + |
| 102 | + if (currentHistoryState) { |
| 103 | + this.cacheSelectionRangeOnState(currentHistoryState); |
| 104 | + } |
| 105 | + }, |
| 106 | + |
| 107 | + createHistoryState () { |
| 108 | + const { props } = this; |
| 109 | + |
| 110 | + if (!props.contentEditableElem) { return; } |
| 111 | + |
| 112 | + const editableContentString = DOM.nodesToHTMLString(DOM.cloneNodes(props.contentEditableElem, { trim: true })).replace(/\u200B/g, ''); |
| 113 | + const historyState = { |
| 114 | + editableContentString, |
| 115 | + }; |
| 116 | + |
| 117 | + this.cacheSelectionRangeOnState(historyState); |
| 118 | + |
| 119 | + return historyState; |
| 120 | + }, |
| 121 | + |
| 122 | + cacheSelectionRangeOnState (state) { |
| 123 | + const { mediator } = this; |
| 124 | + state.selectionRangeCoordinates = mediator.get('selection:range:coordinates'); |
| 125 | + }, |
| 126 | + |
| 127 | + analyzeStates (states) { |
| 128 | + const { |
| 129 | + current, |
| 130 | + previous, |
| 131 | + beforePrevious, |
| 132 | + next |
| 133 | + } = states; |
| 134 | + let isUndo = beforePrevious && current.editableContentString === beforePrevious.editableContentString; |
| 135 | + let isRedo = next && current.editableContentString === next.editableContentString; |
| 136 | + let noChange = previous && current.editableContentString === previous.editableContentString; |
| 137 | + |
| 138 | + isUndo = isUndo || false; |
| 139 | + isRedo = isRedo || false; |
| 140 | + noChange = noChange || false; |
| 141 | + |
| 142 | + return { |
| 143 | + isUndo, |
| 144 | + isRedo, |
| 145 | + noChange |
| 146 | + }; |
| 147 | + } |
| 148 | + } |
| 149 | +}); |
| 150 | + |
| 151 | +export default Undo; |
0 commit comments