title: EditorContext

sidebar_label: EditorContext

EditorContext 实例,可通过 Taro.createSelectorQuery 获取。 EditorContext 通过 id 跟一个 editor 组件绑定,操作对应的 editor 组件。

支持情况:EditorContext - 图1 EditorContext - 图2 EditorContext - 图3

参考文档

方法

blur

编辑器失焦,同时收起键盘。

支持情况:EditorContext - 图4 EditorContext - 图5 EditorContext - 图6

参考文档

  1. (option?: BlurOption) => void
参数 类型
option BlurOption

clear

清空编辑器内容

支持情况:EditorContext - 图7 EditorContext - 图8 EditorContext - 图9

参考文档

  1. (option?: ClearOption) => void
参数 类型
option ClearOption

format

修改样式


支持设置的样式列表

name value
bold
italic
underline
strike
ins
script sub / super
header H1 / H2 / h3 / H4 / h5 / H6
align left / center / right / justify
direction rtl
indent -1 / +1
list ordered / bullet / check
color hex color
backgroundColor hex color
margin/marginTop/marginBottom/marginLeft/marginRight css style
padding/paddingTop/paddingBottom/paddingLeft/paddingRight css style
font/fontSize/fontStyle/fontVariant/fontWeight/fontFamily css style
lineHeight css style
letterSpacing css style
textDecoration css style
textIndent css style

对已经应用样式的选区设置会取消样式。css style 表示 css 中规定的允许值。

支持情况:EditorContext - 图10 EditorContext - 图11 EditorContext - 图12

参考文档

  1. (name: string, value?: string) => void
参数 类型 说明
name string 属性
value string

getContents

获取编辑器内容

支持情况:EditorContext - 图13 EditorContext - 图14 EditorContext - 图15

参考文档

  1. (option?: GetContentsOption) => void
参数 类型
option GetContentsOption

getSelectionText

获取编辑器已选区域内的纯文本内容。当编辑器失焦或未选中一段区间时,返回内容为空。

支持情况:EditorContext - 图16 EditorContext - 图17 EditorContext - 图18

参考文档

  1. (option?: Option) => void
参数 类型
option Option

insertDivider

插入分割线

支持情况:EditorContext - 图19 EditorContext - 图20 EditorContext - 图21

参考文档

  1. (option?: InsertDividerOption) => void
参数 类型
option InsertDividerOption

insertImage

插入图片。

地址为临时文件时,获取的编辑器html格式内容中 <img> 标签增加属性 data-local,delta 格式内容中图片 attributes 属性增加 data-local 字段,该值为传入的临时文件地址。

开发者可选择在提交阶段上传图片到服务器,获取到网络地址后进行替换。替换时对于html内容应替换掉 <img> 的 src 值,对于 delta 内容应替换掉 insert { image: abc } 值。

支持情况:EditorContext - 图22 EditorContext - 图23 EditorContext - 图24

参考文档

  1. (option: InsertImageOption) => void
参数 类型
option InsertImageOption

示例代码

  1. this.editorCtx.insertImage({
  2. src: 'xx',
  3. width: '100px',
  4. height: '50px',
  5. extClass: className
  6. })

insertText

覆盖当前选区,设置一段文本

支持情况:EditorContext - 图25 EditorContext - 图26 EditorContext - 图27

参考文档

  1. (option: InsertTextOption) => void
参数 类型
option InsertTextOption

redo

恢复

支持情况:EditorContext - 图28 EditorContext - 图29 EditorContext - 图30

参考文档

  1. (option?: RedoOption) => void
参数 类型
option RedoOption

removeFormat

清除当前选区的样式

支持情况:EditorContext - 图31 EditorContext - 图32 EditorContext - 图33

参考文档

  1. (option?: RemoveFormatOption) => void
参数 类型
option RemoveFormatOption

scrollIntoView

使得编辑器光标处滚动到窗口可视区域内。

支持情况:EditorContext - 图34 EditorContext - 图35 EditorContext - 图36

参考文档

  1. () => void

setContents

初始化编辑器内容,html和delta同时存在时仅delta生效

支持情况:EditorContext - 图37 EditorContext - 图38 EditorContext - 图39

参考文档

  1. (option: SetContentsOption) => void
参数 类型
option SetContentsOption

undo

撤销

支持情况:EditorContext - 图40 EditorContext - 图41 EditorContext - 图42

参考文档

  1. (option?: UndoOption) => void
参数 类型
option UndoOption

参数

BlurOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

ClearOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

GetContentsOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

getSelectionText

Option

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: SuccessCallbackResult) => void 接口调用成功的回调函数

SuccessCallbackResult

参数 类型 说明
text string 纯文本内容

InsertDividerOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

InsertImageOption

参数 类型 必填 说明
src string 图片地址,仅支持 http(s)、base64、云图片(2.8.0)、临时文件(2.8.3)。
alt string 图像无法显示时的替代文本
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
data TaroGeneral.IAnyObject data 被序列化为 name=value;name1=value2 的格式挂在属性 data-custom 上
extClass string 添加到图片 img 标签上的类名
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
height string 图片高度 (pixels/百分比)
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数
width string 图片宽度(pixels/百分比)

InsertTextOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数
text string 文本内容

RedoOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

RemoveFormatOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

SetContentsOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
delta TaroGeneral.IAnyObject 表示内容的delta对象
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
html string 带标签的HTML内容
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

UndoOption

参数 类型 必填 说明
complete (res: TaroGeneral.CallbackResult) => void 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: TaroGeneral.CallbackResult) => void 接口调用失败的回调函数
success (res: TaroGeneral.CallbackResult) => void 接口调用成功的回调函数

API 支持度

API 微信小程序 H5 React Native
EditorContext ✔️
EditorContext.blur ✔️
EditorContext.clear ✔️
EditorContext.format ✔️
EditorContext.getContents ✔️
EditorContext.getSelectionText ✔️
EditorContext.insertDivider ✔️
EditorContext.insertImage ✔️
EditorContext.insertText ✔️
EditorContext.redo ✔️
EditorContext.removeFormat ✔️
EditorContext.scrollIntoView ✔️
EditorContext.setContents ✔️
EditorContext.undo ✔️