@@ -6,20 +6,50 @@ import { st } from '@/i18n'
66import { api } from '@/scripts/api'
77import type {
88 TemplateGroup ,
9+ TemplateInfo ,
910 WorkflowTemplates
1011} from '@/types/workflowTemplateTypes'
1112import { normalizeI18nKey } from '@/utils/formatUtil'
1213
14+ const SHOULD_SORT_CATEGORIES = new Set ( [
15+ // API Node templates should be strictly sorted by name to avoid any
16+ // favoritism or bias towards a particular API. Other categories can
17+ // have their ordering specified in index.json freely.
18+ 'Image API' ,
19+ 'Video API'
20+ ] )
21+
1322export const useWorkflowTemplatesStore = defineStore (
1423 'workflowTemplates' ,
1524 ( ) => {
1625 const customTemplates = shallowRef < { [ moduleName : string ] : string [ ] } > ( { } )
1726 const coreTemplates = shallowRef < WorkflowTemplates [ ] > ( [ ] )
1827 const isLoaded = ref ( false )
1928
29+ /**
30+ * Sort a list of templates in alphabetical order by name.
31+ */
32+ const sortTemplateList = ( templates : TemplateInfo [ ] ) =>
33+ templates . sort ( ( a , b ) => a . name . localeCompare ( b . name ) )
34+
35+ /**
36+ * Sort any template categories (grouped templates) that should be sorted.
37+ * Leave other categories' templates in their original order specified in index.json
38+ */
39+ const sortCategoryTemplates = ( categories : WorkflowTemplates [ ] ) =>
40+ categories . map ( ( category ) => {
41+ if ( SHOULD_SORT_CATEGORIES . has ( category . title ) ) {
42+ return {
43+ ...category ,
44+ templates : sortTemplateList ( category . templates )
45+ }
46+ }
47+ return category
48+ } )
49+
2050 const groupedTemplates = computed < TemplateGroup [ ] > ( ( ) => {
2151 const allTemplates = [
22- ...coreTemplates . value . map ( ( template ) => ( {
52+ ...sortCategoryTemplates ( coreTemplates . value ) . map ( ( template ) => ( {
2353 ...template ,
2454 title : st (
2555 `templateWorkflows.category.${ normalizeI18nKey ( template . title ) } ` ,
0 commit comments