Hoppa till innehåll

RichText

RichText-redigeringar sparas omedelbart i blockets attribut och ingår i Gutenbergs historik för ångra och gör om, även RichText i repeaterader. När du sparar med flera markerade block bevaras varje blocks eget innehåll och dess attribut.

Komponenten RichText återger ett redigerbart inmatningsfält i redigeraren och ett statiskt HTML-element i frontend. Den bygger på komponenten RichText som tillhandahålls av WordPress.

Grundläggande användning

För att använda RichText i sin enklaste form behöver du lägga till lämpligt attribut i din block.json och lägga till komponenten RichText i ditt block.

JSON
{
  "$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"
      }
    ]
  }
}
PHP
<RichText attribute="myRichText" />

Du kan använda ett obegränsat antal RichText-komponenter i ditt block.

Egenskaper

allowedFormats

Du kan begränsa vilka format som får användas i RichText-komponenten med egenskapen allowedFormats.

PHP

PHP
<RichText attribute="myRichText" allowedFormats="<?php echo esc_attr(wp_json_encode(['core/bold', 'core/italic'])); ?>" />

Twig

Twig
<RichText attribute="myRichText" allowedFormats="{{ ['core/bold', 'core/italic']|json_encode|escape('html_attr') }}" />

platshållare

Du kan ange en platshållare för RichText-komponenten med egenskapen placeholder.

PHP
<RichText attribute="myRichText" placeholder="Enter some text" />

preserveWhiteSpace

Om blankstegstecken ska bevaras i värdet. Normalt slås tabb-, radbrytnings- och mellanslagstecken ihop till ett enda mellanslag. Om alternativet aktiveras sparas mjuka radbrytningar som radbrytningstecken, inte som radbrytningselement.

PHP
<RichText attribute="myRichText" preserveWhiteSpace="true" />

tagg

Som standard återges innehållet RichText inuti ett element p. Du kan ändra detta med egenskapen tag.

PHP
<RichText attribute="myRichText" tag="h1" />

withoutInteractiveFormatting

Som standard visas alla formateringskontroller. Den här inställningen kan användas för att ta bort formateringskontroller som gör innehållet interaktivt.

PHP
<RichText attribute="myRichText" withoutInteractiveFormatting="true" />

Repeaterfält

RichText fungerar i repeaterfält. Använd hakparentesnotation med loopens index för att välja rätt rad.

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "items",
        "type": "repeater",
        "attributes": [
          {
            "id": "heading",
            "type": "richtext"
          }
        ]
      }
    ]
  }
}
PHP
<?php foreach ($a['items'] as $index => $item) : ?>
  <RichText tag="h2" attribute="items[<?php echo $index; ?>].heading" placeholder="Enter heading" />
<?php endforeach; ?>
Twig
{% for item in a.items %}
  <RichText tag="h2" attribute="items[{{ loop.index0 }}].heading" placeholder="Enter heading" />
{% endfor %}

Värdet attribute följer mönstret repeaterId[index].fieldId, där index är det nollbaserade radnumret från loopen.