history.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import Delta from 'quill-delta';
  2. import Quill from '../../../core';
  3. import { getLastChangeIndex } from '../../../modules/history';
  4. describe('History', function() {
  5. describe('getLastChangeIndex', function() {
  6. it('delete', function() {
  7. let delta = new Delta().retain(4).delete(2);
  8. expect(getLastChangeIndex(delta)).toEqual(4);
  9. });
  10. it('delete with inserts', function() {
  11. let delta = new Delta().retain(4).insert('test').delete(2);
  12. expect(getLastChangeIndex(delta)).toEqual(8);
  13. });
  14. it('insert text', function() {
  15. let delta = new Delta().retain(4).insert('testing');
  16. expect(getLastChangeIndex(delta)).toEqual(11);
  17. });
  18. it('insert embed', function() {
  19. let delta = new Delta().retain(4).insert({ image: true });
  20. expect(getLastChangeIndex(delta)).toEqual(5);
  21. });
  22. it('insert with deletes', function() {
  23. let delta = new Delta().retain(4).delete(3).insert('!');
  24. expect(getLastChangeIndex(delta)).toEqual(5);
  25. });
  26. it('format', function() {
  27. let delta = new Delta().retain(4).retain(3, { bold: true });
  28. expect(getLastChangeIndex(delta)).toEqual(7);
  29. });
  30. it('format newline', function() {
  31. let delta = new Delta().retain(4).retain(1, { align: 'left' });
  32. expect(getLastChangeIndex(delta)).toEqual(4);
  33. });
  34. it('format mixed', function() {
  35. let delta = new Delta().retain(4).retain(1, { align: 'left', bold: true });
  36. expect(getLastChangeIndex(delta)).toEqual(4);
  37. });
  38. it('insert newline', function() {
  39. let delta = new Delta().retain(4).insert('a\n');
  40. expect(getLastChangeIndex(delta)).toEqual(5);
  41. });
  42. it('mutliple newline inserts', function() {
  43. let delta = new Delta().retain(4).insert('ab\n\n');
  44. expect(getLastChangeIndex(delta)).toEqual(7);
  45. });
  46. });
  47. describe('undo/redo', function() {
  48. beforeEach(function() {
  49. this.initialize(HTMLElement, '<div><p>The lazy fox</p></div>');
  50. this.quill = new Quill(this.container.firstChild, {
  51. modules: {
  52. 'history': { delay: 400 }
  53. }
  54. });
  55. this.original = this.quill.getContents();
  56. });
  57. it('limits undo stack size', function () {
  58. let quill = new Quill(this.container.firstChild, {
  59. modules: {
  60. history: { delay: 0, maxStack: 2 }
  61. }
  62. });
  63. ['A', 'B', 'C'].forEach(function(text) {
  64. quill.insertText(0, text)
  65. });
  66. expect(quill.history.stack.undo.length).toEqual(2);
  67. });
  68. it('user change', function() {
  69. this.quill.root.firstChild.innerHTML = 'The lazy foxes';
  70. this.quill.update();
  71. let changed = this.quill.getContents();
  72. expect(changed).not.toEqual(this.original);
  73. this.quill.history.undo();
  74. expect(this.quill.getContents()).toEqual(this.original);
  75. this.quill.history.redo();
  76. expect(this.quill.getContents()).toEqual(changed);
  77. });
  78. it('merge changes', function() {
  79. expect(this.quill.history.stack.undo.length).toEqual(0);
  80. this.quill.updateContents(new Delta().retain(12).insert('e'));
  81. expect(this.quill.history.stack.undo.length).toEqual(1);
  82. this.quill.updateContents(new Delta().retain(13).insert('s'));
  83. expect(this.quill.history.stack.undo.length).toEqual(1);
  84. this.quill.history.undo();
  85. expect(this.quill.getContents()).toEqual(this.original);
  86. expect(this.quill.history.stack.undo.length).toEqual(0);
  87. });
  88. it('dont merge changes', function(done) {
  89. expect(this.quill.history.stack.undo.length).toEqual(0);
  90. this.quill.updateContents(new Delta().retain(12).insert('e'));
  91. expect(this.quill.history.stack.undo.length).toEqual(1);
  92. setTimeout(() => {
  93. this.quill.updateContents(new Delta().retain(13).insert('s'));
  94. expect(this.quill.history.stack.undo.length).toEqual(2);
  95. done();
  96. }, this.quill.history.options.delay * 1.25);
  97. });
  98. it('multiple undos', function(done) {
  99. expect(this.quill.history.stack.undo.length).toEqual(0);
  100. this.quill.updateContents(new Delta().retain(12).insert('e'));
  101. let contents = this.quill.getContents();
  102. setTimeout(() => {
  103. this.quill.updateContents(new Delta().retain(13).insert('s'));
  104. this.quill.history.undo();
  105. expect(this.quill.getContents()).toEqual(contents);
  106. this.quill.history.undo();
  107. expect(this.quill.getContents()).toEqual(this.original);
  108. done();
  109. }, this.quill.history.options.delay * 1.25);
  110. });
  111. it('transform api change', function() {
  112. this.quill.history.options.userOnly = true;
  113. this.quill.updateContents(new Delta().retain(12).insert('es'), Quill.sources.USER);
  114. this.quill.history.lastRecorded = 0;
  115. this.quill.updateContents(new Delta().retain(14).insert('!'), Quill.sources.USER);
  116. this.quill.history.undo();
  117. this.quill.updateContents(new Delta().retain(4).delete(5), Quill.sources.API);
  118. expect(this.quill.getContents()).toEqual(new Delta().insert('The foxes\n'));
  119. this.quill.history.undo();
  120. expect(this.quill.getContents()).toEqual(new Delta().insert('The fox\n'));
  121. this.quill.history.redo();
  122. expect(this.quill.getContents()).toEqual(new Delta().insert('The foxes\n'));
  123. this.quill.history.redo();
  124. expect(this.quill.getContents()).toEqual(new Delta().insert('The foxes!\n'));
  125. });
  126. it('transform preserve intention', function() {
  127. let url = 'https://www.google.com/';
  128. this.quill.history.options.userOnly = true;
  129. this.quill.updateContents(new Delta().insert(url, { link: url }), Quill.sources.USER);
  130. this.quill.history.lastRecorded = 0;
  131. this.quill.updateContents(new Delta().delete(url.length).insert('Google', { link: url }), Quill.sources.API);
  132. this.quill.history.lastRecorded = 0;
  133. this.quill.updateContents(new Delta().retain(this.quill.getLength()-1).insert('!'), Quill.sources.USER);
  134. this.quill.history.lastRecorded = 0;
  135. expect(this.quill.getContents()).toEqual(new Delta().insert('Google', { link: url }).insert('The lazy fox!\n'));
  136. this.quill.history.undo();
  137. expect(this.quill.getContents()).toEqual(new Delta().insert('Google', { link: url }).insert('The lazy fox\n'));
  138. this.quill.history.undo();
  139. expect(this.quill.getContents()).toEqual(new Delta().insert('Google', { link: url }).insert('The lazy fox\n'));
  140. });
  141. it('ignore remote changes', function() {
  142. this.quill.history.options.delay = 0;
  143. this.quill.history.options.userOnly = true;
  144. this.quill.setText('\n');
  145. this.quill.insertText(0, 'a', Quill.sources.USER);
  146. this.quill.insertText(1, 'b', Quill.sources.API);
  147. this.quill.insertText(2, 'c', Quill.sources.USER);
  148. this.quill.insertText(3, 'd', Quill.sources.API);
  149. expect(this.quill.getText()).toEqual('abcd\n');
  150. this.quill.history.undo();
  151. expect(this.quill.getText()).toEqual('abd\n');
  152. this.quill.history.undo();
  153. expect(this.quill.getText()).toEqual('bd\n');
  154. this.quill.history.redo();
  155. expect(this.quill.getText()).toEqual('abd\n');
  156. this.quill.history.redo();
  157. expect(this.quill.getText()).toEqual('abcd\n');
  158. });
  159. });
  160. });