Editor.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 'save_enablewhendirty' => true,
  38. ];
  39. protected $disk;
  40. protected $imageUploadDirectory = 'tinymce/images';
  41. /**
  42. * 设置文件上传存储配置.
  43. *
  44. * @param string $disk
  45. *
  46. * @return $this
  47. */
  48. public function disk(string $disk)
  49. {
  50. $this->disk = $disk;
  51. return $this;
  52. }
  53. /**
  54. * 设置图片上传文件夹.
  55. *
  56. * @param string $dir
  57. *
  58. * @return $this
  59. */
  60. public function imageDirectory(string $dir)
  61. {
  62. $this->imageUploadDirectory = $dir;
  63. return $this;
  64. }
  65. /**
  66. * 自定义图片上传接口.
  67. *
  68. * @param string $url
  69. *
  70. * @return $this
  71. */
  72. public function imageUrl(string $url)
  73. {
  74. return $this->options(['images_upload_url' => admin_url($url)]);
  75. }
  76. /**
  77. * 设置语言包url.
  78. *
  79. * @param string $url
  80. *
  81. * @return $this
  82. */
  83. public function languageUrl(string $url)
  84. {
  85. return $this->options(['language_url' => $url]);
  86. }
  87. /**
  88. * @return string
  89. */
  90. protected function formatOptions()
  91. {
  92. $this->options['selector'] = '#'.$this->id;
  93. $this->options['language'] = config('app.locale');
  94. $this->options['readonly'] = ! empty($this->attributes['readonly']) || ! empty($this->attributes['disabled']);
  95. if (empty($this->options['images_upload_url'])) {
  96. $this->options['images_upload_url'] = $this->defaultImageUploadUrl();
  97. }
  98. // 内容更改后保存到隐藏表单
  99. $this->options['init_instance_callback'] = JavaScript::make($this->buildSaveContentScript());
  100. return JavaScript::format($this->options);
  101. }
  102. /**
  103. * @return string
  104. */
  105. protected function defaultImageUploadUrl()
  106. {
  107. return Helper::urlWithQuery(
  108. route('dcat.api.tinymce.upload'),
  109. [
  110. '_token' => csrf_token(),
  111. 'disk' => $this->disk,
  112. 'dir' => $this->imageUploadDirectory,
  113. ]
  114. );
  115. }
  116. /**
  117. * @return string
  118. */
  119. protected function buildSaveContentScript()
  120. {
  121. return <<<JS
  122. function (editor) {
  123. editor.on('Change', function(e) {
  124. $('{$this->getElementClassSelector()}').val(e.level.content);
  125. });
  126. }
  127. JS;
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function render()
  133. {
  134. $this->script = <<<JS
  135. tinymce.remove();
  136. tinymce.init({$this->formatOptions()})
  137. JS;
  138. return parent::render();
  139. }
  140. }