You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5 KiB

  1. <?php
  2. namespace common\widgets\ueditor;
  3. use Yii;
  4. use yii\web\View;
  5. use yii\helpers\Html;
  6. use yii\helpers\Url;
  7. use yii\helpers\Json;
  8. use yii\helpers\ArrayHelper;
  9. use yii\widgets\InputWidget;
  10. use common\widgets\ueditor\assets\UeditorAsset;
  11. class Ueditor extends InputWidget
  12. {
  13. /**
  14. * 编辑器传参配置(配置查看百度编辑器(ueditor)官方文档)
  15. */
  16. public $options = [];
  17. /**
  18. * 编辑器默认基础配置
  19. */
  20. public $_init;
  21. public function init()
  22. {
  23. $this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
  24. $this->_init = [
  25. 'serverUrl' => Url::to(['ueditor']),
  26. 'lang' => (strtolower(\Yii::$app->language) == 'en-us') ? 'en' : 'zh-cn',
  27. ];
  28. $this->options = ArrayHelper::merge($this->_init, $this->options);
  29. //parent::init();
  30. }
  31. public function run()
  32. {
  33. $this->registerClientScript();
  34. if ($this->hasModel()) {
  35. return Html::activeTextarea($this->model, $this->attribute, ['id' => $this->id]);
  36. } else {
  37. return Html::textarea($this->id, $this->value, ['id' => $this->id]);
  38. }
  39. }
  40. /**
  41. * 注册Js
  42. */
  43. protected function registerClientScript()
  44. {
  45. UEditorAsset::register($this->view);
  46. $options = Json::encode($this->options);
  47. $script = "UE.getEditor('" . $this->id . "', " . $options . ")";
  48. $this->view->registerJs($script, View::POS_READY);
  49. }
  50. }