@hailin-zheng/editor-core 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/lib/doc-ruler.d.ts +51 -0
  2. package/lib/doc-ruler.js +313 -0
  3. package/lib/doc-ruler.js.map +1 -0
  4. package/lib/framework/common-util.d.ts +63 -0
  5. package/lib/framework/common-util.js +178 -0
  6. package/lib/framework/common-util.js.map +1 -0
  7. package/lib/framework/document-change.d.ts +265 -0
  8. package/lib/framework/document-change.js +1342 -0
  9. package/lib/framework/document-change.js.map +1 -0
  10. package/lib/framework/document-combine.d.ts +24 -0
  11. package/lib/framework/document-combine.js +40 -0
  12. package/lib/framework/document-combine.js.map +1 -0
  13. package/lib/framework/document-comment.d.ts +46 -0
  14. package/lib/framework/document-comment.js +148 -0
  15. package/lib/framework/document-comment.js.map +1 -0
  16. package/lib/framework/document-context.d.ts +149 -0
  17. package/lib/framework/document-context.js +330 -0
  18. package/lib/framework/document-context.js.map +1 -0
  19. package/lib/framework/document-eval-func.d.ts +18 -0
  20. package/lib/framework/document-eval-func.js +48 -0
  21. package/lib/framework/document-eval-func.js.map +1 -0
  22. package/lib/framework/document-event.d.ts +213 -0
  23. package/lib/framework/document-event.js +1054 -0
  24. package/lib/framework/document-event.js.map +1 -0
  25. package/lib/framework/document-history.d.ts +26 -0
  26. package/lib/framework/document-history.js +65 -0
  27. package/lib/framework/document-history.js.map +1 -0
  28. package/lib/framework/document-images-loader.d.ts +16 -0
  29. package/lib/framework/document-images-loader.js +66 -0
  30. package/lib/framework/document-images-loader.js.map +1 -0
  31. package/lib/framework/document-input-cursor.d.ts +78 -0
  32. package/lib/framework/document-input-cursor.js +239 -0
  33. package/lib/framework/document-input-cursor.js.map +1 -0
  34. package/lib/framework/document-paint.d.ts +34 -0
  35. package/lib/framework/document-paint.js +103 -0
  36. package/lib/framework/document-paint.js.map +1 -0
  37. package/lib/framework/document-print-offscreen.d.ts +38 -0
  38. package/lib/framework/document-print-offscreen.js +128 -0
  39. package/lib/framework/document-print-offscreen.js.map +1 -0
  40. package/lib/framework/document-print.d.ts +60 -0
  41. package/lib/framework/document-print.js +203 -0
  42. package/lib/framework/document-print.js.map +1 -0
  43. package/lib/framework/document-segmenter.d.ts +2 -0
  44. package/lib/framework/document-segmenter.js +106 -0
  45. package/lib/framework/document-segmenter.js.map +1 -0
  46. package/lib/framework/document-selection.d.ts +89 -0
  47. package/lib/framework/document-selection.js +358 -0
  48. package/lib/framework/document-selection.js.map +1 -0
  49. package/lib/framework/document-template.d.ts +4 -0
  50. package/lib/framework/document-template.js +20 -0
  51. package/lib/framework/document-template.js.map +1 -0
  52. package/lib/framework/document-textline-mode.d.ts +7 -0
  53. package/lib/framework/document-textline-mode.js +30 -0
  54. package/lib/framework/document-textline-mode.js.map +1 -0
  55. package/lib/framework/element-define.d.ts +319 -0
  56. package/lib/framework/element-define.js +579 -0
  57. package/lib/framework/element-define.js.map +1 -0
  58. package/lib/framework/element-event-define.d.ts +99 -0
  59. package/lib/framework/element-event-define.js +108 -0
  60. package/lib/framework/element-event-define.js.map +1 -0
  61. package/lib/framework/element-measure.d.ts +97 -0
  62. package/lib/framework/element-measure.js +551 -0
  63. package/lib/framework/element-measure.js.map +1 -0
  64. package/lib/framework/element-paint.d.ts +42 -0
  65. package/lib/framework/element-paint.js +170 -0
  66. package/lib/framework/element-paint.js.map +1 -0
  67. package/lib/framework/element-props.d.ts +301 -0
  68. package/lib/framework/element-props.js +809 -0
  69. package/lib/framework/element-props.js.map +1 -0
  70. package/lib/framework/element-reader.d.ts +19 -0
  71. package/lib/framework/element-reader.js +151 -0
  72. package/lib/framework/element-reader.js.map +1 -0
  73. package/lib/framework/element-render-cut.d.ts +55 -0
  74. package/lib/framework/element-render-cut.js +449 -0
  75. package/lib/framework/element-render-cut.js.map +1 -0
  76. package/lib/framework/element-serialize.d.ts +30 -0
  77. package/lib/framework/element-serialize.js +113 -0
  78. package/lib/framework/element-serialize.js.map +1 -0
  79. package/lib/framework/element-util.d.ts +369 -0
  80. package/lib/framework/element-util.js +1463 -0
  81. package/lib/framework/element-util.js.map +1 -0
  82. package/lib/framework/impl/checkbox/checkbox-impl.d.ts +24 -0
  83. package/lib/framework/impl/checkbox/checkbox-impl.js +64 -0
  84. package/lib/framework/impl/checkbox/checkbox-impl.js.map +1 -0
  85. package/lib/framework/impl/comments/comment-content-impl.d.ts +40 -0
  86. package/lib/framework/impl/comments/comment-content-impl.js +105 -0
  87. package/lib/framework/impl/comments/comment-content-impl.js.map +1 -0
  88. package/lib/framework/impl/comments/comment-element-impl.d.ts +21 -0
  89. package/lib/framework/impl/comments/comment-element-impl.js +62 -0
  90. package/lib/framework/impl/comments/comment-element-impl.js.map +1 -0
  91. package/lib/framework/impl/comments/comments-container-impl.d.ts +22 -0
  92. package/lib/framework/impl/comments/comments-container-impl.js +62 -0
  93. package/lib/framework/impl/comments/comments-container-impl.js.map +1 -0
  94. package/lib/framework/impl/comments/comments-util.d.ts +12 -0
  95. package/lib/framework/impl/comments/comments-util.js +67 -0
  96. package/lib/framework/impl/comments/comments-util.js.map +1 -0
  97. package/lib/framework/impl/comments/validate-msg-impl.d.ts +21 -0
  98. package/lib/framework/impl/comments/validate-msg-impl.js +83 -0
  99. package/lib/framework/impl/comments/validate-msg-impl.js.map +1 -0
  100. package/lib/framework/impl/data-element/data-decorate-impl.d.ts +30 -0
  101. package/lib/framework/impl/data-element/data-decorate-impl.js +91 -0
  102. package/lib/framework/impl/data-element/data-decorate-impl.js.map +1 -0
  103. package/lib/framework/impl/data-element/data-element-barcode.d.ts +31 -0
  104. package/lib/framework/impl/data-element/data-element-barcode.js +115 -0
  105. package/lib/framework/impl/data-element/data-element-barcode.js.map +1 -0
  106. package/lib/framework/impl/data-element/data-element-base-impl.d.ts +68 -0
  107. package/lib/framework/impl/data-element/data-element-base-impl.js +205 -0
  108. package/lib/framework/impl/data-element/data-element-base-impl.js.map +1 -0
  109. package/lib/framework/impl/data-element/data-element-check-impl.d.ts +35 -0
  110. package/lib/framework/impl/data-element/data-element-check-impl.js +133 -0
  111. package/lib/framework/impl/data-element/data-element-check-impl.js.map +1 -0
  112. package/lib/framework/impl/data-element/data-element-date-impl.d.ts +23 -0
  113. package/lib/framework/impl/data-element/data-element-date-impl.js +111 -0
  114. package/lib/framework/impl/data-element/data-element-date-impl.js.map +1 -0
  115. package/lib/framework/impl/data-element/data-element-group-impl.d.ts +23 -0
  116. package/lib/framework/impl/data-element/data-element-group-impl.js +130 -0
  117. package/lib/framework/impl/data-element/data-element-group-impl.js.map +1 -0
  118. package/lib/framework/impl/data-element/data-element-image-impl.d.ts +30 -0
  119. package/lib/framework/impl/data-element/data-element-image-impl.js +137 -0
  120. package/lib/framework/impl/data-element/data-element-image-impl.js.map +1 -0
  121. package/lib/framework/impl/data-element/data-element-list-impl.d.ts +22 -0
  122. package/lib/framework/impl/data-element/data-element-list-impl.js +131 -0
  123. package/lib/framework/impl/data-element/data-element-list-impl.js.map +1 -0
  124. package/lib/framework/impl/data-element/data-element-text-impl.d.ts +23 -0
  125. package/lib/framework/impl/data-element/data-element-text-impl.js +103 -0
  126. package/lib/framework/impl/data-element/data-element-text-impl.js.map +1 -0
  127. package/lib/framework/impl/decorate/fill-null-space-imple.d.ts +21 -0
  128. package/lib/framework/impl/decorate/fill-null-space-imple.js +43 -0
  129. package/lib/framework/impl/decorate/fill-null-space-imple.js.map +1 -0
  130. package/lib/framework/impl/document/doc-body-impl.d.ts +27 -0
  131. package/lib/framework/impl/document/doc-body-impl.js +79 -0
  132. package/lib/framework/impl/document/doc-body-impl.js.map +1 -0
  133. package/lib/framework/impl/document/doc-body-part-impl.d.ts +30 -0
  134. package/lib/framework/impl/document/doc-body-part-impl.js +94 -0
  135. package/lib/framework/impl/document/doc-body-part-impl.js.map +1 -0
  136. package/lib/framework/impl/document/doc-container-impl.d.ts +16 -0
  137. package/lib/framework/impl/document/doc-container-impl.js +21 -0
  138. package/lib/framework/impl/document/doc-container-impl.js.map +1 -0
  139. package/lib/framework/impl/document/doc-footer-impl.d.ts +26 -0
  140. package/lib/framework/impl/document/doc-footer-impl.js +77 -0
  141. package/lib/framework/impl/document/doc-footer-impl.js.map +1 -0
  142. package/lib/framework/impl/document/doc-header-impl.d.ts +26 -0
  143. package/lib/framework/impl/document/doc-header-impl.js +83 -0
  144. package/lib/framework/impl/document/doc-header-impl.js.map +1 -0
  145. package/lib/framework/impl/document/doc-impl.d.ts +61 -0
  146. package/lib/framework/impl/document/doc-impl.js +209 -0
  147. package/lib/framework/impl/document/doc-impl.js.map +1 -0
  148. package/lib/framework/impl/media-formula/menstrual-history.d.ts +35 -0
  149. package/lib/framework/impl/media-formula/menstrual-history.js +153 -0
  150. package/lib/framework/impl/media-formula/menstrual-history.js.map +1 -0
  151. package/lib/framework/impl/paragraph/p-impl.d.ts +45 -0
  152. package/lib/framework/impl/paragraph/p-impl.js +156 -0
  153. package/lib/framework/impl/paragraph/p-impl.js.map +1 -0
  154. package/lib/framework/impl/picture/image-impl.d.ts +31 -0
  155. package/lib/framework/impl/picture/image-impl.js +124 -0
  156. package/lib/framework/impl/picture/image-impl.js.map +1 -0
  157. package/lib/framework/impl/radio/radio-impl.d.ts +22 -0
  158. package/lib/framework/impl/radio/radio-impl.js +72 -0
  159. package/lib/framework/impl/radio/radio-impl.js.map +1 -0
  160. package/lib/framework/impl/symbol/br-symbol-impl.d.ts +22 -0
  161. package/lib/framework/impl/symbol/br-symbol-impl.js +54 -0
  162. package/lib/framework/impl/symbol/br-symbol-impl.js.map +1 -0
  163. package/lib/framework/impl/symbol/p-symbol-impl.d.ts +19 -0
  164. package/lib/framework/impl/symbol/p-symbol-impl.js +54 -0
  165. package/lib/framework/impl/symbol/p-symbol-impl.js.map +1 -0
  166. package/lib/framework/impl/table/table-cell-impl.d.ts +37 -0
  167. package/lib/framework/impl/table/table-cell-impl.js +145 -0
  168. package/lib/framework/impl/table/table-cell-impl.js.map +1 -0
  169. package/lib/framework/impl/table/table-impl.d.ts +55 -0
  170. package/lib/framework/impl/table/table-impl.js +363 -0
  171. package/lib/framework/impl/table/table-impl.js.map +1 -0
  172. package/lib/framework/impl/table/table-row-impl.d.ts +26 -0
  173. package/lib/framework/impl/table/table-row-impl.js +75 -0
  174. package/lib/framework/impl/table/table-row-impl.js.map +1 -0
  175. package/lib/framework/impl/table/table-split-cell-patch.d.ts +20 -0
  176. package/lib/framework/impl/table/table-split-cell-patch.js +89 -0
  177. package/lib/framework/impl/table/table-split-cell-patch.js.map +1 -0
  178. package/lib/framework/impl/table/table-split-cell.d.ts +90 -0
  179. package/lib/framework/impl/table/table-split-cell.js +464 -0
  180. package/lib/framework/impl/table/table-split-cell.js.map +1 -0
  181. package/lib/framework/impl/table/table-util.d.ts +150 -0
  182. package/lib/framework/impl/table/table-util.js +678 -0
  183. package/lib/framework/impl/table/table-util.js.map +1 -0
  184. package/lib/framework/impl/text/text-impl.d.ts +32 -0
  185. package/lib/framework/impl/text/text-impl.js +149 -0
  186. package/lib/framework/impl/text/text-impl.js.map +1 -0
  187. package/lib/framework/impl/text/track-run-impl.d.ts +27 -0
  188. package/lib/framework/impl/text/track-run-impl.js +112 -0
  189. package/lib/framework/impl/text/track-run-impl.js.map +1 -0
  190. package/lib/framework/notify.d.ts +13 -0
  191. package/lib/framework/notify.js +116 -0
  192. package/lib/framework/notify.js.map +1 -0
  193. package/lib/framework/range-util.d.ts +40 -0
  194. package/lib/framework/range-util.js +312 -0
  195. package/lib/framework/range-util.js.map +1 -0
  196. package/lib/framework/render-context.d.ts +91 -0
  197. package/lib/framework/render-context.js +384 -0
  198. package/lib/framework/render-context.js.map +1 -0
  199. package/lib/framework/render-define.d.ts +109 -0
  200. package/lib/framework/render-define.js +195 -0
  201. package/lib/framework/render-define.js.map +1 -0
  202. package/lib/framework/selection-overlays.d.ts +30 -0
  203. package/lib/framework/selection-overlays.js +124 -0
  204. package/lib/framework/selection-overlays.js.map +1 -0
  205. package/lib/texteditor.d.ts +318 -0
  206. package/lib/texteditor.js +913 -0
  207. package/lib/texteditor.js.map +1 -0
  208. package/lib/util/subject.d.ts +34 -0
  209. package/lib/util/subject.js +88 -0
  210. package/lib/util/subject.js.map +1 -0
  211. package/lib/util/table-bind.d.ts +5 -0
  212. package/lib/util/table-bind.js +6 -0
  213. package/lib/util/table-bind.js.map +1 -0
  214. package/package.json +1 -1
  215. package/tsconfig.json +1 -1
  216. package/webpack.config.js +0 -1
