HarmonyOS 鸿蒙Next RichEditor通过安全组件粘贴按钮将this.content的内容改掉 输入框里面没有更新

发布于 1周前 作者 zlyuanteng 来自 鸿蒙OS

HarmonyOS 鸿蒙Next RichEditor通过安全组件粘贴按钮将this.content的内容改掉 输入框里面没有更新 RichEditor(this.options) .backgroundColor(Color.White) .borderRadius(10) .width(‘100%’) .height(‘100%’) .padding(‘12vp’) .margin({ top: ‘10vp’ }) .onReady(() => { this.controller.addTextSpan(this.content) })

通过安全组件粘贴按钮 将this.content的内容改掉

输入框里面没有更新


更多关于HarmonyOS 鸿蒙Next RichEditor通过安全组件粘贴按钮将this.content的内容改掉 输入框里面没有更新的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

icheditor不像text组件,可以通过修改contentStr变量值,实时更新显示内容。

如果要替换内容,可以先deletespans,后重新addTextSpan。 deletespans详细内容参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V13/ts-basic-components-richeditor-V13#deletespans

本地测试的安全控件粘贴和在RichEditor控件里边长按弹出的气泡里边粘贴的内容是一样的,可以使用下面的demo,测试一下粘贴的内容是否一样:

import { pasteboard, BusinessError } from '@kit.BasicServicesKit';

// xxx.ets

@Entry
@Component
struct Index {
  controller: RichEditorController = new RichEditorController();
  options: RichEditorOptions = { controller: this.controller };
  private start: number = -1;
  private end: number = -1;
  @State message: string = "[-1, -1]"
  @State content: string = ""
  @State contentStr: string = 'contentStr'

  build() {
    Column() {
      Column() {
        Text("selection range:")
          .width("100%")
        Text() {
          Span(this.message)
        }
        .width("100%")
        Text("selection content:")
          .width("100%")
        Text() {
          Span(this.content)
        }
        .width("100%")
      }
      .borderWidth(1)
      .borderColor(Color.Red)
      .width("100%")
      .height("20%")
      Row() {
        Button("更新样式:加粗").onClick(() => {
          this.controller.updateSpanStyle({
            start: this.start,
            end: this.end,
            textStyle:
            {
              fontWeight: FontWeight.Bolder
            }
          })
        })
        Button("获取选择内容").onClick(() => {
          this.content = "";
          this.controller.getSpans({
            start: this.start,
            end: this.end
          }).forEach(item => {
            if(typeof(item as RichEditorImageSpanResult)['imageStyle'] != 'undefined'){
              this.content += (item as RichEditorImageSpanResult).valueResourceStr;
              this.content += "\n"
            } else {
              if(typeof(item as RichEditorTextSpanResult)['symbolSpanStyle'] != 'undefined') {
                this.content += (item as RichEditorTextSpanResult).symbolSpanStyle?.fontSize;
                this.content += "\n"
              } else {
                this.content += (item as RichEditorTextSpanResult).value;
                this.content += "\n"
              }
            }
          })
        })
        Button("删除选择内容").onClick(() => {
          this.controller.deleteSpans({
            start: this.start,
            end: this.end
          })
          this.start = -1;
          this.end = -1;
          this.message = "[" + this.start + ", " + this.end + "]"
        })
      }
      .borderWidth(1)
      .borderColor(Color.Red)
      .width("100%")
      .height("10%")
      Column() {
        RichEditor(this.options)
          .onReady(() => {
            this.controller.addTextSpan(this.contentStr,
            {
              style:
              {
                fontColor: Color.Orange,
                fontSize: 30
              }
            })
            this.controller.addSymbolSpan($r("sys.symbol.ohos_trash"),
            {
              style:
              {
                fontSize: 30
              }
            })
            this.controller.addImageSpan($r("app.media.app_icon"),
            {
              imageStyle:
              {
                size: ["57px", "57px"]
              }
            })
            this.controller.addTextSpan("56789",
            {
              style:
              {
                fontColor: Color.Black,
                fontSize: 30
              }
            })
          })
          .onPaste(() => {
            console.log('==================')
          })
          .onSelect((value: RichEditorSelection) => {
            this.start = value.selection[0];
            this.end = value.selection[1];
            this.message = "[" + this.start + ", " + this.end + "]"
          })
          .aboutToIMEInput((value: RichEditorInsertValue) => {
            console.log("---------------------- aboutToIMEInput ----------------------")
            console.log("insertOffset:" + value.insertOffset)
            console.log("insertValue:" + value.insertValue)
            return true;
          })
          .onIMEInputComplete((value: RichEditorTextSpanResult) => {
            console.log("---------------------- onIMEInputComplete ---------------------")
            console.log("spanIndex:" + value.spanPosition.spanIndex)
            console.log("spanRange:[" + value.spanPosition.spanRange[0] + "," + value.spanPosition.spanRange[1] + "]")
            console.log("offsetInSpan:[" + value.offsetInSpan[0] + "," + value.offsetInSpan[1] + "]")
            console.log("value:" + value.value)
          })
          .aboutToDelete((value: RichEditorDeleteValue) => {
            console.log("---------------------- aboutToDelete --------------------------")
            console.log("offset:" + value.offset)
            console.log("direction:" + value.direction)
            console.log("length:" + value.length)
            value.richEditorDeleteSpans.forEach(item => {
              console.log("---------------------- item --------------------------")
              console.log("spanIndex:" + item.spanPosition.spanIndex)
              console.log("spanRange:[" + item.spanPosition.spanRange[0] + "," + item.spanPosition.spanRange[1] + "]")
              console.log("offsetInSpan:[" + item.offsetInSpan[0] + "," + item.offsetInSpan[1] + "]")
              if (typeof(item as RichEditorImageSpanResult)['imageStyle'] != 'undefined') {
                console.log("image:" + (item as RichEditorImageSpanResult).valueResourceStr)
              } else {
                console.log("text:" + (item as RichEditorTextSpanResult).value)
              }
            })
            return true;
          })
          .onDeleteComplete(() => {
            console.log("---------------------- onDeleteComplete ------------------------")
          })
          .placeholder("input...", {
            fontColor: Color.Gray,
            font: {
              size: 16,
              weight: FontWeight.Normal,
              family: "HarmonyOS Sans",
              style: FontStyle.Normal
            }
          })
          .borderWidth(1)
          .borderColor(Color.Green)
          .width("100%")
          .height("30%")
        PasteButton()
          .padding({top: 12, bottom: 12, left: 24, right: 24})
          .onClick((event: ClickEvent, result: PasteButtonOnClickResult) => {
            if (PasteButtonOnClickResult.SUCCESS === result) {
              pasteboard.getSystemPasteboard().getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
                if (err) {
                  console.error(`Failed to get paste data. Code is ${err.code}, message is ${err.message}`);
                  return;
                }
                console.log('粘贴板内容:' + pasteData.getPrimaryText())
                this.contentStr = pasteData.getPrimaryText();
                console.log(this.contentStr)
                this.controller.addTextSpan(this.contentStr,
                {
                  style:
                  {
                    fontColor: Color.Orange,
                    fontSize: 30
                  }
                })
              });
            }
          })
      }
    }
    .borderWidth(1)
    .borderColor(Color.Red)
    .width("100%")
    .height("70%")
  }
}

