这是indexloc提供的服务,不要输入任何密码
Skip to content

If replacing the input’s content via paste, select all after titleizing #7

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

Merged
merged 3 commits into from
Dec 3, 2018
Merged
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
20 changes: 19 additions & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,43 @@ class Index extends PureComponent {
super(props)

this.state = {
replacingWithPaste: false,
value: ''
}

this.handleChange = this.handleChange.bind(this)
this.handlePaste = this.handlePaste.bind(this)
}

handleChange(event) {
const { value } = event.target
const input = this.handler.input
const idx = input.selectionStart
const { replacingWithPaste } = this.state
const caretPosition = () => {
input.selectionStart = input.selectionEnd = idx
if (replacingWithPaste) {
input.selectionStart = 0
input.selectionEnd = input.value.length
} else {
input.selectionStart = input.selectionEnd = idx
}
}

this.setState({
replacingWithPaste: false,
value: toTitle(value)
}, caretPosition)

event.preventDefault()
}

handlePaste(event) {
const { value, selectionStart, selectionEnd } = this.handler.input
if (selectionStart !== 0 || selectionEnd !== value.length) {
return
}
this.setState({ replacingWithPaste: true })
}

componentDidMount() {
if (!this.handler) {
Expand All @@ -45,6 +62,7 @@ class Index extends PureComponent {
type: 'text',
value,
onChange: this.handleChange,
onPaste: this.handlePaste,
placeholder: 'Paste or Enter Your Title',
autoComplete: 'off',
autoCorrect: 'off',
Expand Down