wzp
2021-07-19 e65183d31755a0e5fae4bf428435d2e0fd6afdc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
    menu - table
*/
import $ from '../../util/dom-core.js'
import { getRandom } from '../../util/util.js'
import Panel from '../panel.js'
 
// 构造函数
function Table(editor) {
    this.editor = editor
    this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-table2"></i></div>')
    this.type = 'panel'
 
    // 当前是否 active 状态
    this._active = false
}
 
// 原型
Table.prototype = {
    constructor: Table,
 
    onClick: function () {
        if (this._active) {
            // 编辑现有表格
            this._createEditPanel()
        } else {
            // 插入新表格
            this._createInsertPanel()
        }
    },
 
    // 创建插入新表格的 panel
    _createInsertPanel: function () {
        // 用到的 id
        const btnInsertId = getRandom('btn')
        const textRowNum = getRandom('row')
        const textColNum = getRandom('col')
 
        const panel = new Panel(this, {
            width: 250,
            // panel 包含多个 tab
            tabs: [
                {
                    // 标题
                    title: '插入表格',
                    // 模板
                    tpl: `<div>
                        <p style="text-align:left; padding:5px 0;">
                            创建
                            <input id="${textRowNum}" type="text" value="5" style="width:40px;text-align:center;"/>
                            行
                            <input id="${textColNum}" type="text" value="5" style="width:40px;text-align:center;"/>
                            列的表格
                        </p>
                        <div class="w-e-button-container">
                            <button id="${btnInsertId}" class="right">插入</button>
                        </div>
                    </div>`,
                    // 事件绑定
                    events: [
                        {
                            // 点击按钮,插入表格
                            selector: '#' + btnInsertId,
                            type: 'click',
                            fn: () => {
                                const rowNum = parseInt($('#' + textRowNum).val())
                                const colNum = parseInt($('#' + textColNum).val())
 
                                if (rowNum && colNum && rowNum > 0 && colNum > 0) {
                                    // form 数据有效
                                    this._insert(rowNum, colNum)
                                }
 
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        }
                    ]
                } // first tab end
            ]  // tabs end
        }) // panel end
 
        // 展示 panel
        panel.show()
 
        // 记录属性
        this.panel = panel
    },
 
    // 插入表格
    _insert: function (rowNum, colNum) {
        // 拼接 table 模板
        let r, c
        let html = '<table border="0" width="100%" cellpadding="0" cellspacing="0">'
        for (r = 0; r < rowNum; r++) {
            html += '<tr>'
            if (r === 0) {
                for (c = 0; c < colNum; c++) {
                    html += '<th>&nbsp;</th>'
                }
            } else {
                for (c = 0; c < colNum; c++) {
                    html += '<td>&nbsp;</td>'
                }
            }
            html += '</tr>'
        }
        html += '</table><p><br></p>'
 
        // 执行命令
        const editor = this.editor
        editor.cmd.do('insertHTML', html)
 
        // 防止 firefox 下出现 resize 的控制点
        editor.cmd.do('enableObjectResizing', false)
        editor.cmd.do('enableInlineTableEditing', false)
    },
 
    // 创建编辑表格的 panel
    _createEditPanel: function () {
        // 可用的 id
        const addRowBtnId = getRandom('add-row')
        const addColBtnId = getRandom('add-col')
        const delRowBtnId = getRandom('del-row')
        const delColBtnId = getRandom('del-col')
        const delTableBtnId = getRandom('del-table')
 
        // 创建 panel 对象
        const panel = new Panel(this, {
            width: 320,
            // panel 包含多个 tab
            tabs: [
                {
                    // 标题
                    title: '编辑表格',
                    // 模板
                    tpl: `<div>
                        <div class="w-e-button-container" style="border-bottom:1px solid #f1f1f1;padding-bottom:5px;margin-bottom:5px;">
                            <button id="${addRowBtnId}" class="left">增加行</button>
                            <button id="${delRowBtnId}" class="red left">删除行</button>
                            <button id="${addColBtnId}" class="left">增加列</button>
                            <button id="${delColBtnId}" class="red left">删除列</button>
                        </div>
                        <div class="w-e-button-container">
                            <button id="${delTableBtnId}" class="gray left">删除表格</button>
                        </dv>
                    </div>`,
                    // 事件绑定
                    events: [
                        {
                            // 增加行
                            selector: '#' + addRowBtnId,
                            type: 'click',
                            fn: () => {
                                this._addRow()
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        },
                        {
                            // 增加列
                            selector: '#' + addColBtnId,
                            type: 'click',
                            fn: () => {
                                this._addCol()
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        },
                        {
                            // 删除行
                            selector: '#' + delRowBtnId,
                            type: 'click',
                            fn: () => {
                                this._delRow()
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        },
                        {
                            // 删除列
                            selector: '#' + delColBtnId,
                            type: 'click',
                            fn: () => {
                                this._delCol()
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        },
                        {
                            // 删除表格
                            selector: '#' + delTableBtnId,
                            type: 'click',
                            fn: () => {
                                this._delTable()
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        }
                    ]
                }
            ]
        })
        // 显示 panel
        panel.show()
    },
 
    // 获取选中的单元格的位置信息
    _getLocationData: function () {
        const result = {}
        const editor = this.editor
        const $selectionELem = editor.selection.getSelectionContainerElem()
        if (!$selectionELem) {
            return
        }
        const nodeName = $selectionELem.getNodeName()
        if (nodeName !== 'TD' && nodeName !== 'TH') {
            return
        }
 
        // 获取 td index
        const $tr = $selectionELem.parent()
        const $tds = $tr.children()
        const tdLength = $tds.length
        $tds.forEach((td, index) => {
            if (td === $selectionELem[0]) {
                // 记录并跳出循环
                result.td = {
                    index: index,
                    elem: td,
                    length: tdLength
                }
                return false
            }
        })
 
        // 获取 tr index
        const $tbody = $tr.parent()
        const $trs = $tbody.children()
        const trLength = $trs.length
        $trs.forEach((tr, index) => {
            if (tr === $tr[0]) {
                // 记录并跳出循环
                result.tr = {
                    index: index,
                    elem: tr,
                    length: trLength
                }
                return false
            }
        })
 
        // 返回结果
        return result
    },
 
    // 增加行
    _addRow: function () {
        // 获取当前单元格的位置信息
        const locationData = this._getLocationData()
        if (!locationData) {
            return
        }
        const trData = locationData.tr
        const $currentTr = $(trData.elem)
        const tdData = locationData.td
        const tdLength = tdData.length
 
        // 拼接即将插入的字符串
        const newTr = document.createElement('tr')
        let tpl = '', i
        for (i = 0; i < tdLength; i++) {
            tpl += '<td>&nbsp;</td>'
        }
        newTr.innerHTML = tpl
        // 插入
        $(newTr).insertAfter($currentTr)
    },
 
    // 增加列
    _addCol: function () {
        // 获取当前单元格的位置信息
        const locationData = this._getLocationData()
        if (!locationData) {
            return
        }
        const trData = locationData.tr
        const tdData = locationData.td
        const tdIndex = tdData.index
        const $currentTr = $(trData.elem)
        const $trParent = $currentTr.parent()
        const $trs = $trParent.children()
 
        // 遍历所有行
        $trs.forEach(tr => {
            const $tr = $(tr)
            const $tds = $tr.children()
            const $currentTd = $tds.get(tdIndex)
            const name = $currentTd.getNodeName().toLowerCase()
 
            // new 一个 td,并插入
            const newTd = document.createElement(name)
            $(newTd).insertAfter($currentTd)
        })
    },
 
    // 删除行
    _delRow: function () {
        // 获取当前单元格的位置信息
        const locationData = this._getLocationData()
        if (!locationData) {
            return
        }
        const trData = locationData.tr
        const $currentTr = $(trData.elem)
        $currentTr.remove()
    },
 
    // 删除列
    _delCol: function () {
        // 获取当前单元格的位置信息
        const locationData = this._getLocationData()
        if (!locationData) {
            return
        }
        const trData = locationData.tr
        const tdData = locationData.td
        const tdIndex = tdData.index
        const $currentTr = $(trData.elem)
        const $trParent = $currentTr.parent()
        const $trs = $trParent.children()
 
        // 遍历所有行
        $trs.forEach(tr => {
            const $tr = $(tr)
            const $tds = $tr.children()
            const $currentTd = $tds.get(tdIndex)
            // 删除
            $currentTd.remove()
        })
    },
 
    // 删除表格
    _delTable: function () {
        const editor = this.editor
        const $selectionELem = editor.selection.getSelectionContainerElem()
        if (!$selectionELem) {
            return
        }
        const $table = $selectionELem.parentUntil('table')
        if (!$table) {
            return
        }
        $table.remove()
    },
 
    // 试图改变 active 状态
    tryChangeActive: function (e) {
        const editor = this.editor
        const $elem = this.$elem
        const $selectionELem = editor.selection.getSelectionContainerElem()
        if (!$selectionELem) {
            return
        }
        const nodeName = $selectionELem.getNodeName()
        if (nodeName === 'TD' || nodeName === 'TH') {
            this._active = true
            $elem.addClass('w-e-active')
        } else {
            this._active = false
            $elem.removeClass('w-e-active')
        }
    }
}
 
export default Table