Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to allow pasting files #663

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions src/MentionsInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ const propTypes = {
forceSuggestionsAboveCursor: PropTypes.bool,
ignoreAccents: PropTypes.bool,
a11ySuggestionsListLabel: PropTypes.string,

value: PropTypes.string,
onKeyDown: PropTypes.func,
customSuggestionsContainer: PropTypes.func,
onSelect: PropTypes.func,
onBlur: PropTypes.func,
onChange: PropTypes.func,
setPastedFiles: PropTypes.func,
suggestionsPortalHost:
typeof Element === 'undefined'
? PropTypes.any
Expand Down Expand Up @@ -113,6 +113,7 @@ class MentionsInput extends React.Component {
onKeyDown: () => null,
onSelect: () => null,
onBlur: () => null,
setPastedFiles: () => null,
}

constructor(props) {
Expand Down Expand Up @@ -370,35 +371,36 @@ class MentionsInput extends React.Component {

const pastedMentions = event.clipboardData.getData('text/react-mentions')
const pastedData = event.clipboardData.getData('text/plain')
const pastedFiles = (event.clipboardData || event.originalEvent.clipboardData).files;
if (pastedFiles.length) {
this.props.setPastedFiles(pastedFiles);
} else {
const newValue = spliceString(
value,
markupStartIndex,
markupEndIndex,
pastedMentions || pastedData
).replace(/\r/g, '')
const newPlainTextValue = getPlainText(newValue, config)
const eventMock = { target: { ...event.target, value: newValue } }
this.executeOnChange(
eventMock,
newValue,
newPlainTextValue,
getMentions(newValue, config)
)

const newValue = spliceString(
value,
markupStartIndex,
markupEndIndex,
pastedMentions || pastedData
).replace(/\r/g, '')

const newPlainTextValue = getPlainText(newValue, config)

const eventMock = { target: { ...event.target, value: newValue } }

this.executeOnChange(
eventMock,
newValue,
newPlainTextValue,
getMentions(newValue, config)
)

// Move the cursor position to the end of the pasted data
const startOfMention = findStartOfMentionInPlainText(
value,
config,
selectionStart
)
const nextPos =
(startOfMention || selectionStart) +
getPlainText(pastedMentions || pastedData, config).length
this.setSelection(nextPos, nextPos)
// Move the cursor position to the end of the pasted data
const startOfMention = findStartOfMentionInPlainText(
value,
config,
selectionStart
)
const nextPos =
(startOfMention || selectionStart) +
getPlainText(pastedMentions || pastedData, config).length
this.setSelection(nextPos, nextPos)
}
}

saveSelectionToClipboard(event) {
Expand Down