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