RichEditor支持输入带有样式的图文,在复制有样式的图文后,粘贴时如何去除复制的图文样式?
实现的思路如下:
参考代码如下:
import pasteboard from '@ohos.pasteboard';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct richEditorPage {
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
build() {
RelativeContainer() {
RichEditor(this.options)
.width(300)
.height(400)
.backgroundColor('#fafafa')
// 粘贴时,触发回调。
.onPaste((event?: PasteEvent) => {
if (event !== undefined && event.preventDefault) {
// 阻止默认粘贴行为
event.preventDefault();
// 获取系统剪贴板实例
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
// 获取剪贴板数据
systemPasteboard.getData((err: BusinessError, pasteData: pasteboard.PasteData) => {
if (err) {
console.error(`Failed to get PasteData. Code:${err.code} ,message:${err.message}`);
return;
}
// 提取纯文本内容
let text: string = pasteData.getPrimaryText();
this.controller.addTextSpan(text, { style: { fontSize: 16, fontColor: Color.Gray } });
});
}
})
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
});
}
.height('100%')
.width('100%');
}
} 合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功