Skip to content

Commit

Permalink
Fix send/edit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-nadymov committed Mar 31, 2020
1 parent 61dd28d commit 7634e33
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 65 deletions.
86 changes: 38 additions & 48 deletions src/Components/ColumnMiddle/InputBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,26 @@ class InputBox extends Component {
this.attachPhotoRef.current.click();
};

async getNewItem(file, sendAsFile) {
if (!file) return null;

const caption = this.newMessageRef.current.innerHTML;
if (caption) {
this.newMessageRef.current.innerHTML = null;
this.handleInput();
}

const media = sendAsFile
? await getMediaPhotoFromFile(file)
: await getMediaDocumentFromFile(file);

return {
file,
media,
caption
}
};

handleAttachPhotoComplete = async () => {
const { files } = this.attachPhotoRef.current;
if (files.length === 0) return;
Expand All @@ -518,17 +538,11 @@ class InputBox extends Component {
const [ newFile, ...rest ] = Array.from(files);
if (!newFile) return;

const newCaption = this.newMessageRef.current.innerHTML;
if (newCaption) {
this.newMessageRef.current.innerHTML = null;
}
const newItem = await this.getNewItem(newFile, true);

const newMedia = await getMediaPhotoFromFile(newFile);
this.setState({
openEditMedia: true,
newFile,
newMedia,
newCaption
newItem
});
} else {
Array.from(files).forEach(async file => {
Expand Down Expand Up @@ -562,17 +576,11 @@ class InputBox extends Component {
const [ newFile, ...rest ] = Array.from(files);
if (!newFile) return;

const newCaption = this.newMessageRef.current.innerHTML;
if (newCaption) {
this.newMessageRef.current.innerHTML = null;
}
const newItem = await this.getNewItem(newFile, false);

const newMedia = await getMediaDocumentFromFile(newFile);
this.setState({
openEditMedia: true,
newFile,
newMedia,
newCaption
newItem
});
} else {
Array.from(files).forEach(file => {
Expand Down Expand Up @@ -858,26 +866,13 @@ class InputBox extends Component {
if (!files.length) return;

if (files.length === 1) {
const newFile = files[0];
if (newFile.type.startsWith('image')) {
const newMedia = await getMediaPhotoFromFile(newFile);
if (newMedia) {
this.setState({
newMedia,
newFile,
openEditMedia: true
});
}
} else {
const newMedia = await getMediaDocumentFromFile(newFile);
if (newMedia) {
this.setState({
newMedia,
newFile,
openEditMedia: true
});
}
}
const newItem = await this.getNewItem(files[0], files[0].type.startsWith('image'));
if (!newItem) return;

this.setState({
openEditMedia: true,
newItem
});
} else {
this.setState({ files });
}
Expand Down Expand Up @@ -1221,19 +1216,18 @@ class InputBox extends Component {
};

closeEditMediaDialog(cancel = true) {
const { newCaption } = this.state;
const { newItem } = this.state;

this.setState(
{
openEditMedia: false,
newCaption: null,
newFile: null,
newMedia: null
newItem: null
},
() => {
if (cancel && newCaption) {
this.newMessageRef.current.innerHTML = newCaption;
if (cancel && newItem && newItem.caption) {
this.newMessageRef.current.innerHTML = newItem.caption;
this.focusInput();
this.handleInput();
} else {
this.restoreSelection();
}
Expand All @@ -1250,9 +1244,7 @@ class InputBox extends Component {
const {
chatId,
editMessageId,
newFile,
newMedia,
newCaption,
newItem,
replyToMessageId,
files,
newDraft,
Expand Down Expand Up @@ -1352,9 +1344,7 @@ class InputBox extends Component {
open={openEditMedia}
chatId={chatId}
messageId={editMessageId}
newFile={newFile}
newMedia={newMedia}
newCaption={newCaption}
newItem={newItem}
onEdit={this.handleEditMedia}
onSend={this.handleSendMedia}
onCancel={this.handleCancelEditMedia}
Expand Down
34 changes: 17 additions & 17 deletions src/Components/Popup/EditMediaDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class EditMediaDialog extends React.Component {

static getDerivedStateFromProps(props, state) {
const { prevOpen } = state;
const { open, chatId, messageId, newMedia } = props;
const { open, chatId, messageId, newItem } = props;

if (prevOpen !== open) {
if (open) {
const editMessage = MessageStore.get(chatId, messageId);
let sendAsPhoto = false;
if (editMessage && editMessage.content['@type'] === 'messagePhoto') {
sendAsPhoto = true;
} else if (newMedia && newMedia['@type'] === 'messagePhoto'){
} else if (newItem && newItem.media && newItem.media['@type'] === 'messagePhoto'){
sendAsPhoto = true;
}

Expand Down Expand Up @@ -86,7 +86,7 @@ class EditMediaDialog extends React.Component {
};

handleEnter = () => {
const { chatId, messageId, open, newCaption } = this.props;
const { chatId, messageId, open, newItem } = this.props;
if (!open) return;

let text = null;
Expand All @@ -107,8 +107,8 @@ class EditMediaDialog extends React.Component {
this.setFormattedText(text);
} else if (caption) {
this.setFormattedText(caption);
} else if (newCaption) {
element.innerHTML = newCaption;
} else if (newItem && newItem.caption) {
element.innerHTML = newItem.caption;
} else {
element.innerText = null;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ class EditMediaDialog extends React.Component {
}

handleDone = () => {
const { chatId, newFile, newMedia, onSend, onEdit } = this.props;
const { chatId, newItem, onSend, onEdit } = this.props;
const { editMessage, editFile, editMedia, sendAsPhoto } = this.state;

const element = this.captionRef.current;
Expand Down Expand Up @@ -184,7 +184,8 @@ class EditMediaDialog extends React.Component {

editMessageAction(chatId, 0);
} else {
const { photo, document } = newMedia;
const { media, file } = newItem;
const { photo, document } = media;

let content = null;
if (photo) {
Expand All @@ -198,28 +199,28 @@ class EditMediaDialog extends React.Component {
content = sendAsPhoto
? {
'@type': 'inputMessagePhoto',
photo: { '@type': 'inputFileBlob', name: newFile.name, data: newFile },
photo: { '@type': 'inputFileBlob', name: file.name, data: file },
width,
height,
caption
}
: {
'@type': 'inputMessageDocument',
document: { '@type': 'inputFileBlob', name: newFile.name, data: newFile },
document: { '@type': 'inputFileBlob', name: file.name, data: file },
thumbnail: null,
caption
};
} else if (document) {
content = {
'@type': 'inputMessageDocument',
document: { '@type': 'inputFileBlob', name: newFile.name, data: newFile },
document: { '@type': 'inputFileBlob', name: file.name, data: file },
thumbnail: null,
caption
};
}
if (!content) return;

onSend(content, newFile);
onSend(content, file);
}
};

Expand Down Expand Up @@ -513,14 +514,14 @@ class EditMediaDialog extends React.Component {
};

render() {
const { chatId, messageId, newMedia, open, t } = this.props;
const { chatId, messageId, newItem, open, t } = this.props;
const { defaultText, defaultUrl, openEditUrl, editMessage, editMedia, sendAsPhoto } = this.state;
if (!open) return null;

const message = MessageStore.get(chatId, messageId);
const isEditing = Boolean(message);
let isPhoto = false;
if (newMedia && newMedia['@type'] === 'messagePhoto') {
if (newItem && newItem.media && newItem.media['@type'] === 'messagePhoto') {
isPhoto = true;
} else if (editMedia && editMedia['@type'] === 'messagePhoto') {
isPhoto = true;
Expand All @@ -534,8 +535,8 @@ class EditMediaDialog extends React.Component {
editMedia
? getMedia({ content: editMedia })
: getMedia(message, null);
} else {
media = getMedia({ content: newMedia });
} else if (newItem) {
media = getMedia({ content: newItem.media });
}
const doneLabel = isEditing ? t('Edit') : t('Send');

Expand Down Expand Up @@ -609,8 +610,7 @@ EditMediaDialog.propTypes = {

chatId: PropTypes.number,
messageId: PropTypes.number,
newFile: PropTypes.instanceOf(File),
newMedia: PropTypes.object,
newItem: PropTypes.object,

onSend: PropTypes.func,
onEdit: PropTypes.func,
Expand Down

0 comments on commit 7634e33

Please sign in to comment.