目录

Comment Dialog Box

Overview

The comment module is widely used in image-text and video applications. It consists of an editing area, a friend list, a common emoji list, and an emoji panel (as shown in the figure below). It allows users to type texts, add emojis, mention friends, and select images. This module is generally displayed in the form of a dialog box that directly pops up on image-text and video pages, or above the comment list. This document provides guidance on your development from the following aspects by focusing on the relatively complex scenario where the dialog box pops up above the comment list:

Figure 1 Display effect

The following table lists the module names used in this document.

Module Name

No. in Figure 1

Comment list

1

Friend list

2

Editing area

3

Common emoji list

4

Soft keyboard

5

Emoji panel

6

References:

Implementation Principles

[h2]Dialog Box Components

After trying the CustomDialog, bindSheet, and Navigation Dialog (dialog box type in Page Display Mode) solutions, it is found that the custom dialog box and semi-modal dialog box have certain specifications and may cause some unavoidable issues. Therefore, the Navigation Dialog solution (dialog box type in Page Display Mode) is chosen to implement the comment dialog box. The following describes the advantages and disadvantages of the three solutions.

Navigation Dialog (dialog box type in Page Display Mode) is at lower z-index hierarchy. If you use this solution, the comment module will be displayed under other dialog box modules that use CustomDialog and bindSheet, such as the comment list. Therefore, the other modules should also use Navigation Dialog (dialog box type in Page Display Mode )to implement unified hierarchy control by the navigation stack.

[h2]Editing Area Components and Implementation

In the editing area, users can type texts, add emojis, and mention friends. When it comes to components that offer both interactive text editing and mixed image and text layouts, RichEditor stands out as the ideal option. To implement the adding of emojis, you can use RichEditorController.addImageSpan. To implement the mentioning of friends, you can use either RichEditorController.addTextSpan or RichEditorController.addBuilderSpan. After the following analysis and comparison of the two methods, addBuilderSpan is used.

In the following content, the content added to the editing area using addTextSpan, addImageSpan, and addBuilderSpan is named textSpan, imageSpan, and builderSpan, respectively.

Key Implementation

[h2]Dialog Box Display

Tap the message button on a video page to display the comment list, and tap the comment button on the comment list page to display the comment module.

You can set the mode attribute of Navigation to NavigationMode.Stack. To display the dialog box in full screen, Navigation should be the outermost component.

// features/home/src/main/ets/view/Home.ets
build() {
  Navigation(this.navDialogPageInfos) {
    // ...
  }
  .hideTitleBar(true)
  .mode(NavigationMode.Stack)
  .navDestination(this.NavDialogPageMap)
}

Wrap the dialog module by using NavDestination. Set the mode property of NavDestination to NavDestinationMode.DIALOG and expandSafeArea to [SafeAreaType.KEYBOARD], so that the dialog box does not need to avoid the soft keyboard, resolving the issues with CustomDialog. In addition, add a mask through the navigation stack so that users can tap the mask to close the dialog box. You can also encapsulate the dialog box module into a component. The comment list and comment module can call this component without concerning their interaction, but only the content to display.

// features/home/src/main/ets/view/NavigationDialog.ets
@Component
export struct NavigationDialog {
  @Consume navDialogPageInfos: NavPathStack;
  @Prop alignContent: Alignment = Alignment.Bottom;
  @Prop maskBackgroundColor: ResourceColor

  @Builder
  DefaultContentBuilder() {}

  @BuilderParam
  contentBuilderParam: () => void = this.DefaultContentBuilder

  onClose = () => {
    this.navDialogPageInfos.pop();
  }

  build() {
    NavDestination() {
      Stack() {
        Column()
          .height('100%')
          .width('100%')
          .backgroundColor(this.maskBackgroundColor)
          .onClick(this.onClose)
        this.contentBuilderParam()
      }
      .height('100%')
      .alignContent(this.alignContent)
    }
    .mode(NavDestinationMode.DIALOG)
    .hideTitleBar(true)
    .expandSafeArea([SafeAreaType.KEYBOARD])
  }
}

Use the navigation stack to open or close the dialog box. The dialog box hierarchy is determined by the stack sequence.

[h2]Switching Between the Soft Keyboard and Emoji Panel

