From 573ababb8c249a0a6e2576bac4ae3b85feef255f Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Wed, 9 Jun 2021 15:23:47 +0530 Subject: [PATCH] Get rid of mediaSrc and avatarSrc props --- src/components/views/avatars/MemberAvatar.tsx | 8 ++++---- src/components/views/messages/MAudioBody.js | 6 +++--- src/components/views/messages/MFileBody.js | 8 ++++---- src/components/views/messages/MImageBody.js | 12 ++++++------ src/components/views/messages/MVideoBody.tsx | 10 +++++----- .../views/messages/MVoiceOrAudioBody.tsx | 4 ++-- src/components/views/messages/MessageEvent.js | 4 ---- src/components/views/rooms/EventTile.tsx | 9 +-------- src/utils/exportUtils/HtmlExport.tsx | 17 ++++++++++------- 9 files changed, 35 insertions(+), 43 deletions(-) diff --git a/src/components/views/avatars/MemberAvatar.tsx b/src/components/views/avatars/MemberAvatar.tsx index 67d6ef38f0..48bf0d3423 100644 --- a/src/components/views/avatars/MemberAvatar.tsx +++ b/src/components/views/avatars/MemberAvatar.tsx @@ -34,10 +34,10 @@ interface IProps extends Omit, "name" | resizeMethod?: ResizeMethod; // The onClick to give the avatar onClick?: React.MouseEventHandler; - // Whether the onClick of the avatar should be overriden to dispatch `Action.ViewUser` + // Whether the onClick of the avatar should be overridden to dispatch `Action.ViewUser` viewUserOnClick?: boolean; title?: string; - avatarSrc?: string; + forExport?: boolean; } interface IState { @@ -68,7 +68,7 @@ export default class MemberAvatar extends React.Component { private static getState(props: IProps): IState { if (props.member?.name) { let imageUrl = null; - if (props.avatarSrc) imageUrl = props.avatarSrc; + if (props.forExport && props.member.getMxcAvatarUrl()) imageUrl = "AvatarForExport"; else if (props.member.getMxcAvatarUrl()) { imageUrl = mediaFromMxc(props.member.getMxcAvatarUrl()).getThumbnailOfSourceHttp( props.width, @@ -95,7 +95,7 @@ export default class MemberAvatar extends React.Component { let {member, fallbackUserId, onClick, viewUserOnClick, ...otherProps} = this.props; const userId = member ? member.userId : fallbackUserId; - otherProps = omit(otherProps, "avatarSrc"); + otherProps = omit(otherProps, "forExport"); if (viewUserOnClick) { onClick = () => { diff --git a/src/components/views/messages/MAudioBody.js b/src/components/views/messages/MAudioBody.js index 2c3eba8cb3..6c6a0527ea 100644 --- a/src/components/views/messages/MAudioBody.js +++ b/src/components/views/messages/MAudioBody.js @@ -42,7 +42,7 @@ export default class MAudioBody extends React.Component { } _getContentUrl() { - if (this.props.mediaSrc) return this.props.mediaSrc; + if (this.props.forExport) return "forExport"; const media = mediaFromContent(this.props.mxEvent.getContent()); if (media.isEncrypted) { return this.state.decryptedUrl; @@ -52,7 +52,7 @@ export default class MAudioBody extends React.Component { } getFileBody() { - if (this.props.mediaSrc) return null; + if (this.props.forExport) return null; return ; } @@ -95,7 +95,7 @@ export default class MAudioBody extends React.Component { ); } - if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null) { + if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null) { // Need to decrypt the attachment // The attachment is decrypted in componentDidMount. // For now add an img tag with a 16x16 spinner. diff --git a/src/components/views/messages/MFileBody.js b/src/components/views/messages/MFileBody.js index 1ef2c2eb64..813b340bb9 100644 --- a/src/components/views/messages/MFileBody.js +++ b/src/components/views/messages/MFileBody.js @@ -102,8 +102,7 @@ export default class MFileBody extends React.Component { tileShape: PropTypes.string, /* whether or not to show the default placeholder for the file. Defaults to true. */ showGenericPlaceholder: PropTypes.bool, - /* to set source to local file path during export */ - mediaSrc: PropTypes.string, + forExport: PropTypes.bool, }; @@ -152,6 +151,7 @@ export default class MFileBody extends React.Component { } _getContentUrl() { + if (this.props.forExport) return null; const media = mediaFromContent(this.props.mxEvent.getContent()); return media.srcHttp; } @@ -185,9 +185,9 @@ export default class MFileBody extends React.Component { ); } - if (this.props.mediaSrc) { + if (this.props.forExport) { return - + { placeholder } ; diff --git a/src/components/views/messages/MImageBody.js b/src/components/views/messages/MImageBody.js index 56cc6a5eec..b8220d8a5c 100644 --- a/src/components/views/messages/MImageBody.js +++ b/src/components/views/messages/MImageBody.js @@ -172,7 +172,7 @@ export default class MImageBody extends React.Component { } _getContentUrl() { - if (this.props.mediaSrc) return this.props.mediaSrc; + if (this.props.forExport) return "forExport"; const media = mediaFromContent(this.props.mxEvent.getContent()); if (media.isEncrypted) { return this.state.decryptedUrl; @@ -370,9 +370,9 @@ export default class MImageBody extends React.Component { let gifLabel = null; // e2e image hasn't been decrypted yet - if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null) { + if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null) { placeholder = ; - } else if (!this.props.mediaSrc && !this.state.imgLoaded) { + } else if (!this.props.forExport && !this.state.imgLoaded) { // Deliberately, getSpinner is left unimplemented here, MStickerBody overides placeholder = this.getPlaceholder(); } @@ -418,7 +418,7 @@ export default class MImageBody extends React.Component { } -
+
{ img } { gifLabel }
@@ -450,7 +450,7 @@ export default class MImageBody extends React.Component { // Overidden by MStickerBody getFileBody() { - if (this.props.mediaSrc) return null; + if (this.props.forExport) return null; return ; } @@ -468,7 +468,7 @@ export default class MImageBody extends React.Component { const contentUrl = this._getContentUrl(); let thumbUrl; - if ((this._isGif() && SettingsStore.getValue("autoplayGifsAndVideos")) || this.props.mediaSrc) { + if (this.props.forExport || (this._isGif() && SettingsStore.getValue("autoplayGifsAndVideos"))) { thumbUrl = contentUrl; } else { thumbUrl = this._getThumbUrl(); diff --git a/src/components/views/messages/MVideoBody.tsx b/src/components/views/messages/MVideoBody.tsx index 2b4327068e..a6d849e836 100644 --- a/src/components/views/messages/MVideoBody.tsx +++ b/src/components/views/messages/MVideoBody.tsx @@ -30,7 +30,7 @@ interface IProps { /* called when the video has loaded */ onHeightChanged: () => void; /* used to refer to the local file while exporting */ - mediaSrc?: string; + forExport?: boolean; } interface IState { @@ -78,7 +78,7 @@ export default class MVideoBody extends React.PureComponent { } private getContentUrl(): string|null { - if (this.props.mediaSrc) return this.props.mediaSrc; + if (this.props.forExport) return "forExport"; const media = mediaFromContent(this.props.mxEvent.getContent()); if (media.isEncrypted) { return this.state.decryptedUrl; @@ -94,7 +94,7 @@ export default class MVideoBody extends React.PureComponent { private getThumbUrl(): string|null { // there's no need of thumbnail when the content is local - if (this.props.mediaSrc) return null; + if (this.props.forExport) return null; const content = this.props.mxEvent.getContent(); const media = mediaFromContent(content); @@ -191,7 +191,7 @@ export default class MVideoBody extends React.PureComponent { } private getFileBody = () => { - if (this.props.mediaSrc) return null; + if (this.props.forExport) return null; return ; } @@ -209,7 +209,7 @@ export default class MVideoBody extends React.PureComponent { } // Important: If we aren't autoplaying and we haven't decrypted it yet, show a video with a poster. - if (!this.props.mediaSrc && content.file !== undefined && this.state.decryptedUrl === null && autoplay) { + if (!this.props.forExport && content.file !== undefined && this.state.decryptedUrl === null && autoplay) { // Need to decrypt the attachment // The attachment is decrypted in componentDidMount. // For now add an img tag with a spinner. diff --git a/src/components/views/messages/MVoiceOrAudioBody.tsx b/src/components/views/messages/MVoiceOrAudioBody.tsx index d548d8d2fa..e5482c4b64 100644 --- a/src/components/views/messages/MVoiceOrAudioBody.tsx +++ b/src/components/views/messages/MVoiceOrAudioBody.tsx @@ -23,7 +23,7 @@ import MVoiceMessageBody from "./MVoiceMessageBody"; interface IProps { mxEvent: MatrixEvent; - mediaSrc?: string; + forExport?: boolean; } @replaceableComponent("views.messages.MVoiceOrAudioBody") @@ -31,7 +31,7 @@ export default class MVoiceOrAudioBody extends React.PureComponent { public render() { const isVoiceMessage = !!this.props.mxEvent.getContent()['org.matrix.msc2516.voice']; const voiceMessagesEnabled = SettingsStore.getValue("feature_voice_messages"); - if (isVoiceMessage && voiceMessagesEnabled && !this.props.mediaSrc) { + if (!this.props.forExport && isVoiceMessage && voiceMessagesEnabled) { return ; } else { return ; diff --git a/src/components/views/messages/MessageEvent.js b/src/components/views/messages/MessageEvent.js index 49dcc7b5e2..e53a796f70 100644 --- a/src/components/views/messages/MessageEvent.js +++ b/src/components/views/messages/MessageEvent.js @@ -44,9 +44,6 @@ export default class MessageEvent extends React.Component { /* the shape of the tile, used */ tileShape: PropTypes.string, - /* to set source to local file path during export */ - mediaSrc: PropTypes.string, - /* to set source to local file path during export */ forExport: PropTypes.bool, @@ -126,7 +123,6 @@ export default class MessageEvent extends React.Component { highlightLink={this.props.highlightLink} showUrlPreview={this.props.showUrlPreview} tileShape={this.props.tileShape} - mediaSrc={this.props.mediaSrc} forExport={this.props.forExport} maxImageHeight={this.props.maxImageHeight} replacingEventId={this.props.replacingEventId} diff --git a/src/components/views/rooms/EventTile.tsx b/src/components/views/rooms/EventTile.tsx index e1aa7ca81a..f8e3cc4887 100644 --- a/src/components/views/rooms/EventTile.tsx +++ b/src/components/views/rooms/EventTile.tsx @@ -251,12 +251,6 @@ interface IProps { forExport?: boolean; - // Used while exporting to refer to the local source rather than the online one - mediaSrc?: string; - - // Used while exporting to refer to the local avatar rather than the online one - avatarSrc?: string; - // show twelve hour timestamps isTwelveHour?: boolean; @@ -963,7 +957,7 @@ export default class EventTile extends React.Component { avatar = (
{ forExport={this.props.forExport} replacingEventId={this.props.replacingEventId} editState={this.props.editState} - mediaSrc={this.props.mediaSrc} highlights={this.props.highlights} highlightLink={this.props.highlightLink} showUrlPreview={this.props.showUrlPreview} diff --git a/src/utils/exportUtils/HtmlExport.tsx b/src/utils/exportUtils/HtmlExport.tsx index b9d4763153..8fb30b08a3 100644 --- a/src/utils/exportUtils/HtmlExport.tsx +++ b/src/utils/exportUtils/HtmlExport.tsx @@ -257,11 +257,10 @@ export default class HTMLExporter extends Exporter { } - protected async getEventTile(mxEv: MatrixEvent, continuation: boolean, mediaSrc?: string) { + protected async getEventTile(mxEv: MatrixEvent, continuation: boolean, filePath?: string) { const hasAvatar = this.hasAvatar(mxEv); if (hasAvatar) await this.saveAvatarIfNeeded(mxEv); - - return
  • + const eventTile =
  • false} isTwelveHour={false} last={false} - mediaSrc={mediaSrc} - avatarSrc={hasAvatar ? `users/${mxEv.sender.userId}` : null} lastInSection={false} permalinkCreator={this.permalinkCreator} lastSuccessful={false} @@ -289,10 +286,16 @@ export default class HTMLExporter extends Exporter { />
  • + let eventTileMarkup = renderToStaticMarkup(eventTile); + if (filePath) eventTileMarkup = eventTileMarkup.replace(/(src=|href=)"forExport"/, `$1"${filePath}"`); + if (hasAvatar) { + eventTileMarkup = eventTileMarkup.replace(/src="AvatarForExport"/, `src="users/${mxEv.sender.userId}"`); + } + return eventTileMarkup; } protected async createMessageBody(mxEv: MatrixEvent, joined = false) { - let eventTile: JSX.Element; + let eventTile: string; const attachmentTypes = ["m.sticker", "m.image", "m.file", "m.video", "m.audio"] if (mxEv.getType() === attachmentTypes[0] || attachmentTypes.includes(mxEv.getContent().msgtype)) { @@ -302,7 +305,7 @@ export default class HTMLExporter extends Exporter { this.zip.file(filePath, blob); } else eventTile = await this.getEventTile(mxEv, joined); - return renderToStaticMarkup(eventTile); + return eventTile; } protected async createHTML(events: MatrixEvent[]) {