DEV Community

HarmonyOS
HarmonyOS

Posted on

How to modify indentation in RichEditor based on existing indentation

Read the original article:How to modify indentation in RichEditor based on existing indentation

Requirement Description

Open a rich text editor with existing content. How can I modify the indentation of existing paragraphs?

Background Knowledge

Implementation Steps

  1. The getParagraphs method can be used to obtain paragraphs within a specified range, and then, based on the style information within the paragraphs, retrieve the corresponding distance between the component and the edge.
  2. updateParagraphStyles for different paragraphs based on the obtained indentation values

Code Snippet / Configuration

@Entry
@Component
struct LineBreakStrategyExample {
  controller: RichEditorController = new RichEditorController();
  private spanParagraphs: RichEditorParagraphResult[] = [];
  testStr: string = '0123456789,0123456789,0123456789,0123456789,0123456789.';
  @State left: number = 0;
  @State right: number = 0;

  build() {
    Column() {
      RichEditor({ controller: this.controller })
        .onReady(() => {
          this.controller.addTextSpan(this.testStr, {
            style: {
              fontColor: Color.Black,
              fontSize: '32'
            },
            paragraphStyle: {
              textAlign: TextAlign.Start,
              lineBreakStrategy: LineBreakStrategy.GREEDY,
              leadingMargin: {
                pixelMap: undefined,
                size: [16, 6]
              }
            }
          });
        })
        .width(400)
        .height(300)
        .margin({ bottom: 20 })
        .draggable(false);

      Column({ space: 10 }) {
        Button('Get distance').onClick(() => {
          this.spanParagraphs = this.controller.getParagraphs({ start: 1, end: 30 });
          for (let i = 0; i < this.spanParagraphs.length; i++) {
            let margin: LeadingMarginPlaceholder | Dimension | undefined = this.spanParagraphs[i].style.leadingMargin;
            let str = JSON.stringify(margin);
            let jsonObj: Object | null = JSON.parse(str);
            if (jsonObj) {
              let commObj = (jsonObj as Record<string, Object>);
              let commRecord = (commObj['size'] as Record<string, Object>);
              let arrayJson = JSON.stringify(commRecord);
              // sArray is the value of indentation
              let sArray = JSON.parse(arrayJson) as Array<string>;
              this.left = Number(sArray[0].split('.')[0]);
              this.right = Number(sArray[1].split('.')[0]);
            }
          }
        });
        Button('Paragraph alignment').onClick(() => {
          this.controller.updateParagraphStyle({
            // Select the paragraphs to be updated by string
            start: -1, end: -1,
            style: {
              //The value of leadingMargin is calculated based on the values obtained above and then assigned a new value
              leadingMargin: { pixelMap: null, size: [this.left + 10, this.right + 20] }
            }
          });
        });
      };
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

2.gif

Limitations or Considerations

Constraints and Restrictions

This example supports API Version 19 Release and above.

This example supports HarmonyOS SDK Version 5.1.1 Release and above.

This example requires DevEco Studio Version 5.1.1 Release and above to compile and run.

Summarize

1.LeadingMargin cannot be directly converted to LeadingMarginPlaceholder through methods. JSON is needed here to convert between strings and objects.

2.Both the getParagraphs method and the updateParagraphStyle method involve the selected paragraph range (RichEditorRange). The unit of the selection range here is characters. For example, if the first paragraph is 10 characters long, the range 1-10 is selected. And so on.

Written by Recep Sadullah Yegin

Top comments (0)