| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Form\Field;
- use Dcat\Admin\Support\Helper;
- /**
- * TinyMCE editor.
- *
- * @see https://www.tiny.cloud/docs
- * @see http://tinymce.ax-z.cn/
- */
- class Editor extends Field
- {
- protected $options = [
- 'plugins' => [
- 'advlist',
- 'autolink',
- 'link',
- 'image',
- 'media',
- 'lists',
- 'preview',
- 'code',
- 'help',
- 'fullscreen',
- 'table',
- 'autoresize',
- 'codesample',
- ],
- 'toolbar' => [
- 'undo redo | preview fullscreen | styleselect | fontsizeselect bold italic underline strikethrough forecolor backcolor | link image media blockquote removeformat codesample',
- 'alignleft aligncenter alignright alignjustify| indent outdent bullist numlist table subscript superscript | code',
- ],
- 'min_height' => 400,
- 'save_enablewhendirty' => true,
- 'convert_urls' => false,
- ];
- protected $disk;
- protected $imageUploadDirectory = 'tinymce/images';
- /**
- * 设置文件上传存储配置.
- *
- * @param string $disk
- *
- * @return $this
- */
- public function disk(string $disk)
- {
- $this->disk = $disk;
- return $this;
- }
- /**
- * 设置图片上传文件夹.
- *
- * @param string $dir
- *
- * @return $this
- */
- public function imageDirectory(string $dir)
- {
- $this->imageUploadDirectory = $dir;
- return $this;
- }
- /**
- * 自定义图片上传接口.
- *
- * @param string $url
- *
- * @return $this
- */
- public function imageUrl(string $url)
- {
- return $this->mergeOptions(['images_upload_url' => $this->formatUrl(admin_url($url))]);
- }
- /**
- * 设置语言包url.
- *
- * @param string $url
- *
- * @return $this
- */
- public function languageUrl(string $url)
- {
- return $this->mergeOptions(['language_url' => $url]);
- }
- /**
- * 设置编辑器高度.
- *
- * @param int $height
- *
- * @return $this
- */
- public function height(int $height)
- {
- return $this->mergeOptions(['min_height' => $height]);
- }
- /**
- * @return array
- */
- protected function formatOptions()
- {
- $this->options['language'] = config('app.locale');
- $this->options['readonly'] = ! empty($this->attributes['readonly']) || ! empty($this->attributes['disabled']);
- if (empty($this->options['images_upload_url'])) {
- $this->options['images_upload_url'] = $this->defaultImageUploadUrl();
- }
- return $this->options;
- }
- /**
- * @return string
- */
- protected function defaultImageUploadUrl()
- {
- return $this->formatUrl(route(admin_api_route('tinymce.upload')));
- }
- /**
- * @param string $url
- *
- * @return string
- */
- protected function formatUrl(string $url)
- {
- return Helper::urlWithQuery(
- $url,
- [
- '_token' => csrf_token(),
- 'disk' => $this->disk,
- 'dir' => $this->imageUploadDirectory,
- ]
- );
- }
- /**
- * @return string
- */
- public function render()
- {
- $this->addVariables([
- 'options' => $this->formatOptions(),
- ]);
- return parent::render();
- }
- }
|