RTElements

An interface for retrieving and managing rich text elements.

Hooks provide access to rich text elements and related data from inside React components. Unlike data accessed using static methods, hooks will cause the component to rerender when the data changes.

Returns a rich text element by ID or null if the element does not exist.

useRichTextElement<T extends RTElement>(id: string): T | null;
ArgumentTypeDescription
id*stringThe ID of the rich text element to retrieve.

Returns a { [id]: RTElement } map of rich text elements by ID. If a requested element does not exist, it is ommited from the map.

Optionally, the returned elements can be filtered by passing in RTElementFilters. See the RTElementFilters section for filtering options.

useRichTextElements<T extends RTElement>(elementIds: string[], filters?: RTElementFilters): RTElementMap<T>;
ArgumentTypeDescription
elementIds*string[]The IDs of the rich text elements to retrieve.
filtersRTElementFilters

When present, filters the returned elements according to the provided filters. See the RTElementFilters section for filtering options.

Retrieves rich text elements by ID. If provided a single ID string, returns the matching rich text element. If provided an array of IDs, returns a { [id]: RTElement } map of the corresponding rich text elements.

Optionally, when getting multiple rich text elements, the returned elements can be filtered by passing in RTElementFilters. See the RTElementFilters section for filtering options.

Throws a RichTextElementNotFoundError if a requested element does not exist.

// Get a single element
RichTextElements.get<T extends RTElement>(elementId: string): T
// Get multiple elements
RichTextElements.get<T extends RTElement>(
elementId: string[],
filters?: RTElementFilters
): RTElementMap<T>
ArgumentTypeDescription
elementId*string | string[]The ID(s) of the rich text element(s) to retrieve.
filtersRTElementFilters

When present, filters the returned elements according to the provided filters. Only supported when getting multiple elements. See the RTElementFilters section for filtering options.

Retrieves all rich text elements as a { [id]: RTElement } map.

Optionally, the returned elements can by filtered by passing in RTElementFilters. See the RTElementFilters section for filtering options.

RichTextElements.getAll<T extends RTElement>(filters?: RTElementFilters): RTElementMap<T>
ArgumentTypeDescription
filtersRTElementFilters

When present, filters the returned elements according to the provided filters. See the RTElementFilters section for filtering options.t by which to filter the returned

Filters rich text elements according to the provided filters.

The provided elements must be of registered types, or a RichTextElementTypeNotRegisteredError will be thrown.

RichTextElements.filter<T extends RTElement>(
element: RTElementMap<T>,
filters: RTElementFilters
): RTElementMap<T>
ArgumentTypeDescription
elements*RTElementThe rich text elements to filter.
filters*RTElementFiltersFiltering options. See the table below for details.

RTElementFilters

Options for filtering rich text elements.

PropertyTypeDescription
level'block' | 'inline'Filters by the element level, returning only elements of the given level.
typestring[]Filter elements by type, returning only elements which match one of the given types.
voidboolean Filter elements based on whether they are void elements. When true, returns only void elements. When false, returns only non-void elements. If omitted, returns both kinds of elements.
deletedboolean Filters elements based on whether they are deleted. When true, returns only deleted elements. When false, returns only elements which are not deleted. Omit to include both deleted and non-deleted elements.

Creates a new rich text element and dispaches a rich-text-element:create event. Returns the newly created element.

To create an element, the element type must first be registered using the RichTextElements.register method.

If the element has attached files, the IDs of the file references must be listed as the files property. The element will automatically be added as a parent on the attached files.

If the element has nested elements (only supported on 'block' level elements), the element will automatically be added as a parent on those elements.

  • Throws a RichTextElementTypeNotRegisteredError if the element type is not registered.

  • Throws a RichTextElementValidationError if the element contains invalid data or is missing required fields.

RichTextElements.create< TElement extends RTElement, TData extends CreateRTElementData, >(core: Core, data: TData): TElement;
ArgumentTypeDescription
core*CoreA MindDrop core instance.
data*CreateRTElementDataThe rich text element data. See below for details.

CreateRTElementData

In addition to the data below, you can include any custom data required by your rich text element.

