UEditor.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace common\widgets\ueditor;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\helpers\Url;
  6. use yii\helpers\Json;
  7. use yii\helpers\ArrayHelper;
  8. use yii\widgets\InputWidget;
  9. use common\widgets\ueditor\assets\AppAsset;
  10. /**
  11. * 百度编辑器上传
  12. *
  13. * Class UEditor
  14. * @package common\widgets\ueditor
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class UEditor extends InputWidget
  18. {
  19. /**
  20. * ueditor参数配置
  21. *
  22. * @var array
  23. */
  24. public $config = [];
  25. /**
  26. * @var array
  27. */
  28. public $formData = [];
  29. /**
  30. * @throws \yii\base\InvalidConfigException
  31. */
  32. public function init()
  33. {
  34. parent::init();
  35. // 注册资源文件
  36. $asset = AppAsset::register($this->getView());
  37. $this->value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value;
  38. $this->name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
  39. //常用配置项
  40. $config = [
  41. 'serverUrl' => Url::to(['/ueditor/index']),
  42. 'selectUrl' => Url::to(['/files/selector']),
  43. 'UEDITOR_HOME_URL' => $asset->baseUrl . '/',
  44. 'lang' => 'zh-cn',
  45. 'initialFrameHeight' => 400,
  46. 'initialFrameWidth' => '100%',
  47. 'enableAutoSave' => false,
  48. 'toolbars' => [
  49. [
  50. 'fullscreen', 'source', 'undo', 'redo', '|',
  51. 'customstyle', 'paragraph', 'fontfamily', 'fontsize'
  52. ],
  53. [
  54. 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript',
  55. 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
  56. 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', '|',
  57. 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
  58. 'directionalityltr', 'directionalityrtl', 'indent', '|'
  59. ],
  60. [
  61. 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
  62. 'link', 'unlink', '|',
  63. 'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'insertcode', 'pagebreak', '|',
  64. 'horizontal', 'inserttable', '|',
  65. 'print', 'preview', 'searchreplace', 'help'
  66. ]
  67. ],
  68. ];
  69. if (!empty($this->config['toolbars'])) {
  70. unset($config['toolbars']);
  71. }
  72. // 切换上传驱动
  73. $storageDefault = Yii::$app->services->config->backendConfig('storage_default');
  74. $this->config = ArrayHelper::merge($config, $this->config);
  75. $this->formData = ArrayHelper::merge([
  76. // 'drive' => Yii::$app->params['UEditorUploadDrive'],
  77. 'drive' => $storageDefault,
  78. ], $this->formData);
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function run()
  84. {
  85. $id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id;
  86. $config = Json::encode($this->config);
  87. // 由于百度上传不能传递数组,所以转码成为json
  88. !isset($this->formData) && $this->formData = [];
  89. foreach ($this->formData as $key => &$formDatum) {
  90. if (!empty($formDatum) && is_array($formDatum)) {
  91. $formDatum = Json::encode($formDatum);
  92. }
  93. }
  94. $formData = Json::encode($this->formData);
  95. // ready部分代码,是为了缩略图管理。UEditor本身就很大,在后台直接加载大文件图片会很卡。
  96. $script = <<<JS
  97. UE.delEditor('{$id}');
  98. var ue = UE.getEditor('{$id}',{$config}).ready(function(){
  99. this.addListener( "beforeInsertImage", function ( type, imgObjs ) {
  100. for(var i=0;i < imgObjs.length;i++){
  101. imgObjs[i].src = imgObjs[i].src.replace(".thumbnail","");
  102. }
  103. });
  104. this.execCommand('serverparam', function(editor) {
  105. return {$formData};
  106. });
  107. });
  108. $('.UEditorTemplate').click(function () {
  109. var content = $(this).data('content');
  110. content = content.toString();
  111. if (content.length === 0) {
  112. return;
  113. }
  114. UE.getEditor('{$id}').focus();
  115. UE.getEditor('{$id}').execCommand('inserthtml', content);
  116. });
  117. JS;
  118. $this->getView()->registerJs($script);
  119. if ($this->hasModel()) {
  120. return Html::activeTextarea($this->model, $this->attribute, ['id' => $id]);
  121. }
  122. return Html::textarea(ArrayHelper::getValue($this->config, 'textarea', $this->name), $this->value,
  123. ['id' => $id]);
  124. }
  125. }
粤ICP备19079148号