Skip to content

Commit

Permalink
Allow to specify which list style types are shown in list type select…
Browse files Browse the repository at this point in the history
…or dropdown.
  • Loading branch information
Mati365 committed Jan 10, 2025
1 parent 55ed74d commit 47193c9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
44 changes: 44 additions & 0 deletions packages/ckeditor5-list/src/listconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@ export interface ListPropertiesStyleConfig {
* @default false
*/
useAttribute?: boolean;

/**
* Defines which list styles should be available in the UI.
* Accepts an array of style names or `true` to enable all styles.
*
* Available styles for numbered lists:
* - 'decimal'
* - 'decimal-leading-zero'
* - 'lower-roman'
* - 'upper-roman'
* - 'lower-latin'
* - 'upper-latin'
*
* Available styles for bulleted lists:
* - 'disc'
* - 'circle'
* - 'square'
*
* ```ts
* {
* list: {
* properties: {
* styles: {
* listStyleTypes: [ 'decimal', 'lower-roman', 'upper-roman' ]
* }
* }
* }
* }
* ```
*
* @default ['decimal','decimal-leading-zero','lower-roman','upper-roman','lower-latin','upper-latin','disc','circle','square']
*/
listStyleTypes?: Array<ListPropertiesStyleListStyleType>;
}

export type ListPropertiesStyleListType = 'numbered' | 'bulleted';

export type ListPropertiesStyleListStyleType =
| 'decimal'
| 'decimal-leading-zero'
| 'lower-roman'
| 'upper-roman'
| 'lower-latin'
| 'upper-latin'
| 'disc'
| 'circle'
| 'square';
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function createAttributeStrategies( enabledProperties: ListPropertiesConfig ) {

if ( enabledProperties.styles ) {
const useAttribute = normalizedConfig.styles.useAttribute;
const allowedListStyleTypes = normalizedConfig.styles.listStyleTypes;

strategies.push( {
attributeName: 'listStyle',
Expand All @@ -282,6 +283,10 @@ function createAttributeStrategies( enabledProperties: ListPropertiesConfig ) {
supportedTypes = supportedTypes.filter( styleType => !!getTypeAttributeFromListStyleType( styleType ) );
}

if ( Array.isArray( allowedListStyleTypes ) ) {
supportedTypes = supportedTypes.filter( styleType => allowedListStyleTypes.includes( styleType ) );
}

editor.commands.add( 'listStyle', new ListStyleCommand( editor, DEFAULT_LIST_TYPE, supportedTypes ) );
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ function getDropdownViewCreator( {
styleDefinitions: Array<StyleDefinition>;
} ) {
const parentCommand = editor.commands.get( parentCommandName )!;
const allowedListStyleTypes = normalizedConfig.styles.listStyleTypes;

// Filter style definitions if specific styles are configured
const filteredStyleDefinitions = Array.isArray( allowedListStyleTypes ) ?
styleDefinitions.filter( def => allowedListStyleTypes.includes( def.type ) ) :
styleDefinitions;

return ( locale: Locale ) => {
const dropdownView = createDropdown( locale, SplitButtonView );
Expand Down Expand Up @@ -256,7 +262,7 @@ function getDropdownViewCreator( {
dropdownView,
parentCommandName,
styleGridAriaLabel,
styleDefinitions
styleDefinitions: filteredStyleDefinitions
} );

dropdownView.panelView.children.add( listPropertiesView );
Expand Down
6 changes: 4 additions & 2 deletions packages/ckeditor5-list/src/listproperties/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function getNormalizedConfig( config: ListPropertiesConfig ): NormalizedL
* @returns An object with normalized list properties styles.
*/
function getNormalizedStylesConfig( styles: ListPropertiesConfig[ 'styles' ] ): NormalizedListPropertiesConfig[ 'styles' ] {
const normalizedConfig = {
listTypes: [ 'bulleted', 'numbered' ] as Array<ListPropertiesStyleListType>,
const normalizedConfig: NormalizedListPropertiesConfig['styles'] = {
listTypes: [ 'bulleted', 'numbered' ],
useAttribute: false
};

Expand All @@ -74,6 +74,7 @@ function getNormalizedStylesConfig( styles: ListPropertiesConfig[ 'styles' ] ):
normalizedConfig.listTypes;

normalizedConfig.useAttribute = !!styles.useAttribute;
normalizedConfig.listStyleTypes = styles.listStyleTypes;
}

return normalizedConfig;
Expand All @@ -85,6 +86,7 @@ function getNormalizedStylesConfig( styles: ListPropertiesConfig[ 'styles' ] ):
export type NormalizedListPropertiesConfig = {
styles: {
listTypes: Array<ListPropertiesStyleListType>;
listStyleTypes?: Array<string>;
useAttribute: boolean;
};
startIndex: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/ckeditor5-list/tests/manual/list-properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ <h2>Just styles</h2>
<h2>No properties enabled</h2>

<div id="editor-h"></div>

<h2>Hidden lower-latin and upper-latin styles</h2>

<div id="editor-i"></div>
30 changes: 30 additions & 0 deletions packages/ckeditor5-list/tests/manual/list-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,33 @@ ClassicEditor
.catch( err => {
console.error( err.stack );
} );

// ------------------------------------------------------------------

ClassicEditor
.create( document.querySelector( '#editor-i' ), {
...config,
list: {
properties: {
styles: {
listStyleTypes: [
'decimal',
'decimal-leading-zero',
'lower-roman',
'upper-roman',
// 'lower-latin',
// 'upper-latin',
'disc',
'circle',
'square'
]
}
}
}
} )
.then( editor => {
CKEditorInspector.attach( { 'No properties enabled': editor } );
} )
.catch( err => {
console.error( err.stack );
} );

0 comments on commit 47193c9

Please sign in to comment.