Help for my extension: YiiMarkitUp!

Hello guys, I tried to create an extension, a widget to replace the textarea with the jquery plugin Markitup.




<?php


class YiiMarkitup extends CInputWidget {

    

    public function init()

    {

        parent::init();

        

        $assetsFolder=Yii::app()->assetManager->publish(

            Yii::getPathOfAlias('application.extensions.yiimarkitup.assets')

        );


        Yii::app()->clientScript->registerCssFile($assetsFolder.'/skins/markitup/style.css');

        Yii::app()->clientScript->registerCssFile($assetsFolder.'/sets/bbcode/style.css');

        Yii::app()->clientScript->registerScriptFile($assetsFolder.'/jquery.markitup.js');

        Yii::app()->clientScript->registerScriptFile($assetsFolder.'/sets/bbcode/set.js');

        Yii::app()->clientScript->registerCoreScript('jquery');

        

        if (isset($this->htmlOptions['id']))

            $id = $this->htmlOptions['id'];

        else

            $id = CHtml::activeId($this->model, $this->attribute);


        Yii::app()->clientScript->registerScript("{$id}",

            '$(document).ready(function() {

                $("textarea").markItUp(mySettings);

            });'

        );

    }

    

    public function run()

    {

        echo CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);

    }

    

}



Usage:


<?php $this->widget('YiiMarkitup', array('model'=>$model, 'attribute'=>'contents')); ?>

But I have some problem:

  • The plugin has several folders that contain different sets of configurations (bbcode, html, wiki …), I thought that to use the one chosen by the user (and here’s another question: how does the user to choose the sets they prefer?) would be enough to do it this way:



Yii::app()->clientScript->registerCssFile($assetsFolder.'/sets/'.$type.'/style.css');

Yii::app()->clientScript->registerScriptFile($assetsFolder.'/sets/'.$type.'/set.js');



Where type can be "bbcode", "wiki", "html"…

Any suggestion? Any best way?

  1. The config script is in format json, called set.js. How can I do a merge this with some of user-defined options when calling the widget, and where the user can write them?

Thanks :)

Put the options in a regular array parameter to your widget, and then just pass it to the MarkitUp! script:


		if(empty($this->options))

			$cs->registerScript(__CLASS__.'#'.$id, "jQuery('#$id').markItUp(mySettings);");

		else

		{

			$options=CJavaScript::encode($this->options);

			$cs->registerScript(__CLASS__.'#'.$id, "jQuery('#$id').markItUp(mySettings,$options);");

		}



CJavaScript::encode is a great help!

I think there’s two other MarkitUp! widgets in the extensions repository, but it won’t hurt to have yet another - take a look at those to learn what needs to be done. :)

I found it only now, but in any case is always pressure: p

Rather, take me a curiosity: a possible bbcode parser should be created as components?

Yii has a Markdown parser (in ‘utils’ I think), so that would probably be a good idea to check that out.

IIRC, it’s a component.