How to use editor in plugins?

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: How to use editor in plugins?

Post by Holger » Sat Jan 14, 2012 12:38 am

Ok, did it - but another way ;) :

as an example I use the code from init_tinymce(). Just three lines to change:

1. In top of the function, initialize $temp to false:

Code: Select all

function init_tinymce($classes = array(), $config = false) {
    global $sl, $cf, $plugin_cf, $pth, $hjs, $o, $h, $u, $l, $sn, $onload;
    static $run = 0;
    
    $temp = false; //Add this line
    
    include_tinymce(); 
2. After the foreach - loop:

Code: Select all

foreach ($inits as $init) {
            $temp = explode('_', basename($init, '.js'));

            if (isset($temp[1]) && $temp[1] === $config) {
               $tiny_mode = $config;
               $isFile = false;
           $initFile = $pth['folder']['plugins'] . 'tinymce/' . 'inits/init_' . $tiny_mode . '.js';
               break;
            }
        }
        
        if(!$initFile){
            //$initFile = $config;
            $temp = $config;  // this line is new
            
        } 
3. After inclusion of links.php

Code: Select all

include_once $pth['folder']['plugins'] . 'tinymce/' . 'links.php';

    //$temp = file_get_contents($initFile);
    if ($temp === false) {
        $temp = file_get_contents($initFile); //use $initFile only if $temp is still false
    } 

    $temp = str_replace('%TINY_FOLDER%', $pth['folder']['plugins'] . 'tinymce/', $temp);
    $temp = str_replace('%LANGUAGE%', $tiny_language, $temp); 
That's it.

Now a plugin author can copy a shipped init_xxx.js from the /editor/inits - folder to change it for his needs and to put it to his plugin-folder.
It's possible to use the placeholders too, so he must not mess around with dynamic settings like baseHref or the internal-links.
Adjusting such an existing init_xxx.js should be easy done.

@cmb:
I'll agree with you that it's the best choice to use existing editor-configurations like "full" "minimal" etc, this way:

Code: Select all

init_editor(array('class1', 'class2'), 'minimal')  
But there is a need for special-configured editors in plugins.

The integration of those "special editors" should be done with the changed code above, together with a check, if the plugin provides config-files for the configured editor.
A plugin can do that this way:

Code: Select all

$config = false;
 
 //look for the name of the configured editor
 $myEditor = $cf['editor']['external'];
 
 $config = @file_get_contents('myconfig_' . $myEditor . 'js'); //use '@' to avoid PHP - warnings
 
    if ($config) {
        include_editor($classes, $config);
    }
    else {
        //throw a message: No compatible editor installed!
    } 
That's all to code in the plugin - beside the adjusted config-file. Maybe the plugin can look if there is another editor installed but not activated. So the plugin could use this one. But I'll not recommend that.

IMO that's a flexible and easy way to include editors to plugins. No need to mess around with filebrowsers, baseHref or other dynamic settings of unknown editors. No need to mess around with three single functions and coding of a complete editor-setups.
And adding a new editor to the plugin is done with only a new config-file :) .

At the end I think it's better this way, instead calling the function with a path to a static file. It's somehow as I meant in a post at the beginning of this thread:
myself wrote:if a plugin just needs another skin or another toolbar, maybe it's a good idea when $config only overwrites the default editor-configuration.
So init_editor() will always create a working editor ...
KR
Holger

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to use editor in plugins?

Post by cmb » Sat Jan 14, 2012 2:58 am

Hello Holger,

thanks for your suggestion (already with a working solution, and an explanation, how it could be used by plugin developers). :)

IMO it can be done both ways ($config as path or as string), but perhaps your proposal is even more versatile.

Two remarks:
  • I somewhat dislike the @ operator in production code. A warning will not be displayed anyway, if error_reporting() is off, but the warning might be very useful for debugging purposes (BTW: thank you for inventing the XHdebug mode -- I love it). And most failures could be avoided in this case by checking, if the file is_readable().
  • Is the glob loop in init_tinymce() really necessary? Wouldn't a check for

    Code: Select all

    file_exists($pth['folder']['plugins'].'tinymce/'.'inits/init_'.$config.'.js') 
    suffice? :?
Christoph
Christoph M. Becker – Plugins for CMSimple_XH

Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: How to use editor in plugins?

Post by Holger » Sat Jan 14, 2012 12:26 pm

Hi Christoph,
cmb wrote:IMO it can be done both ways ($config as path or as string), but perhaps your proposal is even more versatile.
Of course it might be useful for another editor to do the configuration with a path to a static file.

