RichText
RichText 편집 내용은 블록의 속성에 즉시 저장되며 Gutenberg의 실행 취소 및 다시 실행 기록에 포함됩니다. 여기에는 반복 행 안의 RichText도 포함됩니다. 여러 블록을 선택한 상태로 저장해도 각 블록의 콘텐츠와 속성이 유지됩니다.
RichText 컴포넌트를 사용하면 편집기 안에 편집 가능한 입력란을 렌더링하고 프런트엔드에는 정적인 HTML 요소를 표시할 수 있습니다. 이 컴포넌트는 RichText 컴포넌트WordPress를 기반으로 합니다.
기본 사용법
RichText을 가장 기본적인 형태로 사용하려면 block.json에 적절한 속성을 추가하고 블록에 RichText 컴포넌트를 추가해야 합니다.
{
"$schema": "https://blockstudio.dev/schema/block",
"name": "blockstudio/rich-text",
"title": "Native Block",
"category": "text",
"icon": "star-filled",
"description": "Native Blockstudio RichText block.",
"blockstudio": {
"attributes": [
{
"id": "myRichText",
"type": "richtext"
}
]
}
}
<RichText attribute="myRichText" />
블록에서 RichText 컴포넌트를 원하는 만큼 사용할 수 있습니다.
속성
allowedFormats
allowedFormats 속성을 사용해 RichText 컴포넌트에서 사용할 수 있는 서식을 제한할 수 있습니다.
PHP
<RichText attribute="myRichText" allowedFormats="<?php echo esc_attr(wp_json_encode(['core/bold', 'core/italic'])); ?>" />
Twig
<RichText attribute="myRichText" allowedFormats="{{ ['core/bold', 'core/italic']|json_encode|escape('html_attr') }}" />
플레이스홀더
placeholder 속성을 사용해 RichText 컴포넌트의 플레이스홀더를 설정할 수 있습니다.
<RichText attribute="myRichText" placeholder="Enter some text" />
preserveWhiteSpace
값의 공백 문자를 유지할지 여부입니다. 일반적으로 탭, 줄 바꿈 및 공백 문자는 하나의 공백으로 축약됩니다. 이 옵션을 켜면 부드러운 줄 바꿈이 줄 바꿈 요소가 아닌 줄 바꿈 문자로 저장됩니다.
<RichText attribute="myRichText" preserveWhiteSpace="true" />
태그
기본적으로 RichText 콘텐츠는 p 요소 안에 렌더링됩니다. tag 속성을 사용해 이를 변경할 수 있습니다.
<RichText attribute="myRichText" tag="h1" />
withoutInteractiveFormatting
기본적으로 모든 서식 컨트롤이 표시됩니다. 이 설정을 사용해 콘텐츠를 대화형으로 만드는 서식 컨트롤을 제거할 수 있습니다.
<RichText attribute="myRichText" withoutInteractiveFormatting="true" />
반복 필드
RichText는 반복 필드 안에서도 작동합니다. 올바른 행을 지정하려면 루프 인덱스를 사용한 대괄호 표기법을 사용하세요.
{
"blockstudio": {
"attributes": [
{
"id": "items",
"type": "repeater",
"attributes": [
{
"id": "heading",
"type": "richtext"
}
]
}
]
}
}
<?php foreach ($a['items'] as $index => $item) : ?>
<RichText tag="h2" attribute="items[<?php echo $index; ?>].heading" placeholder="Enter heading" />
<?php endforeach; ?>
{% for item in a.items %}
<RichText tag="h2" attribute="items[{{ loop.index0 }}].heading" placeholder="Enter heading" />
{% endfor %}
attribute 값은 repeaterId[index].fieldId 패턴을 따릅니다. 여기서 index는 루프의 0부터 시작하는 행 번호입니다.