wireless-desc-converter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -0
- package/dist/wireless-desc-converter.cjs.js +557 -0
- package/dist/wireless-desc-converter.cjs.js.map +1 -0
- package/dist/wireless-desc-converter.esm.js +534 -0
- package/dist/wireless-desc-converter.esm.js.map +1 -0
- package/dist/wireless-desc-converter.umd.js +563 -0
- package/dist/wireless-desc-converter.umd.js.map +1 -0
- package/package.json +40 -0
- package/src/index.js +567 -0
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# wireless-desc-converter
|
|
2
|
+
|
|
3
|
+
淘宝新版图文编辑器 `wirelessDesc` 适配工具 —— 将 HTML 富文本转换为淘宝新版图文编辑器结构化 JSON。
|
|
4
|
+
|
|
5
|
+
## 背景
|
|
6
|
+
|
|
7
|
+
淘宝旺铺新版图文编辑器(1.0.0+)不再区分电脑端和手机端,统一使用 `wireless_desc` 字段。旧的 `description` 和 `wap_desc` 已废弃。
|
|
8
|
+
|
|
9
|
+
本工具将旧版 HTML 富文本(`desc` 字段)自动转换为符合新版接口要求的结构化 JSON。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install wireless-desc-converter
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 快速开始
|
|
18
|
+
|
|
19
|
+
### 最简用法
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { htmlToWirelessDesc } from 'wireless-desc-converter';
|
|
23
|
+
|
|
24
|
+
const html = '<p><h2>上新推荐</h2><img src="https://img.alicdn.com/imgextra/xxx.jpg"><br></p>';
|
|
25
|
+
|
|
26
|
+
// 直接 await 即可
|
|
27
|
+
const desc = await htmlToWirelessDesc(html);
|
|
28
|
+
const jsonStr = JSON.stringify(desc); // 接口提交
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 需要图片尺寸 / 文字合图
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
import { htmlToWirelessDesc } from 'wireless-desc-converter';
|
|
35
|
+
|
|
36
|
+
const desc = await htmlToWirelessDesc(html, {
|
|
37
|
+
imageSize: async ({ url }) => {
|
|
38
|
+
const res = await fetch('/api/image-size?url=' + url);
|
|
39
|
+
return res.json(); // 返回 { width, height }
|
|
40
|
+
},
|
|
41
|
+
textImage: async ({ text, styles }) => {
|
|
42
|
+
const res = await fetch('/api/text-to-image', {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
body: JSON.stringify({ text, styles })
|
|
45
|
+
});
|
|
46
|
+
return res.json(); // 返回 [{ url, width, height }]
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const jsonStr = JSON.stringify(desc); // 提交
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
也可以只传其中一个:
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
// 只补全图片尺寸
|
|
57
|
+
const desc = await htmlToWirelessDesc(html, {
|
|
58
|
+
imageSize: async ({ url }) => getImageSize(url)
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 手动构造模块
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
import {
|
|
66
|
+
buildTextModule,
|
|
67
|
+
buildImageModule,
|
|
68
|
+
buildVersionModule,
|
|
69
|
+
buildConfigModule,
|
|
70
|
+
buildEmptyWirelessDesc
|
|
71
|
+
} from 'wireless-desc-converter';
|
|
72
|
+
|
|
73
|
+
// 单独构造文字模块
|
|
74
|
+
const textModule = buildTextModule({
|
|
75
|
+
text: '商品描述文字',
|
|
76
|
+
images: [{ url: '合图URL', width: 620, height: 62 }],
|
|
77
|
+
styles: { fontSize: '28', color: '#ff0000', textAlign: 'center' },
|
|
78
|
+
index: 1
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// 单独构造图片热区模块
|
|
82
|
+
const imageModule = buildImageModule({
|
|
83
|
+
url: 'https://img.alicdn.com/imgextra/xxx.jpg',
|
|
84
|
+
width: 620,
|
|
85
|
+
height: 827,
|
|
86
|
+
hotAreas: [
|
|
87
|
+
{ start_x: '0.1', start_y: '0.2', end_x: '0.9', end_y: '0.8', link: 'https://...' }
|
|
88
|
+
],
|
|
89
|
+
index: 0
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// 构造完整 wirelessDesc
|
|
93
|
+
const wirelessDesc = buildEmptyWirelessDesc();
|
|
94
|
+
wirelessDesc.value.props.unshift(imageModule, textModule);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API 文档
|
|
98
|
+
|
|
99
|
+
### 核心方法
|
|
100
|
+
|
|
101
|
+
| 方法 | 说明 | 返回值 |
|
|
102
|
+
|------|------|--------|
|
|
103
|
+
| `htmlToWirelessDesc(html, options?)` | HTML → wirelessDesc JSON,async/await 调用 | `Promise<Object>` |
|
|
104
|
+
| `buildEmptyWirelessDesc(options?)` | 构造空 wirelessDesc | `Object` |
|
|
105
|
+
| `validateHeight(desc, maxHeight?)` | 校验总高度是否超限 | `{ valid, totalHeight, maxHeight }` |
|
|
106
|
+
| `serializeWirelessDesc(desc)` | 序列化为 JSON 字符串 | `string` |
|
|
107
|
+
|
|
108
|
+
### 模块构造
|
|
109
|
+
|
|
110
|
+
| 方法 | 说明 |
|
|
111
|
+
|------|------|
|
|
112
|
+
| `buildTextModule(params)` | 构造文字模块 |
|
|
113
|
+
| `buildImageModule(params)` | 构造图片热区模块 |
|
|
114
|
+
| `buildRichTextModule(params)` | 构造富文本模块(仅保留已存在的,设 enable=false) |
|
|
115
|
+
| `buildVersionModule()` | 构造 version 模块 |
|
|
116
|
+
| `buildConfigModule(options?)` | 构造 config 模块 |
|
|
117
|
+
|
|
118
|
+
### 参数说明
|
|
119
|
+
|
|
120
|
+
#### `htmlToWirelessDesc(html, options)`
|
|
121
|
+
|
|
122
|
+
| 参数 | 类型 | 说明 |
|
|
123
|
+
|------|------|------|
|
|
124
|
+
| `html` | `string` | 原始 HTML 富文本 |
|
|
125
|
+
| `options.maxHeight` | `string` | 编辑器最大高度,默认 `'100000'` |
|
|
126
|
+
| `options.width` | `string` | 模块宽度,默认 `'620'` |
|
|
127
|
+
| `options.splitHeight` | `string` | 切图高度,默认 `'1240'` |
|
|
128
|
+
| `options.existingModules` | `Array` | 已有模块(保留提交) |
|
|
129
|
+
| `options.imageSize` | `Function` | 图片尺寸获取,参数 `{ url }`,返回 `Promise<{ width, height }>` |
|
|
130
|
+
| `options.textImage` | `Function` | 文字合图,参数 `{ text, styles, index }`,返回 `Promise<Array<{ url, width, height }>>` |
|
|
131
|
+
|
|
132
|
+
#### `buildTextModule(params)`
|
|
133
|
+
|
|
134
|
+
| 参数 | 类型 | 说明 |
|
|
135
|
+
|------|------|------|
|
|
136
|
+
| `params.text` | `string` | 文字内容 |
|
|
137
|
+
| `params.images` | `Array` | 合图列表 `[{url, width, height}]` |
|
|
138
|
+
| `params.styles` | `Object` | 样式 `{ fontSize, color, textAlign, fontFamily, ... }` |
|
|
139
|
+
| `params.id` | `string` | 模块ID(已有模块传入) |
|
|
140
|
+
| `params.index` | `number` | 序号 |
|
|
141
|
+
|
|
142
|
+
#### `buildImageModule(params)`
|
|
143
|
+
|
|
144
|
+
| 参数 | 类型 | 说明 |
|
|
145
|
+
|------|------|------|
|
|
146
|
+
| `params.url` | `string` | 图片链接 |
|
|
147
|
+
| `params.width` | `number\|string` | 图片宽度 |
|
|
148
|
+
| `params.height` | `number\|string` | 图片高度 |
|
|
149
|
+
| `params.hotAreas` | `Array` | 热区列表 `[{start_x, start_y, end_x, end_y, link}]` |
|
|
150
|
+
| `params.id` | `string` | 模块ID |
|
|
151
|
+
| `params.index` | `number` | 序号 |
|
|
152
|
+
|
|
153
|
+
### 常量
|
|
154
|
+
|
|
155
|
+
| 常量 | 值 | 说明 |
|
|
156
|
+
|------|-----|------|
|
|
157
|
+
| `MODULE_WIDTH` | `620` | 模块默认宽度 |
|
|
158
|
+
| `SPLIT_HEIGHT` | `1240` | 切图高度 |
|
|
159
|
+
| `MAX_HEIGHT` | `100000` | 最大总高度 |
|
|
160
|
+
| `VERSION` | `'1.0.0'` | 编辑器版本号 |
|
|
161
|
+
| `IMAGE_MIN_WIDTH` | `480` | 图片最小宽度 |
|
|
162
|
+
| `IMAGE_MAX_WIDTH` | `1500` | 图片最大宽度 |
|
|
163
|
+
| `IMAGE_MAX_HEIGHT` | `2000` | 图片最大高度 |
|
|
164
|
+
|
|
165
|
+
## 淘宝新版图文编辑器规则提醒
|
|
166
|
+
|
|
167
|
+
1. 所有模块高度总和不超过 **100000px**(`countHeight=true` 的模块计入)
|
|
168
|
+
2. 已有模块(`id` 有值)不能直接删除,须设 `enable=false`
|
|
169
|
+
3. 官方可能新增模块,ISV 须保留提交,不可丢弃
|
|
170
|
+
4. 模块顺序由 `field.id` 后缀序号决定
|
|
171
|
+
5. 文字模块需合成图片提交,宽度620px,高度超过1240px需切图
|
|
172
|
+
6. 图片宽度 480~1500px,高度≤2000px
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
MIT
|