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;
Argument | Type | Description |
---|---|---|
id* | string | The 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>;
Argument | Type | Description |
---|---|---|
elementIds* | string[] | The IDs of the rich text elements to retrieve. |
filters | RTElementFilters | 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>
Argument | Type | Description |
---|---|---|
elementId* | string | string[] | The ID(s) of the rich text element(s) to retrieve. |
filters | RTElementFilters | 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>
Argument | Type | Description |
---|---|---|
filters | RTElementFilters | 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>
Argument | Type | Description |
---|---|---|
elements* | RTElement | The rich text elements to filter. |
filters* | RTElementFilters | Filtering options. See the table below for details. |
Options for filtering rich text elements.
Property | Type | Description |
---|---|---|
level | 'block' | 'inline' | Filters by the element level, returning only elements of the given level. |
type | string[] | Filter elements by type, returning only elements which match one of the given types. |
void | boolean | 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. |
deleted | boolean | 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;
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
data* | CreateRTElementData | The 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.
Property | Type | Description |
---|---|---|
type* | string | The rich text element type indentifier. |
children | RTFragment | 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. |
files | string[] | The IDs of the file reference for all attached files. |
nestedElements | string[] | The IDs of nested RTBlockElement s. 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
type* | string | The element type to create. Must be registered. |
data | DataInsert | RTFragment | The data from which to create the element. If creating a 'block' level element, the data must be a If creating an 'inline' level element, the data must be a |
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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The ID of the rich text element to update. |
data* | UpdateRTElementData | The 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.
Property | Type | Description |
---|---|---|
children | | RTFragment | 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elementId* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
config* | | RTBlockElementConfig | 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
type* | string | The 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
Argument | Type | Description |
---|---|---|
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
Argument | Type | Description |
---|---|---|
type* | string | The element type for which to initialize the data. |
fromData | DataInsert | RTFragment | The data from which to initialize the element data. If initializing the data for a 'block' level element, must be a If initializing the data for an 'inline' level element, must be a |
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:
The default data for the new element type is generated using the element's initializeData
method (if defined).
The conversion data is generated using the new element's convertData
method (if defined).
The element's custom data fields (if present) are removed.
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.
The default data (generated in step 1) is merged into the element.
The conversion data (generated in step 2) is merged into the element.
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
Argument | Type | Description |
---|---|---|
element* | RTBlockElement | The rich text block element to convert. |
type* | string | The 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
Argument | Type | Description |
---|---|---|
core* | Core | A MindDrop core instance. |
elements* | RTElement | The 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.
Name | Data | Description |
---|---|---|
rich-text-element:create | RTElement | Dispatched when a rich text element is created. |
rich-text-element:update | UpdateRTElementEventData | Dispatched when a rich text element is updated. |
rich-text-element:delete | RTElement | Dispatched when a rich text element is deleted. |
rich-text-element:restore | RTElement | Dispatched when a deleted rich text element is restored. |
rich-text-element:delete-permanently | RTElement | Dispatched when a rich text element is permanently deleted. |
rich-text-element:add-parents | AddParentsToRTElementEventData | Dispatched when parents are added to a rich text element. |
rich-text-element:remove-parents | RemoveParentsFromRTElementEventData | Dispatched when parents are removed from a rich text element. |
rich-text-element:nest | NestRTElementEventData | Dispatched when rich text elements are nested into another element. |
rich-text-element:unnest | UnnestRTElementEventData | Dispatched when rich text elements are unnested from another element. |
rich-text-element:add-files | AddFilesToRTElementEventData | Dispatched when files are added to a rich text element. |
rich-text-element:remove-files | RemoveFilesFromRTElementEventData | Dispatched when files are removed from a rich text element. |
rich-text-element:replace-files | ReplaceFilesInRTElementEventData | Dispatched when a rich text element's files are replaced with new ones. |
rich-text-element:register | RTElementConfig | Dispatched when a new rich text element type is registered. |
rich-text-element:unregister | RTElementConfig | Dispatched when a rich text element type is unregistered. |
rich-text-element:load | RTElement[] | Dispatched when rich text elements are loaded into the store. |
RTElement events are dispatched with one of the following types of data.
See the RTElement interface page.
See the RTElementConfig interface page.
See the ParentReference interface page.
Property | Type | Description |
---|---|---|
before* | RTElement | The rich text element before it was updated. |
after* | RTElement | The updated rich text element. |
changes* | RTElementChanges | The 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.
Property | Type | Description |
---|---|---|
updatedAt* | Date | Timestamp at which the rich text element was updated. |
children | | RTFragment | The rich text content of the element. |
nestedElements | | string[] | The IDs of nested block level RTElement s. |
files | | string[] | The IDs of the element's files. All files attached to the element are listed here. |
deleted | true | If true , the rich text element is deleted. Not present if the rich text element is not deleted. |
deletedAt | Date | Timestamp at which the rich text element was deleted. Only set ifdeleted is true . |
Property | Type | Description |
---|---|---|
element* | RTElement | The rich text element to which the parents were added. |
parents* | ParentReference[] | The parent references of the parents added to the element. |
Property | Type | Description |
---|---|---|
element* | RTElement | The rich text element from which the parents were removed. |
parents* | ParentReference[] | The parent references of the parents removed from the element. |
Property | Type | Description |
---|---|---|
element* | RTBlockElement | The rich text block element into which the elements were nested. |
nestedElements* | RTBlockElementMap | The rich text block elements which were nested. |
Property | Type | Description |
---|---|---|
element* | RTBlockElement | The rich text block element from which the elements were unnested. |
unnestedElements* | RTBlockElementMap | The rich text block elements which were unnested. |
Property | Type | Description |
---|---|---|
element* | RTElement | The rich text element to which the files were added. |
files* | FileReferenceMap | The file references of the files which were added to the rich text element. |
Property | Type | Description |
---|---|---|
element* | RTElement | The rich text element from which the files were removed. |
files* | FileReferenceMap | The files references of the files which were removed from the rich text element. |
Property | Type | Description |
---|---|---|
element* | RTElement | The rich text element in which the files were replaced. |
addedFiles* | FileReferenceMap | The file references of the files which were added to the rich text element. |
removedFiles* | FileReferenceMap | The files references of the files which were removed from the rich text element. |