forked from prbaron/pbckcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
executable file
·112 lines (93 loc) · 3.34 KB
/
plugin.js
File metadata and controls
executable file
·112 lines (93 loc) · 3.34 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
// needed js files
var js = {
ace : "ace.js",
aceExtWhitespace : "ext-whitespace.js",
pbSyntaxHighlighter : CKEDITOR.plugins.getPath('pbckcode') + "dialogs/PBSyntaxHighlighter.js"
};
var commandName = 'pbckcode';
/**
* Plugin definition
*/
CKEDITOR.plugins.add('pbckcode', {
icons : 'pbckcode',
hidpi: true,
lang : ['fr', 'en', 'ru'],
init : function (editor) {
var plugin = this;
// if there is no user settings
// create an empty object
if (editor.config.pbckcode === undefined) {
editor.config.pbckcode = {};
}
// default settings object
var DEFAULT_SETTINGS = {
cls : '',
modes : [
['HTML', 'html'],
['CSS', 'css'],
['PHP', 'php'],
['JS', 'javascript']
],
theme : 'textmate',
tab_size : 4,
js : "//cdn.jsdelivr.net//ace/1.1.4/noconflict///"
};
// merge user settings with default settings
editor.settings = CKEDITOR.tools.extend(DEFAULT_SETTINGS, editor.config.pbckcode, true);
editor.settings.js = normalizeJsUrl(editor.settings.js);
// load CSS for the dialog
editor.on('instanceReady', function () {
CKEDITOR.document.appendStyleSheet(plugin.path + "dialogs/style.css");
});
// add the button in the toolbar
editor.ui.addButton('pbckcode', {
label : editor.lang.pbckcode.addCode,
command : commandName,
toolbar : 'pbckcode'
});
// link the button to the command
editor.addCommand(commandName, new CKEDITOR.dialogCommand('pbckcodeDialog', {
allowedContent : 'pre[*]{*}(*)'
})
);
// disable the button while the required js files are not loaded
editor.getCommand(commandName).disable();
// add the plugin dialog element to the plugin
CKEDITOR.dialog.add('pbckcodeDialog', plugin.path + 'dialogs/pbckcode.js');
// add the context menu
if (editor.contextMenu) {
editor.addMenuGroup('pbckcodeGroup');
editor.addMenuItem('pbckcodeItem', {
label : editor.lang.pbckcode.editCode,
icon : plugin.path + "icons/pbckcode.png",
command : commandName,
group : 'pbckcodeGroup'
});
editor.contextMenu.addListener(function (element) {
if (element.getAscendant('pre', true)) {
return {pbckcodeItem : CKEDITOR.TRISTATE_OFF};
}
});
}
var scripts = [
getScriptUrl(editor.settings.js, js.ace),
js.pbSyntaxHighlighter
];
// Load the required js files
// enable the button when loaded
CKEDITOR.scriptLoader.load(scripts, function () {
editor.getCommand(commandName).enable();
// need ace to be loaded
CKEDITOR.scriptLoader.load([
getScriptUrl(editor.settings.js, js.aceExtWhitespace)
]);
});
}
});
function normalizeJsUrl(js) {
return js.concat("/")
.replace(new RegExp('([^:]\/)\/+', 'g'), '$1');
}
function getScriptUrl(prefix, scriptName) {
return prefix + scriptName;
}