Advertisement
KoctrX

Untitled

Oct 7th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. import Utils from './Utils';
  2. import _, { clone } from 'lodash';
  3. import { store } from '../store';
  4.  
  5. export default new class {
  6. constructor() {
  7. this.store = [];
  8. this.redoStore = [];
  9. this.selectedChangeId = false;
  10.  
  11. this.lockStoreAdding = false;
  12. }
  13.  
  14. addToStore(data, type = 'default') {
  15. if (!data || this.lockStoreAdding) return;
  16.  
  17. console.log('ADDED TO STORE!');
  18. this.redoStore = [];
  19.  
  20. const storeData = _.cloneDeep({
  21. id: Utils.uuidv4(),
  22. time: Date.now(),
  23. data, type
  24. });
  25.  
  26. this.store.push(storeData);
  27. this.selectedChangeId = storeData.id;
  28. }
  29.  
  30. get isUndo() {
  31. return !!this.store.length && !this.lockStoreAdding;
  32. }
  33.  
  34. get isRedo() {
  35. return !!this.redoStore.length && !this.lockStoreAdding;
  36. }
  37.  
  38. get lastStore() {
  39. // let currentState = this.store.find(st => st.id == this.selectedChangeId);
  40. // if (!currentState || !this.selectedChangeId) {
  41. return this.store[this.store.length - 1];
  42. // }
  43.  
  44. // return currentState;
  45. }
  46.  
  47. get lastRedoStore() {
  48. // let currentState = this.redoStore.find(st => st.id == this.selectedChangeId);
  49. // if (!currentState || !this.selectedChangeId) {
  50. return this.redoStore[this.redoStore.length - 1];
  51. // }
  52.  
  53. // return currentState;
  54. }
  55.  
  56. undoControlChange() {
  57. const cr = store.state.editor.canvasRender;
  58. const allControllEvents = this.store.filter(st => ['control_moved', 'control_scaled'].includes(st.type));
  59. const lastControllPositionChanged = allControllEvents[allControllEvents.length - 1];
  60. const controlElement = cr.control_canvas.getObjects().find(o => o.xsprite.id == this.lastStore.data.id);
  61.  
  62. if (controlElement) {
  63. let opts = {};
  64. if (lastControllPositionChanged) {
  65. opts = lastControllPositionChanged.data.opts;
  66. } else {
  67. const addedElement = this.store.find(st => st.type == 'add_timeline'
  68. && st.data.id == this.lastStore.data.id);
  69.  
  70. if (addedElement) {
  71. opts = _.pick(addedElement.data.control,
  72. ['scaleX', 'scaleY', 'width', 'height', 'left', 'top']
  73. );
  74. } else {
  75. console.error('Not found add event!');
  76. }
  77. }
  78.  
  79. controlElement.set(opts);
  80. cr._movedEvent({ target: controlElement, _updateAll: true });
  81.  
  82. cr.control_canvas.renderAll();
  83. } else {
  84. console.error('Control element not found...');
  85. }
  86. }
  87.  
  88. async undo() {
  89. if (!this.isUndo) return;
  90. if (this.lastStore.id == this.selectedChangeId
  91. && !['add_timeline'].includes(this.lastStore.type)) {
  92. this.redoStore.push(_.cloneDeep(this.lastStore));
  93. this.store = this.store.filter(st => st.id != this.lastStore.id);
  94.  
  95. return await this.undo();
  96. }
  97.  
  98. this.lockStoreAdding = true;
  99. switch (this.lastStore.type) {
  100. case 'add_timeline': {
  101. if (this.selectedChangeId != this.lastStore.id) {
  102. this.undoControlChange();
  103. } else {
  104. await store.dispatch('deleteElementById', { elementId: this.lastStore.data.item.id });
  105. }
  106.  
  107. break;
  108. }
  109.  
  110. case 'control_moved':
  111. case 'control_scaled': {
  112. await this.undoControlChange();
  113.  
  114. break;
  115. }
  116. }
  117.  
  118. if (this.lastStore.type == 'add_timeline' && this.selectedChangeId != this.lastStore.id) {
  119. this.selectedChangeId = this.lastStore.id;
  120. } else {
  121. if(this.lastStore.type != 'add_timeline') {
  122. this.selectedChangeId = this.lastStore.id;
  123. }
  124.  
  125. this.redoStore.push(_.cloneDeep(this.lastStore));
  126. this.store = this.store.filter(st => st.id != this.lastStore.id);
  127. }
  128.  
  129. this.lockStoreAdding = false;
  130. }
  131.  
  132. async redo() {
  133. if (!this.isRedo) return;
  134.  
  135. if (this.lastRedoStore.id == this.selectedChangeId
  136. && !['add_timeline'].includes(this.lastRedoStore.type)) {
  137. this.store.push(_.cloneDeep(this.lastRedoStore));
  138. this.redoStore = this.redoStore.filter(st => st.id != this.lastRedoStore.id);
  139.  
  140. return await this.redo();
  141. }
  142.  
  143. this.lockStoreAdding = true;
  144. const cr = store.state.editor.canvasRender;
  145.  
  146. switch (this.lastRedoStore.type) {
  147. case 'add_timeline': {
  148. const cloneItem = _.cloneDeep(this.lastRedoStore.data.item);
  149. store.state.timeline.data.push(_.cloneDeep({
  150. ...this.lastRedoStore.data.stack,
  151. items: [this.lastRedoStore.data.item]
  152. }));
  153.  
  154. await store.dispatch('addFileToCanvas', cloneItem);
  155.  
  156. if (cloneItem.filedata && /video/g.test(cloneItem.filedata.type))
  157. store.dispatch('generateVideoThumbs', { item: cloneItem });
  158. break;
  159. }
  160.  
  161. case 'control_moved':
  162. case 'control_scaled': {
  163. const key = this.lastRedoStore.type == 'control_moved' ? '_movedEvent' : '_scaledEvent';
  164. const controlElement = cr.control_canvas.getObjects().find(o => o.xsprite.id == this.lastRedoStore.data.id);
  165. if (controlElement) {
  166. controlElement.set(this.lastRedoStore.data.opts);
  167. cr[key]({ target: controlElement, _updateAll: true });
  168.  
  169. cr.control_canvas.renderAll();
  170. } else {
  171. console.error('Control element not found...');
  172. }
  173.  
  174. break;
  175. }
  176. }
  177.  
  178. this.selectedChangeId = this.lastRedoStore.id;
  179. this.store.push(_.cloneDeep(this.lastRedoStore));
  180. this.redoStore = this.redoStore.filter(st => st.id != this.lastRedoStore.id);
  181.  
  182. this.lockStoreAdding = false;
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement