2012-04-12 10:46:37 +02:00
|
|
|
(function() {
|
|
|
|
|
2016-02-12 04:00:15 +01:00
|
|
|
var ssbuttons = {
|
2012-04-12 10:46:37 +02:00
|
|
|
/**
|
|
|
|
* Returns information about the plugin as a name/value array.
|
|
|
|
* The current keys are longname, author, authorurl, infourl and version.
|
|
|
|
*
|
|
|
|
* @returns Name/value array containing information about the plugin.
|
|
|
|
* @type Array
|
|
|
|
*/
|
|
|
|
getInfo : function() {
|
|
|
|
return {
|
|
|
|
longname : 'Special buttons for SilverStripe CMS',
|
|
|
|
author : 'Sam Minnée',
|
|
|
|
authorurl : 'http://www.siverstripe.com/',
|
|
|
|
infourl : 'http://www.silverstripe.com/',
|
|
|
|
version : "1.0"
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-02-12 04:00:15 +01:00
|
|
|
init : function(ed) {
|
|
|
|
ed.addButton('sslink', {
|
|
|
|
icon : 'link',
|
|
|
|
title : 'Insert Link',
|
|
|
|
cmd : 'sslink'
|
|
|
|
});
|
|
|
|
ed.addMenuItem('sslink', {
|
|
|
|
icon : 'link',
|
|
|
|
text : 'Insert Link',
|
|
|
|
cmd : 'sslink'
|
|
|
|
});
|
|
|
|
ed.addButton('ssmedia', {
|
|
|
|
icon : 'image',
|
|
|
|
title : 'Insert Media',
|
|
|
|
cmd : 'ssmedia'
|
|
|
|
});
|
|
|
|
ed.addMenuItem('ssmedia', {
|
|
|
|
icon : 'image',
|
|
|
|
text : 'Insert Media',
|
|
|
|
cmd : 'ssmedia'
|
|
|
|
});
|
|
|
|
|
2012-04-12 10:46:37 +02:00
|
|
|
|
2012-04-17 04:42:57 +02:00
|
|
|
ed.addCommand('sslink', function(ed) {
|
2016-02-12 04:00:15 +01:00
|
|
|
// See HtmlEditorField.js
|
2012-04-17 04:42:57 +02:00
|
|
|
jQuery('#' + this.id).entwine('ss').openLinkDialog();
|
2012-04-12 10:46:37 +02:00
|
|
|
});
|
|
|
|
|
2012-05-22 06:45:36 +02:00
|
|
|
ed.addCommand('ssmedia', function(ed) {
|
2016-02-12 04:00:15 +01:00
|
|
|
// See HtmlEditorField.js
|
2012-04-17 04:42:57 +02:00
|
|
|
jQuery('#' + this.id).entwine('ss').openMediaDialog();
|
2012-04-12 10:46:37 +02:00
|
|
|
});
|
2013-01-31 04:39:00 +01:00
|
|
|
|
|
|
|
// Replace the mceAdvLink and mceLink commands with the sslink command, and
|
|
|
|
// the mceAdvImage and mceImage commands with the ssmedia command
|
2016-02-12 04:00:15 +01:00
|
|
|
ed.on('BeforeExecCommand', function(e){
|
|
|
|
cmd = e.command;
|
|
|
|
ui = e.ui;
|
|
|
|
val = e.value;
|
2013-01-31 04:39:00 +01:00
|
|
|
if (cmd == 'mceAdvLink' || cmd == 'mceLink'){
|
2016-02-12 04:00:15 +01:00
|
|
|
e.preventDefault();
|
|
|
|
ed.execCommand('sslink', ui, val);
|
|
|
|
} else if (cmd == 'mceAdvImage' || cmd == 'mceImage'){
|
|
|
|
e.preventDefault();
|
|
|
|
ed.execCommand('ssmedia', ui, val);
|
2013-01-31 04:39:00 +01:00
|
|
|
}
|
2012-04-12 10:46:37 +02:00
|
|
|
});
|
2012-05-22 06:45:36 +02:00
|
|
|
|
2016-02-12 04:00:15 +01:00
|
|
|
ed.on('SaveContent', function(o) {
|
2012-05-22 06:45:36 +02:00
|
|
|
var content = jQuery(o.content);
|
|
|
|
content.find('.ss-htmleditorfield-file.embed').each(function() {
|
|
|
|
var el = jQuery(this);
|
2013-05-25 01:26:34 +02:00
|
|
|
var shortCode = '[embed width="' + el.attr('width') + '"'
|
|
|
|
+ ' height="' + el.attr('height') + '"'
|
2013-04-26 01:00:13 +02:00
|
|
|
+ ' class="' + el.data('cssclass') + '"'
|
|
|
|
+ ' thumbnail="' + el.data('thumbnail') + '"'
|
2012-05-22 06:45:36 +02:00
|
|
|
+ ']' + el.data('url')
|
|
|
|
+ '[/embed]';
|
|
|
|
el.replaceWith(shortCode);
|
|
|
|
});
|
2014-11-10 10:30:00 +01:00
|
|
|
|
|
|
|
// Insert outerHTML in order to retain all nodes incl. <script>
|
|
|
|
// tags which would've been filtered out with jQuery.html().
|
|
|
|
// Note that <script> tags might be sanitized separately based on editor config.
|
|
|
|
o.content = '';
|
|
|
|
content.each(function() {
|
|
|
|
if(this.outerHTML !== undefined) {
|
|
|
|
o.content += this.outerHTML;
|
|
|
|
}
|
|
|
|
});
|
2012-05-22 06:45:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
var shortTagRegex = /(.?)\[embed(.*?)\](.+?)\[\/\s*embed\s*\](.?)/gi;
|
2016-02-12 04:00:15 +01:00
|
|
|
ed.on('BeforeSetContent', function(o) {
|
2012-05-22 06:45:36 +02:00
|
|
|
var matches = null, content = o.content;
|
|
|
|
var prefix, suffix, attributes, attributeString, url;
|
|
|
|
var attrs, attr;
|
|
|
|
var imgEl;
|
2012-05-24 23:31:15 +02:00
|
|
|
// Match various parts of the embed tag
|
2012-05-22 06:45:36 +02:00
|
|
|
while((matches = shortTagRegex.exec(content))) {
|
|
|
|
prefix = matches[1];
|
|
|
|
suffix = matches[4];
|
|
|
|
if(prefix === '[' && suffix === ']') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attributes = {};
|
2012-05-24 23:31:15 +02:00
|
|
|
// Remove quotation marks and trim.
|
2012-05-22 06:45:36 +02:00
|
|
|
attributeString = matches[2].replace(/['"]/g, '').replace(/(^\s+|\s+$)/g, '');
|
2012-05-24 23:31:15 +02:00
|
|
|
|
|
|
|
// Extract the attributes and values into a key-value array (or key-key if no value is set)
|
2012-05-22 06:45:36 +02:00
|
|
|
attrs = attributeString.split(/\s+/);
|
|
|
|
for(attribute in attrs) {
|
|
|
|
attr = attrs[attribute].split('=');
|
|
|
|
if(attr.length == 1) {
|
|
|
|
attributes[attr[0]] = attr[0];
|
|
|
|
} else {
|
|
|
|
attributes[attr[0]] = attr[1];
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 23:31:15 +02:00
|
|
|
|
|
|
|
// Build HTML element from embed attributes.
|
|
|
|
attributes.cssclass = attributes['class'];
|
2012-05-22 06:45:36 +02:00
|
|
|
url = matches[3];
|
|
|
|
imgEl = jQuery('<img/>').attr({
|
2012-05-24 23:31:15 +02:00
|
|
|
'src': attributes['thumbnail'],
|
|
|
|
'width': attributes['width'],
|
|
|
|
'height': attributes['height'],
|
|
|
|
'class': attributes['cssclass'],
|
|
|
|
'data-url': url
|
2012-05-22 06:45:36 +02:00
|
|
|
}).addClass('ss-htmleditorfield-file embed');
|
|
|
|
|
|
|
|
jQuery.each(attributes, function (key, value) {
|
|
|
|
imgEl.attr('data-' + key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
content = content.replace(matches[0], prefix + (jQuery('<div/>').append(imgEl).html()) + suffix);
|
|
|
|
}
|
|
|
|
o.content = content;
|
|
|
|
});
|
2012-04-12 10:46:37 +02:00
|
|
|
}
|
2016-02-12 04:00:15 +01:00
|
|
|
};
|
2012-04-12 10:46:37 +02:00
|
|
|
|
|
|
|
// Adds the plugin class to the list of available TinyMCE plugins
|
2016-02-12 04:00:15 +01:00
|
|
|
tinymce.PluginManager.add("ssbuttons", function(editor) {
|
|
|
|
ssbuttons.init(editor);
|
|
|
|
});
|
2012-04-17 04:42:57 +02:00
|
|
|
})();
|