After users touch the emoji icon button, the soft keyboard is switched to the emoji panel, and the button is switched from the emoji icon to the keyboard icon. After users touch the keyboard icon button, the emoji panel is switched to the soft keyboard, and the button is switched from the keyboard icon to the emoji icon.

In this document, a custom keyboard is used to control the switching between the soft keyboard and emoji panel. You can set RichEditor.customKeyboard to the EmojiKeyboard component's constructor to display the emoji panel. Setting this property to undefined will display the default soft keyboard. In this way, when the soft keyboard is switched to the emoji panel, the focus of the richEditor does not need to be manually processed.

// features/home/src/main/ets/view/CommentKeyboard.ets
RichEditor({ controller: this.richEditorController })
  .customKeyboard(this.isEmojiKeyboardVisible ? this.EmojiKeyboard() : undefined)
  // ...

To ensure that the overall comment module height remains unchanged during the switching, you must obtain the soft keyboard height to calculate and manually set the emoji panel height. The soft keyboard height may be manually changed. Therefore, you need to use the keyboardHeightChange event to listen for soft keyboard height changes. If the height is greater than 0, update the state variable that records the soft keyboard height. Note that this listening event must be canceled before the component is destroyed.

aboutToAppear(): void {
  window.getLastWindow(this.getUIContext().getHostContext()).then(win => {
    this.addKeyboardHeightListener(win);
  }).catch((err: BusinessError) => {
    Logger.error(TAG,
      `getLastWindow Failed. Code:${err.code}, message:${err.message}`);
  });
}

aboutToDisappear(): void {
  window.getLastWindow(this.getUIContext().getHostContext()).then(win => {
    this.removeKeyboardHeightListener(win);
  }).catch((err: BusinessError) => {
    Logger.error(TAG,
      `getLastWindow Failed. Code:${err.code}, message:${err.message}`);
  });
}

getResourceString(resource: Resource): string {
  try {
    return this.getUIContext().getHostContext()!.resourceManager.getStringSync(resource.id);
  } catch (exception) {
    Logger.error(TAG,
      `getLastWindow Failed. Code:${exception.code}, message:${exception.message}`);
    return '';
  }
}

addKeyboardHeightListener(win: window.Window) {
  win.on('keyboardHeightChange', height => {
    Logger.info(TAG, 'keyboard height has changed', this.getUIContext().px2vp(height));
    if (height !== 0) {
      this.keyboardHeight = this.getUIContext().px2vp(height);
      return;
    }
    // ...
  });
}

removeKeyboardHeightListener(win: window.Window) {
  win.off('keyboardHeightChange');
}

As shown in Figure 1, the emoji panel height is equal to the emoji list height plus the soft keyboard height. As the dialog box does not avoid the soft keyboard and the custom keyboard, when the soft keyboard is used, a placeholder element is required to push up the area above the soft keyboard, with its height set to the soft keyboard height. Similarly, when the emoji panel is used, the placeholder height should be the emoji panel height (which is the common emoji list height plus the soft keyboard height).

Adaptation code for the comment dialog box height:

// features/home/src/main/ets/view/CommentKeyboard.ets
build() {
  NavigationDialog({ maskBackgroundColor: 'rgba(0, 0, 0, 0.1)' }) {
    Column() {
      this.AtFriendList()
      this.ToolBar()
      Divider()
      if (!this.isEmojiKeyboardVisible) {
        this.FrequentEmojiList()
      }
      Column()
        .height(
          this.isEmojiKeyboardVisible ?
            this.keyboardHeight + this.frequentEmojiListHeight :
            this.keyboardHeight
        )
    }
    .backgroundColor(Color.White)
  }
}

Adaptation code for the emoji panel height:

// features/home/src/main/ets/view/CommentKeyboard.ets
@Builder
EmojiKeyboard() {
  Grid() {
    ForEach(this.getEmojiIcons(), (icon: Resource) => {
      GridItem() {
        Image(icon)
          .width(45)
          .onClick(() => {
            this.onEmojiClick(icon)
          })
      }
    })
  }
  .width('100%')
  .height(this.keyboardHeight + this.frequentEmojiListHeight)
  // ...
}

[h2]Editing Area

In the editing area, users can type texts, add emojis, mention friends, and select images. The following provides effect diagrams and code explanation to describe how to develop these functions.

Sample Code