This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
plugin.js
579 lines (453 loc) · 15.6 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
* Floating-Tools
* Author: Philipp Stracker (2013)
* Project page: http://stracker-phil.github.com/Floating-Tools/
*/
(function() {
var floatingtools = function() {
this.dom = null; // the main toolbar-object
this.toolbars = [];
this.is_visible = false;
this.hide_on_blur = true; //!TODO: Make this an configuration option
this.toolbarsize = false;
this.editoroffset = false;
this.mousepos = {x:0, y:0};
};
CKEDITOR.plugins.add( 'floating-tools', {
requires: 'toolbar',
init: function( editor ) {
/**
* Create the UI elements required by this plugin
* UI is the floating toolbar
* Many parts of this function are taken from the toolbar plugin
*/
editor.on( 'uiSpace', function( event ) {
// Create toolbar only once...
event.removeListener();
editor.floatingtools = new floatingtools();
var labelId = CKEDITOR.tools.getNextId();
var output = [
// Did not find a nicer way to include the CSS required for the toolbar...
'<style>',
'.pos-relative {position:relative}',
'.cke_floatingtools{',
'position:absolute;',
'left:-50000px;',
'top:0;',
'padding: 5px 0 0 6px;',
'border:1px solid #b1b1b1;',
'border-radius:3px;',
'box-shadow: 0 1px 10px rgba(0,0,0,0.3);',
'transition:opacity .1s;-o-transition:opacity .1s;-moz-transition:opacity .1s;-webkit-transition:opacity .1s;',
'}',
'</style>',
'<span id="', labelId, '" class="cke_voice_label">', editor.lang.toolbar.toolbars, '</span>',
'<span id="' + editor.ui.spaceId( 'floatingtools' ) + '" class="cke_floatingtools cke_top" role="group" aria-labelledby="', labelId, '" onmousedown="return false;">' ];
var groupStarted, pendingSeparator;
var toolbars = editor.floatingtools.toolbars,
toolbar = getFloatingToolbarConfig( editor );
// Build the toolbar
for ( var r = 0; r < toolbar.length; r++ ) {
var toolbarId,
toolbarObj = 0,
toolbarName,
row = toolbar[ r ],
items;
// It's better to check if the row object is really
// available because it's a common mistake to leave
// an extra comma in the toolbar definition
// settings, which leads on the editor not loading
// at all in IE. (#3983)
if ( !row )
continue;
if ( groupStarted ) {
output.push( '</span>' );
groupStarted = 0;
pendingSeparator = 0;
}
if ( row === '/' ) {
output.push( '<span class="cke_toolbar_break"></span>' );
continue;
}
items = row.items || row;
// Create all items defined for this toolbar.
for ( var i = 0; i < items.length; i++ ) {
var item = items[ i ],
canGroup;
if ( item ) {
if ( item.type == CKEDITOR.UI_SEPARATOR ) {
// Do not add the separator immediately. Just save
// it be included if we already have something in
// the toolbar and if a new item is to be added (later).
pendingSeparator = groupStarted && item;
continue;
}
canGroup = item.canGroup !== false;
// Initialize the toolbar first, if needed.
if ( !toolbarObj ) {
// Create the basic toolbar object.
toolbarId = CKEDITOR.tools.getNextId();
toolbarObj = { id: toolbarId, items: [] };
toolbarName = row.name && ( editor.lang.toolbar.toolbarGroups[ row.name ] || row.name );
// Output the toolbar opener.
output.push( '<span id="', toolbarId, '" class="cke_toolbar"', ( toolbarName ? ' aria-labelledby="' + toolbarId + '_label"' : '' ), ' role="toolbar">' );
// If a toolbar name is available, send the voice label.
toolbarName && output.push( '<span id="', toolbarId, '_label" class="cke_voice_label">', toolbarName, '</span>' );
output.push( '<span class="cke_toolbar_start"></span>' );
// Add the toolbar to the "editor.toolbox.toolbars"
// array.
var index = toolbars.push( toolbarObj ) - 1;
// Create the next/previous reference.
if ( index > 0 ) {
toolbarObj.previous = toolbars[ index - 1 ];
toolbarObj.previous.next = toolbarObj;
}
}
if ( canGroup ) {
if ( !groupStarted ) {
output.push( '<span class="cke_toolgroup" role="presentation">' );
groupStarted = 1;
}
} else if ( groupStarted ) {
output.push( '</span>' );
groupStarted = 0;
}
function addItem( item ) {
var itemObj = item.render( editor, output );
index = toolbarObj.items.push( itemObj ) - 1;
if ( index > 0 ) {
itemObj.previous = toolbarObj.items[ index - 1 ];
itemObj.previous.next = itemObj;
}
itemObj.toolbar = toolbarObj;
// No need for keyboard handlers, the toolbar is only accessibly by mouse
/*
itemObj.onkey = itemKeystroke;
// Fix for #3052:
// Prevent JAWS from focusing the toolbar after document load.
itemObj.onfocus = function() {
if ( !editor.toolbox.focusCommandExecuted )
editor.focus();
};
*/
}
if ( pendingSeparator ) {
addItem( pendingSeparator );
pendingSeparator = 0;
}
addItem( item );
}
}
if ( groupStarted ) {
output.push( '</span>' );
groupStarted = 0;
pendingSeparator = 0;
}
if ( toolbarObj )
output.push( '<span class="cke_toolbar_end"></span></span>' );
}
output.push( '</span>' );
event.data.html += output.join( '' );
});
/**
* Do the magic: Attach eventhandlers to see if text is selected
* When text is selected then show the floating toolbar, else hide it
*/
editor.on('contentDom', function( event ) {
var editable = editor.editable();
unfocus_toolbar();
/**
* Attach an eventhandler to the mouse-up event
*/
editable.attachListener( editor.document, 'mouseup', function( mouse_event ) {
// When user right-clicks, ctrl-clicks, etc. then do not show the toolbar
data = mouse_event.data.$;
if (data.button !== 0 || data.ctrlKey || data.altKey || data.shiftKey) return true;
// When the user clears the selection by single-clicking in the editor then this event is fired before the selection is removed
// So we add a short delay to give the browser a chance to remove the selection before we do anything
setTimeout( function() {
if (is_text_selected(editor)) {
// Save the current mouse-position
set_mousepos (mouse_event.data.$);
// when there is text selected after mouse-up: show the toolbar
editor.execCommand('showFloatingTools');
} else {
// when no text is selected then hide the toolbar
editor.execCommand('hideFloatingTools');
}
}, 100);
});
/**
* On keypress we will always hide the toolbar
* The toolbar is only accessible via mouse
*/
editable.attachListener( editor.document, 'keyup', function( key_event ) {
editor.execCommand('hideFloatingTools');
});
/**
* On blur hide the toolbar (editor looses focus)
*/
editor.on('blur', function( e ) {
if (editor.floatingtools.hide_on_blur) {
hide_toolbar(editor);
}
});
/**
* Attach the mouse-over event to the toolbar.
* When cursor is above the toolbar then set opacity to 1
*/
toolbar = get_element(editor);
toolbar.on('mouseover', function( mouse_event ) {
focus_toolbar();
});
/**
* When the mouse moves out of the toolbar then make it transparent again
*/
toolbar.on('mouseout', function( mouse_event ) {
unfocus_toolbar();
})
editor.container.addClass('pos-relative');
});
/**
* Display the floating toolbar
*/
editor.addCommand( 'showFloatingTools', {
exec : function( editor ) {
if (is_text_selected(editor)) {
toolbar = get_element();
unfocus_toolbar();
toolbar.show();
// Get the size of the toolbar
size = get_toolbar_size()
// Get the offset of the editor
offset = get_editor_offset();
// Get the mouse position
pos = get_mousepos();
// Calculate the position for the toolbar
toolpos = calculate_position(pos, size, offset);
toolbar.setStyles({
'left' : toolpos.x + 'px',
'top' : toolpos.y + 'px'
});
editor.floatingtools.is_visible = true;
}
}
});
/**
* Hide the floating toolbar
*/
editor.addCommand( 'hideFloatingTools', {
exec : function( editor ) {
hide_toolbar(editor);
}
});
/**
* ===== Behind the scenes. Getters, setters, calculation, etc.
*/
hide_toolbar = function() {
if (false != editor.floatingtools.is_visible) {
toolbar = get_element(editor);
toolbar.hide();
editor.floatingtools.is_visible = false;
}
}
/**
* Store the current mouse-position, so we can position the toolbar near the cursor
*/
set_mousepos = function(data) {
editor.floatingtools.mousepos = {
left: data.clientX,
top: data.clientY
};
}
/**
* Store the current mouse-position, so we can position the toolbar near the cursor
*/
get_mousepos = function() {
return editor.floatingtools.mousepos;
}
/**
* Returns the main toolbar-object (the parent of all items in the floating-toolbar)
*/
get_element = function(editor) {
if (! editor.floatingtools.dom) {
var dom_id = editor.ui.spaceId( 'floatingtools' );
editor.floatingtools.dom = CKEDITOR.document.getById( dom_id );
}
return editor.floatingtools.dom;
}
/**
* Returns the offset of the editor area (effectively the height of the top-toolbar)
*/
get_editor_offset = function() {
if (! editor.floatingtools.editoroffset) {
var editor_id = editor.ui.spaceId( 'contents' );
var obj = CKEDITOR.document.getById( editor_id );
editor.floatingtools.editoroffset = {
left: obj.$.offsetLeft,
top: obj.$.offsetTop,
width: obj.$.offsetWidth,
height: obj.$.offsetHeight
};
}
return editor.floatingtools.editoroffset;
}
/**
* Calculates the position for the toolbar
*/
calculate_position = function(pos, toolbar_size, offset) {
toolpos = {
x: pos.left + offset.left - (toolbar_size.width/2),
y: pos.top + offset.top - (toolbar_size.height + 20)
}
// make sure toolbar does not extend out of the left CKEditor border
if (toolpos.x < offset.left + 2) toolpos.x = offset.left + 2;
// make sure toolbar does not extend out of the right CKEditor border
if (pos.left + (toolbar_size.width/2) >= offset.left + offset.width-2 )
toolpos.x = offset.left + offset.width - toolbar_size.width - 2;
// Make sure toolbar does no go into the top toolbar area
if (toolpos.y < offset.top) toolpos.y = offset.top;
// make sure toolbar does not cover the mouse-cursor when text in the top line is selected
if (offset.top+pos.top > toolpos.y
&& offset.top+pos.top < toolpos.y+toolbar_size.height)
toolpos.y = offset.top + pos.top + 24; // display toolbar below the cursor
return toolpos;
}
/**
* Returns the size of the floating toolbar
*/
get_toolbar_size = function() {
if (! editor.floatingtools.toolbarsize) {
var obj = get_element();
editor.floatingtools.toolbarsize = {
width: obj.$.offsetWidth,
height: obj.$.offsetHeight
};
}
return editor.floatingtools.toolbarsize;
}
/**
* Check if text is selected.
* Retrns true when there is at least 1 character selected in the editor
*/
is_text_selected = function (editor) {
var text = editor.getSelection().getSelectedText();
return text != '';
}
/**
* Make the toolbar opaque
*/
focus_toolbar = function() {
obj = get_element(editor);
obj.setOpacity(1);
}
/**
* Make the toolbar transparent
*/
unfocus_toolbar = function() {
obj = get_element(editor);
obj.setOpacity(0.25);
},
/**
* Get the plugin configuration.
* Kidnapped from the toolbar-plugin...
*/
getFloatingToolbarConfig = function( editor ) {
var removeButtons = editor.config.removeButtons;
removeButtons = removeButtons && removeButtons.split( ',' );
function buildToolbarConfig() {
// Take the base for the new toolbar, which is basically a toolbar
// definition without items.
var toolbar = getPrivateFloatingToolbarGroups( editor );
return populateToolbarConfig( toolbar );
}
// Returns an object containing all toolbar groups used by ui items.
function getItemDefinedGroups() {
var groups = {},
itemName, item, itemToolbar, group, order;
for ( itemName in editor.ui.items ) {
item = editor.ui.items[ itemName ];
itemToolbar = item.toolbar || 'others';
if ( itemToolbar ) {
// Break the toolbar property into its parts: "group_name[,order]".
itemToolbar = itemToolbar.split( ',' );
group = itemToolbar[ 0 ];
order = parseInt( itemToolbar[ 1 ] || -1, 10 );
// Initialize the group, if necessary.
groups[ group ] || ( groups[ group ] = [] );
// Push the data used to build the toolbar later.
groups[ group ].push( { name: itemName, order: order} );
}
}
// Put the items in the right order.
for ( group in groups ) {
groups[ group ] = groups[ group ].sort( function( a, b ) {
return a.order == b.order ? 0 :
b.order < 0 ? -1 :
a.order < 0 ? 1 :
a.order < b.order ? -1 :
1;
});
}
return groups;
}
function fillGroup( toolbarGroup, uiItems ) {
if ( uiItems.length ) {
if ( toolbarGroup.items )
toolbarGroup.items.push( editor.ui.create( '-' ) );
else
toolbarGroup.items = [];
var item, name;
while ( ( item = uiItems.shift() ) ) {
name = typeof item == 'string' ? item : item.name;
// Ignore items that are configured to be removed.
if ( !removeButtons || CKEDITOR.tools.indexOf( removeButtons, name ) == -1 ) {
item = editor.ui.create( name );
if ( !item )
continue;
if ( !editor.addFeature( item ) )
continue;
toolbarGroup.items.push( item );
}
}
}
}
function populateToolbarConfig( config ) {
var toolbar = [],
i, group, newGroup;
for ( i = 0; i < config.length; ++i ) {
group = config[ i ];
newGroup = {};
if ( group == '/' )
toolbar.push( group );
else if ( CKEDITOR.tools.isArray( group) ) {
fillGroup( newGroup, CKEDITOR.tools.clone( group ) );
toolbar.push( newGroup );
}
else if ( group.items ) {
fillGroup( newGroup, CKEDITOR.tools.clone( group.items ) );
newGroup.name = group.name;
toolbar.push( newGroup);
}
}
return toolbar;
}
var toolbar = editor.config.floatingtools;
// If it is a string, return the relative "toolbar_name" config.
if ( typeof toolbar == 'string' )
toolbar = editor.config[ 'floatingtools_' + toolbar ];
return ( editor.toolbar = toolbar ? populateToolbarConfig( toolbar ) : buildToolbarConfig() );
},
/**
* Return the default toolbar configuration.
*/
getPrivateFloatingToolbarGroups = function( editor ) {
return editor._.floatingToolsGroups || ( editor._.floatingToolsGroups = [
{ name: 'styles', items: [ 'Font','FontSize' ]},
{ name: 'format', items: [ 'Bold','Italic' ]},
{ name: 'paragraph', items: [ 'JustifyCenter','Outdent','Indent','NumberedList','BulletedList' ]}
]);
}
}
} );
})();