Skip to main content

Rich Text Editing

WdRichText

Applicable Scenarios

A rich text form component that allows adjusting text styles and supports inserting images, links, and other content.

Basic Capabilities

The rich text editing component can bind to Rich Text type fields, enabling data entry and display of queried data.

  1. After creating a data model, add a Rich Text type field in the model.
  2. In the page editor, place the "Form Container" component and bind it to the data model. The "Rich Text" field will be automatically rendered as a "Rich Text Editor" component, where rich text content can be entered.

Extended Scenarios

Rich text content can be displayed and viewed using the 'Rich Text Display' component.

After binding the "Data Details" or "Data List" component to the data model, place the "Rich Text Display" component within it and bind its content property to the Rich Text type field. This enables the display of rich text content stored in the field.

Other Scenario Practices

Refer to the Form Scenario Practice Guide to learn about various supported scenarios and implementation solutions for forms.

Example

Interactive Preview

Component Input State

Style API Example

#wd-page-root .wd-form-item.wd-pc-rich-text-root .wd-rich-text__label {
color: #05c3c3;
display: flex;
justify-content: center;
align-items: center;
}
#wd-page-root .wd-pc-rich-text-root {
color: red;
}

Properties

External properties received by the component

Property Name
Property Identifier
Type
Description
显示标题labelVisibleboolean

Default value: true

标题对齐labelAlignstring

表单场景下默认会跟随表单容器的标题对齐配置

标题换行labelWrapboolean

如果标题内容过长关闭时只显示一行、溢出省略;开启时换行展示。表单场景下默认会跟随表单容器的标题换行配置

标题位置layoutstring

设置标题在表单组件的展示位置,表单场景下默认会跟随表单容器的标题的位置配置

标题宽度labelWidthstring

您可以输入数值 + px、%等单位,示例:200px

表单场景下默认会跟随表单容器的标题宽度配置

标题提示labelTipsstring

PC/H5端生效

配置标题的工具提示内容

下方提示extrastring

配置后提示内容常显在输入框下方

移动端显示下划线borderedH5boolean

关闭后移动端不显示底部下划线

Default value: true

状态statusstring

Example: "edit"

必填requiredboolean
必填标识requiredFlagboolean

启用后,组件若要求必填,则会显示必填星号标记

Default value: true

必填校验提示requiredMsgstring

Example: "该项为必填项"

绑定字段namestring

表单字段的Key值,用于提交数据时,匹配数据模型字段标识。表单内需保证唯一。

Example: "formTextarea"

标题内容labelstring

Example: "富文本"

值存储方式storageTypestring

选择cloudID方式,文件将以云存储 fileID 的形式直接存储在数据模型中;选择https方式,需先开启云存储公有读写权限,文件将以https链接形式存储在数据模型中。

指定图片存储方式

Default value: "cloudID"

输入值valuestring

Example: ""

Events

Events exposed by the component. You can listen to component events to trigger external actions

Event Name
Event Code
Event Output Parameters event.detail
Applicable Scenarios
Description
值改变changeobject
  • value: string

    输入值

Compatible with all platforms

用户修改组件值时触发

Property API

Through the Property API, you can access the internal state and property values of components. You can access internal values using$w.componentId.propertyName, such as $w.input1.value. For details, please refer to Property API

Read-only Property Name
Property Identifier
Type
Description
绑定字段namestring

表单字段的Key值,用于提交数据时,匹配数据模型字段标识。表单内需保证唯一。

标题内容labelstring
输入值valuestring
必填requiredboolean
是否展示visibleboolean

组件是否展示

是否禁用disabledboolean

组件是否禁用

是否只读readOnlyboolean

组件是否只读

Method API

Through the Method API, you can programmatically trigger internal methods of components, such as submitting forms, displaying popups, etc. You can call component methods using $w.componentId.methodName, such as $w.form1.submit()

Method Name
Method Identifier
Parameters
Method Description
设置值setValuestring

通过 $w.id1.setValue("weda") 设置组件值

设置显隐setVisibleboolean显示

通过 $w.id1.setVisible(false) 设置组件为隐藏

设置禁用setDisabledboolean禁用

通过 $w.id1.setDisabled(true) 设置组件为禁用

清空值clearValue

通过 $w.id1.clearValue() 清空组件值

设置只读setReadOnlyboolean只读

通过 $w.id1.setReadOnly(true) 设置组件为只读

触发校验handleValidate

通过 $w.id1.handleValidate() 校验组件值

清除校验clearValidate

通过 $w.id1.clearValidate() 清除组件校验

Style API

Through the Style API, you can override the styles of internal elements in components to achieve customization. For example, in the low-code editor, you can write styles for all button components using #wd-page-root .wd-btn, and control individual component styles with :scope. For detailed instructions, please refer toStyle API

Name
Class Name
Description and Examples
根元素.wd-rich-text-root组件最外层元素
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-rich-text-root {
  /* Write CSS styles here */
}
H5 端根元素.wd-h5-rich-text-root可设定 H5 端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-h5-rich-text-root {
  /* Write CSS styles here */
}
PC 端根元素.wd-pc-rich-text-root可设定 PC 端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-pc-rich-text-root {
  /* Write CSS styles here */
}
小程序端根元素.wd-mp-rich-text-root可设定小程序端的根元素样式
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-mp-rich-text-root {
  /* Write CSS styles here */
}
组件标题样式.wd-rich-text-root .wd-form-item-wrap__label组件标题元素

