EditorWidget.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/16
  6. * Time: 上午1:57
  7. */
  8. namespace common\widgets\editor;
  9. use common\widgets\editor\editormd\Editormd;
  10. use common\widgets\editor\ueditor\UEditor;
  11. use vova07\imperavi\Widget;
  12. use yii\base\InvalidParamException;
  13. use yii\widgets\InputWidget;
  14. use yii\helpers\Url;
  15. class EditorWidget extends InputWidget
  16. {
  17. public $isMarkdown;
  18. public $typeEnum = ['redactor', 'markdown', 'ueditor'];
  19. protected $defaultRichType = 'ueditor';
  20. protected $defaultMarkdownType = 'markdown';
  21. /**
  22. * @var string 编辑器类型
  23. */
  24. public $type;
  25. public $inputOptions = ['rows' => 10];
  26. public function init()
  27. {
  28. if ($this->type === null) {
  29. if (isset($this->isMarkdown)) {
  30. if ($this->isMarkdown) {
  31. $this->type = $this->defaultMarkdownType;
  32. } else {
  33. $this->type = $this->defaultRichType;
  34. }
  35. } else {
  36. $this->type = $this->defaultRichType;
  37. }
  38. }
  39. if(!in_array($this->type, $this->typeEnum)) {
  40. throw new InvalidParamException('编辑器类型不存在');
  41. }
  42. $this->options = array_merge($this->inputOptions, $this->options);
  43. }
  44. public function run()
  45. {
  46. return call_user_func([$this, $this->type]);
  47. }
  48. protected function markdown()
  49. {
  50. if ($this->hasModel()) {
  51. return Editormd::widget([
  52. 'model' => $this->model,
  53. 'attribute' => $this->attribute,
  54. 'imageUploadRoute' => [
  55. '/upload/md-image-upload',
  56. 'fileparam' => 'editormd-image-file'
  57. ],
  58. 'options' => $this->options
  59. ]);
  60. } else {
  61. return Editormd::widget([
  62. 'name' => $this->name,
  63. 'value' => $this->value,
  64. 'imageUploadRoute' => [
  65. '/attachment/upload/md-image-upload',
  66. 'fileparam' => 'editormd-image-file'
  67. ],
  68. 'options' => $this->options
  69. ]);
  70. }
  71. }
  72. protected function redactor()
  73. {
  74. // $defaultOptions = [
  75. // 'lang' => 'zh_cn',
  76. // 'minHeight' => 200,
  77. // 'imageUpload' => Url::to(['/upload/redactor-image-upload']),
  78. // 'imageManagerJson' => Url::to(['/upload/redactor-images-get']),
  79. // 'fileManagerJson' => Url::to(['/upload/redactor-files-get']),
  80. // 'fileUpload' => Url::to(['/upload/redactor-file-upload']),
  81. // 'plugins' => [
  82. // 'clips',
  83. // 'fullscreen',
  84. // 'imagemanager',
  85. // 'filemanager'
  86. // ]
  87. // ];
  88. $defaultOptions = [
  89. 'lang' => 'zh_cn',
  90. 'minHeight' => 375,
  91. 'maxHeight' => 667,
  92. 'maxWidth' => 375,
  93. 'imageUpload' => Url::to(['/upload/redactor-image-upload']),
  94. 'imageManagerJson' => Url::to(['/upload/redactor-images-get']),
  95. // 'fileManagerJson' => Url::to(['/upload/redactor-files-get']),
  96. // 'fileUpload' => Url::to(['/upload/redactor-file-upload']),
  97. 'buttonsHide'=> ['html','link'],
  98. 'plugins' => [
  99. // 'fullscreen',
  100. 'imagemanager',
  101. 'fontsize',
  102. 'fontcolor',
  103. ],
  104. ];
  105. $options = array_merge($defaultOptions, $this->options);
  106. if ($this->hasModel()) {
  107. return Widget::widget([
  108. 'model' => $this->model,
  109. 'attribute' => $this->attribute,
  110. 'settings' => $options
  111. ]);
  112. } else {
  113. return Widget::widget([
  114. 'name' => $this->name,
  115. 'value' => $this->value,
  116. 'settings' => $options
  117. ]);
  118. }
  119. }
  120. protected function ueditor()
  121. {
  122. $clientOptions = [
  123. //定制菜单
  124. 'toolbars' => [
  125. [
  126. 'fullscreen', 'source', 'undo', 'redo', '|',
  127. 'fontsize', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',
  128. 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', 'simpleupload', 'insertimage', '|',
  129. 'insertorderedlist', 'insertunorderedlist', 'link', 'forecolor', 'backcolor', '|',
  130. 'lineheight', '|',
  131. 'indent', '|'
  132. ],
  133. ]
  134. ];
  135. if ($this->hasModel()) {
  136. return UEditor::widget([
  137. 'model' => $this->model,
  138. 'attribute' => $this->attribute,
  139. 'saveUrl' => ['/upload/ueditor'],
  140. 'options' => $this->options,
  141. 'clientOptions' => $clientOptions
  142. ]);
  143. } else {
  144. return UEditor::widget([
  145. 'name' => $this->name,
  146. 'value' => $this->value,
  147. 'saveUrl' => ['/upload/ueditor'],
  148. 'options' => $this->options,
  149. 'clientOptions' => $clientOptions
  150. ]);
  151. }
  152. }
  153. }