Laravel-admin集成UEditor

2018-11-22  本文已影响0人  易澜湾

先下载Ueditor 并解压到/public目录,比如放在/public/packages/目录下
然后新建扩展文件app/Admin/Extensions/UEditor.php

  <?php
 namespace App\Admin\Extensions;
 use Encore\Admin\Form\Field;

class UEditor extends Field
{
protected $view = 'admin.u-editor';
protected static $css = [];
protected static $js = [
    '/packages/utf8-php/ueditor.config.js',
    'packages/utf8-php/ueditor.all.js'
];

public function render()
{
    $name = $this->formatName($this->column);

    $this->script = <<<EOT
    //解决第二次进入加载不出来的问题
    UE.delEditor("container");
    var ue = UE.getEditor('container',{
    elementPathEnabled: false,
    enableContextMenu: false,
    autoClearEmptyNode: true,
    wordCount: false,
    imagePopup: false,
     autotypeset: {indent: true, imageBlockLine: 'center'}
    });
    ue.ready(function() {
      ue.execCommand('serverparam', '_token', '{{ csrf_token() }}');

    });

EOT;
      return parent::render();
  }
}

新建view resources/views/admin/u-editor.blade.php

<div class="form-group {!! !$errors->has($errorKey) ?: 'has-error' !!}">
  <label for="{{$id}}" class="col-sm-2 control-label">{{$label}}</label>
  <div class="col-sm-8">
    @include('admin::form.error')
    <textarea type='text/plain' style="height:400px;" id='container' id="{{$id}}" name="{{$name}}" placeholder="{{ $placeholder }}" {!! $attributes !!}  class='ueditor'>
        {!! old($column, $value) !!}
    </textarea>
    @include('admin::form.help-block')
  </div>
</div>

然后在app/Admin/bootstrap.php中引入扩展:

use App\Admin\Extensions\UEditor;
use Encore\Admin\Form;
Form::extend('ueditor', UEditor::class);

然后就能在form中使用了:

    $form->ueditor('content');
上一篇 下一篇

猜你喜欢

热点阅读