-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathplugin.js
172 lines (141 loc) · 6.89 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
/**
* @authors: Önder Ceylan <[email protected]>, PPKRAUSS https://github.com/ppKrauss
* @license Licensed under the terms of GPL, LGPL and MPL licenses.
* @version 1.1
* @history v1.0 at 2013-05-09 by onderceylan, v1.1 at 2013-08-27 by ppkrauss.
*/
CKEDITOR.plugins.add('texttransform',
{
// define lang codes for available lang files here
lang: 'en,tr,fr,es,bg',
// plugin initialise
init: function(editor)
{
// set num for switcher loop
var num = 0;
//applies a transformation function to the editor's selected text
var transformSelectedText = function (editor, transformFunc) {
var selection = editor.getSelection();
if (selection.getSelectedText().length > 0) {
var range = selection.getRanges()[0],
walker = new CKEDITOR.dom.walker(range),
node,
nodeText;
//Transform only the selected sections of the first and last nodes,
//but all of the intermediate nodes
while ((node = walker.next())) {
if (node.type == CKEDITOR.NODE_TEXT && node.getText()) {
nodeText = node.$.textContent;
if (node.equals(range.startContainer)) {
nodeText = nodeText.substr(0, range.startOffset) +
transformFunc(nodeText.substr(range.startOffset));
}
else if (node.equals(range.endContainer)) {
nodeText = transformFunc(nodeText.substr(0, range.endOffset)) +
nodeText.substr(range.endOffset);
}
else {
nodeText = transformFunc(nodeText);
}
node.$.textContent = nodeText;
}
}
}
}
// add transformTextSwitch command to be used with button
editor.addCommand('transformTextSwitch',
{
exec: function () {
var selection = editor.getSelection();
var commandArray = ['transformTextToUppercase', 'transformTextToLowercase', 'transformTextCapitalize'];
if (selection.getSelectedText().length > 0) {
selection.lock();
editor.execCommand(commandArray[num]);
selection.unlock(true);
if (num < commandArray.length - 1) {
num++;
} else {
num = 0;
}
}
}
});
// add transformTextToUppercase command to be used with buttons and 'execCommand' method
editor.addCommand('transformTextToUppercase',
{
exec: function () {
transformSelectedText(editor, function (text) {
if (editor.langCode == "tr") {
return text.trToUpperCase();
} else {
return text.toLocaleUpperCase();
}
});
}
});
// add transformTextToUppercase command to be used with buttons and 'execCommand' method
editor.addCommand('transformTextToLowercase',
{
exec: function () {
transformSelectedText(editor, function (text) {
if (editor.langCode == "tr") {
return text.trToLowerCase();
} else {
return text.toLocaleLowerCase();
}
});
}
});
// add transformTextCapitalize command to be used with buttons and 'execCommand' method
editor.addCommand('transformTextCapitalize',
{
exec: function () {
transformSelectedText(editor, function (text) {
return text.replace(/[^\s]\S*/g,
function (word) {
if (editor.langCode == "tr") {
return word.charAt(0).trToUpperCase() +
word.substr(1).trToLowerCase();
} else {
return word.charAt(0).toLocaleUpperCase() +
word.substr(1).toLocaleLowerCase();
}
}
);
});
}
});
// add TransformTextSwitcher button to editor
editor.ui.addButton('TransformTextSwitcher',
{
label: editor.lang.texttransform.transformTextSwitchLabel,
command: 'transformTextSwitch',
icon: this.path + 'images/transformSwitcher.png',
toolbar: 'texttransform'
} );
// add TransformTextToLowercase button to editor
editor.ui.addButton('TransformTextToLowercase',
{
label: editor.lang.texttransform.transformTextToLowercaseLabel,
command: 'transformTextToLowercase',
icon: this.path + 'images/transformToLower.png',
toolbar: 'texttransform'
} );
// add TransformTextToUppercase button to editor
editor.ui.addButton('TransformTextToUppercase',
{
label: editor.lang.texttransform.transformTextToUppercaseLabel,
command: 'transformTextToUppercase',
icon: this.path + 'images/transformToUpper.png',
toolbar: 'texttransform'
} );
// add TransformTextCapitalize button to editor
editor.ui.addButton('TransformTextCapitalize',
{
label: editor.lang.texttransform.transformTextCapitalizeLabel,
command: 'transformTextCapitalize',
icon: this.path + 'images/transformCapitalize.png',
toolbar: 'texttransform'
} );
}
} );