@@ -0,0 +1,678 @@
1
+ import { ElementUtil } from "@/med_editor/framework/element-util";
2
+ import { ParagraphElement } from "../paragraph/p-impl";
3
+ import { TableCellElement } from "./table-cell-impl";
4
+ import { TableElement } from "./table-impl";
5
+ import { TableRowElement } from "./table-row-impl";
6
+ export class TableUtil {
7
+ static canDeleteTable(ss) {
8
+ const { startControl } = ss;
9
+ if (!startControl) {
10
+ return false;
11
+ }
12
+ const tb = ElementUtil.getParentByType(startControl, TableElement);
13
+ return tb ? true : false;
14
+ }
15
+ /**
16
+ * 校验当前选区是否可以执行合并单元格操作
17
+ * @param ss
18
+ * @returns
19
+ */
20
+ static canMergeCells(ss) {
21
+ const { ancestorCommonControl, startControl, endControl, collapsed, selectedRange } = ss;
22
+ if (collapsed) {
23
+ return false;
24
+ }
25
+ if (ancestorCommonControl instanceof TableElement || ancestorCommonControl instanceof TableRowElement) {
26
+ const tbElement = ancestorCommonControl instanceof TableElement ? ancestorCommonControl : ancestorCommonControl.parent;
27
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
28
+ const endCell = ElementUtil.getParentByType(endControl, TableCellElement);
29
+ if (startCell?.parent?.parent === tbElement && endCell?.parent?.parent === tbElement) {
30
+ const tbRegion = this.getTableRectSelectedRegion(tbElement, startCell, endCell);
31
+ if (selectedRange && selectedRange.target === tbElement) {
32
+ const selectedCells = [];
33
+ selectedRange.selectedChildren.every(item => item.selectedChildren.every(cell => selectedCells.push(cell.target)));
34
+ for (let i = tbRegion.startRow; i <= tbRegion.endRow; i++) {
35
+ const currRow = tbElement.getChild(i);
36
+ for (let j = tbRegion.startCol; j <= tbRegion.endCol; j++) {
37
+ const currCell = currRow.getChild(j);
38
+ if (currCell.isNotBeMergedCell() && selectedCells.indexOf(currCell) < 0) {
39
+ return false;
40
+ }
41
+ }
42
+ }
43
+ return true;
44
+ }
45
+ }
46
+ }
47
+ return false;
48
+ }
49
+ /**
50
+ * 获取横向合并当前单元格合并结束索引位置
51
+ * @param tb
52
+ * @param rowIndex
53
+ * @param colIndex
54
+ */
55
+ static getHMergeEndIndex(tb, rowIndex, colIndex) {
56
+ const currRow = tb.getChild(rowIndex);
57
+ let index = colIndex;
58
+ for (let i = colIndex + 1; i < tb.getColsCount(); i++) {
59
+ const cell = currRow.getChild(i);
60
+ if (cell.props.hMerge === 'continue') {
61
+ index = i;
62
+ }
63
+ else {
64
+ break;
65
+ }
66
+ }
67
+ return index;
68
+ }
69
+ /**
70
+ * 获取横向合并当前单元格合并开始索引位置
71
+ * @param tb
72
+ * @param rowIndex
73
+ * @param colIndex
74
+ */
75
+ static getHMergeStartIndex(tb, rowIndex, colIndex) {
76
+ const row = tb.getChild(rowIndex);
77
+ for (let i = colIndex - 1; i >= 0; i--) {
78
+ const cell = row.getChild(i);
79
+ if (cell.props.hMerge === 'restart') {
80
+ return i;
81
+ }
82
+ }
83
+ throw new Error('未找到主合并单元格');
84
+ }
85
+ /**
86
+ * 获取纵向合并当前单元格合并结束索引位置
87
+ * @param tb
88
+ * @param rowIndex
89
+ * @param colIndex i
90
+ */
91
+ static getVMergeEndIndex(tb, rowIndex, colIndex) {
92
+ let index = rowIndex;
93
+ for (let i = rowIndex + 1; i < tb.length; i++) {
94
+ const currRow = tb.getChild(i);
95
+ const cell = currRow.getChild(colIndex);
96
+ if (cell.props.vMerge === 'continue') {
97
+ index = i;
98
+ }
99
+ else {
100
+ break;
101
+ }
102
+ }
103
+ return index;
104
+ }
105
+ /**
106
+ * 获取纵向合并当前单元格合并开始索引位置
107
+ * @param tb
108
+ * @param rowIndex
109
+ * @param colIndex i
110
+ */
111
+ static getVMergeStartIndex(tb, rowIndex, colIndex) {
112
+ for (let i = rowIndex - 1; i >= 0; i--) {
113
+ const currRow = tb.getChild(i);
114
+ const cell = currRow.getChild(colIndex);
115
+ if (cell.props.vMerge === 'restart') {
116
+ return i;
117
+ }
118
+ }
119
+ throw new Error('未找到主合并单元格');
120
+ }
121
+ /**
122
+ * 合并单元格
123
+ * @param ss
124
+ * @returns
125
+ */
126
+ static mergeCells(ss) {
127
+ const { ancestorCommonControl, startControl, endControl, collapsed } = ss;
128
+ if (collapsed) {
129
+ return;
130
+ }
131
+ if (ancestorCommonControl instanceof TableElement || ancestorCommonControl instanceof TableRowElement) {
132
+ const tbElement = ancestorCommonControl instanceof TableElement ? ancestorCommonControl : ancestorCommonControl.parent;
133
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
134
+ const endCell = ElementUtil.getParentByType(endControl, TableCellElement);
135
+ if (startCell?.parent?.parent === tbElement && endCell?.parent?.parent === tbElement) {
136
+ //this.combineCellContent(tbElement, startCell, endCell);
137
+ this.combineCells(tbElement, startCell, endCell);
138
+ }
139
+ this.checkRowBeMerged(ss);
140
+ ss.clear();
141
+ }
142
+ }
143
+ /**
144
+ * 在合并单元格后需要校验
145
+ * 1.如果某个行的所有单元格都不是normal,则需要删除当前行
146
+ * 2.如果某个列的所有的单元格都不是normal,则需要删除当前列
147
+ */
148
+ static checkRowBeMerged(ss) {
149
+ const tb = ElementUtil.getParentByType(ss.startControl, TableElement);
150
+ if (!tb) {
151
+ return;
152
+ }
153
+ const colsCount = tb.getColsCount();
154
+ const fullMergeRows = [];
155
+ for (let i = 0; i < tb.length; i++) {
156
+ const row = tb.getChild(i);
157
+ let count = 0;
158
+ for (let j = 0; j < colsCount; j++) {
159
+ const cell = row.getChild(j);
160
+ if (!cell.isNotBeMergedCell()) {
161
+ count++;
162
+ }
163
+ }
164
+ //当前行所有的单元格都被合并,需要删除
165
+ if (count === colsCount) {
166
+ fullMergeRows.push(row);
167
+ }
168
+ }
169
+ fullMergeRows.forEach(row => this.removeRow(row));
170
+ }
171
+ /**
172
+ * 移除行
173
+ * @param row
174
+ */
175
+ static removeRow(row) {
176
+ const rowIndex = row.getIndex();
177
+ const tb = row.parent;
178
+ for (let i = 0; i < row.length; i++) {
179
+ const cell = row.getChild(i);
180
+ if (cell.props.vMerge === 'restart') {
181
+ const vMergeEndIndex = this.getVMergeEndIndex(tb, rowIndex, i);
182
+ if (vMergeEndIndex > rowIndex) {
183
+ const nextRow = tb.getChild(rowIndex + 1);
184
+ nextRow.getChild(i).props.vMerge = vMergeEndIndex - rowIndex > 2 ? 'restart' : null;
185
+ }
186
+ }
187
+ else if (cell.props.vMerge === 'continue') {
188
+ // const vMergeEndIndex = this.getVMergeEndIndex(tb, rowIndex, i);
189
+ // if (vMergeEndIndex > rowIndex) {
190
+ // const nextRow = tb.getChild<TableRowElement>(rowIndex + 1);
191
+ // nextRow.getChild<TableCellElement>(i).cellProps.vMerge = 'continue';
192
+ // }
193
+ const vMergeStartIndex = this.getVMergeStartIndex(tb, rowIndex, i);
194
+ const vMergeEndIndex = this.getVMergeEndIndex(tb, rowIndex, i);
195
+ //当前单元格处于合并行的最后一行,并且合并的行数为2行
196
+ if (vMergeEndIndex === rowIndex && vMergeEndIndex - vMergeStartIndex === 1) {
197
+ const aboveCell = tb.getChild(vMergeStartIndex).getChild(i);
198
+ aboveCell.props.vMerge = null;
199
+ }
200
+ }
201
+ }
202
+ tb.removeChild(row);
203
+ }
204
+ /**
205
+ * 移除列
206
+ * @param tb
207
+ * @param colIndex
208
+ */
209
+ static removeCol(tb, colIndex) {
210
+ for (let i = 0; i < tb.length; i++) {
211
+ const row = tb.getChild(i);
212
+ const cell = row.getChild(colIndex);
213
+ if (cell.props.hMerge === 'restart') {
214
+ const hMergeEndIndex = this.getHMergeEndIndex(tb, i, colIndex);
215
+ if (hMergeEndIndex > colIndex) {
216
+ const nextCell = row.getChild(colIndex + 1);
217
+ nextCell.props.hMerge = hMergeEndIndex - colIndex > 2 ? 'restart' : null;
218
+ }
219
+ }
220
+ else if (cell.props.hMerge === 'continue') {
221
+ const hMergeStartIndex = this.getHMergeStartIndex(tb, i, colIndex);
222
+ const hMergeEndIndex = this.getHMergeEndIndex(tb, i, colIndex);
223
+ //当前单元格处于合并行的最后一行,并且合并的行数为2行
224
+ if (hMergeEndIndex === colIndex && hMergeEndIndex - hMergeStartIndex === 1) {
225
+ const masterVMergeCell = tb.getChild(hMergeStartIndex).getChild(i);
226
+ masterVMergeCell.props.hMerge = null;
227
+ }
228
+ }
229
+ row.removeChild(cell);
230
+ }
231
+ tb.removeCol(colIndex);
232
+ }
233
+ /**
234
+ * 移除光标所在的当前行
235
+ * @param ss
236
+ */
237
+ static removeCurrRow(ss) {
238
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
239
+ if (!startCell?.parent) {
240
+ throw new Error('parent is null');
241
+ }
242
+ const currRow = startCell.parent;
243
+ const tb = currRow.parent;
244
+ this.removeRow(currRow);
245
+ tb.pubOnChange('self');
246
+ tb.refreshView();
247
+ }
248
+ /**
249
+ * 移除光标所在的当前列
250
+ * @param ss
251
+ */
252
+ static removeCurrCol(ss) {
253
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
254
+ if (!startCell?.parent) {
255
+ throw new Error('parent is null');
256
+ }
257
+ if (ss.collapsed) {
258
+ const currRow = startCell.parent;
259
+ const tb = currRow.parent;
260
+ this.removeCol(tb, startCell.getIndex());
261
+ ss.clear();
262
+ return;
263
+ }
264
+ const { ancestorCommonControl, startControl, endControl } = ss;
265
+ if (ancestorCommonControl instanceof TableElement || ancestorCommonControl instanceof TableRowElement) {
266
+ const tbElement = ancestorCommonControl instanceof TableElement ? ancestorCommonControl : ancestorCommonControl.parent;
267
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
268
+ const endCell = ElementUtil.getParentByType(endControl, TableCellElement);
269
+ if (startCell?.parent?.parent === tbElement && endCell?.parent?.parent === tbElement) {
270
+ const startColIndex = startCell.getIndex();
271
+ let endColIndex = endCell.getIndex();
272
+ while (startColIndex <= endColIndex) {
273
+ this.removeCol(tbElement, endColIndex);
274
+ endColIndex--;
275
+ }
276
+ }
277
+ tbElement.pubOnChange('self');
278
+ }
279
+ ss.clear();
280
+ }
281
+ /**
282
+ * 合并单元格
283
+ */
284
+ static combineCells(tbElement, startCell, endCell) {
285
+ const tbRegion = this.getTableRectSelectedRegion(tbElement, startCell, endCell);
286
+ const cellContents = [];
287
+ for (let i = tbRegion.startRow; i <= tbRegion.endRow; i++) {
288
+ const currRow = tbElement.getChild(i);
289
+ for (let j = tbRegion.startCol; j <= tbRegion.endCol; j++) {
290
+ const currCell = currRow.getChild(j);
291
+ currCell.props.vMerge = null;
292
+ currCell.props.hMerge = 'continue';
293
+ if (j === tbRegion.startCol) {
294
+ currCell.props.vMerge = i === tbRegion.startRow ? 'restart' : 'continue';
295
+ currCell.props.hMerge = 'restart';
296
+ }
297
+ if (currCell !== startCell) {
298
+ cellContents.push(...ElementUtil.getChildrenElements(currCell));
299
+ currCell.clearItems();
300
+ }
301
+ }
302
+ }
303
+ //如果合并的单元格都是空段落,则不合并内容
304
+ if (!cellContents.every((item) => (item instanceof ParagraphElement && item.length === 1))) {
305
+ for (let i = 0; i < cellContents.length; i++) {
306
+ startCell.addChild(cellContents[i]);
307
+ }
308
+ }
309
+ }
310
+ static canSplitCell(ss) {
311
+ const { ancestorCommonControl, startControl, endControl, collapsed } = ss;
312
+ if (ancestorCommonControl instanceof TableElement || ancestorCommonControl instanceof TableRowElement) {
313
+ const tbElement = ancestorCommonControl instanceof TableElement ? ancestorCommonControl : ancestorCommonControl.parent;
314
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
315
+ const endCell = ElementUtil.getParentByType(endControl, TableCellElement);
316
+ if (startCell?.parent?.parent === tbElement && endCell?.parent?.parent === tbElement) {
317
+ if (startCell === endCell) {
318
+ return startCell.props.hMerge === 'restart' || startCell.props.vMerge === 'restart';
319
+ }
320
+ }
321
+ }
322
+ else {
323
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
324
+ if (!startCell) {
325
+ throw new Error('startCell is null');
326
+ }
327
+ const { hMerge, vMerge } = startCell?.props;
328
+ return hMerge === 'restart' || vMerge === 'restart' || !hMerge || !vMerge;
329
+ }
330
+ return false;
331
+ }
332
+ /**
333
+ * 将合并的单元格合并还原
334
+ * @param ss
335
+ */
336
+ static restoreCell(ss) {
337
+ const { ancestorCommonControl, startControl, endControl, collapsed } = ss;
338
+ const startCell = ElementUtil.getParentByType(startControl, TableCellElement);
339
+ if (startCell) {
340
+ this.restoreCellMerge(startCell);
341
+ ss.resetRange(startCell, 0);
342
+ startCell.refreshView();
343
+ }
344
+ }
345
+ static restoreCellMerge(cell) {
346
+ const { vMerge, hMerge } = cell.props;
347
+ const row = cell.parent;
348
+ const tb = row.parent;
349
+ const rowIndex = row.getIndex();
350
+ const colIndex = cell.getIndex();
351
+ if (vMerge === 'restart') {
352
+ for (let i = rowIndex + 1; i < tb.length; i++) {
353
+ const row = tb.getChild(i);
354
+ const cell = row.getChild(colIndex);
355
+ if (cell.props.vMerge === "continue") {
356
+ cell.props.vMerge = null;
357
+ if (cell.props.hMerge === "restart") {
358
+ this.restoreCellMerge(cell);
359
+ }
360
+ }
361
+ }
362
+ }
363
+ if (hMerge === 'restart') {
364
+ for (let i = colIndex + 1; i < row.length; i++) {
365
+ const cell = row.getChild(i);
366
+ if (cell.props.hMerge === "continue") {
367
+ cell.props.hMerge = null;
368
+ if (cell.props.vMerge === "restart") {
369
+ this.restoreCellMerge(cell);
370
+ }
371
+ }
372
+ }
373
+ }
374
+ cell.props.hMerge = null;
375
+ cell.props.vMerge = null;
376
+ }
377
+ static getVMergeRows(tb, masterRowIndex, masterColIndex) {
378
+ const items = [];
379
+ for (let i = masterRowIndex + 1; i < tb.length; i++) {
380
+ const row = tb.getChild(i);
381
+ const cell = row.getChild(masterColIndex);
382
+ if (cell.props.vMerge === 'continue') {
383
+ items.push(row);
384
+ }
385
+ else {
386
+ return items;
387
+ }
388
+ }
389
+ return items;
390
+ }
391
+ static insertCol(ss, insertColIndex) {
392
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
393
+ if (!startCell?.parent) {
394
+ throw new Error('parent is null');
395
+ }
396
+ const startRow = startCell.parent;
397
+ const tb = startRow.parent;
398
+ const currColIndex = startCell.getIndex();
399
+ const colsCount = tb.getColsCount();
400
+ insertColIndex = insertColIndex < 0 ? 0 : insertColIndex;
401
+ insertColIndex = insertColIndex > colsCount ? colsCount : insertColIndex;
402
+ //插入列需要将之前列分割
403
+ const insertColWidth = insertColIndex === colsCount ? tb.getCellWidth(colsCount - 1) : tb.getCellWidth(insertColIndex);
404
+ const newColWidth = Math.floor(insertColWidth / 2);
405
+ if (newColWidth < 20) {
406
+ console.warn('插入列的最小宽度不能小于20,当前为:' + newColWidth);
407
+ return;
408
+ }
409
+ for (let i = 0; i < tb.length; i++) {
410
+ const row = tb.getChild(i);
411
+ const newCell = new TableCellElement();
412
+ if (insertColIndex === 0 || insertColIndex === colsCount) {
413
+ row.addChild(newCell, insertColIndex);
414
+ continue;
415
+ }
416
+ const focusCell = row.getChild(insertColIndex);
417
+ if (focusCell) {
418
+ if (focusCell.props.hMerge === 'continue') {
419
+ //获取合并行的最后的行索引
420
+ const hMergeEndColIndex = this.getHMergeEndIndex(tb, i, currColIndex);
421
+ if (hMergeEndColIndex > insertColIndex || (hMergeEndColIndex === insertColIndex && currColIndex < insertColIndex)) {
422
+ newCell.props.hMerge = 'continue';
423
+ }
424
+ }
425
+ else if (focusCell.props.hMerge === 'restart' && currColIndex > insertColIndex) {
426
+ newCell.props.hMerge = 'continue';
427
+ }
428
+ }
429
+ row.addChild(newCell, insertColIndex);
430
+ }
431
+ tb.setCellWidth(insertColIndex === colsCount ? insertColIndex - 1 : insertColIndex, insertColWidth - newColWidth);
432
+ tb.insertCol(insertColIndex, newColWidth);
433
+ tb.pubOnChange('self');
434
+ tb.refreshView();
435
+ }
436
+ static insertRow(ss, insertRowIndex) {
437
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
438
+ if (!startCell?.parent) {
439
+ throw new Error('parent is null');
440
+ }
441
+ const startRow = startCell.parent;
442
+ const tb = startRow.parent;
443
+ const currRowIndex = startRow.getIndex();
444
+ insertRowIndex = insertRowIndex < 0 ? 0 : insertRowIndex;
445
+ insertRowIndex = insertRowIndex > tb.length ? tb.length : insertRowIndex;
446
+ const newRow = TableRowElement.createRow(tb.getColsCount());
447
+ //新行插在第一行或者最后一行
448
+ if (insertRowIndex === 0 || insertRowIndex === tb.length) {
449
+ tb.addChild(newRow, insertRowIndex);
450
+ tb.refreshView();
451
+ return newRow;
452
+ }
453
+ const destRow = tb.getChild(insertRowIndex);
454
+ for (let i = 0; i < destRow.length; i++) {
455
+ const destCell = destRow.getChild(i);
456
+ const newCell = newRow.getChild(i);
457
+ //如果当前单元格是普通单元格或者合并列的最后一个单元格,则不做处理
458
+ if (destCell.props.vMerge === 'continue') {
459
+ //获取合并列的最后的行索引
460
+ const vMergeEndRowIndex = this.getVMergeEndIndex(tb, insertRowIndex, i);
461
+ if (vMergeEndRowIndex > insertRowIndex || (vMergeEndRowIndex === insertRowIndex && currRowIndex < insertRowIndex)) {
462
+ newCell.props.vMerge = 'continue';
463
+ //newCell.cellProps.hMerge = destCell.cellProps.hMerge;
464
+ }
465
+ }
466
+ else if (destCell.props.vMerge === 'restart' && currRowIndex > insertRowIndex) {
467
+ //在上方新增行,目标行单元格属于合并的起点
468
+ newCell.props.vMerge = 'continue';
469
+ //newCell.cellProps.hMerge = destCell.cellProps.hMerge;
470
+ }
471
+ }
472
+ tb.addChild(newRow, insertRowIndex);
473
+ tb.refreshView();
474
+ return newRow;
475
+ }
476
+ /**
477
+ * 在下方插入行
478
+ */
479
+ static insertRowBelow(ss) {
480
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
481
+ if (!startCell?.parent) {
482
+ throw new Error('parent is null');
483
+ }
484
+ const startRow = startCell.parent;
485
+ const currRowIndex = startRow.getIndex();
486
+ this.insertRow(ss, currRowIndex + 1);
487
+ }
488
+ /**
489
+ * 在上方插入行
490
+ */
491
+ static insertRowAbove(ss) {
492
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
493
+ if (!startCell?.parent) {
494
+ throw new Error('parent is null');
495
+ }
496
+ const startRow = startCell.parent;
497
+ const currRowIndex = startRow.getIndex();
498
+ this.insertRow(ss, currRowIndex);
499
+ }
500
+ /**
501
+ * 在右侧插入列
502
+ */
503
+ static insertColToRight(ss) {
504
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
505
+ if (!startCell?.parent) {
506
+ throw new Error('parent is null');
507
+ }
508
+ const currColIndex = startCell.getIndex();
509
+ this.insertCol(ss, currColIndex + 1);
510
+ }
511
+ /**
512
+ * 在左侧插入列
513
+ */
514
+ static insertColToLeft(ss) {
515
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
516
+ if (!startCell?.parent) {
517
+ throw new Error('parent is null');
518
+ }
519
+ const currColIndex = startCell.getIndex();
520
+ this.insertCol(ss, currColIndex);
521
+ }
522
+ /**
523
+ * 跳转到下一个单元格中
524
+ * @param ss
525
+ */
526
+ static moveNextCell(ss) {
527
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
528
+ if (!startCell?.parent) {
529
+ throw new Error('parent is null');
530
+ }
531
+ const rowIndex = startCell.parent.getIndex();
532
+ const nextCell = this.getNextCell(ss);
533
+ if (nextCell) {
534
+ ss.resetRange(nextCell, 0);
535
+ nextCell.refreshView();
536
+ return;
537
+ }
538
+ //新增行,并定位
539
+ const newRow = this.insertRow(ss, rowIndex + 1);
540
+ ss.resetRange(newRow, 0);
541
+ newRow.refreshView();
542
+ }
543
+ /**
544
+ * 获取下一个可定位的单元格
545
+ * @param ss
546
+ * @returns
547
+ */
548
+ static getNextCell(ss) {
549
+ const startCell = ElementUtil.getParentByType(ss.startControl, TableCellElement);
550
+ if (!startCell?.parent) {
551
+ throw new Error('parent is null');
552
+ }
553
+ const rowIndex = startCell.parent.getIndex();
554
+ const colIndex = startCell.getIndex();
555
+ const tb = startCell.parent.parent;
556
+ for (let i = rowIndex; i < tb.length; i++) {
557
+ let j = i === rowIndex ? colIndex + 1 : 0;
558
+ const currRow = tb.getChild(i);
559
+ for (; j < tb.getColsCount(); j++) {
560
+ const currCell = currRow.getChild(j);
561
+ if (currCell.isNotBeMergedCell()) {
562
+ return currCell;
563
+ }
564
+ }
565
+ }
566
+ //throw new Error('getNextCell is null')
567
+ }
568
+ /**
569
+ * 当前光标是否在最后一个单元格中
570
+ */
571
+ static isLastVisiableCell(ss) {
572
+ return !this.getNextCell(ss);
573
+ }
574
+ /**
575
+ * 移除当前table
576
+ */
577
+ static removeTable(ss) {
578
+ const tb = ElementUtil.getParentByType(ss.startControl, TableElement);
579
+ if (!tb) {
580
+ console.warn('当前位置不存在表格');
581
+ return;
582
+ }
583
+ tb.parent.refreshView();
584
+ tb.remove();
585
+ }
586
+ /**
587
+ * 设置表格单元格对角线
588
+ * @param ss
589
+ * @param diagonal
590
+ * @returns
591
+ */
592
+ static setTableCellDiagonal(ss, diagonal) {
593
+ const tc = ElementUtil.getParentByType(ss.startControl, TableCellElement);
594
+ if (!tc) {
595
+ console.warn('当前位置不存在表格');
596
+ return;
597
+ }
598
+ if (!tc.props.diagonal || tc.props.diagonal !== diagonal) {
599
+ tc.props.diagonal = diagonal;
600
+ }
601
+ else if (tc.props.diagonal === diagonal) {
602
+ tc.props.diagonal = null;
603
+ }
604
+ tc.refreshView();
605
+ }
606
+ /**
607
+ * 将当前表格截断成两个相邻的表格
608
+ * @param tb
609
+ * @param rowIndex
610
+ */
611
+ static cutTable(tb, rowIndex) {
612
+ if (rowIndex === 0 || rowIndex === tb.length - 1) {
613
+ return;
614
+ }
615
+ const newTb = tb.clone(false);
616
+ const len = tb.length - 1;
617
+ while (len > rowIndex) {
618
+ const child = tb.getChild(len);
619
+ newTb.addChild(child.clone(true), 0);
620
+ tb.removeChild(child);
621
+ }
622
+ if (newTb.length > 0) {
623
+ tb.parent.addChild(newTb, tb.getIndex() + 1);
624
+ }
625
+ }
626
+ /**
627
+ * 获取表格选区区域最大的矩形区域
628
+ * @param tb
629
+ * @param startCell
630
+ * @param endCell
631
+ * @returns
632
+ */
633
+ static getTableRectSelectedRegion(tb, startCell, endCell) {
634
+ let startRow = startCell.parent.getIndex();
635
+ let startCol = startCell.getIndex();
636
+ let endRow = endCell.parent.getIndex();
637
+ let endCol = endCell.getIndex();
638
+ if (startRow > endRow) {
639
+ [endRow, startRow] = [startRow, endRow];
640
+ }
641
+ if (startCol > endCol) {
642
+ [endCol, startCol] = [startCol, endCol];
643
+ }
644
+ for (let i = startRow; i <= endRow; i++) {
645
+ const row = tb.getChild(i);
646
+ for (let j = startCol; j <= endCol; j++) {
647
+ const cell = row.getChild(j);
648
+ const { hMerge, vMerge } = cell.props;
649
+ if (hMerge === 'continue') {
650
+ const startColIndex = TableUtil.getHMergeStartIndex(tb, i, j);
651
+ const endColIndex = TableUtil.getHMergeEndIndex(tb, i, j);
652
+ startCol = Math.min(startCol, startColIndex);
653
+ endCol = Math.max(endCol, endColIndex);
654
+ }
655
+ if (vMerge === 'continue') {
656
+ const startRowIndex = TableUtil.getVMergeStartIndex(tb, i, j);
657
+ const endRowIndex = TableUtil.getVMergeEndIndex(tb, i, j);
658
+ startRow = Math.min(startRow, startRowIndex);
659
+ endRow = Math.max(endRow, endRowIndex);
660
+ }
661
+ if (vMerge === 'restart') {
662
+ const rowIndex = TableUtil.getVMergeEndIndex(tb, i, j);
663
+ if (rowIndex > endRow) {
664
+ endRow = rowIndex;
665
+ }
666
+ }
667
+ if (hMerge === 'restart') {
668
+ const colIndex = TableUtil.getHMergeEndIndex(tb, i, j);
669
+ if (colIndex > endCol) {
670
+ endCol = colIndex;
671
+ }
672
+ }
673
+ }
674
+ }
675
+ return { startRow, endRow, startCol, endCol };
676
+ }
677
+ }
678
+ //# sourceMappingURL=table-util.js.map