-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
197 lines (178 loc) · 4.99 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
/*
* syntaxHighlight plugin
* v1.0
* Written by brouillon
* https://github.com/brouillon/syntaxHighlight
* Date 05/10/2013
* Released under LGPL License.
*/
tinymce.PluginManager.requireLangPack('syntaxHighlight');
tinymce.PluginManager.add('syntaxHighlight', function(editor, url) {
var shSettings = {},
defaultBrush = 'html',
selectedNode = false,
hadSelection = false;
function showDialog() {
var selection = editor.selection,
dom = editor.dom,
selectionContent = selection.getContent(),
rBrush, rBrushLen, i;
//clear
shSettings = {};
rBrush = [
{text:'Choose Language', value:'0'},
{text:'BASH', value:'bash'},
{text:'C / C++', value:'c'},
{text:'C#', value:'csharp'},
{text:'CSS', value:'css'},
{text:'DIFF', value:'diff'},
{text:'HTML', value:'html'},
{text:'Java', value:'java'},
{text:'JavaScript', value:'js'},
{text:'Perl', value:'perl'},
{text:'PHP', value:'php'},
{text:'Python', value:'py'},
{text:'Ruby', value:'ruby'},
{text:'Text', value:'text'},
{text:'SQL', value:'sql'},
{text:'XML', value:'XML'}
];
rBrushLen = rBrush.length;
selectedNode = selection.getNode();
//try to get settings
try {
shSettings = JSON.parse('{"'+dom.getAttrib(selectedNode, 'class').replace(/first-line/g, 'firstLine').replace(/: /g,'": "').replace(/; /g, '","').replace(/"true"/g, 'true').replace(/"false"/g, 'false').replace(/"\[/g, '[').replace(/\]"/g, ']')+'}');
}
//or init default
catch(e) {
shSettings = {
brush: defaultBrush,
gutter: true,
firstLine: 1,
highlight: []
};
}
//is there a selection
if (selectedNode.nodeName == 'BODY' && selectionContent.length == 0) {
hadSelection = false;
}
//a block of code
else if (selectedNode.nodeName == 'PRE') {
hadSelection = true;
shSettings.codebox = dom.getParent(selectedNode).textContent.replace(/\<\;/g, "<").replace(/\>\;/g, ">");
}
//Something else, select parent
else {
hadSelection = true;
//more than one block selected
if (selectedNode.nodeName == 'BODY') {
selectedNode = selection.getSelectedBlocks();
shSettings.codebox = selectionContent.replace(/<br \/>/g, "\r\n").replace(/(<[^>]*>)/g, '').replace(/ /g, '');
}
else {
//Select the parent
selectedNode = dom.getParent(selectedNode);
shSettings.codebox = dom.getParent(selectedNode).textContent.replace(/<br \/>/g, "\r\n").replace(/\<\;/g, "<").replace(/\>\;/g, ">");
}
}
//Select brush
for(i = 0; i< rBrushLen; i++){
if( rBrush[ i ].value == shSettings.brush){
rBrush[ i ].selected = true;
}
}
editor.windowManager.open({
title: 'Syntax Highlighter',
data: shSettings,
body: [
{
name: 'Language',
type: 'listbox',
values: rBrush,
onselect : function() {
shSettings.brush = this.value();
}
},
{
type: 'container',
layout: 'flex',
direction: 'row',
align: 'center',
spacing: 5,
items: [
{
type: 'label',
text: 'Start :'
},
{
name: 'firstLine',
type: 'textbox',
tooltip: 'Start number'
},
{
type: 'label',
text: 'Highlights :'
},
{
name: 'highlight',
type: 'textbox',
tooltip: 'Highlights Help'
},
{
type: 'checkbox',
checked: shSettings.gutter,
text: 'Show Numbers',
onclick: function(){
shSettings.gutter = this.value();
}
}
]
},
{
name: 'codebox',
type: 'textbox',
minHeight: 250,
multiline: true
}
],
onsubmit: function(e) {
var content, highlight, firstLine, brush, gutter;
//content to highlight
content = e.data.codebox.replace(/</g, "<").replace(/>/g, ">");
//lines to highlight
highlight = e.data.highlight.replace(/[^0-9,\-]/g, "");
//offset number
firstLine = e.data.firstLine.replace(/[^0-9]/g, "");
//language
brush = shSettings.brush;
//Show number
gutter = shSettings.gutter;
if (firstLine == '') {
firstLine = 1;
}
//if there was a selection before open
if (hadSelection == true) {
//remove content
setTimeout(function(){editor.dom.remove(selectedNode);}, 0);
}
//append new content into tinyMCE
setTimeout(function(){editor.insertContent('<pre style="background-color: #353535; color: #fff; margin: 5px 0; padding: 10px;" class="brush: '+brush+'; gutter: '+gutter+'; first-line: '+firstLine+'; highlight: ['+highlight+']">'+content+'</pre>', {format: 'raw'});}, 0);
}
});
}
editor.addButton('syntaxHighlight', {
image: url+'/img/syntaxHighlight.gif',
tooltip: 'Insert Code',
onclick: function() {
showDialog();
}
});
editor.addMenuItem('syntaxHighlight', {
text: 'code',
image: url+'/img/syntaxHighlight.gif',
context: 'insert',
onclick: function() {
showDialog();
}
});
});