From 7fdeaedb75ae3511f3425d616b15c8db5cf9e3bc Mon Sep 17 00:00:00 2001 From: wangsijie Date: Wed, 14 Oct 2020 15:44:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20linkToImg=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ README_en_US.md | 1 + src/ts/util/fixBrowserBehavior.ts | 4 ++++ types/index.d.ts | 3 +++ 4 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 37075efe6..86eb8d084 100644 --- a/README.md +++ b/README.md @@ -395,6 +395,8 @@ xhr.send(JSON.stringify({url: src})); // src 为站外图片地址 } ``` +后端返回的数据结构不一致时,可使用 `linkToImgFormat` 进行转换。 + * `success`,`format`,`error` 不会同时触发,具体调用情况如下: ```js diff --git a/README_en_US.md b/README_en_US.md index d1efcd41c..03a9079d6 100644 --- a/README_en_US.md +++ b/README_en_US.md @@ -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 | - | diff --git a/src/ts/util/fixBrowserBehavior.ts b/src/ts/util/fixBrowserBehavior.ts index b964a8922..c655be3e8 100644 --- a/src/ts/util/fixBrowserBehavior.ts +++ b/src/ts/util/fixBrowserBehavior.ts @@ -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); diff --git a/types/index.d.ts b/types/index.d.ts index 5891c5ae3..63d14eb0b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -272,6 +272,9 @@ interface IUpload { /** 对服务端返回的数据进行转换,以满足内置的数据结构 */ format?(files: File[], responseText: string): string; + /** 对服务端返回的数据进行转换(对应linkToImgUrl),以满足内置的数据结构 */ + linkToImgFormat?(responseText: string): string; + /** 将上传的文件处理后再返回 */ file?(files: File[]): File[];