wzp
2021-09-01 2891fe0769189be39c9634b2cbc1841dbd52d022
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
/*
    menu - emoticon
*/
import $ from '../../util/dom-core.js'
import Panel from '../panel.js'
 
// 构造函数
function Emoticon(editor) {
    this.editor = editor
    this.$elem = $(
        `<div class="w-e-menu">
            <i class="w-e-icon-happy"></i>
        </div>`
    )
    this.type = 'panel'
 
    // 当前是否 active 状态
    this._active = false
}
 
// 原型
Emoticon.prototype = {
    constructor: Emoticon,
 
    onClick: function () {
        this._createPanel()
    },
 
    _createPanel: function () {
        const editor = this.editor
        const config = editor.config
        // 获取表情配置
        const emotions = config.emotions || []
 
        // 创建表情 dropPanel 的配置
        const tabConfig = []
        emotions.forEach(emotData => {
            const emotType = emotData.type
            const content = emotData.content || []
 
            // 这一组表情最终拼接出来的 html
            let faceHtml = ''
 
            // emoji 表情
            if (emotType === 'emoji') {
                content.forEach(item => {
                    if (item) {
                        faceHtml += '<span class="w-e-item">' + item + '</span>'
                    }
                })
            }
            // 图片表情
            if (emotType === 'image') {
                content.forEach(item => {
                    const src = item.src
                    const alt = item.alt
                    if (src) {
                        // 加一个 data-w-e 属性,点击图片的时候不再提示编辑图片
                        faceHtml += '<span class="w-e-item"><img src="' + src + '" alt="' + alt + '" data-w-e="1"/></span>'
                    }
                })
            }
 
            tabConfig.push({
                title: emotData.title,
                tpl: `<div class="w-e-emoticon-container">${faceHtml}</div>`,
                events: [
                    {
                        selector: 'span.w-e-item',
                        type: 'click',
                        fn: (e) => {
                            const target = e.target
                            const $target = $(target)
                            const nodeName = $target.getNodeName()
 
                            let insertHtml
                            if (nodeName === 'IMG') {
                                // 插入图片
                                insertHtml = $target.parent().html()
                            } else {
                                // 插入 emoji
                                insertHtml = '<span>' + $target.html() + '</span>'
                            }
 
                            this._insert(insertHtml)
                            // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                            return true
                        }
                    }
                ]
            })
        })
 
        const panel = new Panel(this, {
            width: 300,
            height: 200,
            // 一个 Panel 包含多个 tab
            tabs: tabConfig
        })
 
        // 显示 panel
        panel.show()
 
        // 记录属性
        this.panel = panel
    },
 
    // 插入表情
    _insert: function (emotHtml) {
        const editor = this.editor
        editor.cmd.do('insertHTML', emotHtml)
    }
}
 
export default Emoticon