SuggestionDescriptionFactory
Creates descriptions for suggestions and suggestions chains.
The class manages everything related to generating descriptions for suggestions. Here, you register labels for elements, attributes, or custom callback for non-regular cases.
One or more suggestions that are grouped together (based ond various conditions) create "suggestion chain". In such chain one suggestion may impact other suggestion when it comes to the description of the whole chain. For example, insertion next to deletion results in "Replaced" description.
Methods
constructor( schema, locale )module:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#constructorParameters
schema : ModelSchemalocale : Locale
getDescriptions( suggestions ) → Array<SuggestionDescription>module:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#getDescriptionsReturns descriptions for given suggestion chain.
The structure of the descriptions array is as follows (explained on an example):
[ { type: 'insertion', content: '*Insert:* 2 paragraphs' }, { type: 'insertion', content: '*Insert:* image }, { type: 'replace', content: '*Replace:* "Foo" *with* "Bar"' } ]Copy codeIn above example there are three description instances (or lines). Two new (empty) paragraphs were added, an image was added and then "Foo" text was removed and "Bar" text was added. For example, above structure could be rendered as:
<p><strong>Insert:</strong> 2 paragraphs</p> <p><strong>Insert:</strong> image</p> <p><strong>Replace:</strong> "Foo" <strong>with</strong> "Bar"</p>Copy codeParameters
suggestions : Array<Suggestion>
Returns
Array<SuggestionDescription>
getItemLabel( element, quantity ) → stringmodule:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#getItemLabelReturns label registered for given element or the element name if there is no label registered for it.
Parameters
element : ModelElementquantity : numberDefaults to
1
Returns
string
registerAttributeLabel( attributeName, attributeLabel ) → voidmodule:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#registerAttributeLabelFor given
attributeNameregisters how this attribute will be labeled in a description (for example when it is added or removed).Example usage with internationalization:
const t = editor.locale.t; // Remember that you have to use function named `t`. suggestionDescriptionFactory.registerAttributeLabel( 'bold', t( 'bold' ) );Copy codeParameters
attributeName : stringattributeLabel : string
Returns
void
registerDescriptionCallback( callback ) → voidmodule:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#registerDescriptionCallbackRegisters a callback function that returns a custom description for a suggestion.
Registered callback is fired for a suggestion whenever there is a need to generate a description for that suggestion.
The callback takes the suggestion instance as a parameter and should return description object or a falsy value if the suggestion was not handled by the callback.
Example of a description callback for the bold style:
suggestionDescriptionFactory.registerDescriptionCallback( suggestion => { const { data } = suggestion; // Omit suggestions that are not bold style suggestions. if ( !data || data.commandName !== 'bold' ) { return; } const isSet = !!data.commandParams[ 0 ].forceValue; const content = isSet ? '*Set format:* bold' : '*Remove format:* bold'; return { type: 'format', content }; } );Copy codeParameters
callback : SuggestionDescriptionCallback
Returns
void
registerElementLabel( elementNameOrCallback, labelCallback, priority ) → voidmodule:track-changes/suggestiondescriptionfactory~SuggestionDescriptionFactory#registerElementLabelFor given
elementNameregisters how this element will be labeled in a description (for example when it is added or removed).Instead of a string name you may provide a matching function that takes item
ModelItemas an input and should return boolean value.Provided label callback takes one parameter,
quantity, and is expected to return the label for the element as a string.A simple use case without using internationalization:
suggestionDescriptionFactory.registerElementLabel( 'paragraph', quantity => quantity == 1 ? 'paragraph' : quantity + ' paragraphs' );Copy codeIf you want your feature to be localized to other languages, use localization service:
const t = editor.locale.t; // Remember that you have to use function named `t`. suggestionDescriptionFactory.registerElementLabel( 'paragraph', quantity => t( { string: 'paragraph', plural: '%0 paragraphs', id: 'ELEMENT_PARAGRAPH' }, quantity ) );Copy codeParameters
elementNameOrCallback : string | FunctionlabelCallback : SuggestionLabelCallbackpriority : PriorityStringDefaults to
priorities.normal
Returns
void