-
Notifications
You must be signed in to change notification settings - Fork 15
/
plugin.js
392 lines (324 loc) · 10.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* @file Configuration helper plugin for CKEditor
* Copyright (C) 2012 Alfonso Martínez de Lizarrondo
*
*/
/* global CKEDITOR */
(function() {
'use strict';
// Check if the browser supports the placeholder attribute on textareas natively.
var supportsPlaceholder = 'placeholder' in document.createElement( 'textarea' );
// If the data is "empty" (BR, P) or the placeholder then return an empty string.
// Otherwise return the original data
function dataIsEmpty( data ) {
if ( !data)
return true;
if ( data.length > 20 )
return false;
var value = data.replace( /[\n|\t]*/g, '' ).toLowerCase();
if ( !value || value == '<br>' || value == '<p> <br></p>' || value == '<p><br></p>' || value == '<div><br></div>' || value == '<p> </p>' || value == ' ' || value == ' ' || value == ' <br>' || value == ' <br>' )
return true;
return false;
}
function addPlaceholder(ev) {
var editor = ev.editor;
// do not add placeholder in readOnly mode
if (editor.readOnly)
return;
var root = editor.editable();
var placeholder = ev.listenerData;
if (!root)
return;
if (editor.mode == 'wysiwyg') {
// If the blur is due to a dialog, don't apply the placeholder
if ( CKEDITOR.dialog._.currentTop )
return;
if ( !root )
return;
if ( dataIsEmpty( root.getHtml() ) ) {
root.setHtml( placeholder );
root.addClass( 'placeholder' );
}
}
if (editor.mode == 'source') {
if ( supportsPlaceholder ) {
if (ev.name == 'mode') {
root.setAttribute( 'placeholder', placeholder );
}
return;
}
if ( dataIsEmpty( root.getValue() ) ) {
root.setValue( placeholder );
root.addClass( 'placeholder' );
}
}
}
function removePlaceholder(ev) {
var editor = ev.editor;
var root = editor.editable();
if (!root)
return;
if (editor.mode == 'wysiwyg' ) {
if (!root.hasClass( 'placeholder' ))
return;
root.removeClass( 'placeholder' );
// fill it properly
if (CKEDITOR.dtd[ root.getName() ]['p']) {
var value = '';
if ( editor.enterMode === CKEDITOR.ENTER_P ) {
value = '<p><br/></p>';
} else if (editor.enterMode === CKEDITOR.ENTER_DIV) {
value = '<div><br/><\/div>';
} else {
// This is for CKEDITOR.ENTER_BR
value = '<br/>';
// FireFox prepends an additional line
if (CKEDITOR.env.gecko || CKEDITOR.env.ie) {
value = ' ';
}
}
root.setHtml(value);
// Set caret in position
var range = new CKEDITOR.dom.range(editor.document);
range.moveToElementEditablePosition(root.getFirst(), true);
editor.getSelection().selectRanges([ range ]);
} else {
root.setHtml(' ');
}
}
if (editor.mode == 'source') {
if ( !root.hasClass( 'placeholder' ) )
return;
root.removeClass( 'placeholder' );
root.setValue( '' );
}
}
function handleReadOnlyChange(ev) {
var editor = ev.editor;
if (editor.readOnly) {
removePlaceholder(ev);
} else {
addPlaceholder(ev);
}
}
function getLang( element ) {
if (!element)
return null;
return element.getAttribute( 'lang' ) || getLang( element.getParent() );
}
CKEDITOR.plugins.add( 'confighelper', {
getPlaceholderCss : function() {
return '.placeholder{ color: #999; }';
},
onLoad : function() {
CKEDITOR.addCss( this.getPlaceholderCss() );
},
init : function( editor ) {
// correct focus status after switch mode
editor.on( 'mode', function( ev ) {
// Let's update to match reality
ev.editor.focusManager.hasFocus = false;
// Now focus it:
});
// Placeholder - Start
// Get the placeholder from the replaced element or from the configuration
var placeholder = editor.element.getAttribute( 'placeholder' ) || editor.config.placeholder;
if (placeholder) {
// CSS for textarea mode
var node = CKEDITOR.document.getHead().append( 'style' );
node.setAttribute( 'type', 'text/css' );
var content = 'textarea.placeholder { color: #999; font-style: italic; }';
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 11)
node.$.styleSheet.cssText = content;
else
node.$.innerHTML = content;
// Watch for the calls to getData to remove the placeholder
editor.on( 'getData', function( ev ) {
var element = editor.editable();
if ( element && element.hasClass( 'placeholder' ) )
ev.data.dataValue = '';
});
// Watch for setData to remove placeholder class
editor.on('setData', function(ev) {
if ( CKEDITOR.dialog._.currentTop )
return;
if ( editor.mode == 'source' && supportsPlaceholder )
return;
var root = editor.editable();
if ( !root )
return;
if ( !dataIsEmpty( ev.data.dataValue ) ) {
// Remove the class if new data is not empty
if ( root.hasClass( 'placeholder' ) )
root.removeClass( 'placeholder' );
} else {
// if data is empty, set it to the placeholder
addPlaceholder(ev);
}
});
editor.on('blur', addPlaceholder, null, placeholder);
editor.on('mode', addPlaceholder, null, placeholder);
editor.on('contentDom', addPlaceholder, null, placeholder);
editor.on('dataReady', addPlaceholder, null, placeholder);
editor.on('focus', removePlaceholder);
editor.on('key', removePlaceholder);
editor.on('beforeModeUnload', removePlaceholder);
editor.on('readOnly', handleReadOnlyChange, null, placeholder);
} // Placeholder - End
// SCAYT lang from element lang:
var lang = editor.config.contentsLanguage || getLang( editor.element );
if ( lang && editor.plugins.scayt && !editor.config.scayt_sLang ) {
try {
// Remove the stored language
if (localStorage)
localStorage.removeItem( 'scayt_0_lang' );
} catch (e) { /* */}
// Convert from HTML5 Lang to spellchecker.net values
var map = {
'en' : 'en_US',
'en-us': 'en_US',
'en-gb': 'en_GB',
'pt-br': 'pt_BR',
'da' : 'da_DK',
'da-dk': 'da_DK',
'nl-nl': 'nl_NL',
'en-ca': 'en_CA',
'fi-fi': 'fi_FI',
'fr' : 'fr_FR',
'fr-fr': 'fr_FR',
'fr-ca': 'fr_CA',
'de' : 'de_DE',
'de-de': 'de_DE',
'el-gr': 'el_GR',
'it' : 'it_IT',
'it-it': 'it_IT',
'nb-no': 'nb_NO',
'pt' : 'pt_PT',
'pt-pt': 'pt_PT',
'es' : 'es_ES',
'es-es': 'es_ES',
'sv-se': 'sv_SE'
};
editor.config.scayt_sLang = map[ lang.toLowerCase() ];
}
// Parse the config to turn it into a js object
// format= dialogName:tabName:fieldName
var parseDefinitionToObject = function( value ) {
// Allow JSON definitions
if (typeof value == 'object')
return value;
var contents = value.split( ';' ),
tabsToProcess = {},
i;
for ( i = 0; i < contents.length; i++ ) {
var parts = contents[ i ].split( ':' );
if ( parts.length == 3 ) {
var dialogName = parts[ 0 ],
tabName = parts[ 1 ],
fieldName = parts[ 2 ];
if ( !tabsToProcess[ dialogName ] )
tabsToProcess[ dialogName ] = {};
if ( !tabsToProcess[ dialogName ][ tabName ] )
tabsToProcess[ dialogName ][ tabName ] = [];
tabsToProcess[ dialogName ][ tabName ].push( fieldName );
}
}
return tabsToProcess;
};
// Customize dialogs:
function customizeDialogs( ev ) {
if ( editor != ev.editor )
return;
var dialogName = ev.data.name,
dialogDefinition = ev.data.definition,
tabsToProcess,
i, name, fields, tab;
if (dialogName == 'tableProperties')
dialogName = 'table';
// Parse the config to turn it into a js object
if ( !( 'removeDialogFields' in editor._ ) && editor.config.removeDialogFields )
editor._.removeDialogFields = parseDefinitionToObject( editor.config.removeDialogFields );
// Remove fields of this dialog.
if ( editor._.removeDialogFields && ( tabsToProcess = editor._.removeDialogFields[ dialogName ] ) ) {
for ( name in tabsToProcess ) {
fields = tabsToProcess[ name ];
tab = dialogDefinition.getContents( name );
if (!tab)
continue;
for ( i = 0; i < fields.length ; i++ )
tab.remove( fields[ i ] );
}
}
if ( !( 'hideDialogFields' in editor._ ) && editor.config.hideDialogFields )
editor._.hideDialogFields = parseDefinitionToObject( editor.config.hideDialogFields );
// Remove fields of this dialog.
if ( editor._.hideDialogFields && ( tabsToProcess = editor._.hideDialogFields[ dialogName ] ) ) {
for ( name in tabsToProcess ) {
fields = tabsToProcess[ name ];
tab = dialogDefinition.getContents( name );
if (!tab)
continue;
for ( i = 0; i < fields.length ; i++ )
tab.get( fields[ i ] ).hidden = true;
}
}
// Set default values.
if ( editor.config.dialogFieldsDefaultValues && ( tabsToProcess = editor.config.dialogFieldsDefaultValues[ dialogName ] ) ) {
for ( name in tabsToProcess ) {
fields = tabsToProcess[ name ];
tab = dialogDefinition.getContents( name );
if (!tab)
continue;
for ( var fieldName in fields ) {
var dialogField = tab.get( fieldName );
if ( dialogField )
dialogField[ 'default' ] = fields[ fieldName ];
}
}
}
}
CKEDITOR.on( 'dialogDefinition', customizeDialogs );
editor.once( 'beforeDestroy', function() {
CKEDITOR.removeListener( 'dialogDefinition', customizeDialogs );
});
}
} );
})();
/**
* Allows to define which dialog fiels must be removed
* @name CKEDITOR.config.removeDialogFields
* @type {String}
* @example
* editor.config.removeDialogFields = "image:info:txtBorder;image:info:txtHSpace";
*/
/**
* Allows to define which dialog fiels must be hidden
* @name CKEDITOR.config.hideDialogFields
* @type {String}
* @example
* editor.config.hideDialogFields = "image:info:htmlPreview";
*/
/**
* Allows to define default values for dialog fields
* @name CKEDITOR.config.dialogFieldsDefaultValues
* @type {Object}
* @example
config.dialogFieldsDefaultValues =
{
image:
{
advanced:
{
txtGenClass : 'myClass',
txtGenTitle : 'Image title'
}
}
};
*/
/**
* Placeholder text for empty editor
* @name CKEDITOR.config.placeholder
* @type {String}
* @example
* editor.config.placeholder = "Please, type here...";
*/