Ueditor.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace backend\widgets;
  9. use yii\helpers\Html;
  10. use yii\helpers\Url;
  11. use yii\helpers\Json;
  12. use backend\assets\UeditorAsset;
  13. class Ueditor extends \yii\widgets\InputWidget
  14. {
  15. /**
  16. * 生成的ueditor对象的名称,默认为editor。
  17. * 主要用于同一个页面的多个editor实例的管理。
  18. *
  19. * @var string
  20. */
  21. public $name;
  22. /**
  23. * UEditor配置
  24. *
  25. * @var array
  26. */
  27. public $config = [];
  28. /**
  29. * 初始化一些配置。
  30. * 由于不引入config.js文件,因此需要手动配置一些东西。
  31. */
  32. public function init()
  33. {
  34. parent::init();
  35. //注册资源文件
  36. $asset = UeditorAsset::register($this->getView());
  37. //设置UEditor实例的名字
  38. if (! $this->name) {
  39. $this->name = $this->hasModel() ? $this->model->formName() . '_' . $this->attribute : 'ueditor_' . $this->id;
  40. }
  41. //常用配置项
  42. if (empty($this->config['UEDITOR_HOME_URL'])) {
  43. $this->config['UEDITOR_HOME_URL'] = $asset->baseUrl . '/';
  44. }
  45. if (empty($this->config['serverUrl'])) {
  46. $this->config['serverUrl'] = Url::to(['/assets/ueditor']);
  47. } elseif (is_array($this->config['serverUrl'])) {
  48. $this->config['serverUrl'] = Url::to($this->config['serverUrl']);
  49. }
  50. if (empty($this->config['lang'])) {
  51. $this->config['lang'] = 'zh-cn';
  52. }
  53. if (empty($this->config['initialFrameHeight'])) {
  54. $this->config['initialFrameHeight'] = 400;
  55. }
  56. if (empty($this->config['initialFrameWidth'])) {
  57. $this->config['initialFrameWidth'] = '100%';
  58. }
  59. if (empty($this->config['enableAutoSave'])) {
  60. $this->config['enableAutoSave'] = false;
  61. }
  62. //扩展默认不直接引入config.js文件,因此需要自定义配置项.
  63. if (empty($this->config['toolbars'])) {
  64. //为了避免每次使用都输入乱七八糟的按钮,这里预先定义一些常用的编辑器按钮。
  65. //这是一个丑陋的二维数组
  66. $this->config['toolbars'] = [
  67. [
  68. 'fullscreen',
  69. 'source',
  70. 'undo',
  71. 'redo',
  72. '|',
  73. 'customstyle',
  74. 'paragraph',
  75. 'fontfamily',
  76. 'fontsize'
  77. ],
  78. [
  79. 'bold',
  80. 'italic',
  81. 'underline',
  82. 'fontborder',
  83. 'strikethrough',
  84. 'superscript',
  85. 'subscript',
  86. 'removeformat',
  87. 'formatmatch',
  88. 'autotypeset',
  89. 'blockquote',
  90. 'pasteplain',
  91. '|',
  92. 'forecolor',
  93. 'backcolor',
  94. 'insertorderedlist',
  95. 'insertunorderedlist',
  96. '|',
  97. 'rowspacingtop',
  98. 'rowspacingbottom',
  99. 'lineheight',
  100. '|',
  101. 'directionalityltr',
  102. 'directionalityrtl',
  103. 'indent',
  104. '|'
  105. ],
  106. [
  107. 'justifyleft',
  108. 'justifycenter',
  109. 'justifyright',
  110. 'justifyjustify',
  111. '|',
  112. 'link',
  113. 'unlink',
  114. '|',
  115. 'insertimage',
  116. 'emotion',
  117. 'scrawl',
  118. 'insertvideo',
  119. 'music',
  120. 'attachment',
  121. 'map',
  122. 'insertcode',
  123. 'pagebreak',
  124. '|',
  125. 'horizontal',
  126. 'inserttable',
  127. '|',
  128. 'print',
  129. 'preview',
  130. 'searchreplace',
  131. 'help'
  132. ]
  133. ];
  134. }
  135. }
  136. /**
  137. * 输出widget页面,注册相关JS代码。
  138. */
  139. public function run()
  140. {
  141. $id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
  142. $config = Json::encode($this->config);
  143. //ready部分代码,是为了缩略图管理。UEditor本身就很大,在后台直接加载大文件图片会很卡。
  144. $script = <<<UEDITOR
  145. var {$this->name} = UE.getEditor('{$id}',{$config});
  146. {$this->name}.ready(function(){
  147. this.addListener( "beforeInsertImage", function ( type, imgObjs ) {
  148. for(var i=0;i < imgObjs.length;i++){
  149. imgObjs[i].src = imgObjs[i].src.replace(".thumbnail","");
  150. }
  151. });
  152. });
  153. UEDITOR;
  154. $this->getView()->registerJs($script);
  155. if ($this->hasModel()) {
  156. return Html::activeTextarea($this->model, $this->attribute);
  157. } else {
  158. return Html::textarea($this->name, $this->value, ['id' => $id]);
  159. }
  160. }
  161. }