nikitakls / yii2-editor-md

Markdown编辑器editor.md for Yii2

安装次数: 6,203

依赖: 1

建议者: 0

安全: 0

星标: 3

关注者: 1

分支: 2

开放问题: 1

类型:yii2-extension

v0.1.3 2019-04-13 17:56 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:09:15 UTC


README

editor.md for Yii2

安装

安装此扩展的首选方式是通过 Composer

运行以下命令

composer require nikitakls/yii2-editor-md

或者在您的 composer.json 文件的 require 部分添加

"nikitakls/yii2-editor-md": "*"

使用方法

扩展安装完成后,只需在代码中使用它即可

<?php

use nikitakls\markdown\EditorMdWidget;

?>
<?php 
echo $form->field($model, 'info_md')->widget(EditorMdWidget::className(), [
                'options' => [// html attributes
                    'id' => 'editor-markdown',
                ],
                'language' => 'ru',
                'clientOptions' => [
                    'height' => '300',
                    // 'previewTheme' => 'dark',
                    // 'editorTheme' => 'pastel-on-dark',
                    'markdown' => '',
                    //'codeFold' => true,
                    'syncScrolling' => true,
                    'saveHTMLToTextarea' => true,
                    'searchReplace' => true,
                    'watch' => true, 
                    'htmlDecode' => 'style,script,iframe|on*',
                    //'toolbar' => false,             
                    'placeholder' => 'MarkDown',
                    'previewCodeHighlight' => false,  
                    'emoji' => true,
                    'taskList' => true,
                    'tocm' => true, 
                    'tex' => true,   
                    'flowChart' => true,            
                    'sequenceDiagram' => true,     
                    'imageUpload' => true,
                    'imageFormats' => ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'],
                    'imageUploadURL' => Url::to(['file-upload', 'type' => 'md']),
                    'toolbarIcons' => [
                        "undo", "redo", "|",
                        "bold", "del", "italic", "list-ul", "list-ol", "hr", "|",
                        "code", "code-block", "|",
                        "image", "table", "link", "|",
                        "html-entities", "|",
                        "preview", "watch","|",
                        "help"
                    ],
                ]
            ]
) ?>

查看更多选项 [https://pandao.github.io/editor.md/en.html]

Markdown行为

该行为将HTML渲染的Markdown保存到活动记录模型属性中。您可以使用Markdown行为为ActiveRecord Model进行以下操作

use nikitakls\markdown\behaviors\MarkdownModelBehavior

class Content extents ActiveRecord{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            [
                'class' => MarkdownModelBehavior::className(),
                'sourceAttribute' => 'content',
                'destinationAttribute' => 'clean_content',
            ],
        ];
    }

}

Markdown上传文件动作

如果您想要有上传图片功能,请配置您的控制器

use nikitakls\markdown\actions\UploadFileAction; 

class ContentController extends Controller
{

    /**
     * @inheritdoc
     */
    public function actions()
    {

        return [
            'upload-image' => [
                'class' => UploadFileAction::class,
                'url' => '@fileUrl/origin/puzzle/',
                'path' => '@filePath/origin/puzzle/',
                'thumbPath' => '@filePath/thumb/puzzle/',
                'thumbUrl' => '@fileUrl/thumb/puzzle/',
                'thumbs' => [
                    'puzzle' => [
                        'width' => 480,
                        'height' => 320,
                        'main' => true
                    ],
                ],
                'unique' => true,
                'validatorOptions' => [
                    'maxWidth' => 1600,
                    'maxHeight' => 1200
                ]
            ],
        ];
    }