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

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

HarmonyOS
2024-12-25 14:31:29
浏览
收藏 0
回答 2
待解决
回答 2
按赞同
/
按时间
凡尘一梦
1

使用 addBuildSpan实现

   this.richEditorController.addBuilderSpan(this.atBuilder)
 this.atBuilder = () => {
                        this.AtSpan(userId)
                    }
  @Builder
    AtSpan(userId: string) {
        if (userId == "all") {
            Text("@全体成员").height(30).padding({ right: 5 })

        } else {
            Text(`@${CacheViewModel.getInstance().getUserNameByUserId(Number(userId))}`)
                .height(30)
                .padding({ right: 5 })

        }

    }


分享
微博
QQ
微信
回复
2024-12-27 15:04:38
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
微信
回复
2024-12-25 17:01:46
相关问题
Text如何实现删除线功能
1373浏览 • 1回复 待解决
【JS】如何实现左滑删除功能
3769浏览 • 1回复 待解决
如何实现ArrayList删除、去重等功能
652浏览 • 1回复 待解决
侧滑删除功能列表有哪些?
1103浏览 • 1回复 待解决
HarmonyOS元服务添加、删除功能
1119浏览 • 1回复 待解决
HarmonyOS 生成faultlog如何删除
420浏览 • 1回复 待解决