-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtinymce-custom-class.js
51 lines (45 loc) · 1.8 KB
/
tinymce-custom-class.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
(function() {
tinymce.PluginManager.add( 'custom_class', function( editor, url ) {
// Add Button to Visual Editor Toolbar
editor.addButton('custom_class', {
title: 'Insert CSS Class',
cmd: 'custom_class',
icon: 'icon dashicons-wordpress',
});
// Add Command when Button Clicked
editor.addCommand('custom_class', function() {
// Check we have selected some text selected
var text = editor.selection.getContent({
'format': 'html'
});
if ( text.length === 0 ) {
alert( 'Please select some text.' );
return;
}
// Ask the user to enter a CSS class
var result = prompt('Enter the CSS class');
if ( !result ) {
// User cancelled - exit
return;
}
if (result.length === 0) {
// User didn't enter anything - exit
return;
}
// Insert selected text back into editor, wrapping it in an anchor tag
editor.execCommand('mceReplaceContent', false, '<span class="' + result + '">' + text + '</span>');
});
// Enable/disable the button on the node change event
editor.onNodeChange.add(function( editor ) {
// Get selected text, and assume we'll disable our button
var selection = editor.selection.getContent();
var disable = true;
// If we have some text selected, don't disable the button
if ( selection ) {
disable = false;
}
// Define whether our button should be enabled or disabled
editor.controlManager.setDisabled( 'custom_class', disable );
});
});
})();