Markdown.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Support\JavaScript;
  7. /**
  8. * @see https://pandao.github.io/editor.md/
  9. */
  10. class Markdown extends Field
  11. {
  12. protected static $css = [
  13. '@admin/dcat/plugins/editor-md/css/editormd.min.css',
  14. ];
  15. protected static $js = [
  16. '@admin/dcat/plugins/editor-md/lib/raphael.min.js',
  17. '@admin/dcat/plugins/editor-md/editormd.min.js',
  18. ];
  19. /**
  20. * 编辑器配置.
  21. *
  22. * @var array
  23. */
  24. protected $options = [
  25. 'height' => 500,
  26. 'codeFold' => true,
  27. 'saveHTMLToTextarea' => true, // 保存 HTML 到 Textarea
  28. 'searchReplace' => true,
  29. 'emoji' => true,
  30. 'taskList' => true,
  31. 'tocm' => true, // Using [TOCM]
  32. 'tex' => true, // 开启科学公式TeX语言支持,默认关闭
  33. 'flowChart' => false, // 流程图支持,默认关闭
  34. 'sequenceDiagram' => false, // 时序/序列图支持,默认关闭,
  35. 'imageUpload' => true,
  36. 'autoFocus' => true,
  37. ];
  38. protected $language = '@admin/dcat/plugins/editor-md/languages/en.js';
  39. protected $disk;
  40. protected $imageUploadDirectory = 'markdown/images';
  41. /**
  42. * 开启 HTML 标签解析.
  43. * style,script,iframe|on*.
  44. *
  45. * @param string $decode
  46. *
  47. * @return $this
  48. */
  49. public function htmlDecode($decode)
  50. {
  51. $this->options['htmlDecode'] = &$decode;
  52. return $this;
  53. }
  54. /**
  55. * 设置编辑器容器高度.
  56. *
  57. * @param int $height
  58. *
  59. * @return $this
  60. */
  61. public function height($height)
  62. {
  63. $this->options['height'] = $height;
  64. return $this;
  65. }
  66. /**
  67. * 设置文件上传存储配置.
  68. *
  69. * @param string $disk
  70. *
  71. * @return $this
  72. */
  73. public function disk(string $disk)
  74. {
  75. $this->disk = $disk;
  76. return $this;
  77. }
  78. /**
  79. * 设置图片上传文件夹.
  80. *
  81. * @param string $dir
  82. *
  83. * @return $this
  84. */
  85. public function imageDirectory(string $dir)
  86. {
  87. $this->imageUploadDirectory = $dir;
  88. return $this;
  89. }
  90. /**
  91. * 自定义图片上传接口.
  92. *
  93. * @param string $url
  94. *
  95. * @return $this
  96. */
  97. public function imageUrl(string $url)
  98. {
  99. return $this->options(['imageUploadURL' => $this->formatUrl(admin_url($url))]);
  100. }
  101. /**
  102. * 设置语言包路径.
  103. *
  104. * @param string $url
  105. *
  106. * @return $this
  107. */
  108. public function languageUrl(string $url)
  109. {
  110. $this->language = $url;
  111. return $this;
  112. }
  113. /**
  114. * 初始化js.
  115. */
  116. protected function setUpScript()
  117. {
  118. $this->options['path'] = admin_asset('@admin/dcat/plugins/editor-md/lib').'/';
  119. $this->options['name'] = $this->column;
  120. $this->options['placeholder'] = $this->placeholder();
  121. $this->options['readonly'] = ! empty($this->attributes['readonly']) || ! empty($this->attributes['disabled']);
  122. if (empty($this->options['imageUploadURL'])) {
  123. $this->options['imageUploadURL'] = $this->defaultImageUploadUrl();
  124. }
  125. $this->options['onload'] = JavaScript::make(
  126. <<<JS
  127. function () {
  128. this.setMarkdown($('#{$this->id}-template').text());
  129. }
  130. JS
  131. );
  132. if (config('app.locale') !== 'zh-CN') {
  133. Admin::js($this->language);
  134. }
  135. $opts = JavaScript::format($this->options);
  136. $this->script = "editormd(\"{$this->id}\", {$opts});";
  137. }
  138. /**
  139. * @return string
  140. */
  141. protected function defaultImageUploadUrl()
  142. {
  143. return $this->formatUrl(route(admin_api_route('editor-md.upload')));
  144. }
  145. /**
  146. * @param string $url
  147. *
  148. * @return string
  149. */
  150. protected function formatUrl(string $url)
  151. {
  152. return Helper::urlWithQuery(
  153. $url,
  154. [
  155. '_token' => csrf_token(),
  156. 'disk' => $this->disk,
  157. 'dir' => $this->imageUploadDirectory,
  158. ]
  159. );
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function render()
  165. {
  166. $this->setUpScript();
  167. Admin::style('.editormd-fullscreen {z-index: 99999999;}');
  168. return parent::render();
  169. }
  170. }