plugin.min.js
4.11 KB
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
/**
* plugin.js
*
* Copyright, Dmitry A Karasev (dima at karasev dot pro)
*
*/
/*global tinymce:true */
tinymce.PluginManager.add("fieldsetcontrols", function(editor) {
editor.addButton("addfieldset", {
text: "+FieldSet",
icon: false,
tooltip: "Insert/edit fieldset",
onclick: function() {
var data = {};
var selection = editor.selection;
var dom = editor.dom;
var selectedElm, anchorElm;
selectedElm = selection.getNode();
anchorElm = dom.getParent(selectedElm,"fieldset");
data.class = "";
data.legend = "";
if(anchorElm) {
data.class = anchorElm.className;
if(anchorElm.firstChild) {
if(anchorElm.firstChild.nodeName=="LEGEND") {
data.legend = anchorElm.firstChild.innerText;
}
}
}
editor.windowManager.open({
title: "Insert/edit fieldset",
data: data,
body: [
{ type: "textbox", name: "legend", label: "Legend" },
{ type: "listbox", name: "class", label: "Class", values: [{text:"None",value:""},{text:"spoiler",value:"spoiler"}] }
],
width: 600,
height: 130,
onsubmit: function(e) {
data = tinymce.extend(data,e.data);
data.legend = data.legend ? data.legend : "Legend";
var fieldsetAttrs = {
class: data.class ? data.class : null
};
if(anchorElm) {
editor.focus();
dom.setAttribs(anchorElm,fieldsetAttrs);
if(anchorElm.firstChild) {
if(anchorElm.firstChild.nodeName=="LEGEND") {
anchorElm.firstChild.innerText = data.legend;
}
else {
anchorElm.insertBefore(dom.create("legend",null,data.legend),anchorElm.firstChild);
}
}
else {
dom.add(anchorElm,"legend",null,data.legend);
dom.add(anchorElm,"p",null," ");
}
}
else {
editor.insertContent(dom.createHTML("fieldset",fieldsetAttrs,"<legend>"+data.legend+"</legend>"+selection.getContent({format:"html"})+"<p> </p>")+"<p> </p>");
}
}
});
},
stateSelector: "fieldset"
});
editor.addButton("delfieldset", {
text: "-FieldSet",
icon: false,
tooltip: "Remove fieldset",
onclick: function() {
var data = {};
var selection = editor.selection;
var dom = editor.dom;
var selectedElm, anchorElm;
selectedElm = selection.getNode();
anchorElm = dom.getParent(selectedElm,"fieldset");
if(anchorElm) {
editor.focus();
if(dom.isEmpty(selectedElm) && anchorElm.lastChild==selectedElm) {
if(anchorElm.firstChild!=selectedElm) {
dom.insertAfter(selectedElm,anchorElm);
selection.setCursorLocation(selectedElm,0);
}
else {
if(anchorElm.firstChild.nodeName=="LEGEND") {
dom.rename(anchorElm.firstChild,"p");
}
dom.remove(anchorElm,true);
}
}
else if(anchorElm.firstChild) {
if(anchorElm.firstChild.nodeName=="LEGEND") {
dom.rename(anchorElm.firstChild,"p");
}
dom.remove(anchorElm,true);
}
}
},
stateSelector: "fieldset"
});
});