代码之家  ›  专栏  ›  技术社区  ›  redoc01

react原生pell-rich编辑器:我正在尝试将编辑器中键入的url设置为转换为锚标记

  •  0
  • redoc01  · 技术社区  · 5 月前

    使用这个编辑器,我成功地将编辑器文本中的URL设置为红色,并加下划线作为锚标记。但是当我使用setContentHTML()时,光标会跳转或返回到编辑器的开头。

    这是我的代码:

    const regExp = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
    
      
      const editorOnChange = (txt) => {
        embeddedString = txt.replace(regExp, "<a style='text-decoration:underline;color:red;' href='$1' target='_blank'>$1</a>");
        console.log(embeddedString);
        
        richText.current?.setContentHTML(embeddedString);
    
      }
    
    <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"}   style={{ flex: 1, }}>
                    <RichEditor style={{ flex: 1, }}
                    
                    placeholder="Enter Comment..."
                        ref={richText}
                        //initialContentHTML={editorHtml }
                        onChange={ (txt)=> {editorOnChange(txt)}}
                    />
                </KeyboardAvoidingView>
    

    当用户键入时,编辑器如何能够保持其光标位置,而不是在用户键入时光标跳到起点?

    0 回复  |  直到 5 月前