::: sourceCode
## editor
> issues: https://issues.dcloud.net.cn/?mid=component.form-component.editor
> 源码: https://github.com/dcloudio/uni-app/tree/uni-app-x-alpha/src/uni_modules/uni-editor/components/editor
:::
富文本编辑器,可以对图片、文字进行编辑。
### 兼容性
### 属性
| 名称 | 类型 | 默认值 | 兼容性 | 描述 |
| :- | :- | :- | :-: | :- |
| read-only | boolean | false | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android 系统版本: 7.1.1; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 设置编辑器为只读 |
| placeholder | string | "" | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android 系统版本: 7.1.1; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 提示信息 |
| show-img-size | boolean | false | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android 系统版本: 7.1.1; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 点击图片时显示图片大小控件 |
| show-img-toolbar | boolean | false | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android 系统版本: 7.1.1; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 点击图片时显示工具栏控件 |
| show-img-resize | boolean | false | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android 系统版本: 7.1.1; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 点击图片时显示修改尺寸控件 |
| enable-formats | Array.<string> | | Web: x; 微信小程序: 4.41; 支付宝小程序: x; Android: x; iOS: x; HarmonyOS: x | *(Array.<string>)*
编辑器允许的名单内的格式 |
| type | "none" | null | Web: 4.0; 微信小程序: x; 支付宝小程序: x; Android 系统版本: 8.0.0; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 输入类型,暂只支持
"none": 聚焦时不弹出键盘 |
| name | string | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 表单的控件名称,作为键值对的一部分与表单(form组件)一同提交 |
| @ready | (event: [UniEvent](/component/common.md#unievent)) => void | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 编辑器初始化完成时触发 |
| @focus | (event: [UniEvent](/component/common.md#unievent)) => void | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 编辑器聚焦时触发 |
| @blur | (event: [UniEvent](/component/common.md#unievent)) => void | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 编辑器失去焦点时触发 |
| @input | (event: [UniEvent](/component/common.md#unievent)) => void | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 编辑器内容改变时触发 |
| @statuschange | (event: [UniEvent](/component/common.md#unievent)) => void | | Web: 4.0; 微信小程序: 4.41; 支付宝小程序: x; Android: 5.08; iOS: 5.08; HarmonyOS: 5.08 | 通过 Context 方法改变编辑器内样式时触发,返回选区已设置的样式 |
#### type 的属性描述
| 合法值 |
| :- |
| none |
### 上下文对象API
editor组件有上下文对象,api为[uni.createEditorContextAsync()](../api/create-editor-context-async.md)。
给editor组件设一个id属性,将id的值传入uni.createEditorContextAsync(),即可得到editor组件的上下文对象,进一步可使用对象上的方法。
### 示例
示例为[hello uni-app x alpha分支](https://gitcode.com/dcloud/hello-uni-app-x/blob/prod_alpha/pages/component/editor/editor.uvue),与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例[另见](https://gitcode.com/dcloud/hello-uni-app-x/blob/master//pages/component/editor/editor.uvue)
::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/component/editor/editor
> appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/component/editor/editor
>示例
```vue
read-only: {{ data.readOnly }}
type: {{ getTypeLabel() }}
X: {{ data.editorX }}
Y: {{ data.editorY }}
width: {{ data.editorWidth }}
height: {{ data.editorHeight }}
A
{{""}}
{{""}}
{{""}}
{{""}}
{{""}}
{{""}}
@
可动态修改属性
初始化生效属性
placeholder 与 show-img-* 调整后不会直接作用到当前 editor,请点击下方按钮重建后查看。
当前应用中的 editor 配置
placeholder:{{ data.appliedPlaceholder }}
show-img-size:{{ data.appliedShowImgSize }}
show-img-toolbar:{{ data.appliedShowImgToolbar }}
show-img-resize:{{ data.appliedShowImgResize }}
事件示例
@ready:{{ data.readyCount }}
@focus:{{ data.focusCount }}
@blur:{{ data.blurCount }}
@input:{{ data.inputCount }}
@statuschange:{{ data.statusChangeCount }}
最近状态:{{ data.statusSummary }}
{{ item }}
等待事件触发...
```
:::
### 参见
- [参见uni-app相关文档](https://uniapp.dcloud.io/component/editor.html)
# tips
- 虽然editor组件被分类到form组件,但并不能在form的submit中提交
- 如果要做 im 输入类功能,需要使用 [onKeyboardHeightChange](../api/keyboard.md#onkeyboardheightchange) 监听键盘高度变化,来定位 editor 组件的位置,避免被键盘遮挡
- editor 组件可以使用 `readOnly=false` 属性来让键盘失焦,然后通过 `readOnly=true;setTimeout(() => {editor.format(name, value)}, 0)` 来实现在 editor 组件失焦的情况下调用 format 方法来修改 editor 组件内容输入并自动聚焦。如果失焦后直接调用 format 方法,再手动聚焦设置的内容会被覆盖掉。
- editor 存在 `min-height: 200px` 的默认样式,如果需要修改高度,请注意重写 `min-height` 样式
- **Bug:** 在 HBuilderX 5.21 之前 min-height 样式不生效,始终会有 200px 的最小高度