Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 8695397

Browse files
committed
Support for pasting files into normal composer
We don't seem to be in any danger of getting a working RTE any time soon, so implement file pasting in the normal composer too.
1 parent 824da83 commit 8695397

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/components/views/rooms/MessageComposer.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default class MessageComposer extends React.Component {
3333
this.onHangupClick = this.onHangupClick.bind(this);
3434
this.onUploadClick = this.onUploadClick.bind(this);
3535
this.onUploadFileSelected = this.onUploadFileSelected.bind(this);
36+
this.uploadFiles = this.uploadFiles.bind(this);
3637
this.onVoiceCallClick = this.onVoiceCallClick.bind(this);
3738
this.onInputContentChanged = this.onInputContentChanged.bind(this);
3839
this.onUpArrow = this.onUpArrow.bind(this);
@@ -101,10 +102,11 @@ export default class MessageComposer extends React.Component {
101102
this.refs.uploadInput.click();
102103
}
103104

104-
onUploadFileSelected(files, isPasted) {
105-
if (!isPasted)
106-
files = files.target.files;
105+
onUploadFileSelected(files) {
106+
this.uploadFiles(files.target.files);
107+
}
107108

109+
uploadFiles(files) {
108110
let QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
109111
let TintableSvg = sdk.getComponent("elements.TintableSvg");
110112

@@ -310,7 +312,7 @@ export default class MessageComposer extends React.Component {
310312
tryComplete={this._tryComplete}
311313
onUpArrow={this.onUpArrow}
312314
onDownArrow={this.onDownArrow}
313-
onUploadFileSelected={this.onUploadFileSelected}
315+
onFilesPasted={this.uploadFiles}
314316
tabComplete={this.props.tabComplete} // used for old messagecomposerinput/tabcomplete
315317
onContentChanged={this.onInputContentChanged}
316318
onInputStateChanged={this.onInputStateChanged} />,

src/components/views/rooms/MessageComposerInput.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export default class MessageComposerInput extends React.Component {
8484
this.onAction = this.onAction.bind(this);
8585
this.handleReturn = this.handleReturn.bind(this);
8686
this.handleKeyCommand = this.handleKeyCommand.bind(this);
87-
this.handlePastedFiles = this.handlePastedFiles.bind(this);
8887
this.onEditorContentChanged = this.onEditorContentChanged.bind(this);
8988
this.setEditorState = this.setEditorState.bind(this);
9089
this.onUpArrow = this.onUpArrow.bind(this);
@@ -477,10 +476,6 @@ export default class MessageComposerInput extends React.Component {
477476
return false;
478477
}
479478

480-
handlePastedFiles(files) {
481-
this.props.onUploadFileSelected(files, true);
482-
}
483-
484479
handleReturn(ev) {
485480
if (ev.shiftKey) {
486481
this.onEditorContentChanged(RichUtils.insertSoftNewline(this.state.editorState));
@@ -734,7 +729,7 @@ export default class MessageComposerInput extends React.Component {
734729
keyBindingFn={MessageComposerInput.getKeyBinding}
735730
handleKeyCommand={this.handleKeyCommand}
736731
handleReturn={this.handleReturn}
737-
handlePastedFiles={this.handlePastedFiles}
732+
handlePastedFiles={this.props.onFilesPasted}
738733
stripPastedStyles={!this.state.isRichtextEnabled}
739734
onTab={this.onTab}
740735
onUpArrow={this.onUpArrow}
@@ -764,7 +759,7 @@ MessageComposerInput.propTypes = {
764759

765760
onDownArrow: React.PropTypes.func,
766761

767-
onUploadFileSelected: React.PropTypes.func,
762+
onFilesPasted: React.PropTypes.func,
768763

769764
// attempts to confirm currently selected completion, returns whether actually confirmed
770765
tryComplete: React.PropTypes.func,

src/components/views/rooms/MessageComposerInputOld.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export default React.createClass({
6969

7070
// The text to use a placeholder in the input box
7171
placeholder: React.PropTypes.string.isRequired,
72+
73+
// callback to handle files pasted into the composer
74+
onFilesPasted: React.PropTypes.func,
7275
},
7376

7477
componentWillMount: function() {
@@ -439,10 +442,27 @@ export default React.createClass({
439442
this.refs.textarea.focus();
440443
},
441444

445+
_onPaste: function(ev) {
446+
const items = ev.clipboardData.items;
447+
const files = [];
448+
for (const item of items) {
449+
if (item.kind === 'file') {
450+
files.push(item.getAsFile());
451+
}
452+
}
453+
if (files.length && this.props.onFilesPasted) {
454+
this.props.onFilesPasted(files);
455+
return true;
456+
}
457+
return false;
458+
},
459+
442460
render: function() {
443461
return (
444462
<div className="mx_MessageComposer_input" onClick={ this.onInputClick }>
445-
<textarea autoFocus ref="textarea" rows="1" onKeyDown={this.onKeyDown} onKeyUp={this.onKeyUp} placeholder={this.props.placeholder} />
463+
<textarea autoFocus ref="textarea" rows="1" onKeyDown={this.onKeyDown} onKeyUp={this.onKeyUp} placeholder={this.props.placeholder}
464+
onPaste={this._onPaste}
465+
/>
446466
</div>
447467
);
448468
}

0 commit comments

Comments
 (0)