But for TinyMCE and CKeditor it seems to be impossible to create a working installation by a plugin that way.
You should check the config-files of those two editors. How do you want to include things like "baseHref : '%BASE_HREF%'" or "height : '%EDITOR_HEIGHT%'" in a static file?
Maybe it's possible with other editors. So those ones should stick on a path to a config-file for $config.
But we must not include that for CK & Tiny. That should be done "editor-dependant".
(BTW: when will we see a - maybe experimental - plugin of your Whizzywig? Don't you have plans for a release?)

Only the function init_editorname() must work with all editors. This function must return all code for a working editor.
That's IMO the best, or at least the easiest, way for the plugin - editor - interface.
cmb wrote:I somewhat dislike the @ operator in production code.
No problem. Just used it to avoid an additional line for a is_readable() - check. But maybe it would be cleaner doing it that way. So no intentional warnings were produced from the code...
cmb wrote:Is the glob loop in init_tinymce() really necessary?
No. The check on file_exist is enough. But my goal was to implement CK the same as possible as Tiny. So a nearly identical code might be easier to handle on future updates / debugging by uninvolved developers too. So I've avoided to change the initial code.
In other words, if there will be changes with the Tiny-integration, I'll follow them for CK too.

So what should I do now? Should I release the CK-Plugin with this code? Or should I wait for the changes in 1.5.2?
I've finished the CK-Plugin and an updated KCFinder - Plugin (with your proposed check on userDir too). Both are working in all editor / filebrowser - combinations.
It would be nice if things in this case not end like with the editor- / filebrowser - hooks ;) , because I need my rare free time for other projects too.

KR
Holger

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to use editor in plugins?

Post by cmb » Sat Jan 14, 2012 1:24 pm

Hi Holger,
Holger wrote:But for TinyMCE and CKeditor it seems to be impossible to create a working installation by a plugin that way.
You should check the config-files of those two editors. How do you want to include things like "baseHref : '%BASE_HREF%'" or "height : '%EDITOR_HEIGHT%'" in a static file?
IMO it shouldn't be a problem if the external init file would be processed the same way as the internal ones are: read the file, replace all %PLACEHOLDER%s and instantiate the editor(s).
Holger wrote:BTW: when will we see a - maybe experimental - plugin of your Whizzywig? Don't you have plans for a release?
I haven't worked on Whizzywig for quite a while, because I consider other plugins more important. And Whizzywig requires quite some changes to it's core and several additions to work with the new interfaces. But before releasing a public version, I have to make sure, that the generated code is indeed XHTML conforming. So that's a lot of work. :( Yesterday I've spend several hours on updating Ajaxfilemanager to jQuery 1.7.1, and even this doesn't work fully yet (let alone the proposed improvements to the image editor). So Whizzywig unfortunately has to wait.
Holger wrote:So what should I do now? Should I release the CK-Plugin with this code? Or should I wait for the changes in 1.5.2?
If it's already finished, IMO you should release it. The roadmap to 1.5.2 already consists of more than 20 points, which have to be discussed, implemented and tested, before 1.5.2 could be released. So unfortunately this might take some time.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: How to use editor in plugins?

Post by Holger » Sat Jan 14, 2012 2:39 pm

Hi Christoph,
cmb wrote:IMO it shouldn't be a problem if the external init file would be processed the same way as the internal ones are: read the file, replace all %PLACEHOLDER%s and instantiate the editor(s).
Ahh :idea: , now I understand.
You want to move processing of the placeholders from the plugin to editors init.php.
Yep. That's a good idea. More flexible, easier for plugin-development.

So I think I'll break with the proposed code from TinyMCEs init.php:
  1. remove the glob loop and do a check on $config like

    Code: Select all

    file_exists($pth['folder']['plugins'].'tinymce/'.'inits/init_'.$config.'.js') 
  2. check if $config is a file*

    Code: Select all

    if (is_file($config)) { ... } 
    *do you think that is_file() is a good choice for that job, maybe with a following is_readable()?
  3. handle the contents of $config as a config-string and parse the placeholders, like in my code above
I'll do that this weekend and publish the plugin, if nothing goes wrong.

KR
Holger

BTW: nice to have such a discussion in OD, thanks!

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to use editor in plugins?

Post by cmb » Sat Jan 14, 2012 3:00 pm