:scope .wd-form-item-wrap__label {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
表单控件根节点样式.wd-rich-text-root .wd-form-item-wrap__control设置表单控件根元素样式

:scope .wd-form-item-wrap__control {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
编辑态-输入框样式.wd-rich-text-root .ExEditor-basic组件边框、边距样式

    :scope .ExEditor-basic {
      font-size: 20px;
      color: gray;
      text-align: right;
      height: 40px;
      width: 100%;
      background-color: lightgray;
    }
    :scope .exeditor-placeholder-container {
      position: unset;
    }
    /* 仅PC端生效 */
    :scope.wd-pc-rich-text-root .ExEditor-basic {
      border-width: 2px;
      border-color: gray;
      border-radius: 0px;
    }
    
编辑态-输入框样式(获取焦点).wd-rich-text-root .ExEditor-basic.ProseMirror-focused编辑态-输入框样式(获取焦点)

          :scope .ExEditor-basic.ProseMirror-focused {
background-color: lightgray;
          }
          /* 仅PC端生效 */
          :scope.wd-pc-rich-text-root .ExEditor-basic.ProseMirror-focused {
border-color: gray;
          }
          
编辑态-占位文字样式(PC/H5 端).wd-rich-text-root .exeditor-placeholder-container设置占位文字样式

    :scope .exeditor-placeholder-container {
      color: lightgray;
    }
    
禁用态-输入框样式.wd-rich-text-root .is-disabled .ExEditor-basic禁用态-输入框样式

    :scope .is-disabled .ExEditor-basic {
      font-size: 20px;
      color: gray;
      text-align: right;
      height: 40px;
      width: 100%;
      background-color: lightgray;
    }
    :scope .exeditor-placeholder-container {
      position: unset;
    }
    /* 仅PC端生效 */
    :scope.wd-pc-rich-text-root .is-disabled .ExEditor-basic {
      border-width: 2px;
      border-color: gray;
      border-radius: 0px;
    }
    
只读态-表单值样式.wd-rich-text-root .is-readonly .ExEditor-basic只状态-表单值样式

    :scope .is-readonly .ExEditor-basic {
      font-size: 20px;
      color: gray;
    }
    
编辑态-校验信息.wd-rich-text-root .wd-g-text-error设置组件校验信息样式

:scope .wd-g-text-error {
    font-size: 20px;
    color: gray;
  }
提示文字.wd-rich-text-root .wd-form-item__help-text设置组件提示文字样式

:scope .wd-form-item__help-text {
    font-size: 20px;
    color: gray;
  }

Learn about Style API

Version Changes

  • Property changes
  • Style changes
  • widget api changes

Frequently Asked Questions

Why are images uploaded in rich text displaying abnormally?

To ensure the security of your resources, the rich text image upload plugin implements anti-leech protection. Images uploaded to cloud storage use the cloud protocol, preventing users from obtaining permanent addresses.

The rich text editing component and rich text display component internally convert private images using the cloud protocol by transforming private cloud://xxxx protocol addresses into standard http protocol addresses, and then display the images using the generated temporary access links.

Therefore, if the rich text contains images obtained through its built-in upload plugin:

  • Copy the final displayed html code (the image validity period may have expired)
  • Directly use the raw html code from the database (images remain in the original cloud protocol)
  • When exporting via a template for installation, the html template data of rich text (including cloud protocol links) is exported, but the actual file data corresponding to the links is not exported simultaneously, resulting in the corresponding files being unavailable when images are displayed.
  • or other operations that bypass hotlink protection logic, etc.

may cause images to display abnormally.

Users can also flexibly convert and use images with cloud protocol stored in the database. For implementation details, refer to Obtain temporary access links for cloud storage files.

Why are externally linked images displaying abnormally?

Many websites' images check the page source, possibly because anti-leech protection is enabled. When accessed directly, there is no referrer, but when used in your page, it verifies the page source to block access. You can try using other image resources or check the loading failure information in the F12 developer tools.

How to customize the display of rich text content with images

The image addresses stored by rich text uploads use the cloud protocol. The rich text display component internally performs the following conversion to automatically achieve image display. If custom handling of image display is required, refer to the following implementation methods.

  • Define the transformValue method to match rich text image content via regular expressions and convert the image URLs.
export default function () {
// Regular expression for matching img
const regex = new RegExp(/<img [^>]*src=\\*"([^"]*?)\\*"/g);
const transformImg = (value) => {
return (value || '')
.toString()
.replace(/<img style="/g, `<img style="max-width:100%;`)
.replace(/<img /g, `<img `);
};

/**
* By invoking [$w.cloud.getTempFileURL](https://docs.cloudbase.net/lowcode/api/cloud#wcloudgettempfileurl) to obtain temporary access links for cloud storage files, converting private cloud://xxxx protocol addresses to standard http protocol addresses.
* Note: If the cloud://xxx protocol address is incorrect or lacks access permissions, this method will not return an address. Do not arbitrarily modify the default permissions of cloud storage.
*/
const getSrc = async (img) => {
const url = img.replace(regex, '$1');
if (img.includes('cloud://')) {
return await $w.cloud.getTempFileURL(url);
}
};

// Match img tags with cloud protocol image addresses using regex
const imgs = $w.richText1.value.match(regex);

let tempValue = transformImg($w.richText1.value);

if (imgs && imgs.length > 0) {
(async () => {
// Iterate through the image array to obtain http protocol addresses
const imgRes = await Promise.all(
imgs.map(async (img) => {
return await getSrc(img);
})
);
// Set the obtained http address to the image's src for display
imgs.forEach((img, index) => {
const url = img.replace(regex, '$1');
if (imgRes[index]) {
tempValue = tempValue.replace(new RegExp(url, 'g'), imgRes[index]);
}
});
$w.page.dataset.state.var1 = tempValue;
})();
}
}
  • Call the transformValue event to obtain the converted rich text value

image