plugin.js
5.54 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
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
/*
Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* @file Plugin for inserting Drupal teaser and page breaks.
*/
( function() {
var pluginRequires = [ 'fakeobjects' ];
if (Drupal.ckeditor_ver == 3) {
pluginRequires = [ 'fakeobjects', 'htmldataprocessor' ];
}
CKEDITOR.plugins.add( 'drupalbreaks',
{
requires : pluginRequires,
init : function( editor )
{
var addCssObj = CKEDITOR;
if (Drupal.ckeditor_ver == 3) {
addCssObj = editor;
}
// Add the styles that renders our fake objects.
addCssObj.addCss(
'img.cke_drupal_pagebreak,img.cke_drupal_break' +
'{' +
'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/pagebreak.gif' ) + ');' +
'background-position: center center;' +
'background-repeat: no-repeat;' +
'clear: both;' +
'display: block;' +
'float: none;' +
'width: 100%;' +
'border-top: #999999 1px dotted;' +
'border-bottom: #999999 1px dotted;' +
'height: 5px;' +
'}' +
'img.cke_drupal_break' +
'{' +
'border-top: #FF0000 1px dotted;' +
'border-bottom: #FF0000 1px dotted;' +
'}'
);
// Register the toolbar buttons.
editor.ui.addButton( 'DrupalBreak',
{
label : Drupal.t('Insert Teaser Break'),
icon : this.path + 'images/drupalbreak.png',
command : 'drupalbreak'
});
editor.addCommand( 'drupalbreak',
{
exec : function()
{
// There should be only one <!--break--> in document. So, look
// for an image with class "cke_drupal_break" (the fake element).
var images = editor.document.getElementsByTag( 'img' );
for ( var i = 0, len = images.count() ; i < len ; i++ )
{
var img = images.getItem( i );
if ( img.hasClass( 'cke_drupal_break' ) )
{
if ( confirm( Drupal.t( 'The document already contains a teaser break. Do you want to proceed by removing it first?' ) ) )
{
img.remove();
break;
}
else
return;
}
}
insertComment( 'break' );
}
} );
editor.ui.addButton( 'DrupalPageBreak',
{
label : Drupal.t( 'Insert Page Break' ),
icon : this.path + 'images/drupalpagebreak.png',
command : 'drupalpagebreak'
});
editor.addCommand( 'drupalpagebreak',
{
exec : function()
{
insertComment( 'pagebreak' );
}
} );
// This function effectively inserts the comment into the editor.
function insertComment( text )
{
// Create the fake element that will be inserted into the document.
// The trick is declaring it as an <hr>, so it will behave like a
// block element (and in effect it behaves much like an <hr>).
if ( !CKEDITOR.dom.comment.prototype.getAttribute ) {
CKEDITOR.dom.comment.prototype.getAttribute = function() {
return '';
};
CKEDITOR.dom.comment.prototype.attributes = {
align : ''
};
}
var fakeElement = editor.createFakeElement( new CKEDITOR.dom.comment( text ), 'cke_drupal_' + text, 'hr' );
// This is the trick part. We can't use editor.insertElement()
// because we need to put the comment directly at <body> level.
// We need to do range manipulation for that.
// Get a DOM range from the current selection.
var range = editor.getSelection().getRanges()[0],
elementsPath = new CKEDITOR.dom.elementPath( range.getCommonAncestor( true ) ),
element = ( elementsPath.block && elementsPath.block.getParent() ) || elementsPath.blockLimit,
hasMoved;
// If we're not in <body> go moving the position to after the
// elements until reaching it. This may happen when inside tables,
// lists, blockquotes, etc.
while ( element && element.getName() != 'body' )
{
range.moveToPosition( element, CKEDITOR.POSITION_AFTER_END );
hasMoved = 1;
element = element.getParent();
}
// Split the current block.
if ( !hasMoved )
range.splitBlock( 'p' );
// Insert the fake element into the document.
range.insertNode( fakeElement );
// Now, we move the selection to the best possible place following
// our fake element.
var next = fakeElement;
while ( ( next = next.getNext() ) && !range.moveToElementEditStart( next ) )
{}
range.select();
}
},
afterInit : function( editor )
{
// Adds the comment processing rules to the data filter, so comments
// are replaced by fake elements.
editor.dataProcessor.dataFilter.addRules(
{
comment : function( value )
{
if ( !CKEDITOR.htmlParser.comment.prototype.getAttribute ) {
CKEDITOR.htmlParser.comment.prototype.getAttribute = function() {
return '';
};
CKEDITOR.htmlParser.comment.prototype.attributes = {
align : ''
};
}
if ( value == 'break' || value == 'pagebreak' )
return editor.createFakeParserElement( new CKEDITOR.htmlParser.comment( value ), 'cke_drupal_' + value, 'hr' );
return value;
}
});
}
});
} )();