粘贴控件代码:

PasteButton()
  .padding({top: 12, bottom: 12, left: 24, right: 24})
  .onClick((event: ClickEvent, result: PasteButtonOnClickResult) => {
    if (PasteButtonOnClickResult.SUCCESS === result) {
      pasteboard.getSystemPasteboard().getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
        if (err) {
          console.error(`Failed to get paste data. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        let count = pasteData.getRecordCount()
        for(let i = 0; i < count; i++){
          this.controller.addTextSpan(pasteData.getRecord(i).toPlainText(),
          {
            style:
            {
              fontColor: Color.Orange,
              fontSize: 30
            }
          })
        }
      });
    }
  })

更多关于HarmonyOS 鸿蒙Next RichEditor通过安全组件粘贴按钮将this.content的内容改掉 输入框里面没有更新的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS鸿蒙Next RichEditor通过安全组件粘贴按钮将this.content的内容改掉但输入框里面没有更新的问题,可能的原因及解决方法如下:

  1. 数据绑定未正确更新:

    • 检查this.content是否与RichEditor的输入框正确绑定。在鸿蒙系统中,通常需要使用特定的数据绑定机制来确保UI与数据同步。
    • 确认在修改this.content后,是否有触发UI更新的机制。例如,使用状态管理或观察者模式来监听数据变化并更新UI。
  2. 安全组件限制:

    • 安全组件可能对数据流动有严格的限制。检查安全组件的文档,确认是否允许从特定来源或方式更新内容。
    • 如果安全组件限制了直接的数据更新,可能需要通过特定的接口或方法来触发更新。
  3. UI刷新问题:

    • 尝试手动刷新UI组件,看是否能解决问题。在某些情况下,可能需要调用特定的刷新方法或重新加载组件。

如果以上方法均无法解决问题,可能是由于鸿蒙系统的特定行为或Bug导致的。此时,建议直接联系鸿蒙系统的开发者支持或查看官方文档以获取更具体的帮助。如果问题依旧没法解决请联系官网客服,官网地址是: https://www.itying.com/category-93-b0.html

回到顶部