toolbar.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import Quill from '../../../core/quill';
  2. import { addControls } from '../../../modules/toolbar';
  3. describe('Toolbar', function() {
  4. describe('add controls', function() {
  5. it('single level', function() {
  6. addControls(this.container, ['bold', 'italic']);
  7. expect(this.container).toEqualHTML(`
  8. <span class="ql-formats">
  9. <button type="button" class="ql-bold"></button>
  10. <button type="button" class="ql-italic"></button>
  11. </span>
  12. `);
  13. });
  14. it('nested group', function() {
  15. addControls(this.container, [
  16. ['bold', 'italic'],
  17. ['underline', 'strike'],
  18. ]);
  19. expect(this.container).toEqualHTML(`
  20. <span class="ql-formats">
  21. <button type="button" class="ql-bold"></button>
  22. <button type="button" class="ql-italic"></button>
  23. </span>
  24. <span class="ql-formats">
  25. <button type="button" class="ql-underline"></button>
  26. <button type="button" class="ql-strike"></button>
  27. </span>
  28. `);
  29. });
  30. it('button value', function() {
  31. addControls(this.container, ['bold', { header: '2' }]);
  32. expect(this.container).toEqualHTML(`
  33. <span class="ql-formats">
  34. <button type="button" class="ql-bold"></button>
  35. <button type="button" class="ql-header" value="2"></button>
  36. </span>
  37. `);
  38. });
  39. it('select', function() {
  40. addControls(this.container, [{ size: ['10px', false, '18px', '32px'] }]);
  41. expect(this.container).toEqualHTML(`
  42. <span class="ql-formats">
  43. <select class="ql-size">
  44. <option value="10px"></option>
  45. <option selected="selected"></option>
  46. <option value="18px"></option>
  47. <option value="32px"></option>
  48. </select>
  49. </span>
  50. `);
  51. });
  52. it('everything', function() {
  53. addControls(this.container, [
  54. [
  55. { font: [false, 'sans-serif', 'monospace'] },
  56. { size: ['10px', false, '18px', '32px'] },
  57. ],
  58. ['bold', 'italic', 'underline', 'strike'],
  59. [
  60. { list: 'ordered' },
  61. { list: 'bullet' },
  62. { align: [false, 'center', 'right', 'justify'] },
  63. ],
  64. ['link', 'image'],
  65. ]);
  66. expect(this.container).toEqualHTML(`
  67. <span class="ql-formats">
  68. <select class="ql-font">
  69. <option selected="selected"></option>
  70. <option value="sans-serif"></option>
  71. <option value="monospace"></option>
  72. </select>
  73. <select class="ql-size">
  74. <option value="10px"></option>
  75. <option selected="selected"></option>
  76. <option value="18px"></option>
  77. <option value="32px"></option>
  78. </select>
  79. </span>
  80. <span class="ql-formats">
  81. <button type="button" class="ql-bold"></button>
  82. <button type="button" class="ql-italic"></button>
  83. <button type="button" class="ql-underline"></button>
  84. <button type="button" class="ql-strike"></button>
  85. </span>
  86. <span class="ql-formats">
  87. <button type="button" class="ql-list" value="ordered"></button>
  88. <button type="button" class="ql-list" value="bullet"></button>
  89. <select class="ql-align">
  90. <option selected="selected"></option>
  91. <option value="center"></option>
  92. <option value="right"></option>
  93. <option value="justify"></option>
  94. </select>
  95. </span>
  96. <span class="ql-formats">
  97. <button type="button" class="ql-link"></button>
  98. <button type="button" class="ql-image"></button>
  99. </span>
  100. `);
  101. });
  102. });
  103. describe('active', function() {
  104. beforeEach(function() {
  105. const container = this.initialize(
  106. HTMLElement,
  107. `
  108. <p>0123</p>
  109. <p><strong>5678</strong></p>
  110. <p><a href="http://quilljs.com/">0123</a></p>
  111. <p class="ql-align-center">5678</p>
  112. <p><span class="ql-size-small">01</span><span class="ql-size-large">23</span></p>
  113. `,
  114. );
  115. this.quill = new Quill(container, {
  116. modules: {
  117. toolbar: [
  118. ['bold', 'link'],
  119. [{ size: ['small', false, 'large'] }],
  120. [{ align: '' }, { align: 'center' }],
  121. ],
  122. },
  123. theme: 'snow',
  124. });
  125. });
  126. it('toggle button', function() {
  127. const boldButton = this.container.parentNode.querySelector(
  128. 'button.ql-bold',
  129. );
  130. this.quill.setSelection(7);
  131. expect(boldButton.classList.contains('ql-active')).toBe(true);
  132. this.quill.setSelection(2);
  133. expect(boldButton.classList.contains('ql-active')).toBe(false);
  134. });
  135. it('link', function() {
  136. const linkButton = this.container.parentNode.querySelector(
  137. 'button.ql-link',
  138. );
  139. this.quill.setSelection(12);
  140. expect(linkButton.classList.contains('ql-active')).toBe(true);
  141. this.quill.setSelection(2);
  142. expect(linkButton.classList.contains('ql-active')).toBe(false);
  143. });
  144. it('dropdown', function() {
  145. const sizeSelect = this.container.parentNode.querySelector(
  146. 'select.ql-size',
  147. );
  148. this.quill.setSelection(21);
  149. expect(sizeSelect.selectedIndex).toEqual(0);
  150. this.quill.setSelection(23);
  151. expect(sizeSelect.selectedIndex).toEqual(2);
  152. this.quill.setSelection(21, 2);
  153. expect(sizeSelect.selectedIndex).toBeLessThan(0);
  154. this.quill.setSelection(2);
  155. expect(sizeSelect.selectedIndex).toEqual(1);
  156. });
  157. it('custom button', function() {
  158. const centerButton = this.container.parentNode.querySelector(
  159. 'button.ql-align[value="center"]',
  160. );
  161. const leftButton = this.container.parentNode.querySelector(
  162. 'button.ql-align[value]',
  163. );
  164. this.quill.setSelection(17);
  165. expect(centerButton.classList.contains('ql-active')).toBe(true);
  166. expect(leftButton.classList.contains('ql-active')).toBe(false);
  167. this.quill.setSelection(2);
  168. expect(centerButton.classList.contains('ql-active')).toBe(false);
  169. expect(leftButton.classList.contains('ql-active')).toBe(true);
  170. this.quill.blur();
  171. expect(centerButton.classList.contains('ql-active')).toBe(false);
  172. expect(leftButton.classList.contains('ql-active')).toBe(false);
  173. });
  174. });
  175. });