PropertyTypeDescription
type*stringThe rich text element type indentifier.
childrenRTFragment The rich text content of the element. See the RTFragment page for details. If omitted on non-void elements, an empty RTNode will be set as the only child. Not supported on void nodes.
filesstring[]The IDs of the file reference for all attached files.
nestedElementsstring[] The IDs of nested RTBlockElements. Only supported for 'block' level elements.

Creates a new rich text element of the given type. Dispatches a rich-text-element:create event and returns the newly created element.

  • Throws a RichTextElementTypeNotRegisteredError if the element type is not registered.

RichTextElements.createOfType< TElement extends RTElement, >(core: Core, type: string, data?: DataInsert | RTFragment): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
type*stringThe element type to create. Must be registered.
dataDataInsert | RTFragment

The data from which to create the element.

If creating a 'block' level element, the data must be aDataInsert.

If creating an 'inline' level element, the data must be aRTFragment.

Updates a rich text element and dispaches a rich-text-element:update event. Returns the updated rich text element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a RichTextElementTypeNotRegistered if the element type is not registered

  • Throws a RichTextElementValidationError if the updated element is invalid.

RichTextElements.update< TElement extends RTElement, TData extends CreateRTElementData, >(core: Core, elementId: string, data: TData): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to update.
data*UpdateRTElementDataThe changes to make to the rich text element. See below for details.

UpdateRTElementData

In addition to the data below, you can include any custom data required by your rich text element.

PropertyTypeDescription
children| RTFragment
| ArrayUnion
| ArrayRemove
The rich text content of the element. See the RTFragment page for details. Only supported on non-void elements.

Deletes a rich text element and dispaches a rich-text-element:delete event. Returns the deleted element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

Deleted rich text elements can be restored using the RichTextElements.restore method.

RichTextElements.delete<TElement extends Element>(core: Core, elementId: string): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to delete.

Restores a deleted rich text element and dispatches a rich-text-element:restore event. Returns the restored element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

RichTextElements.restore< TElement extends RTElement, >(core: Core, elementId: string): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to restore.

Permanently deletes a rich text element and dispatches a rich-text-element:delete-permanently event. Returns the deleted element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

Permanently deleted rich text elements cannot be restored.

RichTextElement.deletePermanently< TElement extends RTElement, >(core: Core, elementId: string): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to delete.

Adds parent references to a rich text element and dispaches a rich-text-element:add-parents event. Returns the updated rich text element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a ParentReferenceValidationError if any of the parent references are invalid.

RichTextElements.addParents< TElement extends RTElement >(core: Core, elementId: string, parents: ParentReference[]): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to which to add the parents.
parents*ParentReference[] The references of the parents to add. See the ParentReference page for details.

Removes parent references from a rich text element and dispaches a rich-text-element:remove-parents event. Returns the updated rich text element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

RichTextElements.removeParents< TElement extends RTElement >(core: Core, elementId: string, parents: ParentReference[]): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element from which to remove the parents.
parents*ParentReference[]The parent references to remove.

Nests rich text block elements into another rich text block element and dispatches a rich-text-element:nest event. Returns the updated element.

The elements is added as a parent on the nested elements.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a RichTextElementValidationError if the element is not a block level element.

  • Throws a RichTextElementValidationError if any of the nested elements do not exist or are not block level elements.

RichTextElements.nest< TElement extends RTBlockElement >(core: Core, elementId: string, nestElementIds: string[]): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element into which to nest the elements.
nestElementIds*string[]The IDs of the elements to nest.

Unnests rich text block elements from another rich text block element and dispatches a rich-text-element:unnest event. Returns the updated element.

The elements is removed as a parent from the nested elements.

  • Throws a RichTextElementNotFoundError if the element or any of the nested elements do not exist.

RichTextElements.unnest< TElement extends RTBlockElement >(core: Core, elementId: string, unnestElementIds: string[]): TElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element from which to unnest the elements.
unnestElementIds*string[]The IDs of the elements to unnest.

Adds files to a rich text element and dispaches a rich-text-element:add-files event. Returns the updated rich text element.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a FileReferenceNotFoundError if any of the file references do not exist.

The element is added as a parent on the file references.

