editor-context.md 7.2 KB
Newer Older
M
mehaotian 已提交
1 2
## editorContext

W
wanganxp 已提交
3
editor 组件对应的 editorContext 实例,可通过 [uni.createSelectorQuery](/api/ui/nodes-info?id=createselectorquery) 获取。
M
mehaotian 已提交
4

study夏羽's avatar
study夏羽 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
```js
 onEditorReady() {
    uni.createSelectorQuery().select('#editor').context((res) => {
        this.editorCtx = res.context
    }).exec()
}
```

百度小程序 ```Editor``` 富文本编辑器动态库提供了 ```createEditorContext``` 的方法来获取。

```js
 onEditorReady() {
    this.editorCtx = requireDynamicLib('editorLib').createEditorContext('editorId');
  }
```


M
mehaotian 已提交
22 23
`editorContext` 通过 `id` 跟一个 [`<editor>`](/component/editor) 组件绑定,操作对应的 [`<editor>`](/component/editor) 组件。

study夏羽's avatar
study夏羽 已提交
24 25 26 27 28





M
mehaotian 已提交
29 30
**平台差异说明**

study夏羽's avatar
study夏羽 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|App|H5			|微信小程序	|支付宝小程序	|百度小程序	|字节跳动小程序	|QQ小程序	|
|:-:|:-:		|:-:				|:-:					|:-:				|:-:						|:-:			|
|√	|2.4.5+	|√					|x						|需引入动态库					|x							|x				|


**百度小程序引入动态库**


1. 在项目中引用动态库,在 ```manifest.json``` 中增添一项 ```dynamicLib``` 
 
```js
  "mp-baidu" : {
    "usingComponents" : true,
    "appid" : "",
    "setting" : {
      "urlCheck" : true
    },
    "dynamicLib": {//引入百度小程序动态库
      "editorLib": {
        "provider": "swan-editor"
      }
    }
  },

```

2. 在每个使用到富文本编辑器组件的页面,配置 ```pages.json``` 文件如下:

``` js 
{
	"pages": [ 
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "uni-app",
				"usingSwanComponents": {
					"editor": "dynamicLib://editorLib/editor"
				}
			}
		}
	]
}

```


M
mehaotian 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


## editorContext.format(name, value)

修改样式

| 参数 | 类型 | 说明 |
| --- | --- | --- |
|name|String|属性|
|value|String|值|

**支持设置的样式列表**

| 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 |
S
SiiZhao 已提交
111
| textIndent | css style |
M
mehaotian 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

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

## editorContext.insertDivider(OBJECT)

插入分割线

**OBJECT 参数说明**

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

## editorContext.insertImage(OBJECT)

129 130 131 132
插入图片。

微信小程序平台地址为临时文件时,获取的编辑器html格式内容中 `<img>` 标签增加属性 data-local,delta 格式内容中图片 attributes 属性增加 data-local 字段,该值为传入的临时文件地址。
开发者可选择在提交阶段上传图片到服务器,获取到网络地址后进行替换。替换时对于html内容应替换掉 `<img>` 的 src 值,对于 delta 内容应替换掉 `insert { image: abc }` 值。
M
mehaotian 已提交
133 134 135

**OBJECT 参数说明**

136 137 138 139 140 141 142 143 144 145 146
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| src | String |  | 是 | 图片地址 |
| alt | String |  | 否 | 图像无法显示时的替代文本 |
| width | String |  | 否 | 图片宽度(pixels/百分比),2.6.5+ 支持 |
| height | String |  | 否 | 图片高度 (pixels/百分比),2.6.5+ 支持|
| extClass | String |  | 否 | 添加到图片 img 标签上的类名,2.6.5+ 支持 |
| data | Object |  | 否 | data 被序列化为 name=value;name1=value2 的格式挂在属性 data-custom 上,2.6.5+ 支持 |
| success | Function |  | 否 | 接口调用成功的回调函数 |
| fail | Function |  | 否 | 接口调用失败的回调函数 |
| complete | Function |  | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
M
mehaotian 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160

## editorContext.insertText(OBJECT)

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

**OBJECT 参数说明**

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

雪洛's avatar
雪洛 已提交
161
## editorContext.setContents(OBJECT)
M
mehaotian 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

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

**OBJECT 参数说明**

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

## editorContext.getContents(OBJECT)

获取编辑器内容

**OBJECT 参数说明**

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

## editorContext.clear(OBJECT)

清空编辑器内容

**OBJECT 参数说明**

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

## editorContext.removeFormat(OBJECT)

清除当前选区的样式

**OBJECT 参数说明**

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

## editorContext.undo(OBJECT)

撤销

**OBJECT 参数说明**

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

## editorContext.redo(OBJECT)

恢复

**OBJECT 参数说明**

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| --- | --- | --- | --- | --- |
| success | Function |  | 否 | 接口调用成功的回调函数 |
| fail | Function |  | 否 | 接口调用失败的回调函数 |
W
wanganxp 已提交
233
| complete | Function |  | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |