HarmonyOS 使用RichEdit怎么实现@功能,删除的时候能把@的用户整个删除

使用RichEdit怎么实现@功能,删除的时候能把@的用户整个删除?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

参考示例如下:

@Entry
@Component
struct Index {
  controller: RichEditorController = new RichEditorController();
  option: RichEditorOptions = { controller: this.controller };
  @State message: string = "@test(dd)"
  @State content: string = ""
  private my_builder: CustomBuilder = () => {
    this.placeholderBuilder()
  }

  @Builder
  placeholderBuilder() {
    Row() {
      Text(this.message).fontSize(15)
        .margin({
          right: 10
        })
        .fontColor(Color.Blue)
    }
  }

  build() {
    Column() {
      Button("获取选择内容 " + this.content).onClick(() => {
        this.content = ""
        this.controller.getSpans().forEach(item => {
          if (typeof (item as RichEditorImageSpanResult)['imageStyle'] != 'undefined') {
            if ((item as RichEditorImageSpanResult).valueResourceStr == "") {
              console.info("builder span index " + (item as RichEditorImageSpanResult).spanPosition.spanIndex +
                ", range : " + (item as RichEditorImageSpanResult).offsetInSpan[0] + ", " +
              (item as RichEditorImageSpanResult).offsetInSpan[1] + ", size : " +
              (item as RichEditorImageSpanResult).imageStyle[0] + ", " +
              (item as RichEditorImageSpanResult).imageStyle[1])
            } else {
              console.info("image span " + (item as RichEditorImageSpanResult).valueResourceStr + ", index : " +
              (item as RichEditorImageSpanResult).spanPosition.spanIndex + ", range: " +
              (item as RichEditorImageSpanResult).offsetInSpan[0] + ", " +
              (item as RichEditorImageSpanResult).offsetInSpan[1] + ", size : " +
              (item as RichEditorImageSpanResult).imageStyle.size[0] + ", " +
              (item as RichEditorImageSpanResult).imageStyle.size[1])
            }
          } else {
            this.content += (item as RichEditorTextSpanResult).value;
            this.content += "\n"
            console.info("text span: " + (item as RichEditorTextSpanResult).value)
          }
        })
      })
      Column() {
        RichEditor(this.option)
          .onReady(() => {
            this.controller.addBuilderSpan(this.my_builder, { offset: 0 })
          })
          .width("100%")
          .height("30%")
      }
      .width("100%")
      .height("70%")
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
Text如何实现删除线功能
1056浏览 • 1回复 待解决
【JS】如何实现左滑删除功能
3483浏览 • 1回复 待解决
如何实现ArrayList删除、去重等功能
438浏览 • 1回复 待解决
侧滑删除功能列表有哪些?
808浏览 • 1回复 待解决
HarmonyOS元服务添加、删除功能
805浏览 • 1回复 待解决
HarmonyOS 生成faultlog如何删除
324浏览 • 1回复 待解决
怎样删除已添加Module?
813浏览 • 1回复 待解决