Velo Tutorial: Creating a Rich Text Editor Using the HTML Component

Before reading this article, you might want to learn about Working with the HTML Component in Velo.

In this article, we demonstrate how to use an HTML Component to add a rich text editor to your page. 

We start by creating the following page:

The left side of the page is a Wix Text element.

The right side of the page is created with an HTML Component. The component contains HTML that creates a CKEditor and a button. 

Using this setup, a user can enter rich text in the rich text editor, click the Submit text button, and the rich text will be displayed in the Wix text element.

ページコード

The following code is added to the code editor.

In the page's onReady() event handler, we set an event handler that runs when the page receives a message from the HTML Component. The event handler sets the text of the page's Text element to be the data sent from the HTML Component.

コピー
1

Note: We are using the Text element's html property to set its styled text. To learn about how a Text element displays content that is set using the html property, see Formatting Text Elements with Velo.

HTML Component Code

The following code is added to the HTML Component using the Enter Code or  Edit Code button. It contains three main sections.

の中で <body> tag, we define the HTML elements that make up our page:

  • A <textarea> that is used by the CKEditor to create a rich text editor.
  • A <button> that is used for sending the contents of the rich text editor to the page code.

の中で <style> tag, we define some styles that are used to restyle the button. These styles aren't necessary if you don't want to restyle the button.

の中で <script> tag, we define our messaging logic. There are two parts to the messaging logic.

First, when the HTML component loads, we use the CKEditor replace() function to turn our regular HTML <textarea> into a CKEditor rich text editor.

Next, when the button in the HTML Component is clicked, it calls the sendText() function. We define the sendText() function to send the text in the rich text editor to the page code.

コピー
1

APIリスト

この記事のコードでは、以下のAPIを使用している。詳しくはAPIリファレンスをご覧ください。

役に立ちましたか?
はい
いいえ