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
/*
    menu - link
*/
import $ from '../../util/dom-core.js'
import { getRandom } from '../../util/util.js'
import Panel from '../panel.js'
 
// 构造函数
function Link(editor) {
    this.editor = editor
    this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-link"></i></div>')
    this.type = 'panel'
 
    // 当前是否 active 状态
    this._active = false
}
 
// 原型
Link.prototype = {
    constructor: Link,
 
    // 点击事件
    onClick: function (e) {
        const editor = this.editor
        let $linkelem
 
        if (this._active) {
            // 当前选区在链接里面
            $linkelem = editor.selection.getSelectionContainerElem()
            if (!$linkelem) {
                return
            }
            // 将该元素都包含在选取之内,以便后面整体替换
            editor.selection.createRangeByElem($linkelem)
            editor.selection.restoreSelection()
            // 显示 panel
            this._createPanel($linkelem.text(), $linkelem.attr('href'))
        } else {
            // 当前选区不在链接里面
            if (editor.selection.isSelectionEmpty()) {
                // 选区是空的,未选中内容
                this._createPanel('', '')
            } else {
                // 选中内容了
                this._createPanel(editor.selection.getSelectionText(), '')
            }
        }
    },
 
    // 创建 panel
    _createPanel: function (text, link) {
        // panel 中需要用到的id
        const inputLinkId = getRandom('input-link')
        const inputTextId = getRandom('input-text')
        const btnOkId = getRandom('btn-ok')
        const btnDelId = getRandom('btn-del')
 
        // 是否显示“删除链接”
        const delBtnDisplay = this._active ? 'inline-block' : 'none'
 
        // 初始化并显示 panel
        const panel = new Panel(this, {
            width: 300,
            // panel 中可包含多个 tab
            tabs: [
                {
                    // tab 的标题
                    title: '链接',
                    // 模板
                    tpl: `<div>
                            <input id="${inputTextId}" type="text" class="block" value="${text}" placeholder="链接文字"/></td>
                            <input id="${inputLinkId}" type="text" class="block" value="${link}" placeholder="http://..."/></td>
                            <div class="w-e-button-container">
                                <button id="${btnOkId}" class="right">插入</button>
                                <button id="${btnDelId}" class="gray right" style="display:${delBtnDisplay}">删除链接</button>
                            </div>
                        </div>`,
                    // 事件绑定
                    events: [
                        // 插入链接
                        {
                            selector: '#' + btnOkId,
                            type: 'click',
                            fn: () => {
                                // 执行插入链接
                                const $link = $('#' + inputLinkId)
                                const $text = $('#' + inputTextId)
                                const link = $link.val()
                                const text = $text.val()
                                this._insertLink(text, link)
 
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        },
                        // 删除链接
                        {
                            selector: '#' + btnDelId,
                            type: 'click',
                            fn: () => {
                                // 执行删除链接
                                this._delLink()
 
                                // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
                                return true
                            }
                        }
                    ]
                } // tab end
            ] // tabs end
        })
 
        // 显示 panel
        panel.show()
 
        // 记录属性
        this.panel = panel
    },
 
    // 删除当前链接
    _delLink: function () {
        if (!this._active) {
            return
        }
        const editor = this.editor
        const $selectionELem = editor.selection.getSelectionContainerElem()
        if (!$selectionELem) {
            return
        }
        const selectionText = editor.selection.getSelectionText()
        editor.cmd.do('insertHTML', '<span>' + selectionText + '</span>')
    },
 
    // 插入链接
    _insertLink: function (text, link) {
        const editor = this.editor
        const config = editor.config
        const linkCheck = config.linkCheck
        let checkResult = true // 默认为 true
        if (linkCheck && typeof linkCheck === 'function') {
            checkResult = linkCheck(text, link)
        }
        if (checkResult === true) {
            editor.cmd.do('insertHTML', `<a href="${link}" target="_blank">${text}</a>`)
        } else {
            alert(checkResult)
        }
    },
 
    // 试图改变 active 状态
    tryChangeActive: function (e) {
        const editor = this.editor
        const $elem = this.$elem
        const $selectionELem = editor.selection.getSelectionContainerElem()
        if (!$selectionELem) {
            return
        }
        if ($selectionELem.getNodeName() === 'A') {
            this._active = true
            $elem.addClass('w-e-active')
        } else {
            this._active = false
            $elem.removeClass('w-e-active')
        }
    }
}
 
export default Link