赛捷软件论坛's Archiver

vicky.yu 发表于 2026-5-6 10:19

微信小程序API 富文本·EditorContext实例

EditorContext
基础库 2.7.0 开始支持,低版本需做兼容处理。
EditorContext 实例,可通过 wx.createSelectorQuery 获取。

EditorContext 通过 id 跟一个 editor 组件绑定,操作对应的 editor 组件。


方法:
EditorContext.blur(Object object)
基础库 2.8.3 开始支持,低版本需做兼容处理。
编辑器失焦,同时收起键盘。

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.clear(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
清空编辑器内容

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.format(string name, string value)
基础库 2.7.0 开始支持,低版本需做兼容处理。
修改样式

参数
string name
属性

string value


支持设置的样式列表
name        value        verson
bold                2.7.0
italic                2.7.0
underline                2.7.0
strike                2.7.0
ins                2.7.0
script        sub / super        2.7.0
header        H1 / H2 / h3 / H4 / h5 / H6        2.7.0
align        left / center / right / justify        2.7.0
direction        rtl        2.7.0
indent        -1 / +1        2.7.0
list        ordered / bullet / check        2.7.0
color        hex color        2.7.0
backgroundColor        hex color        2.7.0
margin/marginTop/marginBottom/marginLeft/marginRight        css style        2.7.0
padding/paddingTop/paddingBottom/paddingLeft/paddingRight        css style        2.7.0
font/fontSize/fontStyle/fontVariant/fontWeight/fontFamily        css style        2.7.0
lineHeight        css style        2.7.0
letterSpacing        css style        2.7.0
textDecoration        css style        2.7.0
textIndent        css style        2.8.0
wordWrap        css style        2.10.2
wordBreak        css style        2.10.2
whiteSpace        css style        2.10.2
对已经应用样式的选区设置会取消样式。css style 表示 css 中规定的允许值。


EditorContext.getContents(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
获取编辑器内容

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数
参数
Object res
属性        类型        说明
html        string        带标签的HTML内容
text        string        纯文本内容
delta        Object        表示内容的delta对象

EditorContext.getSelectionText(Object object)
基础库 2.10.2 开始支持,低版本需做兼容处理。
获取编辑器已选区域内的纯文本内容。当编辑器失焦或未选中一段区间时,返回内容为空。

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)
object.success 回调函数
参数
Object res
属性        类型        说明
text        string        纯文本内容

EditorContext.insertDivider(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
插入分割线

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.insertImage(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
插入图片。

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

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

参数
Object object
属性        类型        默认值        必填        说明
src        string                是        图片地址,仅支持 http(s)、base64、云图片(2.8.0)、临时文件(2.8.3)。
alt        string                否        图像无法显示时的替代文本
width        string                否        图片宽度(pixels/百分比)
height        string                否        图片高度 (pixels/百分比)
extClass        string                否        添加到图片 img 标签上的类名
data        Object                否        data 被序列化为 name=value;name1=value2 的格式挂在属性 data-custom 上
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)
示例代码
this.editorCtx.insertImage({
  src: 'xx',
  width: '100px',
  height: '50px',
  extClass: className
})

EditorContext.insertText(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
覆盖当前选区,设置一段文本

参数
Object object
属性        类型        默认值        必填        说明
text        string                否        文本内容
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.redo(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
恢复

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.removeFormat(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
清除当前选区的样式

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.scrollIntoView()
基础库 2.8.3 开始支持,低版本需做兼容处理。
使得编辑器光标处滚动到窗口可视区域内。

EditorContext.setContents(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
初始化编辑器内容,html和delta同时存在时仅delta生效

参数
Object object
属性        类型        默认值        必填        说明
html        string                否        带标签的HTML内容
delta        Object                否        表示内容的delta对象
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

EditorContext.undo(Object object)
基础库 2.7.0 开始支持,低版本需做兼容处理。
撤销

参数
Object object
属性        类型        默认值        必填        说明
success        function                否        接口调用成功的回调函数
fail        function                否        接口调用失败的回调函数
complete        function                否        接口调用结束的回调函数(调用成功、失败都会执行)

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.