amis-editor 4.1.0-beta.4 → 4.2.0-beta.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. package/dist/component/Breadcrumb.d.ts +23 -2
  2. package/dist/component/ClassNameControl.d.ts +1 -1
  3. package/dist/component/Control/APIControl.d.ts +2 -2
  4. package/dist/component/Control/OptionControl.d.ts +3 -3
  5. package/dist/component/Control/PopoverEdit.d.ts +4 -4
  6. package/dist/component/Control/ValidationControl.d.ts +1 -1
  7. package/dist/component/Editor.d.ts +20 -1
  8. package/dist/component/IFramePreview.d.ts +2 -1
  9. package/dist/component/NodeWrapper.d.ts +1 -1
  10. package/dist/component/Panel/RenderersPanel.d.ts +1 -2
  11. package/dist/component/Preview.d.ts +3 -1
  12. package/dist/component/RegionWrapper.d.ts +4 -0
  13. package/dist/component/ScaffoldModal.d.ts +1 -1
  14. package/dist/component/VRenderer.d.ts +4 -0
  15. package/dist/component/base/InputComponentName.d.ts +1 -1
  16. package/dist/component/base/SearchCustomRendererPanel.d.ts +15 -0
  17. package/dist/component/base/SearchPanel.d.ts +83 -0
  18. package/dist/component/base/SearchRendererPanel.d.ts +2 -42
  19. package/dist/component/base/ShortcutKey.d.ts +23 -0
  20. package/dist/env.d.ts +1 -1
  21. package/dist/exports.min.js +1 -1
  22. package/dist/index.d.ts +5 -1
  23. package/dist/index.min.js +1 -1
  24. package/dist/manager.d.ts +16 -4
  25. package/dist/plugin/Alert.d.ts +1 -1
  26. package/dist/plugin/Button.d.ts +1 -1
  27. package/dist/plugin/Card.d.ts +1 -0
  28. package/dist/plugin/Cards.d.ts +2 -1
  29. package/dist/plugin/Carousel.d.ts +1 -0
  30. package/dist/plugin/Chart.d.ts +1 -0
  31. package/dist/plugin/Collapse.d.ts +1 -0
  32. package/dist/plugin/Custom.d.ts +3 -0
  33. package/dist/plugin/CustomRegion.d.ts +40 -0
  34. package/dist/plugin/Dialog.d.ts +1 -1
  35. package/dist/plugin/Drawer.d.ts +1 -1
  36. package/dist/plugin/DropDownButton.d.ts +1 -1
  37. package/dist/plugin/Each.d.ts +1 -1
  38. package/dist/plugin/Flex.d.ts +1 -0
  39. package/dist/plugin/Form/Control.d.ts +1 -0
  40. package/dist/plugin/Form/Form.d.ts +19 -3
  41. package/dist/plugin/Form/Formula.d.ts +3 -3
  42. package/dist/plugin/Form/InputURL.d.ts +1 -0
  43. package/dist/plugin/Form/Switch.d.ts +2 -0
  44. package/dist/plugin/Grid.d.ts +1 -0
  45. package/dist/plugin/HBox.d.ts +1 -0
  46. package/dist/plugin/IFrame.d.ts +3 -3
  47. package/dist/plugin/Json.d.ts +1 -0
  48. package/dist/plugin/List.d.ts +2 -1
  49. package/dist/plugin/Mapping.d.ts +1 -0
  50. package/dist/plugin/Markdown.d.ts +1 -0
  51. package/dist/plugin/Nav.d.ts +1 -0
  52. package/dist/plugin/Page.d.ts +1 -1
  53. package/dist/plugin/Panel/Outline.d.ts +8 -0
  54. package/dist/plugin/Progress.d.ts +1 -0
  55. package/dist/plugin/QRCode.d.ts +1 -0
  56. package/dist/plugin/Reset.d.ts +0 -1
  57. package/dist/plugin/Service.d.ts +1 -0
  58. package/dist/plugin/Sparkline.d.ts +1 -0
  59. package/dist/plugin/Status.d.ts +1 -0
  60. package/dist/plugin/Steps.d.ts +1 -0
  61. package/dist/plugin/Submit.d.ts +0 -1
  62. package/dist/plugin/Table.d.ts +1 -1
  63. package/dist/plugin/TableView.d.ts +1 -0
  64. package/dist/plugin/Tasks.d.ts +1 -0
  65. package/dist/plugin/TooltipWrapper.d.ts +1 -0
  66. package/dist/plugin/Video.d.ts +1 -0
  67. package/dist/plugin/WebComponent.d.ts +1 -0
  68. package/dist/plugin/Wizard.d.ts +11 -2
  69. package/dist/plugin/Wrapper.d.ts +1 -0
  70. package/dist/plugin.d.ts +8 -7
  71. package/dist/store/editor.d.ts +58 -22
  72. package/dist/store/node.d.ts +6 -0
  73. package/dist/style.css +1 -1
  74. package/dist/util.d.ts +1 -1
  75. package/package.json +9 -3
  76. package/src/component/schemaTpl.tsx +2157 -0
  77. package/src/plugin/Alert.tsx +87 -0
  78. package/src/plugin/AnchorNav.tsx +233 -0
  79. package/src/plugin/Audio.tsx +154 -0
  80. package/src/plugin/Avatar.tsx +77 -0
  81. package/src/plugin/Breadcrumb.tsx +107 -0
  82. package/src/plugin/Button.tsx +281 -0
  83. package/src/plugin/ButtonGroup.tsx +85 -0
  84. package/src/plugin/ButtonToolbar.tsx +87 -0
  85. package/src/plugin/CRUD.tsx +1835 -0
  86. package/src/plugin/Card.tsx +290 -0
  87. package/src/plugin/Cards.tsx +318 -0
  88. package/src/plugin/Carousel.tsx +377 -0
  89. package/src/plugin/Chart.tsx +218 -0
  90. package/src/plugin/CodeView.tsx +60 -0
  91. package/src/plugin/Collapse.tsx +136 -0
  92. package/src/plugin/CollapseGroup.tsx +167 -0
  93. package/src/plugin/Container.tsx +40 -0
  94. package/src/plugin/Custom.tsx +128 -0
  95. package/src/plugin/CustomRegion.tsx +156 -0
  96. package/src/plugin/Date.tsx +74 -0
  97. package/src/plugin/Datetime.tsx +68 -0
  98. package/src/plugin/Dialog.tsx +176 -0
  99. package/src/plugin/Divider.tsx +36 -0
  100. package/src/plugin/Drawer.tsx +214 -0
  101. package/src/plugin/DropDownButton.tsx +235 -0
  102. package/src/plugin/Each.tsx +150 -0
  103. package/src/plugin/ErrorRenderer.tsx +15 -0
  104. package/src/plugin/Flex.tsx +151 -0
  105. package/src/plugin/Form/ButtonGroupSelect.tsx +75 -0
  106. package/src/plugin/Form/ButtonToolbar.tsx +110 -0
  107. package/src/plugin/Form/ChainedSelect.tsx +70 -0
  108. package/src/plugin/Form/Checkbox.tsx +87 -0
  109. package/src/plugin/Form/Checkboxes.tsx +167 -0
  110. package/src/plugin/Form/CodeEditor.tsx +81 -0
  111. package/src/plugin/Form/Combo.tsx +582 -0
  112. package/src/plugin/Form/ConditionBuilder.tsx +315 -0
  113. package/src/plugin/Form/Control.tsx +139 -0
  114. package/src/plugin/Form/DiffEditor.tsx +111 -0
  115. package/src/plugin/Form/FieldSet.tsx +163 -0
  116. package/src/plugin/Form/Form.tsx +687 -0
  117. package/src/plugin/Form/Formula.tsx +79 -0
  118. package/src/plugin/Form/Group.tsx +295 -0
  119. package/src/plugin/Form/Hidden.tsx +44 -0
  120. package/src/plugin/Form/InputArray.tsx +228 -0
  121. package/src/plugin/Form/InputCity.tsx +93 -0
  122. package/src/plugin/Form/InputColor.tsx +123 -0
  123. package/src/plugin/Form/InputDate.tsx +175 -0
  124. package/src/plugin/Form/InputDateRange.tsx +225 -0
  125. package/src/plugin/Form/InputDateTime.tsx +183 -0
  126. package/src/plugin/Form/InputDateTimeRange.tsx +221 -0
  127. package/src/plugin/Form/InputEmail.tsx +33 -0
  128. package/src/plugin/Form/InputExcel.tsx +85 -0
  129. package/src/plugin/Form/InputFile.tsx +221 -0
  130. package/src/plugin/Form/InputGroup.tsx +96 -0
  131. package/src/plugin/Form/InputImage.tsx +266 -0
  132. package/src/plugin/Form/InputKV.tsx +72 -0
  133. package/src/plugin/Form/InputMonth.tsx +35 -0
  134. package/src/plugin/Form/InputMonthRange.tsx +195 -0
  135. package/src/plugin/Form/InputNumber.tsx +89 -0
  136. package/src/plugin/Form/InputPassword.tsx +33 -0
  137. package/src/plugin/Form/InputQuarter.tsx +35 -0
  138. package/src/plugin/Form/InputQuarterRange.tsx +195 -0
  139. package/src/plugin/Form/InputRange.tsx +121 -0
  140. package/src/plugin/Form/InputRating.tsx +78 -0
  141. package/src/plugin/Form/InputRepeat.tsx +57 -0
  142. package/src/plugin/Form/InputRichText.tsx +186 -0
  143. package/src/plugin/Form/InputSubForm.tsx +189 -0
  144. package/src/plugin/Form/InputTable.tsx +434 -0
  145. package/src/plugin/Form/InputTag.tsx +70 -0
  146. package/src/plugin/Form/InputText.tsx +186 -0
  147. package/src/plugin/Form/InputTime.tsx +85 -0
  148. package/src/plugin/Form/InputTree.tsx +229 -0
  149. package/src/plugin/Form/InputURL.tsx +34 -0
  150. package/src/plugin/Form/InputYear.tsx +35 -0
  151. package/src/plugin/Form/Item.tsx +327 -0
  152. package/src/plugin/Form/ListSelect.tsx +73 -0
  153. package/src/plugin/Form/LocationPicker.tsx +62 -0
  154. package/src/plugin/Form/MatrixCheckboxes.tsx +136 -0
  155. package/src/plugin/Form/NestedSelect.tsx +211 -0
  156. package/src/plugin/Form/Picker.tsx +209 -0
  157. package/src/plugin/Form/Radios.tsx +119 -0
  158. package/src/plugin/Form/Select.tsx +233 -0
  159. package/src/plugin/Form/Static.tsx +322 -0
  160. package/src/plugin/Form/Switch.tsx +107 -0
  161. package/src/plugin/Form/TabsTransfer.tsx +259 -0
  162. package/src/plugin/Form/Textarea.tsx +83 -0
  163. package/src/plugin/Form/Transfer.tsx +368 -0
  164. package/src/plugin/Form/TreeSelect.tsx +263 -0
  165. package/src/plugin/Form/UUID.tsx +48 -0
  166. package/src/plugin/Grid.tsx +798 -0
  167. package/src/plugin/HBox.tsx +727 -0
  168. package/src/plugin/IFrame.tsx +70 -0
  169. package/src/plugin/Image.tsx +314 -0
  170. package/src/plugin/Images.tsx +231 -0
  171. package/src/plugin/Json.tsx +69 -0
  172. package/src/plugin/Link.tsx +93 -0
  173. package/src/plugin/List.tsx +278 -0
  174. package/src/plugin/ListItem.tsx +229 -0
  175. package/src/plugin/Log.tsx +52 -0
  176. package/src/plugin/Mapping.tsx +149 -0
  177. package/src/plugin/Markdown.tsx +47 -0
  178. package/src/plugin/Nav.tsx +184 -0
  179. package/src/plugin/Operation.tsx +95 -0
  180. package/src/plugin/Others/Action.tsx +426 -0
  181. package/src/plugin/Others/BasicToolbar.tsx +585 -0
  182. package/src/plugin/Others/DataDebug.tsx +134 -0
  183. package/src/plugin/Others/TableCell.tsx +480 -0
  184. package/src/plugin/Others/Unknown.tsx +37 -0
  185. package/src/plugin/Page.tsx +306 -0
  186. package/src/plugin/Panel/AvailableRenderers.tsx +41 -0
  187. package/src/plugin/Panel/Code.tsx +44 -0
  188. package/src/plugin/Panel/Name.tsx +26 -0
  189. package/src/plugin/Panel/Outline.tsx +40 -0
  190. package/src/plugin/Panel.tsx +243 -0
  191. package/src/plugin/Plain.tsx +84 -0
  192. package/src/plugin/Progress.tsx +125 -0
  193. package/src/plugin/Property.tsx +139 -0
  194. package/src/plugin/QRCode.tsx +96 -0
  195. package/src/plugin/Reset.tsx +23 -0
  196. package/src/plugin/Service.tsx +162 -0
  197. package/src/plugin/Sparkline.tsx +40 -0
  198. package/src/plugin/Status.tsx +76 -0
  199. package/src/plugin/Steps.tsx +128 -0
  200. package/src/plugin/Submit.tsx +23 -0
  201. package/src/plugin/Table.tsx +428 -0
  202. package/src/plugin/TableView.tsx +709 -0
  203. package/src/plugin/Tabs.tsx +362 -0
  204. package/src/plugin/Tasks.tsx +274 -0
  205. package/src/plugin/Time.tsx +68 -0
  206. package/src/plugin/TooltipWrapper.tsx +193 -0
  207. package/src/plugin/Tpl.tsx +158 -0
  208. package/src/plugin/Video.tsx +158 -0
  209. package/src/plugin/WebComponent.tsx +53 -0
  210. package/src/plugin/Wizard.tsx +740 -0
  211. package/src/plugin/Wrapper.tsx +107 -0
  212. package/src/plugin.ts +1050 -0
  213. package/dist/150a58f3318ca7541ed9.png +0 -0
  214. package/dist/471adb97c322b226e589.png +0 -0
  215. package/dist/4de5f42360bc5946c3c2.png +0 -0
  216. package/dist/4e9968bba3855f088fed.png +0 -0
  217. package/dist/7f09c38ebc687fea847a.png +0 -0
  218. package/dist/c94073576487510314ea.png +0 -0
