wangEditor 工具栏配置
时间: 2023-12-01 14:05:45 浏览: 39
wangEditor 的工具栏配置可以通过以下两种方式实现:
1. 使用默认配置
在实例化 wangEditor 编辑器时,可以不传入任何参数,这样编辑器就会使用默认的工具栏配置。默认配置包含常用的工具按钮,如加粗、斜体、下划线、删除线、字体颜色、背景色、插入链接、插入图片、插入表格等。
```javascript
// 实例化编辑器
const editor = new wangEditor('#editor')
```
2. 自定义配置
如果要自定义工具栏配置,需要在实例化编辑器时传入一个配置对象,其中的 `toolbar` 属性可以用于配置工具栏,它是一个数组,每个元素表示一个工具按钮,可以是字符串或对象。
```javascript
// 自定义工具栏配置
const config = {
// 工具栏配置
toolbar: [
'bold', 'italic', 'underline', 'strikethrough', '|',
'head', 'list', 'indent', '|',
'link', 'image', 'table', '|',
'quote', 'code', 'hr', 'emoji', '|',
'undo', 'redo'
]
}
// 实例化编辑器
const editor = new wangEditor('#editor', config)
```
上面代码中,工具栏配置数组中的每个元素可以是字符串或对象。如果是字符串,则表示使用 wangEditor 内置的工具按钮;如果是对象,则需要指定以下属性:
- `name`:工具按钮名称,必须与 wangEditor 内置的名称不同;
- `title`:工具按钮标题;
- `type`:工具按钮类型,可选值为 `'btn'`(普通按钮)或 `'dropList'`(下拉列表);
- `options`:当 `type` 为 `'dropList'` 时,该属性表示下拉列表中的选项,是一个数组,每个元素是一个对象,包含 `name` 和 `title` 两个属性。
下面是一个自定义工具栏配置的例子:
```javascript
const config = {
// 工具栏配置
toolbar: [
'bold', 'italic', 'underline', 'strikethrough', '|',
{ name: 'head', title: '标题', type: 'dropList', options: [
{ name: 'h1', title: '标题1' },
{ name: 'h2', title: '标题2' },
{ name: 'h3', title: '标题3' }
]},
{ name: 'list', title: '列表', type: 'dropList', options: [
{ name: 'ol', title: '有序列表' },
{ name: 'ul', title: '无序列表' }
]},
'indent', '|',
{ name: 'quote', title: '引用', type: 'dropList', options: [
{ name: 'quote', title: '普通引用' },
{ name: 'code', title: '代码引用' }
]},
'link', 'image', 'table', '|',
'undo', 'redo'
]
}
// 实例化编辑器
const editor = new wangEditor('#editor', config)
```