/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
packages/block-editor/src/utils/selection.ts
45 строк
1 KB
Marco Ciampini
ESLint: Replace strict config with bulk suppressions (#81248)
07 авг 2026, 14:34
Не верифицирован
07 авг 2026, 14:34
7f43eaf
Код
Авторство
О чём код?
import { RichTextData } from '@wordpress/rich-text'; /** * A robust way to retain selection position through various * transforms is to insert a special character at the position and * then recover it. */ export const START_OF_SELECTED_AREA = '\u0086'; /** * Retrieve the block attribute that contains the selection position. * * @param blockAttributes Block attributes. * @return The name of the block attribute that was previously selected. */ export function retrieveSelectedAttribute( blockAttributes: Record< string, unknown > | undefined ) { if ( ! blockAttributes ) { return; } return Object.keys( blockAttributes ).find( ( name ) => { const value = blockAttributes[ name ]; return ( ( typeof value === 'string' || value instanceof RichTextData ) && // To do: refactor this to use rich text's selection instead, so we // no longer have to use on this hack inserting a special character. value.toString().indexOf( START_OF_SELECTED_AREA ) !== -1 ); } ); } type BlockTypeWithAttributes = { attributes: Record< string, { source?: string } >; }; export function findRichTextAttributeKey( blockType: BlockTypeWithAttributes ) { for ( const [ key, value ] of Object.entries( blockType.attributes ) ) { if ( value.source === 'rich-text' || value.source === 'html' ) { return key; } } return undefined; }