跳转到主要内容

富文本

命名空间:core/rich-text。

选择器

getFormatType

根据名称返回格式类型。

用法

import { __, sprintf } from '@wordpress/i18n';
import { store as richTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';

const ExampleComponent = () => {
   const { getFormatType } = useSelect(
       ( select ) => select( richTextStore ),
       []
   );

   const boldFormat = getFormatType( 'core/bold' );

   return boldFormat ? (
       <ul>
           { Object.entries( boldFormat )?.map( ( [ key, value ] ) => (
               <li>
                   { key } : { value }
               </li>
          ) ) }
      </ul>
   ) : (
       __( 'Not Found' )
   ;
};

参数

  • state Object: 数据状态。
  • name string: 格式类型名称。

返回值

  • ?Object: 格式类型。

getFormatTypeForBareElement

Gets the format type, if any, that can handle a bare element (without a data-format-type attribute), given the tag name of this element.

Usage

import { __, sprintf } from '@wordpress/i18n';
import { store as richTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';

const ExampleComponent = () => {
	const { getFormatTypeForBareElement } = useSelect(
		( select ) => select( richTextStore ),
		[]
	);

	const format = getFormatTypeForBareElement( 'strong' );

	return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
};

Parameters

  • state Object: Data state.
  • bareElementTagName string: The tag name of the element to find a format type for.

Returns

  • ?Object: Format type.

getFormatTypeForClassName

Gets the format type, if any, that can handle an element, given its classes.

Usage

import { __, sprintf } from '@wordpress/i18n';
import { store as richTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';

const ExampleComponent = () => {
	const { getFormatTypeForClassName } = useSelect(
		( select ) => select( richTextStore ),
		[]
	);

	const format = getFormatTypeForClassName( 'has-inline-color' );

	return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
};

Parameters

  • state Object: Data state.
  • elementClassName string: The classes of the element to find a format type for.

Returns

  • ?Object: Format type.

getFormatTypes

返回所有可用的格式类型。

用法

import { __, sprintf } from '@wordpress/i18n';
import { store as richTextStore } from '@wordpress/rich-text';
import { useSelect } from '@wordpress/data';

const ExampleComponent = () => {
	const { getFormatTypes } = useSelect(
		( select ) => select( richTextStore ),
		[]
	);

	const availableFormats = getFormatTypes();

	return availableFormats ? (
		<ul>
			{ availableFormats?.map( ( format ) => (
				<li>{ format.name }</li>
			) ) }
		</ul>
	) : (
		__( '无可用格式' )
	);
};

参数

  • state Object: 数据状态。

返回值

  • Array: 格式类型。

操作

暂无内容可记录。