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

136
Vistas
¿Cómo insertar texto programáticamente en el campo de texto enriquecido draft js mientras se mantiene el estilo en línea actual?

Cuando hago clic en un botón con un símbolo determinado, me gustaría colocar el símbolo en el campo de texto.

Para lograr eso, uso una función para la inserción de texto:

 insertText(characterToInsert) { let editorState = this.state.editorState; const currentContent = editorState.getCurrentContent(), currentSelection = editorState.getSelection(); let newContent = Modifier.replaceText( currentContent, currentSelection, characterToInsert ); return EditorState.push(editorState, newContent, 'insert-characters'); }

Me gustaría poner un texto de subíndice allí cuando el estilo en línea de subíndice está activado.

Traté de hacer algo como

 if (this.isSubscriptOn()) { newContent = Modifier.applyInlineStyle(newContent, currentSelection, 'SUBSCRIPT'); }

sin embargo, no sé cómo modificar el segundo argumento para que la selección apunte a los caracteres recién colocados.

¿Hay una mejor manera de abordar esto?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

El primer paso es usar la applyInlineStyle: function (contentState: ContentState, selectionState: SelectionState, inlineStyle: string) de la clase Modifier estática. Como puede ver, requiere una selección del área a la que queremos que se aplique nuestro estilo. Crearemos dicho estilo usando el método set de immutable.js :

 const textToInsertSelection = currentSelection.set('focusOffset', currentSelection.getFocusOffset() + textToInsert.length);

Luego obtendremos el OrderedSet de estilos en línea actuales y aplicaremos cada uno de ellos a la selección mencionada anteriormente:

 let inlineStyles = editorState.getCurrentInlineStyle(); inlineStyles.forEach(inLineStyle => newContent = Modifier.applyInlineStyle(newContent, textToInsertSelection, inLineStyle));

Si nos detenemos aquí, el texto se colocará en el campo de entrada mientras se selecciona (aparecerá un rectángulo azul a su alrededor). Para evitar tal comportamiento, fuercemos una selección al final del texto insertado:

 newState = EditorState.forceSelection(newState, textToInsertSelection.set('anchorOffset', textToInsertSelection.getAnchorOffset() + textToInsert.length));

Toda la función se ve así:

 insertText(textToInsert) { let editorState = this.state.editorState; const currentContent = editorState.getCurrentContent(); const currentSelection = editorState.getSelection(); let newContent = Modifier.replaceText( currentContent, currentSelection, textToInsert ); const textToInsertSelection = currentSelection.set('focusOffset', currentSelection.getFocusOffset() + textToInsert.length); let inlineStyles = editorState.getCurrentInlineStyle(); inlineStyles.forEach(inLineStyle => newContent = Modifier.applyInlineStyle(newContent, textToInsertSelection, inLineStyle)); let newState = EditorState.push(editorState, newContent, 'insert-characters'); newState = EditorState.forceSelection(newState, textToInsertSelection.set('anchorOffset', textToInsertSelection.getAnchorOffset() + textToInsert.length)); return newState; }
about 4 years ago · Juan Pablo Isaza Denunciar
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