Merge pull request #3392 from matrix-org/bwindels/cider-paste-plain

New composer: fix pasting from word processors
This commit is contained in:
Bruno Windels 2019-09-06 09:22:37 +00:00 committed by GitHub
commit df4762871a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 5 deletions

View file

@ -25,10 +25,12 @@ import {
formatRangeAsQuote,
formatRangeAsCode,
formatInline,
replaceRangeAndMoveCaret,
} from '../../../editor/operations';
import {getCaretOffsetAndText, getRangeForSelection} from '../../../editor/dom';
import Autocomplete from '../rooms/Autocomplete';
import {autoCompleteCreator} from '../../../editor/parts';
import {parsePlainTextMessage} from '../../../editor/deserialize';
import {renderModel} from '../../../editor/render';
import {Room} from 'matrix-js-sdk';
import TypingStore from "../../../stores/TypingStore";
@ -172,6 +174,18 @@ export default class BasicMessageEditor extends React.Component {
this._onInput({inputType: "insertCompositionText"});
}
_onPaste = (event) => {
const {model} = this.props;
const {partCreator} = model;
const text = event.clipboardData.getData("text/plain");
if (text) {
const range = getRangeForSelection(this._editorRef, model, document.getSelection());
const parts = parsePlainTextMessage(text, partCreator);
replaceRangeAndMoveCaret(range, parts);
event.preventDefault();
}
}
_onInput = (event) => {
// ignore any input while doing IME compositions
if (this._isIMEComposing) {
@ -495,6 +509,7 @@ export default class BasicMessageEditor extends React.Component {
tabIndex="1"
onBlur={this._onBlur}
onFocus={this._onFocus}
onPaste={this._onPaste}
onKeyDown={this._onKeyDown}
ref={ref => this._editorRef = ref}
aria-label={this.props.label}