Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Image plugin should use Selected Text.. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 127 additions & 106 deletions bgimage/dialog/bgimageDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ CKEDITOR.dialog.add('bgImageDialog', function(editor) {
minWidth: 500,
minHeight: 200,
onOk: function() {
contents = editor.document.getBody().getHtml();
matches = contents.match(/<div style="(.*)">((.|\n)*?)<\/div>/)
// styled div already exists
if(matches){
contents = matches[2];
}

var dialog = this;
var imageURL = dialog.getValueOf('tab1', 'imageURL');
Expand All @@ -20,17 +14,28 @@ CKEDITOR.dialog.add('bgImageDialog', function(editor) {
var attachment = dialog.getValueOf('tab1', 'attachment')
var width = dialog.getValueOf('tab1', 'width');
var height = dialog.getValueOf('tab1', 'height');
var div = '<div class="AA" style="';
div += 'background-image:url(' + imageURL + ');';
div += 'background-repeat:' + repeat + ';';
div += 'background-position:' + pos + ';';
div += 'background-blend-mode:' + blendMode + ';';
div += 'background-attachment:' + attachment + ';';
div += 'background-size:' + width +' '+height + ';';
div += '">';
div += contents;
div += '</div>'
editor.setData(div);
var selected_text = false;

selected_text = bgimage_getselectedtext(editor);
var newElement = new CKEDITOR.dom.element("div"); // Make Paragraff

newElement.setStyle("background-image", "url(" + imageURL + ")");
newElement.setStyle("background-repeat", repeat);
newElement.setStyle("background-position", pos);
newElement.setStyle("background-blend-position", blendMode);
newElement.setStyle("background-attachment", attachment);
newElement.setStyle("background-size", width + ' ' + height);
newElement.addClass('ckeditor-bgimage');

if(selected_text) {
selected_text = "<div>" + selected_text + "</div>";
newElement.setHtml(selected_text);
} else {
newElement.setHtml("<p><br /></p>");
};

editor.insertElement(newElement);

},
contents: [{
id: 'tab1',
Expand All @@ -41,51 +46,51 @@ CKEDITOR.dialog.add('bgImageDialog', function(editor) {
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['280px', '100px;vertical-align: middle;'],
align: 'right',
styles :'',
children: [{
type: 'text',
label: editor.lang.bgimage.imageUrl,
id: 'imageURL',
}, {
type: 'button',
id: 'browse',
label: editor.lang.common.browseServer,
hidden: true,
filebrowser: 'tab1:imageURL'
}]
type: 'hbox',
widths: ['280px', '100px;vertical-align: middle;'],
align: 'right',
styles: '',
children: [{
type: 'text',
label: editor.lang.bgimage.imageUrl,
id: 'imageURL',
}, {
type: 'button',
id: 'browse',
label: editor.lang.common.browseServer,
hidden: true,
filebrowser: 'tab1:imageURL'
}]
}]
}, {
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'select',
id: 'repeat',
label: editor.lang.bgimage.repeat,
items: [
['repeat'],
['no-repeat'],
['repeat-x'],
['repeat-y'],
],
'default': 'repeat'
}, {
type: 'select',
id: 'attachment',
label: editor.lang.bgimage.attachment,
items: [
['scroll'],
['fixed'],
['local'],
]
}]
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'select',
id: 'repeat',
label: editor.lang.bgimage.repeat,
items: [
['repeat'],
['no-repeat'],
['repeat-x'],
['repeat-y'],
],
'default': 'repeat'
}, {
type: 'select',
id: 'attachment',
label: editor.lang.bgimage.attachment,
items: [
['scroll'],
['fixed'],
['local'],
]
}]
}]
}, {
type: 'vbox',
padding: 0,
Expand Down Expand Up @@ -129,57 +134,73 @@ CKEDITOR.dialog.add('bgImageDialog', function(editor) {
'default': 'left top'
}, ]
}]
},{
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'select',
id: 'repeat',
label: editor.lang.bgimage.repeat,
items: [
['repeat'],
['no-repeat'],
['repeat-x'],
['repeat-y'],
],
'default': 'repeat'
}, {
type: 'select',
id: 'attachment',
label: editor.lang.bgimage.attachment,
items: [
['scroll'],
['fixed'],
['local'],
]
}]
}]
}, {
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'text',
id: 'width',
label: editor.lang.bgimage.bgWidth,
width:'50px',
}, {
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'select',
id: 'repeat',
label: editor.lang.bgimage.repeat,
items: [
['repeat'],
['no-repeat'],
['repeat-x'],
['repeat-y'],
],
'default': 'repeat'
}, {
type: 'select',
id: 'attachment',
label: editor.lang.bgimage.attachment,
items: [
['scroll'],
['fixed'],
['local'],
]
}]
}]
}, {
type: 'vbox',
padding: 0,
children: [{
type: 'hbox',
widths: ['150px', '150px'],
align: 'right',
children: [{
type: 'text',
id: 'width',
label: editor.lang.bgimage.bgWidth,
width: '50px',

}, {
type: 'text',
label: editor.lang.bgimage.bgHeight,
id: 'height',
align: 'right',
width:'50px'
}]
}]
}]
}, {
type: 'text',
label: editor.lang.bgimage.bgHeight,
id: 'height',
align: 'right',
width: '50px'
}]
}]
}]
}],
}
})

/**
* Get information from SELECTED Text
* http://stackoverflow.com/questions/10295613/how-to-get-selected-html-from-ckeditor-in-javascript
* @param CKEDTIOR editor
* @return HTML
*/
function bgimage_getselectedtext(editor) {
var sel = editor.getSelection();
var ranges = sel.getRanges();
var el = new CKEDITOR.dom.element("div");
for (var i = 0, len = ranges.length; i < len; ++i) {
el.append(ranges[i].cloneContents());
}
return el.getHtml();
};
9 changes: 7 additions & 2 deletions bgimage/plugin.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@

/**
* Set background image for CKeditor
* Author : Saeed Moqadam <[email protected]>
* Author : Saeed Moqadam <[email protected]>, Michael Jones <[email protected]>
* Under MIT License
*/
CKEDITOR.plugins.add('bgimage',{
lang:['en','fa','ru'],
icons: 'bgimage',
init:function (editor) {

editor.addCommand('bgimage',new CKEDITOR.dialogCommand('bgImageDialog'));
editor.ui.addButton(editor.lang.bgimage.bgImageTitle,{
'label':editor.lang.bgimage.bgImageTitle,
'command':'bgimage',
'toolbar':'insert',
icon: this.path + 'icons/bgimage.png'
});
CKEDITOR.dialog.add('bgImageDialog',this.path+'dialog/bgimageDialog.js');

var bgImagePath = (this.path+'dialog/bgimageDialog.js');
CKEDITOR.dialog.add('bgImageDialog', bgImagePath);

}
})