Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linkToImg支持自定义format #780

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ xhr.send(JSON.stringify({url: src})); // src 为站外图片地址
}
```

后端返回的数据结构不一致时,可使用 `linkToImgFormat` 进行转换。

* `success`,`format`,`error` 不会同时触发,具体调用情况如下:

```js
Expand Down
1 change: 1 addition & 0 deletions README_en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ xhr.send(JSON.stringify({url: src})); // src is the address of the image outside
| max | The largest upload file Byte | 10 * 1024 * 1024 |
| linkToImgUrl | When the clipboard contains the image address, use this url to re-upload | '' |
| linkToImgCallback | Callback when uploading picture address | - |
| linkToImgFormat | Transform the data returned by the server to meet the built-in data structure (responseText: string): string | - |
| success | Upload success callback (editor: HTMLPreElement, msg: string): void | - |
| error | Upload failure callback (msg: string): void | - |
| token | CORS upload verification, header is X-Upload-Token | - |
Expand Down
4 changes: 4 additions & 0 deletions src/ts/util/fixBrowserBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,10 @@ export const paste = (vditor: IVditor, event: ClipboardEvent & { target: HTMLEle
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let responseText = xhr.responseText;
if (vditor.options.upload.linkToImgFormat) {
responseText = vditor.options.upload.linkToImgFormat(xhr.responseText);
}
const responseJSON = JSON.parse(xhr.responseText);
if (responseJSON.code !== 0) {
vditor.tip.show(responseJSON.msg);
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ interface IUpload {
/** 对服务端返回的数据进行转换,以满足内置的数据结构 */
format?(files: File[], responseText: string): string;

/** 对服务端返回的数据进行转换(对应linkToImgUrl),以满足内置的数据结构 */
linkToImgFormat?(responseText: string): string;

/** 将上传的文件处理后再返回 */
file?(files: File[]): File[];

Expand Down