Editor.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * @return $this
  44. */
  45. public function disk(string $disk)
  46. {
  47. $this->disk = $disk;
  48. return $this;
  49. }
  50. /**
  51. * 设置图片上传文件夹.
  52. *
  53. * @param string $dir
  54. * @return $this
  55. */
  56. public function imageDirectory(string $dir)
  57. {
  58. $this->imageUploadDirectory = $dir;
  59. return $this;
  60. }
  61. /**
  62. * 自定义图片上传接口.
  63. *
  64. * @param string $url
  65. * @return $this
  66. */
  67. public function imageUrl(string $url)
  68. {
  69. return $this->mergeOptions(['images_upload_url' => $this->formatUrl(admin_url($url))]);
  70. }
  71. /**
  72. * 设置语言包url.
  73. *
  74. * @param string $url
  75. * @return $this
  76. */
  77. public function languageUrl(string $url)
  78. {
  79. return $this->mergeOptions(['language_url' => $url]);
  80. }
  81. /**
  82. * 设置编辑器高度.
  83. *
  84. * @param int $height
  85. * @return $this
  86. */
  87. public function height(int $height)
  88. {
  89. return $this->mergeOptions(['min_height' => $height]);
  90. }
  91. /**
  92. * @return array
  93. */
  94. protected function formatOptions()
  95. {
  96. $this->options['language'] = config('app.locale');
  97. $this->options['readonly'] = ! empty($this->attributes['readonly']) || ! empty($this->attributes['disabled']);
  98. if (empty($this->options['images_upload_url'])) {
  99. $this->options['images_upload_url'] = $this->defaultImageUploadUrl();
  100. }
  101. return $this->options;
  102. }
  103. /**
  104. * @return string
  105. */
  106. protected function defaultImageUploadUrl()
  107. {
  108. return $this->formatUrl(route(admin_api_route_name('tinymce.upload')));
  109. }
  110. /**
  111. * @param string $url
  112. * @return string
  113. */
  114. protected function formatUrl(string $url)
  115. {
  116. return Helper::urlWithQuery(
  117. $url,
  118. [
  119. '_token' => csrf_token(),
  120. 'disk' => $this->disk,
  121. 'dir' => $this->imageUploadDirectory,
  122. ]
  123. );
  124. }
  125. /**
  126. * @return string
  127. */
  128. public function render()
  129. {
  130. $this->addVariables([
  131. 'options' => $this->formatOptions(),
  132. ]);
  133. return parent::render();
  134. }
  135. }