diff --git a/packages/tsdaodaobase/src/Messages/Image/index.css b/packages/tsdaodaobase/src/Messages/Image/index.css new file mode 100644 index 0000000..2898e23 --- /dev/null +++ b/packages/tsdaodaobase/src/Messages/Image/index.css @@ -0,0 +1,25 @@ +@keyframes loading { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(360deg); + } +} +.loading-box{ + width: 32px; + height: 32px; + position:absolute; + top:50%; + left:50%; + transform:translate(-16px,-16px); +} +.loading-icon{ + width: 32px; + height: 32px; + animation: loading 1s linear infinite; +} +.loading-text{ + font-size: 14px; + white-space: nowrap; +} diff --git a/packages/tsdaodaobase/src/Messages/Image/index.tsx b/packages/tsdaodaobase/src/Messages/Image/index.tsx index 4064973..d8c4a7f 100644 --- a/packages/tsdaodaobase/src/Messages/Image/index.tsx +++ b/packages/tsdaodaobase/src/Messages/Image/index.tsx @@ -1,124 +1,172 @@ -import { MediaMessageContent } from "wukongimjssdk" -import React from "react" -import WKApp from "../../App" -import { MessageContentTypeConst } from "../../Service/Const" -import MessageBase from "../Base" -import { MessageCell } from "../MessageCell" -import Viewer from 'react-viewer'; - - +import WKSDK, { MediaMessageContent, Task } from "wukongimjssdk"; +import React from "react"; +import WKApp from "../../App"; +import { MessageContentTypeConst } from "../../Service/Const"; +import MessageBase from "../Base"; +import { MessageCell } from "../MessageCell"; +import Viewer from "react-viewer"; +import "./index.css"; export class ImageContent extends MediaMessageContent { - width!: number - height!: number - url!: string - imgData?: string - constructor(file?: File, imgData?: string, width?: number, height?: number) { - super() - this.file = file - this.imgData = imgData - this.width = width || 0 - this.height = height || 0 - } - decodeJSON(content: any) { - this.width = content["width"] || 0 - this.height = content["height"] || 0 - this.url = content["url"] || '' - this.remoteUrl = this.url - } - encodeJSON() { - return { "width": this.width || 0, "height": this.height || 0, "url": this.remoteUrl || "" } - } - get contentType() { - return MessageContentTypeConst.image - } - get conversationDigest() { - return "[图片]" - } + width!: number; + height!: number; + url!: string; + imgData?: string; + constructor(file?: File, imgData?: string, width?: number, height?: number) { + super(); + this.file = file; + this.imgData = imgData; + this.width = width || 0; + this.height = height || 0; + } + decodeJSON(content: any) { + this.width = content["width"] || 0; + this.height = content["height"] || 0; + this.url = content["url"] || ""; + this.remoteUrl = this.url; + } + encodeJSON() { + return { width: this.width || 0, height: this.height || 0, url: this.remoteUrl || "" }; + } + get contentType() { + return MessageContentTypeConst.image; + } + get conversationDigest() { + return "[图片]"; + } } - interface ImageCellState { - showPreview: boolean + showPreview: boolean; + loading: boolean; + progress: number; } export class ImageCell extends MessageCell { + taskListener!: any; + constructor(props: any) { + super(props); + this.state = { + showPreview: false, + loading: false, + progress: 0, + }; + } - constructor(props: any) { - super(props) - this.state = { - showPreview: false, + imageScale(orgWidth: number, orgHeight: number, maxWidth = 250, maxHeight = 250) { + let actSize = { width: orgWidth, height: orgHeight }; + if (orgWidth > orgHeight) { + //横图 + if (orgWidth > maxWidth) { + // 横图超过最大宽度 + let rate = maxWidth / orgWidth; // 缩放比例 + actSize.width = maxWidth; + actSize.height = orgHeight * rate; + } + } else if (orgWidth < orgHeight) { + //竖图 + if (orgHeight > maxHeight) { + let rate = maxHeight / orgHeight; // 缩放比例 + actSize.width = orgWidth * rate; + actSize.height = maxHeight; + } + } else if (orgWidth === orgHeight) { + if (orgWidth > maxWidth) { + let rate = maxWidth / orgWidth; // 缩放比例 + actSize.width = maxWidth; + actSize.height = orgHeight * rate; + } + } + return actSize; + } + + getImageSrc(content: ImageContent) { + if (content.url && content.url !== "") { + // 等待发送的消息 + let downloadURL = WKApp.dataSource.commonDataSource.getImageURL(content.url, { + width: content.width, + height: content.height, + }); + if (downloadURL.indexOf("?") != -1) { + downloadURL += "&filename=image.png"; + } else { + downloadURL += "?filename=image.png"; + } + return downloadURL; + } + return content.imgData; + } + + getImageElement() { + const { message } = this.props; + const content = message.content as ImageContent; + let scaleSize = this.imageScale(content.width, content.height); + if (!this.taskListener && !content?.url) { + this.taskListener = (task: Task) => { + this.setState({ + loading: true, + progress: Math.trunc(task.progress() * 100), + }); + if (task.progress() === 1) { + this.setState({ + loading: false, + progress: 100, + }); + WKSDK.shared().taskManager.removeListener(this.taskListener); } + }; + WKSDK.shared().taskManager.addListener(this.taskListener); } + return ( +
+ + {this.state.loading && scaleSize.width > 32 && ( + + + {scaleSize.height > 46 &&

{this.state.progress}%

} +
+ )} +
+ ); + } - imageScale(orgWidth: number, orgHeight: number, maxWidth = 250, maxHeight = 250) { - let actSize = { width: orgWidth, height: orgHeight }; - if (orgWidth > orgHeight) {//横图 - if (orgWidth > maxWidth) { // 横图超过最大宽度 - let rate = maxWidth / orgWidth; // 缩放比例 - actSize.width = maxWidth; - actSize.height = orgHeight * rate; - } - } else if (orgWidth < orgHeight) { //竖图 - if (orgHeight > maxHeight) { - let rate = maxHeight / orgHeight; // 缩放比例 - actSize.width = orgWidth * rate; - actSize.height = maxHeight; - } - } else if (orgWidth === orgHeight) { - if (orgWidth > maxWidth) { - let rate = maxWidth / orgWidth; // 缩放比例 - actSize.width = maxWidth; - actSize.height = orgHeight * rate; - } - } - return actSize; - } - - getImageSrc(content: ImageContent) { - if (content.url && content.url !== "") { // 等待发送的消息 - let downloadURL = WKApp.dataSource.commonDataSource.getImageURL(content.url, { width: content.width, height: content.height }) - if (downloadURL.indexOf("?") != -1) { - downloadURL += "&filename=image.png" - } else { - downloadURL += "?filename=image.png" - } - return downloadURL - } - return content.imgData - } - - getImageElement() { - const { message } = this.props - const content = message.content as ImageContent - let scaleSize = this.imageScale(content.width, content.height); - return - } - - render() { - const { message, context } = this.props - const { showPreview } = this.state - const content = message.content as ImageContent - let scaleSize = this.imageScale(content.width, content.height); - const imageURL = this.getImageSrc(content) || "" - return -
{ - this.setState({ - showPreview: !this.state.showPreview, - }) - }}> - {this.getImageElement()} -
- { this.setState({ showPreview: false }); }} - onClose={() => { this.setState({ showPreview: false }); }} - images={[{ src: imageURL, alt: '', downloadUrl: imageURL }]} - /> -
- } -} \ No newline at end of file + render() { + const { message, context } = this.props; + const { showPreview } = this.state; + const content = message.content as ImageContent; + let scaleSize = this.imageScale(content.width, content.height); + const imageURL = this.getImageSrc(content) || ""; + return ( + +
{ + this.setState({ + showPreview: !this.state.showPreview, + }); + }} + > + {this.getImageElement()} +
+ { + this.setState({ showPreview: false }); + }} + onClose={() => { + this.setState({ showPreview: false }); + }} + images={[{ src: imageURL, alt: "", downloadUrl: imageURL }]} + /> +
+ ); + } +} diff --git a/packages/tsdaodaobase/src/assets/loading.png b/packages/tsdaodaobase/src/assets/loading.png new file mode 100644 index 0000000..1ae2361 Binary files /dev/null and b/packages/tsdaodaobase/src/assets/loading.png differ diff --git a/packages/tsdaodaodatasource/src/task.ts b/packages/tsdaodaodatasource/src/task.ts index 3c45906..4322876 100644 --- a/packages/tsdaodaodatasource/src/task.ts +++ b/packages/tsdaodaodatasource/src/task.ts @@ -18,10 +18,13 @@ export class MediaMessageUploadTask extends MessageTask { async start(): Promise { const mediaContent = this.message.content as MediaMessageContent if(mediaContent.file) { + const fileNameSuffix = mediaContent.file.name.substring( + mediaContent.file.name.lastIndexOf(".") + ); const param = new FormData(); param.append("file", mediaContent.file); const fileName = this.getUUID(); - const path = `/${this.message.channel.channelType}/${this.message.channel.channelID}/${fileName}${mediaContent.extension??""}` + const path = `/${this.message.channel.channelType}/${this.message.channel.channelID}/${fileName}${fileNameSuffix ?? ""}`; const uploadURL = await this.getUploadURL(path) if(uploadURL) { this.uploadFile(mediaContent.file,uploadURL) @@ -52,7 +55,7 @@ export class MediaMessageUploadTask extends MessageTask { this.canceler = c }), onUploadProgress: e => { - var completeProgress = ((e.loaded / e.total) | 0); + var completeProgress = ((e.loaded / e.total) || 0); this._progress = completeProgress this.update() }