*/ class Markdown extends yii\widgets\InputWidget { /** * 上传路径 + 驱动 * * @var */ public $server; /** * @var string */ public $id = 'markdown-editor'; /** * @throws yii\base\InvalidConfigException */ public function init() { if (!$this->server) { $this->server = Url::toRoute([ '/files/images-markdown', 'drive' => Yii::$app->params['uploadConfig']['images']['drive'], ]); } $this->options = ArrayHelper::merge([ 'width' => "100%", 'height' => 500, 'emoji' => false, 'taskList' => true, 'flowChart' => true, // 流程图 'sequenceDiagram' => true, // 序列图 'tex' => true, // 科学公式 'imageUpload' => true, 'imageUploadURL' => $this->server, ], $this->options); parent::init(); } /** * @return string */ public function run() { // 注册资源 MarkdownAsset::register($this->view); $baseUrl = Yii::$app->view->assetBundles[MarkdownAsset::class]->baseUrl; $this->options['path'] = $baseUrl . '/lib/'; $options = Json::htmlEncode($this->options); $js = <<id}", options); // 清理缓存 $(this).on("beforeSubmit", function (event) { // 删除缓存 editor.CodeAutoSaveDelCache(); // 清空缓存的文档内容 editor.CodeAutoSaveEmptyCacheContent(); // 自定义设置缓存 // editor.CodeAutoSaveSetCache('缓存内容'); }); // 模板插入 $('.editorTemplate').click(function () { var content = $(this).data('content'); content = content.toString(); if (content.length === 0) { return; } editor.insertValue(content); editor.focus(); }); $('.editormd-preview-close-btn').attr('style', 'display: none;') }); JS; $this->view->registerJs($js); if ($this->hasModel()) { $html = Html::activeTextarea($this->model, $this->attribute, $this->options); } else { $html = Html::textarea($this->name, $this->value, $this->options); } return Html::tag('div', $html, [ 'id' => $this->id, ]); } }