目录

Device Interconnectivity (RichEditor Control)

RichEditor has integrated the Device Interconnectivity capability, which allows users on a tablet or PC/2-in-1 device to invoke the camera, scanning, and gallery (image access) functions of a phone via the right-click menu.

Use Cases

You can leverage this capability to implement device interconnectivity and enable the use of camera, scanning, and gallery functions of other devices.

Constraints

This capability can be used only when the following conditions are met:

Development Procedure

Add the RichEditor component. A user can right-click within the component to open a shortcut menu and select a required capability of another device to import images. The onWillChange attribute is used to process the returned images.

@Entry
@Component
struct Index {
  controller: RichEditorController = new RichEditorController();
  options: RichEditorOptions = { controller: this.controller };

  build() {
    Column() {
      Column() {
        RichEditor(this.options)
          .onWillChange((value: RichEditorChangeValue) => {
            if (value?.replacedImageSpans[0]?.imageStyle?.objectFit != 0) {
              return true;
            }
            for (let item of value.replacedImageSpans) {
              this.controller.addImageSpan(item.valuePixelMap, {
                imageStyle: {
                  size: ['500px', '500px'],
                  layoutStyle: {
                    borderRadius: '10px'
                  }
                }
              });
            }
            return false;
          })
          .borderWidth(1)
          .borderColor(Color.Green)
          .width('100%')
          .height('100%')
      }
      .borderWidth(1)
      .borderColor(Color.Red)
      .width('100%')
      .height('70%')
    }
  }
}

The process of using RichEditor is as follows:

  1. Right-click within the RichEditor component.

  1. Select the capability to use.

  1. Wait for the remote device to take a photo and send it back.

  1. After the photo is sent back, an image appears behind the cursor.

Disabling Device Interconnectivity of RichEditor

To disable the Device Interconnectivity capability integrated into RichEditor (shown in the right-click menu), you can remove the relevant menu items through the editMenuOptions attribute. The following is the sample code:

@Entry
@Component
struct Index {
  controller: RichEditorController = new RichEditorController();
  options: RichEditorOptions = { controller: this.controller };

  build() {
    Column() {
      Column() {
        RichEditor(this.options)
          .editMenuOptions({
            onCreateMenu: (menuItems: Array<TextMenuItem>) => {
              if (menuItems.length === 0) {
                return menuItems;
              }
              let newMenuItems: TextMenuItem[] = [];
              menuItems.forEach((item, index) => {
                if (!item.id.equals(TextMenuItemId.COLLABORATION_SERVICE)) {
                  newMenuItems.push(item);
                }
              });
              return newMenuItems;
            },
            onMenuItemClick: (menuItem: TextMenuItem, textRange: TextRange) => {
              return false;
            }
          })
          .borderWidth(1)
          .borderColor(Color.Green)
          .width('100%')
          .height('100%')
      }
      .borderWidth(1)
      .borderColor(Color.Red)
      .width('100%')
      .height('70%')
    }
  }
}