Markdown.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace common\widgets\markdown;
  3. use yii;
  4. use yii\helpers\Json;
  5. use yii\helpers\Html;
  6. use common\helpers\Url;
  7. use common\helpers\ArrayHelper;
  8. /**
  9. * Class Markdown
  10. * @package common\widgets\markdown
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class Markdown extends yii\widgets\InputWidget
  14. {
  15. /**
  16. * 上传路径 + 驱动
  17. *
  18. * @var
  19. */
  20. public $server;
  21. /**
  22. * @var string
  23. */
  24. public $id = 'markdown-editor';
  25. /**
  26. * @throws yii\base\InvalidConfigException
  27. */
  28. public function init()
  29. {
  30. if (!$this->server) {
  31. $this->server = Url::toRoute([
  32. '/files/images-markdown',
  33. 'drive' => Yii::$app->params['uploadConfig']['images']['drive'],
  34. ]);
  35. }
  36. $this->options = ArrayHelper::merge([
  37. 'width' => "100%",
  38. 'height' => 500,
  39. 'emoji' => false,
  40. 'taskList' => true,
  41. 'flowChart' => true, // 流程图
  42. 'sequenceDiagram' => true, // 序列图
  43. 'tex' => true, // 科学公式
  44. 'imageUpload' => true,
  45. 'imageUploadURL' => $this->server,
  46. ], $this->options);
  47. parent::init();
  48. }
  49. /**
  50. * @return string
  51. */
  52. public function run()
  53. {
  54. // 注册资源
  55. MarkdownAsset::register($this->view);
  56. $baseUrl = Yii::$app->view->assetBundles[MarkdownAsset::class]->baseUrl;
  57. $this->options['path'] = $baseUrl . '/lib/';
  58. $options = Json::htmlEncode($this->options);
  59. $js = <<<JS
  60. $(function() {
  61. var options = $options;
  62. options['toolbarIcons'] = function() {
  63. // Or return editormd.toolbarModes[name]; // full, simple, mini
  64. // Using "||" set icons align right.
  65. return [
  66. "bold", "del", "italic", "quote", "|",
  67. "h1", "h2", "h3", "h4", "h5", "h6", "|",
  68. "list-ul", "list-ol", "hr", "|",
  69. "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "html-entities", "pagebreak", "|",
  70. "ucwords", "uppercase", "lowercase", "|",
  71. "goto-line", "watch", "preview", "fullscreen", "clear", "search", "|",
  72. "help", "info", 'customIcon', "undo", "redo"
  73. ]
  74. };
  75. // customIcon 自定义草稿箱加载
  76. // 自定义方法的图标 指定一个FontAawsome的图标类
  77. options['toolbarIconsClass'] = {
  78. customIcon: "fa-paste"
  79. };
  80. // 没有图标可以插入内容,字符串或HTML标签
  81. options['toolbarIconTexts'] = {
  82. customIcon: "从草稿箱加载"
  83. };
  84. // 图标的title
  85. options['lang'] = {
  86. toolbar: {
  87. customIcon: "从草稿箱加载"
  88. }
  89. };
  90. // 自定义工具栏按钮的事件处理
  91. options['toolbarHandlers'] = {
  92. customIcon: function(){
  93. // 读取缓存内容
  94. editor.CodeAutoSaveGetCache();
  95. }
  96. };
  97. // 自定义工具栏按钮的事件处理
  98. options['onload'] = function() {
  99. // 引入插件 执行监听方法
  100. editormd.loadPlugin("$baseUrl/plugins/code-auto-save/code-auto-save", function() {
  101. // 初始化插件 实现监听
  102. editor.CodeAutoSave();
  103. });
  104. // 微信/qq里面复制文件是可以的,在桌面直接复制文件这种方法浏览器不支持
  105. editormd.loadPlugin("$baseUrl/plugins/image-handle-paste/image-handle-paste", function() {
  106. // 初始化插件 实现监听
  107. editor.imagePaste();
  108. });
  109. };
  110. var editor = editormd("{$this->id}", options);
  111. // 清理缓存
  112. $(this).on("beforeSubmit", function (event) {
  113. // 删除缓存
  114. editor.CodeAutoSaveDelCache();
  115. // 清空缓存的文档内容
  116. editor.CodeAutoSaveEmptyCacheContent();
  117. // 自定义设置缓存
  118. // editor.CodeAutoSaveSetCache('缓存内容');
  119. });
  120. // 模板插入
  121. $('.editorTemplate').click(function () {
  122. var content = $(this).data('content');
  123. content = content.toString();
  124. if (content.length === 0) {
  125. return;
  126. }
  127. editor.insertValue(content);
  128. editor.focus();
  129. });
  130. $('.editormd-preview-close-btn').attr('style', 'display: none;')
  131. });
  132. JS;
  133. $this->view->registerJs($js);
  134. if ($this->hasModel()) {
  135. $html = Html::activeTextarea($this->model, $this->attribute, $this->options);
  136. } else {
  137. $html = Html::textarea($this->name, $this->value, $this->options);
  138. }
  139. return Html::tag('div', $html, [
  140. 'id' => $this->id,
  141. ]);
  142. }
  143. }
粤ICP备19079148号