1
|
|
|
// needed js files |
2
|
|
|
var js = { |
3
|
|
|
ace: 'ace.js', |
4
|
|
|
aceExtWhitespace: 'ext-whitespace.js', |
5
|
|
|
pbSyntaxHighlighter: CKEDITOR.plugins.getPath('pbckcode') + 'dialogs/PBSyntaxHighlighter.js' |
|
|
|
|
6
|
|
|
}; |
7
|
|
|
|
8
|
|
|
var commandName = 'pbckcode'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Plugin definition |
12
|
|
|
*/ |
13
|
|
|
CKEDITOR.plugins.add('pbckcode', { |
14
|
|
|
icons: 'pbckcode', |
15
|
|
|
hidpi: true, |
16
|
|
|
lang: ['fr', 'en', 'ru'], |
17
|
|
|
init: function(editor) { |
18
|
|
|
// if there is no user settings |
19
|
|
|
// create an empty object |
20
|
|
|
if (editor.config.pbckcode === undefined) { |
21
|
|
|
editor.config.pbckcode = {}; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// default settings object |
25
|
|
|
var DEFAULT_SETTINGS = { |
26
|
|
|
cls: '', |
27
|
|
|
modes: [ |
28
|
|
|
['HTML', 'html'], |
29
|
|
|
['CSS', 'css'], |
30
|
|
|
['PHP', 'php'], |
31
|
|
|
['JS', 'javascript'] |
32
|
|
|
], |
33
|
|
|
theme: 'textmate', |
34
|
|
|
tab_size: 4, |
35
|
|
|
js: '//cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/' |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
// merge user settings with default settings |
39
|
|
|
editor.settings = CKEDITOR.tools.extend(DEFAULT_SETTINGS, editor.config.pbckcode, true); |
|
|
|
|
40
|
|
|
editor.settings.js = normalizeJsUrl(editor.settings.js); |
41
|
|
|
|
42
|
|
|
// load CSS for the dialog |
43
|
|
|
editor.on('instanceReady', function() { |
44
|
|
|
CKEDITOR.document.appendStyleSheet(this.path + 'dialogs/style.css'); |
|
|
|
|
45
|
|
|
}.bind(this)); |
46
|
|
|
|
47
|
|
|
// add the button in the toolbar |
48
|
|
|
editor.ui.addButton('pbckcode', { |
49
|
|
|
label: editor.lang.pbckcode.addCode, |
50
|
|
|
command: commandName, |
51
|
|
|
toolbar: 'pbckcode' |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
// link the button to the command |
55
|
|
|
editor.addCommand(commandName, new CKEDITOR.dialogCommand('pbckcodeDialog', { |
56
|
|
|
allowedContent: 'pre[*]{*}(*)' |
57
|
|
|
}) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
// disable the button while the required js files are not loaded |
61
|
|
|
editor.getCommand(commandName).disable(); |
62
|
|
|
|
63
|
|
|
// add the plugin dialog element to the plugin |
64
|
|
|
CKEDITOR.dialog.add('pbckcodeDialog', this.path + 'dialogs/pbckcode.js'); |
65
|
|
|
|
66
|
|
|
// add the context menu |
67
|
|
|
if (editor.contextMenu) { |
68
|
|
|
editor.addMenuGroup('pbckcodeGroup'); |
69
|
|
|
editor.addMenuItem('pbckcodeItem', { |
70
|
|
|
label: editor.lang.pbckcode.editCode, |
71
|
|
|
icon: this.path + 'icons/pbckcode.png', |
72
|
|
|
command: commandName, |
73
|
|
|
group: 'pbckcodeGroup' |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
editor.contextMenu.addListener(function(element) { |
77
|
|
|
if (element.getAscendant('pre', true)) { |
|
|
|
|
78
|
|
|
return {pbckcodeItem: CKEDITOR.TRISTATE_OFF}; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
var scripts = [ |
84
|
|
|
getScriptUrl(editor.settings.js, js.ace), |
85
|
|
|
js.pbSyntaxHighlighter |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
// Load the required js files |
89
|
|
|
// enable the button when loaded |
90
|
|
|
CKEDITOR.scriptLoader.load(scripts, function() { |
91
|
|
|
editor.getCommand(commandName).enable(); |
92
|
|
|
|
93
|
|
|
// need ace to be loaded |
94
|
|
|
CKEDITOR.scriptLoader.load([ |
|
|
|
|
95
|
|
|
getScriptUrl(editor.settings.js, js.aceExtWhitespace) |
96
|
|
|
]); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
function normalizeJsUrl(js) { |
102
|
|
|
return js.concat('/') |
103
|
|
|
.replace(new RegExp('([^:]\/)\/+', 'g'), '$1'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function getScriptUrl(prefix, scriptName) { |
107
|
|
|
return prefix + scriptName; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.