Skip to main content

Code Editor

WdCodeEditor

Applicable Scenarios

A code editor wrapped based on React CodeMirror. A form component used to assist with code input and display.

Basic Capabilities Description

  1. Bind a Rich Text type field. Input value, placeholder text, and component size can be configured.

  2. Supports language configuration. Currently, only JS, TypeScript, MongoDB, SQL, and MySQL are supported.

  3. Supports switching between light and dark themes.

  4. Supports personalized configuration of titles, including title content, alignment, line break behavior (when enabled, titles exceeding one line wrap; when disabled, titles exceeding one line truncate with ellipsis), title position, width, and title tooltip.

To learn more about usage scenarios for form components, refer to the Form Common Scenarios Practice Guide.

Example

Interactive Preview

Component Decoration

Component Size

Component Alignment

Component Input State

Component Style API Example

#wd-page-root .wd-form-item .wd-code-editor {
border-color: cyan;
color: cyan;
border-width: 2px;
border-radius: 6px;
--wd-color-text-placeholder: red;
}

Properties

External properties received by the component

Property Name
Property Identifier
Type
Description
Display HeadlineslabelVisibleboolean

Default value: true

Title alignmentlabelAlignstring

In the scenario, the form by default follows the title alignment configuration of the form container.

Line break in headinglabelWrapboolean

If the title content is too long when closed, show one line with overflow omitted; when enabled, show with line breaks. In form scenarios, it follows the form container's title line break configuration by default.

Title positionlayoutstring

Set title display position in form component. In the scenario, it follows the title position configuration of the form container by default.

Title widthlabelWidthstring

You can enter a value with units such as px or %, for example: 200px.

In the scenario, the form follows the title width configuration of the form container by default.

Heading NotelabelTipsstring

Take effect on PC/H5

Configure tooltip content for the heading

Prompt.extrastring

The prompt content is displayed below the input box after configuration.

automatically obtain focusfocusboolean

After enabled, the cursor automatically positions to the input box when page loading completes.

Display underscore on mobile terminalborderedH5boolean

After closing, the mobile terminal does not show the bottom underline

Default value: true

Verification rulerulesarray

Default value: []

Status.statusstring

Example: "edit"

Requiredrequiredboolean
Required identifierrequiredFlagboolean

Enabled, the component will display a required asterisk tag if mandatory.

Default value: true

Required validation noterequiredMsgstring

Example: "该项为必填项"

Bound fieldnamestring

The Key value of a form field is used to match the field identifier of the data model when submitting data. It must be unique within the form.

Example: "formCodeEditor"

Title content.labelstring

Example: "标题"

Supported Languageslanguagestring

Example: "javascript"

Topicthemestring

Example: "githubLight"

input valuevaluestring

Example: ""

Placeholder textplaceholderstring

Example: "请输入"

PC component dimensionsizestring

Size configuration is only applicable to PC and takes effect by default in form container scenarios.

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
value changechangeobject
  • value: string

    输入值

Compatible with all platforms

Trigger when a user modifies a component value

code editor readyreadyCompatible with all platforms

The code editor has completed instance initialization.

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
Bound fieldnamestring

The Key value of a form field is used to match the field identifier of the data model when submitting data. It must be unique within the form.

Title content.labelstring
input valuevaluestring
Requiredrequiredboolean
Indicates whether to displayvisibleboolean

Whether to display the component

Whether to disabledisabledboolean

Component Disabled

Specify whether it is read-only or not.readOnlyboolean

Whether the component is read-only

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
set valuesetValuestring

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

Show/Hide SettingssetVisibleboolean显示

Set the component to hidden via $w.id1.setVisible(false)

Set DisabledsetDisabledboolean禁用

Set the component to disabled with $w.id1.setDisabled(true)

Clear valueclearValue

Clear the component value with $w.id1.clearValue()

Set as read-onlysetReadOnlyboolean只读

Set the component to read-only via $w.id1.setReadOnly(true)

Trigger validationhandleValidate

Validate the component value via $w.id1.handleValidate()

Clear verificationclearValidate

Clear component validation via $w.id1.clearValidate()

format jsformatobject
  • isCustomVal: boolean自定义入参

    是否使用传入的格式化内容进行格式化。默认格式化当前组件值,开启则使用传入的格式化内容进行格式化,方法执行后返回格式化结果。

  • value: string格式化内容

Currently only support formatting component values in js

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
root element.wd-codeEditor-rootOutermost component element
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-codeEditor-root {
  /* Write CSS styles here */
}
H5 root element.wd-h5-codeEditor-rootSettable root element style for the H5 side
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-h5-codeEditor-root {
  /* Write CSS styles here */
}
PC-side root element.wd-pc-codeEditor-rootSettable root element style for the PC side
/* :scope refers to the current component element */
/* For details, refer to the Style API documentation */
:scope.wd-pc-codeEditor-root {
  /* Write CSS styles here */
}
Component title style.wd-codeEditor-root .wd-form-item-wrap__labelComponent title element

:scope .wd-form-item-wrap__label {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
form control root node style.wd-codeEditor-root .wd-form-item-wrap__controlSet form control root element style

:scope .wd-form-item-wrap__control {
  font-size: 20px;
  color: gray;
  padding: 0;
  display: flex;
  align-items: center;
}
Input box style - editing status.wd-codeEditor-root .wd-form-input-wrapComponent border, margin style, background color, font size, font color

:scope .wd-form-input-wrap {
  font-size: 20px;
  color: gray;
  text-align: right;
  height: 40px;
  width: 100%;
  background-color: lightgray;
  border-width: 2px;
  border-color: gray;
  border-radius: 0px;
}
Editing status - Verification information.wd-codeEditor-root .wd-g-text-errorSet component verification information style

:scope .wd-g-text-error {
    font-size: 20px;
    color: gray;
  }
prompt text.wd-codeEditor-root .wd-form-item__help-textSet the text style of the component prompt

:scope .wd-form-item__help-text {
    font-size: 20px;
    color: gray;
  }
Disabled state - Input box style.wd-codeEditor-root .wd-form-input-wrap.is-disabledComponent disabled style

:scope .wd-form-input-wrap.is-disabled {
    font-size: 20px;
    color: gray;
    text-align: right;
    height: 40px;
    width: 200px;
    background-color: lightgray;
    border-width: 2px;
    border-color: gray;
    border-radius: 10px;
  }
read-only status - form value style.wd-codeEditor-root .wd-form-item__readonly-valueSet component read-only status

:scope .wd-form-item__readonly-value {
    font-size: 20px;
    color: gray;
  }

Learn about Style API

Version Changes

  • Property changes
  • Style changes
  • widget api changes