d52abb0e73426cfd2faa0f9af22d9b0bdf7ede75c416aba52391bcd6bbbb220ec7efb54a07a144e0c8d2c37f3c0c71f7e6b0033f5f8afcb1c41b7276abe38f 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Delta from 'quill-delta';
  2. import Quill from '../core/quill.js';
  3. import Module from '../core/module.js';
  4. import { TableCell, TableRow, TableBody, TableContainer, tableId } from '../formats/table.js';
  5. class Table extends Module {
  6. static register() {
  7. Quill.register(TableCell);
  8. Quill.register(TableRow);
  9. Quill.register(TableBody);
  10. Quill.register(TableContainer);
  11. }
  12. constructor() {
  13. super(...arguments);
  14. this.listenBalanceCells();
  15. }
  16. balanceTables() {
  17. this.quill.scroll.descendants(TableContainer).forEach(table => {
  18. table.balanceCells();
  19. });
  20. }
  21. deleteColumn() {
  22. const [table,, cell] = this.getTable();
  23. if (cell == null) return;
  24. // @ts-expect-error
  25. table.deleteColumn(cell.cellOffset());
  26. this.quill.update(Quill.sources.USER);
  27. }
  28. deleteRow() {
  29. const [, row] = this.getTable();
  30. if (row == null) return;
  31. row.remove();
  32. this.quill.update(Quill.sources.USER);
  33. }
  34. deleteTable() {
  35. const [table] = this.getTable();
  36. if (table == null) return;
  37. // @ts-expect-error
  38. const offset = table.offset();
  39. // @ts-expect-error
  40. table.remove();
  41. this.quill.update(Quill.sources.USER);
  42. this.quill.setSelection(offset, Quill.sources.SILENT);
  43. }
  44. getTable() {
  45. let range = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.quill.getSelection();
  46. if (range == null) return [null, null, null, -1];
  47. const [cell, offset] = this.quill.getLine(range.index);
  48. if (cell == null || cell.statics.blotName !== TableCell.blotName) {
  49. return [null, null, null, -1];
  50. }
  51. const row = cell.parent;
  52. const table = row.parent.parent;
  53. // @ts-expect-error
  54. return [table, row, cell, offset];
  55. }
  56. insertColumn(offset) {
  57. const range = this.quill.getSelection();
  58. if (!range) return;
  59. const [table, row, cell] = this.getTable(range);
  60. if (cell == null) return;
  61. const column = cell.cellOffset();
  62. table.insertColumn(column + offset);
  63. this.quill.update(Quill.sources.USER);
  64. let shift = row.rowOffset();
  65. if (offset === 0) {
  66. shift += 1;
  67. }
  68. this.quill.setSelection(range.index + shift, range.length, Quill.sources.SILENT);
  69. }
  70. insertColumnLeft() {
  71. this.insertColumn(0);
  72. }
  73. insertColumnRight() {
  74. this.insertColumn(1);
  75. }
  76. insertRow(offset) {
  77. const range = this.quill.getSelection();
  78. if (!range) return;
  79. const [table, row, cell] = this.getTable(range);
  80. if (cell == null) return;
  81. const index = row.rowOffset();
  82. table.insertRow(index + offset);
  83. this.quill.update(Quill.sources.USER);
  84. if (offset > 0) {
  85. this.quill.setSelection(range, Quill.sources.SILENT);
  86. } else {
  87. this.quill.setSelection(range.index + row.children.length, range.length, Quill.sources.SILENT);
  88. }
  89. }
  90. insertRowAbove() {
  91. this.insertRow(0);
  92. }
  93. insertRowBelow() {
  94. this.insertRow(1);
  95. }
  96. insertTable(rows, columns) {
  97. const range = this.quill.getSelection();
  98. if (range == null) return;
  99. const delta = new Array(rows).fill(0).reduce(memo => {
  100. const text = new Array(columns).fill('\n').join('');
  101. return memo.insert(text, {
  102. table: tableId()
  103. });
  104. }, new Delta().retain(range.index));
  105. this.quill.updateContents(delta, Quill.sources.USER);
  106. this.quill.setSelection(range.index, Quill.sources.SILENT);
  107. this.balanceTables();
  108. }
  109. listenBalanceCells() {
  110. this.quill.on(Quill.events.SCROLL_OPTIMIZE, mutations => {
  111. mutations.some(mutation => {
  112. if (['TD', 'TR', 'TBODY', 'TABLE'].includes(mutation.target.tagName)) {
  113. this.quill.once(Quill.events.TEXT_CHANGE, (delta, old, source) => {
  114. if (source !== Quill.sources.USER) return;
  115. this.balanceTables();
  116. });
  117. return true;
  118. }
  119. return false;
  120. });
  121. });
  122. }
  123. }
  124. export default Table;
  125. //# sourceMappingURL=table.js.map