Editormd.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2016/12/7 9:51
  6. * Description:
  7. */
  8. namespace common\widgets\editor\editormd;
  9. use yii\helpers\Html;
  10. use yii\helpers\Json;
  11. use yii\web\JsExpression;
  12. use yii\widgets\InputWidget;
  13. class Editormd extends InputWidget
  14. {
  15. /**
  16. * @var array the HTML attributes for the widget container tag.
  17. */
  18. public $options = [];
  19. /**
  20. * editor options
  21. */
  22. public $clientOptions = [];
  23. public $imageUploadRoute = [
  24. '/upload/md-image-upload',
  25. 'fileparam' => 'editormd-image-file'
  26. ];
  27. public $mode = 'full';
  28. public function init()
  29. {
  30. parent::init();
  31. $this->clientOptions = array_merge([
  32. 'height' => '500',
  33. 'dialogLockScreen' => false,
  34. 'autoFocus' => false,
  35. // 'emoji' => true,
  36. 'watch' => false,
  37. 'placeholder' => '',
  38. 'syncScrolling' => 'single',
  39. 'imageUpload' => true,
  40. 'imageFormats' => ["jpg", "jpeg", "gif", "png", "bmp", "webp", "JPG", "JPEG", "GIF", "PNG", "BMP", "WEBP"],
  41. 'imageUploadURL' => \yii\helpers\Url::to($this->imageUploadRoute),
  42. 'toolbarIcons' => [
  43. "bold", "del", "italic", "quote", "uppercase", "lowercase", "|",
  44. "h1", "h2", "h3", "h4", "h5", "h6", "|",
  45. "list-ul", "list-ol", "hr", "|",
  46. "link", "image", "code", "preformatted-text", "code-block", "|",
  47. "watch", "preview", "fullscreen", "|", "help"
  48. ],
  49. 'onchange' => new JsExpression(<<<js
  50. function () {
  51. $('#' + this.id).blur();
  52. }
  53. js
  54. )
  55. ], $this->clientOptions);
  56. if ($this->mode == 'mini') {
  57. $this->clientOptions['toolbarIcons'] = ["bold", "list-ul", "list-ol", "link", "image", "code-block", "|", "help"];
  58. $this->clientOptions['height'] = '300';
  59. }
  60. }
  61. public function run()
  62. {
  63. echo Html::beginTag('div', ['id' => $this->getId()]);
  64. if ($this->hasModel()) {
  65. echo Html::activeTextarea($this->model, $this->attribute, ['id' => Html::getInputId($this->model, $this->attribute)]);
  66. } else {
  67. echo Html::textarea($this->name, $this->value, $this->options);
  68. }
  69. echo Html::endTag('div');
  70. $this->registerClientScript();
  71. }
  72. protected function registerClientScript()
  73. {
  74. $view = $this->getView();
  75. $editormd = EditormdAsset::register($view);
  76. $this->clientOptions['path'] = $editormd->baseUrl . '/lib/';
  77. $clientOptions = Json::encode($this->clientOptions);
  78. $js = 'var editor = editormd("' . $this->getId() . '", ' . $clientOptions . ');';
  79. $view->registerJs($js);
  80. }
  81. }