Skip to content

Commit

Permalink
fix(core): set default sort of category to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Oct 24, 2023
1 parent 0f21bb5 commit c251e82
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/a-lib/dialog/doc/en-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
order: 2
---
3 changes: 3 additions & 0 deletions packages/a-lib/dialog/doc/zh-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
order: 0
---
6 changes: 6 additions & 0 deletions packages/a-lib/foo/doc/en-us.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: general
title: Foo
subtitle: 测试
order: 10
---
2 changes: 1 addition & 1 deletion packages/a-lib/foo/doc/zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
category: general
title: Foo
subtitle: 测试
order: 2
order: 10
---

<alert>Foo 组件,测试组件库文档的示例组件。</alert>
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/builders/library-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ describe('#library-builder', () => {
const localeCategoriesMap = libraryBuilder['localeCategoriesMap'];
expect(localeCategoriesMap).toEqual({
'zh-cn': [
{ id: 'general', title: '通用', subtitle: undefined, items: [] },
{ id: 'layout', title: '布局', subtitle: undefined, items: [] }
{ id: 'general', title: '通用', subtitle: undefined, items: [], order: 0 },
{ id: 'layout', title: '布局', subtitle: undefined, items: [], order: 0 }
],
'en-us': [
{ id: 'general', title: 'General', subtitle: undefined, items: [] },
{ id: 'layout', title: 'Layout', subtitle: undefined, items: [] }
{ id: 'general', title: 'General', subtitle: undefined, items: [], order: 0 },
{ id: 'layout', title: 'Layout', subtitle: undefined, items: [], order: 0 }
]
});
});
Expand Down Expand Up @@ -253,8 +253,8 @@ describe('#library-builder', () => {
const zhNavs = libraryBuilder.generateDocsAndNavsForLocale('zh-cn', rootNavs);
expect(zhNavs).toEqual([componentDocItems.button, componentDocItems.alert]);
expect(rootNavs[0].items as unknown).toEqual([
{ id: 'general', title: '通用', items: [componentDocItems.button] },
{ id: 'layout', title: '布局', items: [] },
{ id: 'general', title: '通用', items: [componentDocItems.button], order: 0 },
{ id: 'layout', title: '布局', items: [], order: 0 },
{ id: 'alib/alert', path: 'alert', title: 'Alert', channelPath: 'components' }
]);
});
Expand Down Expand Up @@ -324,7 +324,7 @@ describe('#library-builder', () => {
}
];
const zhNavs = libraryBuilder.generateDocsAndNavsForLocale('zh-cn', rootNavs);
expect(zhNavs).toEqual([componentDocItems.alert, componentDocItems.button]);
expect(zhNavs).toEqual([componentDocItems.button, componentDocItems.alert]);
expect(rootNavs[0].items).toEqual([
{ id: 'alib/alert', path: 'alert', title: 'Alert', channelPath: 'components', order: 10 },
{
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/builders/library-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class LibraryBuilderImpl extends FileEmitter implements LibraryBuilder {
}
});
channel.items = ascendingSortByOrder(channel.items);
return ascendingSortByOrder(docItems);
return docItems;
}

private async buildComponent(component: LibraryComponent) {
Expand All @@ -203,7 +203,8 @@ export class LibraryBuilderImpl extends FileEmitter implements LibraryBuilder {
id: rawCategory.id,
title: getItemLocaleProperty(rawCategory, locale.key, 'title'),
subtitle: getItemLocaleProperty(rawCategory, locale.key, 'subtitle'),
items: []
items: [],
order: 0
};
localeCategories[locale.key].push(category);
});
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ export function highlight(sourceCode: string, lang: string) {

export function ascendingSortByOrder<T extends { order?: number }>(items: T[]) {
return items.sort((a, b) => {
return a.order > b.order ? 1 : a.order === b.order ? 0 : -1;
if (toolkit.utils.isNumber(a.order) && toolkit.utils.isUndefinedOrNull(b.order)) {
return -1;
} else if (toolkit.utils.isNumber(b.order) && toolkit.utils.isUndefinedOrNull(a.order)) {
return 1;
} else if (toolkit.utils.isUndefinedOrNull(a.order) && toolkit.utils.isUndefinedOrNull(b.order)) {
return 0;
} else {
return a.order > b.order ? 1 : a.order === b.order ? 0 : -1;
}
});
}

Expand Down

0 comments on commit c251e82

Please sign in to comment.