文档开发者中心
我的

基于Span的RichEditor组件指定光标位置进行插入自定义组件

浏览62发布于2026-04-12 15:36广东
本文章最后由 云斓阿孤 于 2026-04-13 13:24 编辑

记录日常,防止忘记

(1)通过设置 RichEditor 组件的 onSelectionChange 属性对光标位置进行更新,方便后续插入组件。

(2)通过在 onClick 函数里面定义局部变量,记录光标位置也可以直接作为插入偏移量(更直接)。

【1】代码:

收起
自动换行
深色代码主题
复制
@ComponentV2
struct CustomEditorComponent {
  richEditorController: RichEditorController = new RichEditorController();
  basedOnSpanEditorController: RichEditorOptions = { controller: this.richEditorController };

  private myBuilder: CustomBuilder = undefined;
  private selection = this.basedOnSpanEditorController.controller.getCaretOffset();

  @Builder
  CustomComponent(text: string) {
    Row() {
      Text(text)
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(VerticalAlign.Center)
    .width(120)
    .height(40)
    .margin({
      left: 3,
      right: 3
    })
    .borderRadius(16)
    .backgroundColor('#ffffff')
  }

  build() {
    Column({ space: 20 }) {
      Row() {
        Button('添加自定义组件')
          .fontColor('#ffffffff')
          .fontWeight(600)
          .type(ButtonType.Normal)
          .borderRadius(12)
          .backgroundColor('#ff0ea4b3')
          .onClick(() => {
            this.myBuilder = () => { this.CustomComponent('自定义组件'); }
            // 拿到光标位置进行插入
            this.basedOnSpanEditorController.controller.addBuilderSpan(this.myBuilder, {
              offset: this.selection
            });
          })
      }
      .padding({ left: 10 })
      .width('90%')
      .height(60)
      .borderRadius(16)
      .backgroundColor('#ffe9e9e9')

      RichEditor(this.basedOnSpanEditorController)
        .width('100%').height('50%')
        .borderRadius(8)
        .backgroundColor('#ffd9cbb3')
        .padding({
          top: 15,
          left: 10, right: 10,
          bottom: 15
        })
        // 更新光标位置
        .onSelectionChange(() => {
          this.selection = this.basedOnSpanEditorController.controller.getCaretOffset();
        })
    }
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .width('100%')
    .height('100%')
    .padding(20)
    .backgroundColor(Color.White)
  }
}

【2】代码:

收起
自动换行
深色代码主题
复制
      Row() {
        Button('添加自定义组件')
          .fontColor('#ffffffff')
          .fontWeight(600)
          .type(ButtonType.Normal)
          .borderRadius(12)
          .backgroundColor('#ff0ea4b3')
          .onClick(() => {
            // 定义局部变量记录光标位置
            let insertOffset = this.richEditorController.getCaretOffset();
            this.myBuilder = () => { this.CustomComponent('自定义组件'); }
            // 拿到光标位置进行插入
            this.basedOnSpanEditorController.controller.addBuilderSpan(this.myBuilder, {
              offset: insertOffset
            });
          })
      }

效果:

attachmenttype
SVID_20260413_073559_1.mp4
download
收藏
0
回复
2
分享
全部评论
authorIcon
lorin

学习一下

回复于2026-04-13 01:52广东
authorIcon
投资堂
赞
回复于2026-04-13 01:23浙江
写评论
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。
发表