-
Notifications
You must be signed in to change notification settings - Fork 18
/
plugin.js
101 lines (86 loc) · 3.21 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
import style from "./css/style.js";
import pluginAPI from "./js/main.js";
(function () {
"use strict";
const global = tinymce.util.Tools.resolve("tinymce.PluginManager");
let imgList = { length: 0 };
function open (editor) {
editor.windowManager.open({
title: "本地图片上传",
width: 500,
height: 400,
html: `
${style}
<div class="mce-box-content" id="mceBoxContent">
<label class="add-img">
<input type="file" id="imgFile" name="file" multiple="multiple" hidden>
</label>
</div>
`,
buttons: [{
text: 'Ok',
subtype: 'primary',
onclick: function (e) {
pluginAPI.uploadpic(
editor,
imgList,
{
headers: tinyMCE.activeEditor.getParam('imageupload_headers'),
imageUploadUrl: tinyMCE.activeEditor.getParam('imageupload_url'),
convertCb: tinyMCE.activeEditor.getParam('imageupload_converCb')
}).then(() => {
imgList = { length: 0 };
});
editor.windowManager.close();
}
}, {
text: 'Close',
onclick: function () {
editor.windowManager.close();
}
}]
});
};
function commandRegister(editor) {
editor.addCommand("mceImageUpload", function () {
open(editor);
const $imgFile = document.getElementById("imgFile");
const $mceBoxContent = document.getElementById("mceBoxContent");
$imgFile.addEventListener("change", (e) => {
const filesLise = Array.prototype.slice.call(e.target.files);
const orignalLen = imgList.length;
pluginAPI.imagePreview(filesLise, orignalLen, $mceBoxContent);
filesLise.forEach((item, index) => {
imgList[imgList.length] = item;
imgList.length = imgList.length + 1;
});
})
pluginAPI.on($mceBoxContent, "del-btn", "click", (e) => {
const elemIndex = e.target.getAttribute("index");
const parentNode = pluginAPI.parentNodes(e.target, 'prev-img-out-box');
parentNode.remove();
delete imgList[elemIndex];
imgList.length = imgList.length - 1;
})
});
};
function componentRegister (editor) {
editor.addButton("imageupload", {
title: "上传图片",
icon: "image",
cmd: "mceImageUpload",
// image : url + '/img/icon.png'
});
editor.addMenuItem("imageupload", {
icon: "image",
context: "insert",
cmd: "mceImageUpload"
});
};
global.add("imageupload", function (editor) {
componentRegister(editor);
commandRegister(editor);
});
function Plugin() {}
return Plugin;
})()