Skip to content

Commit

Permalink
Merge pull request #2488 from LD4P/diacritcs-insertion
Browse files Browse the repository at this point in the history
Insert diacritics at the cursor's current position
  • Loading branch information
jermnelson committed Sep 19, 2020
2 parents 36638b2 + f0d981a commit 8ddb7ac
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/actions/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export const showDiacritics = (propertyKey) => ({
type: 'SHOW_DIACRITICS',
payload: propertyKey,
})

export const updateCursorPosition = (position) => ({
type: 'SET_CURSOR_POSITION',
payload: position,
})
10 changes: 8 additions & 2 deletions src/components/editor/diacritics/CharacterButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import React from 'react'
import PropTypes from 'prop-types'
import { useSelector, useDispatch } from 'react-redux'
import { selectLiteralInputContent } from 'selectors/inputs'
import { setLiteralContent } from 'actions/inputs'
import { setLiteralContent, updateCursorPosition } from 'actions/inputs'

const CharacterButton = (props) => {
const dispatch = useDispatch()
const targetPropertyKey = useSelector((state) => state.selectorReducer.editor.diacritics.key)
const cursorOffset = useSelector((state) => state.selectorReducer.editor.diacritics.cursorOffset)
const content = useSelector((state) => selectLiteralInputContent(state, targetPropertyKey)) || ''

const cleanCharacter = () => {
Expand All @@ -22,8 +23,13 @@ const CharacterButton = (props) => {
}

const handleClick = (event) => {
const newValue = content + cleanCharacter()
const newValue = content.slice(0, cursorOffset)
+ cleanCharacter()
+ content.slice(cursorOffset)

dispatch(setLiteralContent(targetPropertyKey, newValue))
// Move the insert position over one character
dispatch(updateCursorPosition(cursorOffset + 1))
event.preventDefault()
}

Expand Down
20 changes: 18 additions & 2 deletions src/components/editor/property/InputLiteral.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux'
import TextareaAutosize from 'react-textarea-autosize'
import {
hideDiacritics, showDiacritics,
setLiteralContent,
setLiteralContent, updateCursorPosition,
} from 'actions/inputs'
import { displayResourceValidations } from 'selectors/errors'
import InputValue from './InputValue'
Expand Down Expand Up @@ -47,6 +47,8 @@ const InputLiteral = (props) => {
props.hideDiacritics()
event.preventDefault()
}
// Handle any position changing
props.updateCursorPosition(inputLiteralRef.current.selectionStart)
}

const handleEdit = (content, lang) => {
Expand All @@ -59,6 +61,7 @@ const InputLiteral = (props) => {
if (props.shouldShowDiacritic) {
props.hideDiacritics()
} else {
props.updateCursorPosition(inputLiteralRef.current.selectionStart)
props.showDiacritics(props.property.key)
}
event.preventDefault()
Expand Down Expand Up @@ -103,6 +106,16 @@ const InputLiteral = (props) => {
}
}

// This handles if they change the cursor position within the field after the focus event.
const handleClick = () => {
props.updateCursorPosition(inputLiteralRef.current.selectionStart)
}

// This handles if they focus into the field using tab (no click)
const handleFocus = () => {
props.updateCursorPosition(inputLiteralRef.current.selectionStart)
}

return (
<div className="form-group">
<div className="input-group" onBlur={handleBlur} id={id}>
Expand All @@ -112,6 +125,8 @@ const InputLiteral = (props) => {
placeholder={props.property.propertyTemplate.label}
onChange={(event) => props.setLiteralContent(props.property.key, event.target.value)}
onKeyPress={handleKeypress}
onFocus={handleFocus}
onClick={handleClick}
value={props.content}
disabled={disabled}
ref={inputLiteralRef}
Expand All @@ -137,6 +152,7 @@ InputLiteral.propTypes = {
addValue: PropTypes.func,
content: PropTypes.string,
setLiteralContent: PropTypes.func,
updateCursorPosition: PropTypes.func,
}

const mapStateToProps = (state, ownProps) => ({
Expand All @@ -146,7 +162,7 @@ const mapStateToProps = (state, ownProps) => ({
})

const mapDispatchToProps = (dispatch) => bindActionCreators({
hideDiacritics, showDiacritics, addValue, setLiteralContent,
hideDiacritics, showDiacritics, addValue, setLiteralContent, updateCursorPosition,
}, dispatch)

export default connect(mapStateToProps, mapDispatchToProps)(InputLiteral)
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from './authenticate'
import {
setLiteralInputContent, hideDiacriticsSelection, showDiacriticsSelection,
setCursorPosition,
} from './inputs'
import {
setBaseURL, hideProperty, showProperty,
Expand Down Expand Up @@ -83,6 +84,7 @@ const handlers = {
SET_LITERAL_CONTENT: setLiteralInputContent,
SET_CURRENT_COMPONENT: setCurrentComponent,
SET_CURRENT_RESOURCE: setCurrentResource,
SET_CURSOR_POSITION: setCursorPosition,
SET_RESOURCE_GROUP: setResourceGroup,
SET_TEMPLATE_MESSAGES: setTemplateMessages,
SET_SEARCH_RESULTS: setSearchResults,
Expand Down
6 changes: 6 additions & 0 deletions src/reducers/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export const showDiacriticsSelection = (state, action) => {
newState.editor.diacritics.key = action.payload
return newState
}

export const setCursorPosition = (state, action) => {
const newState = { ...state }
newState.editor.diacritics.cursorOffset = action.payload
return newState
}

0 comments on commit 8ddb7ac

Please sign in to comment.