Froala编辑器是一款功能强大的富文本编辑器组件,支持图片上传、视频嵌入、代码高亮等丰富功能。
用法示例
<?php
use plugin\xbCode\builder\Builder;
use plugin\xbCode\builder\Renders\Form;
// 创建表单
Builder::form(function (Form $builder) {
// 基本Froala编辑器
$builder->addRowEditorFroala('content', '文章内容', '', function ($component) {
$component->placeholder('请输入文章内容');
});
// 自定义高度
$builder->addRowEditorFroala('description', '详细描述', '', function ($component) {
$component->options([
'height' => 300,
]);
});
// 配置工具栏按钮
$builder->addRowEditorFroala('news_content', '新闻内容', '', function ($component) {
$component->options([
'height' => 400,
'toolbarButtons' => [
'bold', 'italic', 'underline', '|',
'fontFamily', 'fontSize', 'color', '|',
'align', 'formatOL', 'formatUL', '|',
'insertLink', 'insertImage', 'insertVideo', '|',
'undo', 'redo'
]
]);
$component->buttons([
'bold', 'italic', 'underline', 'insertImage', 'insertLink'
]);
});
// 配置图片上传
$builder->addRowEditorFroala('article', '文章', '', function ($component) {
$component->receiver('/api/upload/image');
$component->fileField('file');
$component->options([
'height' => 500,
'imageUploadParam' => 'file',
'imageMaxSize' => 5 * 1024 * 1024, // 5MB
'imageAllowedTypes' => ['jpeg', 'jpg', 'png', 'gif']
]);
});
// 配置视频上传
$builder->addRowEditorFroala('video_content', '视频内容', '', function ($component) {
$component->videoReceiver('/api/upload/video');
$component->options([
'videoUploadParam' => 'file',
'videoMaxSize' => 50 * 1024 * 1024, // 50MB
'videoAllowedTypes' => ['mp4', 'webm', 'ogg']
]);
});
// 设置编辑器大小
$builder->addRowEditorFroala('large_content', '内容', '', function ($component) {
$component->size('lg'); // 可选:md, lg
$component->options([
'height' => 600,
]);
});
// 完整配置示例
$builder->addRowEditorFroala('full_content', '完整内容', '', function ($component) {
$component->vendor('froala');
$component->receiver('/api/upload/image');
$component->videoReceiver('/api/upload/video');
$component->fileField('file');
$component->size('lg');
$component->placeholder('请输入内容');
$component->options([
'height' => 500,
'language' => 'zh_cn',
'charCounterCount' => true,
'imageUploadParam' => 'file',
'videoUploadParam' => 'file',
'toolbarButtons' => [
'bold', 'italic', 'underline', 'strikeThrough', '|',
'fontFamily', 'fontSize', 'color', 'paragraphStyle', '|',
'paragraphFormat', 'align', 'formatOL', 'formatUL', '|',
'insertLink', 'insertImage', 'insertVideo', 'insertTable', '|',
'quote', 'insertHR', 'undo', 'redo', 'clearFormatting', '|',
'selectAll', 'html', 'fullscreen'
]
]);
});
});
组件参数
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| field | string | - | 字段名,必填 |
| title | string | - | 标题,必填 |
| value | mixed | '' | 默认值 |
| vendor | string | 'froala' | 编辑器类型,设置为froala |
| saveAsUbb | string | false | 是否保存为UBB格式 |
| receiver | string | - | 图片上传API地址 |
| videoReceiver | string | - | 视频上传API地址 |
| fileField | string | 'file' | 上传文件时的字段名 |
| size | string | - | 编辑器大小,可选:md、lg |
| options | array | [] | Froala编辑器的详细配置选项 |
| buttons | array | [] | 工具栏按钮配置(Froala专用) |
options常用配置
| 配置项 | 类型 | 说明 |
|---|---|---|
| height | int | 编辑器高度(像素) |
| language | string | 界面语言,如:zh_cn |
| charCounterCount | bool | 是否显示字符计数 |
| imageUploadParam | string | 图片上传参数名 |
| imageMaxSize | int | 图片最大大小(字节) |
| imageAllowedTypes | array | 允许的图片类型 |
| videoUploadParam | string | 视频上传参数名 |
| videoMaxSize | int | 视频最大大小(字节) |
| videoAllowedTypes | array | 允许的视频类型 |
| toolbarButtons | array | 工具栏按钮配置 |
| toolbarSticky | bool | 工具栏是否固定 |
| placeholderText | string | 占位提示文本 |
工具栏按钮
常用的工具栏按钮包括:
- 文本格式:bold, italic, underline, strikeThrough
- 字体:fontFamily, fontSize, color, backgroundColor
- 段落:paragraphFormat, paragraphStyle, align
- 列表:formatOL, formatUL, outdent, indent
- 插入:insertLink, insertImage, insertVideo, insertTable, insertHR
- 其他:quote, undo, redo, clearFormatting, selectAll, html, fullscreen
使用说明
- 基本使用:通过
addRowEditorFroala()方法添加Froala编辑器 - 编辑器类型:自动设置
vendor('froala')使用Froala编辑器 - 高度设置:通过options中的height参数设置编辑器高度
- 图片上传:使用
receiver()设置图片上传API - 视频上传:使用
videoReceiver()设置视频上传API,Froala编辑器特有功能 - 工具栏配置:通过
buttons()或 options中的toolbarButtons配置工具栏 - 大小控制:使用
size()方法设置编辑器整体大小(md或lg) - 语言设置:在options中设置language参数
- 文件限制:可在options中配置文件大小和类型限制
- 上传参数:使用
fileField()设置上传时的文件字段名 - 返回格式:服务端上传接口需返回标准格式,包含文件URL
适用场景:
- 新闻文章编辑
- 产品详情描述
- 博客内容编写
- 需要丰富格式的文本输入