quanzo / yii2-editorjs
在表单中使用 editor.js。适用于 Yii2 的模块
1.0.0
2019-09-02 03:46 UTC
Requires
- php: >=7.0
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-29 05:28:45 UTC
README
在 Yii2 的表单中使用 https://editorjs.io/。
安装
通过 Composer 安装
composer require quanzo/yii2-editorjs
或者将以下内容添加到您的 composer.json
文件的 require
部分:
"quanzo/yii2-editorjs" : "*"
quanzo/yii2-editorjs
配置
$config = [ ... 'modules' => [ ... 'editorjs' => [ 'class' => 'x51\yii2\modules\editorjs\Module', /*'uploadDir' => 'upload', // the name of the directory in the root of the site to save information 'useDateDir' => true; // Pictures will be uploaded to folders with the current date 'useUserIdDir' => true; // pictures will be arranged in folders with user id 'maxImageWidth' => 1000; // image size limit 'uploadImageFromUrl' => true; // permission to download images by url (see download controller) 'classRender' => '\\x51\\yii2\\modules\\editorjs\\classes\\Render'; // class for converting json (editorjs) to html 'useCDN' => false; // use javascript download via cdn.jsdelivr.net // static page crete from editorjs 'staticPageDir' => '@app/pages/'; // path to the folder with static editorjs pages 'extWithDot' => '.json'; 'shortcode' => ''; // name of the shortcode processing module (if processing is needed) // if need use access filter 'as access' => [ 'class' => '\yii\filters\AccessControl', 'rules' => [ [ 'controllers' => ['editorjs/static', 'editorjs/upload'], 'roles' => ['admin'], 'allow' => true ], [ 'roles' => ['?'], 'allow' => true ] ] ]*/ ], ... ], ... ];
使用
显示静态页面
在控制器中使用 \x51\yii2\modules\editorjs\actions\StaticPageAction
以显示静态页面。
public function actions() { return [ 'spage' => [ 'class' => '\x51\yii2\modules\editorjs\actions\StaticPageAction', 'editorjsModuleName' => 'editorjs', 'on '.\x51\yii2\modules\editorjs\actions\StaticPageAction::EVENT_BEFORE_RUN => function ($event) { Yii::$app->view->registerAssetBundle('\x51\yii2\modules\editorjs\assets\RenderAssets'); } ], ]; }
表单中的 Editor.js
$form = ActiveForm::begin();
echo $form->field($model, 'post_type')->textInput(['maxlength' => true]);
.....
Yii::$app->getModule('editorjs')->editorjs('editorjs-container', $model->formName(), 'textNameFieldInModel', $model->textNameFieldInModel);
.....
ActiveForm::end();
可以用于任何表单。结果将保存在一个隐藏字段中。字段名为 formName[textNameFieldInModel]
。如果您没有指定表单名称,则结果将保存在 textNameFieldInModel
字段中。