Hi Holger,
Holger wrote:*do you think that is_file() is a good choice for that job, maybe with a following is_readable()?
is_file() is even better than file_exists(), and makes the latter superflous:
http://de.php.net/manual/en/function.is-file.php wrote:Returns TRUE if the filename exists and is a regular file, FALSE otherwise.
An additional is_readable() might be reasonable.
Holger wrote:I'll do that this weekend and publish the plugin, if nothing goes wrong.
That's good, so Frank might use CKeditor for testing, if the editor integration goes well. But wait :idea: : with this init_editor() it's not possible to create the editor instances on demand (at least not without "Ajax"). So finally this might be a good reason to have editor_replace(). Should we change it's specification to process the second parameter exactly like init_editor()? :? In this case, it might be better to stick with your 1st version of processing not a file name, but a config string instead. :?

Christoph

PS: Are there any other plugin developers, who need an editor, or editor integrators out there? What do you think about it?
Christoph M. Becker – Plugins for CMSimple_XH

Holger
Site Admin
Posts: 3470
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany

Re: How to use editor in plugins?

Post by Holger » Sun Jan 15, 2012 10:46 am

Hi Christoph,
cmb wrote:But wait :idea: : with this init_editor() it's not possible to create the editor instances on demand (at least not without "Ajax"). So finally this might be a good reason to have editor_replace(). Should we change it's specification to process the second parameter exactly like init_editor()? :? In this case, it might be better to stick with your 1st version of processing not a file name, but a config string instead. :?
thought about it. Yes, that's a point. And IMO really the own reason to keep with editor_replace().

Would it be a problem to allow the 2nd specification for init_editor() and only config strings for editor_replace()?
I don't know.
Having such a flexible implementation for init_editor() is IMO the best option.
And, because using editor_replace() is maybe not often needed, we leave as only possible option the config string here.

I'm unsure if the different way of using $config is a problem...
cmb wrote:PS: Are there any other plugin developers, who need an editor, or editor integrators out there? What do you think about it?
What about comments_xh?

KR
Holger

Gert
Posts: 3078
Joined: Fri May 30, 2008 4:53 pm
Location: Berlin
Contact:

Re: How to use editor in plugins?

Post by Gert » Sun Jan 15, 2012 11:14 am

Hallo,

Comments_XH has an own mini TinyMCE - special configured for the safety concept of Comments_XH, and RealBlog_XH uses the standard TinyMCE.

By the way: Yesterday I had the chance to test new tablets 10'' by MediMax:

- 1 Android based (Samsung)
- an iPad
- 1 with Windows7

All 3 could write comments with Comments_XH - seems that my strategie "sit and wait" was the right way ;)

Gert
Gert Ebersbach | CMSimple | Templates - Plugins - Services

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: How to use editor in plugins?

Post by cmb » Sun Jan 15, 2012 11:58 am

Hi Holger, hi Gert,

config file / string

Both variants could and IMO should process the %PLACEHOLDER%s.

PRO config file: reading of the file musn't be done by the plugin author.

PRO config string: the plugin author is able to do further dynamic processing of the string, and he doesn't even have to create a file (it's quite possible to assemble the config string on the fly by including settings from $plugin_cf[]).

So probably the config string is the more flexible way.

init_editor() / editor_replace()

If both functions expect exactly the same config parameter, it should be easy to switch between them. If it's a config file or string, which could contain the %PLACEHOLDER%s, it basically comes down to calling init_editor() or include_editor() + editor_replace().

The only drawback I can see in this case, is that the routine to replace the %PLACEHOLDER%s will be called always, when editor_replace() is called even if the config string doesn't use any placeholders (probably just a slight processing overhead).
Gert wrote:By the way: Yesterday I had the chance to test new tablets 10'' by MediMax:
...
All 3 could write comments with Comments_XH
Well, tablets are one thing -- smartphones might be another. (see http://web61.de/cms/index.php?Blog&real ... D=3&page=1)

The only editor that works on my Opera Mobile 10 emulator is Textarea_XH (Codeeditor_XH basically works too, but it's usability is not good on this browser).

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

Gert
Posts: 3078
Joined: Fri May 30, 2008 4:53 pm
Location: Berlin
Contact:

Re: How to use editor in plugins?

Post by Gert » Sun Jan 15, 2012 12:04 pm

cmb wrote:Well, tablets are one thing -- smartphones might be another.
And furthermore I sit and wait ;)

It's a question of the operating systems or the mobile browsers - I'm sure, also that problem will be solved anytime by the smartphones itself,

Gert
Gert Ebersbach | CMSimple | Templates - Plugins - Services

Post Reply