-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
147 lines (140 loc) · 5.05 KB
/
plugin.js
File metadata and controls
147 lines (140 loc) · 5.05 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
/*
Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* @file Plugin for inserting Drupal embeded media
*/
( function() {
CKEDITOR.plugins.add( 'mediaembed',
{
requires : [ 'fakeobjects', 'htmlwriter' ],
init: function( editor )
{
editor.addCss(
'img.cke_mediaembed' +
'{' +
'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/placeholder.gif' ) + ');' +
'background-position: center center;' +
'background-repeat: no-repeat;' +
'border: 1px solid #a9a9a9;' +
'width: 80px;' +
'height: 80px;' +
'}'
);
var me = this;
CKEDITOR.dialog.add( 'MediaEmbedDialog', function( editor ) {
return {
title : Drupal.t('Embed Media Dialog'),
minWidth : 400,
minHeight : 200,
contents : [
{
id : 'mediaTab',
label : Drupal.t('Embed media code'),
title : Drupal.t('Embed media code'),
elements :
[
{
id : 'embed',
type : 'textarea',
rows : 9,
label : Drupal.t('Paste embed code here')
}
]
}
],
onOk : function() {
var editor = this.getParentEditor();
var content = this.getValueOf( 'mediaTab', 'embed' );
if ( content.length>0 ) {
var realElement = CKEDITOR.dom.element.createFromHtml('<div class="media_embed"></div>');
realElement.setHtml(content);
var fakeElement = editor.createFakeElement( realElement , 'cke_mediaembed', 'div', true);
var matches = content.match(/width=(["']?)(\d+)\1/i);
if (matches && matches.length == 3) {
fakeElement.setStyle('width', cssifyLength(matches[2]));
}
matches = content.match(/height=([\"\']?)(\d+)\1/i);
if (matches && matches.length == 3) {
fakeElement.setStyle('height', cssifyLength(matches[2]));
}
editor.insertElement(fakeElement);
}
}
};
});
editor.addCommand( 'MediaEmbed', new CKEDITOR.dialogCommand( 'MediaEmbedDialog' ) );
editor.ui.addButton( 'MediaEmbed',
{
label: 'Embed Media',
command: 'MediaEmbed',
icon: this.path + 'images/icon.png'
} );
},
afterInit : function( editor )
{
var dataProcessor = editor.dataProcessor,
dataFilter = dataProcessor && dataProcessor.dataFilter,
htmlFilter = dataProcessor && dataProcessor.htmlFilter;
if ( htmlFilter )
{
htmlFilter.addRules({
elements :
{
'div' : function ( element ) {
if( element.attributes['class'] == 'media_embed' ) {
for (var x in element.children) {
if (typeof(element.children[x].attributes) != 'undefined') {
if (typeof(element.children[x].attributes.width) != undefined) {
element.children[x].attributes.width = element.attributes.width;
}
if (typeof(element.children[x].attributes.height) != undefined) {
element.children[x].attributes.height = element.attributes.height;
}
}
}
}
}
}
});
}
if ( dataFilter )
{
dataFilter.addRules(
{
elements :
{
'div' : function( element )
{
var attributes = element.attributes,
classId = attributes.classid && String( attributes.classid ).toLowerCase();
if (element.attributes[ 'class' ] == 'media_embed') {
var fakeElement = editor.createFakeParserElement(element, 'cke_mediaembed', 'div', true);
var fakeStyle = fakeElement.attributes.style || '';
if (typeof(element.children[0].attributes) != 'undefined') {
var height = element.children[0].attributes.height,
width = element.children[0].attributes.width;
}
if ( typeof width != 'undefined' )
fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength( width ) + ';';
if ( typeof height != 'undefined' )
fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength( height ) + ';';
return fakeElement;
}
return element;
}
}
},
5);
}
}
} );
var numberRegex = /^\d+(?:\.\d+)?$/;
function cssifyLength( length )
{
if ( numberRegex.test( length ) )
return length + 'px';
return length;
}
} )();