schlechtenburg/packages/rich-text/lib/insert-object.ts
2024-10-08 09:15:26 +02:00

30 lines
1 KiB
TypeScript

/**
* Internal dependencies
*/
import { insert } from './insert';
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
import { RichTextFormat, RichTextValue } from './types';
/**
* Insert a format as an object into a Rich Text value at the given
* `startIndex`. Any content between `startIndex` and `endIndex` will be
* removed. Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} formatToInsert Format to insert as object.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the object inserted.
*/
export function insertObject( value: RichTextValue, formatToInsert: RichTextFormat, startIndex: number, endIndex: number ) {
const valueToInsert = {
formats: [ , ],
replacements: [ formatToInsert ],
text: OBJECT_REPLACEMENT_CHARACTER,
};
return insert( value, valueToInsert, startIndex, endIndex );
}