Editor.php 3.4 KB

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