Hi all. I'm from Russia and have some problems with english.
I want to create plugin and this plugin must insert PHP-code (or JavaScript, or other...) into the CKEditor.
I know about protected source:
config.protectedSource.push( /<\?[\s\S]*?\?>/g ); // PHP Code
this regular expression change for:
<!--{cke_protected}encodeURIComponent(PHP-code)-->This is source of dialog-file:
CKEDITOR.dialog.add( 'code', function( editor )
{
return {
title : 'PHP-code',
minWidth : 600,
minHeight : 400,
onOk : function()
{
var protectedSourceMarker = '{cke_protected}';
var codeTxt = this.getValueOf( 'tab', 'codeSrc' ),
element = CKEDITOR.env.ie ?
editor.document.createElement('<img _cke_code="' + encodeURIComponent( codeTxt ) + '" class="_cke_code" />') :
editor.document.createElement('img');
editor.document.createText( codeTxt );
if(!CKEDITOR.env.ie) {
element.setAttribute( 'class', '_cke_code' );
element.setAttribute( '_cke_code', encodeURIComponent( codeTxt ) );
}
if ( !this.editMode ) {
editor.insertHtml( codeTxt );
} else {
element.replace( this.fakeObj );
editor.getSelection().selectElement( element );
}
return true;
},
onShow : function()
{
var protectedSourceMarker = '{cke_protected}';
this.editObj = false;
this.editMode = false;
this.fakeObj = false;
var selection = editor.getSelection();
var element = selection.getSelectedElement();
if ( element && element.getAttribute( '_cke_code' ) && element.getAttribute( 'class' ) == '_cke_code' )
{
this.editObj = element;
this.editMode = true;
this.fakeObj = element;
var attrCode = decodeURIComponent( element.getAttribute( '_cke_code' ) );
if ( attrCode ) {
this.setValueOf( 'tab','codeSrc', attrCode );
} else {
this.setValueOf( 'tab','codeSrc', "" );
}
selection.selectElement( element );
}
this.getContentElement( 'tab', 'codeSrc' ).focus();
},
contents : [
{
id : 'tab',
label : 'First Tab',
title : 'First Tab',
elements :
[
{
id : 'codeSrc',
type : 'textarea',
label : 'Insert code:',
validate : function() {
if ( !this.getValue() ) {
alert( 'Empty field' );
return false;
}
return true;
}
}
]
}
]
};
}
);
This is source of plugin file
/*
Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/
/**
* @file Code plugin.
*/
(function() {
var pluginName = 'code';
CKEDITOR.plugins.add( pluginName,
{
requires : [ 'htmlwriter' ],
init : function( editor )
{
editor.addCommand( pluginName,new CKEDITOR.dialogCommand( 'code' ));
CKEDITOR.dialog.add( pluginName, this.path + 'dialogs/code.js' );
editor.ui.addButton( 'Code',
{
label : 'Add code',
command : pluginName,
icon : this.path + 'logo.gif'
}
);
// Add the CSS styles for anchor placeholders.
editor.addCss(
'img._cke_code' +
'{' +
'background: #c0c0c0;' +
'border: 1px solid #a9a9a9;' +
'width: 50px;' +
'height: 20px;' +
'color: #000;' +
'}\n'
);
},
afterInit : function( editor )
{
var dataProcessor = editor.dataProcessor,
dataFilter = dataProcessor && dataProcessor.dataFilter;
if ( dataFilter )
{
dataFilter.addRules(
{
comment : function( contents )
{
var protectedSourceMarker = '{cke_protected}';
if ( contents.substr( 0, protectedSourceMarker.length ) == protectedSourceMarker ) {
return new CKEDITOR.htmlParser.cdata('<!--' + contents + '--><img _cke_code="' + contents.substr( protectedSourceMarker.length) + '" class="_cke_code" />' );
//return new CKEDITOR.htmlParser.cdata('<img _cke_code="' + contents.substr( protectedSourceMarker.length) + '" class="_cke_code" />' );
//return new CKEDITOR.htmlParser.cdata('<!--' + contents + '-->' );
}
}
}
);
}
}
});
})();in result i have a fake <img> and protected comment with my PHP-code. But if I will change mode to 'source' i see code of <img />.
If I use second variant (see commented strings at the end of source of plugin-file) - I get images, but lost my code.
If i use third variant - I get code, but lost fake image.
Fake image I need because I want to select this block and at the opened dialog edit PHP-code.
Can you help me?
P.S. - excuse me for my english.

Re: Code Plugin
Is anyone can help me?
Re: Code Plugin
Yes - this is exactly what I need also - I have a plugin that pastes a bunch of text into the ckeditor instance wherever your cursor is located. How do I make ckeditor automatically replace this text with an image as some placeholder until the content is submitted?
I'm thinking I would insert "<fakeObject>...</fakeObject>" into the ckeditor instance. This is no problem. When in wysiwyg mode everything between those tags would get replaced by an image or some other kind of placeholder automatically by ckeditor. That way the user knows where the unseen code was input.
How do you do something like this?
Re: Code Plugin
So if i want to enter php i have something like this written in code mode:
<php><? echo 'test'; ?></php>
before we use a submitted text from editor we should remove the <php> and </php> with
str_ireplace( array('<php>','</php>'), '', stripslashes($_POST[text1]) ));
and when we load a code to the editor we should add <php> tags for the real php
$editor_text = preg_replace('/<\?(.*?)\?>/' , '<php><?$1?></php>', stripslashes($db_code));
Attachments: