Skip to content

Commit f8ef74d

Browse files
committed
Refactored the extension grouping
to use `Object.groupBy` Also added sorting, to show the extensions in alphabetical order per group
1 parent 6483425 commit f8ef74d

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/packages/rte/tiptap/property-editors/property-editor-ui-tiptap-extensions-configuration.element.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ export class UmbPropertyEditorUiTiptapExtensionsConfigurationElement
5656
super.firstUpdated(_changedProperties);
5757

5858
this.observe(umbExtensionsRegistry.byType('tiptapExtension'), (extensions) => {
59-
this._extensionConfigs = extensions.map((ext) => {
60-
return {
61-
alias: ext.alias,
62-
label: ext.meta.label,
63-
icon: ext.meta.icon,
64-
group: ext.meta.group,
65-
};
66-
});
59+
this._extensionConfigs = extensions
60+
.sort((a, b) => a.alias.localeCompare(b.alias))
61+
.map((ext) => {
62+
return {
63+
alias: ext.alias,
64+
label: ext.meta.label,
65+
icon: ext.meta.icon,
66+
group: ext.meta.group,
67+
};
68+
});
6769

6870
if (!this.value) {
6971
// The default value is all extensions enabled
@@ -84,18 +86,16 @@ export class UmbPropertyEditorUiTiptapExtensionsConfigurationElement
8486
};
8587
});
8688

87-
const grouped = withSelectedProperty.reduce((acc: any, item) => {
88-
const group = item.group || 'Uncategorized'; // Assign to "Uncategorized" if no group
89-
if (!acc[group]) {
90-
acc[group] = [];
91-
}
92-
acc[group].push(item);
93-
return acc;
94-
}, {});
95-
this._extensionCategories = Object.keys(grouped).map((group) => ({
96-
category: group.charAt(0).toUpperCase() + group.slice(1).replace(/-/g, ' '),
97-
extensions: grouped[group],
98-
}));
89+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
90+
// @ts-expect-error
91+
const grouped = Object.groupBy(withSelectedProperty, (item: ExtensionConfig) => item.group || 'Uncategorized');
92+
93+
this._extensionCategories = Object.keys(grouped)
94+
.sort((a, b) => a.localeCompare(b))
95+
.map((key) => ({
96+
category: key,
97+
extensions: grouped[key],
98+
}));
9999
}
100100

101101
#onExtensionClick(item: ExtensionCategoryItem) {

0 commit comments

Comments
 (0)