root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / wysiwyg_plugins / media_ckeditor / plugin.js @ ca0757b9
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 |
// Check if this instance has widget support. All the default distributions
|
17 |
// of the editor have the widget plugin disabled by default.
|
18 |
hasWidgetSupport: typeof(CKEDITOR.plugins.registered.widget) != 'undefined', |
19 |
mediaLegacyWrappers: false, |
20 |
|
21 |
// Wrap Drupal plugin in a proxy plugin.
|
22 |
init: function(editor){ |
23 |
editor.addCommand( 'media',
|
24 |
{ |
25 |
exec: function (editor) { |
26 |
var data = {
|
27 |
format: 'html', |
28 |
node: null, |
29 |
content: '' |
30 |
}; |
31 |
var selection = editor.getSelection();
|
32 |
|
33 |
if (selection) {
|
34 |
data.node = selection.getSelectedElement(); |
35 |
if (data.node) {
|
36 |
data.node = data.node.$;
|
37 |
} |
38 |
if (selection.getType() == CKEDITOR.SELECTION_TEXT) {
|
39 |
if (CKEDITOR.env.ie && CKEDITOR.env.version < 10) { |
40 |
data.content = selection.getNative().createRange().text; |
41 |
} |
42 |
else {
|
43 |
data.content = selection.getNative().toString(); |
44 |
} |
45 |
} |
46 |
else if (data.node) { |
47 |
// content is supposed to contain the "outerHTML".
|
48 |
data.content = data.node.parentNode.innerHTML; |
49 |
} |
50 |
} |
51 |
Drupal.settings.ckeditor.plugins['media'].invoke(data, Drupal.settings.ckeditor.plugins['media'], editor.name); |
52 |
} |
53 |
}); |
54 |
|
55 |
editor.ui.addButton( 'Media',
|
56 |
{ |
57 |
label: 'Add media', |
58 |
command: 'media', |
59 |
icon: this.path + 'images/icon.gif' |
60 |
}); |
61 |
|
62 |
var ckeditorversion = parseFloat(CKEDITOR.version);
|
63 |
|
64 |
// Because the media comment wrapper don't work well for CKEditor we
|
65 |
// replace them by using a custom mediawrapper element.
|
66 |
// Instead having
|
67 |
// <!--MEDIA-WRAPPER-START-1--><img /><!--MEDIA-WRAPPER-END-1-->
|
68 |
// We wrap the placeholder with
|
69 |
// <mediawrapper data="1"><img /></mediawrapper>
|
70 |
// That way we can deal better with selections - see selectionChange.
|
71 |
CKEDITOR.dtd['mediawrapper'] = CKEDITOR.dtd;
|
72 |
CKEDITOR.dtd.$blockLimit['mediawrapper'] = 1; |
73 |
CKEDITOR.dtd.$inline['mediawrapper'] = 1; |
74 |
CKEDITOR.dtd.$nonEditable['mediawrapper'] = 1; |
75 |
if (ckeditorversion >= 4.1) { |
76 |
// Register allowed tag for advanced filtering.
|
77 |
editor.filter.allow( 'mediawrapper[!data]', 'mediawrapper', true); |
78 |
// Don't remove the data-file_info attribute added by media!
|
79 |
editor.filter.allow( '*[!data-file_info]', 'mediawrapper', true); |
80 |
// Ensure image tags accept all kinds of attributes.
|
81 |
editor.filter.allow( 'img[*]{*}(*)', 'mediawrapper', true); |
82 |
// Objects should be selected as a whole in the editor.
|
83 |
CKEDITOR.dtd.$object['mediawrapper'] = 1; |
84 |
} |
85 |
function prepareDataForWysiwygMode(data) { |
86 |
data = Drupal.media.filter.replaceTokenWithPlaceholder(data); |
87 |
// Legacy media wrapper.
|
88 |
mediaPluginDefinition.mediaLegacyWrappers = (data.indexOf("<!--MEDIA-WRAPPER-START-") !== -1); |
89 |
data = data.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '<mediawrapper data="$1">$2</mediawrapper>'); |
90 |
return data;
|
91 |
} |
92 |
function prepareDataForSourceMode(data) { |
93 |
var replacement = '$2'; |
94 |
// Legacy wrapper
|
95 |
if (mediaPluginDefinition.mediaLegacyWrappers) {
|
96 |
replacement = '<!--MEDIA-WRAPPER-START-$1-->$2<!--MEDIA-WRAPPER-END-$1-->';
|
97 |
} |
98 |
data = data.replace(/<mediawrapper data="(.*)">(.*?)<\/mediawrapper>/gi, replacement);
|
99 |
data = Drupal.media.filter.replacePlaceholderWithToken(data); |
100 |
return data;
|
101 |
} |
102 |
|
103 |
// Ensure the tokens are replaced by placeholders while editing.
|
104 |
// Check for widget support.
|
105 |
if (mediaPluginDefinition.hasWidgetSupport) {
|
106 |
editor.widgets.add( 'mediabox',
|
107 |
{ |
108 |
button: 'Create a mediabox', |
109 |
template: '<mediawrapper></mediawrapper>', |
110 |
editables: {},
|
111 |
allowedContent: '*', |
112 |
upcast: function( element ) { |
113 |
if (element.name != 'mediawrapper') { |
114 |
// Ensure media tokens are converted to media placeholdes.
|
115 |
element.setHtml(prepareDataForWysiwygMode(element.getHtml())); |
116 |
} |
117 |
return element.name == 'mediawrapper'; |
118 |
}, |
119 |
|
120 |
downcast: function( widgetElement ) { |
121 |
var token = prepareDataForSourceMode(widgetElement.getOuterHtml());
|
122 |
return new CKEDITOR.htmlParser.text(token); |
123 |
return element.name == 'mediawrapper'; |
124 |
} |
125 |
}); |
126 |
} |
127 |
else if (ckeditorversion >= 4) { |
128 |
// CKEditor >=4.0
|
129 |
editor.on('setData', function( event ) { |
130 |
event.data.dataValue = prepareDataForWysiwygMode(event.data.dataValue); |
131 |
}); |
132 |
} |
133 |
else {
|
134 |
// CKEditor >=3.6 behaviour.
|
135 |
editor.on( 'beforeSetMode', function( event, data ) { |
136 |
event.removeListener(); |
137 |
var wysiwyg = editor._.modes[ 'wysiwyg' ]; |
138 |
var source = editor._.modes[ 'source' ]; |
139 |
wysiwyg.loadData = CKEDITOR.tools.override( wysiwyg.loadData, function( org )
|
140 |
{ |
141 |
return function( data ) { |
142 |
return ( org.call( this, prepareDataForWysiwygMode(data)) ); |
143 |
}; |
144 |
} ); |
145 |
source.loadData = CKEDITOR.tools.override( source.loadData, function( org )
|
146 |
{ |
147 |
return function( data ) { |
148 |
return ( org.call( this, prepareDataForSourceMode(data) ) ); |
149 |
}; |
150 |
} ); |
151 |
}); |
152 |
} |
153 |
|
154 |
// Provide alternative to the widget functionality introduced in 4.3.
|
155 |
if (!mediaPluginDefinition.hasWidgetSupport) {
|
156 |
// Ensure tokens instead the html element is saved.
|
157 |
editor.on('getData', function( event ) { |
158 |
event.data.dataValue = prepareDataForSourceMode(event.data.dataValue); |
159 |
}); |
160 |
|
161 |
// Ensure our enclosing wrappers are always included in the selection.
|
162 |
editor.on('selectionChange', function( event ) { |
163 |
var ranges = editor.getSelection().getRanges().createIterator();
|
164 |
var newRanges = [];
|
165 |
var currRange;
|
166 |
while(currRange = ranges.getNextRange()) {
|
167 |
var commonAncestor = currRange.getCommonAncestor(false); |
168 |
if (commonAncestor && typeof(commonAncestor.getName) != 'undefined' && commonAncestor.getName() == 'mediawrapper') { |
169 |
var range = new CKEDITOR.dom.range( editor.document ); |
170 |
if (currRange.collapsed === true) { |
171 |
// Don't allow selection within the wrapper element.
|
172 |
if (currRange.startOffset == 0) { |
173 |
// While v3 plays nice with setting start and end to avoid
|
174 |
// editing within the media wrapper element, v4 ignores that.
|
175 |
// Thus we try to move the cursor further away.
|
176 |
if (parseInt(CKEDITOR.version) > 3) { |
177 |
range.setStart(commonAncestor.getPrevious()); |
178 |
range.setEnd(commonAncestor.getPrevious()); |
179 |
} |
180 |
else {
|
181 |
range.setStartBefore(commonAncestor); |
182 |
} |
183 |
} |
184 |
else {
|
185 |
// While v3 plays nice with setting start and end to avoid
|
186 |
// editing within the media wrapper element, v4 ignores that.
|
187 |
// Thus we try to move the cursor further away.
|
188 |
if (parseInt(CKEDITOR.version) > 3) { |
189 |
range.setStart(commonAncestor.getNext(), 1);
|
190 |
range.setEnd(commonAncestor.getNext(), 1);
|
191 |
} |
192 |
else {
|
193 |
range.setStartAfter(commonAncestor); |
194 |
} |
195 |
} |
196 |
} |
197 |
else {
|
198 |
// Always select the whole wrapper element.
|
199 |
range.setStartBefore(commonAncestor); |
200 |
range.setEndAfter(commonAncestor); |
201 |
} |
202 |
newRanges.push(range); |
203 |
} |
204 |
} |
205 |
if (newRanges.length) {
|
206 |
editor.getSelection().selectRanges(newRanges); |
207 |
} |
208 |
}); |
209 |
} |
210 |
} |
211 |
}; |
212 |
// Add dependency to widget plugin if possible.
|
213 |
if (parseFloat(CKEDITOR.version) >= 4.3 && mediaPluginDefinition.hasWidgetSupport) { |
214 |
mediaPluginDefinition.requires.push('widget');
|
215 |
} |
216 |
CKEDITOR.plugins.add( 'media', mediaPluginDefinition);
|
217 |
} )(); |