/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/block-library/src/buttons/transforms.js
71 строка
2 KB
Marco Ciampini
ESLint: Replace strict config with bulk suppressions (#81248)
07 авг 2026, 14:34
Не верифицирован
07 авг 2026, 14:34
7f43eaf
Код
Авторство
О чём код?
import { createBlock } from '@wordpress/blocks'; import { __unstableCreateElement as createElement } from '@wordpress/rich-text'; import { getTransformedAttributes } from '../utils/get-transformed-attributes'; const transforms = { from: [ { type: 'block', isMultiBlock: true, blocks: [ 'core/button' ], transform: ( buttons ) => // Creates the buttons block. createBlock( 'core/buttons', {}, // Loop the selected buttons. buttons.map( ( attributes ) => // Create singular button in the buttons block. createBlock( 'core/button', attributes ) ) ), }, { type: 'block', isMultiBlock: true, blocks: [ 'core/paragraph' ], transform: ( buttons ) => // Creates the buttons block. createBlock( 'core/buttons', {}, // Loop the selected buttons. buttons.map( ( attributes ) => { const { content } = attributes; const element = createElement( document, content ); // Remove any HTML tags. const text = element.innerText || ''; // Get first url. const link = element.querySelector( 'a' ); const url = link?.getAttribute( 'href' ); // Create singular button in the buttons block. return createBlock( 'core/button', { ...attributes, ...getTransformedAttributes( attributes, 'core/button', ( { content: contentBinding } ) => ( { text: contentBinding, } ) ), text, url, } ); } ) ), isMatch: ( paragraphs ) => { return paragraphs.every( ( attributes ) => { const element = createElement( document, attributes.content ); const text = element.innerText || ''; const links = element.querySelectorAll( 'a' ); return text.length <= 30 && links.length <= 1; } ); }, }, ], }; export default transforms;