root / drupal7 / sites / all / modules / media_ckeditor / js / plugins / media / plugin.js @ 29771811
1 |
/*
|
---|---|
2 |
Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
|
3 |
For licensing, see LICENSE.html or http://ckeditor.com/license
|
4 |
*/
|
5 |
|
6 |
/**
|
7 |
* @file Plugin for inserting images from Drupal media module
|
8 |
*
|
9 |
* @TODO Remove all the legecy media wrapper once it's sure nobody uses that
|
10 |
* anymore.
|
11 |
*/
|
12 |
( function() {
|
13 |
var mediaPluginDefinition = {
|
14 |
icons: 'media', |
15 |
requires: ['button'], |
16 |
// All the default distributions of the editor have the widget plugin
|
17 |
// disabled by default.
|
18 |
hasWidgetSupport: false, |
19 |
mediaLegacyWrappers: false, |
20 |
hidpi: true, |
21 |
onLoad: function() { |
22 |
// Check if this instance has widget support.
|
23 |
// mediaPluginDefinition.hasWidgetSupport = typeof(CKEDITOR.plugins.registered.widget) != 'undefined';
|
24 |
// Add dependency to widget plugin if possible.
|
25 |
if (Drupal.settings.ckeditor.plugins['media'].compareVersions(CKEDITOR.version, '4.3') >= 0 && mediaPluginDefinition.hasWidgetSupport) { |
26 |
mediaPluginDefinition.requires.push('widget');
|
27 |
} |
28 |
}, |
29 |
|
30 |
// Wrap Drupal plugin in a proxy plugin.
|
31 |
init: function(editor){ |
32 |
editor.addCommand( 'media',
|
33 |
{ |
34 |
exec: function (editor) { |
35 |
var data = {
|
36 |
format: 'html', |
37 |
node: null, |
38 |
content: '' |
39 |
}; |
40 |
var selection = editor.getSelection();
|
41 |
if (selection) {
|
42 |
data.node = selection.getSelectedElement(); |
43 |
|
44 |
if (data.node) {
|
45 |
data.node = data.node.$;
|
46 |
} |
47 |
if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
|
48 |
if (CKEDITOR.env.ie && CKEDITOR.env.version < 10) { |
49 |
data.content = selection.getNative().createRange().text; |
50 |
} |
51 |
else {
|
52 |
data.content = selection.getNative().toString(); |
53 |
} |
54 |
} |
55 |
else if (data.node) { |
56 |
// content is supposed to contain the "outerHTML".
|
57 |
data.content = data.node.parentNode.innerHTML; |
58 |
} |
59 |
} |
60 |
|
61 |
// If we are inside another media element, we need to move the cursor to the end of the element
|
62 |
var selected_element = selection.getStartElement();
|
63 |
var parent = selected_element.getParent();
|
64 |
//if we are inside "a" tag and inside "span" (with class media-element)
|
65 |
if (selected_element.is('a') && parent.is('span') && parent.hasClass('media-element')) { |
66 |
selection.selectElement(parent); |
67 |
selected_ranges = selection.getRanges(); |
68 |
selected_ranges[0].collapse(false); |
69 |
selection.selectRanges(selected_ranges); |
70 |
} |
71 |
//if we are outside "a" tag but inside "span" (with class media-element)
|
72 |
else if (selected_element.is('span') && selected_element.hasClass('media-element')) { |
73 |
selection.selectElement(selected_element); |
74 |
selected_ranges = selection.getRanges(); |
75 |
selected_ranges[0].collapse(false); |
76 |
selection.selectRanges(selected_ranges); |
77 |
} |
78 |
Drupal.settings.ckeditor.plugins['media'].invoke(data, Drupal.settings.ckeditor.plugins['media'], editor.name); |
79 |
} |
80 |
}); |
81 |
|
82 |
// Add a Ckeditor context menu item for editing already-inserted media.
|
83 |
if (editor.contextMenu) {
|
84 |
editor.addCommand('mediaConfigure', {
|
85 |
exec: function (editor) { |
86 |
editor.execCommand('media');
|
87 |
}, |
88 |
}); |
89 |
|
90 |
editor.addMenuGroup('mediaGroup');
|
91 |
editor.addMenuItem('mediaConfigureItem', {
|
92 |
label: Drupal.settings.media_ckeditor.labels['settings'], |
93 |
icon: this.path + 'icons/media.png', |
94 |
command: 'mediaConfigure', |
95 |
group: 'mediaGroup' |
96 |
}); |
97 |
|
98 |
editor.contextMenu.addListener(function(element) {
|
99 |
if (element.getAttribute('data-media-element') || |
100 |
element.find('[data-media-element]').count()) {
|
101 |
return { mediaConfigureItem: CKEDITOR.TRISTATE_OFF }; |
102 |
}; |
103 |
}); |
104 |
} |
105 |
|
106 |
// Add the toolbar button.
|
107 |
editor.ui.addButton( 'Media',
|
108 |
{ |
109 |
label: Drupal.settings.media_ckeditor.labels['add'], |
110 |
command: 'media' |
111 |
}); |
112 |
|
113 |
// Because the media comment wrapper don't work well for CKEditor we
|
114 |
// replace them by using a custom mediawrapper element.
|
115 |
// Instead having
|
116 |
// <!--MEDIA-WRAPPER-START-1--><img /><!--MEDIA-WRAPPER-END-1-->
|
117 |
// We wrap the placeholder with
|
118 |
// <mediawrapper data="1"><img /></mediawrapper>
|
119 |
// That way we can deal better with selections - see selectionChange.
|
120 |
CKEDITOR.dtd['mediawrapper'] = CKEDITOR.dtd;
|
121 |
CKEDITOR.dtd.$blockLimit['mediawrapper'] = 1; |
122 |
CKEDITOR.dtd.$inline['mediawrapper'] = 1; |
123 |
CKEDITOR.dtd.$nonEditable['mediawrapper'] = 1; |
124 |
if (Drupal.settings.ckeditor.plugins['media'].compareVersions(CKEDITOR.version, '4.1') >= 0) { |
125 |
// Register allowed tag for advanced filtering.
|
126 |
editor.filter.allow( 'mediawrapper[!data]', 'mediawrapper', true); |
127 |
// Don't remove the data-file_info attribute added by media!
|
128 |
editor.filter.allow( '*[!data-file_info]', 'mediawrapper', true); |
129 |
// Ensure image tags accept all kinds of attributes.
|
130 |
editor.filter.allow( 'img[*]{*}(*)', 'mediawrapper', true); |
131 |
// Objects should be selected as a whole in the editor.
|
132 |
CKEDITOR.dtd.$object['mediawrapper'] = 1; |
133 |
} |
134 |
function prepareDataForWysiwygMode(data) { |
135 |
if (typeof Drupal.media !== 'undefined') { |
136 |
data = Drupal.media.filter.replaceTokenWithPlaceholder(data); |
137 |
} |
138 |
// Legacy media wrapper.
|
139 |
mediaPluginDefinition.mediaLegacyWrappers = (data.indexOf("<!--MEDIA-WRAPPER-START-") !== -1); |
140 |
if (mediaPluginDefinition.mediaLegacyWrappers) {
|
141 |
data = data.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '<mediawrapper data="$1">$2</mediawrapper>'); |
142 |
} |
143 |
return data;
|
144 |
} |
145 |
function prepareDataForSourceMode(data) { |
146 |
var replacement = '$2'; |
147 |
// Legacy wrapper
|
148 |
if (mediaPluginDefinition.mediaLegacyWrappers) {
|
149 |
replacement = '<!--MEDIA-WRAPPER-START-$1-->$2<!--MEDIA-WRAPPER-END-$1-->';
|
150 |
} |
151 |
data = data.replace(/<mediawrapper data="(.*?)">(.*?)<\/mediawrapper>/gi, replacement);
|
152 |
if (typeof Drupal.media !== 'undefined') { |
153 |
data = Drupal.media.filter.replacePlaceholderWithToken(data); |
154 |
} |
155 |
return data;
|
156 |
} |
157 |
|
158 |
// Ensure the tokens are replaced by placeholders while editing.
|
159 |
// Check for widget support.
|
160 |
if (mediaPluginDefinition.hasWidgetSupport) {
|
161 |
editor.widgets.add( 'mediabox',
|
162 |
{ |
163 |
button: 'Create a mediabox', |
164 |
editables: {},
|
165 |
allowedContent: '*', |
166 |
upcast: function( element ) { |
167 |
// Ensure media tokens are converted to media placeholders.
|
168 |
html = Drupal.media.filter.replaceTokenWithPlaceholder(element.getHtml()); |
169 |
// Only replace html if it's different
|
170 |
if (html != element.getHtml()) {
|
171 |
element.setHtml(html); |
172 |
// CKEditor's setHtml() method automatically fixes the HTML that
|
173 |
// it receives, which means that if it gets an <li> without a
|
174 |
// <ul> or <ol> parent, it adds a <ul>. These extra <ul> tags just
|
175 |
// keep piling up every time this function runs. So check here to
|
176 |
// see if we may need to fix this.
|
177 |
if (element.children && element.children[0]) { |
178 |
// We identify this by looking for a <ul> inside a <ul> or <ol>.
|
179 |
if (('ul' === element.name && 'ul' === element.children[0].name) || |
180 |
('ol' === element.name && 'ul' === element.children[0].name)) { |
181 |
// If this did happen, fix it by promoting the grandchildren
|
182 |
// (ie, actual list items) to children.
|
183 |
element.children = element.children[0].children;
|
184 |
} |
185 |
} |
186 |
} |
187 |
return element.name == 'mediawrapper' || 'data-media-element' in element.attributes; |
188 |
}, |
189 |
|
190 |
downcast: function( widgetElement ) { |
191 |
var token = Drupal.media.filter.replacePlaceholderWithToken(widgetElement.getOuterHtml());
|
192 |
if (token) {
|
193 |
return new CKEDITOR.htmlParser.text(token); |
194 |
} |
195 |
return false; |
196 |
}, |
197 |
|
198 |
init: function() { |
199 |
// Add double-click functionality to the widget.
|
200 |
this.on('doubleclick', function(evt) { |
201 |
editor.execCommand('media');
|
202 |
}, null, null, 5 ); |
203 |
} |
204 |
}); |
205 |
} |
206 |
else if (Drupal.settings.ckeditor.plugins['media'].compareVersions(CKEDITOR.version, '4') >= 0) { |
207 |
// CKEditor >=4.0
|
208 |
editor.on('setData', function( event ) { |
209 |
event.data.dataValue = prepareDataForWysiwygMode(event.data.dataValue); |
210 |
}); |
211 |
} |
212 |
else {
|
213 |
// CKEditor >=3.6 behaviour.
|
214 |
editor.on( 'beforeSetMode', function( event, data ) { |
215 |
event.removeListener(); |
216 |
var wysiwyg = editor._.modes[ 'wysiwyg' ]; |
217 |
var source = editor._.modes[ 'source' ]; |
218 |
wysiwyg.loadData = CKEDITOR.tools.override( wysiwyg.loadData, function( org )
|
219 |
{ |
220 |
return function( data ) { |
221 |
return ( org.call( this, prepareDataForWysiwygMode(data)) ); |
222 |
}; |
223 |
} ); |
224 |
source.loadData = CKEDITOR.tools.override( source.loadData, function( org )
|
225 |
{ |
226 |
return function( data ) { |
227 |
return ( org.call( this, prepareDataForSourceMode(data) ) ); |
228 |
}; |
229 |
} ); |
230 |
}); |
231 |
} |
232 |
|
233 |
// Provide alternative to the widget functionality introduced in 4.3.
|
234 |
if (!mediaPluginDefinition.hasWidgetSupport) {
|
235 |
// Ensure tokens instead the html element is saved.
|
236 |
editor.on('getData', function( event ) { |
237 |
event.data.dataValue = prepareDataForSourceMode(event.data.dataValue); |
238 |
}); |
239 |
|
240 |
// Ensure our enclosing wrappers are always included in the selection.
|
241 |
editor.on('selectionChange', function( event ) { |
242 |
var ranges = editor.getSelection().getRanges().createIterator();
|
243 |
var newRanges = [];
|
244 |
var currRange;
|
245 |
while(currRange = ranges.getNextRange()) {
|
246 |
var commonAncestor = currRange.getCommonAncestor(false); |
247 |
if (commonAncestor && typeof(commonAncestor.getName) != 'undefined' && commonAncestor.getName() == 'mediawrapper') { |
248 |
var range = new CKEDITOR.dom.range( editor.document ); |
249 |
if (currRange.collapsed === true) { |
250 |
// Don't allow selection within the wrapper element.
|
251 |
if (currRange.startOffset == 0) { |
252 |
// While v3 plays nice with setting start and end to avoid
|
253 |
// editing within the media wrapper element, v4 ignores that.
|
254 |
// Thus we try to move the cursor further away.
|
255 |
if (Drupal.settings.ckeditor.plugins['media'].compareVersions(CKEDITOR.version, '3') > 0) { |
256 |
range.setStart(commonAncestor.getPrevious()); |
257 |
range.setEnd(commonAncestor.getPrevious()); |
258 |
} |
259 |
else {
|
260 |
range.setStartBefore(commonAncestor); |
261 |
} |
262 |
} |
263 |
else {
|
264 |
// While v3 plays nice with setting start and end to avoid
|
265 |
// editing within the media wrapper element, v4 ignores that.
|
266 |
// Thus we try to move the cursor further away.
|
267 |
if (Drupal.settings.ckeditor.plugins['media'].compareVersions(CKEDITOR.version, '3') > 0) { |
268 |
range.setStart(commonAncestor.getNext(), 1);
|
269 |
range.setEnd(commonAncestor.getNext(), 1);
|
270 |
} |
271 |
else {
|
272 |
range.setStartAfter(commonAncestor); |
273 |
} |
274 |
} |
275 |
} |
276 |
else {
|
277 |
// Always select the whole wrapper element.
|
278 |
range.setStartBefore(commonAncestor); |
279 |
range.setEndAfter(commonAncestor); |
280 |
} |
281 |
newRanges.push(range); |
282 |
} |
283 |
} |
284 |
if (newRanges.length) {
|
285 |
editor.getSelection().selectRanges(newRanges); |
286 |
} |
287 |
}); |
288 |
} |
289 |
} |
290 |
}; |
291 |
|
292 |
CKEDITOR.plugins.add( 'media', mediaPluginDefinition);
|
293 |
} )(); |