Editor.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Support\JavaScript;
  6. /**
  7. * TinyMCE editor.
  8. *
  9. * @see https://www.tiny.cloud/docs
  10. * @see http://tinymce.ax-z.cn/
  11. */
  12. class Editor extends Field
  13. {
  14. protected static $js = [
  15. '@tinymce',
  16. ];
  17. protected $options = [
  18. 'plugins' => [
  19. 'advlist',
  20. 'autolink',
  21. 'link',
  22. 'image',
  23. 'media',
  24. 'lists',
  25. 'preview',
  26. 'code',
  27. 'help',
  28. 'fullscreen',
  29. 'table',
  30. 'autoresize',
  31. 'codesample',
  32. ],
  33. 'toolbar' => [
  34. 'undo redo | preview fullscreen | styleselect | fontsizeselect bold italic underline strikethrough forecolor backcolor | link image media blockquote removeformat codesample',
  35. 'alignleft aligncenter alignright alignjustify| indent outdent bullist numlist table subscript superscript | code',
  36. ],
  37. 'min_height' => 400,
  38. 'save_enablewhendirty' => true,
  39. ];
  40. protected $disk;
  41. protected $imageUploadDirectory = 'tinymce/images';
  42. /**
  43. * 设置文件上传存储配置.
  44. *
  45. * @param string $disk
  46. *
  47. * @return $this
  48. */
  49. public function disk(string $disk)
  50. {
  51. $this->disk = $disk;
  52. return $this;
  53. }
  54. /**
  55. * 设置图片上传文件夹.
  56. *
  57. * @param string $dir
  58. *
  59. * @return $this
  60. */
  61. public function imageDirectory(string $dir)
  62. {
  63. $this->imageUploadDirectory = $dir;
  64. return $this;
  65. }
  66. /**
  67. * 自定义图片上传接口.
  68. *
  69. * @param string $url
  70. *
  71. * @return $this
  72. */
  73. public function imageUrl(string $url)
  74. {
  75. return $this->options(['images_upload_url' => $this->formatUrl(admin_url($url))]);
  76. }
  77. /**
  78. * 设置语言包url.
  79. *
  80. * @param string $url
  81. *
  82. * @return $this
  83. */
  84. public function languageUrl(string $url)
  85. {
  86. return $this->options(['language_url' => $url]);
  87. }
  88. /**
  89. * 设置编辑器高度.
  90. *
  91. * @param int $height
  92. *
  93. * @return $this
  94. */
  95. public function height(int $height)
  96. {
  97. return $this->options(['min_height' => $height]);
  98. }
  99. /**
  100. * @return string
  101. */
  102. protected function formatOptions()
  103. {
  104. $this->options['selector'] = '#'.$this->id;
  105. $this->options['language'] = config('app.locale');
  106. $this->options['readonly'] = ! empty($this->attributes['readonly']) || ! empty($this->attributes['disabled']);
  107. if (empty($this->options['images_upload_url'])) {
  108. $this->options['images_upload_url'] = $this->defaultImageUploadUrl();
  109. }
  110. // 内容更改后保存到隐藏表单
  111. $this->options['init_instance_callback'] = JavaScript::make($this->buildSaveContentScript());
  112. return JavaScript::format($this->options);
  113. }
  114. /**
  115. * @return string
  116. */
  117. protected function defaultImageUploadUrl()
  118. {
  119. return $this->formatUrl(route('dcat.api.tinymce.upload'));
  120. }
  121. /**
  122. * @param string $url
  123. *
  124. * @return string
  125. */
  126. protected function formatUrl(string $url)
  127. {
  128. return Helper::urlWithQuery(
  129. $url,
  130. [
  131. '_token' => csrf_token(),
  132. 'disk' => $this->disk,
  133. 'dir' => $this->imageUploadDirectory,
  134. ]
  135. );
  136. }
  137. /**
  138. * @return string
  139. */
  140. protected function buildSaveContentScript()
  141. {
  142. return <<<JS
  143. function (editor) {
  144. editor.on('Change', function(e) {
  145. $('{$this->getElementClassSelector()}').val(e.level.content);
  146. });
  147. }
  148. JS;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function render()
  154. {
  155. $this->script = <<<JS
  156. tinymce.init({$this->formatOptions()})
  157. JS;
  158. return parent::render();
  159. }
  160. }