HarmonyOS RichEditor可以支持多个文字整体删除吗?

类似@功能,比如:话题@搞笑  希望删除的时候可以整体把他们一起删除掉。现在只能一个一个文字删除。

HarmonyOS
2024-10-15 11:17:24
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

自身提供的API还不行,目前配合RichEditorController是可以达到需要的,实现思路如下:

1、通过aboutToIMEInput拦截输入,输入@时手动添加Tag 。

2、在aboutToDelete识别当前删除位置是否在Tag内(demo中通过fontColor识别是否为Tag,可自定义),如果是则删除整个Tag。

样例代码如下:

@Entry  
@Component  
struct RichEditorPage {  
  controller: RichEditorController = new RichEditorController();  
  options: RichEditorOptions = { controller: this.controller };  
  private tagMap: string[] = ["搞笑", "话题", "asdasdasd"]  
  @State showTagSheet: boolean = false  
  private readonly tagColor: string = "#FF0000FF"  
  private readonly normalColor: string = "#FF000000"  
  @Builder tagSheet() {  
    Column() {  
      ForEach(this.tagMap, (item: string) => {  
        Text(item)  
          .fontSize(25)  
          .onClick(() => {  
            this.showTagSheet = false  
            let curIndex = this.controller.getCaretOffset()  
            this.controller.deleteSpans({start: curIndex - 1, end: curIndex})  
            this.controller.addTextSpan("@" + item, {  
              offset: curIndex - 1,  
              style: {  
                fontColor: this.tagColor  
              }  
            })  
            this.controller.addTextSpan(" ", {  
              offset: curIndex + item.length,  
              style: {  
                fontColor: this.normalColor,  
          }  
        })  
      })  
    Divider()  
  })  
}  
.width('100%')  
.height('100%')  
  }  
  getCurSpan(curIndex: number): RichEditorTextSpanResult | undefined {  
    let spans = this.controller.getSpans()  
    for (let i = 0; i < spans.length; i++) {  
      const textSpan = spans[i] as RichEditorTextSpanResult;  
      if (textSpan.textStyle == undefined) continue  
  let range = textSpan.spanPosition.spanRange  
  if (range[0] <= curIndex && range[1] >= curIndex) {  
    return textSpan  
  }  
}  
return undefined  
  }  
  build() {  
    RelativeContainer() {  
      RichEditor(this.options)  
        .aboutToIMEInput((value: RichEditorInsertValue) => {  
          if (value.insertValue === "@") {  
            this.showTagSheet = true  
          }  
          return true  
        })  
        .aboutToDelete((value: RichEditorDeleteValue) => {  
          let curTextSpan = this.getCurSpan(value.offset + 1)  
          if (curTextSpan && curTextSpan.textStyle.fontColor.toString() === this.tagColor.toString()) {  
            let spanRange = curTextSpan.spanPosition.spanRange  
            this.controller.deleteSpans({  
              start: spanRange[0],  
              end: spanRange[1]  
            })  
            return false  
          } else {  
            return true  
          }  
        })  
        .width('100%')  
        .height(200)  
        .backgroundColor(Color.White)  
        .id("rich_editor")  
    }  
    .backgroundColor(Color.Gray)  
    .bindSheet(this.showTagSheet, this.tagSheet(), {  
      shouldDismiss: () => {  
        this.showTagSheet = false  
      }  
    })  
    .height('100%')  
    .width('100%')  
  }  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
分享
微博
QQ
微信
回复
2024-10-15 16:05:09
相关问题
RichEditor添加、删除、重载图片
1824浏览 • 1回复 待解决
HarmonyOS so加载时可以多个文件注册
605浏览 • 1回复 待解决
ohpm仓库地址可以设置多个
927浏览 • 1回复 待解决
多个UIAbility是多个进程
3155浏览 • 1回复 待解决
多个UIAbility是多个进程
3316浏览 • 1回复 待解决