Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
cursor moved to the start position when insert chars in contenteditable div

I was copying facebook draftjs, in which just main features were added that accepts chars native insertion, the case here that cursor moved to the start position of a line where it been typed in when inserting char in contenteditable div and then updating state.

From the console output, the 'onSelect' event handles and casts the right SelectionState from dom object which triggers twice that, the first for focusing the position of the text area, the second occurs in after 'onInput' event returns as the cursor position changes.

The issue here that after insert chars(which works), it didnt work as expected to remain cursor to the position where it was but the initial value, see console log screenshot.png. And I've no idea where the hack & trick doing this is as in relevant part of draftjs.

function onSelect(_this: Editor): void {
  console.log('SELECT')
  
  const { editorState } = _this.props
  
  if(_this._blockSelectEvent ||
     _this._latestEditorState != _this.props.editorState) {
    return
  }
  
  // forceSelection
  const selection = getCastSelection(editorState, getContentEditable(_this))
  
  let newEditorState: EditorState 

  if(selection !== editorState.getSelection()) {
    newEditorState = editorState.acceptSelection(selection)    
    _this.sync(newEditorState)
  }
}
function onBeforeInput(
  _this: Editor,
  e: SyntheticInputEvent<HTMLElement>
): void {
  console.log('BEFOREINPUT')  
  
  const editorState = _this._latestEditorState,
        { data } = e
  
  if(!data) {
    return
  }
  
  const selection = editorState.getSelection(),
        inlineStyle = editorState.getInlineStyle(),
        chars = data
  
  if(!selection.collapsed()) {}
 
  let newEditorState = replaceChars(
    editorState,
    chars,
    inlineStyle,
    false
  )

  _this._pendingEditorState = newEditorState
}

function onInput(
  _this: Editor,
  e: ?SyntheticInputEvent<>
): void {
  console.log('INPUT')

  if(_this._pendingEditorState != undefined) {
    _this.sync(_this._pendingEditorState)
    _this._pendingEditorState = undefined
  }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda