-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
229 lines (200 loc) · 7.36 KB
/
plugin.js
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
/* global tinymce, twemoji, _wpemojiSettings, _smileySettings */
function loadCSS() {
let url = "/wp-content/plugins/easy-emoji/plugin.css"
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
document.getElementsByTagName('head')[0].appendChild(link);
}
// 加载 CSS 文件
loadCSS();
(function () {
function get_div_html() {
let json_data = window.easy_emoji_json_data;
window.easy_emoji_tab_switch = function (elementID, listName, n) {
var elem = document.getElementById(elementID);
var elemlist = elem.getElementsByTagName("li");
for (var i = 0; i < elemlist.length; i++) {
elemlist[i].className = "normal";
var m = i + 1;
document.getElementById(listName + "_" + m).className = "normal";
}
elemlist[n - 1].className = "current";
document.getElementById(listName + "_" + n).className = "current";
}
let tabBarString = "";
{
let tagNameStr = "";
{
let index = 1;
for (key in json_data) {
if (index == 1) {
tagNameStr += ` <li class="current" onmouseover="easy_emoji_tab_switch('hotnews_caption','list',${index++});">${key}</li>`;
} else {
tagNameStr += ` <li class="normal" onmouseover="easy_emoji_tab_switch('hotnews_caption','list',${index++});">${key}</li>`;
}
tagNameStr += "\n"
}
}
tabBarString = `
<div id="hotnews_caption">
<ul style="margin-left:0px;">
${tagNameStr}
</ul>
</div>
`
}
let tabContent = '<div id="hotnews_content">\n'
{
let tabContentItems = "";
let index = 1;
for (key in json_data) {
let beginStr = "";
if (index == 1) {
beginStr = `<div class="current" id="list_${index++}">`
} else {
beginStr = `<div class="normal" id="list_${index++}">`
}
beginStr += "\n";
let item = json_data[key]
for (imgname in item.names) {
//let imgStr = `<div class="IMG_PARENT"> <img src="${item.url + item.names[imgname]}"> </div>`
let imgStr = `<img src="${item.url + item.names[imgname]}" >`
beginStr = beginStr + "\t" + imgStr + "\n"
}
beginStr += "</div>";
tabContentItems += beginStr
tabContentItems += "\n"
}
tabContent += tabContentItems;
tabContent += '\n'
}
let curHtml = `
<div id="easy_emoji">
${tabBarString}
${tabContent}
</div>
`
return curHtml;
}
window.easy_emoji_mce_function = function (editor) {
editor.addButton('easy_emoji', {
type: 'panelbutton',
panel: {
classes: 'easy-panel',
role: 'application',
autohide: true,
html: get_div_html,
onShow: function (e) {
let id = this._id;
//需要删除这个面板高度
let resetHeigth;
resetHeigth = function () {
let ok = false;
do {
let ele = document.getElementById(id)
if (!ele) break;
if (ele.style.height == "") break;
ele.style.height = ""
ele.style.width = ""
ok = true;
} while (false)
if (!ok) setTimeout(resetHeigth, 10);
}
resetHeigth();
},
onclick: function (e) {
var img = e.target;
if (img && img.tagName == "IMG") {
var imgHtml = "<img width=48 height=48 src='" + img.currentSrc + "'>"
editor.insertContent(imgHtml);
}
this.hide();
}
},
tooltip: 'Emoticons'
});
};
if (typeof (tinymce) != 'undefined') {
tinymce.PluginManager.add('easy_emoji', easy_emoji_mce_function);
return;
}
//新版块编辑器
let plugin_name = "easy/emoji";
const { registerFormatType, unregisterFormatType, toggleFormat, applyFormat } = window.wp.richText;
const { BlockControls } = wp.blockEditor;
const { ToolbarGroup, ToolbarButton } = wp.components;
const { registerBlockType } = wp.blocks;
unregisterFormatType(plugin_name);
let showEmojiDialog = function (cb) {
let x = 0, y = 0;
{
let parentDiv = document.querySelector('.dashicons-easy_emoji');
while (parentDiv.parentElement && parentDiv.parentElement.tagName !== 'DIV') {
parentDiv = parentDiv.parentElement;
}
let butRect = parentDiv.getBoundingClientRect();
x = butRect.x - 10;
y = butRect.bottom + 3;
}
let dlg = document.getElementById("easy_emoji_panel");
if (!dlg) {
let htmlStr = `
<div class="easy_popup-content" id="easy_emoji_content">
${get_div_html()}
</div>
`;
let div = document.createElement("div");
div.id = "easy_emoji_panel";
div.className = "easy_popup";
div.innerHTML = htmlStr;
document.body.appendChild(div);
dlg = document.getElementById("easy_emoji_panel");
}
let dlgContent = document.getElementById("easy_emoji_content");
dlgContent.style.left = x + "px";
dlgContent.style.top = y + "px";
dlg.style.display = "block";
dlg.onclick = function (e) {
dlg.style.display = "none";
if (e.target.tagName != "IMG") return;
cb(e.target.src);
}
}
const buttonEasyEmoji = function ({ isActive, onChange, value }) {
const onClick = function () {
showEmojiDialog((url) => {
obj = wp.richText.create({
html: `<img src="${url}" width="32">`,
});
onChange(wp.richText.insert(
value,
obj,
value.start,
value.end
));
});
};
return wp.element.createElement(
BlockControls,
null,
wp.element.createElement(
ToolbarGroup,
null,
wp.element.createElement(ToolbarButton, {
icon: "easy_emoji",
title: "插入表情图片",
onClick: onClick,
isActive: isActive
})
)
);
};
registerFormatType(plugin_name, {
title: "easy_emoji",
tagName: "easy_emoji",
className: null,
edit: buttonEasyEmoji
});
})();