RichTextElements.addFiles(core: Core, elementId: string, fileIds: string[]): RTElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element to which to add the files.
fileIds*string[]The IDs of the files to add.

Removes files from a rich text element and dispatches a rich-text-element:remove-files. Returns the updated rich text element.

The element is removed as a parent from the file references.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a FileReferenceNotFoundError if any of the file references do not exist.

RichTextElements.removeFiles(core: Core, elementId: string, fileIds: string[]): RTElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element from which to remove the files.
fileIds*string[]The IDs of the files to remove.

Replaces files in a rich text element (removing current files and adding given files) and dispatches a rich-text-element:replace-files event. Returns the updated rich text element.

The element will be added as a parent onto the added files, and removed as a parent from the removed files.

  • Throws a RichTextElementNotFoundError if the element does not exist.

  • Throws a FileReferenceNotFoundError if any of the file references do not exist.

RichTextElements.replaceFiles(core: Core, elementId: string, fileIds: string[]): RTElement
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elementId*stringThe ID of the rich text element for which to replace the files.
fileIds*string[]The IDs of the files to add.

Registers a new rich text element type and dispaches a rich-text-element:register event.

RichTextElements.register(
core: Core,
config: RTBlockElementConfig | RTInlineElementConfig,
): void
ArgumentTypeDescription
core*CoreA MindDrop core instance.
config*| RTBlockElementConfig
| RTInlineElementConfig
The config of the rich text element to register. Either a block element type config or an inline element type config.

Unregisters a rich text element type and dispaches a rich-text-element:unregister event.

RichTextElements.unregister(core: Core, type: string): void
ArgumentTypeDescription
core*CoreA MindDrop core instance.
type*stringThe type of the rich text element to unregister.

Converts one or more rich text elements to a plain text string. Void elements are converted using their toPlainText method. If they do not have such a method, they are omited.

  • Throws a RichTextElementNotFoundError if any of the elements' nested elements do not exist.

RichTextElements.toPlainText(element: RTElement | RTElement[]): string
ArgumentTypeDescription
element*RTElement | RTElement[]The rich text element(s) to convert into plain text.

Returns the initialzed custom data for a new rich text element. The data is initialized using the element type config's initializeData method if present, otherwise, returns an empty object.

  • Throws a RichTextElementTypeNotRegistered error if the element type is not registered.

RichTextElements.initializeData<TData = {}>(
type: string,
fromData?: DataInsert | RTFragment
): TData
ArgumentTypeDescription
type*stringThe element type for which to initialize the data.
fromDataDataInsert | RTFragment

The data from which to initialize the element data.

If initializing the data for a 'block' level element, must be a DataInsert.

If initializing the data for an 'inline' level element, must be a RTFragment.

Converts a rich text block element from one type to another. Note that his method does not actually update the element, it simply returns the converted element.

