目录

RichEditor限制最大输入以及粘贴的字符数

问题现象

在使用RichEditor组件时,设置了最大输入字符数限制后,在复制一段超长文本时,仍可将其粘贴到编辑器中。如何解决此问题?

背景知识

RichEditor是一个支持图文混排和文本交互式编辑的组件。当执行粘贴操作时,可以通过onPaste回调来覆盖系统默认行为,实现自定义的图文粘贴功能。此外,当RichEditor组件的内容选择区域或编辑状态下的光标位置发生变化时,会触发onSelectionChange回调;而在输入法即将输入内容之前,则会触发aboutToIMEInput回调。

解决方案

  1. 在aboutToIMEInput方法中监听输入法的输入内容。若检测到输入的字符数量超过了预设的最大字符限制,提示超长,并返回false以阻止进一步的输入操作;
  2. 使用onSelectionChange方法实时监听内容区域中的输入变化情况,并在事件触发时,将当前内容区域中已输入的字符数量获取并赋值给变量currentContentLength,以实现对输入内容长度的动态跟踪与更新;
  3. 当粘贴内容触发onPaste回调时,在该回调中将对输入框内已有的字符数量与允许的最大字符串长度进行比较,并对超长内容进行裁剪。在此过程中,将覆盖系统的默认粘贴行为,并通过addTextSpan方法将处理后的文本内容赋值给显示区域。

示例代码如下:

// 当前示例支持图片、emoji表情的计数
import pasteboard from '@ohos.pasteboard';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct RichEditorLimitedLengthDemo {
  controller: RichEditorController = new RichEditorController();
  options: RichEditorOptions = { controller: this.controller };
  @State maxContentLength: number = 10;
  @State currentTextLength: number = 0;


  // 统计当前RichEditor内容长度
  calculateCurrentTextLength(): number {
    const spans = this.controller.getSpans();
    let length = 0;
    spans?.forEach(span => {
      const textSpan = span as RichEditorTextSpanResult;
      if (textSpan.value) { // 纯文本计数
        // 过滤掉特殊标记,只计算真实文本长度
        length += this.countStrLength(textSpan.value.replace(/&&at&&|&&topic&&|&&img&&/g, ''));
      } else { // 图片计数
        length++;
      }
    });
    return length;
  }


  // 计算文本长度:由于emoji表情占据两个字符,计算长度时需要减掉emoji个数
  countStrLength(str: string): number {
    const arr = str.split('');
    // 统计emoji表情个数
    let emojiLength = 0;
    for (const item of arr) {
      if (item >= '\uD800' && item <= '\uDBFF') {
        emojiLength++;
      }
    }
    return str.length - emojiLength;
  }


  build() {
    Column() {
      RichEditor({ controller: this.controller })
        .width("100%")
        .height(186)
        .placeholder("最大输入字数:10")
        .borderRadius(8)
        .padding(15)
        .border({
          width: 1, color: Color.Pink
        })
        .onPaste((event?: PasteEvent) => {
          if (event !== undefined && event.preventDefault) {
            // 覆盖系统默认粘贴行为
            event.preventDefault();
            // 获取粘贴板数据
            pasteboard.getSystemPasteboard().getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
              if (err) {
                console.error('Failed to get PasteData. Cause: ', err.message);
                return;
              }
              this.currentTextLength = this.calculateCurrentTextLength();
              for (let index = 0; index < pasteData.getRecordCount(); index++) {
                if (this.currentTextLength >= this.maxContentLength) {
                  this.getUIContext().getPromptAction()
                    .showToast({ message: '最多输入' + this.maxContentLength + '个字' });
                  return;
                }
                if (pasteData.getRecord(index).plainText) { // 对纯文本计数
                  let text: string = pasteData.getRecord(index).plainText;
                  let copyTempLength = this.countStrLength(text);
                  let canLength = this.maxContentLength - this.currentTextLength;
                  // 当粘贴板文本长度超过最大输入限制,对内容进行裁剪
                  if (copyTempLength > canLength) {
                    this.currentTextLength = this.maxContentLength;
                    let str = this.dateTrim(text, canLength);
                    this.controller.addTextSpan(str, { style: { fontSize: 16, fontColor: Color.Black } });
                  } else {
                    this.currentTextLength += copyTempLength;
                    this.controller.addTextSpan(text, { style: { fontSize: 16, fontColor: Color.Black } });
                  }
                } else if (pasteData.getRecord(index).uri) { // 对图片进行计数
                  this.currentTextLength++;
                  this.controller.addImageSpan(pasteData.getRecord(index).uri, {
                    imageStyle: { size: ["57px", "57px"] }
                  });
                }
              }
            });
          }
        })
        .defaultFocus(true)
        .constraintSize({ maxHeight: 120 })
        .aboutToIMEInput((value: RichEditorInsertValue) => {
          let currentLength = this.calculateCurrentTextLength();
          if (currentLength >= this.maxContentLength) {
            this.getUIContext().getPromptAction().showToast({ message: '最多输入' + this.maxContentLength + '个字' });
            return false;
          }
          const cleanInsertValue = value.insertValue.replace(/&&at&&|&&topic&&|&&img&&/g, '');
          let canLength = this.maxContentLength - currentLength;
          // 当输入文本小于等于最大字符限制,直接输入;否则,做内容裁剪
          if (canLength >= this.countStrLength(cleanInsertValue)) {
            return true;
          }
          let str = this.dateTrim(cleanInsertValue, canLength);
          this.controller.addTextSpan(str, { offset: this.controller.getCaretOffset() });
          this.getUIContext().getPromptAction().showToast({ message: '内容已截取至' + this.maxContentLength + '个字' });
          return false;
        })
        .onReady(() => {
          this.controller.addImageSpan($r("app.media.startIcon"), {
            imageStyle: { size: ["57px", "57px"] }
          });
        })
        .onSelectionChange((value: RichEditorRange) => {
          console.info("当前内容长度: ", this.calculateCurrentTextLength());
        });
    };
  }


  // 对超长内容进行裁剪
  // value:待裁剪内容;canLength:待裁剪长度
  dateTrim(value: string, canLength: number): string {
    let str = '';
    let length = 0;
    for (const item of value.split('')) {
      length++;
      // emoji表情占2位字符长度,需要减一处理
      if (item >= '\uD800' && item <= '\uDBFF' && length <= canLength) {
        length--;
      }
      if (length <= canLength) {
        str = str.concat(item);
      }
    }
    return str;
  }
}