RichEditor添加、删除、重载图片

实现添加,删除,重载图片。

HarmonyOS
2024-05-26 10:59:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
savan

使用的核心API

RichEditor

核心代码解释

import { Logger } from '../utils/Logger'; 
import inputMethod from '@ohos.inputMethod'; 
import { BusinessError } from '@ohos.base'; 
import image from '@ohos.multimedia.image' 
import testNapi from 'libentry.so' 
import resourceManager from '@ohos.resourceManager'; 
const TAG = 'demo' 
 
export const INDENT_MAX_LEVEL: number = 5; // 缩进最大的级数 
  
@Entry 
@Component 
struct Index { 
  controller: RichEditorController = new RichEditorController(); 
  @State pixelMap: image.PixelMap | Resource | undefined = undefined; 
  @State fontSize: number = 16; 
  @State fontColor: number = 0; 
  @State colorNum: number = 0; 
  @State styleNum: number = 0; 
  @State weightNum: number = 0; 
  @State decorationNum: number = 0; 
  @State numberA: number = 10; 
  @State curCareOffset: number = 0; // 当前光标的位置 
  // @State contentSelection: RichEditorSelection = null; 
  @State contentSelection: RichEditorSelection | null = null; 
  @State isCheck: boolean = false; 
  // @State pixelMap: undefined | image.PixelMap | Resource = undefined; 
  @State someFlag: boolean = true; 
  
  aboutToAppear() { 
    Logger.info(TAG, `i'm about to appear: `); 
  } 
  
  onPageShow() { 
    Logger.info(TAG, `I am show `); 
  } 
  
  build() { 
    Column() { 
      Scroll() { 
        RichEditor({ controller: this.controller }) 
          .width('100%') 
          .layoutWeight(1) 
          .focusable(true) 
          .aboutToIMEInput((value) => { 
            Logger.info(TAG, `aboutToIMEInput: ${JSON.stringify(value)}`); 
            return true; 
          }) 
          .onDragMove((event: DragEvent, extraParams: string) => { 
            Logger.debug(TAG, 'onDragMoving'); 
          }) 
          .onDrop((dragEvent: DragEvent, extraParams: string) => { 
            Logger.debug(TAG, 'onDrop'); 
          }) 
          .onIMEInputComplete((value) => { 
            Logger.info(TAG, `someMessage return value:++ ${JSON.stringify(value.value)} ++`); 
            this.controller.getSpans().forEach((span:RichEditorImageSpanResult | RichEditorTextSpanResult) => { 
              const nSpan:RichEditorTextSpanResult = span as RichEditorTextSpanResult; 
              Logger.info(TAG, `someMessage uispan:${JSON.stringify(nSpan)}`); 
            }) 
            Logger.info(TAG, `someMessage end :=====================================`); 
  
            // ----------------------------------------- 
          }) 
          .aboutToDelete((value) => { 
            Logger.info(TAG, `aboutToDelete: ${JSON.stringify(value)}`); 
            return true; 
          }) 
          .onDeleteComplete(() => { 
            Logger.info(TAG, `onDeleteComplete`); 
          }) 
          .onSelect((selection: RichEditorSelection) => { 
            Logger.info(TAG, `selection0:${selection.selection[0]}`); 
            Logger.info(TAG, `selection1:${selection.selection[1]}`); 
            Logger.info(TAG, `selectionSpan:${JSON.stringify(selection)}`); 
            this.contentSelection = selection; 
            Logger.info(TAG, `careoffset when multipul select:${this.controller.getCaretOffset()}`); 
            Logger.info(TAG, `haha`); 
          }) 
          .height('100%') 
          .borderWidth(1) 
      } 
      .width('100%') 
      .height('40%') 
      .borderWidth(1) 
  
      Row() { 
        Column() { 
          Row() { 
            Button("add image").onClick((v: ClickEvent) => { 
              this.controller.addImageSpan($r('app.media.app_icon'), { 
                offset: 0, 
                imageStyle: { 
                  size: [(100 + 'vp') as Dimension, (100 + 'vp') as Dimension], 
                  objectFit: ImageFit.Contain, 
                } 
              }); 
  
              Logger.info(TAG, `careoffset:${this.controller.getCaretOffset()}`) 
            }) 
  
            Button("reload image").onClick((v: ClickEvent) => { 
              this.controller.deleteSpans(); 
              this.controller.addImageSpan($r('app.media.app_icon'), { 
                offset: this.controller.getCaretOffset(), 
                imageStyle: { 
                  size: [(100 + 'vp') as Dimension, (100 + 'vp') as Dimension], 
                  objectFit: ImageFit.Contain, 
                } 
              }); 
  
              Logger.info(TAG, `careoffset:${this.controller.getCaretOffset()}`) 
            }) 
  
          }.width('100%') 
          .justifyContent(FlexAlign.SpaceAround) 
  
        } 
        .width('50%') 
        .margin({bottom:150}) 
        .borderWidth(1) 
        .borderColor(Color.Orange) 
  
      } 
      .width('100%') 
      .justifyContent(FlexAlign.SpaceAround) 
  
    }.width('100%') 
    .borderWidth(2) 
    .borderColor(Color.Gray) 
  } 
}

适配版本信息

IDE:DevEco Studio 4.1.1.300

SDK:4.1.2.1

分享
微博
QQ
微信
回复
2024-05-27 10:40:12
相关问题
怎样删除添加的Module?
281浏览 • 1回复 待解决
如何给按钮添加图片
2317浏览 • 3回复 待解决
图片如何添加渐变模糊
785浏览 • 1回复 待解决
TabList可以添加图片么?
6775浏览 • 3回复 待解决
鸿蒙-富文本如何添加图片
5537浏览 • 1回复 待解决
ArkTS类的方法是否支持重载
955浏览 • 1回复 待解决
关于har和hsp的热重载使用
468浏览 • 1回复 待解决
自定义组件中如何添加图片
1296浏览 • 1回复 待解决
Text换行后无法在后面添加图片
376浏览 • 1回复 待解决
RichEditor如何拿到BuilderSpan?
560浏览 • 1回复 待解决
基于RichEditor的评论编辑
240浏览 • 1回复 待解决
4.0release不支持热重载
994浏览 • 1回复 待解决
删除按钮在哪里???
5583浏览 • 2回复 待解决
请教一下如何实现函数的重载
287浏览 • 1回复 待解决
重载该如何实现?有人知道吗?
386浏览 • 1回复 待解决
RichEditor组件如何设置初始字体样式
509浏览 • 1回复 待解决