The conversion of an element from one type to another follows these steps:

  1. The default data for the new element type is generated using the element's initializeData method (if defined).

  2. The conversion data is generated using the new element's convertData method (if defined).

  3. The element's custom data fields (if present) are removed.

  4. If converting to a void element type, children is removed. Conversly, if converting from a void element type to a non-void element type, children (consisting of a RTFragment containing the original element's plain text value) is added.

  5. The default data (generated in step 1) is merged into the element.

  6. The conversion data (generated in step 2) is merged into the element.

  7. The element's type is changed to the new type.

  • Throws a RichTextElementTypeNotRegistered if the current or new element type is not registered

  • Throws a InvalidParameterError if the either the element or the new element type are not 'block' level elements.

  • Throws a RichTextElementValidationError if the updated element is invalid.

RichTextElements.convert< TElement extends RTBlockElement, >(element: RTBlockElement, type: string): TElement
ArgumentTypeDescription
element*RTBlockElementThe rich text block element to convert.
type*stringThe rich text block element type into which to convert the element.

Loads rich text elements into the store and dispatches a rich-text-element:load event.

RichTextElements.load(core: Core, element: RTElement[]): void
ArgumentTypeDescription
core*CoreA MindDrop core instance.
elements*RTElementThe rich text elements to load.

Clears all rich text elements from the store. This method is only intended for use in tests, do not use within your extension code.

RichTextElements.clearElements(): void

Clears all registered rich text element configs from the store. This method is only intended for use in tests, do not use within your extension code.

RichTextElements.clearRegistered(): void

See below the table for more details on the data passed by rich text element related events.

NameDataDescription
rich-text-element:createRTElementDispatched when a rich text element is created.
rich-text-element:updateUpdateRTElementEventDataDispatched when a rich text element is updated.
rich-text-element:deleteRTElementDispatched when a rich text element is deleted.
rich-text-element:restoreRTElementDispatched when a deleted rich text element is restored.
rich-text-element:delete-permanentlyRTElementDispatched when a rich text element is permanently deleted.
rich-text-element:add-parentsAddParentsToRTElementEventDataDispatched when parents are added to a rich text element.
rich-text-element:remove-parentsRemoveParentsFromRTElementEventDataDispatched when parents are removed from a rich text element.
rich-text-element:nestNestRTElementEventDataDispatched when rich text elements are nested into another element.
rich-text-element:unnestUnnestRTElementEventDataDispatched when rich text elements are unnested from another element.
rich-text-element:add-filesAddFilesToRTElementEventDataDispatched when files are added to a rich text element.
rich-text-element:remove-filesRemoveFilesFromRTElementEventDataDispatched when files are removed from a rich text element.
rich-text-element:replace-filesReplaceFilesInRTElementEventDataDispatched when a rich text element's files are replaced with new ones.
rich-text-element:registerRTElementConfigDispatched when a new rich text element type is registered.
rich-text-element:unregisterRTElementConfigDispatched when a rich text element type is unregistered.
rich-text-element:loadRTElement[]Dispatched when rich text elements are loaded into the store.

RTElement events are dispatched with one of the following types of data.

RTElement

See the RTElement interface page.

RTElementConfig

See the RTElementConfig interface page.

ParentReference

See the ParentReference interface page.

UpdateRTElementEventData

PropertyTypeDescription
before*RTElementThe rich text element before it was updated.
after*RTElementThe updated rich text element.
changes*RTElementChangesThe changes applied to the rich text element. See below for details.

RTElementChanges

In addition to the data below, the changes data may include custom data according to the rich text element type.

PropertyTypeDescription
updatedAt*DateTimestamp at which the rich text element was updated.
children| RTFragment
| ArrayUnion
| ArrayRemove
The rich text content of the element.
nestedElements| string[]
| ArrayUnion
| ArrayRemove
| Delete
The IDs of nested block level RTElements.
files| string[]
| ArrayUnion
| ArrayRemove
| Delete
The IDs of the element's files. All files attached to the element are listed here.
deletedtrue If true, the rich text element is deleted. Not present if the rich text element is not deleted.
deletedAtDate Timestamp at which the rich text element was deleted. Only set ifdeleted is true.

AddParentsToRTElementEventData

PropertyTypeDescription
element*RTElementThe rich text element to which the parents were added.
parents*ParentReference[]The parent references of the parents added to the element.

RemoveParentsFromRTElementEventData

PropertyTypeDescription
element*RTElementThe rich text element from which the parents were removed.
parents*ParentReference[]The parent references of the parents removed from the element.

NestRTElementEventData

PropertyTypeDescription
element*RTBlockElementThe rich text block element into which the elements were nested.
nestedElements*RTBlockElementMapThe rich text block elements which were nested.

RemoveParentsFromRTElementEventData

PropertyTypeDescription
element*RTBlockElementThe rich text block element from which the elements were unnested.
unnestedElements*RTBlockElementMapThe rich text block elements which were unnested.

AddFilesToRTElementEventData

PropertyTypeDescription
element*RTElementThe rich text element to which the files were added.
files*FileReferenceMapThe file references of the files which were added to the rich text element.

RemoveFilesFromRTElementEventData

PropertyTypeDescription
element*RTElementThe rich text element from which the files were removed.
files*FileReferenceMapThe files references of the files which were removed from the rich text element.

ReplaceFilesInRTElementEventData

PropertyTypeDescription
element*RTElementThe rich text element in which the files were replaced.
addedFiles*FileReferenceMapThe file references of the files which were added to the rich text element.
removedFiles*FileReferenceMapThe files references of the files which were removed from the rich text element.