@@ -0,0 +1,709 @@
1
+ /**
2
+ * @file table view 组件的可视化编辑
3
+ */
4
+
5
+ import React from 'react';
6
+ import {registerEditorPlugin} from '../manager';
7
+ import {
8
+ BaseEventContext,
9
+ BasePlugin,
10
+ BasicPanelItem,
11
+ BasicToolbarItem,
12
+ BuildPanelEventContext,
13
+ RegionConfig,
14
+ RendererInfo,
15
+ VRendererConfig
16
+ } from '../plugin';
17
+ import {getSchemaTpl} from '../component/schemaTpl';
18
+ import {VRenderer} from '../component/VRenderer';
19
+ import {TableViewEditor} from '../component/TableViewEditor';
20
+ import {TableViewSchema} from 'amis/lib/renderers/TableView';
21
+ import {JSONGetById} from '../util';
22
+
23
+ /**
24
+ * 尚未实现的功能:
25
+ * * 按列删除,需要将一些 colspan 减一
26
+ * * 水平/垂直拆分单元格,增加周围节点的 colspan 和 rowspan
27
+ */
28
+
29
+ // td 节点模板
30
+ const TD_TEMPLATE = {
31
+ body: {
32
+ type: 'tpl',
33
+ tpl: '---'
34
+ }
35
+ } as {body: {type: 'tpl'}};
36
+
37
+ /**
38
+ * 遍历表格,算出每个单元格在最终渲染时的实际行和列,后续许多操作都需要以这个作为依据
39
+ * 比如插入列的时候,不能根据单元格在数组的位置,而是要根据单元格实际渲染时所属列
40
+ */
41
+ function getCellRealPosition(table: TableViewSchema) {
42
+ if (!table) {
43
+ return {
44
+ trs: []
45
+ };
46
+ }
47
+ // 记录有哪些行列被合并了,这样后续计算的时候就要跳过这些行列
48
+ const spannedCell: boolean[][] = [];
49
+ const trs = table.trs || [];
50
+
51
+ let currentRow = 0; // 当前渲染的实际行
52
+ for (const tr of trs) {
53
+ const tds = tr.tds || [];
54
+
55
+ let currentCol = 0; // 当前渲染的实际列
56
+ for (const td of tds) {
57
+ // 跳过被合并的行
58
+ while (spannedCell[currentRow] && spannedCell[currentRow][currentCol]) {
59
+ currentCol = currentCol + 1;
60
+ }
61
+ const rowspan = td.rowspan || 1;
62
+ const colspan = td.colspan || 1;
63
+ // 标记后续行合并情况
64
+ if (rowspan > 1 || colspan > 1) {
65
+ for (let i = 0; i < rowspan; i++) {
66
+ const spanRow = currentRow + i;
67
+ if (!spannedCell[spanRow]) {
68
+ spannedCell[spanRow] = [];
69
+ }
70
+ for (let j = 0; j < colspan; j++) {
71
+ const spanCol = currentCol + j;
72
+ spannedCell[spanRow][spanCol] = true;
73
+ }
74
+ }
75
+ }
76
+ (td as any).$$row = currentRow;
77
+ (td as any).$$col = currentCol;
78
+ currentCol = currentCol + 1;
79
+ }
80
+
81
+ currentRow = currentRow + 1;
82
+ }
83
+ return table;
84
+ }
85
+
86
+ export class TableViewPlugin extends BasePlugin {
87
+ // 关联渲染器名字
88
+ rendererName = 'table-view';
89
+ $schema = '/schemas/TableViewSchema.json';
90
+
91
+ // 组件名称
92
+ name = '表格视图';
93
+ isBaseComponent = true;
94
+ icon = 'fa fa-columns';
95
+ description = '表格类型的展现';
96
+ docLink = '/amis/zh-CN/components/table-view';
97
+ tags = ['容器'];
98
+ scaffold = {
99
+ type: 'table-view',
100
+ trs: [
101
+ {
102
+ background: '#F7F7F7',
103
+ tds: [
104
+ {
105
+ body: {
106
+ type: 'tpl',
107
+ tpl: '地区'
108
+ }
109
+ },
110
+ {
111
+ body: {
112
+ type: 'tpl',
113
+ tpl: '城市'
114
+ }
115
+ },
116
+ {
117
+ body: {
118
+ type: 'tpl',
119
+ tpl: '销量'
120
+ }
121
+ }
122
+ ]
123
+ },
124
+ {
125
+ tds: [
126
+ {
127
+ rowspan: 2,
128
+ body: {
129
+ type: 'tpl',
130
+ tpl: '华北'
131
+ }
132
+ },
133
+ {
134
+ body: {
135
+ type: 'tpl',
136
+ tpl: '北京'
137
+ }
138
+ },
139
+ {
140
+ body: {
141
+ type: 'tpl',
142
+ tpl: '${beijing}'
143
+ }
144
+ }
145
+ ]
146
+ },
147
+ {
148
+ tds: [
149
+ {
150
+ body: {
151
+ type: 'tpl',
152
+ tpl: '天津'
153
+ }
154
+ },
155
+ {
156
+ body: {
157
+ type: 'tpl',
158
+ tpl: '${tianjing}'
159
+ }
160
+ }
161
+ ]
162
+ }
163
+ ]
164
+ };
165
+ previewSchema = {
166
+ ...this.scaffold
167
+ };
168
+
169
+ regions: Array<RegionConfig> = [
170
+ {
171
+ key: 'body',
172
+ label: '内容区',
173
+ renderMethod: 'renderTdBody',
174
+ preferTag: '展示'
175
+ }
176
+ ];
177
+
178
+ panelTitle = '表格视图';
179
+ panelBody = [
180
+ getSchemaTpl('tabs', [
181
+ {
182
+ title: '常规',
183
+ body: [
184
+ {type: 'input-text', label: '宽度', name: 'width', clearable: true},
185
+ {
186
+ type: 'input-text',
187
+ label: '默认内边距',
188
+ name: 'padding',
189
+ clearable: true
190
+ },
191
+ {
192
+ type: 'switch',
193
+ name: 'border',
194
+ mode: 'inline',
195
+ className: 'w-full',
196
+ label: '是否显示边框'
197
+ },
198
+ {
199
+ label: '边框颜色',
200
+ type: 'input-color',
201
+ name: 'borderColor'
202
+ }
203
+ ]
204
+ },
205
+ {
206
+ title: '外观',
207
+ body: [getSchemaTpl('className'), getSchemaTpl('style')]
208
+ },
209
+ {
210
+ title: '显隐',
211
+ body: [getSchemaTpl('visible')]
212
+ }
213
+ ])
214
+ ];
215
+
216
+ fieldWrapperResolve = (dom: HTMLElement) => dom;
217
+
218
+ overrides = {
219
+ renderTd(this: any, td: any, colIndex: number, rowIndex: number) {
220
+ const dom = this.super(td, colIndex, rowIndex);
221
+ const info: RendererInfo = this.props.$$editor;
222
+
223
+ if (!info || !td.$$id) {
224
+ return dom;
225
+ }
226
+
227
+ const plugin = info.plugin as TableViewPlugin;
228
+ const id = td.$$id;
229
+ return (
230
+ <VRenderer
231
+ plugin={info.plugin}
232
+ renderer={info.renderer}
233
+ key={id}
234
+ $schema="/schemas/TdObject.json"
235
+ hostId={info.id}
236
+ memberIndex={colIndex} // TODO: colIndex 其实不对,需要增加 schema filter 功能来让插件能
237
+ name={`${`单元格 ${rowIndex + 1},${colIndex + 1}`}`}
238
+ id={id}
239
+ draggable={false}
240
+ wrapperResolve={plugin.fieldWrapperResolve}
241
+ schemaPath={`${info.schemaPath}/td`}
242
+ path={`${this.props.$path}/tr/${rowIndex}/td/${colIndex}`}
243
+ data={this.props.data}
244
+ children={dom}
245
+ />
246
+ );
247
+ },
248
+ renderTr(this: any, tr: any, rowIndex: number) {
249
+ const dom = this.super(tr, rowIndex);
250
+ const info: RendererInfo = this.props.$$editor;
251
+
252
+ if (!info || !tr.$$id) {
253
+ return dom;
254
+ }
255
+
256
+ const plugin = info.plugin as TableViewPlugin;
257
+ const id = tr.$$id;
258
+ return (
259
+ <VRenderer
260
+ plugin={info.plugin}
261
+ renderer={info.renderer}
262
+ key={id}
263
+ $schema="/schemas/TrObject.json"
264
+ hostId={info.id}
265
+ memberIndex={rowIndex}
266
+ name={`${`行 ${rowIndex + 1}`}`}
267
+ id={id}
268
+ draggable={false}
269
+ wrapperResolve={plugin.fieldWrapperResolve}
270
+ schemaPath={`${info.schemaPath}/tr`}
271
+ path={`${this.props.$path}/tr/${rowIndex}`}
272
+ data={this.props.data}
273
+ children={dom}
274
+ />
275
+ );
276
+ }
277
+ };
278
+
279
+ tdVRendererConfig: VRendererConfig = {
280
+ panelTitle: '单元格',
281
+ panelBodyCreator: (context: BaseEventContext) => {
282
+ return [
283
+ {
284
+ label: '单元格背景色',
285
+ type: 'input-color',
286
+ name: 'background'
287
+ },
288
+ {
289
+ label: '文字颜色',
290
+ type: 'input-color',
291
+ name: 'color'
292
+ },
293
+ {
294
+ type: 'switch',
295
+ name: 'bold',
296
+ mode: 'inline',
297
+ className: 'w-full',
298
+ label: '文字加粗'
299
+ },
300
+ {
301
+ type: 'input-number',
302
+ name: 'padding',
303
+ label: '内边距'
304
+ },
305
+ {
306
+ type: 'select',
307
+ name: 'align',
308
+ label: '水平对齐',
309
+ options: [
310
+ {
311
+ label: '左',
312
+ value: 'left'
313
+ },
314
+ {
315
+ label: '中',
316
+ value: 'center'
317
+ },
318
+ {
319
+ label: '右',
320
+ value: 'right'
321
+ }
322
+ ]
323
+ },
324
+ {
325
+ type: 'select',
326
+ name: 'valign',
327
+ label: '垂直对齐',
328
+ options: [
329
+ {
330
+ label: '顶',
331
+ value: 'top'
332
+ },
333
+ {
334
+ label: '中',
335
+ value: 'middle'
336
+ },
337
+ {
338
+ label: '底',
339
+ value: 'bottom'
340
+ }
341
+ ]
342
+ },
343
+ {
344
+ type: 'input-number',
345
+ name: 'rowspan',
346
+ label: '跨几行'
347
+ },
348
+ {
349
+ type: 'input-number',
350
+ name: 'colspan',
351
+ label: '跨几列'
352
+ },
353
+ getSchemaTpl('style')
354
+ ];
355
+ }
356
+ };
357
+
358
+ trVRendererConfig: VRendererConfig = {
359
+ panelTitle: ' 行',
360
+ panelBodyCreator: (context: BaseEventContext) => {
361
+ return [
362
+ {
363
+ label: '行背景色',
364
+ type: 'input-color',
365
+ name: 'background'
366
+ },
367
+ {
368
+ label: '行高度',
369
+ type: 'input-number',
370
+ name: 'height'
371
+ }
372
+ ];
373
+ }
374
+ };
375
+
376
+ renderRenderer(props: any) {
377
+ const $$editor = props.$$editor;
378
+ const renderer = $$editor.renderer;
379
+ const schema = props.$schema;
380
+ getCellRealPosition(schema);
381
+ return (
382
+ <TableViewEditor schema={schema} manager={this.manager}>
383
+ <renderer.component {...props} />
384
+ </TableViewEditor>
385
+ );
386
+ }
387
+
388
+ // 根据路径判断是选中单元格还是行
389
+ buildEditorPanel(
390
+ context: BuildPanelEventContext,
391
+ panels: Array<BasicPanelItem>
392
+ ) {
393
+ super.buildEditorPanel(context, panels);
394
+ if (context.info.schemaPath.endsWith('/td')) {
395
+ panels.push({
396
+ key: 'td',
397
+ order: 100,
398
+ icon: this.tdVRendererConfig.panelIcon || 'fa fa-tablet',
399
+ title: this.tdVRendererConfig.panelTitle || '格子',
400
+ render: this.manager.makeSchemaFormRender({
401
+ controls: this.tdVRendererConfig.panelControlsCreator
402
+ ? this.tdVRendererConfig.panelControlsCreator(context)
403
+ : this.tdVRendererConfig.panelControls!,
404
+ body: this.tdVRendererConfig.panelBodyCreator
405
+ ? this.tdVRendererConfig.panelBodyCreator(context)
406
+ : this.tdVRendererConfig.panelBody!
407
+ })
408
+ });
409
+ } else if (context.info.schemaPath.endsWith('/tr')) {
410
+ panels.push({
411
+ key: 'tr',
412
+ order: 100,
413
+ icon: this.trVRendererConfig.panelIcon || 'fa fa-tablet',
414
+ title: this.trVRendererConfig.panelTitle || '格子',
415
+ render: this.manager.makeSchemaFormRender({
416
+ controls: this.trVRendererConfig.panelControlsCreator
417
+ ? this.trVRendererConfig.panelControlsCreator(context)
418
+ : this.trVRendererConfig.panelControls!,
419
+ body: this.trVRendererConfig.panelBodyCreator
420
+ ? this.trVRendererConfig.panelBodyCreator(context)
421
+ : this.trVRendererConfig.panelBody!
422
+ })
423
+ });
424
+ }
425
+ }
426
+
427
+ /**
428
+ * 插入行,需要处理前面有 rowspan 的情况
429
+ *
430
+ * +---+---+---+
431
+ * | a | b | c |
432
+ * + +---+---+
433
+ * | | d | e |
434
+ * + +---+---+
435
+ * | | f | g |
436
+ * +---+---+---+
437
+ *
438
+ * 比如在 d 位置的前面插入行,需要将 a 的 rowspan 加一,然后再插入两个单元格
439
+ */
440
+ insertRow(tdId: string, position: 'above' | 'below') {
441
+ const store = this.manager.store;
442
+ const paths = store.getNodePathById(tdId);
443
+ const tableId = paths[paths.length - 3].id;
444
+ const table = store.getSchema(tableId);
445
+ getCellRealPosition(table);
446
+
447
+ const td = JSONGetById(table, tdId);
448
+
449
+ if (!td) {
450
+ console.warn('找不到对应的 td id');
451
+ return;
452
+ }
453
+
454
+ let insertRow = td.$$row;
455
+ if (position === 'below') {
456
+ insertRow = insertRow + 1;
457
+ }
458
+
459
+ // 通过第一行来确认表格一共多少列
460
+ const firstRow = table.trs[0];
461
+ const firstRowLastTd = firstRow.tds[firstRow.tds.length - 1];
462
+ if (!firstRowLastTd) {
463
+ console.warn('第一列没内容');
464
+ return;
465
+ }
466
+ let colSize = firstRowLastTd.$$col + (firstRowLastTd.colspan || 1);
467
+ let insertIndex = table.trs.length;
468
+ for (let trIndex = 0; trIndex < table.trs.length; trIndex++) {
469
+ for (const td of table.trs[trIndex].tds || []) {
470
+ const tdRow = td.$$row;
471
+ const rowspan = td.rowspan || 1;
472
+ const colspan = td.colspan || 1;
473
+ // 如果覆盖到要插入的行,则增加 rowspan,并在这个插入的行中减去对应
474
+ if (rowspan > 1) {
475
+ const isOverlapping = tdRow + rowspan > insertRow;
476
+ if (isOverlapping) {
477
+ td.rowspan = rowspan + 1;
478
+ colSize = colSize - colspan;
479
+ }
480
+ }
481
+ if (tdRow === insertRow) {
482
+ insertIndex = trIndex;
483
+ break;
484
+ }
485
+ }
486
+ }
487
+
488
+ const insertTds = [];
489
+ for (let i = 0; i < colSize; i++) {
490
+ insertTds.push(TD_TEMPLATE);
491
+ }
492
+ table.trs.splice(insertIndex, 0, {tds: insertTds});
493
+ this.manager.store.changeValueById(tableId, table);
494
+ }
495
+
496
+ /**
497
+ * 插入列
498
+ *
499
+ * +---+---+---+
500
+ * | a | b |
501
+ * + +---+
502
+ * | | c |
503
+ * +---+---+---+
504
+ * | d | e | f |
505
+ * +---+---+---+
506
+ *
507
+ * 比如在 c 位置左侧插入列,应该将 a 的 colspan 加一,然后在最后一行增加一个单元格
508
+ */
509
+ insertCol(tdId: string, position: 'left' | 'right') {
510
+ const store = this.manager.store;
511
+ const paths = store.getNodePathById(tdId);
512
+ const tableId = paths[paths.length - 3].id;
513
+ const table = store.getSchema(tableId);
514
+ getCellRealPosition(table);
515
+ const td = JSONGetById(table, tdId);
516
+ if (!td) {
517
+ console.warn('找不到对应的 td id');
518
+ return;
519
+ }
520
+
521
+ let insertCol = td.$$col;
522
+ if (position === 'right') {
523
+ insertCol = insertCol + 1;
524
+ }
525
+
526
+ for (const tr of table.trs || []) {
527
+ const tds = tr.tds || [];
528
+ let isInserted = false;
529
+ for (let tdIndex = 0; tdIndex < tds.length; tdIndex++) {
530
+ const td = tds[tdIndex];
531
+ const tdColspan = td.colspan || 1;
532
+ const tdCol = td.$$col;
533
+ // 如果要插入的行被覆盖了,则对节点加一并跳过插入
534
+ if (tdColspan > 1) {
535
+ const isOverlapping = tdCol + tdColspan > insertCol;
536
+ if (isOverlapping) {
537
+ td.colspan = tdColspan + 1;
538
+ isInserted = true;
539
+ break;
540
+ }
541
+ }
542
+ if (insertCol <= tdCol) {
543
+ tds.splice(tdIndex, 0, TD_TEMPLATE);
544
+ isInserted = true;
545
+ break;
546
+ }
547
+ }
548
+ // 如果没找到对应的节点,那可能是插入到最后一条或者这一列节点数量不够,此时就要插入到最后
549
+ if (!isInserted) {
550
+ tds.push(TD_TEMPLATE);
551
+ }
552
+ }
553
+ this.manager.store.changeValueById(tableId, table);
554
+ }
555
+
556
+ /**
557
+ * 拆分有跨行或跨列的单元格
558
+ *
559
+ * +---+---+---+
560
+ * | a | b |
561
+ * + +---+
562
+ * | | c |
563
+ * +---+---+---+
564
+ * | d | e | f |
565
+ * +---+---+---+
566
+ *
567
+ * 比如拆分 a,最后要变成
568
+ *
569
+ * +---+---+---+
570
+ * | a | g | b |
571
+ * +---+---+---+
572
+ * | h | i | c |
573
+ * +---+---+---+
574
+ * | d | e | f |
575
+ * +---+---+---+
576
+ *
577
+ * 因此要新增 g、h、i 三个单元格
578
+ */
579
+ splitCell(tdId: string) {
580
+ const store = this.manager.store;
581
+ const paths = store.getNodePathById(tdId);
582
+ const tableId = paths[paths.length - 3].id;
583
+ const table = store.getSchema(tableId);
584
+ getCellRealPosition(table);
585
+ const td = JSONGetById(table, tdId);
586
+ if (!td) {
587
+ console.warn('找不到对应的 td id');
588
+ return;
589
+ }
590
+
591
+ const rowspan = td.rowspan || 1;
592
+ const colspan = td.colspan || 1;
593
+
594
+ // 将这个单元格的跨行和跨列都设置为 1
595
+ td.colspan = 1;
596
+ td.rowspan = 1;
597
+
598
+ // 算出需要补充哪些单元格及位置
599
+ const tdRow = td.$$row;
600
+ const tdCol = td.$$col;
601
+ const insertTds = [];
602
+ for (var i = 0; i < rowspan; i++) {
603
+ for (var j = 0; j < colspan; j++) {
604
+ // 跳过第一个,也就是这个单元格自己的位置
605
+ if (i === 0 && j === 0) {
606
+ continue;
607
+ }
608
+ insertTds.push({row: tdRow + i, col: tdCol + j});
609
+ }
610
+ }
611
+
612
+ // 需要将列大的放前面,主要是因为后面需要反向遍历才能动态删数据
613
+ insertTds.sort((a: any, b: any) => {
614
+ return b.col - a.col;
615
+ });
616
+
617
+ for (const tr of table.trs) {
618
+ for (let tdIndex = 0; tdIndex < tr.tds.length; tdIndex++) {
619
+ const td = tr.tds[tdIndex];
620
+ const currentRow = td.$$row;
621
+ const currentCol = td.$$col;
622
+ let insertIndex = insertTds.length;
623
+ while (insertIndex--) {
624
+ const insertTd = insertTds[insertIndex];
625
+ if (currentRow === insertTd.row) {
626
+ if (insertTd.col <= currentCol) {
627
+ tr.tds.splice(tdIndex, 0, TD_TEMPLATE);
628
+ } else {
629
+ tr.tds.push(TD_TEMPLATE);
630
+ }
631
+ insertTds.splice(insertIndex, 1);
632
+ }
633
+ }
634
+ }
635
+ }
636
+
637
+ // 如果前面有单元格找不到位置,那意味着是下面这种情况,这个单元格跨两行且是最后一行
638
+ // 这时 table.tr 其实只有一行数据,需要在添加一行数据
639
+ // +---+---+
640
+ // | a |
641
+ // + +
642
+ // | |
643
+ // +---+---+
644
+ if (insertTds.length) {
645
+ const newTds = [];
646
+ for (var i = 0; i < insertTds.length; i++) {
647
+ newTds.push(TD_TEMPLATE);
648
+ }
649
+ table.trs.push({tds: newTds});
650
+ }
651
+
652
+ this.manager.store.changeValueById(tableId, table);
653
+ }
654
+
655
+ buildEditorToolbar(
656
+ {schema, info}: BaseEventContext,
657
+ toolbars: Array<BasicToolbarItem>
658
+ ) {
659
+ if (info.schemaPath.endsWith('/td')) {
660
+ const tdId = schema.$$id;
661
+ toolbars.push({
662
+ icon: 'fa fa-chevron-left',
663
+ order: 100,
664
+ tooltip: '左侧新增列',
665
+ onClick: () => {
666
+ this.insertCol(tdId, 'left');
667
+ }
668
+ });
669
+ toolbars.push({
670
+ icon: 'fa fa-chevron-down',
671
+ order: 100,
672
+ tooltip: '下方新增行',
673
+ onClick: () => {
674
+ this.insertRow(tdId, 'below');
675
+ }
676
+ });
677
+ toolbars.push({
678
+ icon: 'fa fa-chevron-up',
679
+ order: 100,
680
+ tooltip: '上方新增行',
681
+ onClick: () => {
682
+ this.insertRow(tdId, 'above');
683
+ }
684
+ });
685
+ toolbars.push({
686
+ icon: 'fa fa-chevron-right',
687
+ order: 100,
688
+ tooltip: '右侧新增列',
689
+ onClick: () => {
690
+ this.insertCol(tdId, 'right');
691
+ }
692
+ });
693
+ const colspan = schema.colspan || 1;
694
+ const rowspan = schema.rowspan || 1;
695
+ if (colspan > 1 || rowspan > 1) {
696
+ toolbars.push({
697
+ icon: 'fa fa-columns',
698
+ order: 100,
699
+ tooltip: '拆分单元格',
700
+ onClick: () => {
701
+ this.splitCell(tdId);
702
+ }
703
+ });
704
+ }
705
+ }
706
+ }
707
+ }
708
+
709
+ registerEditorPlugin(TableViewPlugin);