History.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. SC.loadPackage({ 'History': {
  2. comment: 'I manage the history of the editing session.',
  3. properties: {
  4. stack: { comment: 'I hold the action stack.' },
  5. stackPointer: { comment: 'I hold the action stack.' },
  6. actionInProgress: { comment: 'Wether an action hast started and not succeeded nor failed yet.' },
  7. temporaryRestoreFunc: { comment: 'During actionInProgress a store temporarily the restoreFunction.' }
  8. },
  9. methods: {
  10. init: {
  11. comment: 'I initialise the history object.',
  12. code: function(){
  13. this.set({
  14. stack: [],
  15. stackPointer: 0,
  16. actionInProgress: false
  17. });
  18. }
  19. },
  20. undo: {
  21. comment: 'I undo.',
  22. code: function(){
  23. if(this.get('stackPointer') === 0){
  24. return;
  25. }
  26. this.set({ stackPointer: (this.get('stackPointer') - 1) });
  27. this.get('stack')[this.get('stackPointer')].restoreFunction.call();
  28. }
  29. },
  30. redo: {
  31. comment: 'I redo.',
  32. code: function(){
  33. if(this.get('stackPointer') >= this.get('stack').length){
  34. return;
  35. }
  36. this.get('stack')[this.get('stackPointer')].repeatFunction.call();
  37. this.set({ stackPointer: (this.get('stackPointer') + 1) });
  38. }
  39. },
  40. actionHasStarted: {
  41. comment: 'I undo.',
  42. code: function(restoreFunction){
  43. this.set({
  44. actionInProgress: true,
  45. temporaryRestoreFunc: restoreFunction
  46. });
  47. }
  48. },
  49. actionHasSucceeded: {
  50. comment: 'I undo.',
  51. code: function(repeatFunction){
  52. if(this.get('actionInProgress') === false){
  53. console.log('cleared corrupted history')
  54. this.do('init');
  55. return;
  56. }
  57. this.set({ actionInProgress: false });
  58. this.get('stack')[this.get('stackPointer')] = {
  59. restoreFunction: this.get('temporaryRestoreFunc'),
  60. repeatFunction: repeatFunction
  61. };
  62. this.set({ stackPointer: (1 + this.get('stackPointer')) });
  63. if(this.get('stack').length > this.get('stackPointer')){
  64. this.get('stack').splice(this.get('stackPointer'));
  65. }
  66. if(this.get('stack').length > 40){
  67. this.get('stack').splice(0, 1);
  68. this.set({ stackPointer: (this.get('stackPointer') - 1) });
  69. }
  70. }
  71. }
  72. }
  73. }});